Socket interface for C027Interface. Implements the NetworkSocketAPI
Dependencies: C027_Support
Dependents: HelloC027Interface U_Blox_DeviceConnector U_Blox_DeviceConnector U-Blox_Client
Fork of LWIPInterface by
C027Interface.cpp@14:3d1845f5cd81, 2016-04-21 (annotated)
- Committer:
- Christopher Haster
- Date:
- Thu Apr 21 06:53:31 2016 -0500
- Revision:
- 14:3d1845f5cd81
- Parent:
- 12:181a9415736b
- Child:
- 15:21d4f56eb172
Match changes to the NSAPI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geky | 12:181a9415736b | 1 | /* C027 implementation of NetworkInterfaceAPI |
geky | 12:181a9415736b | 2 | * Copyright (c) 2015 ARM Limited |
geky | 12:181a9415736b | 3 | * |
geky | 12:181a9415736b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
geky | 12:181a9415736b | 5 | * you may not use this file except in compliance with the License. |
geky | 12:181a9415736b | 6 | * You may obtain a copy of the License at |
geky | 12:181a9415736b | 7 | * |
geky | 12:181a9415736b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
geky | 12:181a9415736b | 9 | * |
geky | 12:181a9415736b | 10 | * Unless required by applicable law or agreed to in writing, software |
geky | 12:181a9415736b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
geky | 12:181a9415736b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
geky | 12:181a9415736b | 13 | * See the License for the specific language governing permissions and |
geky | 12:181a9415736b | 14 | * limitations under the License. |
geky | 12:181a9415736b | 15 | */ |
geky | 12:181a9415736b | 16 | |
geky | 12:181a9415736b | 17 | #include "C027Interface.h" |
geky | 12:181a9415736b | 18 | #include "mbed.h" |
geky | 12:181a9415736b | 19 | |
geky | 12:181a9415736b | 20 | |
geky | 12:181a9415736b | 21 | static void ipToString(MDMParser::IP ip, char *buffer) { |
geky | 12:181a9415736b | 22 | sprintf(buffer, IPSTR, IPNUM(ip)); |
geky | 12:181a9415736b | 23 | } |
geky | 12:181a9415736b | 24 | |
geky | 12:181a9415736b | 25 | |
geky | 12:181a9415736b | 26 | // C027Interface implementation |
geky | 12:181a9415736b | 27 | C027Interface::C027Interface(const char *simpin, bool debug) |
geky | 12:181a9415736b | 28 | : _debug(debug) |
geky | 12:181a9415736b | 29 | { |
geky | 12:181a9415736b | 30 | strcpy(_pin, simpin); |
geky | 12:181a9415736b | 31 | } |
geky | 12:181a9415736b | 32 | |
Christopher Haster |
14:3d1845f5cd81 | 33 | int C027Interface::connect(const char *apn, const char *username, const char *password) |
geky | 12:181a9415736b | 34 | { |
geky | 12:181a9415736b | 35 | // create the modem |
geky | 12:181a9415736b | 36 | _mdm = new MDMSerial; |
geky | 12:181a9415736b | 37 | if (_debug) { |
geky | 12:181a9415736b | 38 | _mdm->setDebug(4); |
geky | 12:181a9415736b | 39 | } else { |
geky | 12:181a9415736b | 40 | _mdm->setDebug(0); |
geky | 12:181a9415736b | 41 | } |
geky | 12:181a9415736b | 42 | |
geky | 12:181a9415736b | 43 | // initialize the modem |
geky | 12:181a9415736b | 44 | MDMParser::DevStatus devStatus = {}; |
geky | 12:181a9415736b | 45 | MDMParser::NetStatus netStatus = {}; |
geky | 12:181a9415736b | 46 | bool mdmOk = _mdm->init(_pin, &devStatus); |
geky | 12:181a9415736b | 47 | if (_debug) { |
geky | 12:181a9415736b | 48 | _mdm->dumpDevStatus(&devStatus); |
geky | 12:181a9415736b | 49 | } |
geky | 12:181a9415736b | 50 | |
geky | 12:181a9415736b | 51 | if (mdmOk) { |
geky | 12:181a9415736b | 52 | // wait until we are connected |
geky | 12:181a9415736b | 53 | mdmOk = _mdm->registerNet(&netStatus); |
geky | 12:181a9415736b | 54 | if (_debug) { |
geky | 12:181a9415736b | 55 | _mdm->dumpNetStatus(&netStatus); |
geky | 12:181a9415736b | 56 | } |
geky | 12:181a9415736b | 57 | } |
geky | 12:181a9415736b | 58 | |
Christopher Haster |
14:3d1845f5cd81 | 59 | if (mdmOk) { |
geky | 12:181a9415736b | 60 | // join the internet connection |
geky | 12:181a9415736b | 61 | MDMParser::IP ip = _mdm->join(apn, username, password); |
geky | 12:181a9415736b | 62 | ipToString(ip, _ip_address); |
geky | 12:181a9415736b | 63 | mdmOk = (ip != NOIP); |
geky | 12:181a9415736b | 64 | } |
geky | 12:181a9415736b | 65 | |
Christopher Haster |
14:3d1845f5cd81 | 66 | return mdmOk ? 0 : NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 67 | } |
geky | 12:181a9415736b | 68 | |
Christopher Haster |
14:3d1845f5cd81 | 69 | int C027Interface::disconnect() |
geky | 12:181a9415736b | 70 | { |
geky | 12:181a9415736b | 71 | if (!_mdm->disconnect()) { |
Christopher Haster |
14:3d1845f5cd81 | 72 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 73 | } |
geky | 12:181a9415736b | 74 | |
geky | 12:181a9415736b | 75 | return 0; |
geky | 12:181a9415736b | 76 | } |
geky | 12:181a9415736b | 77 | |
Christopher Haster |
14:3d1845f5cd81 | 78 | const char *C027Interface::get_ip_address() |
geky | 12:181a9415736b | 79 | { |
geky | 12:181a9415736b | 80 | return _ip_address; |
geky | 12:181a9415736b | 81 | } |
geky | 12:181a9415736b | 82 | |
Christopher Haster |
14:3d1845f5cd81 | 83 | const char *C027Interface::get_mac_address() |
geky | 12:181a9415736b | 84 | { |
geky | 12:181a9415736b | 85 | return 0; |
geky | 12:181a9415736b | 86 | } |
geky | 12:181a9415736b | 87 | |
Christopher Haster |
14:3d1845f5cd81 | 88 | struct c027_socket { |
Christopher Haster |
14:3d1845f5cd81 | 89 | int socket; |
Christopher Haster |
14:3d1845f5cd81 | 90 | MDMParser::IpProtocol proto; |
Christopher Haster |
14:3d1845f5cd81 | 91 | MDMParser::IP ip; |
Christopher Haster |
14:3d1845f5cd81 | 92 | int port; |
Christopher Haster |
14:3d1845f5cd81 | 93 | }; |
Christopher Haster |
14:3d1845f5cd81 | 94 | |
Christopher Haster |
14:3d1845f5cd81 | 95 | int C027Interface::socket_open(void **handle, nsapi_protocol_t proto) |
geky | 12:181a9415736b | 96 | { |
Christopher Haster |
14:3d1845f5cd81 | 97 | MDMParser::IpProtocol mdmproto = (proto == NSAPI_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP; |
Christopher Haster |
14:3d1845f5cd81 | 98 | int fd = _mdm->socketSocket(mdmproto); |
Christopher Haster |
14:3d1845f5cd81 | 99 | if (fd < 0) { |
Christopher Haster |
14:3d1845f5cd81 | 100 | return NSAPI_ERROR_NO_SOCKET; |
geky | 12:181a9415736b | 101 | } |
geky | 12:181a9415736b | 102 | |
Christopher Haster |
14:3d1845f5cd81 | 103 | _mdm->socketSetBlocking(fd, 10000); |
Christopher Haster |
14:3d1845f5cd81 | 104 | struct c027_socket *socket = new struct c027_socket; |
Christopher Haster |
14:3d1845f5cd81 | 105 | if (!socket) { |
Christopher Haster |
14:3d1845f5cd81 | 106 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
14:3d1845f5cd81 | 107 | } |
geky | 12:181a9415736b | 108 | |
Christopher Haster |
14:3d1845f5cd81 | 109 | socket->socket = fd; |
Christopher Haster |
14:3d1845f5cd81 | 110 | socket->proto = mdmproto; |
Christopher Haster |
14:3d1845f5cd81 | 111 | *handle = socket; |
Christopher Haster |
14:3d1845f5cd81 | 112 | return 0; |
geky | 12:181a9415736b | 113 | } |
geky | 12:181a9415736b | 114 | |
Christopher Haster |
14:3d1845f5cd81 | 115 | int C027Interface::socket_close(void *handle) |
geky | 12:181a9415736b | 116 | { |
Christopher Haster |
14:3d1845f5cd81 | 117 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 118 | _mdm->socketFree(socket->socket); |
geky | 12:181a9415736b | 119 | |
geky | 12:181a9415736b | 120 | delete socket; |
Christopher Haster |
14:3d1845f5cd81 | 121 | return 0; |
geky | 12:181a9415736b | 122 | } |
geky | 12:181a9415736b | 123 | |
Christopher Haster |
14:3d1845f5cd81 | 124 | int C027Interface::socket_bind(void *handle, const SocketAddress &address) |
Christopher Haster |
14:3d1845f5cd81 | 125 | { |
Christopher Haster |
14:3d1845f5cd81 | 126 | return NSAPI_ERROR_UNSUPPORTED; |
Christopher Haster |
14:3d1845f5cd81 | 127 | } |
geky | 12:181a9415736b | 128 | |
Christopher Haster |
14:3d1845f5cd81 | 129 | int C027Interface::socket_listen(void *handle, int backlog) |
geky | 12:181a9415736b | 130 | { |
Christopher Haster |
14:3d1845f5cd81 | 131 | return NSAPI_ERROR_UNSUPPORTED; |
Christopher Haster |
14:3d1845f5cd81 | 132 | } |
Christopher Haster |
14:3d1845f5cd81 | 133 | |
Christopher Haster |
14:3d1845f5cd81 | 134 | int C027Interface::socket_connect(void *handle, const SocketAddress &addr) |
Christopher Haster |
14:3d1845f5cd81 | 135 | { |
Christopher Haster |
14:3d1845f5cd81 | 136 | struct c027_socket *socket = (struct c027_socket *)handle; |
geky | 12:181a9415736b | 137 | |
Christopher Haster |
14:3d1845f5cd81 | 138 | if (!_mdm->socketConnect(socket->socket, addr.get_ip_address(), addr.get_port())) { |
Christopher Haster |
14:3d1845f5cd81 | 139 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 140 | } |
geky | 12:181a9415736b | 141 | |
geky | 12:181a9415736b | 142 | return 0; |
geky | 12:181a9415736b | 143 | } |
geky | 12:181a9415736b | 144 | |
Christopher Haster |
14:3d1845f5cd81 | 145 | int C027Interface::socket_accept(void **handle, void *server) |
geky | 12:181a9415736b | 146 | { |
Christopher Haster |
14:3d1845f5cd81 | 147 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 12:181a9415736b | 148 | } |
geky | 12:181a9415736b | 149 | |
Christopher Haster |
14:3d1845f5cd81 | 150 | int C027Interface::socket_send(void *handle, const void *data, unsigned size) |
geky | 12:181a9415736b | 151 | { |
Christopher Haster |
14:3d1845f5cd81 | 152 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 153 | |
Christopher Haster |
14:3d1845f5cd81 | 154 | int sent = _mdm->socketSend(socket->socket, (const char *)data, size); |
Christopher Haster |
14:3d1845f5cd81 | 155 | if (sent == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 156 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 157 | } |
geky | 12:181a9415736b | 158 | |
Christopher Haster |
14:3d1845f5cd81 | 159 | return sent; |
geky | 12:181a9415736b | 160 | } |
geky | 12:181a9415736b | 161 | |
Christopher Haster |
14:3d1845f5cd81 | 162 | int C027Interface::socket_recv(void *handle, void *data, unsigned size) |
geky | 12:181a9415736b | 163 | { |
Christopher Haster |
14:3d1845f5cd81 | 164 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 165 | if (!_mdm->socketReadable(socket->socket)) { |
Christopher Haster |
14:3d1845f5cd81 | 166 | return NSAPI_ERROR_WOULD_BLOCK; |
geky | 12:181a9415736b | 167 | } |
geky | 12:181a9415736b | 168 | |
Christopher Haster |
14:3d1845f5cd81 | 169 | int recv = _mdm->socketRecv(socket->socket, (char *)data, size); |
geky | 12:181a9415736b | 170 | if (recv == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 171 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 172 | } |
geky | 12:181a9415736b | 173 | |
geky | 12:181a9415736b | 174 | return recv; |
geky | 12:181a9415736b | 175 | } |
geky | 12:181a9415736b | 176 | |
Christopher Haster |
14:3d1845f5cd81 | 177 | int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
Christopher Haster |
14:3d1845f5cd81 | 178 | { |
Christopher Haster |
14:3d1845f5cd81 | 179 | struct c027_socket *socket = (struct c027_socket *)handle; |
geky | 12:181a9415736b | 180 | |
Christopher Haster |
14:3d1845f5cd81 | 181 | MDMParser::IP ip = _mdm->gethostbyname(addr.get_ip_address()); |
Christopher Haster |
14:3d1845f5cd81 | 182 | uint16_t port = addr.get_port(); |
Christopher Haster |
14:3d1845f5cd81 | 183 | |
Christopher Haster |
14:3d1845f5cd81 | 184 | int sent = _mdm->socketSendTo(socket->socket, ip, port, (const char *)data, size); |
Christopher Haster |
14:3d1845f5cd81 | 185 | if (sent == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 186 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
14:3d1845f5cd81 | 187 | } |
Christopher Haster |
14:3d1845f5cd81 | 188 | |
Christopher Haster |
14:3d1845f5cd81 | 189 | return sent; |
Christopher Haster |
14:3d1845f5cd81 | 190 | } |
Christopher Haster |
14:3d1845f5cd81 | 191 | |
Christopher Haster |
14:3d1845f5cd81 | 192 | int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
Christopher Haster |
14:3d1845f5cd81 | 193 | { |
Christopher Haster |
14:3d1845f5cd81 | 194 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 195 | if (!_mdm->socketReadable(socket->socket)) { |
Christopher Haster |
14:3d1845f5cd81 | 196 | return NSAPI_ERROR_WOULD_BLOCK; |
Christopher Haster |
14:3d1845f5cd81 | 197 | } |
Christopher Haster |
14:3d1845f5cd81 | 198 | |
Christopher Haster |
14:3d1845f5cd81 | 199 | MDMParser::IP ip; |
Christopher Haster |
14:3d1845f5cd81 | 200 | int port; |
Christopher Haster |
14:3d1845f5cd81 | 201 | |
Christopher Haster |
14:3d1845f5cd81 | 202 | int recv = _mdm->socketRecvFrom(socket->socket, &ip, &port, (char *)data, size); |
Christopher Haster |
14:3d1845f5cd81 | 203 | if (recv == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 204 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
14:3d1845f5cd81 | 205 | } |
Christopher Haster |
14:3d1845f5cd81 | 206 | |
Christopher Haster |
14:3d1845f5cd81 | 207 | if (addr) { |
Christopher Haster |
14:3d1845f5cd81 | 208 | char buffer[NSAPI_IP_SIZE]; |
Christopher Haster |
14:3d1845f5cd81 | 209 | ipToString(ip, buffer); |
Christopher Haster |
14:3d1845f5cd81 | 210 | addr->set_ip_address(buffer); |
Christopher Haster |
14:3d1845f5cd81 | 211 | addr->set_port(port); |
Christopher Haster |
14:3d1845f5cd81 | 212 | } |
Christopher Haster |
14:3d1845f5cd81 | 213 | |
Christopher Haster |
14:3d1845f5cd81 | 214 | return recv; |
Christopher Haster |
14:3d1845f5cd81 | 215 | } |
Christopher Haster |
14:3d1845f5cd81 | 216 | |
Christopher Haster |
14:3d1845f5cd81 | 217 | void C027Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
Christopher Haster |
14:3d1845f5cd81 | 218 | { |
Christopher Haster |
14:3d1845f5cd81 | 219 | } |