Skip to content

wifi.radio Access Point modes #4650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 5, 2021
Merged
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
18 changes: 9 additions & 9 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ msgstr ""
msgid "All state machines in use"
msgstr ""

#: ports/atmel-samd/audio_dma.c ports/atmel-samd/common-hal/audiobusio/PDMIn.c
#: ports/atmel-samd/audio_dma.c
msgid "All sync event channels in use"
msgstr ""

Expand Down Expand Up @@ -441,6 +441,10 @@ msgstr ""
msgid "Attempted heap allocation when VM not running."
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "AuthMode.OPEN is not used with password"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Authentication failure"
msgstr ""
Expand Down Expand Up @@ -1193,6 +1197,10 @@ msgstr ""
msgid "Invalid ADC Unit value"
msgstr ""

#: ports/esp32s2/common-hal/wifi/Radio.c
msgid "Invalid AuthMode"
msgstr ""

#: shared-module/displayio/OnDiskBitmap.c
msgid "Invalid BMP file"
msgstr ""
Expand Down Expand Up @@ -2115,14 +2123,6 @@ msgstr ""
msgid "Timeout is too long: Maximum timeout length is %d seconds"
msgstr ""

#: ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c
msgid "Timeout waiting for DRDY"
msgstr ""

#: ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c
msgid "Timeout waiting for VSYNC"
msgstr ""

#: supervisor/shared/safe_mode.c
msgid "To exit, please reset the board without "
msgstr ""
Expand Down
1 change: 0 additions & 1 deletion ports/esp32s2/common-hal/wifi/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include <string.h>

#include "py/enum.h"
#include "shared-bindings/wifi/Network.h"
#include "shared-bindings/wifi/AuthMode.h"

Expand Down
120 changes: 108 additions & 12 deletions ports/esp32s2/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,50 @@
#include "py/runtime.h"
#include "shared-bindings/ipaddress/IPv4Address.h"
#include "shared-bindings/wifi/ScannedNetworks.h"
#include "shared-bindings/wifi/AuthMode.h"
#include "shared-module/ipaddress/__init__.h"

#include "components/esp_wifi/include/esp_wifi.h"
#include "components/lwip/include/apps/ping/ping_sock.h"

#define MAC_ADDRESS_LENGTH 6

static void start_station(wifi_radio_obj_t *self) {
if (self->sta_mode) {
return;
}
static void set_mode_station(wifi_radio_obj_t *self, bool state) {
wifi_mode_t next_mode;
if (self->ap_mode) {
next_mode = WIFI_MODE_APSTA;
if (state) {
if (self->ap_mode) {
next_mode = WIFI_MODE_APSTA;
} else {
next_mode = WIFI_MODE_STA;
}
} else {
next_mode = WIFI_MODE_STA;
if (self->ap_mode) {
next_mode = WIFI_MODE_AP;
} else {
next_mode = WIFI_MODE_NULL;
}
}
esp_wifi_set_mode(next_mode);
self->sta_mode = state;
}

self->sta_mode = 1;
static void set_mode_ap(wifi_radio_obj_t *self, bool state) {
wifi_mode_t next_mode;
if (state) {
if (self->sta_mode) {
next_mode = WIFI_MODE_APSTA;
} else {
next_mode = WIFI_MODE_AP;
}
} else {
if (self->sta_mode) {
next_mode = WIFI_MODE_STA;
} else {
next_mode = WIFI_MODE_NULL;
}
}
esp_wifi_set_mode(next_mode);
self->ap_mode = state;
}

bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self) {
Expand All @@ -71,8 +95,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
return;
}
if (!self->started && enabled) {
// esp_wifi_start() would default to soft-AP, thus setting it to station
start_station(self);
ESP_ERROR_CHECK(esp_wifi_start());
self->started = true;
return;
Expand All @@ -85,14 +107,20 @@ mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH);
}

mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) {
uint8_t mac[MAC_ADDRESS_LENGTH];
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);
return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH);
}

mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) {
if (self->current_scan != NULL) {
mp_raise_RuntimeError(translate("Already scanning for wifi networks"));
}
if (!common_hal_wifi_radio_get_enabled(self)) {
mp_raise_RuntimeError(translate("wifi is not enabled"));
}
start_station(self);
set_mode_station(self, true);

wifi_scannednetworks_obj_t *scan = m_new_obj(wifi_scannednetworks_obj_t);
scan->base.type = &wifi_scannednetworks_type;
Expand Down Expand Up @@ -127,6 +155,50 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host
esp_netif_set_hostname(self->netif, hostname);
}

void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
set_mode_station(self, true);
}

void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
set_mode_station(self, false);
}

void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, uint8_t authmode) {
set_mode_ap(self, true);

switch (authmode) {
case (1 << AUTHMODE_OPEN):
authmode = WIFI_AUTH_OPEN;
break;
case ((1 << AUTHMODE_WPA) | (1 << AUTHMODE_PSK)):
authmode = WIFI_AUTH_WPA_PSK;
break;
case ((1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK)):
authmode = WIFI_AUTH_WPA2_PSK;
break;
case ((1 << AUTHMODE_WPA) | (1 << AUTHMODE_WPA2) | (1 << AUTHMODE_PSK)):
authmode = WIFI_AUTH_WPA_WPA2_PSK;
break;
default:
mp_raise_ValueError(translate("Invalid AuthMode"));
break;
}

wifi_config_t *config = &self->ap_config;
memcpy(&config->ap.ssid, ssid, ssid_len);
config->ap.ssid[ssid_len] = 0;
memcpy(&config->ap.password, password, password_len);
config->ap.password[password_len] = 0;
config->ap.channel = channel;
config->ap.authmode = authmode;
config->ap.max_connection = 4; // kwarg?
esp_wifi_set_config(WIFI_IF_AP, config);
}

void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
set_mode_ap(self, false);
}

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) {
if (!common_hal_wifi_radio_get_enabled(self)) {
mp_raise_RuntimeError(translate("wifi is not enabled"));
Expand All @@ -147,7 +219,7 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
// explicitly clear bits since xEventGroupWaitBits may have timed out
xEventGroupClearBits(self->event_group_handle, WIFI_CONNECTED_BIT);
xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
start_station(self);
set_mode_station(self, true);

wifi_config_t *config = &self->sta_config;
memcpy(&config->sta.ssid, ssid, ssid_len);
Expand Down Expand Up @@ -239,6 +311,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
return common_hal_ipaddress_new_ipv4address(self->ip_info.gw.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->ap_netif)) {
return mp_const_none;
}
esp_netif_get_ip_info(self->ap_netif, &self->ap_ip_info);
return common_hal_ipaddress_new_ipv4address(self->ap_ip_info.gw.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
Expand All @@ -247,6 +327,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
return common_hal_ipaddress_new_ipv4address(self->ip_info.netmask.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->ap_netif)) {
return mp_const_none;
}
esp_netif_get_ip_info(self->ap_netif, &self->ap_ip_info);
return common_hal_ipaddress_new_ipv4address(self->ap_ip_info.netmask.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
Expand All @@ -255,6 +343,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->ap_netif)) {
return mp_const_none;
}
esp_netif_get_ip_info(self->ap_netif, &self->ap_ip_info);
return common_hal_ipaddress_new_ipv4address(self->ap_ip_info.ip.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
Expand Down
4 changes: 4 additions & 0 deletions ports/esp32s2/common-hal/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ typedef struct {
uint8_t retries_left;
uint8_t starting_retries;
uint8_t last_disconnect_reason;

wifi_config_t ap_config;
esp_netif_ip_info_t ap_ip_info;
esp_netif_t *ap_netif;
} wifi_radio_obj_t;

extern void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self);
Expand Down
20 changes: 18 additions & 2 deletions ports/esp32s2/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ static void event_handler(void *arg, esp_event_base_t event_base,
ESP_LOGW(TAG, "scan");
xEventGroupSetBits(radio->event_group_handle, WIFI_SCAN_DONE_BIT);
break;
case WIFI_EVENT_AP_START:
ESP_LOGW(TAG, "ap start");
break;
case WIFI_EVENT_AP_STOP:
ESP_LOGW(TAG, "ap stop");
break;
case WIFI_EVENT_AP_STACONNECTED:
break;
case WIFI_EVENT_AP_STADISCONNECTED:
break;
case WIFI_EVENT_STA_START:
ESP_LOGW(TAG, "start");
ESP_LOGW(TAG, "sta start");
break;
case WIFI_EVENT_STA_STOP:
ESP_LOGW(TAG, "stop");
ESP_LOGW(TAG, "sta stop");
break;
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGW(TAG, "connected");
Expand Down Expand Up @@ -109,6 +119,7 @@ void common_hal_wifi_init(void) {

wifi_radio_obj_t *self = &common_hal_wifi_radio_obj;
self->netif = esp_netif_create_default_wifi_sta();
self->ap_netif = esp_netif_create_default_wifi_ap();
self->started = false;

// Even though we just called esp_netif_create_default_wifi_sta,
Expand Down Expand Up @@ -137,6 +148,9 @@ void common_hal_wifi_init(void) {
} else if (result != ESP_OK) {
mp_raise_RuntimeError(translate("Failed to init wifi"));
}
// set station mode to avoid the default SoftAP
esp_wifi_set_mode(WIFI_MODE_STA);
// start wifi
common_hal_wifi_radio_set_enabled(self, true);
}

Expand All @@ -155,6 +169,8 @@ void wifi_reset(void) {
ESP_ERROR_CHECK(esp_wifi_deinit());
esp_netif_destroy(radio->netif);
radio->netif = NULL;
esp_netif_destroy(radio->ap_netif);
radio->ap_netif = NULL;
}

void ipaddress_ipaddress_to_esp_idf(mp_obj_t ip_address, ip_addr_t *esp_ip_address) {
Expand Down
3 changes: 3 additions & 0 deletions shared-bindings/wifi/AuthMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H

#include "py/enum.h"

typedef enum {
AUTHMODE_OPEN,
AUTHMODE_WEP,
Expand All @@ -38,5 +40,6 @@ typedef enum {
} wifi_authmode_t;

extern const mp_obj_type_t wifi_authmode_type;
extern const cp_enum_obj_t authmode_OPEN_obj;

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_AUTHMODE_H
Loading