diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 2dd59c76d..8bb66f215 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -175,4 +175,9 @@ uint8_t WiFiClass::status() return WiFiDrv::getConnectionStatus(); } +int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) +{ + return WiFiDrv::getHostByName(aHostname, aResult); +} + WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 1bdaf9fe0..16d6a0ee7 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -159,6 +159,15 @@ public: */ uint8_t status(); + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + int hostByName(const char* aHostname, IPAddress& aResult); + friend class WiFiClient; friend class WiFiServer; }; diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index c85ea4217..ce325cc9f 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -21,22 +21,12 @@ WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { } int WiFiClient::connect(const char* host, uint16_t port) { - /* TODO Add DNS wifi spi function to resolve DNS */ -#if 0 - // Look up the host first - int ret = 0; - DNSClient dns; - IPAddress remote_addr; - - dns.begin(Ethernet.dnsServerIP()); - ret = dns.getHostByName(host, remote_addr); - if (ret == 1) { - return connect(remote_addr, port); - } else { - return ret; - } -#endif - return 0; + IPAddress remote_addr; + if (WiFi.hostByName(host, remote_addr)) + { + return connect(remote_addr, port); + } + return 0; } int WiFiClient::connect(IPAddress ip, uint16_t port) { diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 00a52eb4b..0e3c2813c 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -376,4 +376,66 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) return networkRssi; } +uint8_t WiFiDrv::reqHostByName(const char* aHostname) +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +int WiFiDrv::getHostByName(IPAddress& aResult) +{ + uint8_t _ipAddr[WL_IPV4_LENGTH]; + IPAddress dummy(0xFF,0xFF,0xFF,0xFF); + int result = 0; + + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen)) + { + WARN("error waitResponse"); + }else{ + aResult = _ipAddr; + result = (aResult != dummy); + } + SpiDrv::spiSlaveDeselect(); + return result; +} + +int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) +{ + uint8_t retry = 10; + if (reqHostByName(aHostname)) + { + while(!getHostByName(aResult) && --retry > 0) + { + delay(500); + } + }else{ + return 0; + } + return 1; +} + WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index a1a8a8383..056e6126a 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -31,6 +31,10 @@ private: */ static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + static uint8_t reqHostByName(const char* aHostname); + + static int getHostByName(IPAddress& aResult); + public: /* @@ -181,6 +185,15 @@ public: */ static uint8_t getEncTypeNetowrks(uint8_t networkItem); + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + static int getHostByName(const char* aHostname, IPAddress& aResult); + }; extern WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index daa06ea45..01e1acd0b 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -45,6 +45,8 @@ enum { GET_IDX_SSID_CMD = 0x31, GET_IDX_RSSI_CMD = 0x32, GET_IDX_ENCT_CMD = 0x33, + REQ_HOST_BY_NAME_CMD= 0x34, + GET_HOST_BY_NAME_CMD= 0x35, // All command with DATA_FLAG 0x40 send a 16bit Len