emw3162 driver for mbed os 5
Fork of emw3162-driver by
EMW3162Interface.cpp@2:fb6251306b21, 2016-11-14 (annotated)
- Committer:
- Maggie17
- Date:
- Mon Nov 14 02:30:49 2016 +0000
- Revision:
- 2:fb6251306b21
- Parent:
- 0:2d5090e1ceb4
- Child:
- 3:a6f80a95b039
Change the API name from ESP8266 to EMW3162
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maggie17 | 2:fb6251306b21 | 1 | /* EMW3162 implementation of NetworkInterfaceAPI |
Maggie17 | 0:2d5090e1ceb4 | 2 | * Copyright (c) 2015 ARM Limited |
Maggie17 | 0:2d5090e1ceb4 | 3 | * |
Maggie17 | 0:2d5090e1ceb4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Maggie17 | 0:2d5090e1ceb4 | 5 | * you may not use this file except in compliance with the License. |
Maggie17 | 0:2d5090e1ceb4 | 6 | * You may obtain a copy of the License at |
Maggie17 | 0:2d5090e1ceb4 | 7 | * |
Maggie17 | 0:2d5090e1ceb4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Maggie17 | 0:2d5090e1ceb4 | 9 | * |
Maggie17 | 0:2d5090e1ceb4 | 10 | * Unless required by applicable law or agreed to in writing, software |
Maggie17 | 0:2d5090e1ceb4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Maggie17 | 0:2d5090e1ceb4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Maggie17 | 0:2d5090e1ceb4 | 13 | * See the License for the specific language governing permissions and |
Maggie17 | 0:2d5090e1ceb4 | 14 | * limitations under the License. |
Maggie17 | 0:2d5090e1ceb4 | 15 | */ |
Maggie17 | 0:2d5090e1ceb4 | 16 | |
Maggie17 | 0:2d5090e1ceb4 | 17 | |
Maggie17 | 0:2d5090e1ceb4 | 18 | #include "EMW3162Interface.h" |
Maggie17 | 0:2d5090e1ceb4 | 19 | |
Maggie17 | 2:fb6251306b21 | 20 | // Various timeouts for different EMW3162 operations |
Maggie17 | 2:fb6251306b21 | 21 | #define EMW3162_CONNECT_TIMEOUT 15000 |
Maggie17 | 2:fb6251306b21 | 22 | #define EMW3162_SEND_TIMEOUT 500 |
Maggie17 | 2:fb6251306b21 | 23 | #define EMW3162_RECV_TIMEOUT 0 |
Maggie17 | 2:fb6251306b21 | 24 | #define EMW3162_MISC_TIMEOUT 500 |
Maggie17 | 0:2d5090e1ceb4 | 25 | |
Maggie17 | 0:2d5090e1ceb4 | 26 | |
Maggie17 | 2:fb6251306b21 | 27 | // EMW3162Interface implementation |
Maggie17 | 2:fb6251306b21 | 28 | EMW3162Interface::EMW3162Interface(PinName tx, PinName rx, bool debug) |
Maggie17 | 0:2d5090e1ceb4 | 29 | : _esp(tx, rx, debug) |
Maggie17 | 0:2d5090e1ceb4 | 30 | { |
Maggie17 | 0:2d5090e1ceb4 | 31 | memset(_ids, 0, sizeof(_ids)); |
Maggie17 | 0:2d5090e1ceb4 | 32 | memset(_cbs, 0, sizeof(_cbs)); |
Maggie17 | 0:2d5090e1ceb4 | 33 | |
Maggie17 | 2:fb6251306b21 | 34 | _esp.attach(this, &EMW3162Interface::event); |
Maggie17 | 0:2d5090e1ceb4 | 35 | } |
Maggie17 | 0:2d5090e1ceb4 | 36 | |
Maggie17 | 2:fb6251306b21 | 37 | int EMW3162Interface::connect( |
Maggie17 | 0:2d5090e1ceb4 | 38 | const char *ssid, |
Maggie17 | 0:2d5090e1ceb4 | 39 | const char *pass, |
Maggie17 | 0:2d5090e1ceb4 | 40 | nsapi_security_t security) |
Maggie17 | 0:2d5090e1ceb4 | 41 | { |
Maggie17 | 2:fb6251306b21 | 42 | _esp.setTimeout(EMW3162_CONNECT_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 43 | |
Maggie17 | 0:2d5090e1ceb4 | 44 | if (!_esp.startup()) { |
Maggie17 | 0:2d5090e1ceb4 | 45 | return NSAPI_ERROR_DEVICE_ERROR; |
Maggie17 | 0:2d5090e1ceb4 | 46 | } |
Maggie17 | 0:2d5090e1ceb4 | 47 | |
Maggie17 | 0:2d5090e1ceb4 | 48 | if (!_esp.dhcp(true)) { |
Maggie17 | 0:2d5090e1ceb4 | 49 | return NSAPI_ERROR_DHCP_FAILURE; |
Maggie17 | 0:2d5090e1ceb4 | 50 | } |
Maggie17 | 0:2d5090e1ceb4 | 51 | |
Maggie17 | 0:2d5090e1ceb4 | 52 | if (!_esp.connect(ssid, pass)) { |
Maggie17 | 0:2d5090e1ceb4 | 53 | return NSAPI_ERROR_NO_CONNECTION; |
Maggie17 | 0:2d5090e1ceb4 | 54 | } |
Maggie17 | 0:2d5090e1ceb4 | 55 | |
Maggie17 | 0:2d5090e1ceb4 | 56 | if (!_esp.getIPAddress()) { |
Maggie17 | 0:2d5090e1ceb4 | 57 | return NSAPI_ERROR_DHCP_FAILURE; |
Maggie17 | 0:2d5090e1ceb4 | 58 | } |
Maggie17 | 0:2d5090e1ceb4 | 59 | |
Maggie17 | 0:2d5090e1ceb4 | 60 | return 0; |
Maggie17 | 0:2d5090e1ceb4 | 61 | } |
Maggie17 | 0:2d5090e1ceb4 | 62 | |
Maggie17 | 2:fb6251306b21 | 63 | int EMW3162Interface::disconnect() |
Maggie17 | 0:2d5090e1ceb4 | 64 | { |
Maggie17 | 2:fb6251306b21 | 65 | _esp.setTimeout(EMW3162_MISC_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 66 | |
Maggie17 | 0:2d5090e1ceb4 | 67 | if (!_esp.disconnect()) { |
Maggie17 | 0:2d5090e1ceb4 | 68 | return NSAPI_ERROR_DEVICE_ERROR; |
Maggie17 | 0:2d5090e1ceb4 | 69 | } |
Maggie17 | 0:2d5090e1ceb4 | 70 | |
Maggie17 | 0:2d5090e1ceb4 | 71 | return 0; |
Maggie17 | 0:2d5090e1ceb4 | 72 | } |
Maggie17 | 0:2d5090e1ceb4 | 73 | |
Maggie17 | 2:fb6251306b21 | 74 | const char* EMW3162Interface::get_ip_address() |
Maggie17 | 0:2d5090e1ceb4 | 75 | { |
Maggie17 | 0:2d5090e1ceb4 | 76 | return _esp.getIPAddress(); |
Maggie17 | 0:2d5090e1ceb4 | 77 | } |
Maggie17 | 0:2d5090e1ceb4 | 78 | |
Maggie17 | 2:fb6251306b21 | 79 | const char* EMW3162Interface::get_mac_address() |
Maggie17 | 0:2d5090e1ceb4 | 80 | { |
Maggie17 | 0:2d5090e1ceb4 | 81 | return _esp.getMACAddress(); |
Maggie17 | 0:2d5090e1ceb4 | 82 | } |
Maggie17 | 0:2d5090e1ceb4 | 83 | |
Maggie17 | 2:fb6251306b21 | 84 | struct EMW3162_socket { |
Maggie17 | 0:2d5090e1ceb4 | 85 | int id; |
Maggie17 | 0:2d5090e1ceb4 | 86 | int socketId; |
Maggie17 | 0:2d5090e1ceb4 | 87 | nsapi_protocol_t proto; |
Maggie17 | 0:2d5090e1ceb4 | 88 | bool connected; |
Maggie17 | 0:2d5090e1ceb4 | 89 | }; |
Maggie17 | 0:2d5090e1ceb4 | 90 | |
Maggie17 | 2:fb6251306b21 | 91 | int EMW3162Interface::socket_open(void **handle, nsapi_protocol_t proto) |
Maggie17 | 0:2d5090e1ceb4 | 92 | { |
Maggie17 | 0:2d5090e1ceb4 | 93 | // Look for an unused socket |
Maggie17 | 0:2d5090e1ceb4 | 94 | int id = -1; |
Maggie17 | 0:2d5090e1ceb4 | 95 | |
Maggie17 | 2:fb6251306b21 | 96 | for (int i = 1; i < EMW3162_SOCKET_COUNT; i++) { |
Maggie17 | 0:2d5090e1ceb4 | 97 | if (!_ids[i]) { |
Maggie17 | 0:2d5090e1ceb4 | 98 | id = i; |
Maggie17 | 0:2d5090e1ceb4 | 99 | _ids[i] = true; |
Maggie17 | 0:2d5090e1ceb4 | 100 | break; |
Maggie17 | 0:2d5090e1ceb4 | 101 | } |
Maggie17 | 0:2d5090e1ceb4 | 102 | } |
Maggie17 | 0:2d5090e1ceb4 | 103 | |
Maggie17 | 0:2d5090e1ceb4 | 104 | if (id == -1) { |
Maggie17 | 0:2d5090e1ceb4 | 105 | return NSAPI_ERROR_NO_SOCKET; |
Maggie17 | 0:2d5090e1ceb4 | 106 | } |
Maggie17 | 0:2d5090e1ceb4 | 107 | |
Maggie17 | 2:fb6251306b21 | 108 | struct EMW3162_socket *socket = new struct EMW3162_socket; |
Maggie17 | 0:2d5090e1ceb4 | 109 | if (!socket) { |
Maggie17 | 0:2d5090e1ceb4 | 110 | return NSAPI_ERROR_NO_SOCKET; |
Maggie17 | 0:2d5090e1ceb4 | 111 | } |
Maggie17 | 0:2d5090e1ceb4 | 112 | |
Maggie17 | 0:2d5090e1ceb4 | 113 | socket->id = id; |
Maggie17 | 0:2d5090e1ceb4 | 114 | socket->socketId = 0; |
Maggie17 | 0:2d5090e1ceb4 | 115 | socket->proto = proto; |
Maggie17 | 0:2d5090e1ceb4 | 116 | socket->connected = false; |
Maggie17 | 0:2d5090e1ceb4 | 117 | *handle = socket; |
Maggie17 | 0:2d5090e1ceb4 | 118 | return 0; |
Maggie17 | 0:2d5090e1ceb4 | 119 | } |
Maggie17 | 0:2d5090e1ceb4 | 120 | |
Maggie17 | 2:fb6251306b21 | 121 | int EMW3162Interface::socket_close(void *handle) |
Maggie17 | 0:2d5090e1ceb4 | 122 | { |
Maggie17 | 2:fb6251306b21 | 123 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 0:2d5090e1ceb4 | 124 | int err = 0; |
Maggie17 | 2:fb6251306b21 | 125 | _esp.setTimeout(EMW3162_MISC_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 126 | |
Maggie17 | 0:2d5090e1ceb4 | 127 | if (!_esp.close(socket->socketId)) { |
Maggie17 | 0:2d5090e1ceb4 | 128 | err = NSAPI_ERROR_DEVICE_ERROR; |
Maggie17 | 0:2d5090e1ceb4 | 129 | } |
Maggie17 | 0:2d5090e1ceb4 | 130 | |
Maggie17 | 0:2d5090e1ceb4 | 131 | _ids[socket->id] = false; |
Maggie17 | 0:2d5090e1ceb4 | 132 | delete socket; |
Maggie17 | 0:2d5090e1ceb4 | 133 | return err; |
Maggie17 | 0:2d5090e1ceb4 | 134 | } |
Maggie17 | 0:2d5090e1ceb4 | 135 | |
Maggie17 | 2:fb6251306b21 | 136 | int EMW3162Interface::socket_bind(void *handle, const SocketAddress &address) |
Maggie17 | 0:2d5090e1ceb4 | 137 | { |
Maggie17 | 0:2d5090e1ceb4 | 138 | return NSAPI_ERROR_UNSUPPORTED; |
Maggie17 | 0:2d5090e1ceb4 | 139 | } |
Maggie17 | 0:2d5090e1ceb4 | 140 | |
Maggie17 | 2:fb6251306b21 | 141 | int EMW3162Interface::socket_listen(void *handle, int backlog) |
Maggie17 | 0:2d5090e1ceb4 | 142 | { |
Maggie17 | 0:2d5090e1ceb4 | 143 | return NSAPI_ERROR_UNSUPPORTED; |
Maggie17 | 0:2d5090e1ceb4 | 144 | } |
Maggie17 | 0:2d5090e1ceb4 | 145 | |
Maggie17 | 2:fb6251306b21 | 146 | int EMW3162Interface::socket_connect(void *handle, const SocketAddress &addr) |
Maggie17 | 0:2d5090e1ceb4 | 147 | { |
Maggie17 | 2:fb6251306b21 | 148 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 2:fb6251306b21 | 149 | _esp.setTimeout(EMW3162_MISC_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 150 | |
Maggie17 | 0:2d5090e1ceb4 | 151 | const char *proto = (socket->proto == NSAPI_UDP) ? "UNICAST" : "CLIENT"; |
Maggie17 | 0:2d5090e1ceb4 | 152 | socket -> socketId = _esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port()); |
Maggie17 | 0:2d5090e1ceb4 | 153 | if (!(socket -> socketId)) { |
Maggie17 | 0:2d5090e1ceb4 | 154 | return NSAPI_ERROR_DEVICE_ERROR; |
Maggie17 | 0:2d5090e1ceb4 | 155 | } |
Maggie17 | 0:2d5090e1ceb4 | 156 | |
Maggie17 | 0:2d5090e1ceb4 | 157 | socket->connected = true; |
Maggie17 | 0:2d5090e1ceb4 | 158 | return 0; |
Maggie17 | 0:2d5090e1ceb4 | 159 | } |
Maggie17 | 0:2d5090e1ceb4 | 160 | |
Maggie17 | 2:fb6251306b21 | 161 | int EMW3162Interface::socket_accept(void **handle, void *server) |
Maggie17 | 0:2d5090e1ceb4 | 162 | { |
Maggie17 | 0:2d5090e1ceb4 | 163 | return NSAPI_ERROR_UNSUPPORTED; |
Maggie17 | 0:2d5090e1ceb4 | 164 | } |
Maggie17 | 0:2d5090e1ceb4 | 165 | |
Maggie17 | 2:fb6251306b21 | 166 | int EMW3162Interface::socket_send(void *handle, const void *data, unsigned size) |
Maggie17 | 0:2d5090e1ceb4 | 167 | { |
Maggie17 | 2:fb6251306b21 | 168 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 2:fb6251306b21 | 169 | _esp.setTimeout(EMW3162_SEND_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 170 | |
Maggie17 | 0:2d5090e1ceb4 | 171 | if (!_esp.send(socket->socketId, data, size)) { |
Maggie17 | 0:2d5090e1ceb4 | 172 | return NSAPI_ERROR_DEVICE_ERROR; |
Maggie17 | 0:2d5090e1ceb4 | 173 | } |
Maggie17 | 0:2d5090e1ceb4 | 174 | |
Maggie17 | 0:2d5090e1ceb4 | 175 | return size; |
Maggie17 | 0:2d5090e1ceb4 | 176 | } |
Maggie17 | 0:2d5090e1ceb4 | 177 | |
Maggie17 | 2:fb6251306b21 | 178 | int EMW3162Interface::socket_recv(void *handle, void *data, unsigned size) |
Maggie17 | 0:2d5090e1ceb4 | 179 | { |
Maggie17 | 2:fb6251306b21 | 180 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 2:fb6251306b21 | 181 | _esp.setTimeout(EMW3162_RECV_TIMEOUT); |
Maggie17 | 0:2d5090e1ceb4 | 182 | |
Maggie17 | 0:2d5090e1ceb4 | 183 | int32_t recv = _esp.recv(socket->socketId, data, size); |
Maggie17 | 0:2d5090e1ceb4 | 184 | if (recv < 0) { |
Maggie17 | 0:2d5090e1ceb4 | 185 | return NSAPI_ERROR_WOULD_BLOCK; |
Maggie17 | 0:2d5090e1ceb4 | 186 | } |
Maggie17 | 0:2d5090e1ceb4 | 187 | |
Maggie17 | 0:2d5090e1ceb4 | 188 | return recv; |
Maggie17 | 0:2d5090e1ceb4 | 189 | } |
Maggie17 | 0:2d5090e1ceb4 | 190 | |
Maggie17 | 2:fb6251306b21 | 191 | int EMW3162Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
Maggie17 | 0:2d5090e1ceb4 | 192 | { |
Maggie17 | 2:fb6251306b21 | 193 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 0:2d5090e1ceb4 | 194 | if (!socket->connected) { |
Maggie17 | 0:2d5090e1ceb4 | 195 | int err = socket_connect(socket, addr); |
Maggie17 | 0:2d5090e1ceb4 | 196 | if (err < 0) { |
Maggie17 | 0:2d5090e1ceb4 | 197 | return err; |
Maggie17 | 0:2d5090e1ceb4 | 198 | } |
Maggie17 | 0:2d5090e1ceb4 | 199 | } |
Maggie17 | 0:2d5090e1ceb4 | 200 | |
Maggie17 | 0:2d5090e1ceb4 | 201 | return socket_send(socket, data, size); |
Maggie17 | 0:2d5090e1ceb4 | 202 | } |
Maggie17 | 0:2d5090e1ceb4 | 203 | |
Maggie17 | 2:fb6251306b21 | 204 | int EMW3162Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
Maggie17 | 0:2d5090e1ceb4 | 205 | { |
Maggie17 | 2:fb6251306b21 | 206 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 0:2d5090e1ceb4 | 207 | return socket_recv(socket, data, size); |
Maggie17 | 0:2d5090e1ceb4 | 208 | } |
Maggie17 | 0:2d5090e1ceb4 | 209 | |
Maggie17 | 2:fb6251306b21 | 210 | void EMW3162Interface::socket_attach(void *handle, void (*callback)(void *), void *data) |
Maggie17 | 0:2d5090e1ceb4 | 211 | { |
Maggie17 | 2:fb6251306b21 | 212 | struct EMW3162_socket *socket = (struct EMW3162_socket *)handle; |
Maggie17 | 0:2d5090e1ceb4 | 213 | _cbs[socket->id].callback = callback; |
Maggie17 | 0:2d5090e1ceb4 | 214 | _cbs[socket->id].data = data; |
Maggie17 | 0:2d5090e1ceb4 | 215 | } |
Maggie17 | 0:2d5090e1ceb4 | 216 | |
Maggie17 | 2:fb6251306b21 | 217 | void EMW3162Interface::event() { |
Maggie17 | 2:fb6251306b21 | 218 | for (int i = 0; i < EMW3162_SOCKET_COUNT; i++) { |
Maggie17 | 0:2d5090e1ceb4 | 219 | if (_cbs[i].callback) { |
Maggie17 | 0:2d5090e1ceb4 | 220 | _cbs[i].callback(_cbs[i].data); |
Maggie17 | 0:2d5090e1ceb4 | 221 | } |
Maggie17 | 0:2d5090e1ceb4 | 222 | } |
Maggie17 | 0:2d5090e1ceb4 | 223 | } |