wifi test

Dependencies:   X_NUCLEO_IKS01A2 mbed-http

Committer:
JMF
Date:
Wed Sep 05 14:28:24 2018 +0000
Revision:
0:24d3eb812fd4
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:24d3eb812fd4 1 /* ISM43362 implementation of NetworkInterfaceAPI
JMF 0:24d3eb812fd4 2 * Copyright (c) STMicroelectronics 2017
JMF 0:24d3eb812fd4 3 *
JMF 0:24d3eb812fd4 4 * Licensed under the Apache License, Version 2.0 (the "License");
JMF 0:24d3eb812fd4 5 * you may not use this file except in compliance with the License.
JMF 0:24d3eb812fd4 6 * You may obtain a copy of the License at
JMF 0:24d3eb812fd4 7 *
JMF 0:24d3eb812fd4 8 * http://www.apache.org/licenses/LICENSE-2.0
JMF 0:24d3eb812fd4 9 *
JMF 0:24d3eb812fd4 10 * Unless required by applicable law or agreed to in writing, software
JMF 0:24d3eb812fd4 11 * distributed under the License is distributed on an "AS IS" BASIS,
JMF 0:24d3eb812fd4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JMF 0:24d3eb812fd4 13 * See the License for the specific language governing permissions and
JMF 0:24d3eb812fd4 14 * limitations under the License.
JMF 0:24d3eb812fd4 15 */
JMF 0:24d3eb812fd4 16
JMF 0:24d3eb812fd4 17 #ifndef ISM43362_INTERFACE_H
JMF 0:24d3eb812fd4 18 #define ISM43362_INTERFACE_H
JMF 0:24d3eb812fd4 19
JMF 0:24d3eb812fd4 20 #include "mbed.h"
JMF 0:24d3eb812fd4 21 #include "ISM43362.h"
JMF 0:24d3eb812fd4 22
JMF 0:24d3eb812fd4 23
JMF 0:24d3eb812fd4 24 #define ISM43362_SOCKET_COUNT 4
JMF 0:24d3eb812fd4 25
JMF 0:24d3eb812fd4 26 /** ISM43362Interface class
JMF 0:24d3eb812fd4 27 * Implementation of the NetworkStack for the ISM43362
JMF 0:24d3eb812fd4 28 */
JMF 0:24d3eb812fd4 29 class ISM43362Interface : public NetworkStack, public WiFiInterface
JMF 0:24d3eb812fd4 30 {
JMF 0:24d3eb812fd4 31 public:
JMF 0:24d3eb812fd4 32 /** ISM43362Interface lifetime
JMF 0:24d3eb812fd4 33 * @param debug Enable debugging
JMF 0:24d3eb812fd4 34 */
JMF 0:24d3eb812fd4 35 ISM43362Interface(bool debug = MBED_CONF_ISM43362_WIFI_DEBUG);
JMF 0:24d3eb812fd4 36
JMF 0:24d3eb812fd4 37 /** Start the interface
JMF 0:24d3eb812fd4 38 *
JMF 0:24d3eb812fd4 39 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
JMF 0:24d3eb812fd4 40 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
JMF 0:24d3eb812fd4 41 *
JMF 0:24d3eb812fd4 42 * @return 0 on success, negative error code on failure
JMF 0:24d3eb812fd4 43 */
JMF 0:24d3eb812fd4 44 virtual int connect();
JMF 0:24d3eb812fd4 45
JMF 0:24d3eb812fd4 46 /** Start the interface
JMF 0:24d3eb812fd4 47 *
JMF 0:24d3eb812fd4 48 * Attempts to connect to a WiFi network.
JMF 0:24d3eb812fd4 49 *
JMF 0:24d3eb812fd4 50 * @param ssid Name of the network to connect to
JMF 0:24d3eb812fd4 51 * @param pass Security passphrase to connect to the network
JMF 0:24d3eb812fd4 52 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
JMF 0:24d3eb812fd4 53 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
JMF 0:24d3eb812fd4 54 * @return 0 on success, or error code on failure
JMF 0:24d3eb812fd4 55 */
JMF 0:24d3eb812fd4 56 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
JMF 0:24d3eb812fd4 57 uint8_t channel = 0);
JMF 0:24d3eb812fd4 58
JMF 0:24d3eb812fd4 59 /** Translates a hostname to an IP address with specific version
JMF 0:24d3eb812fd4 60 *
JMF 0:24d3eb812fd4 61 * The hostname may be either a domain name or an IP address. If the
JMF 0:24d3eb812fd4 62 * hostname is an IP address, no network transactions will be performed.
JMF 0:24d3eb812fd4 63 *
JMF 0:24d3eb812fd4 64 *
JMF 0:24d3eb812fd4 65 * @param host Hostname to resolve
JMF 0:24d3eb812fd4 66 * @param address Destination for the host SocketAddress
JMF 0:24d3eb812fd4 67 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
JMF 0:24d3eb812fd4 68 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
JMF 0:24d3eb812fd4 69 * @return 0 on success, negative error code on failure
JMF 0:24d3eb812fd4 70 */
JMF 0:24d3eb812fd4 71 virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
JMF 0:24d3eb812fd4 72
JMF 0:24d3eb812fd4 73 /** Set the WiFi network credentials
JMF 0:24d3eb812fd4 74 *
JMF 0:24d3eb812fd4 75 * @param ssid Name of the network to connect to
JMF 0:24d3eb812fd4 76 * @param pass Security passphrase to connect to the network
JMF 0:24d3eb812fd4 77 * @param security Type of encryption for connection
JMF 0:24d3eb812fd4 78 * (defaults to NSAPI_SECURITY_NONE)
JMF 0:24d3eb812fd4 79 * @return 0 on success, or error code on failure
JMF 0:24d3eb812fd4 80 */
JMF 0:24d3eb812fd4 81 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
JMF 0:24d3eb812fd4 82
JMF 0:24d3eb812fd4 83 /** Set the WiFi network channel - NOT SUPPORTED
JMF 0:24d3eb812fd4 84 *
JMF 0:24d3eb812fd4 85 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
JMF 0:24d3eb812fd4 86 *
JMF 0:24d3eb812fd4 87 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
JMF 0:24d3eb812fd4 88 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
JMF 0:24d3eb812fd4 89 */
JMF 0:24d3eb812fd4 90 virtual int set_channel(uint8_t channel);
JMF 0:24d3eb812fd4 91
JMF 0:24d3eb812fd4 92 /** Stop the interface
JMF 0:24d3eb812fd4 93 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 94 */
JMF 0:24d3eb812fd4 95 virtual int disconnect();
JMF 0:24d3eb812fd4 96
JMF 0:24d3eb812fd4 97 /** Get the internally stored IP address
JMF 0:24d3eb812fd4 98 * @return IP address of the interface or null if not yet connected
JMF 0:24d3eb812fd4 99 */
JMF 0:24d3eb812fd4 100 virtual const char *get_ip_address();
JMF 0:24d3eb812fd4 101
JMF 0:24d3eb812fd4 102 /** Get the internally stored MAC address
JMF 0:24d3eb812fd4 103 * @return MAC address of the interface
JMF 0:24d3eb812fd4 104 */
JMF 0:24d3eb812fd4 105 virtual const char *get_mac_address();
JMF 0:24d3eb812fd4 106
JMF 0:24d3eb812fd4 107 /** Get the local gateway
JMF 0:24d3eb812fd4 108 *
JMF 0:24d3eb812fd4 109 * @return Null-terminated representation of the local gateway
JMF 0:24d3eb812fd4 110 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 111 */
JMF 0:24d3eb812fd4 112 virtual const char *get_gateway();
JMF 0:24d3eb812fd4 113
JMF 0:24d3eb812fd4 114 /** Get the local network mask
JMF 0:24d3eb812fd4 115 *
JMF 0:24d3eb812fd4 116 * @return Null-terminated representation of the local network mask
JMF 0:24d3eb812fd4 117 * or null if no network mask has been recieved
JMF 0:24d3eb812fd4 118 */
JMF 0:24d3eb812fd4 119 virtual const char *get_netmask();
JMF 0:24d3eb812fd4 120
JMF 0:24d3eb812fd4 121 /** Gets the current radio signal strength for active connection
JMF 0:24d3eb812fd4 122 *
JMF 0:24d3eb812fd4 123 * @return Connection strength in dBm (negative value)
JMF 0:24d3eb812fd4 124 */
JMF 0:24d3eb812fd4 125 virtual int8_t get_rssi();
JMF 0:24d3eb812fd4 126
JMF 0:24d3eb812fd4 127 /** Scan for available networks
JMF 0:24d3eb812fd4 128 *
JMF 0:24d3eb812fd4 129 * This function will block.
JMF 0:24d3eb812fd4 130 *
JMF 0:24d3eb812fd4 131 * @param ap Pointer to allocated array to store discovered AP
JMF 0:24d3eb812fd4 132 * @param count Size of allocated @a res array, or 0 to only count available AP
JMF 0:24d3eb812fd4 133 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
JMF 0:24d3eb812fd4 134 * see @a nsapi_error
JMF 0:24d3eb812fd4 135 */
JMF 0:24d3eb812fd4 136 virtual int scan(WiFiAccessPoint *res, unsigned count);
JMF 0:24d3eb812fd4 137
JMF 0:24d3eb812fd4 138 /** Translates a hostname to an IP address with specific version
JMF 0:24d3eb812fd4 139 *
JMF 0:24d3eb812fd4 140 * The hostname may be either a domain name or an IP address. If the
JMF 0:24d3eb812fd4 141 * hostname is an IP address, no network transactions will be performed.
JMF 0:24d3eb812fd4 142 *
JMF 0:24d3eb812fd4 143 * If no stack-specific DNS resolution is provided, the hostname
JMF 0:24d3eb812fd4 144 * will be resolve using a UDP socket on the stack.
JMF 0:24d3eb812fd4 145 *
JMF 0:24d3eb812fd4 146 * @param address Destination for the host SocketAddress
JMF 0:24d3eb812fd4 147 * @param host Hostname to resolve
JMF 0:24d3eb812fd4 148 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
JMF 0:24d3eb812fd4 149 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
JMF 0:24d3eb812fd4 150 * @return 0 on success, negative error code on failure
JMF 0:24d3eb812fd4 151 */
JMF 0:24d3eb812fd4 152 using NetworkInterface::gethostbyname;
JMF 0:24d3eb812fd4 153
JMF 0:24d3eb812fd4 154 /** Add a domain name server to list of servers to query
JMF 0:24d3eb812fd4 155 *
JMF 0:24d3eb812fd4 156 * @param addr Destination for the host address
JMF 0:24d3eb812fd4 157 * @return 0 on success, negative error code on failure
JMF 0:24d3eb812fd4 158 */
JMF 0:24d3eb812fd4 159 using NetworkInterface::add_dns_server;
JMF 0:24d3eb812fd4 160
JMF 0:24d3eb812fd4 161 protected:
JMF 0:24d3eb812fd4 162 /** Open a socket
JMF 0:24d3eb812fd4 163 * @param handle Handle in which to store new socket
JMF 0:24d3eb812fd4 164 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
JMF 0:24d3eb812fd4 165 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 166 */
JMF 0:24d3eb812fd4 167 virtual int socket_open(void **handle, nsapi_protocol_t proto);
JMF 0:24d3eb812fd4 168
JMF 0:24d3eb812fd4 169 /** Close the socket
JMF 0:24d3eb812fd4 170 * @param handle Socket handle
JMF 0:24d3eb812fd4 171 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 172 * @note On failure, any memory associated with the socket must still
JMF 0:24d3eb812fd4 173 * be cleaned up
JMF 0:24d3eb812fd4 174 */
JMF 0:24d3eb812fd4 175 virtual int socket_close(void *handle);
JMF 0:24d3eb812fd4 176
JMF 0:24d3eb812fd4 177 /** Bind a server socket to a specific port
JMF 0:24d3eb812fd4 178 * @param handle Socket handle
JMF 0:24d3eb812fd4 179 * @param address Local address to listen for incoming connections on
JMF 0:24d3eb812fd4 180 * @return 0 on success, negative on failure.
JMF 0:24d3eb812fd4 181 */
JMF 0:24d3eb812fd4 182 virtual int socket_bind(void *handle, const SocketAddress &address);
JMF 0:24d3eb812fd4 183
JMF 0:24d3eb812fd4 184 /** Start listening for incoming connections
JMF 0:24d3eb812fd4 185 * @param handle Socket handle
JMF 0:24d3eb812fd4 186 * @param backlog Number of pending connections that can be queued up at any
JMF 0:24d3eb812fd4 187 * one time [Default: 1]
JMF 0:24d3eb812fd4 188 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 189 */
JMF 0:24d3eb812fd4 190 virtual int socket_listen(void *handle, int backlog);
JMF 0:24d3eb812fd4 191
JMF 0:24d3eb812fd4 192 /** Connects this TCP socket to the server
JMF 0:24d3eb812fd4 193 * @param handle Socket handle
JMF 0:24d3eb812fd4 194 * @param address SocketAddress to connect to
JMF 0:24d3eb812fd4 195 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 196 */
JMF 0:24d3eb812fd4 197 virtual int socket_connect(void *handle, const SocketAddress &address);
JMF 0:24d3eb812fd4 198
JMF 0:24d3eb812fd4 199 /** Accept a new connection.
JMF 0:24d3eb812fd4 200 * @param handle Handle in which to store new socket
JMF 0:24d3eb812fd4 201 * @param server Socket handle to server to accept from
JMF 0:24d3eb812fd4 202 * @return 0 on success, negative on failure
JMF 0:24d3eb812fd4 203 * @note This call is not-blocking, if this call would block, must
JMF 0:24d3eb812fd4 204 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 0:24d3eb812fd4 205 */
JMF 0:24d3eb812fd4 206 virtual int socket_accept(void *handle, void **socket, SocketAddress *address);
JMF 0:24d3eb812fd4 207
JMF 0:24d3eb812fd4 208 /** Send data to the remote host
JMF 0:24d3eb812fd4 209 * @param handle Socket handle
JMF 0:24d3eb812fd4 210 * @param data The buffer to send to the host
JMF 0:24d3eb812fd4 211 * @param size The length of the buffer to send
JMF 0:24d3eb812fd4 212 * @return Number of written bytes on success, negative on failure
JMF 0:24d3eb812fd4 213 * @note This call is not-blocking, if this call would block, must
JMF 0:24d3eb812fd4 214 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 0:24d3eb812fd4 215 */
JMF 0:24d3eb812fd4 216 virtual int socket_send(void *handle, const void *data, unsigned size);
JMF 0:24d3eb812fd4 217
JMF 0:24d3eb812fd4 218 /** Receive data from the remote host
JMF 0:24d3eb812fd4 219 * @param handle Socket handle
JMF 0:24d3eb812fd4 220 * @param data The buffer in which to store the data received from the host
JMF 0:24d3eb812fd4 221 * @param size The maximum length of the buffer
JMF 0:24d3eb812fd4 222 * @return Number of received bytes on success, negative on failure
JMF 0:24d3eb812fd4 223 * @note This call is not-blocking, if this call would block, must
JMF 0:24d3eb812fd4 224 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 0:24d3eb812fd4 225 */
JMF 0:24d3eb812fd4 226 virtual int socket_recv(void *handle, void *data, unsigned size);
JMF 0:24d3eb812fd4 227
JMF 0:24d3eb812fd4 228 /** Send a packet to a remote endpoint
JMF 0:24d3eb812fd4 229 * @param handle Socket handle
JMF 0:24d3eb812fd4 230 * @param address The remote SocketAddress
JMF 0:24d3eb812fd4 231 * @param data The packet to be sent
JMF 0:24d3eb812fd4 232 * @param size The length of the packet to be sent
JMF 0:24d3eb812fd4 233 * @return The number of written bytes on success, negative on failure
JMF 0:24d3eb812fd4 234 * @note This call is not-blocking, if this call would block, must
JMF 0:24d3eb812fd4 235 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 0:24d3eb812fd4 236 */
JMF 0:24d3eb812fd4 237 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
JMF 0:24d3eb812fd4 238
JMF 0:24d3eb812fd4 239 /** Receive a packet from a remote endpoint
JMF 0:24d3eb812fd4 240 * @param handle Socket handle
JMF 0:24d3eb812fd4 241 * @param address Destination for the remote SocketAddress or null
JMF 0:24d3eb812fd4 242 * @param buffer The buffer for storing the incoming packet data
JMF 0:24d3eb812fd4 243 * If a packet is too long to fit in the supplied buffer,
JMF 0:24d3eb812fd4 244 * excess bytes are discarded
JMF 0:24d3eb812fd4 245 * @param size The length of the buffer
JMF 0:24d3eb812fd4 246 * @return The number of received bytes on success, negative on failure
JMF 0:24d3eb812fd4 247 * @note This call is not-blocking, if this call would block, must
JMF 0:24d3eb812fd4 248 * immediately return NSAPI_ERROR_WOULD_WAIT
JMF 0:24d3eb812fd4 249 */
JMF 0:24d3eb812fd4 250 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
JMF 0:24d3eb812fd4 251
JMF 0:24d3eb812fd4 252 /** Register a callback on state change of the socket
JMF 0:24d3eb812fd4 253 * @param handle Socket handle
JMF 0:24d3eb812fd4 254 * @param callback Function to call on state change
JMF 0:24d3eb812fd4 255 * @param data Argument to pass to callback
JMF 0:24d3eb812fd4 256 * @note Callback may be called in an interrupt context.
JMF 0:24d3eb812fd4 257 */
JMF 0:24d3eb812fd4 258 virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
JMF 0:24d3eb812fd4 259
JMF 0:24d3eb812fd4 260 /** Provide access to the NetworkStack object
JMF 0:24d3eb812fd4 261 *
JMF 0:24d3eb812fd4 262 * @return The underlying NetworkStack object
JMF 0:24d3eb812fd4 263 */
JMF 0:24d3eb812fd4 264 virtual NetworkStack *get_stack()
JMF 0:24d3eb812fd4 265 {
JMF 0:24d3eb812fd4 266 return this;
JMF 0:24d3eb812fd4 267 }
JMF 0:24d3eb812fd4 268
JMF 0:24d3eb812fd4 269 private:
JMF 0:24d3eb812fd4 270 ISM43362 _ism;
JMF 0:24d3eb812fd4 271 bool _ids[ISM43362_SOCKET_COUNT];
JMF 0:24d3eb812fd4 272 uint32_t _socket_obj[ISM43362_SOCKET_COUNT]; // store addresses of socket handles
JMF 0:24d3eb812fd4 273 Mutex _mutex;
JMF 0:24d3eb812fd4 274 Thread thread_read_socket;
JMF 0:24d3eb812fd4 275 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
JMF 0:24d3eb812fd4 276 ism_security_t ap_sec;
JMF 0:24d3eb812fd4 277 uint8_t ap_ch;
JMF 0:24d3eb812fd4 278 char ap_pass[64]; /* The longest allowed passphrase */
JMF 0:24d3eb812fd4 279
JMF 0:24d3eb812fd4 280 bool _ism_debug;
JMF 0:24d3eb812fd4 281 uint32_t _FwVersion;
JMF 0:24d3eb812fd4 282
JMF 0:24d3eb812fd4 283 void event();
JMF 0:24d3eb812fd4 284 struct {
JMF 0:24d3eb812fd4 285 void (*callback)(void *);
JMF 0:24d3eb812fd4 286 void *data;
JMF 0:24d3eb812fd4 287 } _cbs[ISM43362_SOCKET_COUNT];
JMF 0:24d3eb812fd4 288
JMF 0:24d3eb812fd4 289 /** Function called by the socket read thread to check if data is available on the wifi module
JMF 0:24d3eb812fd4 290 *
JMF 0:24d3eb812fd4 291 */
JMF 0:24d3eb812fd4 292 virtual void socket_check_read();
JMF 0:24d3eb812fd4 293 int socket_send_nolock(void *handle, const void *data, unsigned size);
JMF 0:24d3eb812fd4 294 int socket_connect_nolock(void *handle, const SocketAddress &addr);
JMF 0:24d3eb812fd4 295
JMF 0:24d3eb812fd4 296 };
JMF 0:24d3eb812fd4 297
JMF 0:24d3eb812fd4 298 #endif