test

Dependents:   mbed-os-example-blinky-1stDevDemo

Committer:
karen801
Date:
Wed May 23 14:37:10 2018 +0000
Revision:
0:79ce2b184a43
Initial commit

Who changed what in which revision?

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