mayuresh bharmoria / Mbed OS mbed-os-example-wifi
Committer:
mayur098
Date:
Thu Jun 21 17:50:21 2018 +0000
Revision:
0:8f8e8f3cbd1c
first commit;

Who changed what in which revision?

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