Skip to content

Commit 80b15f6

Browse files
committed
Add error handling w/temp debug logs
1 parent eb8b42a commit 80b15f6

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

ports/esp32s2/common-hal/socketpool/Socket.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@
2626

2727
#include "shared-bindings/socketpool/Socket.h"
2828

29+
#include "lib/utils/interrupt_char.h"
2930
#include "py/mperrno.h"
3031
#include "py/runtime.h"
32+
#include "supervisor/shared/tick.h"
33+
34+
#include "esp_log.h"
35+
static const char* TAG = "socket";
3136

3237
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
3338
self->timeout_ms = timeout_ms;
@@ -43,13 +48,15 @@ bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const c
4348
tls_config = &self->ssl_context->ssl_config;
4449
}
4550
int result = esp_tls_conn_new_sync(host, hostlen, port, tls_config, self->tcp);
51+
ESP_EARLY_LOGW(TAG, "connect result %d", result);
4652
self->connected = result >= 0;
4753
if (result < 0) {
4854
int esp_tls_code;
49-
esp_tls_get_and_clear_last_error(self->tcp->error_handle, &esp_tls_code, NULL);
55+
int flags;
56+
esp_err_t err = esp_tls_get_and_clear_last_error(self->tcp->error_handle, &esp_tls_code, &flags);
5057

5158
// mp_raise_espidf_MemoryError
52-
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d"), esp_tls_code);
59+
mp_raise_OSError_msg_varg(translate("Unhandled ESP TLS error %d %d %x"), esp_tls_code, flags, err);
5360
}
5461
return self->connected;
5562
}
@@ -68,19 +75,44 @@ mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const
6875
}
6976

7077
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
71-
size_t received = esp_tls_conn_read(self->tcp, (void*) buf, len);
78+
size_t received = 0;
79+
ssize_t last_read = 1;
80+
uint64_t start_ticks = supervisor_ticks_ms64();
81+
while (received < len &&
82+
last_read > 0 &&
83+
(self->timeout_ms == 0 || supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) &&
84+
!mp_hal_is_interrupted()) {
85+
RUN_BACKGROUND_TASKS;
86+
size_t available = esp_tls_get_bytes_avail(self->tcp);
87+
ESP_EARLY_LOGW(TAG, "available %d", available);
88+
size_t remaining = len - received;
89+
if (available > remaining) {
90+
available = remaining;
91+
}
92+
if (true || available > 0) {
93+
if (available == 0) {
94+
available = len - received;
95+
}
96+
last_read = esp_tls_conn_read(self->tcp, (void*) buf + received, available);
97+
ESP_EARLY_LOGW(TAG, "read %d out of %d", last_read, available);
98+
received += last_read;
99+
}
100+
}
72101

73-
if (received == 0) {
102+
if (last_read == 0) {
74103
// socket closed
104+
ESP_EARLY_LOGW(TAG, "receive close %d %d", received, len);
75105
common_hal_socketpool_socket_close(self);
76106
}
77-
if (received < 0) {
107+
if (last_read < 0) {
108+
// ESP_EARLY_LOGI(TAG, "received %d", received);
78109
mp_raise_BrokenPipeError();
79110
}
80111
return received;
81112
}
82113

83114
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
115+
// ESP_EARLY_LOGW(TAG, "close");
84116
if (self->connected) {
85117
self->connected = false;
86118
}
@@ -95,5 +127,6 @@ void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
95127
}
96128

97129
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self) {
130+
// ESP_EARLY_LOGW(TAG, "tcp %p", self->tcp);
98131
return self->tcp == NULL;
99132
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
3939
#include "esp-idf/components/lwip/include/apps/ping/ping_sock.h"
4040

41+
#include "esp_log.h"
42+
static const char* TAG = "radio";
43+
4144
static void start_station(wifi_radio_obj_t *self) {
4245
if (self->sta_mode) {
4346
return;
@@ -160,6 +163,8 @@ mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address,
160163
uint32_t elapsed_time = 0xffffffff;
161164
if (received > 0) {
162165
esp_ping_get_profile(ping, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
166+
} else {
167+
ESP_EARLY_LOGW(TAG, "received none - time %d timeout %d", total_time_ms, timeout_ms);
163168
}
164169
esp_ping_delete_session(ping);
165170

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
wifi_radio_obj_t common_hal_wifi_radio_obj;
3939

40+
#include "esp_log.h"
41+
static const char* TAG = "wifi";
42+
4043
static void event_handler(void* arg, esp_event_base_t event_base,
4144
int32_t event_id, void* event_data) {
4245
wifi_radio_obj_t* radio = arg;
@@ -46,11 +49,15 @@ static void event_handler(void* arg, esp_event_base_t event_base,
4649
} else if (event_id == WIFI_EVENT_STA_START) {
4750
} else if (event_id == WIFI_EVENT_STA_STOP) {
4851
} else if (event_id == WIFI_EVENT_STA_CONNECTED) {
52+
ESP_EARLY_LOGW(TAG, "connected");
4953
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
50-
// wifi_event_sta_disconnected_t* d = (wifi_event_sta_disconnected_t*) event_data;
51-
if (event_id != WIFI_REASON_ASSOC_LEAVE) {
54+
ESP_EARLY_LOGW(TAG, "disconnected");
55+
wifi_event_sta_disconnected_t* d = (wifi_event_sta_disconnected_t*) event_data;
56+
uint8_t reason = d->reason;
57+
if (reason != WIFI_REASON_ASSOC_LEAVE) {
5258
// reconnect
5359
}
60+
ESP_EARLY_LOGW(TAG, "reason %d 0x%02x", reason, reason);
5461
xEventGroupSetBits(radio->event_group_handle, WIFI_DISCONNECTED_BIT);
5562
} else if (event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
5663
}
@@ -70,6 +77,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
7077
// ESP_LOGI(TAG,"connect to the AP fail");
7178
// } else
7279
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
80+
ESP_EARLY_LOGW(TAG, "got ip");
7381
xEventGroupSetBits(radio->event_group_handle, WIFI_CONNECTED_BIT);
7482
}
7583
}

shared-bindings/socketpool/Socket.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include "py/runtime.h"
3737
#include "py/mperrno.h"
3838

39+
#include "esp_log.h"
40+
static const char* TAG = "socket binding";
41+
3942
//| class Socket:
4043
//| """TCP, UDP and RAW socket. Cannot be created directly. Instead, call
4144
//| `SocketPool.socket()`.
@@ -180,6 +183,7 @@ STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
180183

181184
bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, port);
182185
if (!ok) {
186+
ESP_EARLY_LOGW(TAG, "socket connect failed");
183187
mp_raise_OSError(0);
184188
}
185189

@@ -262,6 +266,11 @@ STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args)
262266
}
263267
}
264268

269+
if (len == 0) {
270+
ESP_EARLY_LOGW(TAG, "len 0");
271+
mp_raise_OSError(0);
272+
}
273+
265274
mp_int_t ret = common_hal_socketpool_socket_recv_into(self, (byte*)bufinfo.buf, len);
266275
return mp_obj_new_int_from_uint(ret);
267276
}

shared-bindings/socketpool/SocketPool.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include "shared-bindings/socketpool/Socket.h"
3838
#include "shared-bindings/socketpool/SocketPool.h"
3939

40+
#include "esp_log.h"
41+
static const char* TAG = "socketpool binding";
42+
4043
//| class SocketPool:
4144
//| """A pool of socket resources available for the given radio. Only one
4245
//| SocketPool can be created for each radio.
@@ -119,6 +122,7 @@ STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t
119122
}
120123

121124
if (ip_str == mp_const_none) {
125+
ESP_EARLY_LOGW(TAG, "no ip str");
122126
mp_raise_OSError(0);
123127
}
124128

shared-bindings/wifi/Radio.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ const mp_obj_property_t wifi_radio_ipv4_address_obj = {
159159
};
160160

161161
//| def ping(self, ip, *, timeout: float = 0.5) -> float:
162-
//| """Ping an IP to test connectivity. Returns echo time in seconds."""
162+
//| """Ping an IP to test connectivity. Returns echo time in seconds.
163+
//| Returns None when it times out."""
163164
//| ...
164165
//|
165166
STATIC mp_obj_t wifi_radio_ping(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@@ -179,6 +180,9 @@ STATIC mp_obj_t wifi_radio_ping(size_t n_args, const mp_obj_t *pos_args, mp_map_
179180
}
180181

181182
mp_int_t time_ms = common_hal_wifi_radio_ping(self, args[ARG_ip].u_obj, timeout);
183+
if (time_ms == -1) {
184+
return mp_const_none;
185+
}
182186

183187
return mp_obj_new_float(time_ms / 1000.0);
184188
}

0 commit comments

Comments
 (0)