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.
EMW3162Interface.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 "network-socket/NetworkStack.h" 00021 #include "network-socket/WiFiInterface.h" 00022 #include "EMW3162.h" 00023 00024 00025 #define ESP8266_SOCKET_COUNT 5 00026 00027 /** ESP8266Interface class 00028 * Implementation of the NetworkStack for the ESP8266 00029 */ 00030 class ESP8266Interface : public NetworkStack, public WiFiInterface 00031 { 00032 public: 00033 /** ESP8266Interface lifetime 00034 * @param tx TX pin 00035 * @param rx RX pin 00036 * @param debug Enable debugging 00037 */ 00038 ESP8266Interface(PinName tx, PinName rx, bool debug = true); 00039 00040 /** Start the interface 00041 * 00042 * Attempts to connect to a WiFi network. If passphrase is invalid, 00043 * NSAPI_ERROR_AUTH_ERROR is returned. 00044 * 00045 * @param ssid Name of the network to connect to 00046 * @param pass Security passphrase to connect to the network 00047 * @param security Type of encryption for connection 00048 * @return 0 on success, negative error code on failure 00049 */ 00050 virtual int connect( 00051 const char *ssid, 00052 const char *pass, 00053 nsapi_security_t security = NSAPI_SECURITY_NONE); 00054 00055 /** Stop the interface 00056 * @return 0 on success, negative on failure 00057 */ 00058 virtual int disconnect(); 00059 00060 /** Get the internally stored IP address 00061 * @return IP address of the interface or null if not yet connected 00062 */ 00063 virtual const char *get_ip_address(); 00064 00065 /** Get the internally stored MAC address 00066 * @return MAC address of the interface 00067 */ 00068 virtual const char *get_mac_address(); 00069 00070 protected: 00071 /** Open a socket 00072 * @param handle Handle in which to store new socket 00073 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP 00074 * @return 0 on success, negative on failure 00075 */ 00076 virtual int socket_open(void **handle, nsapi_protocol_t proto); 00077 00078 /** Close the socket 00079 * @param handle Socket handle 00080 * @return 0 on success, negative on failure 00081 * @note On failure, any memory associated with the socket must still 00082 * be cleaned up 00083 */ 00084 virtual int socket_close(void *handle); 00085 00086 /** Bind a server socket to a specific port 00087 * @param handle Socket handle 00088 * @param address Local address to listen for incoming connections on 00089 * @return 0 on success, negative on failure. 00090 */ 00091 virtual int socket_bind(void *handle, const SocketAddress &address); 00092 00093 /** Start listening for incoming connections 00094 * @param handle Socket handle 00095 * @param backlog Number of pending connections that can be queued up at any 00096 * one time [Default: 1] 00097 * @return 0 on success, negative on failure 00098 */ 00099 virtual int socket_listen(void *handle, int backlog); 00100 00101 /** Connects this TCP socket to the server 00102 * @param handle Socket handle 00103 * @param address SocketAddress to connect to 00104 * @return 0 on success, negative on failure 00105 */ 00106 virtual int socket_connect(void *handle, const SocketAddress &address); 00107 00108 /** Accept a new connection. 00109 * @param handle Handle in which to store new socket 00110 * @param server Socket handle to server to accept from 00111 * @return 0 on success, negative on failure 00112 * @note This call is not-blocking, if this call would block, must 00113 * immediately return NSAPI_ERROR_WOULD_WAIT 00114 */ 00115 virtual int socket_accept(void **handle, void *server); 00116 00117 /** Send data to the remote host 00118 * @param handle Socket handle 00119 * @param data The buffer to send to the host 00120 * @param size The length of the buffer to send 00121 * @return Number of written bytes on success, negative on failure 00122 * @note This call is not-blocking, if this call would block, must 00123 * immediately return NSAPI_ERROR_WOULD_WAIT 00124 */ 00125 virtual int socket_send(void *handle, const void *data, unsigned size); 00126 00127 /** Receive data from the remote host 00128 * @param handle Socket handle 00129 * @param data The buffer in which to store the data received from the host 00130 * @param size The maximum length of the buffer 00131 * @return Number of received bytes on success, negative on failure 00132 * @note This call is not-blocking, if this call would block, must 00133 * immediately return NSAPI_ERROR_WOULD_WAIT 00134 */ 00135 virtual int socket_recv(void *handle, void *data, unsigned size); 00136 00137 /** Send a packet to a remote endpoint 00138 * @param handle Socket handle 00139 * @param address The remote SocketAddress 00140 * @param data The packet to be sent 00141 * @param size The length of the packet to be sent 00142 * @return The number of written bytes on success, negative on failure 00143 * @note This call is not-blocking, if this call would block, must 00144 * immediately return NSAPI_ERROR_WOULD_WAIT 00145 */ 00146 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00147 00148 /** Receive a packet from a remote endpoint 00149 * @param handle Socket handle 00150 * @param address Destination for the remote SocketAddress or null 00151 * @param buffer The buffer for storing the incoming packet data 00152 * If a packet is too long to fit in the supplied buffer, 00153 * excess bytes are discarded 00154 * @param size The length of the buffer 00155 * @return The number of received bytes on success, negative on failure 00156 * @note This call is not-blocking, if this call would block, must 00157 * immediately return NSAPI_ERROR_WOULD_WAIT 00158 */ 00159 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 00160 00161 /** Register a callback on state change of the socket 00162 * @param handle Socket handle 00163 * @param callback Function to call on state change 00164 * @param data Argument to pass to callback 00165 * @note Callback may be called in an interrupt context. 00166 */ 00167 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00168 00169 /** Provide access to the NetworkStack object 00170 * 00171 * @return The underlying NetworkStack object 00172 */ 00173 virtual NetworkStack *get_stack() 00174 { 00175 return this; 00176 } 00177 00178 private: 00179 ESP8266 _esp; 00180 bool _ids[ESP8266_SOCKET_COUNT]; 00181 00182 void event(); 00183 struct { 00184 void (*callback)(void *); 00185 void *data; 00186 } _cbs[ESP8266_SOCKET_COUNT]; 00187 }; 00188 00189 00190 #endif
Generated on Fri Jul 15 2022 08:03:00 by
1.7.2