Skip to content
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

DHCP hostname support #1296

Merged
merged 1 commit into from
May 2, 2017
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
2 changes: 2 additions & 0 deletions hal/inc/hal_dynalib_wlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ DYNALIB_FN(23, hal_wlan, wlan_scan, int(wlan_scan_result_t, void*))
DYNALIB_FN(24, hal_wlan, wlan_get_credentials, int(wlan_scan_result_t, void*))
DYNALIB_FN(25, hal_wlan, softap_set_application_page_handler, int(PageProvider* provider, void* reserved))
DYNALIB_FN(26, hal_wlan, wlan_restart, int(void))
DYNALIB_FN(27, hal_wlan, wlan_set_hostname, int(const char*, void*))
DYNALIB_FN(28, hal_wlan, wlan_get_hostname, int(char*, size_t, void*))
DYNALIB_END(hal_wlan)

#endif // PLATFORM_ID != 10
Expand Down
3 changes: 3 additions & 0 deletions hal/inc/wlan_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ bool isWiFiPowersaveClockDisabled(void);
*/
int wlan_restart();

int wlan_set_hostname(const char* hostname, void* reserved);
int wlan_get_hostname(char* buf, size_t buf_size, void* reserved);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions hal/src/core/wlan_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,18 @@ int wlan_restart()
{
return -1;
}

int wlan_get_hostname(char* buf, size_t len, void* reserved)
{
// Unsupported
if (buf) {
buf[0] = '\0';
}
return -1;
}

int wlan_set_hostname(const char* hostname, void* reserved)
{
// Unsupported
return -1;
}
15 changes: 15 additions & 0 deletions hal/src/gcc/wlan_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ int wlan_restart()
{
return -1;
}

int wlan_get_hostname(char* buf, size_t len, void* reserved)
{
// Unsupported
if (buf) {
buf[0] = '\0';
}
return -1;
}

int wlan_set_hostname(const char* hostname, void* reserved)
{
// Unsupported
return -1;
}
Binary file modified hal/src/photon/lib/FreeRTOS/LwIP.a
Binary file not shown.
Binary file modified hal/src/photon/lib/FreeRTOS/STM32F2xx.a
Binary file not shown.
Binary file modified hal/src/photon/lib/FreeRTOS/WICED_LwIP_Interface.a
Binary file not shown.
Binary file modified hal/src/photon/lib/FreeRTOS/WWD_LwIP_Interface_FreeRTOS.a
Binary file not shown.
Binary file modified hal/src/photon/lib/FreeRTOS/Wiced_Network_LwIP_FreeRTOS.a
Binary file not shown.
32 changes: 32 additions & 0 deletions hal/src/photon/wlan_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ uint64_t tls_host_get_time_ms_local() {
return time_ms;
}

char* lwip_hostname_ptr = NULL;

LOG_SOURCE_CATEGORY("hal.wlan");

// dns.h includes a class member, which doesn't compile in C++
Expand Down Expand Up @@ -564,6 +566,36 @@ static wiced_result_t wlan_join() {
return result;
}

int wlan_get_hostname(char* buf, size_t len, void* reserved)
{
wiced_result_t result;
wiced_hostname_t hostname;

memset(&hostname, 0, sizeof(hostname));
memset(buf, 0, len);

result = wiced_network_get_hostname(&hostname);
if (result == WICED_SUCCESS)
{
if (hostname.value[0] != 0xff) {
size_t l = strnlen(hostname.value, HOSTNAME_SIZE);
if (len > l) {
memcpy(buf, hostname.value, l);
} else {
result = WICED_ERROR;
}
} else {
result = WICED_ERROR;
}
}
return (int)result;
}

int wlan_set_hostname(const char* hostname, void* reserved)
{
return (int)wiced_network_set_hostname(hostname ? hostname : "");
}

/**
* Do what is needed to finalize the connection.
* @return
Expand Down
15 changes: 15 additions & 0 deletions hal/src/template/wlan_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,18 @@ int wlan_restart()
{
return -1;
}

int wlan_get_hostname(char* buf, size_t len, void* reserved)
{
// Unsupported
if (buf) {
buf[0] = '\0';
}
return -1;
}

int wlan_set_hostname(const char* hostname, void* reserved)
{
// Unsupported
return -1;
}
2 changes: 2 additions & 0 deletions system/inc/system_dynalib_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DYNALIB_FN(10, system_net, network_set_credentials, int(network_handle_t, uint32
DYNALIB_FN(11, system_net, network_clear_credentials, bool(network_handle_t, uint32_t, NetworkCredentials*, void*))
DYNALIB_FN(12, system_net, network_set_listen_timeout, void(network_handle_t, uint16_t, void*))
DYNALIB_FN(13, system_net, network_get_listen_timeout, uint16_t(network_handle_t, uint32_t, void*))
DYNALIB_FN(14, system_net, network_set_hostname, int(network_handle_t, uint32_t, const char*, void*))
DYNALIB_FN(15, system_net, network_get_hostname, int(network_handle_t, uint32_t, char*, size_t, void*))

DYNALIB_END(system_net)

Expand Down
2 changes: 2 additions & 0 deletions system/inc/system_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ bool network_clear_credentials(network_handle_t network, uint32_t flags, Network

void network_setup(network_handle_t network, uint32_t flags, void* reserved);

int network_set_hostname(network_handle_t network, uint32_t flags, const char* hostname, void* reserved);
int network_get_hostname(network_handle_t network, uint32_t flags, char* buffer, size_t buffer_len, void* reserved);
/**
* Disable automatic listening mode when no credentials are configured.
*/
Expand Down
10 changes: 10 additions & 0 deletions system/src/system_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ void manage_ip_config()
nif(0).update_config();
}

int network_set_hostname(network_handle_t network, uint32_t flags, const char* hostname, void* reserved)
{
SYSTEM_THREAD_CONTEXT_SYNC_CALL_RESULT(nif(network).set_hostname(hostname));
}

int network_get_hostname(network_handle_t network, uint32_t flags, char* buffer, size_t buffer_len, void* reserved)
{
SYSTEM_THREAD_CONTEXT_SYNC_CALL_RESULT(nif(network).get_hostname(buffer, buffer_len));
}

#endif
13 changes: 13 additions & 0 deletions system/src/system_network_cellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,18 @@ class CellularNetworkInterface : public ManagedIPNetworkInterface<CellularConfig
}

void set_error_count(unsigned count) override { /* n/a */ }

virtual int set_hostname(const char* hostname) override
{
return 1;
}

virtual int get_hostname(char* buf, size_t buf_len, bool noDefault) override
{
if (buf) {
buf[0] = '\0';
}
return 1;
}
};

13 changes: 13 additions & 0 deletions system/src/system_network_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ struct NetworkInterface
virtual void update_config(bool force=false)=0;
virtual void* config()=0; // not really happy about lack of type

virtual int set_hostname(const char* hostname)=0;
virtual int get_hostname(char* buffer, size_t buffer_len, bool noDefault=false)=0;

};


Expand Down Expand Up @@ -326,6 +329,15 @@ class ManagedNetworkInterface : public NetworkInterface
*/
virtual void on_finalize_listening(bool external_process_complete)=0;

virtual void config_hostname() {
char hostname[33] = {0};
if (get_hostname(hostname, sizeof(hostname), true) || strlen(hostname) == 0)
{
String deviceId = spark_deviceID();
set_hostname(deviceId.c_str());
}
}

public:

virtual void get_ipconfig(IPConfig* config)=0;
Expand Down Expand Up @@ -400,6 +412,7 @@ class ManagedNetworkInterface : public NetworkInterface
}
else
{
config_hostname();
LED_SIGNAL_START(NETWORK_CONNECTING, NORMAL);
WLAN_CONNECTING = 1;
INFO("ARM_WLAN_WD 1");
Expand Down
13 changes: 13 additions & 0 deletions system/src/system_network_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,18 @@ class WiFiNetworkInterface : public ManagedIPNetworkInterface<WLanConfig, WiFiNe
wlan_set_error_count(count);
}

virtual int set_hostname(const char* hostname) override
{
return wlan_set_hostname(hostname, NULL);
}

virtual int get_hostname(char* buf, size_t buf_len, bool noDefault) override
{
if (!noDefault) {
config_hostname();
}
return wlan_get_hostname(buf, buf_len, NULL);
}

};

9 changes: 9 additions & 0 deletions user/tests/wiring/api/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,13 @@ test(api_wifi_get_credentials)
}
}

test(api_wifi_hostname)
{
String hostname;
const char shostname[] = "testhostname";
API_COMPILE(hostname = WiFi.hostname());
API_COMPILE(WiFi.setHostname(hostname));
API_COMPILE(WiFi.setHostname(shostname));
}

#endif
28 changes: 28 additions & 0 deletions user/tests/wiring/no_fixture/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,32 @@ test(WIFI_06_restore_connection)
}
}

#if PLATFORM_ID == 6 || PLATFORM_ID == 8

test(WIFI_07_reset_hostname)
{
assertEqual(WiFi.setHostname(NULL), 0);
}

test(WIFI_08_default_hostname_equals_device_id)
{
String hostname = WiFi.hostname();
String devId = System.deviceID();
assertEqual(hostname, devId);
}

test(WIFI_09_custom_hostname_can_be_set)
{
String hostname("testhostname");
assertEqual(WiFi.setHostname(hostname), 0);
assertEqual(WiFi.hostname(), hostname);
}

test(WIFI_10_restore_default_hostname)
{
assertEqual(WiFi.setHostname(NULL), 0);
}

#endif // PLATFORM_ID == 6 || PLATFORM_ID == 8

#endif
18 changes: 18 additions & 0 deletions wiring/inc/spark_wiring_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ class WiFiClass : public NetworkClass
}

int getCredentials(WiFiAccessPoint* results, size_t result_count);

String hostname()
{
const size_t maxHostname = 64;
char buf[maxHostname] = {0};
network_get_hostname(*this, 0, buf, maxHostname, nullptr);
return String(buf);
}

int setHostname(const String& hostname)
{
return setHostname(hostname.c_str());
}

int setHostname(const char* hostname)
{
return network_set_hostname(*this, 0, hostname, nullptr);
}
};

extern WiFiClass WiFi;
Expand Down