Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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