Added support for the WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Easy Connect

Easily add all supported connectivity methods to your mbed OS project

This project is derived from https://developer.mbed.org/teams/sandbox/code/simple-mbed-client-example/file/dd6231df71bb/easy-connect.lib. It give user the ability to switch between connectivity methods and includes support for the WNC14A2A Data Module. The `NetworkInterface` API makes this easy, but you still need a mechanism for the user to select the connection method, The selection is made by modifying the `mbed_app.json` file and using `easy_connect()` from your application.

Specifying connectivity method

To add support for the WNC14A2A, add the following to your ``mbed_app.json`` file:

mbed_app.json

{
    "config": {
        "network-interface":{
            "help": "options are ETHERNET,WIFI_ESP8266,WIFI_ODIN,MESH_LOWPAN_ND,MESH_THREAD,WNC14A2A",
            "value": "WNC14A2A"
        }
    },
}

After you choose `WNC14A2A` you'll also need to indicate if you want debug output or not by Enabling (true) or Disabling (false) WNC_DEBUG.

If WNC_DEBUG is enabled, there are 3 different levels of debug output (selected via bit settings). These debug levels are set using the following values:

ValueDescription
1Basic WNC driver debug output
2Comprehensive WNC driver debug output
4Network Layer debug output

You can have any combination of these three bit values for a total value of 0 – 7.

WNC Debug Settings

    "config": {
        "WNC_DEBUG": {
            "value": false
        },
        "WNC_DEBUG_SETTING": {
            "value": 4
        },
    }

Using Easy Connect from your application

Easy Connect has just one function which will either return a `NetworkInterface`-pointer or `NULL`:

Sample Code

#include "easy-connect.h"

int main(int, char**) {
    NetworkInterface* network = easy_connect(true); /* has 1 argument, enable_logging (pass in true to log to serial port) */
    if (!network) {
        printf("Connecting to the network failed... See serial output.\r\n");
        return 1;
    }
 
    // Rest of your program
}

Tested on

  • K64F with Ethernet.
  • AT&T Cellular IoT Starter Kit with WNC M14A2A Cellular Data Module

The WNCInterface class currently supports the following version(s):

  • MPSS: M14A2A_v11.50.164451 APSS: M14A2A_v11.53.164451

License

This library is released under the Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License and may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Committer:
group-Avnet
Date:
Wed Apr 19 01:08:11 2017 +0000
Revision:
0:478cfd88041f
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-Avnet 0:478cfd88041f 1 /* ESP8266 implementation of NetworkInterfaceAPI
group-Avnet 0:478cfd88041f 2 * Copyright (c) 2015 ARM Limited
group-Avnet 0:478cfd88041f 3 *
group-Avnet 0:478cfd88041f 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-Avnet 0:478cfd88041f 5 * you may not use this file except in compliance with the License.
group-Avnet 0:478cfd88041f 6 * You may obtain a copy of the License at
group-Avnet 0:478cfd88041f 7 *
group-Avnet 0:478cfd88041f 8 * http://www.apache.org/licenses/LICENSE-2.0
group-Avnet 0:478cfd88041f 9 *
group-Avnet 0:478cfd88041f 10 * Unless required by applicable law or agreed to in writing, software
group-Avnet 0:478cfd88041f 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-Avnet 0:478cfd88041f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-Avnet 0:478cfd88041f 13 * See the License for the specific language governing permissions and
group-Avnet 0:478cfd88041f 14 * limitations under the License.
group-Avnet 0:478cfd88041f 15 */
group-Avnet 0:478cfd88041f 16
group-Avnet 0:478cfd88041f 17 #include <string.h>
group-Avnet 0:478cfd88041f 18 #include "ESP8266Interface.h"
group-Avnet 0:478cfd88041f 19
group-Avnet 0:478cfd88041f 20 // Various timeouts for different ESP8266 operations
group-Avnet 0:478cfd88041f 21 #define ESP8266_CONNECT_TIMEOUT 15000
group-Avnet 0:478cfd88041f 22 #define ESP8266_SEND_TIMEOUT 500
group-Avnet 0:478cfd88041f 23 #define ESP8266_RECV_TIMEOUT 0
group-Avnet 0:478cfd88041f 24 #define ESP8266_MISC_TIMEOUT 500
group-Avnet 0:478cfd88041f 25
group-Avnet 0:478cfd88041f 26 // ESP8266Interface implementation
group-Avnet 0:478cfd88041f 27 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug)
group-Avnet 0:478cfd88041f 28 : _esp(tx, rx, debug)
group-Avnet 0:478cfd88041f 29 {
group-Avnet 0:478cfd88041f 30 memset(_ids, 0, sizeof(_ids));
group-Avnet 0:478cfd88041f 31 memset(_cbs, 0, sizeof(_cbs));
group-Avnet 0:478cfd88041f 32
group-Avnet 0:478cfd88041f 33 _esp.attach(this, &ESP8266Interface::event);
group-Avnet 0:478cfd88041f 34 }
group-Avnet 0:478cfd88041f 35
group-Avnet 0:478cfd88041f 36 int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
group-Avnet 0:478cfd88041f 37 uint8_t channel)
group-Avnet 0:478cfd88041f 38 {
group-Avnet 0:478cfd88041f 39 if (channel != 0) {
group-Avnet 0:478cfd88041f 40 return NSAPI_ERROR_UNSUPPORTED;
group-Avnet 0:478cfd88041f 41 }
group-Avnet 0:478cfd88041f 42
group-Avnet 0:478cfd88041f 43 set_credentials(ssid, pass, security);
group-Avnet 0:478cfd88041f 44 return connect();
group-Avnet 0:478cfd88041f 45 }
group-Avnet 0:478cfd88041f 46
group-Avnet 0:478cfd88041f 47 int ESP8266Interface::connect()
group-Avnet 0:478cfd88041f 48 {
group-Avnet 0:478cfd88041f 49 _esp.setTimeout(ESP8266_CONNECT_TIMEOUT);
group-Avnet 0:478cfd88041f 50
group-Avnet 0:478cfd88041f 51 if (!_esp.startup(3)) {
group-Avnet 0:478cfd88041f 52 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 53 }
group-Avnet 0:478cfd88041f 54
group-Avnet 0:478cfd88041f 55 if (!_esp.dhcp(true, 1)) {
group-Avnet 0:478cfd88041f 56 return NSAPI_ERROR_DHCP_FAILURE;
group-Avnet 0:478cfd88041f 57 }
group-Avnet 0:478cfd88041f 58
group-Avnet 0:478cfd88041f 59 if (!_esp.connect(ap_ssid, ap_pass)) {
group-Avnet 0:478cfd88041f 60 return NSAPI_ERROR_NO_CONNECTION;
group-Avnet 0:478cfd88041f 61 }
group-Avnet 0:478cfd88041f 62
group-Avnet 0:478cfd88041f 63 if (!_esp.getIPAddress()) {
group-Avnet 0:478cfd88041f 64 return NSAPI_ERROR_DHCP_FAILURE;
group-Avnet 0:478cfd88041f 65 }
group-Avnet 0:478cfd88041f 66
group-Avnet 0:478cfd88041f 67 return NSAPI_ERROR_OK;
group-Avnet 0:478cfd88041f 68 }
group-Avnet 0:478cfd88041f 69
group-Avnet 0:478cfd88041f 70 int ESP8266Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
group-Avnet 0:478cfd88041f 71 {
group-Avnet 0:478cfd88041f 72 memset(ap_ssid, 0, sizeof(ap_ssid));
group-Avnet 0:478cfd88041f 73 strncpy(ap_ssid, ssid, sizeof(ap_ssid));
group-Avnet 0:478cfd88041f 74
group-Avnet 0:478cfd88041f 75 memset(ap_pass, 0, sizeof(ap_pass));
group-Avnet 0:478cfd88041f 76 strncpy(ap_pass, pass, sizeof(ap_pass));
group-Avnet 0:478cfd88041f 77
group-Avnet 0:478cfd88041f 78 ap_sec = security;
group-Avnet 0:478cfd88041f 79
group-Avnet 0:478cfd88041f 80 return 0;
group-Avnet 0:478cfd88041f 81 }
group-Avnet 0:478cfd88041f 82
group-Avnet 0:478cfd88041f 83 int ESP8266Interface::set_channel(uint8_t channel)
group-Avnet 0:478cfd88041f 84 {
group-Avnet 0:478cfd88041f 85 return NSAPI_ERROR_UNSUPPORTED;
group-Avnet 0:478cfd88041f 86 }
group-Avnet 0:478cfd88041f 87
group-Avnet 0:478cfd88041f 88
group-Avnet 0:478cfd88041f 89 int ESP8266Interface::disconnect()
group-Avnet 0:478cfd88041f 90 {
group-Avnet 0:478cfd88041f 91 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-Avnet 0:478cfd88041f 92
group-Avnet 0:478cfd88041f 93 if (!_esp.disconnect()) {
group-Avnet 0:478cfd88041f 94 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 95 }
group-Avnet 0:478cfd88041f 96
group-Avnet 0:478cfd88041f 97 return NSAPI_ERROR_OK;
group-Avnet 0:478cfd88041f 98 }
group-Avnet 0:478cfd88041f 99
group-Avnet 0:478cfd88041f 100 const char *ESP8266Interface::get_ip_address()
group-Avnet 0:478cfd88041f 101 {
group-Avnet 0:478cfd88041f 102 return _esp.getIPAddress();
group-Avnet 0:478cfd88041f 103 }
group-Avnet 0:478cfd88041f 104
group-Avnet 0:478cfd88041f 105 const char *ESP8266Interface::get_mac_address()
group-Avnet 0:478cfd88041f 106 {
group-Avnet 0:478cfd88041f 107 return _esp.getMACAddress();
group-Avnet 0:478cfd88041f 108 }
group-Avnet 0:478cfd88041f 109
group-Avnet 0:478cfd88041f 110 const char *ESP8266Interface::get_gateway()
group-Avnet 0:478cfd88041f 111 {
group-Avnet 0:478cfd88041f 112 return _esp.getGateway();
group-Avnet 0:478cfd88041f 113 }
group-Avnet 0:478cfd88041f 114
group-Avnet 0:478cfd88041f 115 const char *ESP8266Interface::get_netmask()
group-Avnet 0:478cfd88041f 116 {
group-Avnet 0:478cfd88041f 117 return _esp.getNetmask();
group-Avnet 0:478cfd88041f 118 }
group-Avnet 0:478cfd88041f 119
group-Avnet 0:478cfd88041f 120 int8_t ESP8266Interface::get_rssi()
group-Avnet 0:478cfd88041f 121 {
group-Avnet 0:478cfd88041f 122 return _esp.getRSSI();
group-Avnet 0:478cfd88041f 123 }
group-Avnet 0:478cfd88041f 124
group-Avnet 0:478cfd88041f 125 int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
group-Avnet 0:478cfd88041f 126 {
group-Avnet 0:478cfd88041f 127 return _esp.scan(res, count);
group-Avnet 0:478cfd88041f 128 }
group-Avnet 0:478cfd88041f 129
group-Avnet 0:478cfd88041f 130 struct esp8266_socket {
group-Avnet 0:478cfd88041f 131 int id;
group-Avnet 0:478cfd88041f 132 nsapi_protocol_t proto;
group-Avnet 0:478cfd88041f 133 bool connected;
group-Avnet 0:478cfd88041f 134 SocketAddress addr;
group-Avnet 0:478cfd88041f 135 };
group-Avnet 0:478cfd88041f 136
group-Avnet 0:478cfd88041f 137 int ESP8266Interface::socket_open(void **handle, nsapi_protocol_t proto)
group-Avnet 0:478cfd88041f 138 {
group-Avnet 0:478cfd88041f 139 // Look for an unused socket
group-Avnet 0:478cfd88041f 140 int id = -1;
group-Avnet 0:478cfd88041f 141
group-Avnet 0:478cfd88041f 142 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-Avnet 0:478cfd88041f 143 if (!_ids[i]) {
group-Avnet 0:478cfd88041f 144 id = i;
group-Avnet 0:478cfd88041f 145 _ids[i] = true;
group-Avnet 0:478cfd88041f 146 break;
group-Avnet 0:478cfd88041f 147 }
group-Avnet 0:478cfd88041f 148 }
group-Avnet 0:478cfd88041f 149
group-Avnet 0:478cfd88041f 150 if (id == -1) {
group-Avnet 0:478cfd88041f 151 return NSAPI_ERROR_NO_SOCKET;
group-Avnet 0:478cfd88041f 152 }
group-Avnet 0:478cfd88041f 153
group-Avnet 0:478cfd88041f 154 struct esp8266_socket *socket = new struct esp8266_socket;
group-Avnet 0:478cfd88041f 155 if (!socket) {
group-Avnet 0:478cfd88041f 156 return NSAPI_ERROR_NO_SOCKET;
group-Avnet 0:478cfd88041f 157 }
group-Avnet 0:478cfd88041f 158
group-Avnet 0:478cfd88041f 159 socket->id = id;
group-Avnet 0:478cfd88041f 160 socket->proto = proto;
group-Avnet 0:478cfd88041f 161 socket->connected = false;
group-Avnet 0:478cfd88041f 162 *handle = socket;
group-Avnet 0:478cfd88041f 163 return 0;
group-Avnet 0:478cfd88041f 164 }
group-Avnet 0:478cfd88041f 165
group-Avnet 0:478cfd88041f 166 int ESP8266Interface::socket_close(void *handle)
group-Avnet 0:478cfd88041f 167 {
group-Avnet 0:478cfd88041f 168 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 169 int err = 0;
group-Avnet 0:478cfd88041f 170 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-Avnet 0:478cfd88041f 171
group-Avnet 0:478cfd88041f 172 if (!_esp.close(socket->id)) {
group-Avnet 0:478cfd88041f 173 err = NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 174 }
group-Avnet 0:478cfd88041f 175
group-Avnet 0:478cfd88041f 176 _ids[socket->id] = false;
group-Avnet 0:478cfd88041f 177 delete socket;
group-Avnet 0:478cfd88041f 178 return err;
group-Avnet 0:478cfd88041f 179 }
group-Avnet 0:478cfd88041f 180
group-Avnet 0:478cfd88041f 181 int ESP8266Interface::socket_bind(void *handle, const SocketAddress &address)
group-Avnet 0:478cfd88041f 182 {
group-Avnet 0:478cfd88041f 183 return NSAPI_ERROR_UNSUPPORTED;
group-Avnet 0:478cfd88041f 184 }
group-Avnet 0:478cfd88041f 185
group-Avnet 0:478cfd88041f 186 int ESP8266Interface::socket_listen(void *handle, int backlog)
group-Avnet 0:478cfd88041f 187 {
group-Avnet 0:478cfd88041f 188 return NSAPI_ERROR_UNSUPPORTED;
group-Avnet 0:478cfd88041f 189 }
group-Avnet 0:478cfd88041f 190
group-Avnet 0:478cfd88041f 191 int ESP8266Interface::socket_connect(void *handle, const SocketAddress &addr)
group-Avnet 0:478cfd88041f 192 {
group-Avnet 0:478cfd88041f 193 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 194 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-Avnet 0:478cfd88041f 195
group-Avnet 0:478cfd88041f 196 const char *proto = (socket->proto == NSAPI_UDP) ? "UDP" : "TCP";
group-Avnet 0:478cfd88041f 197 if (!_esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port())) {
group-Avnet 0:478cfd88041f 198 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 199 }
group-Avnet 0:478cfd88041f 200
group-Avnet 0:478cfd88041f 201 socket->connected = true;
group-Avnet 0:478cfd88041f 202 return 0;
group-Avnet 0:478cfd88041f 203 }
group-Avnet 0:478cfd88041f 204
group-Avnet 0:478cfd88041f 205 int ESP8266Interface::socket_accept(void *server, void **socket, SocketAddress *addr)
group-Avnet 0:478cfd88041f 206 {
group-Avnet 0:478cfd88041f 207 return NSAPI_ERROR_UNSUPPORTED;
group-Avnet 0:478cfd88041f 208 }
group-Avnet 0:478cfd88041f 209
group-Avnet 0:478cfd88041f 210 int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
group-Avnet 0:478cfd88041f 211 {
group-Avnet 0:478cfd88041f 212 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 213 _esp.setTimeout(ESP8266_SEND_TIMEOUT);
group-Avnet 0:478cfd88041f 214
group-Avnet 0:478cfd88041f 215 if (!_esp.send(socket->id, data, size)) {
group-Avnet 0:478cfd88041f 216 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 217 }
group-Avnet 0:478cfd88041f 218
group-Avnet 0:478cfd88041f 219 return size;
group-Avnet 0:478cfd88041f 220 }
group-Avnet 0:478cfd88041f 221
group-Avnet 0:478cfd88041f 222 int ESP8266Interface::socket_recv(void *handle, void *data, unsigned size)
group-Avnet 0:478cfd88041f 223 {
group-Avnet 0:478cfd88041f 224 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 225 _esp.setTimeout(ESP8266_RECV_TIMEOUT);
group-Avnet 0:478cfd88041f 226
group-Avnet 0:478cfd88041f 227 int32_t recv = _esp.recv(socket->id, data, size);
group-Avnet 0:478cfd88041f 228 if (recv < 0) {
group-Avnet 0:478cfd88041f 229 return NSAPI_ERROR_WOULD_BLOCK;
group-Avnet 0:478cfd88041f 230 }
group-Avnet 0:478cfd88041f 231
group-Avnet 0:478cfd88041f 232 return recv;
group-Avnet 0:478cfd88041f 233 }
group-Avnet 0:478cfd88041f 234
group-Avnet 0:478cfd88041f 235 int ESP8266Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
group-Avnet 0:478cfd88041f 236 {
group-Avnet 0:478cfd88041f 237 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 238
group-Avnet 0:478cfd88041f 239 if (socket->connected && socket->addr != addr) {
group-Avnet 0:478cfd88041f 240 _esp.setTimeout(ESP8266_MISC_TIMEOUT);
group-Avnet 0:478cfd88041f 241 if (!_esp.close(socket->id)) {
group-Avnet 0:478cfd88041f 242 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 243 }
group-Avnet 0:478cfd88041f 244 socket->connected = false;
group-Avnet 0:478cfd88041f 245 }
group-Avnet 0:478cfd88041f 246
group-Avnet 0:478cfd88041f 247 if (!socket->connected) {
group-Avnet 0:478cfd88041f 248 int err = socket_connect(socket, addr);
group-Avnet 0:478cfd88041f 249 if (err < 0) {
group-Avnet 0:478cfd88041f 250 return err;
group-Avnet 0:478cfd88041f 251 }
group-Avnet 0:478cfd88041f 252 socket->addr = addr;
group-Avnet 0:478cfd88041f 253 }
group-Avnet 0:478cfd88041f 254
group-Avnet 0:478cfd88041f 255 return socket_send(socket, data, size);
group-Avnet 0:478cfd88041f 256 }
group-Avnet 0:478cfd88041f 257
group-Avnet 0:478cfd88041f 258 int ESP8266Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
group-Avnet 0:478cfd88041f 259 {
group-Avnet 0:478cfd88041f 260 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 261 int ret = socket_recv(socket, data, size);
group-Avnet 0:478cfd88041f 262 if (ret >= 0 && addr) {
group-Avnet 0:478cfd88041f 263 *addr = socket->addr;
group-Avnet 0:478cfd88041f 264 }
group-Avnet 0:478cfd88041f 265
group-Avnet 0:478cfd88041f 266 return ret;
group-Avnet 0:478cfd88041f 267 }
group-Avnet 0:478cfd88041f 268
group-Avnet 0:478cfd88041f 269 void ESP8266Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
group-Avnet 0:478cfd88041f 270 {
group-Avnet 0:478cfd88041f 271 struct esp8266_socket *socket = (struct esp8266_socket *)handle;
group-Avnet 0:478cfd88041f 272 _cbs[socket->id].callback = callback;
group-Avnet 0:478cfd88041f 273 _cbs[socket->id].data = data;
group-Avnet 0:478cfd88041f 274 }
group-Avnet 0:478cfd88041f 275
group-Avnet 0:478cfd88041f 276 void ESP8266Interface::event() {
group-Avnet 0:478cfd88041f 277 for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {
group-Avnet 0:478cfd88041f 278 if (_cbs[i].callback) {
group-Avnet 0:478cfd88041f 279 _cbs[i].callback(_cbs[i].data);
group-Avnet 0:478cfd88041f 280 }
group-Avnet 0:478cfd88041f 281 }
group-Avnet 0:478cfd88041f 282 }