Skip to content

DNS server IP getter dnsIP() #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,71 @@ void setup()
void loop () {}
```

### `WiFi.dnsIP()`

#### Description

Returns the DNS server IP address for the device.


#### Syntax

```
WiFi.dnsIP()
WiFi.dnsIP(n)

```

#### Parameters
optional parameter n for the number of the DNS server to get the second DNS serverv

#### Returns
- the DNS server IP address for the device (IPAddress).

#### Example

```
#include <WiFiNINA.h>

#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

IPAddress emptyIP;

void setup() {

Serial.begin(115200);
while (!Serial) {}

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}

Serial.print("DHCP assigned DNS server: ");
IPAddress dns1 = WiFi.dnsIP();
if (dns1 == emptyIP) {
Serial.println("not set");
} else {
dns1.printTo(Serial);
Serial.println();
IPAddress dns2 = WiFi.dnsIP(1);
if (dns2 != emptyIP) {
Serial.print("DNS server2: ");
dns2.printTo(Serial);
Serial.println();
}
}

}

void loop() {
}
```
### `WiFi.getTime()`

#### Description
Expand Down
10 changes: 10 additions & 0 deletions src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ IPAddress WiFiClass::gatewayIP()
return ret;
}

IPAddress WiFiClass::dnsIP(int n)
{
if (n > 1)
return IPAddress(0,0,0,0);
IPAddress dnsip0;
IPAddress dnsip1;
WiFiDrv::getDNS(dnsip0, dnsip1);
return n ? dnsip1 : dnsip0;
}

const char* WiFiClass::SSID()
{
return WiFiDrv::getCurrentSSID();
Expand Down
8 changes: 8 additions & 0 deletions src/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ class WiFiClass
*/
IPAddress gatewayIP();

/*
* Get the DNS server IP address.
* param n: index of the DNS server
* return: DNS server IP address value
* requires firmware version > 1.5.0
*/
IPAddress dnsIP(int n = 0);

/*
* Return the current SSID associated with the network
*
Expand Down
33 changes: 33 additions & 0 deletions src/utility/wifi_drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,39 @@ void WiFiDrv::getIpAddress(IPAddress& ip)
ip = _gatewayIp;
}

void WiFiDrv::getDNS(IPAddress& dnsip0, IPAddress& dnsip1)
{
uint8_t ip0[WL_IPV4_LENGTH] = {0};
uint8_t ip1[WL_IPV4_LENGTH] = {0};

tParam params[PARAM_NUMS_2] = { {0, (char*)ip0}, {0, (char*)ip1}};

WAIT_FOR_SLAVE_SELECT();

// Send Command
SpiDrv::sendCmd(GET_DNS_CONFIG_CMD, PARAM_NUMS_1);

uint8_t _dummy = DUMMY_DATA;
SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM);

// pad to multiple of 4
SpiDrv::readChar();
SpiDrv::readChar();

SpiDrv::spiSlaveDeselect();
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
SpiDrv::spiSlaveSelect();

// Wait for reply
SpiDrv::waitResponseParams(GET_DNS_CONFIG_CMD, PARAM_NUMS_2, params);

SpiDrv::spiSlaveDeselect();

dnsip0 = ip0;
dnsip1 = ip1;
}

const char* WiFiDrv::getCurrentSSID()
{
WAIT_FOR_SLAVE_SELECT();
Expand Down
7 changes: 7 additions & 0 deletions src/utility/wifi_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class WiFiDrv
*/
static void getGatewayIP(IPAddress& ip);

/*
* Get the DNS servers IP addresses.
*
* return: copy the DNS servers IP addresses into IPAddress objects
*/
static void getDNS(IPAddress& dnsip0, IPAddress& dnsip1);

/*
* Return the current SSID associated with the network
*
Expand Down
1 change: 1 addition & 0 deletions src/utility/wifi_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum {
SET_AP_PASSPHRASE_CMD = 0x19,
SET_DEBUG_CMD = 0x1A,
GET_TEMPERATURE_CMD = 0x1B,
GET_DNS_CONFIG_CMD = 0x1E,
GET_REASON_CODE_CMD = 0x1F,

GET_CONN_STATUS_CMD = 0x20,
Expand Down