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.
SpwfSAInterface.h
00001 /* mbed Microcontroller Library 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 /** 00018 ****************************************************************************** 00019 * @file SpwfSAInterface.h 00020 * @author STMicroelectronics 00021 * @brief Header file of the NetworkStack for the SPWF Device 00022 ****************************************************************************** 00023 * @copy 00024 * 00025 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00026 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00027 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00028 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00029 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00030 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00031 * 00032 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> 00033 ****************************************************************************** 00034 */ 00035 00036 #ifndef SPWFSA_INTERFACE_H 00037 #define SPWFSA_INTERFACE_H 00038 00039 #include <limits.h> 00040 00041 #include "mbed.h" 00042 00043 #define IDW01M1 1 00044 #define IDW04A1 2 00045 00046 #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 00047 #include "SPWFSA01/SPWFSA01.h" 00048 #elif MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 00049 #include "SPWFSA04/SPWFSA04.h" 00050 #else 00051 #error No (valid) Wi-Fi exapnsion board defined (MBED_CONF_IDW0XX1_EXPANSION_BOARD: options are IDW01M1 and IDW04A1) 00052 #endif 00053 00054 // Various timeouts for different SPWF operations 00055 #define SPWF_CONNECT_TIMEOUT 60000 00056 #define SPWF_DISCONNECT_TIMEOUT 30002 00057 #define SPWF_HF_TIMEOUT 30001 00058 #define SPWF_NETLOST_TIMEOUT 30000 00059 #define SPWF_READ_BIN_TIMEOUT 13000 00060 #define SPWF_CLOSE_TIMEOUT 10001 00061 #define SPWF_SEND_TIMEOUT 10000 00062 #define SPWF_INIT_TIMEOUT 8000 00063 #define SPWF_OPEN_TIMEOUT 5002 00064 #define SPWF_CONN_SND_TIMEOUT 5001 00065 #define SPWF_SCAN_TIMEOUT 5000 00066 #define SPWF_MISC_TIMEOUT 301 00067 #define SPWF_RECV_TIMEOUT 300 00068 00069 /** SpwfSAInterface class 00070 * Implementation of the NetworkStack for the SPWF Device 00071 */ 00072 // NOTE - betzw - TODO: MUST become singleton! 00073 class SpwfSAInterface : public NetworkStack, public WiFiInterface 00074 { 00075 public: 00076 /** SpwfSAInterface constructor 00077 * @param tx TX pin 00078 * @param rx RX pin 00079 * @param rts RTS pin 00080 * @param cts CTS pin 00081 * @param debug Enable debugging 00082 * @param wakeup Wakeup pin 00083 * @param reset Reset pin 00084 */ 00085 SpwfSAInterface(PinName tx = MBED_CONF_IDW0XX1_TX, PinName rx = MBED_CONF_IDW0XX1_RX, 00086 PinName rts = SPWFSAXX_RTS_PIN, PinName cts = SPWFSAXX_CTS_PIN, bool debug = false, 00087 PinName wakeup = SPWFSAXX_WAKEUP_PIN, PinName reset = SPWFSAXX_RESET_PIN); 00088 00089 /** Start the interface 00090 * 00091 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. 00092 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. 00093 * 00094 * @return 0 on success, negative error code on failure 00095 */ 00096 virtual nsapi_error_t connect(); 00097 00098 /** Start the interface 00099 * 00100 * Attempts to connect to a WiFi network. 00101 * 00102 * @param ssid Name of the network to connect to 00103 * @param pass Security passphrase to connect to the network 00104 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) 00105 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED 00106 * @return 0 on success, or error code on failure 00107 */ 00108 virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, 00109 uint8_t channel = 0); 00110 00111 /** Set the WiFi network credentials 00112 * 00113 * @param ssid Name of the network to connect to 00114 * @param pass Security passphrase to connect to the network 00115 * @param security Type of encryption for connection 00116 * (defaults to NSAPI_SECURITY_NONE) 00117 * @return 0 on success, or error code on failure 00118 */ 00119 virtual nsapi_error_t set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); 00120 00121 /** Set the WiFi network channel - NOT SUPPORTED 00122 * 00123 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00124 * 00125 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) 00126 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00127 */ 00128 virtual nsapi_error_t set_channel(uint8_t channel); 00129 00130 /** Stop the interface 00131 * @return `NSAPI_ERROR_OK` on success, negative on failure 00132 */ 00133 virtual nsapi_error_t disconnect(); 00134 00135 /** Get the internally stored IP address 00136 * @return IP address of the interface or null if not yet connected 00137 */ 00138 virtual const char *get_ip_address(); 00139 00140 /** Get the internally stored MAC address 00141 * @return MAC address of the interface 00142 */ 00143 virtual const char *get_mac_address(); 00144 00145 /** Get the local gateway 00146 * 00147 * @return Null-terminated representation of the local gateway 00148 * or null if no network mask has been received 00149 */ 00150 virtual const char *get_gateway(); 00151 00152 /** Get the local network mask 00153 * 00154 * @return Null-terminated representation of the local network mask 00155 * or null if no network mask has been received 00156 */ 00157 virtual const char *get_netmask(); 00158 00159 /** Gets the current radio signal strength for active connection 00160 * 00161 * @return Connection strength in dBm (negative value) 00162 */ 00163 virtual int8_t get_rssi(); 00164 00165 /** Scan for available networks 00166 * 00167 * This function will block. If the @a count is 0, function will only return count of available networks, so that 00168 * user can allocated necessary memory. If the @count is grater than 0 and the @a ap is not NULL it'll be populated 00169 * with discovered networks up to value of @a count. 00170 * 00171 * @param res Pointer to allocated array to store discovered AP 00172 * @param count Size of allocated @a res array, or 0 to only count available AP 00173 * @return Number of entries in @a, or if @a count was 0 number of available networks, 00174 * negative on error see @a nsapi_error 00175 */ 00176 virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, unsigned count); 00177 00178 /** Translates a hostname to an IP address with specific version 00179 * 00180 * The hostname may be either a domain name or an IP address. If the 00181 * hostname is an IP address, no network transactions will be performed. 00182 * 00183 * If no stack-specific DNS resolution is provided, the hostname 00184 * will be resolve using a UDP socket on the stack. 00185 * 00186 * @param address Destination for the host SocketAddress 00187 * @param host Hostname to resolve 00188 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00189 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00190 * @return 0 on success, negative error code on failure 00191 */ 00192 using NetworkInterface::gethostbyname; 00193 00194 /** Add a domain name server to list of servers to query 00195 * 00196 * @param addr Destination for the host address 00197 * @return 0 on success, negative error code on failure 00198 */ 00199 using NetworkInterface::add_dns_server; 00200 00201 private: 00202 /** Open a socket 00203 * @param handle Handle in which to store new socket 00204 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP 00205 * @return `NSAPI_ERROR_OK` on success, negative on failure 00206 */ 00207 virtual nsapi_error_t socket_open(void **handle, nsapi_protocol_t proto); 00208 00209 /** Close the socket 00210 * @param handle Socket handle 00211 * @return `NSAPI_ERROR_OK` on success, negative on failure 00212 * @note On failure, any memory associated with the socket must still 00213 * be cleaned up 00214 */ 00215 virtual nsapi_error_t socket_close(void *handle); 00216 00217 /** Bind a server socket to a specific port - NOT SUPPORTED 00218 * 00219 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00220 * 00221 * @param handle Socket handle 00222 * @param address Local address to listen for incoming connections on 00223 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00224 */ 00225 virtual nsapi_error_t socket_bind(void *handle, const SocketAddress &address); 00226 00227 /** Start listening for incoming connections - NOT SUPPORTED 00228 * 00229 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00230 * 00231 * @param handle Socket handle 00232 * @param backlog Number of pending connections that can be queued up at any 00233 * one time [Default: 1] 00234 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00235 */ 00236 virtual nsapi_error_t socket_listen(void *handle, int backlog); 00237 00238 /** Connects this TCP socket to the server 00239 * @param handle Socket handle 00240 * @param address SocketAddress to connect to 00241 * @return `NSAPI_ERROR_OK` on success, negative on failure 00242 */ 00243 virtual nsapi_error_t socket_connect(void *handle, const SocketAddress &address); 00244 00245 /** Accept a new connection - NOT SUPPORTED 00246 * 00247 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00248 * 00249 * @param handle Handle in which to store new socket 00250 * @param server Socket handle to server to accept from 00251 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00252 */ 00253 virtual nsapi_error_t socket_accept(void *handle, void **socket, SocketAddress *address); 00254 00255 /** Send data to the remote host 00256 * @param handle Socket handle 00257 * @param data The buffer to send to the host 00258 * @param size The length of the buffer to send 00259 * @return Number of written bytes on success, negative on failure 00260 * @note This call is not-blocking, if this call would block, must 00261 * immediately return NSAPI_ERROR_WOULD_BLOCK 00262 */ 00263 virtual nsapi_size_or_error_t socket_send(void *handle, const void *data, unsigned size); 00264 00265 /** Receive data from the remote host 00266 * @param handle Socket handle 00267 * @param data The buffer in which to store the data received from the host 00268 * @param size The maximum length of the buffer 00269 * @return Number of received bytes on success, negative on failure 00270 * @note This call is not-blocking, if this call would block, must 00271 * immediately return NSAPI_ERROR_WOULD_BLOCK 00272 */ 00273 virtual nsapi_size_or_error_t socket_recv(void *handle, void *data, unsigned size); 00274 00275 /** Send a packet to a remote endpoint 00276 * @param handle Socket handle 00277 * @param address The remote SocketAddress 00278 * @param data The packet to be sent 00279 * @param size The length of the packet to be sent 00280 * @return The number of written bytes on success, negative on failure 00281 * @note This call is not-blocking, if this call would block, must 00282 * immediately return NSAPI_ERROR_WOULD_BLOCK 00283 */ 00284 virtual nsapi_size_or_error_t socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00285 00286 /** Receive a packet from a remote endpoint 00287 * @param handle Socket handle 00288 * @param address Destination for the remote SocketAddress or null 00289 * @param buffer The buffer for storing the incoming packet data 00290 * If a packet is too long to fit in the supplied buffer, 00291 * excess bytes are discarded 00292 * @param size The length of the buffer 00293 * @return The number of received bytes on success, negative on failure 00294 * @note This call is not-blocking, if this call would block, must 00295 * immediately return NSAPI_ERROR_WOULD_BLOCK 00296 */ 00297 virtual nsapi_size_or_error_t socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 00298 00299 /** Register a callback on state change of the socket 00300 * @param handle Socket handle 00301 * @param callback Function to call on state change 00302 * @param data Argument to pass to callback 00303 * @note Callback may be called in an interrupt context. 00304 */ 00305 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00306 00307 /** Provide access to the NetworkStack object 00308 * 00309 * @return The underlying NetworkStack object 00310 */ 00311 virtual NetworkStack *get_stack() 00312 { 00313 return this; 00314 } 00315 00316 private: 00317 /** spwf_socket class 00318 * Implementation of SPWF socket structure 00319 */ 00320 typedef struct spwf_socket { 00321 int8_t internal_id; 00322 int spwf_id; 00323 bool server_gone; 00324 bool no_more_data; 00325 nsapi_protocol_t proto; 00326 SocketAddress addr; 00327 } spwf_socket_t; 00328 00329 bool _socket_is_open(spwf_socket_t *sock) { 00330 if(((unsigned int)sock->internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { 00331 return (_ids[sock->internal_id].internal_id == sock->internal_id); 00332 } 00333 return false; 00334 } 00335 00336 bool _socket_has_connected(spwf_socket_t *sock) { 00337 return (_socket_is_open(sock) && (((unsigned int)sock->spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT))); 00338 } 00339 00340 bool _socket_is_still_connected(spwf_socket_t *sock) { 00341 return (_socket_has_connected(sock) && !sock->server_gone); 00342 } 00343 00344 bool _socket_is_open(int internal_id) { 00345 if(((unsigned int)internal_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { 00346 return (_ids[internal_id].internal_id == internal_id); 00347 } 00348 return false; 00349 } 00350 00351 bool _socket_has_connected(int internal_id) { 00352 if(!_socket_is_open(internal_id)) return false; 00353 00354 spwf_socket_t &sock = _ids[internal_id]; 00355 return (sock.spwf_id != SPWFSA_SOCKET_COUNT); 00356 } 00357 00358 bool _socket_is_still_connected(int internal_id) { 00359 if(!_socket_has_connected(internal_id)) return false; 00360 00361 spwf_socket_t &sock = _ids[internal_id]; 00362 return (!sock.server_gone); 00363 } 00364 00365 void reset_credentials() { 00366 memset(ap_ssid, 0, sizeof(ap_ssid)); 00367 memset(ap_pass, 0, sizeof(ap_pass)); 00368 ap_sec = NSAPI_SECURITY_NONE; 00369 } 00370 00371 #if MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW01M1 00372 SPWFSA01 _spwf; 00373 #elif MBED_CONF_IDW0XX1_EXPANSION_BOARD == IDW04A1 00374 SPWFSA04 _spwf; 00375 #endif 00376 00377 bool _isInitialized; 00378 bool _dbg_on; 00379 bool _connected_to_network; 00380 00381 spwf_socket_t _ids[SPWFSA_SOCKET_COUNT]; 00382 struct { 00383 void (*callback)(void *); 00384 void *data; 00385 } _cbs[SPWFSA_SOCKET_COUNT]; 00386 int _internal_ids[SPWFSA_SOCKET_COUNT]; 00387 00388 #if MBED_CONF_RTOS_PRESENT 00389 Mutex _spwf_mutex; 00390 #endif 00391 00392 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ 00393 nsapi_security_t ap_sec; 00394 char ap_pass[64]; /* The longest allowed passphrase */ 00395 00396 private: 00397 void event(void); 00398 nsapi_error_t init(void); 00399 nsapi_size_or_error_t _socket_recv(void *handle, void *data, unsigned size, bool datagram); 00400 00401 00402 int get_internal_id(int spwf_id) { // checks also if `spwf_id` is (still) "valid" 00403 if(((unsigned int)spwf_id) < ((unsigned int)SPWFSA_SOCKET_COUNT)) { // valid `spwf_id` 00404 int internal_id = _internal_ids[spwf_id]; 00405 if((_socket_is_open(internal_id)) && (_ids[internal_id].spwf_id == spwf_id)) { 00406 return internal_id; 00407 } else { 00408 return SPWFSA_SOCKET_COUNT; 00409 } 00410 } else { // invalid `spwf_id` 00411 return SPWFSA_SOCKET_COUNT; 00412 } 00413 } 00414 00415 /* Called at initialization or after module hard fault */ 00416 void inner_constructor() { 00417 memset(_ids, 0, sizeof(_ids)); 00418 memset(_cbs, 0, sizeof(_cbs)); 00419 00420 for (int sock_cnt = 0; sock_cnt < SPWFSA_SOCKET_COUNT; sock_cnt++) { 00421 _ids[sock_cnt].internal_id = SPWFSA_SOCKET_COUNT; 00422 _ids[sock_cnt].spwf_id = SPWFSA_SOCKET_COUNT; 00423 _internal_ids[sock_cnt] = SPWFSA_SOCKET_COUNT; 00424 } 00425 00426 _spwf.attach(this, &SpwfSAInterface::event); 00427 00428 _connected_to_network = false; 00429 _isInitialized = false; 00430 } 00431 00432 private: 00433 friend class SPWFSAxx; 00434 friend class SPWFSA01; 00435 friend class SPWFSA04; 00436 }; 00437 00438 #define CHECK_NOT_CONNECTED_ERR() { \ 00439 if(!_connected_to_network) return NSAPI_ERROR_NO_CONNECTION; \ 00440 } \ 00441 00442 00443 #endif
Generated on Tue Jul 12 2022 16:59:38 by
1.7.2