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.
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
4340public:
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
0 commit comments