Skip to content

Commit 4fde794

Browse files
authored
Merge pull request #2627 from bulislaw/feature_wifi
Extend and rework WiFi interface
2 parents b826f0e + f9f1f44 commit 4fde794

File tree

3 files changed

+107
-24
lines changed

3 files changed

+107
-24
lines changed

features/net/network-socket/NetworkInterface.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class NetworkInterface {
3636
*/
3737
virtual const char *get_ip_address() = 0;
3838

39+
/** Get the local MAC address
40+
*
41+
* @return Null-terminated representation of the local MAC address
42+
*/
43+
virtual const char *get_mac_address() = 0;
44+
3945
protected:
4046
friend class Socket;
4147
friend class UDPSocket;
Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* WiFiInterface
2-
* Copyright (c) 2015 ARM Limited
2+
* Copyright (c) 2015 - 2016 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,22 +17,19 @@
1717
#ifndef WIFI_INTERFACE_H
1818
#define WIFI_INTERFACE_H
1919

20+
#include <string.h>
2021
#include "network-socket/NetworkInterface.h"
2122

23+
typedef struct wifi_ap {
24+
char ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
25+
uint8_t bssid[6];
26+
nsapi_security_t security;
27+
int8_t rssi;
28+
uint8_t channel;
29+
} wifi_ap_t;
2230

23-
/** Enum of WiFi encryption types
24-
*
25-
* The security type specifies a particular security to use when
26-
* connected to a WiFi network
27-
*
28-
* @enum nsapi_protocol_t
29-
*/
30-
enum nsapi_security_t {
31-
NSAPI_SECURITY_NONE = 0, /*!< open access point */
32-
NSAPI_SECURITY_WEP, /*!< phrase conforms to WEP */
33-
NSAPI_SECURITY_WPA, /*!< phrase conforms to WPA */
34-
NSAPI_SECURITY_WPA2, /*!< phrase conforms to WPA2 */
35-
};
31+
typedef void (*wifi_ap_scan_cb_t)(wifi_ap_t *ap, void *data);
32+
typedef void (*wifi_connect_cb_t)(nsapi_error_t res, void *data);
3633

3734
/** WiFiInterface class
3835
*
@@ -43,28 +40,81 @@ class WiFiInterface: public NetworkInterface
4340
public:
4441
/** Start the interface
4542
*
46-
* Attempts to connect to a WiFi network. If passphrase is invalid,
47-
* NSAPI_ERROR_AUTH_ERROR is returned.
43+
* Attempts to connect to a WiFi network.
44+
*
45+
* @param ssid Name of the network to connect to
46+
* @param pass Security passphrase to connect to the network
47+
* @param security Type of encryption for connection
48+
* @param timeout Timeout in milliseconds; 0 for no timeout
49+
* @return 0 on success, or error code on failure
50+
*/
51+
virtual nsapi_error_t connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
52+
unsigned timeout = 0) = 0;
53+
54+
/** Start the interface
55+
*
56+
* Attempts to connect to a WiFi network asynchronously, the call will return straight away. If the @a cb was NULL
57+
* you'll need to query @a get_state until it's in NSAPI_IF_STATE_CONNECTED state, otherwise the @a cb will be
58+
* called with connection results.
59+
*
60+
* Note: @a ssid and @a pass must be kept until the connection is made, that is the callback has been called or the
61+
* state changed to @a NSAPI_IF_STATE_CONNECTED as they are passed by value.
4862
*
4963
* @param ssid Name of the network to connect to
5064
* @param pass Security passphrase to connect to the network
5165
* @param security Type of encryption for connection
52-
* @return 0 on success, negative error code on failure
66+
* @param cb Function to be called when the connect finishes
67+
* @param data Arbitrary user data to pass to @a cb function
5368
*/
54-
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE) = 0;
69+
virtual void connect_async(const char *ssid, const char *pass,nsapi_security_t security = NSAPI_SECURITY_NONE,
70+
wifi_connect_cb_t cb = NULL, void *data = NULL) = 0;
5571

5672
/** Stop the interface
5773
*
58-
* @return 0 on success, negative error code on failure
74+
* @return 0 on success, or error code on failure
5975
*/
60-
virtual int disconnect() = 0;
76+
virtual nsapi_error_t disconnect() = 0;
6177

6278
/** Get the local MAC address
6379
*
6480
* @return Null-terminated representation of the local MAC address
6581
*/
6682
virtual const char *get_mac_address() = 0;
67-
};
6883

84+
/** Get the current WiFi interface state
85+
*
86+
* @returns Interface state enum
87+
*/
88+
virtual nsapi_if_state_t get_state() = 0;
89+
90+
/** Gets the current radio signal strength for active connection
91+
*
92+
* @return Connection strength in dBm (negative value), or 0 if measurement impossible
93+
*/
94+
virtual int8_t get_rssi() = 0;
95+
96+
/** Scan for available networks
97+
*
98+
* This function will block.
99+
*
100+
* @param ap Pointer to allocated array to store discovered AP
101+
* @param count Size of allocated @a res array, or 0 to only count available AP
102+
* @param timeout Timeout in milliseconds; 0 for no timeout
103+
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
104+
* see @a nsapi_error
105+
*/
106+
virtual int scan(wifi_ap_t *res, unsigned count, unsigned timeout = 0) = 0;
107+
108+
/** Scan for available networks
109+
*
110+
* This function won't block, it'll return immediately and call provided callback for each discovered network with
111+
* AP data and user @a data. After the last discovered network @a cb will be called with NULL as @a ap, so it
112+
* will be always called at least once.
113+
*
114+
* @param cb Function to be called for every discovered network
115+
* @param data A user handle that will be passed to @a cb along with the AP data
116+
*/
117+
virtual void scan_async(wifi_ap_scan_cb_t cb, void *data = NULL) = 0;
118+
};
69119

70120
#endif

features/net/network-socket/nsapi_types.h

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ extern "C" {
2424
#endif
2525

2626

27-
/** Enum of standardized error codes
27+
/** Enum of standardized error codes
2828
*
2929
* Valid error codes have negative values and may
3030
* be returned by any network operation.
3131
*
3232
* @enum nsapi_error_t
3333
*/
3434
typedef enum nsapi_error {
35+
NSAPI_ERROR_OK = 0, /*!< no error */
3536
NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
3637
NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported functionality */
3738
NSAPI_ERROR_PARAMETER = -3003, /*!< invalid configuration */
@@ -41,10 +42,36 @@ typedef enum nsapi_error {
4142
NSAPI_ERROR_NO_MEMORY = -3007, /*!< memory resource not available */
4243
NSAPI_ERROR_DNS_FAILURE = -3008, /*!< DNS failed to complete successfully */
4344
NSAPI_ERROR_DHCP_FAILURE = -3009, /*!< DHCP failed to complete successfully */
44-
NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point faield */
45-
NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network procesor */
45+
NSAPI_ERROR_AUTH_FAILURE = -3010, /*!< connection to access point failed */
46+
NSAPI_ERROR_DEVICE_ERROR = -3011, /*!< failure interfacing with the network processor */
47+
NSAPI_ERROR_TIMEOUT = -3012, /*!< operation timed out */
48+
NSAPI_ERROR_BAD_SSID = -3013, /*!< ssid not found */
4649
} nsapi_error_t;
4750

51+
/** Enum of encryption types
52+
*
53+
* The security type specifies a particular security to use when
54+
* connected to a WiFi network
55+
*/
56+
typedef enum nsapi_security {
57+
NSAPI_SECURITY_NONE = 0x0, /*!< open access point */
58+
NSAPI_SECURITY_WEP = 0x1, /*!< phrase conforms to WEP */
59+
NSAPI_SECURITY_WPA = 0x2, /*!< phrase conforms to WPA */
60+
NSAPI_SECURITY_WPA2 = 0x3, /*!< phrase conforms to WPA2 */
61+
NSAPI_SECURITY_WPA_WPA2 = 0x4, /*!< phrase conforms to WPA/WPA2 */
62+
NSAPI_SECURITY_UNSSUPPORTED = 0xFF, /*!< unknown/unsupported security in scan results */
63+
} nsapi_security_t;
64+
65+
/** Enum of interface states
66+
*
67+
* List of all possible states a WiFi network interface can be in
68+
*/
69+
typedef enum nsapi_if_state {
70+
NSAPI_IF_STATE_NOT_CONNECTED = 0x0,
71+
NSAPI_IF_STATE_CONNECTING = 0x01,
72+
NSAPI_IF_STATE_CONNECTED = 0x02,
73+
NSAPI_IF_STATE_ERROR = 0xFF
74+
} nsapi_if_state_t;
4875

4976
/** Maximum size of IP address representation
5077
*/

0 commit comments

Comments
 (0)