Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Added hostname configuration to WLAN module #100

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions esp32/mods/modwlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "freertos/task.h"
#include "freertos/event_groups.h"

#include "tcpip_adapter.h"


/******************************************************************************
DEFINE TYPES
******************************************************************************/
Expand Down Expand Up @@ -151,6 +154,7 @@ STATIC void wlan_validate_mode (uint mode);
STATIC void wlan_set_mode (uint mode);
STATIC void wlan_setup_ap (const char *ssid, uint32_t auth, const char *key, uint32_t channel, bool add_mac);
STATIC void wlan_validate_ssid_len (uint32_t len);
STATIC void wlan_validate_hostname_len (uint32_t len);
STATIC uint32_t wlan_set_ssid_internal (const char *ssid, uint8_t len, bool add_mac);
STATIC void wlan_validate_security (uint8_t auth, const char *key);
STATIC void wlan_set_security_internal (uint8_t auth, const char *key);
Expand Down Expand Up @@ -199,7 +203,7 @@ void wlan_pre_init (void) {
wlan_obj.base.type = (mp_obj_t)&mod_network_nic_type_wlan;
}

void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac) {
void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, const char *hostname) {

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
Expand All @@ -216,8 +220,14 @@ void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key,
wlan_set_antenna(antenna);
wlan_set_mode(mode);

// Add hostname

if (mode != WIFI_MODE_STA) {
wlan_setup_ap (ssid, auth, key, channel, add_mac);
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname);
}
else {
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname);
}

esp_wifi_start();
Expand Down Expand Up @@ -394,6 +404,12 @@ STATIC void wlan_validate_ssid_len (uint32_t len) {
}
}

STATIC void wlan_validate_hostname_len (uint32_t len) {
if (len > 16) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
}

STATIC uint32_t wlan_set_ssid_internal (const char *ssid, uint8_t len, bool add_mac) {
// save the ssid
memcpy(&wlan_obj.ssid, ssid, len);
Expand Down Expand Up @@ -655,6 +671,13 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {

wlan_obj.pwrsave = args[5].u_bool;

// get the hostname
const char *hostname = NULL;
if (args[6].u_obj != NULL) {
hostname = mp_obj_str_get_str(args[6].u_obj);
wlan_validate_hostname_len(strlen(hostname));
}

if (mode != WIFI_MODE_STA) {
if (ssid == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "AP SSID not given"));
Expand All @@ -665,7 +688,7 @@ STATIC mp_obj_t wlan_init_helper(wlan_obj_t *self, const mp_arg_val_t *args) {
}

// initialize the wlan subsystem
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false);
wlan_setup(mode, (const char *)ssid, auth, (const char *)key, channel, antenna, false, (const char *)hostname);
mod_network_register_nic(&wlan_obj);

return mp_const_none;
Expand All @@ -679,7 +702,9 @@ STATIC const mp_arg_t wlan_init_args[] = {
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ANTENNA_TYPE_INTERNAL} },
{ MP_QSTR_power_save, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};

STATIC mp_obj_t wlan_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
// parse args
mp_map_t kw_args;
Expand Down
3 changes: 2 additions & 1 deletion esp32/mods/modwlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct _wlan_obj_t {
uint8_t ssid[(MODWLAN_SSID_LEN_MAX + 1)];
uint8_t key[65];
uint8_t mac[6];
uint8_t hostname[16];

// the sssid (or name) and mac of the other device
uint8_t ssid_o[33];
Expand All @@ -74,7 +75,7 @@ extern wlan_obj_t wlan_obj;
DECLARE PUBLIC FUNCTIONS
******************************************************************************/
extern void wlan_pre_init (void);
extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac);
extern void wlan_setup (int32_t mode, const char *ssid, uint32_t auth, const char *key, uint32_t channel, uint32_t antenna, bool add_mac, const char *hostname);
extern void wlan_update(void);
extern void wlan_get_mac (uint8_t *macAddress);
extern void wlan_get_ip (uint32_t *ip);
Expand Down
3 changes: 1 addition & 2 deletions esp32/mptask.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ STATIC void mptask_update_lpwan_mac_address (void) {
#endif

STATIC void mptask_enable_wifi_ap (void) {
wlan_setup (WIFI_MODE_AP, DEFAULT_AP_SSID, WIFI_AUTH_WPA2_PSK, DEFAULT_AP_PASSWORD,
DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true);
wlan_setup (WIFI_MODE_AP, DEFAULT_AP_SSID, WIFI_AUTH_WPA2_PSK, DEFAULT_AP_PASSWORD, DEFAULT_AP_CHANNEL, ANTENNA_TYPE_INTERNAL, true, "illysky");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the illysky default should be changed to something more appropriate to all users

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes! Sorry forgot about that. Perhaps “Pycom” or “MicroPython”.

mod_network_register_nic(&wlan_obj);
}

Expand Down