Skip to content

Commit 18fbff4

Browse files
committed
Update wifi hostname method
1 parent ceb5310 commit 18fbff4

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

ports/esp32s2/common-hal/wifi/Radio.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
104104
self->current_scan = NULL;
105105
}
106106

107+
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
108+
const char *hostname = NULL;
109+
esp_netif_get_hostname(self->netif, &hostname);
110+
if (hostname == NULL) {
111+
return mp_const_none;
112+
}
113+
return mp_obj_new_str(hostname, strlen(hostname));
114+
}
115+
107116
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
108117
esp_netif_set_hostname(self->netif, hostname);
109118
}

shared-bindings/wifi/Radio.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
//|
5151
STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) {
5252
return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self));
53-
5453
}
5554
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled);
5655

@@ -102,11 +101,16 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
102101
}
103102
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
104103

105-
//| def set_hostname(self, hostname: ReadableBuffer) -> None:
106-
//| """Sets hostname for wifi interface. When the hostname is altered after interface started/connected
107-
//| the changes would only be reflected once the interface restarts/reconnects."""
108-
//| ...
104+
//| hostname: ReadableBuffer
105+
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
106+
//| the changes would only be reflected once the interface restarts/reconnects."""
109107
//|
108+
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
109+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
110+
return common_hal_wifi_radio_get_hostname(self);
111+
}
112+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
113+
110114
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
111115
mp_buffer_info_t hostname;
112116
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
@@ -122,6 +126,13 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in)
122126
}
123127
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
124128

129+
const mp_obj_property_t wifi_radio_hostname_obj = {
130+
.base.type = &mp_type_property,
131+
.proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
132+
(mp_obj_t)&wifi_radio_set_hostname_obj,
133+
(mp_obj_t)&mp_const_none_obj},
134+
};
135+
125136
//| def connect(self, ssid: ReadableBuffer, password: ReadableBuffer = b"", *, channel: Optional[int] = 0, timeout: Optional[float] = None) -> bool:
126137
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
127138
//| automatically once one connection succeeds."""
@@ -236,7 +247,7 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
236247
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
237248
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
238249

239-
{ MP_ROM_QSTR(MP_QSTR_set_hostname), MP_ROM_PTR(&wifi_radio_set_hostname_obj) },
250+
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
240251

241252
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
242253
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },

shared-bindings/wifi/Radio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ typedef enum {
4545
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);
4646
extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled);
4747

48+
extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
4849
extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);
4950

5051
extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);

0 commit comments

Comments
 (0)