Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

Example Functionality

This example showcases the following device functionality:

  • On timer button increment, simulate Pelion LWM2M button resource change

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common
cd pelion-example-common

2. Download your developer certificate from pelion portal

3. Compile the program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

4. Copy the binary file pelion-example-common.bin to your mbed device.

Committer:
chuanga
Date:
Tue Mar 12 13:48:39 2019 +0800
Revision:
0:43ff9e3bc244
copying sources from github repository

Who changed what in which revision?

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