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

Who changed what in which revision?

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