version 1.6

Dependents:   iot_water_monitor_v2

Committer:
DuyLionTran
Date:
Tue Dec 12 15:57:57 2017 +0000
Revision:
0:9fa9929d1a8c
version 1.6

Who changed what in which revision?

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