wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wifi-x-nucleo-idw01m1/SpwfSAInterface.h@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* mbed Microcontroller Library |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2015 ARM Limited |
JMF | 0:24d3eb812fd4 | 3 | * |
JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:24d3eb812fd4 | 5 | * you may not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 7 | * |
JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JMF | 0:24d3eb812fd4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 15 | */ |
JMF | 0:24d3eb812fd4 | 16 | |
JMF | 0:24d3eb812fd4 | 17 | /** |
JMF | 0:24d3eb812fd4 | 18 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 19 | * @file SpwfSAInterface.h |
JMF | 0:24d3eb812fd4 | 20 | * @author STMicroelectronics |
JMF | 0:24d3eb812fd4 | 21 | * @brief Header file of the NetworkStack for the SPWF Device |
JMF | 0:24d3eb812fd4 | 22 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 23 | * @copy |
JMF | 0:24d3eb812fd4 | 24 | * |
JMF | 0:24d3eb812fd4 | 25 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
JMF | 0:24d3eb812fd4 | 26 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
JMF | 0:24d3eb812fd4 | 27 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
JMF | 0:24d3eb812fd4 | 28 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
JMF | 0:24d3eb812fd4 | 29 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
JMF | 0:24d3eb812fd4 | 30 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
JMF | 0:24d3eb812fd4 | 31 | * |
JMF | 0:24d3eb812fd4 | 32 | * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> |
JMF | 0:24d3eb812fd4 | 33 | ****************************************************************************** |
JMF | 0:24d3eb812fd4 | 34 | */ |
JMF | 0:24d3eb812fd4 | 35 | |
JMF | 0:24d3eb812fd4 | 36 | #ifndef SPWFSA_INTERFACE_H |
JMF | 0:24d3eb812fd4 | 37 | #define SPWFSA_INTERFACE_H |
JMF | 0:24d3eb812fd4 | 38 | |
JMF | 0:24d3eb812fd4 | 39 | #include <limits.h> |
JMF | 0:24d3eb812fd4 | 40 | |
JMF | 0:24d3eb812fd4 | 41 | #include "mbed.h" |
JMF | 0:24d3eb812fd4 | 42 | |
JMF | 0:24d3eb812fd4 | 43 | #define IDW01M1 1 |
JMF | 0:24d3eb812fd4 | 44 | #define IDW04A1 2 |
JMF | 0:24d3eb812fd4 | 45 | |
JMF | 0:24d3eb812fd4 | 46 | #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 |
JMF | 0:24d3eb812fd4 | 47 | #include "SPWFSA01/SPWFSA01.h" |
JMF | 0:24d3eb812fd4 | 48 | #elif MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 |
JMF | 0:24d3eb812fd4 | 49 | #include "SPWFSA04/SPWFSA04.h" |
JMF | 0:24d3eb812fd4 | 50 | #else |
JMF | 0:24d3eb812fd4 | 51 | #error No (valid) Wi-Fi exapnsion board defined (MBED_CONF_IDW0XX1_EXPANSION_BOARD: options are IDW01M1 and IDW04A1) |
JMF | 0:24d3eb812fd4 | 52 | #endif |
JMF | 0:24d3eb812fd4 | 53 | |
JMF | 0:24d3eb812fd4 | 54 | // Various timeouts for different SPWF operations |
JMF | 0:24d3eb812fd4 | 55 | #define SPWF_CONNECT_TIMEOUT 60000 |
JMF | 0:24d3eb812fd4 | 56 | #define SPWF_DISCONNECT_TIMEOUT 30002 |
JMF | 0:24d3eb812fd4 | 57 | #define SPWF_HF_TIMEOUT 30001 |
JMF | 0:24d3eb812fd4 | 58 | #define SPWF_NETLOST_TIMEOUT 30000 |
JMF | 0:24d3eb812fd4 | 59 | #define SPWF_READ_BIN_TIMEOUT 13000 |
JMF | 0:24d3eb812fd4 | 60 | #define SPWF_CLOSE_TIMEOUT 10001 |
JMF | 0:24d3eb812fd4 | 61 | #define SPWF_SEND_TIMEOUT 10000 |
JMF | 0:24d3eb812fd4 | 62 | #define SPWF_INIT_TIMEOUT 8000 |
JMF | 0:24d3eb812fd4 | 63 | #define SPWF_OPEN_TIMEOUT 5002 |
JMF | 0:24d3eb812fd4 | 64 | #define SPWF_CONN_SND_TIMEOUT 5001 |
JMF | 0:24d3eb812fd4 | 65 | #define SPWF_SCAN_TIMEOUT 5000 |
JMF | 0:24d3eb812fd4 | 66 | #define SPWF_MISC_TIMEOUT 301 |
JMF | 0:24d3eb812fd4 | 67 | #define SPWF_RECV_TIMEOUT 300 |
JMF | 0:24d3eb812fd4 | 68 | |
JMF | 0:24d3eb812fd4 | 69 | /** SpwfSAInterface class |
JMF | 0:24d3eb812fd4 | 70 | * Implementation of the NetworkStack for the SPWF Device |
JMF | 0:24d3eb812fd4 | 71 | */ |
JMF | 0:24d3eb812fd4 | 72 | // NOTE - betzw - TODO: MUST become singleton! |
JMF | 0:24d3eb812fd4 | 73 | class SpwfSAInterface : public NetworkStack, public WiFiInterface |
JMF | 0:24d3eb812fd4 | 74 | { |
JMF | 0:24d3eb812fd4 | 75 | public: |
JMF | 0:24d3eb812fd4 | 76 | /** SpwfSAInterface constructor |
JMF | 0:24d3eb812fd4 | 77 | * @param tx TX pin |
JMF | 0:24d3eb812fd4 | 78 | * @param rx RX pin |
JMF | 0:24d3eb812fd4 | 79 | * @param rts RTS pin |
JMF | 0:24d3eb812fd4 | 80 | * @param cts CTS pin |
JMF | 0:24d3eb812fd4 | 81 | * @param debug Enable debugging |
JMF | 0:24d3eb812fd4 | 82 | * @param wakeup Wakeup pin |
JMF | 0:24d3eb812fd4 | 83 | * @param reset Reset pin |
JMF | 0:24d3eb812fd4 | 84 | */ |
JMF | 0:24d3eb812fd4 | 85 | SpwfSAInterface(PinName tx = MBED_CONF_IDW0XX1_TX, PinName rx = MBED_CONF_IDW0XX1_RX, |
JMF | 0:24d3eb812fd4 | 86 | PinName rts = SPWFSAXX_RTS_PIN, PinName cts = SPWFSAXX_CTS_PIN, bool debug = false, |
JMF | 0:24d3eb812fd4 | 87 | PinName wakeup = SPWFSAXX_WAKEUP_PIN, PinName reset = SPWFSAXX_RESET_PIN); |
JMF | 0:24d3eb812fd4 | 88 | |
JMF | 0:24d3eb812fd4 | 89 | /** Start the interface |
JMF | 0:24d3eb812fd4 | 90 | * |
JMF | 0:24d3eb812fd4 | 91 | * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
JMF | 0:24d3eb812fd4 | 92 | * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
JMF | 0:24d3eb812fd4 | 93 | * |
JMF | 0:24d3eb812fd4 | 94 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 95 | */ |
JMF | 0:24d3eb812fd4 | 96 | virtual nsapi_error_t connect(); |
JMF | 0:24d3eb812fd4 | 97 | |
JMF | 0:24d3eb812fd4 | 98 | /** Start the interface |
JMF | 0:24d3eb812fd4 | 99 | * |
JMF | 0:24d3eb812fd4 | 100 | * Attempts to connect to a WiFi network. |
JMF | 0:24d3eb812fd4 | 101 | * |
JMF | 0:24d3eb812fd4 | 102 | * @param ssid Name of the network to connect to |
JMF | 0:24d3eb812fd4 | 103 | * @param pass Security passphrase to connect to the network |
JMF | 0:24d3eb812fd4 | 104 | * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
JMF | 0:24d3eb812fd4 | 105 | * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 106 | * @return 0 on success, or error code on failure |
JMF | 0:24d3eb812fd4 | 107 | */ |
JMF | 0:24d3eb812fd4 | 108 | virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, |
JMF | 0:24d3eb812fd4 | 109 | uint8_t channel = 0); |
JMF | 0:24d3eb812fd4 | 110 | |
JMF | 0:24d3eb812fd4 | 111 | /** Set the WiFi network credentials |
JMF | 0:24d3eb812fd4 | 112 | * |
JMF | 0:24d3eb812fd4 | 113 | * @param ssid Name of the network to connect to |
JMF | 0:24d3eb812fd4 | 114 | * @param pass Security passphrase to connect to the network |
JMF | 0:24d3eb812fd4 | 115 | * @param security Type of encryption for connection |
JMF | 0:24d3eb812fd4 | 116 | * (defaults to NSAPI_SECURITY_NONE) |
JMF | 0:24d3eb812fd4 | 117 | * @return 0 on success, or error code on failure |
JMF | 0:24d3eb812fd4 | 118 | */ |
JMF | 0:24d3eb812fd4 | 119 | virtual nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
JMF | 0:24d3eb812fd4 | 120 | |
JMF | 0:24d3eb812fd4 | 121 | /** Set the WiFi network channel - NOT SUPPORTED |
JMF | 0:24d3eb812fd4 | 122 | * |
JMF | 0:24d3eb812fd4 | 123 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 124 | * |
JMF | 0:24d3eb812fd4 | 125 | * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
JMF | 0:24d3eb812fd4 | 126 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 127 | */ |
JMF | 0:24d3eb812fd4 | 128 | virtual nsapi_error_t set_channel(uint8_t channel); |
JMF | 0:24d3eb812fd4 | 129 | |
JMF | 0:24d3eb812fd4 | 130 | /** Stop the interface |
JMF | 0:24d3eb812fd4 | 131 | * @return `NSAPI_ERROR_OK` on success, negative on failure |
JMF | 0:24d3eb812fd4 | 132 | */ |
JMF | 0:24d3eb812fd4 | 133 | virtual nsapi_error_t disconnect(); |
JMF | 0:24d3eb812fd4 | 134 | |
JMF | 0:24d3eb812fd4 | 135 | /** Get the internally stored IP address |
JMF | 0:24d3eb812fd4 | 136 | * @return IP address of the interface or null if not yet connected |
JMF | 0:24d3eb812fd4 | 137 | */ |
JMF | 0:24d3eb812fd4 | 138 | virtual const char *get_ip_address(); |
JMF | 0:24d3eb812fd4 | 139 | |
JMF | 0:24d3eb812fd4 | 140 | /** Get the internally stored MAC address |
JMF | 0:24d3eb812fd4 | 141 | * @return MAC address of the interface |
JMF | 0:24d3eb812fd4 | 142 | */ |
JMF | 0:24d3eb812fd4 | 143 | virtual const char *get_mac_address(); |
JMF | 0:24d3eb812fd4 | 144 | |
JMF | 0:24d3eb812fd4 | 145 | /** Get the local gateway |
JMF | 0:24d3eb812fd4 | 146 | * |
JMF | 0:24d3eb812fd4 | 147 | * @return Null-terminated representation of the local gateway |
JMF | 0:24d3eb812fd4 | 148 | * or null if no network mask has been received |
JMF | 0:24d3eb812fd4 | 149 | */ |
JMF | 0:24d3eb812fd4 | 150 | virtual const char *get_gateway(); |
JMF | 0:24d3eb812fd4 | 151 | |
JMF | 0:24d3eb812fd4 | 152 | /** Get the local network mask |
JMF | 0:24d3eb812fd4 | 153 | * |
JMF | 0:24d3eb812fd4 | 154 | * @return Null-terminated representation of the local network mask |
JMF | 0:24d3eb812fd4 | 155 | * or null if no network mask has been received |
JMF | 0:24d3eb812fd4 | 156 | */ |
JMF | 0:24d3eb812fd4 | 157 | virtual const char *get_netmask(); |
JMF | 0:24d3eb812fd4 | 158 | |
JMF | 0:24d3eb812fd4 | 159 | /** Gets the current radio signal strength for active connection |
JMF | 0:24d3eb812fd4 | 160 | * |
JMF | 0:24d3eb812fd4 | 161 | * @return Connection strength in dBm (negative value) |
JMF | 0:24d3eb812fd4 | 162 | */ |
JMF | 0:24d3eb812fd4 | 163 | virtual int8_t get_rssi(); |
JMF | 0:24d3eb812fd4 | 164 | |
JMF | 0:24d3eb812fd4 | 165 | /** Scan for available networks |
JMF | 0:24d3eb812fd4 | 166 | * |
JMF | 0:24d3eb812fd4 | 167 | * This function will block. If the @a count is 0, function will only return count of available networks, so that |
JMF | 0:24d3eb812fd4 | 168 | * user can allocated necessary memory. If the @count is grater than 0 and the @a ap is not NULL it'll be populated |
JMF | 0:24d3eb812fd4 | 169 | * with discovered networks up to value of @a count. |
JMF | 0:24d3eb812fd4 | 170 | * |
JMF | 0:24d3eb812fd4 | 171 | * @param res Pointer to allocated array to store discovered AP |
JMF | 0:24d3eb812fd4 | 172 | * @param count Size of allocated @a res array, or 0 to only count available AP |
JMF | 0:24d3eb812fd4 | 173 | * @return Number of entries in @a, or if @a count was 0 number of available networks, |
JMF | 0:24d3eb812fd4 | 174 | * negative on error see @a nsapi_error |
JMF | 0:24d3eb812fd4 | 175 | */ |
JMF | 0:24d3eb812fd4 | 176 | virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, unsigned count); |
JMF | 0:24d3eb812fd4 | 177 | |
JMF | 0:24d3eb812fd4 | 178 | /** Translates a hostname to an IP address with specific version |
JMF | 0:24d3eb812fd4 | 179 | * |
JMF | 0:24d3eb812fd4 | 180 | * The hostname may be either a domain name or an IP address. If the |
JMF | 0:24d3eb812fd4 | 181 | * hostname is an IP address, no network transactions will be performed. |
JMF | 0:24d3eb812fd4 | 182 | * |
JMF | 0:24d3eb812fd4 | 183 | * If no stack-specific DNS resolution is provided, the hostname |
JMF | 0:24d3eb812fd4 | 184 | * will be resolve using a UDP socket on the stack. |
JMF | 0:24d3eb812fd4 | 185 | * |
JMF | 0:24d3eb812fd4 | 186 | * @param address Destination for the host SocketAddress |
JMF | 0:24d3eb812fd4 | 187 | * @param host Hostname to resolve |
JMF | 0:24d3eb812fd4 | 188 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
JMF | 0:24d3eb812fd4 | 189 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
JMF | 0:24d3eb812fd4 | 190 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 191 | */ |
JMF | 0:24d3eb812fd4 | 192 | using NetworkInterface::gethostbyname; |
JMF | 0:24d3eb812fd4 | 193 | |
JMF | 0:24d3eb812fd4 | 194 | /** Add a domain name server to list of servers to query |
JMF | 0:24d3eb812fd4 | 195 | * |
JMF | 0:24d3eb812fd4 | 196 | * @param addr Destination for the host address |
JMF | 0:24d3eb812fd4 | 197 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 198 | */ |
JMF | 0:24d3eb812fd4 | 199 | using NetworkInterface::add_dns_server; |
JMF | 0:24d3eb812fd4 | 200 | |
JMF | 0:24d3eb812fd4 | 201 | private: |
JMF | 0:24d3eb812fd4 | 202 | /** Open a socket |
JMF | 0:24d3eb812fd4 | 203 | * @param handle Handle in which to store new socket |
JMF | 0:24d3eb812fd4 | 204 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
JMF | 0:24d3eb812fd4 | 205 | * @return `NSAPI_ERROR_OK` on success, negative on failure |
JMF | 0:24d3eb812fd4 | 206 | */ |
JMF | 0:24d3eb812fd4 | 207 | virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto); |
JMF | 0:24d3eb812fd4 | 208 | |
JMF | 0:24d3eb812fd4 | 209 | /** Close the socket |
JMF | 0:24d3eb812fd4 | 210 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 211 | * @return `NSAPI_ERROR_OK` on success, negative on failure |
JMF | 0:24d3eb812fd4 | 212 | * @note On failure, any memory associated with the socket must still |
JMF | 0:24d3eb812fd4 | 213 | * be cleaned up |
JMF | 0:24d3eb812fd4 | 214 | */ |
JMF | 0:24d3eb812fd4 | 215 | virtual nsapi_error_t socket_close(void *handle); |
JMF | 0:24d3eb812fd4 | 216 | |
JMF | 0:24d3eb812fd4 | 217 | /** Bind a server socket to a specific port - NOT SUPPORTED |
JMF | 0:24d3eb812fd4 | 218 | * |
JMF | 0:24d3eb812fd4 | 219 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 220 | * |
JMF | 0:24d3eb812fd4 | 221 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 222 | * @param address Local address to listen for incoming connections on |
JMF | 0:24d3eb812fd4 | 223 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 224 | */ |
JMF | 0:24d3eb812fd4 | 225 | virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address); |
JMF | 0:24d3eb812fd4 | 226 | |
JMF | 0:24d3eb812fd4 | 227 | /** Start listening for incoming connections - NOT SUPPORTED |
JMF | 0:24d3eb812fd4 | 228 | * |
JMF | 0:24d3eb812fd4 | 229 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 230 | * |
JMF | 0:24d3eb812fd4 | 231 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 232 | * @param backlog Number of pending connections that can be queued up at any |
JMF | 0:24d3eb812fd4 | 233 | * one time [Default: 1] |
JMF | 0:24d3eb812fd4 | 234 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 235 | */ |
JMF | 0:24d3eb812fd4 | 236 | virtual nsapi_error_t socket_listen(void *handle, int backlog); |
JMF | 0:24d3eb812fd4 | 237 | |
JMF | 0:24d3eb812fd4 | 238 | /** Connects this TCP socket to the server |
JMF | 0:24d3eb812fd4 | 239 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 240 | * @param address SocketAddress to connect to |
JMF | 0:24d3eb812fd4 | 241 | * @return `NSAPI_ERROR_OK` on success, negative on failure |
JMF | 0:24d3eb812fd4 | 242 | */ |
JMF | 0:24d3eb812fd4 | 243 | virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address); |
JMF | 0:24d3eb812fd4 | 244 | |
JMF | 0:24d3eb812fd4 | 245 | /** Accept a new connection - NOT SUPPORTED |
JMF | 0:24d3eb812fd4 | 246 | * |
JMF | 0:24d3eb812fd4 | 247 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 248 | * |
JMF | 0:24d3eb812fd4 | 249 | * @param handle Handle in which to store new socket |
JMF | 0:24d3eb812fd4 | 250 | * @param server Socket handle to server to accept from |
JMF | 0:24d3eb812fd4 | 251 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 252 | */ |
JMF | 0:24d3eb812fd4 | 253 | virtual nsapi_error_t socket_accept(void *handle, void **socket, SocketAddress *address); |
JMF | 0:24d3eb812fd4 | 254 | |
JMF | 0:24d3eb812fd4 | 255 | /** Send data to the remote host |
JMF | 0:24d3eb812fd4 | 256 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 257 | * @param data The buffer to send to the host |
JMF | 0:24d3eb812fd4 | 258 | * @param size The length of the buffer to send |
JMF | 0:24d3eb812fd4 | 259 | * @return Number of written bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 260 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 261 | * immediately return NSAPI_ERROR_WOULD_BLOCK |
JMF | 0:24d3eb812fd4 | 262 | */ |
JMF | 0:24d3eb812fd4 | 263 | virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 264 | |
JMF | 0:24d3eb812fd4 | 265 | /** Receive data from the remote host |
JMF | 0:24d3eb812fd4 | 266 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 267 | * @param data The buffer in which to store the data received from the host |
JMF | 0:24d3eb812fd4 | 268 | * @param size The maximum length of the buffer |
JMF | 0:24d3eb812fd4 | 269 | * @return Number of received bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 270 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 271 | * immediately return NSAPI_ERROR_WOULD_BLOCK |
JMF | 0:24d3eb812fd4 | 272 | */ |
JMF | 0:24d3eb812fd4 | 273 | virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 274 | |
JMF | 0:24d3eb812fd4 | 275 | /** Send a packet to a remote endpoint |
JMF | 0:24d3eb812fd4 | 276 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 277 | * @param address The remote SocketAddress |
JMF | 0:24d3eb812fd4 | 278 | * @param data The packet to be sent |
JMF | 0:24d3eb812fd4 | 279 | * @param size The length of the packet to be sent |
JMF | 0:24d3eb812fd4 | 280 | * @return The number of written bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 281 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 282 | * immediately return NSAPI_ERROR_WOULD_BLOCK |
JMF | 0:24d3eb812fd4 | 283 | */ |
JMF | 0:24d3eb812fd4 | 284 | virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 285 | |
JMF | 0:24d3eb812fd4 | 286 | /** Receive a packet from a remote endpoint |
JMF | 0:24d3eb812fd4 | 287 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 288 | * @param address Destination for the remote SocketAddress or null |
JMF | 0:24d3eb812fd4 | 289 | * @param buffer The buffer for storing the incoming packet data |
JMF | 0:24d3eb812fd4 | 290 | * If a packet is too long to fit in the supplied buffer, |
JMF | 0:24d3eb812fd4 | 291 | * excess bytes are discarded |
JMF | 0:24d3eb812fd4 | 292 | * @param size The length of the buffer |
JMF | 0:24d3eb812fd4 | 293 | * @return The number of received bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 294 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 295 | * immediately return NSAPI_ERROR_WOULD_BLOCK |
JMF | 0:24d3eb812fd4 | 296 | */ |
JMF | 0:24d3eb812fd4 | 297 | virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
JMF | 0:24d3eb812fd4 | 298 | |
JMF | 0:24d3eb812fd4 | 299 | /** Register a callback on state change of the socket |
JMF | 0:24d3eb812fd4 | 300 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 301 | * @param callback Function to call on state change |
JMF | 0:24d3eb812fd4 | 302 | * @param data Argument to pass to callback |
JMF | 0:24d3eb812fd4 | 303 | * @note Callback may be called in an interrupt context. |
JMF | 0:24d3eb812fd4 | 304 | */ |
JMF | 0:24d3eb812fd4 | 305 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
JMF | 0:24d3eb812fd4 | 306 | |
JMF | 0:24d3eb812fd4 | 307 | /** Provide access to the NetworkStack object |
JMF | 0:24d3eb812fd4 | 308 | * |
JMF | 0:24d3eb812fd4 | 309 | * @return The underlying NetworkStack object |
JMF | 0:24d3eb812fd4 | 310 | */ |
JMF | 0:24d3eb812fd4 | 311 | virtual NetworkStack *get_stack() |
JMF | 0:24d3eb812fd4 | 312 | { |
JMF | 0:24d3eb812fd4 | 313 | return this; |
JMF | 0:24d3eb812fd4 | 314 | } |
JMF | 0:24d3eb812fd4 | 315 | |
JMF | 0:24d3eb812fd4 | 316 | private: |
JMF | 0:24d3eb812fd4 | 317 | /** spwf_socket class |
JMF | 0:24d3eb812fd4 | 318 | * Implementation of SPWF socket structure |
JMF | 0:24d3eb812fd4 | 319 | */ |
JMF | 0:24d3eb812fd4 | 320 | typedef struct spwf_socket { |
JMF | 0:24d3eb812fd4 | 321 | int8_t internal_id; |
JMF | 0:24d3eb812fd4 | 322 | int spwf_id; |
JMF | 0:24d3eb812fd4 | 323 | bool server_gone; |
JMF | 0:24d3eb812fd4 | 324 | bool no_more_data; |
JMF | 0:24d3eb812fd4 | 325 | nsapi_protocol_t proto; |
JMF | 0:24d3eb812fd4 | 326 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 327 | } spwf_socket_t; |
JMF | 0:24d3eb812fd4 | 328 | |
JMF | 0:24d3eb812fd4 | 329 | bool _socket_is_open(spwf_socket_t *sock) { |
JMF | 0:24d3eb812fd4 | 330 | if(((unsigned int)sock->internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { |
JMF | 0:24d3eb812fd4 | 331 | return (_ids[sock->internal_id].internal_id == sock->internal_id); |
JMF | 0:24d3eb812fd4 | 332 | } |
JMF | 0:24d3eb812fd4 | 333 | return false; |
JMF | 0:24d3eb812fd4 | 334 | } |
JMF | 0:24d3eb812fd4 | 335 | |
JMF | 0:24d3eb812fd4 | 336 | bool _socket_has_connected(spwf_socket_t *sock) { |
JMF | 0:24d3eb812fd4 | 337 | return (_socket_is_open(sock) && (((unsigned int)sock->spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT))); |
JMF | 0:24d3eb812fd4 | 338 | } |
JMF | 0:24d3eb812fd4 | 339 | |
JMF | 0:24d3eb812fd4 | 340 | bool _socket_is_still_connected(spwf_socket_t *sock) { |
JMF | 0:24d3eb812fd4 | 341 | return (_socket_has_connected(sock) && !sock->server_gone); |
JMF | 0:24d3eb812fd4 | 342 | } |
JMF | 0:24d3eb812fd4 | 343 | |
JMF | 0:24d3eb812fd4 | 344 | bool _socket_is_open(int internal_id) { |
JMF | 0:24d3eb812fd4 | 345 | if(((unsigned int)internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { |
JMF | 0:24d3eb812fd4 | 346 | return (_ids[internal_id].internal_id == internal_id); |
JMF | 0:24d3eb812fd4 | 347 | } |
JMF | 0:24d3eb812fd4 | 348 | return false; |
JMF | 0:24d3eb812fd4 | 349 | } |
JMF | 0:24d3eb812fd4 | 350 | |
JMF | 0:24d3eb812fd4 | 351 | bool _socket_has_connected(int internal_id) { |
JMF | 0:24d3eb812fd4 | 352 | if(!_socket_is_open(internal_id)) return false; |
JMF | 0:24d3eb812fd4 | 353 | |
JMF | 0:24d3eb812fd4 | 354 | spwf_socket_t &sock = _ids[internal_id]; |
JMF | 0:24d3eb812fd4 | 355 | return (sock.spwf_id != SPWFSA_SOCKET_COUNT); |
JMF | 0:24d3eb812fd4 | 356 | } |
JMF | 0:24d3eb812fd4 | 357 | |
JMF | 0:24d3eb812fd4 | 358 | bool _socket_is_still_connected(int internal_id) { |
JMF | 0:24d3eb812fd4 | 359 | if(!_socket_has_connected(internal_id)) return false; |
JMF | 0:24d3eb812fd4 | 360 | |
JMF | 0:24d3eb812fd4 | 361 | spwf_socket_t &sock = _ids[internal_id]; |
JMF | 0:24d3eb812fd4 | 362 | return (!sock.server_gone); |
JMF | 0:24d3eb812fd4 | 363 | } |
JMF | 0:24d3eb812fd4 | 364 | |
JMF | 0:24d3eb812fd4 | 365 | void reset_credentials() { |
JMF | 0:24d3eb812fd4 | 366 | memset(ap_ssid, 0, sizeof(ap_ssid)); |
JMF | 0:24d3eb812fd4 | 367 | memset(ap_pass, 0, sizeof(ap_pass)); |
JMF | 0:24d3eb812fd4 | 368 | ap_sec = NSAPI_SECURITY_NONE; |
JMF | 0:24d3eb812fd4 | 369 | } |
JMF | 0:24d3eb812fd4 | 370 | |
JMF | 0:24d3eb812fd4 | 371 | #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 |
JMF | 0:24d3eb812fd4 | 372 | SPWFSA01 _spwf; |
JMF | 0:24d3eb812fd4 | 373 | #elif MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 |
JMF | 0:24d3eb812fd4 | 374 | SPWFSA04 _spwf; |
JMF | 0:24d3eb812fd4 | 375 | #endif |
JMF | 0:24d3eb812fd4 | 376 | |
JMF | 0:24d3eb812fd4 | 377 | bool _isInitialized; |
JMF | 0:24d3eb812fd4 | 378 | bool _dbg_on; |
JMF | 0:24d3eb812fd4 | 379 | bool _connected_to_network; |
JMF | 0:24d3eb812fd4 | 380 | |
JMF | 0:24d3eb812fd4 | 381 | spwf_socket_t _ids[SPWFSA_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 382 | struct { |
JMF | 0:24d3eb812fd4 | 383 | void (*callback)(void *); |
JMF | 0:24d3eb812fd4 | 384 | void *data; |
JMF | 0:24d3eb812fd4 | 385 | } _cbs[SPWFSA_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 386 | int _internal_ids[SPWFSA_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 387 | |
JMF | 0:24d3eb812fd4 | 388 | #if MBED_CONF_RTOS_PRESENT |
JMF | 0:24d3eb812fd4 | 389 | Mutex _spwf_mutex; |
JMF | 0:24d3eb812fd4 | 390 | #endif |
JMF | 0:24d3eb812fd4 | 391 | |
JMF | 0:24d3eb812fd4 | 392 | char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ |
JMF | 0:24d3eb812fd4 | 393 | nsapi_security_t ap_sec; |
JMF | 0:24d3eb812fd4 | 394 | char ap_pass[64]; /* The longest allowed passphrase */ |
JMF | 0:24d3eb812fd4 | 395 | |
JMF | 0:24d3eb812fd4 | 396 | private: |
JMF | 0:24d3eb812fd4 | 397 | void event(void); |
JMF | 0:24d3eb812fd4 | 398 | nsapi_error_t init(void); |
JMF | 0:24d3eb812fd4 | 399 | nsapi_size_or_error_t _socket_recv(void *handle, void *data, unsigned size, bool datagram); |
JMF | 0:24d3eb812fd4 | 400 | |
JMF | 0:24d3eb812fd4 | 401 | |
JMF | 0:24d3eb812fd4 | 402 | int get_internal_id(int spwf_id) { // checks also if `spwf_id` is (still) "valid" |
JMF | 0:24d3eb812fd4 | 403 | if(((unsigned int)spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { // valid `spwf_id` |
JMF | 0:24d3eb812fd4 | 404 | int internal_id = _internal_ids[spwf_id]; |
JMF | 0:24d3eb812fd4 | 405 | if((_socket_is_open(internal_id)) && (_ids[internal_id].spwf_id == spwf_id)) { |
JMF | 0:24d3eb812fd4 | 406 | return internal_id; |
JMF | 0:24d3eb812fd4 | 407 | } else { |
JMF | 0:24d3eb812fd4 | 408 | return SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 409 | } |
JMF | 0:24d3eb812fd4 | 410 | } else { // invalid `spwf_id` |
JMF | 0:24d3eb812fd4 | 411 | return SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 412 | } |
JMF | 0:24d3eb812fd4 | 413 | } |
JMF | 0:24d3eb812fd4 | 414 | |
JMF | 0:24d3eb812fd4 | 415 | /* Called at initialization or after module hard fault */ |
JMF | 0:24d3eb812fd4 | 416 | void inner_constructor() { |
JMF | 0:24d3eb812fd4 | 417 | memset(_ids, 0, sizeof(_ids)); |
JMF | 0:24d3eb812fd4 | 418 | memset(_cbs, 0, sizeof(_cbs)); |
JMF | 0:24d3eb812fd4 | 419 | |
JMF | 0:24d3eb812fd4 | 420 | for (int sock_cnt = 0; sock_cnt < SPWFSA_SOCKET_COUNT; sock_cnt++) { |
JMF | 0:24d3eb812fd4 | 421 | _ids[sock_cnt].internal_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 422 | _ids[sock_cnt].spwf_id = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 423 | _internal_ids[sock_cnt] = SPWFSA_SOCKET_COUNT; |
JMF | 0:24d3eb812fd4 | 424 | } |
JMF | 0:24d3eb812fd4 | 425 | |
JMF | 0:24d3eb812fd4 | 426 | _spwf.attach(this, &SpwfSAInterface::event); |
JMF | 0:24d3eb812fd4 | 427 | |
JMF | 0:24d3eb812fd4 | 428 | _connected_to_network = false; |
JMF | 0:24d3eb812fd4 | 429 | _isInitialized = false; |
JMF | 0:24d3eb812fd4 | 430 | } |
JMF | 0:24d3eb812fd4 | 431 | |
JMF | 0:24d3eb812fd4 | 432 | private: |
JMF | 0:24d3eb812fd4 | 433 | friend class SPWFSAxx; |
JMF | 0:24d3eb812fd4 | 434 | friend class SPWFSA01; |
JMF | 0:24d3eb812fd4 | 435 | friend class SPWFSA04; |
JMF | 0:24d3eb812fd4 | 436 | }; |
JMF | 0:24d3eb812fd4 | 437 | |
JMF | 0:24d3eb812fd4 | 438 | #define CHECK_NOT_CONNECTED_ERR() { \ |
JMF | 0:24d3eb812fd4 | 439 | if(!_connected_to_network) return NSAPI_ERROR_NO_CONNECTION; \ |
JMF | 0:24d3eb812fd4 | 440 | } \ |
JMF | 0:24d3eb812fd4 | 441 | |
JMF | 0:24d3eb812fd4 | 442 | |
JMF | 0:24d3eb812fd4 | 443 | #endif |