Added support for WNC M14A2A Cellular LTE Data Module.

Dependencies:   WNC14A2AInterface

Dependents:   http-example-wnc http-example-wnc-modified

Committer:
root@developer-sjc-cyan-compiler.local.mbed.org
Date:
Sun Apr 23 18:40:51 2017 +0000
Revision:
5:391eac6a0a94
Parent:
0:2563b0415d1f
Added tag att_cellular_K64_wnc_14A2A_20170423 for changeset daf182af022b

Who changed what in which revision?

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