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