forked from particle-iot/device-os
/
spark_wiring_wifi.h
226 lines (174 loc) · 5.84 KB
/
spark_wiring_wifi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
******************************************************************************
* @file spark_wiring_wifi.h
* @author Satish Nair
* @version V1.0.0
* @date 7-Mar-2014
* @brief Header for spark_wiring_wifi.cpp module
******************************************************************************
Copyright (c) 2013-2015 Particle Industries, Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#ifndef __SPARK_WIRING_WIFI_H
#define __SPARK_WIRING_WIFI_H
#include "spark_wiring_platform.h"
#if Wiring_WiFi
#include "spark_wiring_network.h"
#include "wlan_hal.h"
#include "system_network.h"
#include "inet_hal.h"
#include <string.h>
class IPAddress;
namespace spark {
enum SecurityType {
UNSEC = WLAN_SEC_UNSEC,
WEP = WLAN_SEC_WEP,
WPA = WLAN_SEC_WPA,
WPA2 = WLAN_SEC_WPA2
};
class WiFiClass : public NetworkClass
{
void setIPAddressSource(IPAddressSource source) {
wlan_set_ipaddress_source(source, true, NULL);
}
public:
WiFiClass() {}
~WiFiClass() {}
operator network_handle_t() {
return 0;
}
WLanConfig* wifi_config() {
return (WLanConfig*)network_config(*this, 0, NULL);
}
/**
* Retrieves a 6-octet MAC address
* @param mac
* @return
*/
uint8_t* macAddress(uint8_t *mac) {
memcpy(mac, wifi_config()->nw.uaMacAddr, 6);
return mac;
}
IPAddress localIP() {
return IPAddress(wifi_config()->nw.aucIP);
}
IPAddress subnetMask() {
return IPAddress(wifi_config()->nw.aucSubnetMask);
}
IPAddress gatewayIP() {
return IPAddress(wifi_config()->nw.aucDefaultGateway);
}
IPAddress dnsServerIP() {
return IPAddress(wifi_config()->nw.aucDNSServer);
}
IPAddress dhcpServerIP() {
return IPAddress(wifi_config()->nw.aucDHCPServer);
}
const char *SSID() {
return (const char *) wifi_config()->uaSSID;
}
int8_t RSSI();
uint32_t ping(IPAddress remoteIP) {
return ping(remoteIP, 5);
}
uint32_t ping(IPAddress remoteIP, uint8_t nTries) {
return inet_ping(&remoteIP.raw(), *this, nTries, NULL);
}
void connect(unsigned flags=0) {
network_connect(*this, flags, 0, NULL);
}
void disconnect(void) {
network_disconnect(*this, 0, NULL);
}
bool connecting(void) {
return network_connecting(*this, 0, NULL);
}
bool ready(void) {
return network_ready(*this, 0, NULL);
}
void on(void) {
network_on(*this, 0, 0, NULL);
}
void off(void) {
network_off(*this, 0, 0, NULL);
}
void listen(bool begin=true) {
network_listen(*this, begin ? 0 : 1, NULL);
}
bool listening(void) {
return network_listening(*this, 0, NULL);
}
void setCredentials(const char *ssid) {
setCredentials(ssid, NULL, UNSEC);
}
void setCredentials(const char *ssid, const char *password) {
setCredentials(ssid, password, WPA2);
}
void setCredentials(const char *ssid, const char *password, unsigned long security, unsigned long cipher=WLAN_CIPHER_NOT_SET) {
setCredentials(ssid, strlen(ssid), password, strlen(password), security, cipher);
}
void setCredentials(const char *ssid, unsigned int ssidLen, const char *password,
unsigned int passwordLen, unsigned long security=WLAN_SEC_UNSEC, unsigned long cipher=WLAN_CIPHER_NOT_SET) {
WLanCredentials creds;
memset(&creds, 0, sizeof(creds));
creds.size = sizeof(creds);
creds.ssid = ssid;
creds.ssid_len = ssidLen;
creds.password = password;
creds.password_len = passwordLen;
creds.security = WLanSecurityType(security);
creds.cipher = WLanSecurityCipher(cipher);
network_set_credentials(*this, 0, &creds, NULL);
}
bool hasCredentials(void) {
return network_has_credentials(*this, 0, NULL);
}
bool clearCredentials(void) {
return network_clear_credentials(*this, 0, NULL, NULL);
}
int selectAntenna(WLanSelectAntenna_TypeDef antenna) {
return wlan_select_antenna(antenna);
}
IPAddress resolve(const char* name)
{
HAL_IPAddress ip;
return (inet_gethostbyname(name, strlen(name), &ip, *this, NULL)<0) ?
IPAddress(uint32_t(0)) : IPAddress(ip);
}
void setStaticIP(const IPAddress& host, const IPAddress& netmask,
const IPAddress& gateway, const IPAddress& dns)
{
wlan_set_ipaddress(host, netmask, gateway, dns, NULL, NULL);
}
void useStaticIP()
{
setIPAddressSource(STATIC_IP);
}
void useDynamicIP()
{
setIPAddressSource(DYNAMIC_IP);
}
int scan(wlan_scan_result_t callback, void* cookie=NULL) {
return wlan_scan(callback, cookie);
}
int scan(WiFiAccessPoint* results, size_t result_count);
template <typename T>
int scan(void (*handler)(WiFiAccessPoint* ap, T* instance), T* instance) {
return scan((wlan_scan_result_t)handler, (void*)instance);
}
int getCredentials(WiFiAccessPoint* results, size_t result_count);
};
extern WiFiClass WiFi;
} // namespace Spark
#endif // Wiring_WiFi
#endif