circuitpython/ports/espressif/common-hal/wifi/__init__.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

269 lines
9.5 KiB
C
Raw Normal View History

2020-07-28 21:23:33 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "bindings/espidf/__init__.h"
2020-08-11 17:50:37 -04:00
#include "common-hal/wifi/__init__.h"
#include "shared-bindings/wifi/__init__.h"
2020-08-11 17:50:37 -04:00
#include "shared-bindings/ipaddress/IPv4Address.h"
#include "shared-bindings/wifi/Monitor.h"
2020-07-28 21:23:33 -04:00
#include "shared-bindings/wifi/Radio.h"
#include "py/mpstate.h"
2020-07-28 21:23:33 -04:00
#include "py/runtime.h"
2020-09-28 18:59:29 -04:00
#include "components/esp_wifi/include/esp_wifi.h"
2020-07-28 21:23:33 -04:00
2020-09-28 18:59:29 -04:00
#include "components/heap/include/esp_heap_caps.h"
2020-07-28 21:23:33 -04:00
wifi_radio_obj_t common_hal_wifi_radio_obj;
2020-09-28 18:59:29 -04:00
#include "components/log/include/esp_log.h"
#include "supervisor/port.h"
#include "supervisor/workflow.h"
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
#include "supervisor/shared/status_bar.h"
#endif
#include "esp_ipc.h"
#ifdef CONFIG_IDF_TARGET_ESP32
#include "nvs_flash.h"
#endif
2022-06-16 20:03:52 -04:00
static const char *TAG = "CP wifi";
2020-08-19 20:51:33 -04:00
STATIC void schedule_background_on_cp_core(void *arg) {
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
supervisor_status_bar_request_update(false);
#endif
// CircuitPython's VM is run in a separate FreeRTOS task from wifi callbacks. So, we have to
// notify the main task every time in case it's waiting for us.
port_wake_main_task();
}
2021-03-15 09:57:36 -04:00
static void event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
// This runs on the PRO CORE! It cannot share CP interrupt enable/disable
// directly.
2021-03-15 09:57:36 -04:00
wifi_radio_obj_t *radio = arg;
2020-08-05 15:53:35 -04:00
if (event_base == WIFI_EVENT) {
2020-09-03 19:32:12 -04:00
switch (event_id) {
case WIFI_EVENT_SCAN_DONE:
ESP_LOGW(TAG, "scan");
2020-09-03 19:32:12 -04:00
xEventGroupSetBits(radio->event_group_handle, WIFI_SCAN_DONE_BIT);
break;
2021-04-22 23:21:50 -04:00
case WIFI_EVENT_AP_START:
ESP_LOGW(TAG, "ap start");
break;
case WIFI_EVENT_AP_STOP:
ESP_LOGW(TAG, "ap stop");
break;
2021-04-26 01:22:51 -04:00
case WIFI_EVENT_AP_STACONNECTED:
2021-04-22 23:21:50 -04:00
break;
2021-04-26 01:22:51 -04:00
case WIFI_EVENT_AP_STADISCONNECTED:
2021-04-22 23:21:50 -04:00
break;
2020-12-06 17:38:04 -05:00
case WIFI_EVENT_STA_START:
2021-04-22 23:21:50 -04:00
ESP_LOGW(TAG, "sta start");
2020-12-06 17:38:04 -05:00
break;
case WIFI_EVENT_STA_STOP:
2021-04-22 23:21:50 -04:00
ESP_LOGW(TAG, "sta stop");
2020-12-06 17:38:04 -05:00
break;
2020-09-03 19:32:12 -04:00
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGW(TAG, "connected");
2020-09-03 19:32:12 -04:00
break;
case WIFI_EVENT_STA_DISCONNECTED: {
ESP_LOGW(TAG, "disconnected");
2021-03-15 09:57:36 -04:00
wifi_event_sta_disconnected_t *d = (wifi_event_sta_disconnected_t *)event_data;
2020-09-03 19:32:12 -04:00
uint8_t reason = d->reason;
ESP_LOGW(TAG, "reason %d 0x%02x", reason, reason);
2020-09-03 19:32:12 -04:00
if (radio->retries_left > 0 &&
2021-03-15 09:57:36 -04:00
reason != WIFI_REASON_AUTH_FAIL &&
reason != WIFI_REASON_NO_AP_FOUND &&
reason != WIFI_REASON_ASSOC_LEAVE) {
2020-09-03 19:32:12 -04:00
radio->retries_left--;
ESP_LOGI(TAG, "Retrying connect. %d retries remaining", radio->retries_left);
2020-09-03 19:32:12 -04:00
esp_wifi_connect();
return;
}
radio->last_disconnect_reason = reason;
xEventGroupSetBits(radio->event_group_handle, WIFI_DISCONNECTED_BIT);
2020-12-30 10:45:15 -05:00
break;
2020-08-21 14:00:02 -04:00
}
2020-09-03 19:32:12 -04:00
// Cases to handle later.
// case WIFI_EVENT_STA_AUTHMODE_CHANGE:
2020-12-30 10:45:15 -05:00
default: {
ESP_LOGW(TAG, "event %d 0x%02x", event_id, event_id);
2020-09-03 19:32:12 -04:00
break;
2020-12-30 10:45:15 -05:00
}
2020-08-05 15:53:35 -04:00
}
}
2020-08-05 15:53:35 -04:00
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ESP_LOGW(TAG, "got ip");
2020-08-21 14:00:02 -04:00
radio->retries_left = radio->starting_retries;
2020-08-05 15:53:35 -04:00
xEventGroupSetBits(radio->event_group_handle, WIFI_CONNECTED_BIT);
}
// Use IPC to ensure we run schedule background on the same core as CircuitPython.
#if defined(CONFIG_FREERTOS_UNICORE) && CONFIG_FREERTOS_UNICORE
schedule_background_on_cp_core(NULL);
#else
// This only blocks until the start of the function. That's ok since the PRO
// core shouldn't care what we do.
esp_ipc_call(CONFIG_ESP_MAIN_TASK_AFFINITY, schedule_background_on_cp_core, NULL);
#endif
}
static bool wifi_inited;
static bool wifi_ever_inited;
static bool wifi_user_initiated;
void common_hal_wifi_init(bool user_initiated) {
wifi_radio_obj_t *self = &common_hal_wifi_radio_obj;
if (wifi_inited) {
if (user_initiated && !wifi_user_initiated) {
common_hal_wifi_radio_set_enabled(self, true);
}
return;
}
wifi_inited = true;
wifi_user_initiated = user_initiated;
2020-09-03 19:32:12 -04:00
common_hal_wifi_radio_obj.base.type = &wifi_radio_type;
2020-07-28 21:23:33 -04:00
if (!wifi_ever_inited) {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
}
wifi_ever_inited = true;
self->netif = esp_netif_create_default_wifi_sta();
2021-04-22 23:21:50 -04:00
self->ap_netif = esp_netif_create_default_wifi_ap();
2021-03-07 18:23:14 -05:00
self->started = false;
2020-10-20 17:49:57 -04:00
// Even though we just called esp_netif_create_default_wifi_sta,
2020-10-20 18:14:35 -04:00
// station mode isn't actually ready for use until esp_wifi_set_mode()
2020-10-20 17:49:57 -04:00
// is called and the configuration is loaded via esp_wifi_set_config().
// Set both convenience flags to false so it's not forgotten.
2020-10-20 17:49:57 -04:00
self->sta_mode = 0;
self->ap_mode = 0;
self->event_group_handle = xEventGroupCreateStatic(&self->event_group);
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
2021-03-15 09:57:36 -04:00
ESP_EVENT_ANY_ID,
&event_handler,
self,
&self->handler_instance_all_wifi));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
2021-03-15 09:57:36 -04:00
IP_EVENT_STA_GOT_IP,
&event_handler,
self,
&self->handler_instance_got_ip));
2020-09-03 19:32:12 -04:00
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
#ifdef CONFIG_IDF_TARGET_ESP32
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
#endif
2020-09-03 19:32:12 -04:00
esp_err_t result = esp_wifi_init(&config);
if (result == ESP_ERR_NO_MEM) {
mp_raise_msg(&mp_type_MemoryError, translate("Failed to allocate Wifi memory"));
} else if (result != ESP_OK) {
raise_esp_error(result);
2020-09-03 19:32:12 -04:00
}
// set station mode to avoid the default SoftAP
2021-11-12 17:27:13 -05:00
common_hal_wifi_radio_start_station(self);
// start wifi
common_hal_wifi_radio_set_enabled(self, true);
2020-07-28 21:23:33 -04:00
}
void wifi_user_reset(void) {
if (wifi_user_initiated) {
wifi_reset();
wifi_user_initiated = false;
}
}
2020-07-28 21:23:33 -04:00
void wifi_reset(void) {
if (!wifi_inited) {
return;
}
common_hal_wifi_monitor_deinit(MP_STATE_VM(wifi_monitor_singleton));
2021-03-15 09:57:36 -04:00
wifi_radio_obj_t *radio = &common_hal_wifi_radio_obj;
2020-09-03 19:32:12 -04:00
common_hal_wifi_radio_set_enabled(radio, false);
#ifndef CONFIG_IDF_TARGET_ESP32
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT,
2021-03-15 09:57:36 -04:00
ESP_EVENT_ANY_ID,
radio->handler_instance_all_wifi));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT,
2021-03-15 09:57:36 -04:00
IP_EVENT_STA_GOT_IP,
radio->handler_instance_got_ip));
2020-09-03 19:32:12 -04:00
ESP_ERROR_CHECK(esp_wifi_deinit());
esp_netif_destroy(radio->netif);
radio->netif = NULL;
2021-04-22 23:21:50 -04:00
esp_netif_destroy(radio->ap_netif);
radio->ap_netif = NULL;
wifi_inited = false;
#endif
supervisor_workflow_request_background();
2020-07-28 21:23:33 -04:00
}
2020-08-11 17:50:37 -04:00
2021-03-15 09:57:36 -04:00
void ipaddress_ipaddress_to_esp_idf(mp_obj_t ip_address, ip_addr_t *esp_ip_address) {
if (!mp_obj_is_type(ip_address, &ipaddress_ipv4address_type)) {
2020-08-11 17:50:37 -04:00
mp_raise_ValueError(translate("Only IPv4 addresses supported"));
}
mp_obj_t packed = common_hal_ipaddress_ipv4address_get_packed(ip_address);
size_t len;
2021-03-15 09:57:36 -04:00
const char *bytes = mp_obj_str_get_data(packed, &len);
2020-08-11 17:50:37 -04:00
IP_ADDR4(esp_ip_address, bytes[0], bytes[1], bytes[2], bytes[3]);
}
2020-12-07 11:06:33 -05:00
2022-05-27 16:33:54 -04:00
void ipaddress_ipaddress_to_esp_idf_ip4(mp_obj_t ip_address, esp_ip4_addr_t *esp_ip_address) {
if (!mp_obj_is_type(ip_address, &ipaddress_ipv4address_type)) {
mp_raise_ValueError(translate("Only IPv4 addresses supported"));
}
mp_obj_t packed = common_hal_ipaddress_ipv4address_get_packed(ip_address);
size_t len;
const char *bytes = mp_obj_str_get_data(packed, &len);
esp_netif_set_ip4_addr(esp_ip_address, bytes[0], bytes[1], bytes[2], bytes[3]);
}
2020-12-07 11:06:33 -05:00
void common_hal_wifi_gc_collect(void) {
common_hal_wifi_radio_gc_collect(&common_hal_wifi_radio_obj);
}