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 /* ESP8266Interface 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 #ifndef ESP8266_H
group-Avnet 0:478cfd88041f 18 #define ESP8266_H
group-Avnet 0:478cfd88041f 19
group-Avnet 0:478cfd88041f 20 #include "ATParser.h"
group-Avnet 0:478cfd88041f 21
group-Avnet 0:478cfd88041f 22 /** ESP8266Interface class.
group-Avnet 0:478cfd88041f 23 This is an interface to a ESP8266 radio.
group-Avnet 0:478cfd88041f 24 */
group-Avnet 0:478cfd88041f 25 class ESP8266
group-Avnet 0:478cfd88041f 26 {
group-Avnet 0:478cfd88041f 27 public:
group-Avnet 0:478cfd88041f 28 ESP8266(PinName tx, PinName rx, bool debug=false);
group-Avnet 0:478cfd88041f 29
group-Avnet 0:478cfd88041f 30 /**
group-Avnet 0:478cfd88041f 31 * Startup the ESP8266
group-Avnet 0:478cfd88041f 32 *
group-Avnet 0:478cfd88041f 33 * @param mode mode of WIFI 1-client, 2-host, 3-both
group-Avnet 0:478cfd88041f 34 * @return true only if ESP8266 was setup correctly
group-Avnet 0:478cfd88041f 35 */
group-Avnet 0:478cfd88041f 36 bool startup(int mode);
group-Avnet 0:478cfd88041f 37
group-Avnet 0:478cfd88041f 38 /**
group-Avnet 0:478cfd88041f 39 * Reset ESP8266
group-Avnet 0:478cfd88041f 40 *
group-Avnet 0:478cfd88041f 41 * @return true only if ESP8266 resets successfully
group-Avnet 0:478cfd88041f 42 */
group-Avnet 0:478cfd88041f 43 bool reset(void);
group-Avnet 0:478cfd88041f 44
group-Avnet 0:478cfd88041f 45 /**
group-Avnet 0:478cfd88041f 46 * Enable/Disable DHCP
group-Avnet 0:478cfd88041f 47 *
group-Avnet 0:478cfd88041f 48 * @param enabled DHCP enabled when true
group-Avnet 0:478cfd88041f 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
group-Avnet 0:478cfd88041f 50 * @return true only if ESP8266 enables/disables DHCP successfully
group-Avnet 0:478cfd88041f 51 */
group-Avnet 0:478cfd88041f 52 bool dhcp(bool enabled, int mode);
group-Avnet 0:478cfd88041f 53
group-Avnet 0:478cfd88041f 54 /**
group-Avnet 0:478cfd88041f 55 * Connect ESP8266 to AP
group-Avnet 0:478cfd88041f 56 *
group-Avnet 0:478cfd88041f 57 * @param ap the name of the AP
group-Avnet 0:478cfd88041f 58 * @param passPhrase the password of AP
group-Avnet 0:478cfd88041f 59 * @return true only if ESP8266 is connected successfully
group-Avnet 0:478cfd88041f 60 */
group-Avnet 0:478cfd88041f 61 bool connect(const char *ap, const char *passPhrase);
group-Avnet 0:478cfd88041f 62
group-Avnet 0:478cfd88041f 63 /**
group-Avnet 0:478cfd88041f 64 * Disconnect ESP8266 from AP
group-Avnet 0:478cfd88041f 65 *
group-Avnet 0:478cfd88041f 66 * @return true only if ESP8266 is disconnected successfully
group-Avnet 0:478cfd88041f 67 */
group-Avnet 0:478cfd88041f 68 bool disconnect(void);
group-Avnet 0:478cfd88041f 69
group-Avnet 0:478cfd88041f 70 /**
group-Avnet 0:478cfd88041f 71 * Get the IP address of ESP8266
group-Avnet 0:478cfd88041f 72 *
group-Avnet 0:478cfd88041f 73 * @return null-teriminated IP address or null if no IP address is assigned
group-Avnet 0:478cfd88041f 74 */
group-Avnet 0:478cfd88041f 75 const char *getIPAddress(void);
group-Avnet 0:478cfd88041f 76
group-Avnet 0:478cfd88041f 77 /**
group-Avnet 0:478cfd88041f 78 * Get the MAC address of ESP8266
group-Avnet 0:478cfd88041f 79 *
group-Avnet 0:478cfd88041f 80 * @return null-terminated MAC address or null if no MAC address is assigned
group-Avnet 0:478cfd88041f 81 */
group-Avnet 0:478cfd88041f 82 const char *getMACAddress(void);
group-Avnet 0:478cfd88041f 83
group-Avnet 0:478cfd88041f 84 /** Get the local gateway
group-Avnet 0:478cfd88041f 85 *
group-Avnet 0:478cfd88041f 86 * @return Null-terminated representation of the local gateway
group-Avnet 0:478cfd88041f 87 * or null if no network mask has been recieved
group-Avnet 0:478cfd88041f 88 */
group-Avnet 0:478cfd88041f 89 const char *getGateway();
group-Avnet 0:478cfd88041f 90
group-Avnet 0:478cfd88041f 91 /** Get the local network mask
group-Avnet 0:478cfd88041f 92 *
group-Avnet 0:478cfd88041f 93 * @return Null-terminated representation of the local network mask
group-Avnet 0:478cfd88041f 94 * or null if no network mask has been recieved
group-Avnet 0:478cfd88041f 95 */
group-Avnet 0:478cfd88041f 96 const char *getNetmask();
group-Avnet 0:478cfd88041f 97
group-Avnet 0:478cfd88041f 98 /* Return RSSI for active connection
group-Avnet 0:478cfd88041f 99 *
group-Avnet 0:478cfd88041f 100 * @return Measured RSSI
group-Avnet 0:478cfd88041f 101 */
group-Avnet 0:478cfd88041f 102 int8_t getRSSI();
group-Avnet 0:478cfd88041f 103
group-Avnet 0:478cfd88041f 104 /**
group-Avnet 0:478cfd88041f 105 * Check if ESP8266 is conenected
group-Avnet 0:478cfd88041f 106 *
group-Avnet 0:478cfd88041f 107 * @return true only if the chip has an IP address
group-Avnet 0:478cfd88041f 108 */
group-Avnet 0:478cfd88041f 109 bool isConnected(void);
group-Avnet 0:478cfd88041f 110
group-Avnet 0:478cfd88041f 111 /** Scan for available networks
group-Avnet 0:478cfd88041f 112 *
group-Avnet 0:478cfd88041f 113 * @param ap Pointer to allocated array to store discovered AP
group-Avnet 0:478cfd88041f 114 * @param limit Size of allocated @a res array, or 0 to only count available AP
group-Avnet 0:478cfd88041f 115 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
group-Avnet 0:478cfd88041f 116 * see @a nsapi_error
group-Avnet 0:478cfd88041f 117 */
group-Avnet 0:478cfd88041f 118 int scan(WiFiAccessPoint *res, unsigned limit);
group-Avnet 0:478cfd88041f 119
group-Avnet 0:478cfd88041f 120 /**
group-Avnet 0:478cfd88041f 121 * Open a socketed connection
group-Avnet 0:478cfd88041f 122 *
group-Avnet 0:478cfd88041f 123 * @param type the type of socket to open "UDP" or "TCP"
group-Avnet 0:478cfd88041f 124 * @param id id to give the new socket, valid 0-4
group-Avnet 0:478cfd88041f 125 * @param port port to open connection with
group-Avnet 0:478cfd88041f 126 * @param addr the IP address of the destination
group-Avnet 0:478cfd88041f 127 * @return true only if socket opened successfully
group-Avnet 0:478cfd88041f 128 */
group-Avnet 0:478cfd88041f 129 bool open(const char *type, int id, const char* addr, int port);
group-Avnet 0:478cfd88041f 130
group-Avnet 0:478cfd88041f 131 /**
group-Avnet 0:478cfd88041f 132 * Sends data to an open socket
group-Avnet 0:478cfd88041f 133 *
group-Avnet 0:478cfd88041f 134 * @param id id of socket to send to
group-Avnet 0:478cfd88041f 135 * @param data data to be sent
group-Avnet 0:478cfd88041f 136 * @param amount amount of data to be sent - max 1024
group-Avnet 0:478cfd88041f 137 * @return true only if data sent successfully
group-Avnet 0:478cfd88041f 138 */
group-Avnet 0:478cfd88041f 139 bool send(int id, const void *data, uint32_t amount);
group-Avnet 0:478cfd88041f 140
group-Avnet 0:478cfd88041f 141 /**
group-Avnet 0:478cfd88041f 142 * Receives data from an open socket
group-Avnet 0:478cfd88041f 143 *
group-Avnet 0:478cfd88041f 144 * @param id id to receive from
group-Avnet 0:478cfd88041f 145 * @param data placeholder for returned information
group-Avnet 0:478cfd88041f 146 * @param amount number of bytes to be received
group-Avnet 0:478cfd88041f 147 * @return the number of bytes received
group-Avnet 0:478cfd88041f 148 */
group-Avnet 0:478cfd88041f 149 int32_t recv(int id, void *data, uint32_t amount);
group-Avnet 0:478cfd88041f 150
group-Avnet 0:478cfd88041f 151 /**
group-Avnet 0:478cfd88041f 152 * Closes a socket
group-Avnet 0:478cfd88041f 153 *
group-Avnet 0:478cfd88041f 154 * @param id id of socket to close, valid only 0-4
group-Avnet 0:478cfd88041f 155 * @return true only if socket is closed successfully
group-Avnet 0:478cfd88041f 156 */
group-Avnet 0:478cfd88041f 157 bool close(int id);
group-Avnet 0:478cfd88041f 158
group-Avnet 0:478cfd88041f 159 /**
group-Avnet 0:478cfd88041f 160 * Allows timeout to be changed between commands
group-Avnet 0:478cfd88041f 161 *
group-Avnet 0:478cfd88041f 162 * @param timeout_ms timeout of the connection
group-Avnet 0:478cfd88041f 163 */
group-Avnet 0:478cfd88041f 164 void setTimeout(uint32_t timeout_ms);
group-Avnet 0:478cfd88041f 165
group-Avnet 0:478cfd88041f 166 /**
group-Avnet 0:478cfd88041f 167 * Checks if data is available
group-Avnet 0:478cfd88041f 168 */
group-Avnet 0:478cfd88041f 169 bool readable();
group-Avnet 0:478cfd88041f 170
group-Avnet 0:478cfd88041f 171 /**
group-Avnet 0:478cfd88041f 172 * Checks if data can be written
group-Avnet 0:478cfd88041f 173 */
group-Avnet 0:478cfd88041f 174 bool writeable();
group-Avnet 0:478cfd88041f 175
group-Avnet 0:478cfd88041f 176 /**
group-Avnet 0:478cfd88041f 177 * Attach a function to call whenever network state has changed
group-Avnet 0:478cfd88041f 178 *
group-Avnet 0:478cfd88041f 179 * @param func A pointer to a void function, or 0 to set as none
group-Avnet 0:478cfd88041f 180 */
group-Avnet 0:478cfd88041f 181 void attach(Callback<void()> func);
group-Avnet 0:478cfd88041f 182
group-Avnet 0:478cfd88041f 183 /**
group-Avnet 0:478cfd88041f 184 * Attach a function to call whenever network state has changed
group-Avnet 0:478cfd88041f 185 *
group-Avnet 0:478cfd88041f 186 * @param obj pointer to the object to call the member function on
group-Avnet 0:478cfd88041f 187 * @param method pointer to the member function to call
group-Avnet 0:478cfd88041f 188 */
group-Avnet 0:478cfd88041f 189 template <typename T, typename M>
group-Avnet 0:478cfd88041f 190 void attach(T *obj, M method) {
group-Avnet 0:478cfd88041f 191 attach(Callback<void()>(obj, method));
group-Avnet 0:478cfd88041f 192 }
group-Avnet 0:478cfd88041f 193
group-Avnet 0:478cfd88041f 194 private:
group-Avnet 0:478cfd88041f 195 BufferedSerial _serial;
group-Avnet 0:478cfd88041f 196 ATParser _parser;
group-Avnet 0:478cfd88041f 197
group-Avnet 0:478cfd88041f 198 struct packet {
group-Avnet 0:478cfd88041f 199 struct packet *next;
group-Avnet 0:478cfd88041f 200 int id;
group-Avnet 0:478cfd88041f 201 uint32_t len;
group-Avnet 0:478cfd88041f 202 // data follows
group-Avnet 0:478cfd88041f 203 } *_packets, **_packets_end;
group-Avnet 0:478cfd88041f 204 void _packet_handler();
group-Avnet 0:478cfd88041f 205 bool recv_ap(nsapi_wifi_ap_t *ap);
group-Avnet 0:478cfd88041f 206
group-Avnet 0:478cfd88041f 207 char _ip_buffer[16];
group-Avnet 0:478cfd88041f 208 char _gateway_buffer[16];
group-Avnet 0:478cfd88041f 209 char _netmask_buffer[16];
group-Avnet 0:478cfd88041f 210 char _mac_buffer[18];
group-Avnet 0:478cfd88041f 211 };
group-Avnet 0:478cfd88041f 212
group-Avnet 0:478cfd88041f 213 #endif