emw3162 driver

Fork of emw3162-driver-mbed-os-5 by Maggie Mei

Committer:
sarahmarshy
Date:
Thu Feb 16 19:24:40 2017 +0000
Revision:
6:80cdb5076d18
Parent:
5:a8a6cb7e1128
Child:
7:635ff20df767
Return NSAPI_ERROR_UNSUPPORTED for scan.

Who changed what in which revision?

UserRevisionLine numberNew 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 3:a6f80a95b039 37 int EMW3162Interface::connect()
Maggie17 3:a6f80a95b039 38 {
Maggie17 2:fb6251306b21 39 _esp.setTimeout(EMW3162_CONNECT_TIMEOUT);
Maggie17 0:2d5090e1ceb4 40
Maggie17 0:2d5090e1ceb4 41 if (!_esp.startup()) {
Maggie17 0:2d5090e1ceb4 42 return NSAPI_ERROR_DEVICE_ERROR;
Maggie17 0:2d5090e1ceb4 43 }
Maggie17 0:2d5090e1ceb4 44
Maggie17 0:2d5090e1ceb4 45 if (!_esp.dhcp(true)) {
Maggie17 0:2d5090e1ceb4 46 return NSAPI_ERROR_DHCP_FAILURE;
Maggie17 0:2d5090e1ceb4 47 }
Maggie17 0:2d5090e1ceb4 48
sarahmarshy 5:a8a6cb7e1128 49 if (!_esp.connect(ap_ssid, ap_pass)) {
Maggie17 0:2d5090e1ceb4 50 return NSAPI_ERROR_NO_CONNECTION;
Maggie17 0:2d5090e1ceb4 51 }
Maggie17 0:2d5090e1ceb4 52
Maggie17 0:2d5090e1ceb4 53 if (!_esp.getIPAddress()) {
Maggie17 0:2d5090e1ceb4 54 return NSAPI_ERROR_DHCP_FAILURE;
Maggie17 0:2d5090e1ceb4 55 }
Maggie17 0:2d5090e1ceb4 56
sarahmarshy 5:a8a6cb7e1128 57 return 0;
sarahmarshy 5:a8a6cb7e1128 58 }
sarahmarshy 5:a8a6cb7e1128 59 int EMW3162Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
sarahmarshy 5:a8a6cb7e1128 60 uint8_t channel)
sarahmarshy 5:a8a6cb7e1128 61 {
sarahmarshy 5:a8a6cb7e1128 62 if (channel != 0) {
sarahmarshy 5:a8a6cb7e1128 63 return NSAPI_ERROR_UNSUPPORTED;
sarahmarshy 5:a8a6cb7e1128 64 }
sarahmarshy 5:a8a6cb7e1128 65
sarahmarshy 5:a8a6cb7e1128 66 set_credentials(ssid, pass, security);
sarahmarshy 5:a8a6cb7e1128 67 return connect();
Maggie17 3:a6f80a95b039 68 }
Maggie17 3:a6f80a95b039 69
Maggie17 3:a6f80a95b039 70 int EMW3162Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
Maggie17 3:a6f80a95b039 71 {
sarahmarshy 5:a8a6cb7e1128 72 memset(ap_ssid, 0, sizeof(ap_ssid));
sarahmarshy 5:a8a6cb7e1128 73 strncpy(ap_ssid, ssid, sizeof(ap_ssid));
sarahmarshy 5:a8a6cb7e1128 74
sarahmarshy 5:a8a6cb7e1128 75 memset(ap_pass, 0, sizeof(ap_pass));
sarahmarshy 5:a8a6cb7e1128 76 strncpy(ap_pass, pass, sizeof(ap_pass));
sarahmarshy 5:a8a6cb7e1128 77
sarahmarshy 5:a8a6cb7e1128 78 ap_sec = security;
sarahmarshy 5:a8a6cb7e1128 79
Maggie17 0:2d5090e1ceb4 80 return 0;
Maggie17 0:2d5090e1ceb4 81 }
Maggie17 0:2d5090e1ceb4 82
Maggie17 3:a6f80a95b039 83 int EMW3162Interface::set_channel(uint8_t channel)
Maggie17 3:a6f80a95b039 84 {
Maggie17 3:a6f80a95b039 85 return NSAPI_ERROR_UNSUPPORTED;
Maggie17 3:a6f80a95b039 86 }
Maggie17 3:a6f80a95b039 87
Maggie17 2:fb6251306b21 88 int EMW3162Interface::disconnect()
Maggie17 0:2d5090e1ceb4 89 {
Maggie17 2:fb6251306b21 90 _esp.setTimeout(EMW3162_MISC_TIMEOUT);
Maggie17 0:2d5090e1ceb4 91
Maggie17 0:2d5090e1ceb4 92 if (!_esp.disconnect()) {
Maggie17 0:2d5090e1ceb4 93 return NSAPI_ERROR_DEVICE_ERROR;
Maggie17 0:2d5090e1ceb4 94 }
Maggie17 0:2d5090e1ceb4 95
Maggie17 0:2d5090e1ceb4 96 return 0;
Maggie17 0:2d5090e1ceb4 97 }
Maggie17 0:2d5090e1ceb4 98
Maggie17 2:fb6251306b21 99 const char* EMW3162Interface::get_ip_address()
Maggie17 0:2d5090e1ceb4 100 {
Maggie17 0:2d5090e1ceb4 101 return _esp.getIPAddress();
Maggie17 0:2d5090e1ceb4 102 }
Maggie17 0:2d5090e1ceb4 103
Maggie17 2:fb6251306b21 104 const char* EMW3162Interface::get_mac_address()
Maggie17 0:2d5090e1ceb4 105 {
Maggie17 0:2d5090e1ceb4 106 return _esp.getMACAddress();
Maggie17 0:2d5090e1ceb4 107 }
Maggie17 0:2d5090e1ceb4 108
Maggie17 3:a6f80a95b039 109 const char *EMW3162Interface::get_gateway()
Maggie17 3:a6f80a95b039 110 {
Maggie17 3:a6f80a95b039 111 return NULL;
Maggie17 3:a6f80a95b039 112 }
Maggie17 3:a6f80a95b039 113
Maggie17 3:a6f80a95b039 114 const char *EMW3162Interface::get_netmask()
Maggie17 3:a6f80a95b039 115 {
Maggie17 3:a6f80a95b039 116 return NULL;
Maggie17 3:a6f80a95b039 117 }
Maggie17 3:a6f80a95b039 118
Maggie17 3:a6f80a95b039 119 int8_t EMW3162Interface::get_rssi()
Maggie17 3:a6f80a95b039 120 {
Maggie17 3:a6f80a95b039 121 return 0;
Maggie17 3:a6f80a95b039 122 }
Maggie17 3:a6f80a95b039 123
Maggie17 3:a6f80a95b039 124 int EMW3162Interface::scan(WiFiAccessPoint *res, unsigned count)
Maggie17 3:a6f80a95b039 125 {
sarahmarshy 6:80cdb5076d18 126 return NSAPI_ERROR_UNSUPPORTED;
Maggie17 3:a6f80a95b039 127 }
Maggie17 3:a6f80a95b039 128
Maggie17 2:fb6251306b21 129 struct EMW3162_socket {
Maggie17 0:2d5090e1ceb4 130 int id;
Maggie17 0:2d5090e1ceb4 131 int socketId;
Maggie17 0:2d5090e1ceb4 132 nsapi_protocol_t proto;
Maggie17 0:2d5090e1ceb4 133 bool connected;
Maggie17 0:2d5090e1ceb4 134 };
Maggie17 0:2d5090e1ceb4 135
Maggie17 2:fb6251306b21 136 int EMW3162Interface::socket_open(void **handle, nsapi_protocol_t proto)
Maggie17 0:2d5090e1ceb4 137 {
Maggie17 0:2d5090e1ceb4 138 // Look for an unused socket
Maggie17 0:2d5090e1ceb4 139 int id = -1;
Maggie17 0:2d5090e1ceb4 140
Maggie17 2:fb6251306b21 141 for (int i = 1; i < EMW3162_SOCKET_COUNT; i++) {
Maggie17 0:2d5090e1ceb4 142 if (!_ids[i]) {
Maggie17 0:2d5090e1ceb4 143 id = i;
Maggie17 0:2d5090e1ceb4 144 _ids[i] = true;
Maggie17 0:2d5090e1ceb4 145 break;
Maggie17 0:2d5090e1ceb4 146 }
Maggie17 0:2d5090e1ceb4 147 }
Maggie17 0:2d5090e1ceb4 148
Maggie17 0:2d5090e1ceb4 149 if (id == -1) {
Maggie17 0:2d5090e1ceb4 150 return NSAPI_ERROR_NO_SOCKET;
Maggie17 0:2d5090e1ceb4 151 }
Maggie17 0:2d5090e1ceb4 152
Maggie17 2:fb6251306b21 153 struct EMW3162_socket *socket = new struct EMW3162_socket;
Maggie17 0:2d5090e1ceb4 154 if (!socket) {
Maggie17 0:2d5090e1ceb4 155 return NSAPI_ERROR_NO_SOCKET;
Maggie17 0:2d5090e1ceb4 156 }
Maggie17 0:2d5090e1ceb4 157
Maggie17 0:2d5090e1ceb4 158 socket->id = id;
Maggie17 0:2d5090e1ceb4 159 socket->socketId = 0;
Maggie17 0:2d5090e1ceb4 160 socket->proto = proto;
Maggie17 0:2d5090e1ceb4 161 socket->connected = false;
Maggie17 0:2d5090e1ceb4 162 *handle = socket;
Maggie17 0:2d5090e1ceb4 163 return 0;
Maggie17 0:2d5090e1ceb4 164 }
Maggie17 0:2d5090e1ceb4 165
Maggie17 2:fb6251306b21 166 int EMW3162Interface::socket_close(void *handle)
Maggie17 0:2d5090e1ceb4 167 {
Maggie17 2:fb6251306b21 168 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 0:2d5090e1ceb4 169 int err = 0;
Maggie17 2:fb6251306b21 170 _esp.setTimeout(EMW3162_MISC_TIMEOUT);
Maggie17 0:2d5090e1ceb4 171
Maggie17 0:2d5090e1ceb4 172 if (!_esp.close(socket->socketId)) {
Maggie17 0:2d5090e1ceb4 173 err = NSAPI_ERROR_DEVICE_ERROR;
Maggie17 0:2d5090e1ceb4 174 }
Maggie17 0:2d5090e1ceb4 175
Maggie17 0:2d5090e1ceb4 176 _ids[socket->id] = false;
Maggie17 0:2d5090e1ceb4 177 delete socket;
Maggie17 0:2d5090e1ceb4 178 return err;
Maggie17 0:2d5090e1ceb4 179 }
Maggie17 0:2d5090e1ceb4 180
Maggie17 2:fb6251306b21 181 int EMW3162Interface::socket_bind(void *handle, const SocketAddress &address)
Maggie17 0:2d5090e1ceb4 182 {
Maggie17 0:2d5090e1ceb4 183 return NSAPI_ERROR_UNSUPPORTED;
Maggie17 0:2d5090e1ceb4 184 }
Maggie17 0:2d5090e1ceb4 185
Maggie17 2:fb6251306b21 186 int EMW3162Interface::socket_listen(void *handle, int backlog)
Maggie17 0:2d5090e1ceb4 187 {
Maggie17 0:2d5090e1ceb4 188 return NSAPI_ERROR_UNSUPPORTED;
Maggie17 0:2d5090e1ceb4 189 }
Maggie17 0:2d5090e1ceb4 190
Maggie17 2:fb6251306b21 191 int EMW3162Interface::socket_connect(void *handle, const SocketAddress &addr)
Maggie17 0:2d5090e1ceb4 192 {
Maggie17 2:fb6251306b21 193 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 2:fb6251306b21 194 _esp.setTimeout(EMW3162_MISC_TIMEOUT);
Maggie17 0:2d5090e1ceb4 195
Maggie17 0:2d5090e1ceb4 196 const char *proto = (socket->proto == NSAPI_UDP) ? "UNICAST" : "CLIENT";
Maggie17 0:2d5090e1ceb4 197 socket -> socketId = _esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port());
Maggie17 0:2d5090e1ceb4 198 if (!(socket -> socketId)) {
Maggie17 0:2d5090e1ceb4 199 return NSAPI_ERROR_DEVICE_ERROR;
Maggie17 0:2d5090e1ceb4 200 }
Maggie17 0:2d5090e1ceb4 201
Maggie17 0:2d5090e1ceb4 202 socket->connected = true;
Maggie17 0:2d5090e1ceb4 203 return 0;
Maggie17 0:2d5090e1ceb4 204 }
Maggie17 0:2d5090e1ceb4 205
Maggie17 3:a6f80a95b039 206 int EMW3162Interface::socket_accept(void *handle, void **socket, SocketAddress *address)
Maggie17 0:2d5090e1ceb4 207 {
Maggie17 0:2d5090e1ceb4 208 return NSAPI_ERROR_UNSUPPORTED;
Maggie17 0:2d5090e1ceb4 209 }
Maggie17 0:2d5090e1ceb4 210
Maggie17 2:fb6251306b21 211 int EMW3162Interface::socket_send(void *handle, const void *data, unsigned size)
Maggie17 0:2d5090e1ceb4 212 {
Maggie17 2:fb6251306b21 213 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 2:fb6251306b21 214 _esp.setTimeout(EMW3162_SEND_TIMEOUT);
Maggie17 0:2d5090e1ceb4 215
Maggie17 0:2d5090e1ceb4 216 if (!_esp.send(socket->socketId, data, size)) {
Maggie17 0:2d5090e1ceb4 217 return NSAPI_ERROR_DEVICE_ERROR;
Maggie17 0:2d5090e1ceb4 218 }
Maggie17 0:2d5090e1ceb4 219
Maggie17 0:2d5090e1ceb4 220 return size;
Maggie17 0:2d5090e1ceb4 221 }
Maggie17 0:2d5090e1ceb4 222
Maggie17 2:fb6251306b21 223 int EMW3162Interface::socket_recv(void *handle, void *data, unsigned size)
Maggie17 0:2d5090e1ceb4 224 {
Maggie17 2:fb6251306b21 225 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 2:fb6251306b21 226 _esp.setTimeout(EMW3162_RECV_TIMEOUT);
Maggie17 0:2d5090e1ceb4 227
Maggie17 0:2d5090e1ceb4 228 int32_t recv = _esp.recv(socket->socketId, data, size);
Maggie17 0:2d5090e1ceb4 229 if (recv < 0) {
Maggie17 0:2d5090e1ceb4 230 return NSAPI_ERROR_WOULD_BLOCK;
Maggie17 0:2d5090e1ceb4 231 }
Maggie17 0:2d5090e1ceb4 232
Maggie17 0:2d5090e1ceb4 233 return recv;
Maggie17 0:2d5090e1ceb4 234 }
Maggie17 0:2d5090e1ceb4 235
Maggie17 2:fb6251306b21 236 int EMW3162Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
Maggie17 0:2d5090e1ceb4 237 {
Maggie17 2:fb6251306b21 238 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 0:2d5090e1ceb4 239 if (!socket->connected) {
Maggie17 0:2d5090e1ceb4 240 int err = socket_connect(socket, addr);
Maggie17 0:2d5090e1ceb4 241 if (err < 0) {
Maggie17 0:2d5090e1ceb4 242 return err;
Maggie17 0:2d5090e1ceb4 243 }
Maggie17 0:2d5090e1ceb4 244 }
Maggie17 0:2d5090e1ceb4 245
Maggie17 0:2d5090e1ceb4 246 return socket_send(socket, data, size);
Maggie17 0:2d5090e1ceb4 247 }
Maggie17 0:2d5090e1ceb4 248
Maggie17 2:fb6251306b21 249 int EMW3162Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
Maggie17 0:2d5090e1ceb4 250 {
Maggie17 2:fb6251306b21 251 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 0:2d5090e1ceb4 252 return socket_recv(socket, data, size);
Maggie17 0:2d5090e1ceb4 253 }
Maggie17 0:2d5090e1ceb4 254
Maggie17 2:fb6251306b21 255 void EMW3162Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
Maggie17 0:2d5090e1ceb4 256 {
Maggie17 2:fb6251306b21 257 struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
Maggie17 0:2d5090e1ceb4 258 _cbs[socket->id].callback = callback;
Maggie17 0:2d5090e1ceb4 259 _cbs[socket->id].data = data;
Maggie17 0:2d5090e1ceb4 260 }
Maggie17 0:2d5090e1ceb4 261
Maggie17 2:fb6251306b21 262 void EMW3162Interface::event() {
Maggie17 2:fb6251306b21 263 for (int i = 0; i < EMW3162_SOCKET_COUNT; i++) {
Maggie17 0:2d5090e1ceb4 264 if (_cbs[i].callback) {
Maggie17 0:2d5090e1ceb4 265 _cbs[i].callback(_cbs[i].data);
Maggie17 0:2d5090e1ceb4 266 }
Maggie17 0:2d5090e1ceb4 267 }
Maggie17 0:2d5090e1ceb4 268 }