Merge pull request #3547 from microDev1/wifiHost

Add method to set custom hostname
This commit is contained in:
Scott Shawcroft 2020-10-15 11:17:07 -07:00 committed by GitHub
commit b67acea587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 6 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-10 23:49-0700\n"
"POT-Creation-Date: 2020-10-15 16:06+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -947,6 +947,10 @@ msgstr ""
msgid "Hardware in use, try alternative pins"
msgstr ""
#: shared-bindings/wifi/Radio.c
msgid "Hostname must be between 1 and 253 characters"
msgstr ""
#: extmod/vfs_posix_file.c py/objstringio.c
msgid "I/O operation on closed file"
msgstr ""
@ -1026,6 +1030,7 @@ msgstr ""
msgid "Invalid BSSID"
msgstr ""
#: ports/esp32s2/common-hal/analogio/AnalogOut.c
#: ports/stm/common-hal/analogio/AnalogOut.c
msgid "Invalid DAC pin supplied"
msgstr ""
@ -1237,7 +1242,6 @@ msgid "No CCCD for this Characteristic"
msgstr ""
#: ports/atmel-samd/common-hal/analogio/AnalogOut.c
#: ports/esp32s2/common-hal/analogio/AnalogOut.c
#: ports/stm/common-hal/analogio/AnalogOut.c
msgid "No DAC on chip"
msgstr ""
@ -2728,6 +2732,10 @@ msgstr ""
msgid "invalid format specifier"
msgstr ""
#: shared-bindings/wifi/Radio.c
msgid "invalid hostname"
msgstr ""
#: extmod/modussl_axtls.c
msgid "invalid key"
msgstr ""

View File

@ -32,3 +32,8 @@ CONFIG_SPIRAM_MEMTEST=y
# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set
# end of SPI RAM config
#
# LWIP
#
CONFIG_LWIP_LOCAL_HOSTNAME="microS2"
# end of LWIP

View File

@ -104,6 +104,19 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
self->current_scan = NULL;
}
mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self) {
const char *hostname = NULL;
esp_netif_get_hostname(self->netif, &hostname);
if (hostname == NULL) {
return mp_const_none;
}
return mp_obj_new_str(hostname, strlen(hostname));
}
void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname) {
esp_netif_set_hostname(self->netif, hostname);
}
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) {
// check enabled
start_station(self);

View File

@ -24,11 +24,13 @@
* THE SOFTWARE.
*/
#include "shared-bindings/wifi/__init__.h"
#include <regex.h>
#include <string.h>
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/wifi/__init__.h"
#include "py/objproperty.h"
//| class Radio:
//| """Native wifi radio.
@ -50,7 +52,6 @@
//|
STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) {
return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled);
@ -102,6 +103,45 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
//| hostname: ReadableBuffer
//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
//| the changes would only be reflected once the interface restarts/reconnects."""
//|
STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return common_hal_wifi_radio_get_hostname(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
mp_buffer_info_t hostname;
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
if (hostname.len < 1 || hostname.len > 253) {
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
}
regex_t regex; //validate hostname according to RFC 1123
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);
if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
mp_raise_ValueError(translate("invalid hostname"));
}
regfree(&regex);
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_hostname(self, hostname.buf);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
const mp_obj_property_t wifi_radio_hostname_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
(mp_obj_t)&wifi_radio_set_hostname_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| def connect(self, ssid: ReadableBuffer, password: ReadableBuffer = b"", *, channel: Optional[int] = 0, timeout: Optional[float] = None) -> bool:
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
//| automatically once one connection succeeds."""
@ -216,6 +256,8 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },

View File

@ -35,7 +35,6 @@
const mp_obj_type_t wifi_radio_type;
typedef enum {
WIFI_RADIO_ERROR_NONE,
WIFI_RADIO_ERROR_UNKNOWN,
@ -46,6 +45,9 @@ typedef enum {
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled);
extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);
extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);