Skip to content

Commit 52cd613

Browse files
committed
Add generic IP utilities
Add: broadcastIP() => Retrieve the network id (e.g. 192.168.0.0) networkID() => Retrieve the broadcast IP (e.g. 192.168.0.255) subnetCIDR() => Retrieve the subnet CIDR (e.g. 24)
1 parent d1ad5ec commit 52cd613

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

libraries/WiFi/src/WiFiSTA.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,48 @@ IPAddress WiFiSTAClass::dnsIP(uint8_t dns_no)
492492
return IPAddress(dns_ip.u_addr.ip4.addr);
493493
}
494494

495+
/**
496+
* Get the broadcast ip address.
497+
* @return IPAddress broadcastIP
498+
*/
499+
IPAddress WiFiSTAClass::broadcastIP()
500+
{
501+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
502+
return IPAddress();
503+
}
504+
tcpip_adapter_ip_info_t ip;
505+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
506+
return WiFiGenericClass::calculateBroadcast(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
507+
}
508+
509+
/**
510+
* Get the network id.
511+
* @return IPAddress netwrokID
512+
*/
513+
IPAddress WiFiSTAClass::netwrokID()
514+
{
515+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
516+
return IPAddress();
517+
}
518+
tcpip_adapter_ip_info_t ip;
519+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
520+
return WiFiGenericClass::calculateNetworkID(IPAddress(ip.gw.addr), IPAddress(ip.netmask.addr));
521+
}
522+
523+
/**
524+
* Get the network id.
525+
* @return uint8_t netwrokID
526+
*/
527+
uint8_t WiFiSTAClass::subnetCIDR()
528+
{
529+
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
530+
return (uint8_t)0;
531+
}
532+
tcpip_adapter_ip_info_t ip;
533+
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
534+
return WiFiGenericClass::calculateSubnetCIDR(IPAddress(ip.netmask.addr));
535+
}
536+
495537
/**
496538
* Return the current SSID associated with the network
497539
* @return SSID

libraries/WiFi/src/WiFiSTA.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class WiFiSTAClass
6464
IPAddress subnetMask();
6565
IPAddress gatewayIP();
6666
IPAddress dnsIP(uint8_t dns_no = 0);
67+
68+
IPAddress broadcastIP();
69+
IPAddress netwrokID();
70+
uint8_t subnetCIDR();
6771

6872
bool enableIpV6();
6973
IPv6Address localIPv6();

0 commit comments

Comments
 (0)