Skip to content

Commit ef43f46

Browse files
committed
Turn on BT for now
1 parent 8ed2ae4 commit ef43f46

File tree

4 files changed

+104
-90
lines changed

4 files changed

+104
-90
lines changed

ports/zephyr-cp/common-hal/socketpool/SocketPool.c

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "shared-bindings/wifi/__init__.h"
1212
#include "common-hal/socketpool/__init__.h"
1313

14+
#include <zephyr/net/socket.h>
15+
1416
void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *self, mp_obj_t radio) {
1517
if (radio != MP_OBJ_FROM_PTR(&common_hal_wifi_radio_obj)) {
1618
mp_raise_ValueError(MP_ERROR_TEXT("SocketPool can only be used with wifi.radio"));
@@ -20,104 +22,109 @@ void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *sel
2022

2123
// common_hal_socketpool_socket is in socketpool/Socket.c to centralize open socket tracking.
2224

23-
// int socketpool_getaddrinfo_common(const char *host, int service, const struct addrinfo *hints, struct addrinfo **res) {
24-
// // As of 2022, the version of lwip in esp-idf does not handle the
25-
// // trailing-dot syntax of domain names, so emulate it.
26-
// // Remove this once https://github.com/espressif/esp-idf/issues/10013 has
27-
// // been implemented
28-
// if (host) {
29-
// size_t strlen_host = strlen(host);
30-
// if (strlen_host && host[strlen_host - 1] == '.') {
31-
// mp_obj_t nodot = mp_obj_new_str(host, strlen_host - 1);
32-
// host = mp_obj_str_get_str(nodot);
33-
// }
34-
// }
35-
36-
// // char service_buf[6];
37-
// // snprintf(service_buf, sizeof(service_buf), "%d", service);
38-
39-
// // return lwip_getaddrinfo(host, service_buf, hints, res);
40-
// return 0;
41-
// }
42-
43-
// static mp_obj_t format_address(const struct sockaddr *addr, int family) {
44-
// char ip_str[IPADDR_STRLEN_MAX]; // big enough for any supported address type
45-
// const struct sockaddr_in *a = (void *)addr;
46-
47-
// switch (family) {
48-
// #if CIRCUITPY_SOCKETPOOL_IPV6
49-
// case AF_INET6:
50-
// inet_ntop(family, &((const struct sockaddr_in6 *)a)->sin6_addr, ip_str, sizeof(ip_str));
51-
// break;
52-
// #endif
53-
// default:
54-
// case AF_INET:
55-
// inet_ntop(family, &((const struct sockaddr_in *)a)->sin_addr, ip_str, sizeof(ip_str));
56-
// break;
57-
// }
58-
// return mp_obj_new_str(ip_str, strlen(ip_str));
59-
// }
60-
61-
// static mp_obj_t convert_sockaddr(const struct addrinfo *ai, int port) {
62-
// #if CIRCUITPY_SOCKETPOOL_IPV6
63-
// mp_int_t n_tuple = ai->ai_family == AF_INET6 ? 4 : 2;
64-
// #else
65-
// mp_int_t n_tuple = 2;
66-
// #endif
67-
// mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(n_tuple, NULL));
68-
// result->items[0] = format_address(ai->ai_addr, ai->ai_family);
69-
// result->items[1] = MP_OBJ_NEW_SMALL_INT(port);
70-
// #if CIRCUITPY_SOCKETPOOL_IPV6
71-
// if (ai->ai_family == AF_INET6) {
72-
// const struct sockaddr_in6 *ai6 = (void *)ai->ai_addr;
73-
// result->items[2] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_flowinfo);
74-
// result->items[3] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_scope_id);
75-
// }
76-
// #endif
77-
// return result;
78-
// }
79-
80-
// static mp_obj_t convert_addrinfo(const struct addrinfo *ai, int port) {
81-
// MP_STATIC_ASSERT(AF_INET == SOCKETPOOL_AF_INET);
82-
// #if CIRCUITPY_SOCKETPOOL_IPV6
83-
// MP_STATIC_ASSERT(AF_INET6 == SOCKETPOOL_AF_INET6);
84-
// #endif
85-
// // MP_STATIC_ASSERT(AF_UNSPEC == SOCKETPOOL_AF_UNSPEC);
86-
// mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
87-
// result->items[0] = MP_OBJ_NEW_SMALL_INT(ai->ai_family);
88-
// result->items[1] = MP_OBJ_NEW_SMALL_INT(ai->ai_socktype);
89-
// result->items[2] = MP_OBJ_NEW_SMALL_INT(ai->ai_protocol);
90-
// result->items[3] = ai->ai_canonname ? mp_obj_new_str(ai->ai_canonname, strlen(ai->ai_canonname)) : MP_OBJ_NEW_QSTR(MP_QSTR_);
91-
// result->items[4] = convert_sockaddr(ai, port);
92-
// return result;
93-
// }
25+
int socketpool_getaddrinfo_common(const char *host, int service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res) {
26+
// As of 2022, the version of lwip in esp-idf does not handle the
27+
// trailing-dot syntax of domain names, so emulate it.
28+
// Remove this once https://github.com/espressif/esp-idf/issues/10013 has
29+
// been implemented
30+
// if (host) {
31+
// size_t strlen_host = strlen(host);
32+
// if (strlen_host && host[strlen_host - 1] == '.') {
33+
// mp_obj_t nodot = mp_obj_new_str(host, strlen_host - 1);
34+
// host = mp_obj_str_get_str(nodot);
35+
// }
36+
// }
37+
38+
char service_buf[6];
39+
snprintf(service_buf, sizeof(service_buf), "%d", service);
40+
41+
return zsock_getaddrinfo(host, service_buf, hints, res);
42+
}
43+
44+
#if CIRCUITPY_SOCKETPOOL_IPV6
45+
#define IPADDR_STRLEN_MAX INET6_ADDRSTRLEN
46+
#else
47+
#define IPADDR_STRLEN_MAX INET_ADDRSTRLEN
48+
#endif
49+
50+
static mp_obj_t format_address(const struct sockaddr *addr, int family) {
51+
char ip_str[IPADDR_STRLEN_MAX]; // big enough for any supported address type
52+
const struct sockaddr_in *a = (void *)addr;
53+
54+
switch (family) {
55+
#if CIRCUITPY_SOCKETPOOL_IPV6
56+
case AF_INET6:
57+
zsock_inet_ntop(family, &((const struct sockaddr_in6 *)a)->sin6_addr, ip_str, sizeof(ip_str));
58+
break;
59+
#endif
60+
default:
61+
case AF_INET:
62+
zsock_inet_ntop(family, &((const struct sockaddr_in *)a)->sin_addr, ip_str, sizeof(ip_str));
63+
break;
64+
}
65+
return mp_obj_new_str(ip_str, strlen(ip_str));
66+
}
67+
68+
static mp_obj_t convert_sockaddr(const struct zsock_addrinfo *ai, int port) {
69+
#if CIRCUITPY_SOCKETPOOL_IPV6
70+
mp_int_t n_tuple = ai->ai_family == AF_INET6 ? 4 : 2;
71+
#else
72+
mp_int_t n_tuple = 2;
73+
#endif
74+
mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(n_tuple, NULL));
75+
result->items[0] = format_address(ai->ai_addr, ai->ai_family);
76+
result->items[1] = MP_OBJ_NEW_SMALL_INT(port);
77+
#if CIRCUITPY_SOCKETPOOL_IPV6
78+
if (ai->ai_family == AF_INET6) {
79+
const struct sockaddr_in6 *ai6 = (void *)ai->ai_addr;
80+
result->items[2] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_flowinfo);
81+
result->items[3] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_scope_id);
82+
}
83+
#endif
84+
return result;
85+
}
86+
87+
static mp_obj_t convert_addrinfo(const struct addrinfo *ai, int port) {
88+
MP_STATIC_ASSERT(AF_INET == SOCKETPOOL_AF_INET);
89+
#if CIRCUITPY_SOCKETPOOL_IPV6
90+
MP_STATIC_ASSERT(AF_INET6 == SOCKETPOOL_AF_INET6);
91+
#endif
92+
// MP_STATIC_ASSERT(AF_UNSPEC == SOCKETPOOL_AF_UNSPEC);
93+
mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
94+
result->items[0] = MP_OBJ_NEW_SMALL_INT(ai->ai_family);
95+
result->items[1] = MP_OBJ_NEW_SMALL_INT(ai->ai_socktype);
96+
result->items[2] = MP_OBJ_NEW_SMALL_INT(ai->ai_protocol);
97+
result->items[3] = ai->ai_canonname ? mp_obj_new_str(ai->ai_canonname, strlen(ai->ai_canonname)) : MP_OBJ_NEW_QSTR(MP_QSTR_);
98+
result->items[4] = convert_sockaddr(ai, port);
99+
return result;
100+
}
94101

95102
mp_obj_t common_hal_socketpool_getaddrinfo_raise(socketpool_socketpool_obj_t *self, const char *host, int port, int family, int type, int proto, int flags) {
96-
printk("common_hal_socketpool_getaddrinfo_raise\n");
97-
// const struct addrinfo hints = {
98-
// .ai_flags = flags,
99-
// .ai_family = family,
100-
// .ai_protocol = proto,
101-
// .ai_socktype = type,
102-
// };
103-
104-
// struct addrinfo *res = NULL;
105-
// int err = socketpool_getaddrinfo_common(host, port, &hints, &res);
106-
if (true) {
103+
const struct zsock_addrinfo hints = {
104+
.ai_flags = flags,
105+
.ai_family = family,
106+
.ai_protocol = proto,
107+
.ai_socktype = type,
108+
};
109+
110+
struct addrinfo *res = NULL;
111+
int err = socketpool_getaddrinfo_common(host, port, &hints, &res);
112+
if (err != 0 || res == NULL) {
113+
printk("common_hal_socketpool_getaddrinfo_raise\n");
107114
common_hal_socketpool_socketpool_raise_gaierror_noname();
108115
}
109116

110117
nlr_buf_t nlr;
111118
if (nlr_push(&nlr) == 0) {
112119
mp_obj_t result = mp_obj_new_list(0, NULL);
113-
// for (struct addrinfo *ai = res; ai; ai = ai->ai_next) {
114-
// mp_obj_list_append(result, convert_addrinfo(ai, port));
115-
// }
120+
for (struct addrinfo *ai = res; ai; ai = ai->ai_next) {
121+
mp_obj_list_append(result, convert_addrinfo(ai, port));
122+
}
116123
nlr_pop();
117-
// lwip_freeaddrinfo(res);
124+
zsock_freeaddrinfo(res);
118125
return result;
119126
} else {
120-
// lwip_freeaddrinfo(res);
127+
zsock_freeaddrinfo(res);
121128
nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val));
122129
}
123130
}

ports/zephyr-cp/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ CONFIG_WIFI_NM_WPA_SUPPLICANT_LOG_LEVEL_INF=y
3333
CONFIG_WIFI_NM_WPA_SUPPLICANT_THREAD_STACK_SIZE=12288
3434
# For wpa_supplicant use.
3535
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=8192
36-
CONFIG_BT=y

ports/zephyr-cp/supervisor/port.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <zephyr/sys/reboot.h>
1414

1515
#include "lib/tlsf/tlsf.h"
16+
#include "zephyr/bluetooth/bluetooth.h"
1617
#include <zephyr/device.h>
1718

1819
static tlsf_t heap;
@@ -27,6 +28,12 @@ static pool_t pools[CIRCUITPY_RAM_DEVICE_COUNT];
2728
static K_EVENT_DEFINE(main_needed);
2829

2930
safe_mode_t port_init(void) {
31+
32+
int ret = bt_enable(NULL);
33+
if (ret != 0) {
34+
printk("bt enable failed: %d\n", ret);
35+
}
36+
3037
return SAFE_MODE_NONE;
3138
}
3239

shared-bindings/socketpool/enum.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#pragma once
88

9-
// Note: This file must be designed so it be included by ssl
10-
// whether or not CIRCUITPY_SOCKETPOOL is set.
9+
// Note: This file must be designed so it be included by ssl whether or not
10+
// CIRCUITPY_SOCKETPOOL is set. Do NOT change these values because Python level
11+
// network providers may rely on them when interfacing with ssl.
1112
//
1213
typedef enum {
1314
SOCKETPOOL_SOCK_STREAM = 1,

0 commit comments

Comments
 (0)