Skip to content

Commit e70c085

Browse files
authored
Merge pull request #816 from JAndrassy/wifi_config_fix
WiFi - make standard `config` functions work
2 parents 8074ecf + e8c848d commit e70c085

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

libraries/SocketWrapper/src/SocketHelpers.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,37 @@ arduino::IPAddress arduino::MbedSocketClass::dnsIP(int n) {
6262
}
6363

6464
void arduino::MbedSocketClass::config(arduino::IPAddress local_ip) {
65-
nsapi_addr_t convertedIP = { NSAPI_IPv4, { local_ip[0], local_ip[1], local_ip[2], local_ip[3] } };
66-
_ip = SocketAddress(convertedIP);
65+
IPAddress dns = local_ip;
66+
dns[3] = 1;
67+
config(local_ip, dns);
6768
}
6869

6970
void arduino::MbedSocketClass::config(const char* local_ip) {
7071
_ip = SocketAddress(local_ip);
7172
}
7273

7374
void arduino::MbedSocketClass::config(IPAddress local_ip, IPAddress dns_server) {
74-
config(local_ip);
75-
setDNS(dns_server);
75+
IPAddress gw = local_ip;
76+
gw[3] = 1;
77+
config(local_ip, dns_server, gw);
7678
}
7779

7880
void arduino::MbedSocketClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) {
79-
config(local_ip, dns_server);
80-
nsapi_addr_t convertedGatewayIP = { NSAPI_IPv4, { gateway[0], gateway[1], gateway[2], gateway[3] } };
81-
_gateway = SocketAddress(convertedGatewayIP);
81+
IPAddress nm(255, 255, 255, 0);
82+
config(local_ip, dns_server, gateway, nm);
8283
}
8384

8485
void arduino::MbedSocketClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) {
85-
config(local_ip, dns_server, gateway);
86+
_useStaticIP = (local_ip != INADDR_NONE);
87+
if (!_useStaticIP)
88+
return;
89+
nsapi_addr_t convertedIP = { NSAPI_IPv4, { local_ip[0], local_ip[1], local_ip[2], local_ip[3] } };
90+
_ip = SocketAddress(convertedIP);
91+
nsapi_addr_t convertedGatewayIP = { NSAPI_IPv4, { gateway[0], gateway[1], gateway[2], gateway[3] } };
92+
_gateway = SocketAddress(convertedGatewayIP);
8693
nsapi_addr_t convertedSubnetMask = { NSAPI_IPv4, { subnet[0], subnet[1], subnet[2], subnet[3] } };
8794
_netmask = SocketAddress(convertedSubnetMask);
95+
setDNS(dns_server);
8896
}
8997

9098
void arduino::MbedSocketClass::setDNS(IPAddress dns_server1) {

libraries/SocketWrapper/src/SocketHelpers.h

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class MbedSocketClass {
144144
SocketAddress _netmask = nullptr;
145145
SocketAddress _dnsServer1 = nullptr;
146146
SocketAddress _dnsServer2 = nullptr;
147+
bool _useStaticIP = false;
147148

148149
voidFuncPtr _feed_watchdog_func = nullptr;
149150

libraries/WiFi/src/WiFi.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ int arduino::WiFiClass::begin(const char* ssid, const char* passphrase) {
2727
return _currentNetworkStatus;
2828
}
2929

30+
wifi_if->set_dhcp(!_useStaticIP);
31+
if (_useStaticIP) {
32+
wifi_if->set_network(_ip, _netmask, _gateway);
33+
char if_name[5];
34+
wifi_if->get_interface_name(if_name);
35+
wifi_if->add_dns_server(_dnsServer2, if_name);
36+
wifi_if->add_dns_server(_dnsServer1, if_name); // pushes dnsServer2 at index 1
37+
}
38+
3039
nsapi_error_t result = wifi_if->connect(ssid, passphrase, ap_list[connected_ap].get_security());
3140

3241
if(result == NSAPI_ERROR_IS_CONNECTED) {
@@ -39,8 +48,7 @@ int arduino::WiFiClass::begin(const char* ssid, const char* passphrase) {
3948

4049
//Config Wifi to set Static IP && Disable DHCP
4150
void arduino::WiFiClass::config(const char* localip, const char* netmask, const char* gateway){
42-
wifi_if->set_network(localip, netmask, gateway);
43-
wifi_if->set_dhcp(false);
51+
SocketHelpers::config(IPAddress(localip), dnsIP(0), IPAddress(gateway), IPAddress(netmask));
4452
}
4553

4654
int arduino::WiFiClass::beginAP(const char* ssid, const char* passphrase, uint8_t channel) {

0 commit comments

Comments
 (0)