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

add support for querying and setting the hostname #401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions components/lua/modules/net/net_eth.inc
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,34 @@ static int leth_stat(lua_State* L) {
return table;
}

static int leth_hostname(lua_State* L) {
driver_error_t *error;
const char *hostname = luaL_optstring( L, 1, NULL );

if (NULL != hostname) {
if ((error = eth_set_hostname(hostname))) {
return luaL_driver_error(L, error);
}

return 0;
}

hostname = NULL;
if ((error = eth_get_hostname(&hostname))) {
return luaL_driver_error(L, error);
}

lua_pushstring(L, hostname);
return 1;
}


static const LUA_REG_TYPE eth_map[] = {
{ LSTRKEY( "setup" ), LFUNCVAL( leth_setup ) },
{ LSTRKEY( "start" ), LFUNCVAL( leth_start ) },
{ LSTRKEY( "stop" ), LFUNCVAL( leth_stop ) },
{ LSTRKEY( "stat" ), LFUNCVAL( leth_stat ) },
{ LSTRKEY( "setup" ), LFUNCVAL( leth_setup ) },
{ LSTRKEY( "start" ), LFUNCVAL( leth_start ) },
{ LSTRKEY( "stop" ), LFUNCVAL( leth_stop ) },
{ LSTRKEY( "stat" ), LFUNCVAL( leth_stat ) },
{ LSTRKEY( "hostname" ), LFUNCVAL( leth_hostname ) },
DRIVER_REGISTER_LUA_ERRORS(eth)
{ LNILKEY, LNILVAL }
};
Expand Down
22 changes: 22 additions & 0 deletions components/lua/modules/net/net_wifi.inc
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,27 @@ static int lwifi_smartconfig(lua_State* L) {
return 0;
}

static int lwifi_hostname(lua_State* L) {
driver_error_t *error;
const char *hostname = luaL_optstring( L, 1, NULL );

if (NULL != hostname) {
if ((error = wifi_set_hostname(hostname))) {
return luaL_driver_error(L, error);
}

return 0;
}

hostname = NULL;
if ((error = wifi_get_hostname(&hostname))) {
return luaL_driver_error(L, error);
}

lua_pushstring(L, hostname);
return 1;
}


static const LUA_REG_TYPE wifi_auth_map[] = {
{ LSTRKEY( "OPEN" ), LINTVAL( WIFI_AUTH_OPEN ) },
Expand Down Expand Up @@ -559,6 +580,7 @@ static const LUA_REG_TYPE wifi_map[] = {
{ LSTRKEY( "start" ), LFUNCVAL( lwifi_start ) },
{ LSTRKEY( "stop" ), LFUNCVAL( lwifi_stop ) },
{ LSTRKEY( "stat" ), LFUNCVAL( lwifi_stat ) },
{ LSTRKEY( "hostname" ), LFUNCVAL( lwifi_hostname ) },
#if CONFIG_ESP32_WIFI_NVS_ENABLED
// wps ssid+auth can only be stored to nvs
// so if nvs is disabled, wps would have to be repeated on every boot
Expand Down
14 changes: 14 additions & 0 deletions components/sys/drivers/eth.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,18 @@ driver_error_t *eth_stat(ifconfig_t *info) {
return NULL;
}

driver_error_t *eth_set_hostname(const char *hostname) {
esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname);
if (ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS == ret) return driver_error(ETH_DRIVER, ETH_ERR_INVALID_ARGUMENT, NULL);
if (ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY == ret) return driver_error(ETH_DRIVER, ETH_ERR_NOT_START, NULL);
return NULL;
}

driver_error_t *eth_get_hostname(const char **hostname) {
esp_err_t ret = tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_ETH, hostname);
if (ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS == ret) return driver_error(ETH_DRIVER, ETH_ERR_INVALID_ARGUMENT, NULL);
if (ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY == ret) return driver_error(ETH_DRIVER, ETH_ERR_NOT_START, NULL);
return NULL;
}

#endif
2 changes: 2 additions & 0 deletions components/sys/drivers/eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,7 @@ driver_error_t *eth_setup(uint32_t ip, uint32_t mask, uint32_t gw, uint32_t dns1
driver_error_t *eth_start(uint8_t async);
driver_error_t *eth_stop();
driver_error_t *eth_stat(ifconfig_t *info);
driver_error_t *eth_set_hostname(const char *hostname);
driver_error_t *eth_get_hostname(const char **hostname);

#endif /* DRIVERS_ETH_H_ */
40 changes: 40 additions & 0 deletions components/sys/drivers/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,44 @@ driver_error_t *wifi_get_mac(uint8_t mac[6]) {
return NULL;
}

driver_error_t *wifi_set_hostname(const char *hostname) {
driver_error_t *error;

uint8_t interface = ESP_IF_WIFI_STA;
if (status_get(STATUS_WIFI_INITED)) {
wifi_mode_t mode;
if ((error = wifi_check_error(esp_wifi_get_mode(&mode)))) return error;

if (mode == WIFI_MODE_AP)
interface = ESP_IF_WIFI_AP;

//TODO add mac for WIFI_MODE_APSTA
}

esp_err_t ret = tcpip_adapter_set_hostname(interface, hostname);
if (ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS == ret) return driver_error(ETH_DRIVER, WIFI_ERR_INVALID_ARGUMENT, NULL);
if (ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY == ret) return driver_error(ETH_DRIVER, WIFI_ERR_WIFI_NOT_START, NULL);
return NULL;
}

driver_error_t *wifi_get_hostname(const char **hostname) {
driver_error_t *error;

uint8_t interface = ESP_IF_WIFI_STA;
if (status_get(STATUS_WIFI_INITED)) {
wifi_mode_t mode;
if ((error = wifi_check_error(esp_wifi_get_mode(&mode)))) return error;

if (mode == WIFI_MODE_AP)
interface = ESP_IF_WIFI_AP;

//TODO add mac for WIFI_MODE_APSTA
}

esp_err_t ret = tcpip_adapter_get_hostname(interface, hostname);
if (ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS == ret) return driver_error(ETH_DRIVER, WIFI_ERR_INVALID_ARGUMENT, NULL);
if (ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY == ret) return driver_error(ETH_DRIVER, WIFI_ERR_WIFI_NOT_START, NULL);
return NULL;
}

#endif
2 changes: 2 additions & 0 deletions components/sys/drivers/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ driver_error_t *wifi_start(uint8_t async);
driver_error_t *wifi_stop();
driver_error_t *wifi_stat(ifconfig_t *info);
driver_error_t *wifi_get_mac(uint8_t mac[6]);
driver_error_t *wifi_set_hostname(const char *hostname);
driver_error_t *wifi_get_hostname(const char **hostname);

void wifi_wps_reconnect();
void wifi_wps_disable();
Expand Down