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