11#include " WiFi.h"
22
3- bool arduino::WiFiClass::isVisible (char * ssid) {
3+ bool arduino::WiFiClass::isVisible (const char * ssid) {
44 for (int i=0 ; i<10 ; i++) {
55 if (strncmp (ap_list[i].get_ssid (), ssid, 32 ) == 0 ) {
66 connected_ap = i;
@@ -10,7 +10,12 @@ bool arduino::WiFiClass::isVisible(char* ssid) {
1010 return false ;
1111}
1212
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) {
1419 if (_ssid) free (_ssid);
1520
1621 _ssid = (char *)malloc (33 );
@@ -19,51 +24,109 @@ int arduino::WiFiClass::begin(char* ssid, const char *passphrase) {
1924 return WL_CONNECT_FAILED;
2025 }
2126
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;
2431 }
2532
26- // too long? break it off
27- if (strlen (ssid) > 32 ) ssid[32 ] = 0 ;
2833 memcpy (_ssid, ssid, 33 );
34+ // too long? break it off
35+ if (strlen (ssid) > 32 ) _ssid[32 ] = 0 ;
2936
3037 scanNetworks ();
3138 // 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;
3442 }
3543
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 ());
3745
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;
3948}
4049
4150int arduino::WiFiClass::beginAP (const char * ssid, const char *passphrase, uint8_t channel) {
4251
4352#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
44- softap = WhdSoftAPInterface::get_default_instance ();
53+ _softAP = WhdSoftAPInterface::get_default_instance ();
4554#endif
4655
47- if (softap == NULL ) {
56+ if (_softAP == NULL ) {
4857 return WL_CONNECT_FAILED;
4958 }
5059
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 */ );
5765
5866 return ret == NSAPI_ERROR_OK ? WL_AP_LISTENING : WL_CONNECT_FAILED;
5967}
6068
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+
6181void 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 ();
6490 }
6591}
6692
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+
67130char * arduino::WiFiClass::SSID () {
68131 return _ssid;
69132}
@@ -108,7 +171,7 @@ static uint8_t sec2enum(nsapi_security_t sec)
108171
109172int8_t arduino::WiFiClass::scanNetworks () {
110173 uint8_t count = 10 ;
111- if (ap_list == NULL ) {
174+ if (ap_list == nullptr ) {
112175 ap_list = new WiFiAccessPoint[count];
113176 }
114177 return wifi_if->scan (ap_list, count);
@@ -130,9 +193,8 @@ int32_t arduino::WiFiClass::RSSI() {
130193 return wifi_if->get_rssi ();
131194}
132195
133- uint8_t arduino::WiFiClass::status () {
134- // @todo: fix
135- return WL_CONNECTED;
196+ uint8_t arduino::WiFiClass::status () {
197+ return _currentNetworkStatus;
136198}
137199
138200uint8_t arduino::WiFiClass::encryptionType () {
@@ -148,7 +210,7 @@ uint8_t* arduino::WiFiClass::BSSID(unsigned char* bssid) {
148210}
149211
150212uint8_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 ();
152214 for ( int b = 0 ; b < 6 ; b++ )
153215 {
154216 uint32_t tmp;
@@ -159,28 +221,46 @@ uint8_t* arduino::WiFiClass::macAddress(uint8_t* mac) {
159221 return mac;
160222}
161223
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+ }
164237
238+ arduino::IPAddress arduino::WiFiClass::gatewayIP () {
165239 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);
173243}
174244
175245NetworkInterface *arduino::WiFiClass::getNetwork () {
176- if (softap != NULL ) {
177- return softap ;
246+ if (_softAP != nullptr ) {
247+ return _softAP ;
178248 } else {
179249 return wifi_if;
180250 }
181251}
182252
253+ unsigned long arduino::WiFiClass::getTime () {
254+ return 0 ;
255+ }
256+
183257#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+
184264arduino::WiFiClass WiFi (WiFiInterface::get_default_instance());
185265#endif
186266
0 commit comments