Skip to content

Commit 647242e

Browse files
authored
Merge pull request #13 from sbhklr/wifi-fix
Fix Wifi AP issues
2 parents c774aac + 9e8ac4a commit 647242e

File tree

2 files changed

+134
-42
lines changed

2 files changed

+134
-42
lines changed

libraries/WiFi/src/WiFi.cpp

Lines changed: 100 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ bool arduino::WiFiClass::isVisible(char* ssid) {
1010
return false;
1111
}
1212

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+
1318
int arduino::WiFiClass::begin(char* ssid, const char *passphrase) {
1419
if (_ssid) free(_ssid);
1520

@@ -19,8 +24,10 @@ 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

2633
// too long? break it off
@@ -30,40 +37,96 @@ int arduino::WiFiClass::begin(char* ssid, const char *passphrase) {
3037
scanNetworks();
3138
// use scan result to populate security field
3239
if (!isVisible(ssid)) {
33-
return WL_CONNECT_FAILED;
40+
_currentNetworkStatus = WL_CONNECT_FAILED;
41+
return _currentNetworkStatus;
3442
}
3543

3644
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

4150
int 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+
6181
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();
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+
67130
char* arduino::WiFiClass::SSID() {
68131
return _ssid;
69132
}
@@ -108,7 +171,7 @@ static uint8_t sec2enum(nsapi_security_t sec)
108171

109172
int8_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

138200
uint8_t arduino::WiFiClass::encryptionType() {
@@ -148,7 +210,7 @@ uint8_t* arduino::WiFiClass::BSSID(unsigned char* bssid) {
148210
}
149211

150212
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();
152214
for( int b = 0; b < 6; b++ )
153215
{
154216
uint32_t tmp;
@@ -159,22 +221,30 @@ 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+
}
164230

231+
arduino::IPAddress arduino::WiFiClass::subnetMask() {
165232
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;
233+
NetworkInterface *interface = getNetwork();
234+
interface->get_netmask(&ip);
235+
return ipAddressFromSocketAddress(ip);
236+
}
237+
238+
arduino::IPAddress arduino::WiFiClass::gatewayIP() {
239+
SocketAddress ip;
240+
NetworkInterface *interface = getNetwork();
241+
interface->get_gateway(&ip);
242+
return ipAddressFromSocketAddress(ip);
173243
}
174244

175245
NetworkInterface *arduino::WiFiClass::getNetwork() {
176-
if (softap != NULL) {
177-
return softap;
246+
if (_softAP != nullptr) {
247+
return _softAP;
178248
} else {
179249
return wifi_if;
180250
}

libraries/WiFi/src/WiFi.h

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,31 @@ extern "C" {
3737
#include "WhdSoftAPInterface.h"
3838
#endif
3939

40+
#ifndef DEFAULT_IP_ADDRESS
41+
#define DEFAULT_IP_ADDRESS "192.168.3.1"
42+
#endif
43+
44+
#ifndef DEFAULT_NETMASK
45+
#define DEFAULT_NETMASK "255.255.255.0"
46+
#endif
47+
48+
#ifndef DEFAULT_AP_CHANNEL
49+
#define DEFAULT_AP_CHANNEL 6
50+
#endif
51+
4052
namespace arduino {
4153

4254
typedef void* (*voidPrtFuncPtr)(void);
4355

4456
class WiFiClass
4557
{
46-
private:
47-
48-
static void init();
4958
public:
5059
static int16_t _state[MAX_SOCK_NUM];
5160
static uint16_t _server_port[MAX_SOCK_NUM];
5261

5362
WiFiClass(WiFiInterface* _if) : wifi_if(_if) {};
5463

55-
WiFiClass(voidPrtFuncPtr _cb) : cb(_cb) {};
64+
WiFiClass(voidPrtFuncPtr _cb) : _initializerCallback(_cb) {};
5665

5766
/*
5867
* Get the first socket available
@@ -90,13 +99,19 @@ class WiFiClass
9099
*/
91100
int begin(char* ssid, const char *passphrase);
92101

93-
int beginAP(const char *ssid, const char* passphrase, uint8_t channel = 6);
102+
int beginAP(const char *ssid, const char* passphrase, uint8_t channel = DEFAULT_AP_CHANNEL);
94103

95104
/* Change Ip configuration settings disabling the dhcp client
96105
*
97106
* param local_ip: Static ip configuration
98107
*/
99108
void config(IPAddress local_ip);
109+
110+
/* Change Ip configuration settings disabling the dhcp client
111+
*
112+
* param local_ip: Static ip configuration as string
113+
*/
114+
void config(const char *local_ip);
100115

101116
/* Change Ip configuration settings disabling the dhcp client
102117
*
@@ -276,19 +291,26 @@ class WiFiClass
276291
friend class WiFiClient;
277292
friend class WiFiServer;
278293

279-
public:
280294
NetworkInterface *getNetwork();
281295

282296
private:
283297

284-
EMACInterface* softap;
298+
EMACInterface* _softAP = nullptr;
299+
SocketAddress _ip = nullptr;
300+
SocketAddress _gateway = nullptr;
301+
SocketAddress _netmask = nullptr;
302+
SocketAddress _dnsServer1 = nullptr;
303+
SocketAddress _dnsServer2 = nullptr;
304+
char* _ssid = nullptr;
305+
wl_status_t _currentNetworkStatus = WL_IDLE_STATUS;
306+
WiFiInterface* wifi_if = nullptr;
307+
voidPrtFuncPtr _initializerCallback;
308+
WiFiAccessPoint* ap_list = nullptr;
309+
uint8_t connected_ap;
285310

311+
void ensureDefaultAPNetworkConfiguration();
286312
bool isVisible(char* ssid);
287-
char* _ssid;
288-
WiFiInterface* wifi_if;
289-
voidPrtFuncPtr cb;
290-
WiFiAccessPoint* ap_list = NULL;
291-
uint8_t connected_ap;
313+
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
292314
};
293315

294316
}

0 commit comments

Comments
 (0)