Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

Committer:
RobMeades
Date:
Fri Nov 03 13:01:23 2017 +0000
Revision:
6:304d3ba87a01
Parent:
0:19aa55d66228
Add comment concerning N2XX baud rate.

Who changed what in which revision?

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