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 #ifndef ESP8266_INTERFACE_H
group-Avnet 0:478cfd88041f 18 #define ESP8266_INTERFACE_H
group-Avnet 0:478cfd88041f 19
group-Avnet 0:478cfd88041f 20 #include "mbed.h"
group-Avnet 0:478cfd88041f 21 #include "ESP8266.h"
group-Avnet 0:478cfd88041f 22
group-Avnet 0:478cfd88041f 23
group-Avnet 0:478cfd88041f 24 #define ESP8266_SOCKET_COUNT 5
group-Avnet 0:478cfd88041f 25
group-Avnet 0:478cfd88041f 26 /** ESP8266Interface class
group-Avnet 0:478cfd88041f 27 * Implementation of the NetworkStack for the ESP8266
group-Avnet 0:478cfd88041f 28 */
group-Avnet 0:478cfd88041f 29 class ESP8266Interface : public NetworkStack, public WiFiInterface
group-Avnet 0:478cfd88041f 30 {
group-Avnet 0:478cfd88041f 31 public:
group-Avnet 0:478cfd88041f 32 /** ESP8266Interface lifetime
group-Avnet 0:478cfd88041f 33 * @param tx TX pin
group-Avnet 0:478cfd88041f 34 * @param rx RX pin
group-Avnet 0:478cfd88041f 35 * @param debug Enable debugging
group-Avnet 0:478cfd88041f 36 */
group-Avnet 0:478cfd88041f 37 ESP8266Interface(PinName tx, PinName rx, bool debug = false);
group-Avnet 0:478cfd88041f 38
group-Avnet 0:478cfd88041f 39 /** Start the interface
group-Avnet 0:478cfd88041f 40 *
group-Avnet 0:478cfd88041f 41 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
group-Avnet 0:478cfd88041f 42 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
group-Avnet 0:478cfd88041f 43 *
group-Avnet 0:478cfd88041f 44 * @return 0 on success, negative error code on failure
group-Avnet 0:478cfd88041f 45 */
group-Avnet 0:478cfd88041f 46 virtual int connect();
group-Avnet 0:478cfd88041f 47
group-Avnet 0:478cfd88041f 48 /** Start the interface
group-Avnet 0:478cfd88041f 49 *
group-Avnet 0:478cfd88041f 50 * Attempts to connect to a WiFi network.
group-Avnet 0:478cfd88041f 51 *
group-Avnet 0:478cfd88041f 52 * @param ssid Name of the network to connect to
group-Avnet 0:478cfd88041f 53 * @param pass Security passphrase to connect to the network
group-Avnet 0:478cfd88041f 54 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
group-Avnet 0:478cfd88041f 55 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
group-Avnet 0:478cfd88041f 56 * @return 0 on success, or error code on failure
group-Avnet 0:478cfd88041f 57 */
group-Avnet 0:478cfd88041f 58 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
group-Avnet 0:478cfd88041f 59 uint8_t channel = 0);
group-Avnet 0:478cfd88041f 60
group-Avnet 0:478cfd88041f 61 /** Set the WiFi network credentials
group-Avnet 0:478cfd88041f 62 *
group-Avnet 0:478cfd88041f 63 * @param ssid Name of the network to connect to
group-Avnet 0:478cfd88041f 64 * @param pass Security passphrase to connect to the network
group-Avnet 0:478cfd88041f 65 * @param security Type of encryption for connection
group-Avnet 0:478cfd88041f 66 * (defaults to NSAPI_SECURITY_NONE)
group-Avnet 0:478cfd88041f 67 * @return 0 on success, or error code on failure
group-Avnet 0:478cfd88041f 68 */
group-Avnet 0:478cfd88041f 69 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
group-Avnet 0:478cfd88041f 70
group-Avnet 0:478cfd88041f 71 /** Set the WiFi network channel - NOT SUPPORTED
group-Avnet 0:478cfd88041f 72 *
group-Avnet 0:478cfd88041f 73 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
group-Avnet 0:478cfd88041f 74 *
group-Avnet 0:478cfd88041f 75 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
group-Avnet 0:478cfd88041f 76 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
group-Avnet 0:478cfd88041f 77 */
group-Avnet 0:478cfd88041f 78 virtual int set_channel(uint8_t channel);
group-Avnet 0:478cfd88041f 79
group-Avnet 0:478cfd88041f 80 /** Stop the interface
group-Avnet 0:478cfd88041f 81 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 82 */
group-Avnet 0:478cfd88041f 83 virtual int disconnect();
group-Avnet 0:478cfd88041f 84
group-Avnet 0:478cfd88041f 85 /** Get the internally stored IP address
group-Avnet 0:478cfd88041f 86 * @return IP address of the interface or null if not yet connected
group-Avnet 0:478cfd88041f 87 */
group-Avnet 0:478cfd88041f 88 virtual const char *get_ip_address();
group-Avnet 0:478cfd88041f 89
group-Avnet 0:478cfd88041f 90 /** Get the internally stored MAC address
group-Avnet 0:478cfd88041f 91 * @return MAC address of the interface
group-Avnet 0:478cfd88041f 92 */
group-Avnet 0:478cfd88041f 93 virtual const char *get_mac_address();
group-Avnet 0:478cfd88041f 94
group-Avnet 0:478cfd88041f 95 /** Get the local gateway
group-Avnet 0:478cfd88041f 96 *
group-Avnet 0:478cfd88041f 97 * @return Null-terminated representation of the local gateway
group-Avnet 0:478cfd88041f 98 * or null if no network mask has been recieved
group-Avnet 0:478cfd88041f 99 */
group-Avnet 0:478cfd88041f 100 virtual const char *get_gateway();
group-Avnet 0:478cfd88041f 101
group-Avnet 0:478cfd88041f 102 /** Get the local network mask
group-Avnet 0:478cfd88041f 103 *
group-Avnet 0:478cfd88041f 104 * @return Null-terminated representation of the local network mask
group-Avnet 0:478cfd88041f 105 * or null if no network mask has been recieved
group-Avnet 0:478cfd88041f 106 */
group-Avnet 0:478cfd88041f 107 virtual const char *get_netmask();
group-Avnet 0:478cfd88041f 108
group-Avnet 0:478cfd88041f 109 /** Gets the current radio signal strength for active connection
group-Avnet 0:478cfd88041f 110 *
group-Avnet 0:478cfd88041f 111 * @return Connection strength in dBm (negative value)
group-Avnet 0:478cfd88041f 112 */
group-Avnet 0:478cfd88041f 113 virtual int8_t get_rssi();
group-Avnet 0:478cfd88041f 114
group-Avnet 0:478cfd88041f 115 /** Scan for available networks
group-Avnet 0:478cfd88041f 116 *
group-Avnet 0:478cfd88041f 117 * This function will block.
group-Avnet 0:478cfd88041f 118 *
group-Avnet 0:478cfd88041f 119 * @param ap Pointer to allocated array to store discovered AP
group-Avnet 0:478cfd88041f 120 * @param count Size of allocated @a res array, or 0 to only count available AP
group-Avnet 0:478cfd88041f 121 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
group-Avnet 0:478cfd88041f 122 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
group-Avnet 0:478cfd88041f 123 * see @a nsapi_error
group-Avnet 0:478cfd88041f 124 */
group-Avnet 0:478cfd88041f 125 virtual int scan(WiFiAccessPoint *res, unsigned count);
group-Avnet 0:478cfd88041f 126
group-Avnet 0:478cfd88041f 127 /** Translates a hostname to an IP address with specific version
group-Avnet 0:478cfd88041f 128 *
group-Avnet 0:478cfd88041f 129 * The hostname may be either a domain name or an IP address. If the
group-Avnet 0:478cfd88041f 130 * hostname is an IP address, no network transactions will be performed.
group-Avnet 0:478cfd88041f 131 *
group-Avnet 0:478cfd88041f 132 * If no stack-specific DNS resolution is provided, the hostname
group-Avnet 0:478cfd88041f 133 * will be resolve using a UDP socket on the stack.
group-Avnet 0:478cfd88041f 134 *
group-Avnet 0:478cfd88041f 135 * @param address Destination for the host SocketAddress
group-Avnet 0:478cfd88041f 136 * @param host Hostname to resolve
group-Avnet 0:478cfd88041f 137 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
group-Avnet 0:478cfd88041f 138 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
group-Avnet 0:478cfd88041f 139 * @return 0 on success, negative error code on failure
group-Avnet 0:478cfd88041f 140 */
group-Avnet 0:478cfd88041f 141 using NetworkInterface::gethostbyname;
group-Avnet 0:478cfd88041f 142
group-Avnet 0:478cfd88041f 143 /** Add a domain name server to list of servers to query
group-Avnet 0:478cfd88041f 144 *
group-Avnet 0:478cfd88041f 145 * @param addr Destination for the host address
group-Avnet 0:478cfd88041f 146 * @return 0 on success, negative error code on failure
group-Avnet 0:478cfd88041f 147 */
group-Avnet 0:478cfd88041f 148 using NetworkInterface::add_dns_server;
group-Avnet 0:478cfd88041f 149
group-Avnet 0:478cfd88041f 150 protected:
group-Avnet 0:478cfd88041f 151 /** Open a socket
group-Avnet 0:478cfd88041f 152 * @param handle Handle in which to store new socket
group-Avnet 0:478cfd88041f 153 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
group-Avnet 0:478cfd88041f 154 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 155 */
group-Avnet 0:478cfd88041f 156 virtual int socket_open(void **handle, nsapi_protocol_t proto);
group-Avnet 0:478cfd88041f 157
group-Avnet 0:478cfd88041f 158 /** Close the socket
group-Avnet 0:478cfd88041f 159 * @param handle Socket handle
group-Avnet 0:478cfd88041f 160 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 161 * @note On failure, any memory associated with the socket must still
group-Avnet 0:478cfd88041f 162 * be cleaned up
group-Avnet 0:478cfd88041f 163 */
group-Avnet 0:478cfd88041f 164 virtual int socket_close(void *handle);
group-Avnet 0:478cfd88041f 165
group-Avnet 0:478cfd88041f 166 /** Bind a server socket to a specific port
group-Avnet 0:478cfd88041f 167 * @param handle Socket handle
group-Avnet 0:478cfd88041f 168 * @param address Local address to listen for incoming connections on
group-Avnet 0:478cfd88041f 169 * @return 0 on success, negative on failure.
group-Avnet 0:478cfd88041f 170 */
group-Avnet 0:478cfd88041f 171 virtual int socket_bind(void *handle, const SocketAddress &address);
group-Avnet 0:478cfd88041f 172
group-Avnet 0:478cfd88041f 173 /** Start listening for incoming connections
group-Avnet 0:478cfd88041f 174 * @param handle Socket handle
group-Avnet 0:478cfd88041f 175 * @param backlog Number of pending connections that can be queued up at any
group-Avnet 0:478cfd88041f 176 * one time [Default: 1]
group-Avnet 0:478cfd88041f 177 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 178 */
group-Avnet 0:478cfd88041f 179 virtual int socket_listen(void *handle, int backlog);
group-Avnet 0:478cfd88041f 180
group-Avnet 0:478cfd88041f 181 /** Connects this TCP socket to the server
group-Avnet 0:478cfd88041f 182 * @param handle Socket handle
group-Avnet 0:478cfd88041f 183 * @param address SocketAddress to connect to
group-Avnet 0:478cfd88041f 184 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 185 */
group-Avnet 0:478cfd88041f 186 virtual int socket_connect(void *handle, const SocketAddress &address);
group-Avnet 0:478cfd88041f 187
group-Avnet 0:478cfd88041f 188 /** Accept a new connection.
group-Avnet 0:478cfd88041f 189 * @param handle Handle in which to store new socket
group-Avnet 0:478cfd88041f 190 * @param server Socket handle to server to accept from
group-Avnet 0:478cfd88041f 191 * @return 0 on success, negative on failure
group-Avnet 0:478cfd88041f 192 * @note This call is not-blocking, if this call would block, must
group-Avnet 0:478cfd88041f 193 * immediately return NSAPI_ERROR_WOULD_WAIT
group-Avnet 0:478cfd88041f 194 */
group-Avnet 0:478cfd88041f 195 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
group-Avnet 0:478cfd88041f 196
group-Avnet 0:478cfd88041f 197 /** Send data to the remote host
group-Avnet 0:478cfd88041f 198 * @param handle Socket handle
group-Avnet 0:478cfd88041f 199 * @param data The buffer to send to the host
group-Avnet 0:478cfd88041f 200 * @param size The length of the buffer to send
group-Avnet 0:478cfd88041f 201 * @return Number of written bytes on success, negative on failure
group-Avnet 0:478cfd88041f 202 * @note This call is not-blocking, if this call would block, must
group-Avnet 0:478cfd88041f 203 * immediately return NSAPI_ERROR_WOULD_WAIT
group-Avnet 0:478cfd88041f 204 */
group-Avnet 0:478cfd88041f 205 virtual int socket_send(void *handle, const void *data, unsigned size);
group-Avnet 0:478cfd88041f 206
group-Avnet 0:478cfd88041f 207 /** Receive data from the remote host
group-Avnet 0:478cfd88041f 208 * @param handle Socket handle
group-Avnet 0:478cfd88041f 209 * @param data The buffer in which to store the data received from the host
group-Avnet 0:478cfd88041f 210 * @param size The maximum length of the buffer
group-Avnet 0:478cfd88041f 211 * @return Number of received bytes on success, negative on failure
group-Avnet 0:478cfd88041f 212 * @note This call is not-blocking, if this call would block, must
group-Avnet 0:478cfd88041f 213 * immediately return NSAPI_ERROR_WOULD_WAIT
group-Avnet 0:478cfd88041f 214 */
group-Avnet 0:478cfd88041f 215 virtual int socket_recv(void *handle, void *data, unsigned size);
group-Avnet 0:478cfd88041f 216
group-Avnet 0:478cfd88041f 217 /** Send a packet to a remote endpoint
group-Avnet 0:478cfd88041f 218 * @param handle Socket handle
group-Avnet 0:478cfd88041f 219 * @param address The remote SocketAddress
group-Avnet 0:478cfd88041f 220 * @param data The packet to be sent
group-Avnet 0:478cfd88041f 221 * @param size The length of the packet to be sent
group-Avnet 0:478cfd88041f 222 * @return The number of written bytes on success, negative on failure
group-Avnet 0:478cfd88041f 223 * @note This call is not-blocking, if this call would block, must
group-Avnet 0:478cfd88041f 224 * immediately return NSAPI_ERROR_WOULD_WAIT
group-Avnet 0:478cfd88041f 225 */
group-Avnet 0:478cfd88041f 226 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
group-Avnet 0:478cfd88041f 227
group-Avnet 0:478cfd88041f 228 /** Receive a packet from a remote endpoint
group-Avnet 0:478cfd88041f 229 * @param handle Socket handle
group-Avnet 0:478cfd88041f 230 * @param address Destination for the remote SocketAddress or null
group-Avnet 0:478cfd88041f 231 * @param buffer The buffer for storing the incoming packet data
group-Avnet 0:478cfd88041f 232 * If a packet is too long to fit in the supplied buffer,
group-Avnet 0:478cfd88041f 233 * excess bytes are discarded
group-Avnet 0:478cfd88041f 234 * @param size The length of the buffer
group-Avnet 0:478cfd88041f 235 * @return The number of received bytes on success, negative on failure
group-Avnet 0:478cfd88041f 236 * @note This call is not-blocking, if this call would block, must
group-Avnet 0:478cfd88041f 237 * immediately return NSAPI_ERROR_WOULD_WAIT
group-Avnet 0:478cfd88041f 238 */
group-Avnet 0:478cfd88041f 239 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
group-Avnet 0:478cfd88041f 240
group-Avnet 0:478cfd88041f 241 /** Register a callback on state change of the socket
group-Avnet 0:478cfd88041f 242 * @param handle Socket handle
group-Avnet 0:478cfd88041f 243 * @param callback Function to call on state change
group-Avnet 0:478cfd88041f 244 * @param data Argument to pass to callback
group-Avnet 0:478cfd88041f 245 * @note Callback may be called in an interrupt context.
group-Avnet 0:478cfd88041f 246 */
group-Avnet 0:478cfd88041f 247 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
group-Avnet 0:478cfd88041f 248
group-Avnet 0:478cfd88041f 249 /** Provide access to the NetworkStack object
group-Avnet 0:478cfd88041f 250 *
group-Avnet 0:478cfd88041f 251 * @return The underlying NetworkStack object
group-Avnet 0:478cfd88041f 252 */
group-Avnet 0:478cfd88041f 253 virtual NetworkStack *get_stack()
group-Avnet 0:478cfd88041f 254 {
group-Avnet 0:478cfd88041f 255 return this;
group-Avnet 0:478cfd88041f 256 }
group-Avnet 0:478cfd88041f 257
group-Avnet 0:478cfd88041f 258 private:
group-Avnet 0:478cfd88041f 259 ESP8266 _esp;
group-Avnet 0:478cfd88041f 260 bool _ids[ESP8266_SOCKET_COUNT];
group-Avnet 0:478cfd88041f 261
group-Avnet 0:478cfd88041f 262 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
group-Avnet 0:478cfd88041f 263 nsapi_security_t ap_sec;
group-Avnet 0:478cfd88041f 264 uint8_t ap_ch;
group-Avnet 0:478cfd88041f 265 char ap_pass[64]; /* The longest allowed passphrase */
group-Avnet 0:478cfd88041f 266
group-Avnet 0:478cfd88041f 267 void event();
group-Avnet 0:478cfd88041f 268
group-Avnet 0:478cfd88041f 269 struct {
group-Avnet 0:478cfd88041f 270 void (*callback)(void *);
group-Avnet 0:478cfd88041f 271 void *data;
group-Avnet 0:478cfd88041f 272 } _cbs[ESP8266_SOCKET_COUNT];
group-Avnet 0:478cfd88041f 273 };
group-Avnet 0:478cfd88041f 274
group-Avnet 0:478cfd88041f 275 #endif