forked from chunlinhan/WiFiRM04
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFiRM04.cpp
241 lines (204 loc) · 5.18 KB
/
WiFiRM04.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "wifi_drv.h"
#include "WiFiRM04.h"
extern "C" {
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
#include "debug.h"
}
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
#if MAX_SOCK_NUM == 1
int16_t WiFiRM04Class::_state[MAX_SOCK_NUM] = { NA_STATE };
uint16_t WiFiRM04Class::_server_port[MAX_SOCK_NUM] = { 0 };
#else
int16_t WiFiRM04Class::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE };
uint16_t WiFiRM04Class::_server_port[MAX_SOCK_NUM] = { 0, 0 };
#endif
WiFiRM04Class::WiFiRM04Class()
{
// Driver initialization
init();
}
void WiFiRM04Class::init()
{
WiFiDrv::wifiDriverInit();
}
uint8_t WiFiRM04Class::getSocket()
{
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
{
if (WiFiRM04Class::_server_port[i] == 0)
{
return i;
}
}
return NO_SOCKET_AVAIL;
}
char* WiFiRM04Class::firmwareVersion()
{
return WiFiDrv::getFwVersion();
}
bool WiFiRM04Class::beginAsAp(char* ssid)
{
return WiFiDrv::wifiSetApMode(ssid, strlen(ssid));
}
int WiFiRM04Class::begin(char* ssid)
{
uint8_t status = WL_IDLE_STATUS;
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
{
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else
{
status = WL_CONNECT_FAILED;
}
return status;
}
int WiFiRM04Class::begin(char* ssid, uint8_t key_idx, const char *key)
{
uint8_t status = WL_IDLE_STATUS;
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
// set encryption key
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
{
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{
status = WL_CONNECT_FAILED;
}
return status;
}
int WiFiRM04Class::begin(char* ssid, const char *passphrase)
{
uint8_t status = WL_IDLE_STATUS;
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
// set passphrase
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
{
do
{
delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus();
}
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{
status = WL_CONNECT_FAILED;
}
return status;
}
void WiFiRM04Class::config(IPAddress local_ip)
{
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
}
void WiFiRM04Class::config(IPAddress local_ip, IPAddress dns_server)
{
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
}
void WiFiRM04Class::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
{
WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0);
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
}
void WiFiRM04Class::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet);
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
}
void WiFiRM04Class::setDNS(IPAddress dns_server1)
{
WiFiDrv::setDNS(1, (uint32_t)dns_server1, 0);
}
void WiFiRM04Class::setDNS(IPAddress dns_server1, IPAddress dns_server2)
{
WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2);
}
int WiFiRM04Class::disconnect()
{
return WiFiDrv::disconnect();
}
uint8_t* WiFiRM04Class::macAddress(uint8_t* mac)
{
uint8_t* _mac = WiFiDrv::getMacAddress();
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
return mac;
}
IPAddress WiFiRM04Class::localIP()
{
IPAddress ret;
WiFiDrv::getIpAddress(ret);
return ret;
}
IPAddress WiFiRM04Class::subnetMask()
{
IPAddress ret;
WiFiDrv::getSubnetMask(ret);
return ret;
}
IPAddress WiFiRM04Class::gatewayIP()
{
IPAddress ret;
WiFiDrv::getGatewayIP(ret);
return ret;
}
char* WiFiRM04Class::SSID()
{
return WiFiDrv::getCurrentSSID();
}
uint8_t* WiFiRM04Class::BSSID(uint8_t* bssid)
{
uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
return bssid;
}
int32_t WiFiRM04Class::RSSI()
{
return WiFiDrv::getCurrentRSSI();
}
uint8_t WiFiRM04Class::encryptionType()
{
return WiFiDrv::getCurrentEncryptionType();
}
int8_t WiFiRM04Class::scanNetworks()
{
uint8_t attempts = 10;
uint8_t numOfNetworks = 0;
if (WiFiDrv::startScanNetworks() == WL_FAILURE)
return WL_FAILURE;
do
{
delay(2000);
numOfNetworks = WiFiDrv::getScanNetworks();
}
while (( numOfNetworks == 0)&&(--attempts>0));
return numOfNetworks;
}
char* WiFiRM04Class::SSID(uint8_t networkItem)
{
return WiFiDrv::getSSIDNetoworks(networkItem);
}
int32_t WiFiRM04Class::RSSI(uint8_t networkItem)
{
return WiFiDrv::getRSSINetoworks(networkItem);
}
uint8_t WiFiRM04Class::encryptionType(uint8_t networkItem)
{
return WiFiDrv::getEncTypeNetowrks(networkItem);
}
uint8_t WiFiRM04Class::status()
{
return WiFiDrv::getConnectionStatus();
}
int WiFiRM04Class::hostByName(const char* aHostname, IPAddress& aResult)
{
return WiFiDrv::getHostByName(aHostname, aResult);
}
WiFiRM04Class WiFi;