C027Interface
Fork of C027Interface by
Diff: C027Interface.cpp
- Revision:
- 12:181a9415736b
- Child:
- 14:3d1845f5cd81
diff -r 82796df87b0a -r 181a9415736b C027Interface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C027Interface.cpp Fri Apr 15 16:06:47 2016 +0000 @@ -0,0 +1,177 @@ +/* C027 implementation of NetworkInterfaceAPI + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "C027Interface.h" +#include "mbed.h" + + +static void ipToString(MDMParser::IP ip, char *buffer) { + sprintf(buffer, IPSTR, IPNUM(ip)); +} + + +// C027Interface implementation +C027Interface::C027Interface(const char *simpin, bool debug) + : _debug(debug) +{ + strcpy(_pin, simpin); +} + +int32_t C027Interface::connect(const char *apn, const char *username, const char *password) +{ + // create the modem + _mdm = new MDMSerial; + if (_debug) { + _mdm->setDebug(4); + } else { + _mdm->setDebug(0); + } + + // initialize the modem + MDMParser::DevStatus devStatus = {}; + MDMParser::NetStatus netStatus = {}; + bool mdmOk = _mdm->init(_pin, &devStatus); + if (_debug) { + _mdm->dumpDevStatus(&devStatus); + } + + if (mdmOk) { + // wait until we are connected + mdmOk = _mdm->registerNet(&netStatus); + if (_debug) { + _mdm->dumpNetStatus(&netStatus); + } + } + + if (mdmOk) + { + // join the internet connection + MDMParser::IP ip = _mdm->join(apn, username, password); + ipToString(ip, _ip_address); + mdmOk = (ip != NOIP); + } + + return mdmOk ? 0 : NS_ERROR_DEVICE_ERROR; +} + +int32_t C027Interface::disconnect() +{ + if (!_mdm->disconnect()) { + return NS_ERROR_DEVICE_ERROR; + } + + return 0; +} + +const char *C027Interface::getIPAddress() +{ + return _ip_address; +} + +const char *C027Interface::getMACAddress() +{ + return 0; +} + +SocketInterface *C027Interface::createSocket(ns_protocol_t proto) +{ + MDMParser::IpProtocol mdmproto = (proto == NS_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP; + int socket = _mdm->socketSocket(mdmproto); + if (socket < 0) { + return 0; + } + + _mdm->socketSetBlocking(socket, 10000); + + return new C027Socket(_mdm, socket, mdmproto); +} + +void C027Interface::destroySocket(SocketInterface *siface) +{ + C027Socket *socket = (C027Socket *)siface; + _mdm->socketFree(socket->_socket); + + delete socket; +} + + +// TCP SocketInterface implementation +int32_t C027Interface::C027Socket::open(const char *ip, uint16_t port) +{ + _ip = _mdm->gethostbyname(ip); + _port = port; + + if (_proto == MDMParser::IPPROTO_TCP) { + if (!_mdm->socketConnect(_socket, ip, port)) { + return NS_ERROR_DEVICE_ERROR; + } + } + + return 0; +} + +int32_t C027Interface::C027Socket::close() +{ + if (_proto == MDMParser::IPPROTO_TCP) { + if (!_mdm->socketClose(_socket)) { + return NS_ERROR_DEVICE_ERROR; + } + } + + return 0; +} + +int32_t C027Interface::C027Socket::send(const void *data, uint32_t size) +{ + int sent; + + if (_proto == MDMParser::IPPROTO_TCP) { + sent = _mdm->socketSend(_socket, (const char *)data, size); + } else { + sent = _mdm->socketSendTo(_socket, _ip, _port, (const char *)data, size); + } + + if (sent != size) { + return NS_ERROR_DEVICE_ERROR; + } + + return 0; +} + +int32_t C027Interface::C027Socket::recv(void *data, uint32_t size) +{ + if (!_mdm->socketReadable(_socket)) { + return NS_ERROR_WOULD_BLOCK; + } + + int recv; + + if (_proto == MDMParser::IPPROTO_TCP) { + recv = _mdm->socketRecv(_socket, (char *)data, size); + } else { + MDMParser::IP ip; + int port; + recv = _mdm->socketRecvFrom(_socket, &ip, &port, (char *)data, size); + } + + if (recv == SOCKET_ERROR) { + return NS_ERROR_DEVICE_ERROR; + } + + return recv; +} + +