Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ESP8266Interface.h
00001 /* ESP8266 implementation of NetworkInterfaceAPI 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef ESP8266_INTERFACE_H 00018 #define ESP8266_INTERFACE_H 00019 00020 #include "mbed.h" 00021 #include "ESP8266.h" 00022 00023 00024 #define ESP8266_SOCKET_COUNT 5 00025 00026 /** ESP8266Interface class 00027 * Implementation of the NetworkStack for the ESP8266 00028 */ 00029 class ESP8266Interface : public NetworkStack, public WiFiInterface 00030 { 00031 public: 00032 /** ESP8266Interface lifetime 00033 * @param tx TX pin 00034 * @param rx RX pin 00035 * @param debug Enable debugging 00036 */ 00037 ESP8266Interface(PinName tx, PinName rx, bool debug = false); 00038 00039 /** Start the interface 00040 * 00041 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. 00042 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. 00043 * 00044 * @return 0 on success, negative error code on failure 00045 */ 00046 virtual int connect(); 00047 00048 /** Start the interface 00049 * 00050 * Attempts to connect to a WiFi network. 00051 * 00052 * @param ssid Name of the network to connect to 00053 * @param pass Security passphrase to connect to the network 00054 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) 00055 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED 00056 * @return 0 on success, or error code on failure 00057 */ 00058 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, 00059 uint8_t channel = 0); 00060 00061 /** Set the WiFi network credentials 00062 * 00063 * @param ssid Name of the network to connect to 00064 * @param pass Security passphrase to connect to the network 00065 * @param security Type of encryption for connection 00066 * (defaults to NSAPI_SECURITY_NONE) 00067 * @return 0 on success, or error code on failure 00068 */ 00069 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); 00070 00071 /** Set the WiFi network channel - NOT SUPPORTED 00072 * 00073 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00074 * 00075 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) 00076 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00077 */ 00078 virtual int set_channel(uint8_t channel); 00079 00080 /** Stop the interface 00081 * @return 0 on success, negative on failure 00082 */ 00083 virtual int disconnect(); 00084 00085 /** Get the internally stored IP address 00086 * @return IP address of the interface or null if not yet connected 00087 */ 00088 virtual const char *get_ip_address(); 00089 00090 /** Get the internally stored MAC address 00091 * @return MAC address of the interface 00092 */ 00093 virtual const char *get_mac_address(); 00094 00095 /** Get the local gateway 00096 * 00097 * @return Null-terminated representation of the local gateway 00098 * or null if no network mask has been recieved 00099 */ 00100 virtual const char *get_gateway(); 00101 00102 /** Get the local network mask 00103 * 00104 * @return Null-terminated representation of the local network mask 00105 * or null if no network mask has been recieved 00106 */ 00107 virtual const char *get_netmask(); 00108 00109 /** Gets the current radio signal strength for active connection 00110 * 00111 * @return Connection strength in dBm (negative value) 00112 */ 00113 virtual int8_t get_rssi(); 00114 00115 /** Scan for available networks 00116 * 00117 * This function will block. 00118 * 00119 * @param ap Pointer to allocated array to store discovered AP 00120 * @param count Size of allocated @a res array, or 0 to only count available AP 00121 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) 00122 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error 00123 * see @a nsapi_error 00124 */ 00125 virtual int scan(WiFiAccessPoint *res, unsigned count); 00126 00127 protected: 00128 /** Open a socket 00129 * @param handle Handle in which to store new socket 00130 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP 00131 * @return 0 on success, negative on failure 00132 */ 00133 virtual int socket_open(void **handle, nsapi_protocol_t proto); 00134 00135 /** Close the socket 00136 * @param handle Socket handle 00137 * @return 0 on success, negative on failure 00138 * @note On failure, any memory associated with the socket must still 00139 * be cleaned up 00140 */ 00141 virtual int socket_close(void *handle); 00142 00143 /** Bind a server socket to a specific port 00144 * @param handle Socket handle 00145 * @param address Local address to listen for incoming connections on 00146 * @return 0 on success, negative on failure. 00147 */ 00148 virtual int socket_bind(void *handle, const SocketAddress &address); 00149 00150 /** Start listening for incoming connections 00151 * @param handle Socket handle 00152 * @param backlog Number of pending connections that can be queued up at any 00153 * one time [Default: 1] 00154 * @return 0 on success, negative on failure 00155 */ 00156 virtual int socket_listen(void *handle, int backlog); 00157 00158 /** Connects this TCP socket to the server 00159 * @param handle Socket handle 00160 * @param address SocketAddress to connect to 00161 * @return 0 on success, negative on failure 00162 */ 00163 virtual int socket_connect(void *handle, const SocketAddress &address); 00164 00165 /** Accept a new connection. 00166 * @param handle Handle in which to store new socket 00167 * @param server Socket handle to server to accept from 00168 * @return 0 on success, negative on failure 00169 * @note This call is not-blocking, if this call would block, must 00170 * immediately return NSAPI_ERROR_WOULD_WAIT 00171 */ 00172 virtual int socket_accept(void *handle, void **socket, SocketAddress *address); 00173 00174 /** Send data to the remote host 00175 * @param handle Socket handle 00176 * @param data The buffer to send to the host 00177 * @param size The length of the buffer to send 00178 * @return Number of written bytes on success, negative on failure 00179 * @note This call is not-blocking, if this call would block, must 00180 * immediately return NSAPI_ERROR_WOULD_WAIT 00181 */ 00182 virtual int socket_send(void *handle, const void *data, unsigned size); 00183 00184 /** Receive data from the remote host 00185 * @param handle Socket handle 00186 * @param data The buffer in which to store the data received from the host 00187 * @param size The maximum length of the buffer 00188 * @return Number of received bytes on success, negative on failure 00189 * @note This call is not-blocking, if this call would block, must 00190 * immediately return NSAPI_ERROR_WOULD_WAIT 00191 */ 00192 virtual int socket_recv(void *handle, void *data, unsigned size); 00193 00194 /** Send a packet to a remote endpoint 00195 * @param handle Socket handle 00196 * @param address The remote SocketAddress 00197 * @param data The packet to be sent 00198 * @param size The length of the packet to be sent 00199 * @return The number of written bytes on success, negative on failure 00200 * @note This call is not-blocking, if this call would block, must 00201 * immediately return NSAPI_ERROR_WOULD_WAIT 00202 */ 00203 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00204 00205 /** Receive a packet from a remote endpoint 00206 * @param handle Socket handle 00207 * @param address Destination for the remote SocketAddress or null 00208 * @param buffer The buffer for storing the incoming packet data 00209 * If a packet is too long to fit in the supplied buffer, 00210 * excess bytes are discarded 00211 * @param size The length of the buffer 00212 * @return The number of received bytes on success, negative on failure 00213 * @note This call is not-blocking, if this call would block, must 00214 * immediately return NSAPI_ERROR_WOULD_WAIT 00215 */ 00216 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 00217 00218 /** Register a callback on state change of the socket 00219 * @param handle Socket handle 00220 * @param callback Function to call on state change 00221 * @param data Argument to pass to callback 00222 * @note Callback may be called in an interrupt context. 00223 */ 00224 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00225 00226 /** Provide access to the NetworkStack object 00227 * 00228 * @return The underlying NetworkStack object 00229 */ 00230 virtual NetworkStack *get_stack() 00231 { 00232 return this; 00233 } 00234 00235 private: 00236 ESP8266 _esp; 00237 bool _ids[ESP8266_SOCKET_COUNT]; 00238 00239 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ 00240 nsapi_security_t ap_sec; 00241 uint8_t ap_ch; 00242 char ap_pass[64]; /* The longest allowed passphrase */ 00243 00244 void event(); 00245 00246 struct { 00247 void (*callback)(void *); 00248 void *data; 00249 } _cbs[ESP8266_SOCKET_COUNT]; 00250 }; 00251 00252 #endif
Generated on Tue Jul 12 2022 19:06:58 by
1.7.2