Skip to content

Commit 4f0a7ae

Browse files
committed
WIP adding devices.json and auth
1 parent 77cecdb commit 4f0a7ae

File tree

8 files changed

+366
-141
lines changed

8 files changed

+366
-141
lines changed

ports/espressif/boards/espressif_esp32s3_box/mpconfigboard.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ LONGINT_IMPL = MPZ
1313
CFLAGS += -DCFG_TUD_TASK_QUEUE_SZ=32
1414

1515
CIRCUITPY_ESP_FLASH_MODE=dio
16-
CIRCUITPY_ESP_FLASH_FREQ=80m
16+
CIRCUITPY_ESP_FLASH_FREQ=40m
1717
CIRCUITPY_ESP_FLASH_SIZE=16MB

ports/espressif/common-hal/mdns/Server.c

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
STATIC bool inited = false;
3737

38-
void mdns_server_construct(mdns_server_obj_t *self) {
38+
void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
3939
if (inited) {
4040
return;
4141
}
@@ -46,19 +46,21 @@ void mdns_server_construct(mdns_server_obj_t *self) {
4646
snprintf(self->default_hostname, sizeof(self->default_hostname), "cpy-%02x%02x%02x", mac[3], mac[4], mac[5]);
4747
common_hal_mdns_server_set_hostname(self, self->default_hostname);
4848

49-
// Set a delegated entry to ourselves. This allows us to respond to "circuitpython.local"
50-
// queries as well.
51-
// TODO: Allow for disabling this with `supervisor.disable_web_workflow()`.
52-
mdns_ip_addr_t our_ip;
53-
esp_netif_get_ip_info(common_hal_wifi_radio_obj.netif, &common_hal_wifi_radio_obj.ip_info);
54-
our_ip.next = NULL;
55-
our_ip.addr.type = ESP_IPADDR_TYPE_V4;
56-
our_ip.addr.u_addr.ip4 = common_hal_wifi_radio_obj.ip_info.ip;
57-
our_ip.addr.u_addr.ip6.addr[1] = 0;
58-
our_ip.addr.u_addr.ip6.addr[2] = 0;
59-
our_ip.addr.u_addr.ip6.addr[3] = 0;
60-
our_ip.addr.u_addr.ip6.zone = 0;
61-
mdns_delegate_hostname_add("circuitpython", &our_ip);
49+
if (workflow) {
50+
// Set a delegated entry to ourselves. This allows us to respond to "circuitpython.local"
51+
// queries as well.
52+
// TODO: Allow for disabling this with `supervisor.disable_web_workflow()`.
53+
mdns_ip_addr_t our_ip;
54+
esp_netif_get_ip_info(common_hal_wifi_radio_obj.netif, &common_hal_wifi_radio_obj.ip_info);
55+
our_ip.next = NULL;
56+
our_ip.addr.type = ESP_IPADDR_TYPE_V4;
57+
our_ip.addr.u_addr.ip4 = common_hal_wifi_radio_obj.ip_info.ip;
58+
our_ip.addr.u_addr.ip6.addr[1] = 0;
59+
our_ip.addr.u_addr.ip6.addr[2] = 0;
60+
our_ip.addr.u_addr.ip6.addr[3] = 0;
61+
our_ip.addr.u_addr.ip6.zone = 0;
62+
mdns_delegate_hostname_add("circuitpython", &our_ip);
63+
}
6264
}
6365

6466
void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_interface) {
@@ -69,7 +71,7 @@ void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_
6971
if (inited) {
7072
mp_raise_RuntimeError(translate("mDNS already initialized"));
7173
}
72-
mdns_server_construct(self);
74+
mdns_server_construct(self, false);
7375
}
7476

7577
void common_hal_mdns_server_deinit(mdns_server_obj_t *self) {
@@ -104,6 +106,48 @@ void common_hal_mdns_server_set_instance_name(mdns_server_obj_t *self, const cha
104106
self->instance_name = instance_name;
105107
}
106108

109+
size_t mdns_server_find(mdns_server_obj_t *self, const char *service_type, const char *protocol,
110+
mp_float_t timeout, mdns_remoteservice_obj_t *out, size_t out_len) {
111+
mdns_search_once_t *search = mdns_query_async_new(NULL, service_type, protocol, MDNS_TYPE_PTR, timeout * 1000, 255, NULL);
112+
if (search == NULL) {
113+
return 0;
114+
}
115+
mdns_result_t *results;
116+
while (!mdns_query_async_get_results(search, 1, &results)) {
117+
RUN_BACKGROUND_TASKS;
118+
}
119+
mdns_query_async_delete(search);
120+
// Count how many results we got.
121+
// TODO: Remove this loop when moving off 4.4. Newer APIs will give us num_results
122+
// back directly.
123+
mdns_result_t *next = results;
124+
uint8_t num_results = 0;
125+
while (next != NULL) {
126+
num_results++;
127+
next = next->next;
128+
}
129+
130+
next = results;
131+
// Don't error if we're out of memory. Instead, truncate the tuple.
132+
uint8_t added = 0;
133+
while (next != NULL && added < out_len) {
134+
mdns_remoteservice_obj_t *service = &out[added];
135+
136+
service->result = next;
137+
service->base.type = &mdns_remoteservice_type;
138+
next = next->next;
139+
// Break the linked list so we free each result separately.
140+
service->result->next = NULL;
141+
added++;
142+
}
143+
if (added < out_len) {
144+
// Free the remaining results from the IDF because we don't have
145+
// enough space in Python.
146+
mdns_query_results_free(next);
147+
}
148+
return num_results;
149+
}
150+
107151
mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_float_t timeout) {
108152
mdns_search_once_t *search = mdns_query_async_new(NULL, service_type, protocol, MDNS_TYPE_PTR, timeout * 1000, 255, NULL);
109153
if (search == NULL) {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848

4949
#define MAC_ADDRESS_LENGTH 6
5050

51+
#include "esp_log.h"
52+
53+
static const char *TAG = "radio";
54+
5155
static void set_mode_station(wifi_radio_obj_t *self, bool state) {
5256
wifi_mode_t next_mode;
5357
if (state) {
@@ -236,11 +240,12 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
236240
if (!common_hal_wifi_radio_get_enabled(self)) {
237241
mp_raise_RuntimeError(translate("wifi is not enabled"));
238242
}
243+
ESP_LOGI(TAG, "connect");
239244
wifi_config_t *config = &self->sta_config;
240245

241-
size_t timeout_ms = timeout * 1000;
242-
uint32_t start_time = common_hal_time_monotonic_ms();
243-
uint32_t end_time = start_time + timeout_ms;
246+
// size_t timeout_ms = timeout * 1000;
247+
// uint32_t start_time = common_hal_time_monotonic_ms();
248+
// uint32_t end_time = start_time + timeout_ms;
244249

245250
EventBits_t bits;
246251
// can't block since both bits are false after wifi_init
@@ -304,7 +309,9 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
304309
esp_wifi_set_config(ESP_IF_WIFI_STA, config);
305310
self->starting_retries = 5;
306311
self->retries_left = 5;
312+
ESP_LOGI(TAG, "wifi connect");
307313
esp_wifi_connect();
314+
ESP_LOGI(TAG, "wifi connect done");
308315

309316
do {
310317
RUN_BACKGROUND_TASKS;
@@ -314,10 +321,11 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
314321
pdTRUE,
315322
0);
316323
// Don't retry anymore if we're over our time budget.
317-
if (self->retries_left > 0 && common_hal_time_monotonic_ms() > end_time) {
318-
self->retries_left = 0;
319-
}
324+
// if (self->retries_left > 0 && common_hal_time_monotonic_ms() > end_time) {
325+
// self->retries_left = 0;
326+
// }
320327
} while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT)) == 0 && !mp_hal_is_interrupted());
328+
ESP_LOGI(TAG, "connect done");
321329
if ((bits & WIFI_DISCONNECTED_BIT) != 0) {
322330
if (self->last_disconnect_reason == WIFI_REASON_AUTH_FAIL) {
323331
return WIFI_RADIO_ERROR_AUTH_FAIL;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ wifi_radio_obj_t common_hal_wifi_radio_obj;
4444

4545
#include "supervisor/workflow.h"
4646

47-
static const char *TAG = "wifi";
47+
static const char *TAG = "CP wifi";
4848

4949
static void event_handler(void *arg, esp_event_base_t event_base,
5050
int32_t event_id, void *event_data) {

ports/espressif/esp-idf-config/sdkconfig.defaults

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304
297297
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
298298
CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048
299299
CONFIG_ESP_INT_WDT=y
300-
CONFIG_ESP_INT_WDT_TIMEOUT_MS=300
300+
CONFIG_ESP_INT_WDT_TIMEOUT_MS=1000
301301
CONFIG_ESP_INT_WDT_CHECK_CPU1=y
302302
# CONFIG_ESP_TASK_WDT is not set
303303
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
@@ -805,9 +805,9 @@ CONFIG_LOG_BOOTLOADER_LEVEL_INFO=y
805805
CONFIG_LOG_BOOTLOADER_LEVEL=3
806806
# CONFIG_APP_ROLLBACK_ENABLE is not set
807807
# CONFIG_FLASH_ENCRYPTION_ENABLED is not set
808-
# CONFIG_FLASHMODE_QIO is not set
808+
CONFIG_FLASHMODE_QIO=y
809809
# CONFIG_FLASHMODE_QOUT is not set
810-
CONFIG_FLASHMODE_DIO=y
810+
# CONFIG_FLASHMODE_DIO is not set
811811
# CONFIG_FLASHMODE_DOUT is not set
812812
# CONFIG_MONITOR_BAUD_9600B is not set
813813
# CONFIG_MONITOR_BAUD_57600B is not set

shared-bindings/mdns/Server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ MP_PROPERTY_GETSET(mdns_server_instance_name_obj,
136136
//| :param float/int timeout: Time to wait for responses"""
137137
//| ...
138138
//|
139-
STATIC mp_obj_t mdns_server_find(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
139+
STATIC mp_obj_t _mdns_server_find(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
140140
mdns_server_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
141141
check_for_deinit(self);
142142

@@ -156,7 +156,7 @@ STATIC mp_obj_t mdns_server_find(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
156156

157157
return common_hal_mdns_server_find(self, service_type, protocol, timeout);
158158
}
159-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_server_find_obj, 1, mdns_server_find);
159+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mdns_server_find_obj, 1, _mdns_server_find);
160160

161161
//| def advertise_service(self, *, service_type: str, protocol: str, port: int) -> None:
162162
//| """Respond to queries for the given service with the given port.

shared-bindings/mdns/Server.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include "common-hal/mdns/Server.h"
3232

33+
#include "shared-bindings/mdns/RemoteService.h"
34+
3335
extern const mp_obj_type_t mdns_server_type;
3436

3537
void common_hal_mdns_server_construct(mdns_server_obj_t *self, mp_obj_t network_interface);
@@ -43,4 +45,6 @@ mp_obj_t common_hal_mdns_server_find(mdns_server_obj_t *self, const char *servic
4345
void common_hal_mdns_server_advertise_service(mdns_server_obj_t *self, const char *service_type, const char *protocol, mp_int_t port);
4446

4547
// For internal use.
46-
void mdns_server_construct(mdns_server_obj_t *self);
48+
void mdns_server_construct(mdns_server_obj_t *self, bool workflow);
49+
size_t mdns_server_find(mdns_server_obj_t *self, const char *service_type, const char *protocol,
50+
mp_float_t timeout, mdns_remoteservice_obj_t *out, size_t out_len);

0 commit comments

Comments
 (0)