1
1
#include " WiFi.h"
2
2
3
- bool arduino::WiFiClass::isVisible (char * ssid) {
3
+ bool arduino::WiFiClass::isVisible (const char * ssid) {
4
4
for (int i=0 ; i<10 ; i++) {
5
5
if (strncmp (ap_list[i].get_ssid (), ssid, 32 ) == 0 ) {
6
6
connected_ap = i;
@@ -10,7 +10,12 @@ bool arduino::WiFiClass::isVisible(char* ssid) {
10
10
return false ;
11
11
}
12
12
13
- int arduino::WiFiClass::begin (char * ssid, const char *passphrase) {
13
+ arduino::IPAddress arduino::WiFiClass::ipAddressFromSocketAddress (SocketAddress socketAddress) {
14
+ nsapi_addr_t address = socketAddress.get_addr ();
15
+ return IPAddress (address.bytes [0 ], address.bytes [1 ], address.bytes [2 ], address.bytes [3 ]);
16
+ }
17
+
18
+ int arduino::WiFiClass::begin (const char * ssid, const char *passphrase) {
14
19
if (_ssid) free (_ssid);
15
20
16
21
_ssid = (char *)malloc (33 );
@@ -19,51 +24,109 @@ int arduino::WiFiClass::begin(char* ssid, const char *passphrase) {
19
24
return WL_CONNECT_FAILED;
20
25
}
21
26
22
- if (wifi_if == NULL ) {
23
- wifi_if = (WiFiInterface*)cb ();
27
+ if (wifi_if == nullptr ) {
28
+ // Q: What is the callback for?
29
+ _initializerCallback ();
30
+ if (wifi_if == nullptr ) return WL_CONNECT_FAILED;
24
31
}
25
32
26
- // too long? break it off
27
- if (strlen (ssid) > 32 ) ssid[32 ] = 0 ;
28
33
memcpy (_ssid, ssid, 33 );
34
+ // too long? break it off
35
+ if (strlen (ssid) > 32 ) _ssid[32 ] = 0 ;
29
36
30
37
scanNetworks ();
31
38
// use scan result to populate security field
32
- if (!isVisible (ssid)) {
33
- return WL_CONNECT_FAILED;
39
+ if (!isVisible (_ssid)) {
40
+ _currentNetworkStatus = WL_CONNECT_FAILED;
41
+ return _currentNetworkStatus;
34
42
}
35
43
36
- nsapi_error_t ret = wifi_if->connect (ssid , passphrase, ap_list[connected_ap].get_security ());
44
+ nsapi_error_t ret = wifi_if->connect (_ssid , passphrase, ap_list[connected_ap].get_security ());
37
45
38
- return ret == NSAPI_ERROR_OK ? WL_CONNECTED : WL_CONNECT_FAILED;
46
+ _currentNetworkStatus = ret == NSAPI_ERROR_OK ? WL_CONNECTED : WL_CONNECT_FAILED;
47
+ return _currentNetworkStatus;
39
48
}
40
49
41
50
int arduino::WiFiClass::beginAP (const char * ssid, const char *passphrase, uint8_t channel) {
42
51
43
52
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
44
- softap = WhdSoftAPInterface::get_default_instance ();
53
+ _softAP = WhdSoftAPInterface::get_default_instance ();
45
54
#endif
46
55
47
- if (softap == NULL ) {
56
+ if (_softAP == NULL ) {
48
57
return WL_CONNECT_FAILED;
49
58
}
50
59
51
- // Set ap ssid, password and channel
52
- SocketAddress ip (" 192.168.3.1" );
53
- SocketAddress gw (" 192.168.3.1" );
54
- SocketAddress netmask (" 255.255.255.0" );
55
- ((WhdSoftAPInterface*)softap)->set_network (ip, gw, netmask);
56
- nsapi_error_t ret = ((WhdSoftAPInterface*)softap)->start (ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */ , NULL , true /* cohexistance */ );
60
+ ensureDefaultAPNetworkConfiguration ();
61
+
62
+ // Set ap ssid, password and channel
63
+ static_cast <WhdSoftAPInterface*>(_softAP)->set_network (_ip, _netmask, _gateway);
64
+ nsapi_error_t ret = static_cast <WhdSoftAPInterface*>(_softAP)->start (ssid, passphrase, NSAPI_SECURITY_WPA2, channel, true /* dhcp server */ , NULL , true /* cohexistance */ );
57
65
58
66
return ret == NSAPI_ERROR_OK ? WL_AP_LISTENING : WL_CONNECT_FAILED;
59
67
}
60
68
69
+ void arduino::WiFiClass::ensureDefaultAPNetworkConfiguration () {
70
+ if (_ip == nullptr ){
71
+ _ip = SocketAddress (DEFAULT_IP_ADDRESS);
72
+ }
73
+ if (_gateway == nullptr ){
74
+ _gateway = _ip;
75
+ }
76
+ if (_netmask == nullptr ){
77
+ _netmask = SocketAddress (DEFAULT_NETMASK);
78
+ }
79
+ }
80
+
61
81
void arduino::WiFiClass::end () {
62
- if (softap != NULL ) {
63
- ((WhdSoftAPInterface*)softap)->stop ();
82
+ disconnect ();
83
+ }
84
+
85
+ int arduino::WiFiClass::disconnect () {
86
+ if (_softAP != nullptr ) {
87
+ return static_cast <WhdSoftAPInterface*>(_softAP)->stop ();
88
+ } else {
89
+ return wifi_if->disconnect ();
64
90
}
65
91
}
66
92
93
+ void arduino::WiFiClass::config (arduino::IPAddress local_ip){
94
+ nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0 ], local_ip[1 ], local_ip[2 ], local_ip[3 ]}};
95
+ _ip = SocketAddress (convertedIP);
96
+ }
97
+
98
+ void arduino::WiFiClass::config (const char *local_ip){
99
+ _ip = SocketAddress (local_ip);
100
+ }
101
+
102
+ void arduino::WiFiClass::config (IPAddress local_ip, IPAddress dns_server){
103
+ config (local_ip);
104
+ setDNS (dns_server);
105
+ }
106
+
107
+ void arduino::WiFiClass::config (IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
108
+ config (local_ip, dns_server);
109
+ nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0 ], gateway[1 ], gateway[2 ], gateway[3 ]}};
110
+ _gateway = SocketAddress (convertedGatewayIP);
111
+ }
112
+
113
+ void arduino::WiFiClass::config (IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
114
+ config (local_ip, dns_server, gateway);
115
+ nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0 ], subnet[1 ], subnet[2 ], subnet[3 ]}};
116
+ _netmask = SocketAddress (convertedSubnetMask);
117
+ }
118
+
119
+ void arduino::WiFiClass::setDNS (IPAddress dns_server1){
120
+ nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0 ], dns_server1[1 ], dns_server1[2 ], dns_server1[3 ]}};
121
+ _dnsServer1 = SocketAddress (convertedDNSServer);
122
+ }
123
+
124
+ void arduino::WiFiClass::setDNS (IPAddress dns_server1, IPAddress dns_server2){
125
+ setDNS (dns_server1);
126
+ nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0 ], dns_server2[1 ], dns_server2[2 ], dns_server2[3 ]}};
127
+ _dnsServer2 = SocketAddress (convertedDNSServer2);
128
+ }
129
+
67
130
char * arduino::WiFiClass::SSID () {
68
131
return _ssid;
69
132
}
@@ -108,7 +171,7 @@ static uint8_t sec2enum(nsapi_security_t sec)
108
171
109
172
int8_t arduino::WiFiClass::scanNetworks () {
110
173
uint8_t count = 10 ;
111
- if (ap_list == NULL ) {
174
+ if (ap_list == nullptr ) {
112
175
ap_list = new WiFiAccessPoint[count];
113
176
}
114
177
return wifi_if->scan (ap_list, count);
@@ -130,9 +193,8 @@ int32_t arduino::WiFiClass::RSSI() {
130
193
return wifi_if->get_rssi ();
131
194
}
132
195
133
- uint8_t arduino::WiFiClass::status () {
134
- // @todo: fix
135
- return WL_CONNECTED;
196
+ uint8_t arduino::WiFiClass::status () {
197
+ return _currentNetworkStatus;
136
198
}
137
199
138
200
uint8_t arduino::WiFiClass::encryptionType () {
@@ -148,7 +210,7 @@ uint8_t* arduino::WiFiClass::BSSID(unsigned char* bssid) {
148
210
}
149
211
150
212
uint8_t * arduino::WiFiClass::macAddress (uint8_t * mac) {
151
- const char *mac_str = wifi_if ->get_mac_address ();
213
+ const char *mac_str = getNetwork () ->get_mac_address ();
152
214
for ( int b = 0 ; b < 6 ; b++ )
153
215
{
154
216
uint32_t tmp;
@@ -159,28 +221,46 @@ uint8_t* arduino::WiFiClass::macAddress(uint8_t* mac) {
159
221
return mac;
160
222
}
161
223
162
- arduino::IPAddress arduino::WiFiClass::localIP () {
163
- arduino::IPAddress addr;
224
+ arduino::IPAddress arduino::WiFiClass::localIP () {
225
+ SocketAddress ip;
226
+ NetworkInterface *interface = getNetwork ();
227
+ interface->get_ip_address (&ip);
228
+ return ipAddressFromSocketAddress (ip);
229
+ }
230
+
231
+ arduino::IPAddress arduino::WiFiClass::subnetMask () {
232
+ SocketAddress ip;
233
+ NetworkInterface *interface = getNetwork ();
234
+ interface->get_netmask (&ip);
235
+ return ipAddressFromSocketAddress (ip);
236
+ }
164
237
238
+ arduino::IPAddress arduino::WiFiClass::gatewayIP () {
165
239
SocketAddress ip;
166
- if (softap != NULL ) {
167
- softap->get_ip_address (&ip);
168
- } else {
169
- wifi_if->get_ip_address (&ip);
170
- }
171
- addr.fromString (ip.get_ip_address ()); // @todo: the IP we get from Mbed is correct, but is parsed incorrectly by Arduino
172
- return addr;
240
+ NetworkInterface *interface = getNetwork ();
241
+ interface->get_gateway (&ip);
242
+ return ipAddressFromSocketAddress (ip);
173
243
}
174
244
175
245
NetworkInterface *arduino::WiFiClass::getNetwork () {
176
- if (softap != NULL ) {
177
- return softap ;
246
+ if (_softAP != nullptr ) {
247
+ return _softAP ;
178
248
} else {
179
249
return wifi_if;
180
250
}
181
251
}
182
252
253
+ unsigned long arduino::WiFiClass::getTime () {
254
+ return 0 ;
255
+ }
256
+
183
257
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
258
+
259
+ #include " whd_version.h"
260
+ char * arduino::WiFiClass::firmwareVersion () {
261
+ return WHD_VERSION;
262
+ }
263
+
184
264
arduino::WiFiClass WiFi (WiFiInterface::get_default_instance());
185
265
#endif
186
266
0 commit comments