1
1
/* WiFiInterface
2
- * Copyright (c) 2015 ARM Limited
2
+ * Copyright (c) 2015 - 2016 ARM Limited
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
#ifndef WIFI_INTERFACE_H
18
18
#define WIFI_INTERFACE_H
19
19
20
+ #include < string.h>
20
21
#include " network-socket/NetworkInterface.h"
21
22
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 ;
22
30
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);
36
33
37
34
/* * WiFiInterface class
38
35
*
@@ -43,28 +40,81 @@ class WiFiInterface: public NetworkInterface
43
40
public:
44
41
/* * Start the interface
45
42
*
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.
48
62
*
49
63
* @param ssid Name of the network to connect to
50
64
* @param pass Security passphrase to connect to the network
51
65
* @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
53
68
*/
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;
55
71
56
72
/* * Stop the interface
57
73
*
58
- * @return 0 on success, negative error code on failure
74
+ * @return 0 on success, or error code on failure
59
75
*/
60
- virtual int disconnect () = 0;
76
+ virtual nsapi_error_t disconnect () = 0;
61
77
62
78
/* * Get the local MAC address
63
79
*
64
80
* @return Null-terminated representation of the local MAC address
65
81
*/
66
82
virtual const char *get_mac_address () = 0;
67
- };
68
83
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
+ };
69
119
70
120
#endif
0 commit comments