Skip to content

Commit b67acea

Browse files
authored
Merge pull request #3547 from microDev1/wifiHost
Add method to set custom hostname
2 parents f1e8f2b + 26fd2c6 commit b67acea

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

locale/circuitpython.pot

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-10-10 23:49-0700\n"
11+
"POT-Creation-Date: 2020-10-15 16:06+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -947,6 +947,10 @@ msgstr ""
947947
msgid "Hardware in use, try alternative pins"
948948
msgstr ""
949949

950+
#: shared-bindings/wifi/Radio.c
951+
msgid "Hostname must be between 1 and 253 characters"
952+
msgstr ""
953+
950954
#: extmod/vfs_posix_file.c py/objstringio.c
951955
msgid "I/O operation on closed file"
952956
msgstr ""
@@ -1026,6 +1030,7 @@ msgstr ""
10261030
msgid "Invalid BSSID"
10271031
msgstr ""
10281032

1033+
#: ports/esp32s2/common-hal/analogio/AnalogOut.c
10291034
#: ports/stm/common-hal/analogio/AnalogOut.c
10301035
msgid "Invalid DAC pin supplied"
10311036
msgstr ""
@@ -1237,7 +1242,6 @@ msgid "No CCCD for this Characteristic"
12371242
msgstr ""
12381243

12391244
#: ports/atmel-samd/common-hal/analogio/AnalogOut.c
1240-
#: ports/esp32s2/common-hal/analogio/AnalogOut.c
12411245
#: ports/stm/common-hal/analogio/AnalogOut.c
12421246
msgid "No DAC on chip"
12431247
msgstr ""
@@ -2728,6 +2732,10 @@ msgstr ""
27282732
msgid "invalid format specifier"
27292733
msgstr ""
27302734

2735+
#: shared-bindings/wifi/Radio.c
2736+
msgid "invalid hostname"
2737+
msgstr ""
2738+
27312739
#: extmod/modussl_axtls.c
27322740
msgid "invalid key"
27332741
msgstr ""

ports/esp32s2/boards/microdev_micro_s2/sdkconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ CONFIG_SPIRAM_MEMTEST=y
3232
# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set
3333
# end of SPI RAM config
3434

35+
#
36+
# LWIP
37+
#
38+
CONFIG_LWIP_LOCAL_HOSTNAME="microS2"
39+
# end of LWIP

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ 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+
116+
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
117+
esp_netif_set_hostname(self->netif, hostname);
118+
}
119+
107120
wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len) {
108121
// check enabled
109122
start_station(self);

shared-bindings/wifi/Radio.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "shared-bindings/wifi/__init__.h"
28+
29+
#include <regex.h>
2730
#include <string.h>
2831

29-
#include "py/objproperty.h"
3032
#include "py/runtime.h"
31-
#include "shared-bindings/wifi/__init__.h"
33+
#include "py/objproperty.h"
3234

3335
//| class Radio:
3436
//| """Native wifi radio.
@@ -50,7 +52,6 @@
5052
//|
5153
STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) {
5254
return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self));
53-
5455
}
5556
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled);
5657

@@ -102,6 +103,45 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
102103
}
103104
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
104105

106+
//| hostname: ReadableBuffer
107+
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
108+
//| the changes would only be reflected once the interface restarts/reconnects."""
109+
//|
110+
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
111+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
112+
return common_hal_wifi_radio_get_hostname(self);
113+
}
114+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
115+
116+
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
117+
mp_buffer_info_t hostname;
118+
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
119+
120+
if (hostname.len < 1 || hostname.len > 253) {
121+
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
122+
}
123+
124+
regex_t regex; //validate hostname according to RFC 1123
125+
regcomp(&regex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB);
126+
if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
127+
mp_raise_ValueError(translate("invalid hostname"));
128+
}
129+
regfree(&regex);
130+
131+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
132+
common_hal_wifi_radio_set_hostname(self, hostname.buf);
133+
134+
return mp_const_none;
135+
}
136+
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
137+
138+
const mp_obj_property_t wifi_radio_hostname_obj = {
139+
.base.type = &mp_type_property,
140+
.proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
141+
(mp_obj_t)&wifi_radio_set_hostname_obj,
142+
(mp_obj_t)&mp_const_none_obj},
143+
};
144+
105145
//| def connect(self, ssid: ReadableBuffer, password: ReadableBuffer = b"", *, channel: Optional[int] = 0, timeout: Optional[float] = None) -> bool:
106146
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
107147
//| automatically once one connection succeeds."""
@@ -216,6 +256,8 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
216256
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
217257
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
218258

259+
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
260+
219261
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
220262
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
221263

shared-bindings/wifi/Radio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
const mp_obj_type_t wifi_radio_type;
3737

38-
3938
typedef enum {
4039
WIFI_RADIO_ERROR_NONE,
4140
WIFI_RADIO_ERROR_UNKNOWN,
@@ -46,6 +45,9 @@ typedef enum {
4645
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);
4746
extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled);
4847

48+
extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
49+
extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);
50+
4951
extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
5052

5153
extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);

0 commit comments

Comments
 (0)