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 Example
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 "ESP8266.h"
group-Avnet 0:478cfd88041f 18
group-Avnet 0:478cfd88041f 19 ESP8266::ESP8266(PinName tx, PinName rx, bool debug)
group-Avnet 0:478cfd88041f 20 : _serial(tx, rx, 1024), _parser(_serial)
group-Avnet 0:478cfd88041f 21 , _packets(0), _packets_end(&_packets)
group-Avnet 0:478cfd88041f 22 {
group-Avnet 0:478cfd88041f 23 _serial.baud(115200);
group-Avnet 0:478cfd88041f 24 _parser.debugOn(debug);
group-Avnet 0:478cfd88041f 25 }
group-Avnet 0:478cfd88041f 26
group-Avnet 0:478cfd88041f 27 bool ESP8266::startup(int mode)
group-Avnet 0:478cfd88041f 28 {
group-Avnet 0:478cfd88041f 29 //only 3 valid modes
group-Avnet 0:478cfd88041f 30 if(mode < 1 || mode > 3) {
group-Avnet 0:478cfd88041f 31 return false;
group-Avnet 0:478cfd88041f 32 }
group-Avnet 0:478cfd88041f 33
group-Avnet 0:478cfd88041f 34 bool success = reset()
group-Avnet 0:478cfd88041f 35 && _parser.send("AT+CWMODE=%d", mode)
group-Avnet 0:478cfd88041f 36 && _parser.recv("OK")
group-Avnet 0:478cfd88041f 37 && _parser.send("AT+CIPMUX=1")
group-Avnet 0:478cfd88041f 38 && _parser.recv("OK");
group-Avnet 0:478cfd88041f 39
group-Avnet 0:478cfd88041f 40 _parser.oob("+IPD", this, &ESP8266::_packet_handler);
group-Avnet 0:478cfd88041f 41
group-Avnet 0:478cfd88041f 42 return success;
group-Avnet 0:478cfd88041f 43 }
group-Avnet 0:478cfd88041f 44
group-Avnet 0:478cfd88041f 45 bool ESP8266::reset(void)
group-Avnet 0:478cfd88041f 46 {
group-Avnet 0:478cfd88041f 47 for (int i = 0; i < 2; i++) {
group-Avnet 0:478cfd88041f 48 if (_parser.send("AT+RST")
group-Avnet 0:478cfd88041f 49 && _parser.recv("OK\r\nready")) {
group-Avnet 0:478cfd88041f 50 return true;
group-Avnet 0:478cfd88041f 51 }
group-Avnet 0:478cfd88041f 52 }
group-Avnet 0:478cfd88041f 53
group-Avnet 0:478cfd88041f 54 return false;
group-Avnet 0:478cfd88041f 55 }
group-Avnet 0:478cfd88041f 56
group-Avnet 0:478cfd88041f 57 bool ESP8266::dhcp(bool enabled, int mode)
group-Avnet 0:478cfd88041f 58 {
group-Avnet 0:478cfd88041f 59 //only 3 valid modes
group-Avnet 0:478cfd88041f 60 if(mode < 0 || mode > 2) {
group-Avnet 0:478cfd88041f 61 return false;
group-Avnet 0:478cfd88041f 62 }
group-Avnet 0:478cfd88041f 63
group-Avnet 0:478cfd88041f 64 return _parser.send("AT+CWDHCP=%d,%d", enabled?1:0, mode)
group-Avnet 0:478cfd88041f 65 && _parser.recv("OK");
group-Avnet 0:478cfd88041f 66 }
group-Avnet 0:478cfd88041f 67
group-Avnet 0:478cfd88041f 68 bool ESP8266::connect(const char *ap, const char *passPhrase)
group-Avnet 0:478cfd88041f 69 {
group-Avnet 0:478cfd88041f 70 return _parser.send("AT+CWJAP=\"%s\",\"%s\"", ap, passPhrase)
group-Avnet 0:478cfd88041f 71 && _parser.recv("OK");
group-Avnet 0:478cfd88041f 72 }
group-Avnet 0:478cfd88041f 73
group-Avnet 0:478cfd88041f 74 bool ESP8266::disconnect(void)
group-Avnet 0:478cfd88041f 75 {
group-Avnet 0:478cfd88041f 76 return _parser.send("AT+CWQAP") && _parser.recv("OK");
group-Avnet 0:478cfd88041f 77 }
group-Avnet 0:478cfd88041f 78
group-Avnet 0:478cfd88041f 79 const char *ESP8266::getIPAddress(void)
group-Avnet 0:478cfd88041f 80 {
group-Avnet 0:478cfd88041f 81 if (!(_parser.send("AT+CIFSR")
group-Avnet 0:478cfd88041f 82 && _parser.recv("+CIFSR:STAIP,\"%15[^\"]\"", _ip_buffer)
group-Avnet 0:478cfd88041f 83 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 84 return 0;
group-Avnet 0:478cfd88041f 85 }
group-Avnet 0:478cfd88041f 86
group-Avnet 0:478cfd88041f 87 return _ip_buffer;
group-Avnet 0:478cfd88041f 88 }
group-Avnet 0:478cfd88041f 89
group-Avnet 0:478cfd88041f 90 const char *ESP8266::getMACAddress(void)
group-Avnet 0:478cfd88041f 91 {
group-Avnet 0:478cfd88041f 92 if (!(_parser.send("AT+CIFSR")
group-Avnet 0:478cfd88041f 93 && _parser.recv("+CIFSR:STAMAC,\"%17[^\"]\"", _mac_buffer)
group-Avnet 0:478cfd88041f 94 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 95 return 0;
group-Avnet 0:478cfd88041f 96 }
group-Avnet 0:478cfd88041f 97
group-Avnet 0:478cfd88041f 98 return _mac_buffer;
group-Avnet 0:478cfd88041f 99 }
group-Avnet 0:478cfd88041f 100
group-Avnet 0:478cfd88041f 101 const char *ESP8266::getGateway()
group-Avnet 0:478cfd88041f 102 {
group-Avnet 0:478cfd88041f 103 if (!(_parser.send("AT+CIPSTA?")
group-Avnet 0:478cfd88041f 104 && _parser.recv("+CIPSTA:gateway:\"%15[^\"]\"", _gateway_buffer)
group-Avnet 0:478cfd88041f 105 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 106 return 0;
group-Avnet 0:478cfd88041f 107 }
group-Avnet 0:478cfd88041f 108
group-Avnet 0:478cfd88041f 109 return _gateway_buffer;
group-Avnet 0:478cfd88041f 110 }
group-Avnet 0:478cfd88041f 111
group-Avnet 0:478cfd88041f 112 const char *ESP8266::getNetmask()
group-Avnet 0:478cfd88041f 113 {
group-Avnet 0:478cfd88041f 114 if (!(_parser.send("AT+CIPSTA?")
group-Avnet 0:478cfd88041f 115 && _parser.recv("+CIPSTA:netmask:\"%15[^\"]\"", _netmask_buffer)
group-Avnet 0:478cfd88041f 116 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 117 return 0;
group-Avnet 0:478cfd88041f 118 }
group-Avnet 0:478cfd88041f 119
group-Avnet 0:478cfd88041f 120 return _netmask_buffer;
group-Avnet 0:478cfd88041f 121 }
group-Avnet 0:478cfd88041f 122
group-Avnet 0:478cfd88041f 123 int8_t ESP8266::getRSSI()
group-Avnet 0:478cfd88041f 124 {
group-Avnet 0:478cfd88041f 125 int8_t rssi;
group-Avnet 0:478cfd88041f 126 char bssid[18];
group-Avnet 0:478cfd88041f 127
group-Avnet 0:478cfd88041f 128 if (!(_parser.send("AT+CWJAP?")
group-Avnet 0:478cfd88041f 129 && _parser.recv("+CWJAP:\"%*[^\"]\",\"%17[^\"]\"", bssid)
group-Avnet 0:478cfd88041f 130 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 131 return 0;
group-Avnet 0:478cfd88041f 132 }
group-Avnet 0:478cfd88041f 133
group-Avnet 0:478cfd88041f 134 if (!(_parser.send("AT+CWLAP=\"\",\"%s\",", bssid)
group-Avnet 0:478cfd88041f 135 && _parser.recv("+CWLAP:(%*d,\"%*[^\"]\",%hhd,", &rssi)
group-Avnet 0:478cfd88041f 136 && _parser.recv("OK"))) {
group-Avnet 0:478cfd88041f 137 return 0;
group-Avnet 0:478cfd88041f 138 }
group-Avnet 0:478cfd88041f 139
group-Avnet 0:478cfd88041f 140 return rssi;
group-Avnet 0:478cfd88041f 141 }
group-Avnet 0:478cfd88041f 142
group-Avnet 0:478cfd88041f 143 bool ESP8266::isConnected(void)
group-Avnet 0:478cfd88041f 144 {
group-Avnet 0:478cfd88041f 145 return getIPAddress() != 0;
group-Avnet 0:478cfd88041f 146 }
group-Avnet 0:478cfd88041f 147
group-Avnet 0:478cfd88041f 148 int ESP8266::scan(WiFiAccessPoint *res, unsigned limit)
group-Avnet 0:478cfd88041f 149 {
group-Avnet 0:478cfd88041f 150 unsigned cnt = 0;
group-Avnet 0:478cfd88041f 151 nsapi_wifi_ap_t ap;
group-Avnet 0:478cfd88041f 152
group-Avnet 0:478cfd88041f 153 if (!_parser.send("AT+CWLAP")) {
group-Avnet 0:478cfd88041f 154 return NSAPI_ERROR_DEVICE_ERROR;
group-Avnet 0:478cfd88041f 155 }
group-Avnet 0:478cfd88041f 156
group-Avnet 0:478cfd88041f 157 while (recv_ap(&ap)) {
group-Avnet 0:478cfd88041f 158 if (cnt < limit) {
group-Avnet 0:478cfd88041f 159 res[cnt] = WiFiAccessPoint(ap);
group-Avnet 0:478cfd88041f 160 }
group-Avnet 0:478cfd88041f 161
group-Avnet 0:478cfd88041f 162 cnt++;
group-Avnet 0:478cfd88041f 163 if (limit != 0 && cnt >= limit) {
group-Avnet 0:478cfd88041f 164 break;
group-Avnet 0:478cfd88041f 165 }
group-Avnet 0:478cfd88041f 166 }
group-Avnet 0:478cfd88041f 167
group-Avnet 0:478cfd88041f 168 return cnt;
group-Avnet 0:478cfd88041f 169 }
group-Avnet 0:478cfd88041f 170
group-Avnet 0:478cfd88041f 171 bool ESP8266::open(const char *type, int id, const char* addr, int port)
group-Avnet 0:478cfd88041f 172 {
group-Avnet 0:478cfd88041f 173 //IDs only 0-4
group-Avnet 0:478cfd88041f 174 if(id > 4) {
group-Avnet 0:478cfd88041f 175 return false;
group-Avnet 0:478cfd88041f 176 }
group-Avnet 0:478cfd88041f 177
group-Avnet 0:478cfd88041f 178 return _parser.send("AT+CIPSTART=%d,\"%s\",\"%s\",%d", id, type, addr, port)
group-Avnet 0:478cfd88041f 179 && _parser.recv("OK");
group-Avnet 0:478cfd88041f 180 }
group-Avnet 0:478cfd88041f 181
group-Avnet 0:478cfd88041f 182 bool ESP8266::send(int id, const void *data, uint32_t amount)
group-Avnet 0:478cfd88041f 183 {
group-Avnet 0:478cfd88041f 184 //May take a second try if device is busy
group-Avnet 0:478cfd88041f 185 for (unsigned i = 0; i < 2; i++) {
group-Avnet 0:478cfd88041f 186 if (_parser.send("AT+CIPSEND=%d,%d", id, amount)
group-Avnet 0:478cfd88041f 187 && _parser.recv(">")
group-Avnet 0:478cfd88041f 188 && _parser.write((char*)data, (int)amount) >= 0) {
group-Avnet 0:478cfd88041f 189 return true;
group-Avnet 0:478cfd88041f 190 }
group-Avnet 0:478cfd88041f 191 }
group-Avnet 0:478cfd88041f 192
group-Avnet 0:478cfd88041f 193 return false;
group-Avnet 0:478cfd88041f 194 }
group-Avnet 0:478cfd88041f 195
group-Avnet 0:478cfd88041f 196 void ESP8266::_packet_handler()
group-Avnet 0:478cfd88041f 197 {
group-Avnet 0:478cfd88041f 198 int id;
group-Avnet 0:478cfd88041f 199 uint32_t amount;
group-Avnet 0:478cfd88041f 200
group-Avnet 0:478cfd88041f 201 // parse out the packet
group-Avnet 0:478cfd88041f 202 if (!_parser.recv(",%d,%d:", &id, &amount)) {
group-Avnet 0:478cfd88041f 203 return;
group-Avnet 0:478cfd88041f 204 }
group-Avnet 0:478cfd88041f 205
group-Avnet 0:478cfd88041f 206 struct packet *packet = (struct packet*)malloc(
group-Avnet 0:478cfd88041f 207 sizeof(struct packet) + amount);
group-Avnet 0:478cfd88041f 208 if (!packet) {
group-Avnet 0:478cfd88041f 209 return;
group-Avnet 0:478cfd88041f 210 }
group-Avnet 0:478cfd88041f 211
group-Avnet 0:478cfd88041f 212 packet->id = id;
group-Avnet 0:478cfd88041f 213 packet->len = amount;
group-Avnet 0:478cfd88041f 214 packet->next = 0;
group-Avnet 0:478cfd88041f 215
group-Avnet 0:478cfd88041f 216 if (!(_parser.read((char*)(packet + 1), amount))) {
group-Avnet 0:478cfd88041f 217 free(packet);
group-Avnet 0:478cfd88041f 218 return;
group-Avnet 0:478cfd88041f 219 }
group-Avnet 0:478cfd88041f 220
group-Avnet 0:478cfd88041f 221 // append to packet list
group-Avnet 0:478cfd88041f 222 *_packets_end = packet;
group-Avnet 0:478cfd88041f 223 _packets_end = &packet->next;
group-Avnet 0:478cfd88041f 224 }
group-Avnet 0:478cfd88041f 225
group-Avnet 0:478cfd88041f 226 int32_t ESP8266::recv(int id, void *data, uint32_t amount)
group-Avnet 0:478cfd88041f 227 {
group-Avnet 0:478cfd88041f 228 while (true) {
group-Avnet 0:478cfd88041f 229 // check if any packets are ready for us
group-Avnet 0:478cfd88041f 230 for (struct packet **p = &_packets; *p; p = &(*p)->next) {
group-Avnet 0:478cfd88041f 231 if ((*p)->id == id) {
group-Avnet 0:478cfd88041f 232 struct packet *q = *p;
group-Avnet 0:478cfd88041f 233
group-Avnet 0:478cfd88041f 234 if (q->len <= amount) { // Return and remove full packet
group-Avnet 0:478cfd88041f 235 memcpy(data, q+1, q->len);
group-Avnet 0:478cfd88041f 236
group-Avnet 0:478cfd88041f 237 if (_packets_end == &(*p)->next) {
group-Avnet 0:478cfd88041f 238 _packets_end = p;
group-Avnet 0:478cfd88041f 239 }
group-Avnet 0:478cfd88041f 240 *p = (*p)->next;
group-Avnet 0:478cfd88041f 241
group-Avnet 0:478cfd88041f 242 uint32_t len = q->len;
group-Avnet 0:478cfd88041f 243 free(q);
group-Avnet 0:478cfd88041f 244 return len;
group-Avnet 0:478cfd88041f 245 } else { // return only partial packet
group-Avnet 0:478cfd88041f 246 memcpy(data, q+1, amount);
group-Avnet 0:478cfd88041f 247
group-Avnet 0:478cfd88041f 248 q->len -= amount;
group-Avnet 0:478cfd88041f 249 memmove(q+1, (uint8_t*)(q+1) + amount, q->len);
group-Avnet 0:478cfd88041f 250
group-Avnet 0:478cfd88041f 251 return amount;
group-Avnet 0:478cfd88041f 252 }
group-Avnet 0:478cfd88041f 253 }
group-Avnet 0:478cfd88041f 254 }
group-Avnet 0:478cfd88041f 255
group-Avnet 0:478cfd88041f 256 // Wait for inbound packet
group-Avnet 0:478cfd88041f 257 if (!_parser.recv("OK")) {
group-Avnet 0:478cfd88041f 258 return -1;
group-Avnet 0:478cfd88041f 259 }
group-Avnet 0:478cfd88041f 260 }
group-Avnet 0:478cfd88041f 261 }
group-Avnet 0:478cfd88041f 262
group-Avnet 0:478cfd88041f 263 bool ESP8266::close(int id)
group-Avnet 0:478cfd88041f 264 {
group-Avnet 0:478cfd88041f 265 //May take a second try if device is busy
group-Avnet 0:478cfd88041f 266 for (unsigned i = 0; i < 2; i++) {
group-Avnet 0:478cfd88041f 267 if (_parser.send("AT+CIPCLOSE=%d", id)
group-Avnet 0:478cfd88041f 268 && _parser.recv("OK")) {
group-Avnet 0:478cfd88041f 269 return true;
group-Avnet 0:478cfd88041f 270 }
group-Avnet 0:478cfd88041f 271 }
group-Avnet 0:478cfd88041f 272
group-Avnet 0:478cfd88041f 273 return false;
group-Avnet 0:478cfd88041f 274 }
group-Avnet 0:478cfd88041f 275
group-Avnet 0:478cfd88041f 276 void ESP8266::setTimeout(uint32_t timeout_ms)
group-Avnet 0:478cfd88041f 277 {
group-Avnet 0:478cfd88041f 278 _parser.setTimeout(timeout_ms);
group-Avnet 0:478cfd88041f 279 }
group-Avnet 0:478cfd88041f 280
group-Avnet 0:478cfd88041f 281 bool ESP8266::readable()
group-Avnet 0:478cfd88041f 282 {
group-Avnet 0:478cfd88041f 283 return _serial.readable();
group-Avnet 0:478cfd88041f 284 }
group-Avnet 0:478cfd88041f 285
group-Avnet 0:478cfd88041f 286 bool ESP8266::writeable()
group-Avnet 0:478cfd88041f 287 {
group-Avnet 0:478cfd88041f 288 return _serial.writeable();
group-Avnet 0:478cfd88041f 289 }
group-Avnet 0:478cfd88041f 290
group-Avnet 0:478cfd88041f 291 void ESP8266::attach(Callback<void()> func)
group-Avnet 0:478cfd88041f 292 {
group-Avnet 0:478cfd88041f 293 _serial.attach(func);
group-Avnet 0:478cfd88041f 294 }
group-Avnet 0:478cfd88041f 295
group-Avnet 0:478cfd88041f 296 bool ESP8266::recv_ap(nsapi_wifi_ap_t *ap)
group-Avnet 0:478cfd88041f 297 {
group-Avnet 0:478cfd88041f 298 int sec;
group-Avnet 0:478cfd88041f 299 bool ret = _parser.recv("+CWLAP:(%d,\"%32[^\"]\",%hhd,\"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\",%d", &sec, ap->ssid,
group-Avnet 0:478cfd88041f 300 &ap->rssi, &ap->bssid[0], &ap->bssid[1], &ap->bssid[2], &ap->bssid[3], &ap->bssid[4],
group-Avnet 0:478cfd88041f 301 &ap->bssid[5], &ap->channel);
group-Avnet 0:478cfd88041f 302
group-Avnet 0:478cfd88041f 303 ap->security = sec < 5 ? (nsapi_security_t)sec : NSAPI_SECURITY_UNKNOWN;
group-Avnet 0:478cfd88041f 304
group-Avnet 0:478cfd88041f 305 return ret;
group-Avnet 0:478cfd88041f 306 }