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@21:0c4d90bc84af, 2017-03-27 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Mar 27 14:10:06 2017 -0500
- Revision:
- 21:0c4d90bc84af
- Parent:
- 19:fed715eb9c83
- Child:
- 22:69ea34ad7fc6
Manage multiple sockets in socket_attach
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 | // C027Interface implementation |
geky | 12:181a9415736b | 22 | C027Interface::C027Interface(const char *simpin, bool debug) |
geky | 12:181a9415736b | 23 | : _debug(debug) |
geky | 12:181a9415736b | 24 | { |
geky | 12:181a9415736b | 25 | strcpy(_pin, simpin); |
sarahmarshy | 19:fed715eb9c83 | 26 | running = false; |
geky | 12:181a9415736b | 27 | } |
geky | 12:181a9415736b | 28 | |
mbed_official | 21:0c4d90bc84af | 29 | C027Interface::~C027Interface() |
mbed_official | 21:0c4d90bc84af | 30 | { |
mbed_official | 21:0c4d90bc84af | 31 | _thread.join(); |
mbed_official | 21:0c4d90bc84af | 32 | } |
mbed_official | 21:0c4d90bc84af | 33 | |
mbed_official | 21:0c4d90bc84af | 34 | nsapi_error_t C027Interface::set_credentials(const char *apn, const char *username, const char *password) |
mbed_official | 21:0c4d90bc84af | 35 | { |
mbed_official | 21:0c4d90bc84af | 36 | memset(_apn, 0, sizeof(_apn)); |
mbed_official | 21:0c4d90bc84af | 37 | strncpy(_apn, apn, sizeof(_apn)); |
mbed_official | 21:0c4d90bc84af | 38 | |
mbed_official | 21:0c4d90bc84af | 39 | memset(_username, 0, sizeof(_username)); |
mbed_official | 21:0c4d90bc84af | 40 | strncpy(_username, username, sizeof(_username)); |
mbed_official | 21:0c4d90bc84af | 41 | |
mbed_official | 21:0c4d90bc84af | 42 | memset(_password, 0, sizeof(_password)); |
mbed_official | 21:0c4d90bc84af | 43 | strncpy(_password, password, sizeof(_password)); |
mbed_official | 21:0c4d90bc84af | 44 | return 0; |
mbed_official | 21:0c4d90bc84af | 45 | } |
mbed_official | 21:0c4d90bc84af | 46 | |
mbed_official | 21:0c4d90bc84af | 47 | nsapi_error_t C027Interface::connect() |
geky | 12:181a9415736b | 48 | { |
geky | 12:181a9415736b | 49 | // create the modem |
geky | 12:181a9415736b | 50 | _mdm = new MDMSerial; |
geky | 12:181a9415736b | 51 | if (_debug) { |
geky | 12:181a9415736b | 52 | _mdm->setDebug(4); |
geky | 12:181a9415736b | 53 | } else { |
geky | 12:181a9415736b | 54 | _mdm->setDebug(0); |
geky | 12:181a9415736b | 55 | } |
geky | 12:181a9415736b | 56 | |
geky | 12:181a9415736b | 57 | // initialize the modem |
geky | 12:181a9415736b | 58 | MDMParser::DevStatus devStatus = {}; |
geky | 12:181a9415736b | 59 | MDMParser::NetStatus netStatus = {}; |
geky | 12:181a9415736b | 60 | bool mdmOk = _mdm->init(_pin, &devStatus); |
geky | 12:181a9415736b | 61 | if (_debug) { |
geky | 12:181a9415736b | 62 | _mdm->dumpDevStatus(&devStatus); |
geky | 12:181a9415736b | 63 | } |
geky | 12:181a9415736b | 64 | |
geky | 12:181a9415736b | 65 | if (mdmOk) { |
geky | 12:181a9415736b | 66 | // wait until we are connected |
geky | 12:181a9415736b | 67 | mdmOk = _mdm->registerNet(&netStatus); |
geky | 12:181a9415736b | 68 | if (_debug) { |
geky | 12:181a9415736b | 69 | _mdm->dumpNetStatus(&netStatus); |
geky | 12:181a9415736b | 70 | } |
geky | 12:181a9415736b | 71 | } |
geky | 12:181a9415736b | 72 | |
Christopher Haster |
14:3d1845f5cd81 | 73 | if (mdmOk) { |
geky | 12:181a9415736b | 74 | // join the internet connection |
mbed_official | 21:0c4d90bc84af | 75 | MDMParser::IP ip = _mdm->join(_apn, _username, _password); |
geky | 15:21d4f56eb172 | 76 | _ip_address.set_ip_bytes(&ip, NSAPI_IPv4); |
geky | 12:181a9415736b | 77 | mdmOk = (ip != NOIP); |
geky | 12:181a9415736b | 78 | } |
geky | 12:181a9415736b | 79 | |
Christopher Haster |
14:3d1845f5cd81 | 80 | return mdmOk ? 0 : NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 81 | } |
mbed_official | 21:0c4d90bc84af | 82 | |
mbed_official | 21:0c4d90bc84af | 83 | nsapi_error_t C027Interface::connect(const char *apn, const char *username, const char *password) |
mbed_official | 21:0c4d90bc84af | 84 | { |
mbed_official | 21:0c4d90bc84af | 85 | |
mbed_official | 21:0c4d90bc84af | 86 | set_credentials(apn, username, password); |
mbed_official | 21:0c4d90bc84af | 87 | return connect(); |
mbed_official | 21:0c4d90bc84af | 88 | } |
geky | 12:181a9415736b | 89 | |
Christopher Haster |
14:3d1845f5cd81 | 90 | int C027Interface::disconnect() |
geky | 12:181a9415736b | 91 | { |
geky | 12:181a9415736b | 92 | if (!_mdm->disconnect()) { |
Christopher Haster |
14:3d1845f5cd81 | 93 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 94 | } |
geky | 12:181a9415736b | 95 | |
geky | 12:181a9415736b | 96 | return 0; |
geky | 12:181a9415736b | 97 | } |
geky | 12:181a9415736b | 98 | |
Christopher Haster |
14:3d1845f5cd81 | 99 | const char *C027Interface::get_ip_address() |
geky | 12:181a9415736b | 100 | { |
geky | 15:21d4f56eb172 | 101 | return _ip_address.get_ip_address(); |
geky | 12:181a9415736b | 102 | } |
geky | 12:181a9415736b | 103 | |
Christopher Haster |
14:3d1845f5cd81 | 104 | const char *C027Interface::get_mac_address() |
geky | 12:181a9415736b | 105 | { |
geky | 12:181a9415736b | 106 | return 0; |
geky | 12:181a9415736b | 107 | } |
geky | 12:181a9415736b | 108 | |
Christopher Haster |
14:3d1845f5cd81 | 109 | struct c027_socket { |
Christopher Haster |
14:3d1845f5cd81 | 110 | int socket; |
Christopher Haster |
14:3d1845f5cd81 | 111 | MDMParser::IpProtocol proto; |
Christopher Haster |
14:3d1845f5cd81 | 112 | MDMParser::IP ip; |
Christopher Haster |
14:3d1845f5cd81 | 113 | int port; |
Christopher Haster |
14:3d1845f5cd81 | 114 | }; |
Christopher Haster |
14:3d1845f5cd81 | 115 | |
Christopher Haster |
14:3d1845f5cd81 | 116 | int C027Interface::socket_open(void **handle, nsapi_protocol_t proto) |
geky | 12:181a9415736b | 117 | { |
Christopher Haster |
14:3d1845f5cd81 | 118 | MDMParser::IpProtocol mdmproto = (proto == NSAPI_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP; |
Christopher Haster |
14:3d1845f5cd81 | 119 | int fd = _mdm->socketSocket(mdmproto); |
Christopher Haster |
14:3d1845f5cd81 | 120 | if (fd < 0) { |
Christopher Haster |
14:3d1845f5cd81 | 121 | return NSAPI_ERROR_NO_SOCKET; |
geky | 12:181a9415736b | 122 | } |
geky | 12:181a9415736b | 123 | |
Christopher Haster |
14:3d1845f5cd81 | 124 | _mdm->socketSetBlocking(fd, 10000); |
Christopher Haster |
14:3d1845f5cd81 | 125 | struct c027_socket *socket = new struct c027_socket; |
Christopher Haster |
14:3d1845f5cd81 | 126 | if (!socket) { |
Christopher Haster |
14:3d1845f5cd81 | 127 | return NSAPI_ERROR_NO_SOCKET; |
Christopher Haster |
14:3d1845f5cd81 | 128 | } |
geky | 12:181a9415736b | 129 | |
Christopher Haster |
14:3d1845f5cd81 | 130 | socket->socket = fd; |
Christopher Haster |
14:3d1845f5cd81 | 131 | socket->proto = mdmproto; |
Christopher Haster |
14:3d1845f5cd81 | 132 | *handle = socket; |
Christopher Haster |
14:3d1845f5cd81 | 133 | return 0; |
geky | 12:181a9415736b | 134 | } |
geky | 12:181a9415736b | 135 | |
Christopher Haster |
14:3d1845f5cd81 | 136 | int C027Interface::socket_close(void *handle) |
geky | 12:181a9415736b | 137 | { |
Christopher Haster |
14:3d1845f5cd81 | 138 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 139 | _mdm->socketFree(socket->socket); |
geky | 12:181a9415736b | 140 | |
geky | 12:181a9415736b | 141 | delete socket; |
Christopher Haster |
14:3d1845f5cd81 | 142 | return 0; |
geky | 12:181a9415736b | 143 | } |
geky | 12:181a9415736b | 144 | |
Christopher Haster |
14:3d1845f5cd81 | 145 | int C027Interface::socket_bind(void *handle, const SocketAddress &address) |
Christopher Haster |
14:3d1845f5cd81 | 146 | { |
Christopher Haster |
14:3d1845f5cd81 | 147 | return NSAPI_ERROR_UNSUPPORTED; |
Christopher Haster |
14:3d1845f5cd81 | 148 | } |
geky | 12:181a9415736b | 149 | |
Christopher Haster |
14:3d1845f5cd81 | 150 | int C027Interface::socket_listen(void *handle, int backlog) |
geky | 12:181a9415736b | 151 | { |
Christopher Haster |
14:3d1845f5cd81 | 152 | return NSAPI_ERROR_UNSUPPORTED; |
Christopher Haster |
14:3d1845f5cd81 | 153 | } |
Christopher Haster |
14:3d1845f5cd81 | 154 | |
Christopher Haster |
14:3d1845f5cd81 | 155 | int C027Interface::socket_connect(void *handle, const SocketAddress &addr) |
Christopher Haster |
14:3d1845f5cd81 | 156 | { |
Christopher Haster |
14:3d1845f5cd81 | 157 | struct c027_socket *socket = (struct c027_socket *)handle; |
geky | 12:181a9415736b | 158 | |
Christopher Haster |
14:3d1845f5cd81 | 159 | if (!_mdm->socketConnect(socket->socket, addr.get_ip_address(), addr.get_port())) { |
Christopher Haster |
14:3d1845f5cd81 | 160 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 161 | } |
geky | 12:181a9415736b | 162 | |
geky | 12:181a9415736b | 163 | return 0; |
geky | 12:181a9415736b | 164 | } |
geky | 12:181a9415736b | 165 | |
mbed_official | 21:0c4d90bc84af | 166 | nsapi_error_t C027Interface::socket_accept(nsapi_socket_t server, |
mbed_official | 21:0c4d90bc84af | 167 | nsapi_socket_t *handle, SocketAddress *address) |
geky | 12:181a9415736b | 168 | { |
Christopher Haster |
14:3d1845f5cd81 | 169 | return NSAPI_ERROR_UNSUPPORTED; |
geky | 12:181a9415736b | 170 | } |
geky | 12:181a9415736b | 171 | |
Christopher Haster |
14:3d1845f5cd81 | 172 | int C027Interface::socket_send(void *handle, const void *data, unsigned size) |
geky | 12:181a9415736b | 173 | { |
Christopher Haster |
14:3d1845f5cd81 | 174 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 175 | |
Christopher Haster |
14:3d1845f5cd81 | 176 | int sent = _mdm->socketSend(socket->socket, (const char *)data, size); |
Christopher Haster |
14:3d1845f5cd81 | 177 | if (sent == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 178 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 179 | } |
geky | 12:181a9415736b | 180 | |
Christopher Haster |
14:3d1845f5cd81 | 181 | return sent; |
geky | 12:181a9415736b | 182 | } |
geky | 12:181a9415736b | 183 | |
Christopher Haster |
14:3d1845f5cd81 | 184 | int C027Interface::socket_recv(void *handle, void *data, unsigned size) |
geky | 12:181a9415736b | 185 | { |
Christopher Haster |
14:3d1845f5cd81 | 186 | struct c027_socket *socket = (struct c027_socket *)handle; |
mbed_official | 21:0c4d90bc84af | 187 | |
mbed_official | 21:0c4d90bc84af | 188 | if (!_mdm->socketReadable(socket->socket)) { |
Christopher Haster |
14:3d1845f5cd81 | 189 | return NSAPI_ERROR_WOULD_BLOCK; |
geky | 12:181a9415736b | 190 | } |
geky | 12:181a9415736b | 191 | |
Christopher Haster |
14:3d1845f5cd81 | 192 | int recv = _mdm->socketRecv(socket->socket, (char *)data, size); |
geky | 12:181a9415736b | 193 | if (recv == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 194 | return NSAPI_ERROR_DEVICE_ERROR; |
geky | 12:181a9415736b | 195 | } |
geky | 12:181a9415736b | 196 | |
geky | 12:181a9415736b | 197 | return recv; |
geky | 12:181a9415736b | 198 | } |
geky | 12:181a9415736b | 199 | |
Christopher Haster |
14:3d1845f5cd81 | 200 | int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
Christopher Haster |
14:3d1845f5cd81 | 201 | { |
Christopher Haster |
14:3d1845f5cd81 | 202 | struct c027_socket *socket = (struct c027_socket *)handle; |
geky | 12:181a9415736b | 203 | |
geky | 15:21d4f56eb172 | 204 | int sent = _mdm->socketSendTo(socket->socket, |
geky | 15:21d4f56eb172 | 205 | *(MDMParser::IP *)addr.get_ip_bytes(), addr.get_port(), |
geky | 15:21d4f56eb172 | 206 | (const char *)data, size); |
geky | 15:21d4f56eb172 | 207 | |
Christopher Haster |
14:3d1845f5cd81 | 208 | if (sent == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 209 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
14:3d1845f5cd81 | 210 | } |
Christopher Haster |
14:3d1845f5cd81 | 211 | |
Christopher Haster |
14:3d1845f5cd81 | 212 | return sent; |
Christopher Haster |
14:3d1845f5cd81 | 213 | } |
Christopher Haster |
14:3d1845f5cd81 | 214 | |
Christopher Haster |
14:3d1845f5cd81 | 215 | int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
Christopher Haster |
14:3d1845f5cd81 | 216 | { |
Christopher Haster |
14:3d1845f5cd81 | 217 | struct c027_socket *socket = (struct c027_socket *)handle; |
Christopher Haster |
14:3d1845f5cd81 | 218 | if (!_mdm->socketReadable(socket->socket)) { |
Christopher Haster |
14:3d1845f5cd81 | 219 | return NSAPI_ERROR_WOULD_BLOCK; |
Christopher Haster |
14:3d1845f5cd81 | 220 | } |
Christopher Haster |
14:3d1845f5cd81 | 221 | |
Christopher Haster |
14:3d1845f5cd81 | 222 | MDMParser::IP ip; |
Christopher Haster |
14:3d1845f5cd81 | 223 | int port; |
Christopher Haster |
14:3d1845f5cd81 | 224 | |
Christopher Haster |
14:3d1845f5cd81 | 225 | int recv = _mdm->socketRecvFrom(socket->socket, &ip, &port, (char *)data, size); |
Christopher Haster |
14:3d1845f5cd81 | 226 | if (recv == SOCKET_ERROR) { |
Christopher Haster |
14:3d1845f5cd81 | 227 | return NSAPI_ERROR_DEVICE_ERROR; |
Christopher Haster |
14:3d1845f5cd81 | 228 | } |
Christopher Haster |
14:3d1845f5cd81 | 229 | |
Christopher Haster |
14:3d1845f5cd81 | 230 | if (addr) { |
geky | 15:21d4f56eb172 | 231 | addr->set_ip_bytes(&ip, NSAPI_IPv4); |
Christopher Haster |
14:3d1845f5cd81 | 232 | addr->set_port(port); |
Christopher Haster |
14:3d1845f5cd81 | 233 | } |
Christopher Haster |
14:3d1845f5cd81 | 234 | |
Christopher Haster |
14:3d1845f5cd81 | 235 | return recv; |
Christopher Haster |
14:3d1845f5cd81 | 236 | } |
Christopher Haster |
14:3d1845f5cd81 | 237 | |
sarahmarshy | 19:fed715eb9c83 | 238 | void C027Interface::socket_check() |
sarahmarshy | 16:eaf31bd133f0 | 239 | { |
sarahmarshy | 19:fed715eb9c83 | 240 | while(running){ |
mbed_official | 21:0c4d90bc84af | 241 | for (int i = 0; i < NUMSOCKETS; i++){ |
mbed_official | 21:0c4d90bc84af | 242 | int readable = _mdm->socketReadable(i); |
mbed_official | 21:0c4d90bc84af | 243 | if (readable != 1) { |
mbed_official | 21:0c4d90bc84af | 244 | event(); |
sarahmarshy | 19:fed715eb9c83 | 245 | } |
sarahmarshy | 19:fed715eb9c83 | 246 | } |
mbed_official | 21:0c4d90bc84af | 247 | wait(1); |
mbed_official | 21:0c4d90bc84af | 248 | } |
mbed_official | 21:0c4d90bc84af | 249 | running = false; |
sarahmarshy | 19:fed715eb9c83 | 250 | } |
sarahmarshy | 19:fed715eb9c83 | 251 | |
sarahmarshy | 19:fed715eb9c83 | 252 | |
sarahmarshy | 19:fed715eb9c83 | 253 | void C027Interface::event(){ |
mbed_official | 21:0c4d90bc84af | 254 | for (int i = 0; i < NUMSOCKETS; i++){ |
mbed_official | 21:0c4d90bc84af | 255 | if (_cbs[i].callback) { |
mbed_official | 21:0c4d90bc84af | 256 | _cbs[i].callback(_cbs[i].data); |
mbed_official | 21:0c4d90bc84af | 257 | } |
mbed_official | 21:0c4d90bc84af | 258 | } |
sarahmarshy | 16:eaf31bd133f0 | 259 | } |
sarahmarshy | 16:eaf31bd133f0 | 260 | |
Christopher Haster |
14:3d1845f5cd81 | 261 | void C027Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
Christopher Haster |
14:3d1845f5cd81 | 262 | { |
mbed_official | 21:0c4d90bc84af | 263 | struct c027_socket *socket = (struct c027_socket *)handle; |
mbed_official | 21:0c4d90bc84af | 264 | _cbs[socket->socket].callback = callback; |
mbed_official | 21:0c4d90bc84af | 265 | _cbs[socket->socket].data = data; |
sarahmarshy | 19:fed715eb9c83 | 266 | if(!running){ |
sarahmarshy | 19:fed715eb9c83 | 267 | running = true; |
sarahmarshy | 19:fed715eb9c83 | 268 | _thread.start(this, &C027Interface::socket_check); |
sarahmarshy | 19:fed715eb9c83 | 269 | } |
Christopher Haster |
14:3d1845f5cd81 | 270 | } |
sarahmarshy | 19:fed715eb9c83 | 271 |