Skip to content

Commit

Permalink
Add MDNS hostname as a config element (#17)
Browse files Browse the repository at this point in the history
* add MDNS hostname as a config element
* add MDNS hostname to http config

Co-authored-by: SG <[email protected]>
  • Loading branch information
0xDRRB and DrZlo13 authored Oct 27, 2022
1 parent c4aefda commit 473bdd4
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 18 deletions.
4 changes: 0 additions & 4 deletions components/svelte-portal/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/svelte-portal/public/build/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/svelte-portal/public/build/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/svelte-portal/public/build/bundle.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions components/svelte-portal/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
let ap_pass_input;
let sta_ssid_input;
let sta_pass_input;
let hostname_input;
let current_tab = "WiFi";
if (localStorage.getItem("current_tab") != null) {
Expand All @@ -56,6 +57,7 @@
ap_pass: ap_pass_input.get_value(),
sta_ssid: sta_ssid_input.get_value(),
sta_pass: sta_pass_input.get_value(),
hostname: hostname_input.get_value(),
}).then((json) => {
if (json.error) {
popup_message_text = json.error;
Expand Down Expand Up @@ -145,6 +147,8 @@
<div><Spinner /></div>
<div class="value-name">Pass:</div>
<div><Spinner /></div>
<div class="value-name">Hostname:</div>
<div><Spinner /></div>
{:then json}
<div class="value-name">Mode:</div>
<div>
Expand Down Expand Up @@ -184,6 +188,11 @@
<div>
<Input value={json.ap_pass} bind:this={ap_pass_input} />
</div>

<div class="value-name">Hostname:</div>
<div>
<Input value={json.hostname} bind:this={hostname_input} />
</div>
{:catch error}
<error>{error.message}</error>
{/await}
Expand Down
27 changes: 26 additions & 1 deletion main/cli/cli-commands-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ void cli_config_get(Cli* cli, mstring_t* args) {
cli_printf(cli, "sta_pass: %s", mstring_get_cstr(value));
cli_write_eol(cli);

nvs_config_get_hostname(value);
cli_printf(cli, "hostname: %s", mstring_get_cstr(value));
cli_write_eol(cli);

nvs_config_get_wifi_mode(&wifi_mode);
switch(wifi_mode) {
case WiFiModeAP:
Expand Down Expand Up @@ -165,6 +169,27 @@ void cli_config_set_sta_ssid(Cli* cli, mstring_t* args) {
mstring_free(ssid);
}

void cli_config_set_hostname(Cli* cli, mstring_t* args) {
mstring_t* hostname = mstring_alloc();

do {
if(!cli_args_read_quoted_string_and_trim(args, hostname)) {
cli_write_str(cli, "config_set_hostname \"<hostname>\", 1-32 symbols");
break;
}

if(nvs_config_set_hostname(hostname) == ESP_OK) {
cli_write_str(cli, "OK");
cli_write_eol(cli);
cli_write_str(cli, "Reboot to apply");
} else {
cli_write_str(cli, "config_set_hostname \"<hostname>\", 1-32 symbols");
}
} while(false);

mstring_free(hostname);
}

const char* nvs_type_to_str(nvs_type_t type) {
switch(type) {
case NVS_TYPE_U8:
Expand Down Expand Up @@ -225,4 +250,4 @@ void cli_nvs_dump(Cli* cli, mstring_t* args) {
}

cli_write_str(cli, "OK");
}
}
8 changes: 7 additions & 1 deletion main/cli/cli-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void cli_config_set_ap_pass(Cli* cli, mstring_t* args);
void cli_config_set_ap_ssid(Cli* cli, mstring_t* args);
void cli_config_set_sta_pass(Cli* cli, mstring_t* args);
void cli_config_set_sta_ssid(Cli* cli, mstring_t* args);
void cli_config_set_hostname(Cli* cli, mstring_t* args);

void cli_nvs_dump(Cli* cli, mstring_t* args);

Expand Down Expand Up @@ -73,6 +74,11 @@ const CliItem cli_items[] = {
.desc = "set STA mode SSID, requires a reboot to apply",
.callback = cli_config_set_sta_ssid,
},
{
.name = "config_set_hostname",
.desc = "set MDNS host name, requires a reboot to apply",
.callback = cli_config_set_hostname,
},
{
.name = "device_info",
.desc = "show device info (mac, fw version, chip info, etc)",
Expand Down Expand Up @@ -221,4 +227,4 @@ void cli_led(Cli* cli, mstring_t* args) {
led_set((uint8_t)red, (uint8_t)green, (uint8_t)blue);
cli_write_str(cli, "OK");
} while(false);
}
}
27 changes: 27 additions & 0 deletions main/network-http.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,21 @@ static esp_err_t wifi_get_credentials_handler(httpd_req_t* req) {
mstring_t* ap_pass = mstring_alloc();
mstring_t* sta_ssid = mstring_alloc();
mstring_t* sta_pass = mstring_alloc();
mstring_t* hostname = mstring_alloc();
WiFiMode wifi_mode;

nvs_config_get_wifi_mode(&wifi_mode);
nvs_config_get_ap_ssid(ap_ssid);
nvs_config_get_ap_pass(ap_pass);
nvs_config_get_sta_ssid(sta_ssid);
nvs_config_get_sta_pass(sta_pass);
nvs_config_get_hostname(hostname);

cJSON_AddStringToObject(root, "ap_ssid", mstring_get_cstr(ap_ssid));
cJSON_AddStringToObject(root, "ap_pass", mstring_get_cstr(ap_pass));
cJSON_AddStringToObject(root, "sta_ssid", mstring_get_cstr(sta_ssid));
cJSON_AddStringToObject(root, "sta_pass", mstring_get_cstr(sta_pass));
cJSON_AddStringToObject(root, "hostname", mstring_get_cstr(hostname));

switch(wifi_mode) {
case WiFiModeAP:
Expand All @@ -335,6 +338,7 @@ static esp_err_t wifi_get_credentials_handler(httpd_req_t* req) {
mstring_free(ap_pass);
mstring_free(sta_ssid);
mstring_free(sta_pass);
mstring_free(hostname);
return ESP_OK;
}

Expand All @@ -348,6 +352,7 @@ static esp_err_t wifi_set_credentials_handler(httpd_req_t* req) {
mstring_t* sta_ssid = mstring_alloc();
mstring_t* sta_pass = mstring_alloc();
mstring_t* wifi_mode = mstring_alloc();
mstring_t* hostname = mstring_alloc();
const char* error_text = JSON_ERROR("unknown error");
int received = 0;
httpd_resp_set_type(req, "application/json");
Expand Down Expand Up @@ -407,6 +412,14 @@ static esp_err_t wifi_set_credentials_handler(httpd_req_t* req) {
error_text = JSON_ERROR("request dont have [wifi_mode] field");
goto err_fail;
}

if(cJSON_GetObjectItem(root, "hostname") != NULL) {
mstring_set(hostname, cJSON_GetObjectItem(root, "hostname")->valuestring);
} else {
cJSON_Delete(root);
error_text = JSON_ERROR("request dont have [hostname] field");
goto err_fail;
}
cJSON_Delete(root);

if(strcmp(mstring_get_cstr(wifi_mode), CFG_WIFI_MODE_AP) != 0 &&
Expand Down Expand Up @@ -444,7 +457,19 @@ static esp_err_t wifi_set_credentials_handler(httpd_req_t* req) {
}
}

if(nvs_config_set_hostname(hostname) != ESP_OK) {
error_text = JSON_ERROR("invalid value in [hostname]");
goto err_fail;
}

httpd_resp_sendstr(req, JSON_RESULT("WIFI settings saved"));
free(buffer);
mstring_free(ap_ssid);
mstring_free(ap_pass);
mstring_free(sta_ssid);
mstring_free(sta_pass);
mstring_free(wifi_mode);
mstring_free(hostname);
return ESP_OK;

err_fail:
Expand All @@ -455,6 +480,7 @@ static esp_err_t wifi_set_credentials_handler(httpd_req_t* req) {
mstring_free(sta_ssid);
mstring_free(sta_pass);
mstring_free(wifi_mode);
mstring_free(hostname);
return ESP_FAIL;
}

Expand Down Expand Up @@ -559,6 +585,7 @@ static esp_err_t gpio_led_set_handler(httpd_req_t* req) {
}

httpd_resp_sendstr(req, JSON_RESULT("OK"));
free(buffer);
return ESP_OK;

err_fail:
Expand Down
12 changes: 9 additions & 3 deletions main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,13 @@ static bool network_connect_ap(mstring_t* ap_ssid, mstring_t* ap_pass) {
}

void network_post_init(void) {
mstring_t* hostname = mstring_alloc();

ESP_LOGI(TAG, "init mdns");
mdns_init();
mdns_hostname_set(MDNS_HOST_NAME);

nvs_config_get_hostname(hostname);
mdns_hostname_set(mstring_get_cstr(hostname));
mdns_instance_name_set(MDNS_INSTANCE);

mdns_txt_item_t serviceTxtData[] = {{"board", "esp32"}, {"path", "/"}};
Expand All @@ -171,8 +175,10 @@ void network_post_init(void) {

ESP_LOGI(TAG, "init netbios");
netbiosns_init();
netbiosns_set_name(MDNS_HOST_NAME);
netbiosns_set_name(mstring_get_cstr(hostname));
ESP_LOGI(TAG, "init netbios done");

mstring_free(hostname);
}

WiFiMode network_init(void) {
Expand Down Expand Up @@ -207,4 +213,4 @@ WiFiMode network_init(void) {

WiFiMode network_get_mode(void) {
return wifi_mode;
}
}
24 changes: 23 additions & 1 deletion main/nvs-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#define WIFI_AP_PASS_KEY "wifi_ap_pass"
#define WIFI_STA_SSID_KEY "wifi_sta_ssid"
#define WIFI_STA_PASS_KEY "wifi_sta_pass"
#define WIFI_HOSTNAME "wifi_hostname"

#define ESP_WIFI_DEFAULT_SSID "blackmagic"
#define ESP_WIFI_DEFAULT_PASS "iamwitcher"
#define ESP_WIFI_DEFAULT_HOSTNAME "blackmagic"

esp_err_t nvs_config_set_wifi_mode(WiFiMode value) {
mstring_t* mode = mstring_alloc();
Expand Down Expand Up @@ -69,6 +71,16 @@ esp_err_t nvs_config_set_sta_pass(const mstring_t* pass) {
return err;
}

esp_err_t nvs_config_set_hostname(const mstring_t* hostname) {
esp_err_t err = ESP_FAIL;

if(mstring_size(hostname) > 0 && mstring_size(hostname) <= 32) {
err = nvs_save_string(WIFI_HOSTNAME, hostname);
}

return err;
}

esp_err_t nvs_config_get_wifi_mode(WiFiMode* value) {
mstring_t* mode = mstring_alloc();
esp_err_t err = nvs_load_string(WIFI_MODE_KEY, mode);
Expand Down Expand Up @@ -122,4 +134,14 @@ esp_err_t nvs_config_get_sta_pass(mstring_t* pass) {
}

return err;
}
}

esp_err_t nvs_config_get_hostname(mstring_t* hostname) {
esp_err_t err = nvs_load_string(WIFI_HOSTNAME, hostname);

if(err != ESP_OK) {
mstring_set(hostname, ESP_WIFI_DEFAULT_HOSTNAME);
}

return err;
}
4 changes: 3 additions & 1 deletion main/nvs-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ esp_err_t nvs_config_set_ap_ssid(const mstring_t* ssid);
esp_err_t nvs_config_set_ap_pass(const mstring_t* pass);
esp_err_t nvs_config_set_sta_ssid(const mstring_t* ssid);
esp_err_t nvs_config_set_sta_pass(const mstring_t* pass);
esp_err_t nvs_config_set_hostname(const mstring_t* hostname);

esp_err_t nvs_config_get_wifi_mode(WiFiMode* value);
esp_err_t nvs_config_get_ap_ssid(mstring_t* ssid);
esp_err_t nvs_config_get_ap_pass(mstring_t* pass);
esp_err_t nvs_config_get_sta_ssid(mstring_t* ssid);
esp_err_t nvs_config_get_sta_pass(mstring_t* pass);
esp_err_t nvs_config_get_sta_pass(mstring_t* pass);
esp_err_t nvs_config_get_hostname(mstring_t* hostname);
Loading

0 comments on commit 473bdd4

Please sign in to comment.