Skip to content

Commit 84403e2

Browse files
committed
Fixed DNS issue
1 parent a984979 commit 84403e2

File tree

6 files changed

+97
-16
lines changed

6 files changed

+97
-16
lines changed

WiFi/WiFi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,9 @@ uint8_t WiFiClass::status()
175175
return WiFiDrv::getConnectionStatus();
176176
}
177177

178+
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
179+
{
180+
return WiFiDrv::getHostByName(aHostname, aResult);
181+
}
182+
178183
WiFiClass WiFi;

WiFi/WiFi.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ class WiFiClass
159159
*/
160160
uint8_t status();
161161

162+
/*
163+
* Resolve the given hostname to an IP address.
164+
* param aHostname: Name to be resolved
165+
* param aResult: IPAddress structure to store the returned IP address
166+
* result: 1 if aIPAddrString was successfully converted to an IP address,
167+
* else error code
168+
*/
169+
int hostByName(const char* aHostname, IPAddress& aResult);
170+
162171
friend class WiFiClient;
163172
friend class WiFiServer;
164173
};

WiFi/WiFiClient.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,12 @@ WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) {
2121
}
2222

2323
int WiFiClient::connect(const char* host, uint16_t port) {
24-
/* TODO Add DNS wifi spi function to resolve DNS */
25-
#if 0
26-
// Look up the host first
27-
int ret = 0;
28-
DNSClient dns;
29-
IPAddress remote_addr;
30-
31-
dns.begin(Ethernet.dnsServerIP());
32-
ret = dns.getHostByName(host, remote_addr);
33-
if (ret == 1) {
34-
return connect(remote_addr, port);
35-
} else {
36-
return ret;
37-
}
38-
#endif
39-
return 0;
24+
IPAddress remote_addr;
25+
if (WiFi.hostByName(host, remote_addr))
26+
{
27+
return connect(remote_addr, port);
28+
}
29+
return 0;
4030
}
4131

4232
int WiFiClient::connect(IPAddress ip, uint16_t port) {

WiFi/utility/wifi_drv.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,66 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem)
376376
return networkRssi;
377377
}
378378

379+
uint8_t WiFiDrv::reqHostByName(const char* aHostname)
380+
{
381+
WAIT_FOR_SLAVE_SELECT();
382+
383+
// Send Command
384+
SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1);
385+
SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM);
386+
387+
//Wait the reply elaboration
388+
SpiDrv::waitForSlaveReady();
389+
390+
// Wait for reply
391+
uint8_t _data = 0;
392+
uint8_t _dataLen = 0;
393+
uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen);
394+
395+
SpiDrv::spiSlaveDeselect();
396+
397+
return result;
398+
}
399+
400+
int WiFiDrv::getHostByName(IPAddress& aResult)
401+
{
402+
uint8_t _ipAddr[WL_IPV4_LENGTH];
403+
IPAddress dummy(0xFF,0xFF,0xFF,0xFF);
404+
int result = 0;
405+
406+
WAIT_FOR_SLAVE_SELECT();
407+
// Send Command
408+
SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0);
409+
410+
//Wait the reply elaboration
411+
SpiDrv::waitForSlaveReady();
412+
413+
// Wait for reply
414+
uint8_t _dataLen = 0;
415+
if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen))
416+
{
417+
WARN("error waitResponse");
418+
}else{
419+
aResult = _ipAddr;
420+
result = (aResult != dummy);
421+
}
422+
SpiDrv::spiSlaveDeselect();
423+
return result;
424+
}
425+
426+
int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult)
427+
{
428+
uint8_t retry = 10;
429+
if (reqHostByName(aHostname))
430+
{
431+
while(!getHostByName(aResult) && --retry > 0)
432+
{
433+
delay(500);
434+
}
435+
}else{
436+
return 0;
437+
}
438+
return 1;
439+
}
440+
379441
WiFiDrv wiFiDrv;

WiFi/utility/wifi_drv.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class WiFiDrv
3131
*/
3232
static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
3333

34+
static uint8_t reqHostByName(const char* aHostname);
35+
36+
static int getHostByName(IPAddress& aResult);
37+
3438
public:
3539

3640
/*
@@ -181,6 +185,15 @@ class WiFiDrv
181185
*/
182186
static uint8_t getEncTypeNetowrks(uint8_t networkItem);
183187

188+
/*
189+
* Resolve the given hostname to an IP address.
190+
* param aHostname: Name to be resolved
191+
* param aResult: IPAddress structure to store the returned IP address
192+
* result: 1 if aIPAddrString was successfully converted to an IP address,
193+
* else error code
194+
*/
195+
static int getHostByName(const char* aHostname, IPAddress& aResult);
196+
184197
};
185198

186199
extern WiFiDrv wiFiDrv;

WiFi/utility/wifi_spi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ enum {
4545
GET_IDX_SSID_CMD = 0x31,
4646
GET_IDX_RSSI_CMD = 0x32,
4747
GET_IDX_ENCT_CMD = 0x33,
48+
REQ_HOST_BY_NAME_CMD= 0x34,
49+
GET_HOST_BY_NAME_CMD= 0x35,
4850

4951
// All command with DATA_FLAG 0x40 send a 16bit Len
5052

0 commit comments

Comments
 (0)