Skip to content

Commit fe6266e

Browse files
mniestrojnashif
authored andcommitted
drivers: wifi: esp: fix hostname configuration
By the time hostname configuration was implemented, driver was switching only between STA and STA+AP modes. After dynamic selection between NONE, STA, AP and STA+AP was implemented (commit referenced below), hostname configuration no longer takes effect when ESP chip obtains address over DHCP (and sends hostname in the DHCP request). Set hostname each time after enabling STA mode, so that it takes effect in DHCP requests. Fixes: 03ce610 ("drivers: wifi: esp: control CWMODE depending on current needs") Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 1cb2dce commit fe6266e

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

drivers/wifi/esp/esp.c

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ K_KERNEL_STACK_DEFINE(esp_workq_stack,
6767

6868
struct esp_data esp_driver_data;
6969

70+
static void esp_configure_hostname(struct esp_data *data)
71+
{
72+
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
73+
char cmd[sizeof("AT+CWHOSTNAME=\"\"") + NET_HOSTNAME_MAX_LEN];
74+
75+
snprintk(cmd, sizeof(cmd), "AT+CWHOSTNAME=\"%s\"", net_hostname_get());
76+
cmd[sizeof(cmd) - 1] = '\0';
77+
78+
esp_cmd_send(data, NULL, 0, cmd, ESP_CMD_TIMEOUT);
79+
#else
80+
ARG_UNUSED(data);
81+
#endif
82+
}
83+
7084
static inline uint8_t esp_mode_from_flags(struct esp_data *data)
7185
{
7286
uint8_t flags = data->flags;
@@ -111,10 +125,25 @@ static int esp_mode_switch(struct esp_data *data, uint8_t mode)
111125
static int esp_mode_switch_if_needed(struct esp_data *data)
112126
{
113127
uint8_t new_mode = esp_mode_from_flags(data);
128+
uint8_t old_mode = data->mode;
129+
int err;
130+
131+
if (old_mode == new_mode) {
132+
return 0;
133+
}
134+
135+
data->mode = new_mode;
114136

115-
if (data->mode != new_mode) {
116-
data->mode = new_mode;
117-
return esp_mode_switch(data, new_mode);
137+
err = esp_mode_switch(data, new_mode);
138+
if (err) {
139+
return err;
140+
}
141+
142+
if (!(old_mode & ESP_MODE_STA) && (new_mode & ESP_MODE_STA)) {
143+
/*
144+
* Hostname change is applied only when STA is enabled.
145+
*/
146+
esp_configure_hostname(data);
118147
}
119148

120149
return 0;
@@ -877,20 +906,6 @@ static int esp_mgmt_ap_disable(const struct device *dev)
877906
return esp_mode_flags_clear(data, EDF_AP_ENABLED);
878907
}
879908

880-
static void esp_configure_hostname(struct esp_data *data)
881-
{
882-
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
883-
char cmd[sizeof("AT+CWHOSTNAME=\"\"") + NET_HOSTNAME_MAX_LEN];
884-
885-
snprintk(cmd, sizeof(cmd), "AT+CWHOSTNAME=\"%s\"", net_hostname_get());
886-
cmd[sizeof(cmd) - 1] = '\0';
887-
888-
esp_cmd_send(data, NULL, 0, cmd, ESP_CMD_TIMEOUT);
889-
#else
890-
ARG_UNUSED(data);
891-
#endif
892-
}
893-
894909
static void esp_init_work(struct k_work *work)
895910
{
896911
struct esp_data *dev;
@@ -981,7 +996,16 @@ static void esp_init_work(struct k_work *work)
981996
net_if_set_link_addr(dev->net_iface, dev->mac_addr,
982997
sizeof(dev->mac_addr), NET_LINK_ETHERNET);
983998

984-
esp_configure_hostname(dev);
999+
if (IS_ENABLED(CONFIG_WIFI_ESP_AT_VERSION_1_7)) {
1000+
/* This is the mode entered in above setup commands */
1001+
dev->mode = ESP_MODE_STA;
1002+
1003+
/*
1004+
* In case of ESP 1.7 this is the first time CWMODE is entered
1005+
* STA mode, so request hostname change now.
1006+
*/
1007+
esp_configure_hostname(dev);
1008+
}
9851009

9861010
LOG_INF("ESP Wi-Fi ready");
9871011

0 commit comments

Comments
 (0)