Skip to content

Commit eb2c388

Browse files
committed
HTTP works with my adafruit_requests
1 parent c9ece21 commit eb2c388

File tree

21 files changed

+700
-380
lines changed

21 files changed

+700
-380
lines changed

ports/esp32s2/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
cmake_minimum_required(VERSION 3.13)
44

55
set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)
6-
set(COMPONENTS esptool_py soc driver log main)
6+
7+
# The component list here determines what options we get in menuconfig and what the ninja file
8+
# can build.
9+
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls esp_event esp_netif esp_wifi lwip wpa_supplicant freertos)
710

811
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
912
project(circuitpython)

ports/esp32s2/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ INC += -Iesp-idf/components/esp_rom/include
8585
INC += -Iesp-idf/components/esp_wifi/include
8686
INC += -Iesp-idf/components/xtensa/include
8787
INC += -Iesp-idf/components/esp_timer/include
88+
INC += -Iesp-idf/components/mbedtls/mbedtls/include
89+
INC += -Iesp-idf/components/mbedtls/port/include/
8890
INC += -Iesp-idf/components/newlib/platform_include
8991
INC += -Iesp-idf/components/lwip/lwip/src/include
9092
INC += -Iesp-idf/components/lwip/port/esp32/include
@@ -253,7 +255,7 @@ menuconfig: $(BUILD)/esp-idf/config
253255
$(HEADER_BUILD)/qstr.i.last: | $(BUILD)/esp-idf/config/sdkconfig.h
254256

255257
# Order here matters
256-
ESP_IDF_COMPONENTS_LINK = freertos log esp_system esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf esp_wifi esp_event wpa_supplicant mbedtls efuse nvs_flash esp_netif lwip esp_rom
258+
ESP_IDF_COMPONENTS_LINK = freertos log esp_system esp32s2 bootloader_support pthread esp_timer vfs spi_flash app_update esp_common esp32s2 heap newlib driver xtensa soc esp_ringbuf esp_wifi esp_event wpa_supplicant mbedtls efuse nvs_flash esp_netif lwip esp_rom esp-tls
257259

258260
ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc
259261

@@ -281,6 +283,7 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
281283
$(Q)ninja -C $(BUILD)/esp-idf \
282284
bootloader/bootloader.bin \
283285
esp-idf/bootloader_support/libbootloader_support.a \
286+
esp-idf/esp-tls/libesp-tls.a \
284287
esp-idf/esp32s2/ld/esp32s2.project.ld \
285288
esp-idf/esp_event/libesp_event.a \
286289
esp-idf/esp_netif/libesp_netif.a \

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,57 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26+
27+
#include "shared-bindings/socketpool/Socket.h"
28+
29+
#include "esp_log.h"
30+
static const char *TAG = "socket";
31+
32+
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
33+
self->timeout_ms = timeout_ms;
34+
}
35+
36+
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, mp_uint_t hostlen, mp_int_t port) {
37+
// For simplicity we use esp_tls for all TCP connections. If it's not SSL, ssl_context will be
38+
// NULL and should still work. This makes regular TCP connections more memory expensive but TLS
39+
// should become more and more common. Therefore, we optimize for the TLS case.
40+
41+
ESP_LOGI(TAG, "connecting to %s:%d %p", host, port, self->ssl_context);
42+
int result = esp_tls_conn_new_sync(host, hostlen, port, self->ssl_context, self->tcp);
43+
ESP_LOGI(TAG, "result %d", result);
44+
return result >= 0;
45+
}
46+
47+
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
48+
size_t sent = esp_tls_conn_write(self->tcp, buf, len);
49+
50+
ESP_LOGI(TAG, "sent %d bytes", sent);
51+
if (sent < 0) {
52+
// raise an error
53+
}
54+
return sent;
55+
}
56+
57+
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
58+
size_t received = esp_tls_conn_read(self->tcp, (void*) buf, len);
59+
60+
ESP_LOGI(TAG, "received %d bytes", received);
61+
if (received == 0) {
62+
// socket closed
63+
}
64+
if (received < 0) {
65+
// raise an error
66+
}
67+
return received;
68+
}
69+
70+
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
71+
if (self->tcp != NULL) {
72+
int status = esp_tls_conn_destroy(self->tcp);
73+
74+
if (status < 0) {
75+
// raise an error
76+
}
77+
self->tcp = NULL;
78+
}
79+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,17 @@
2929

3030
#include "py/obj.h"
3131

32+
#include "common-hal/socketpool/SocketPool.h"
33+
34+
#include "esp-idf/components/esp-tls/esp_tls.h"
35+
3236
typedef struct {
3337
mp_obj_base_t base;
38+
int num;
39+
esp_tls_t* tcp;
40+
esp_tls_cfg_t* ssl_context;
41+
socketpool_socketpool_obj_t* pool;
42+
mp_uint_t timeout_ms;
3443
} socketpool_socket_obj_t;
3544

3645
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKET_H

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,77 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26+
27+
#include "shared-bindings/socketpool/SocketPool.h"
28+
29+
#include "py/runtime.h"
30+
31+
#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
32+
33+
socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_t* self,
34+
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
35+
36+
int addr_family;
37+
int ipproto;
38+
if (family == SOCKETPOOL_AF_INET) {
39+
addr_family = AF_INET;
40+
ipproto = IPPROTO_IP;
41+
} else { // INET6
42+
addr_family = AF_INET6;
43+
ipproto = IPPROTO_IPV6;
44+
}
45+
46+
int socket_type;
47+
if (type == SOCKETPOOL_SOCK_STREAM) {
48+
socket_type = SOCK_STREAM;
49+
} else if (type == SOCKETPOOL_SOCK_DGRAM) {
50+
socket_type = SOCK_DGRAM;
51+
} else { // SOCKETPOOL_SOCK_RAW
52+
socket_type = SOCK_RAW;
53+
}
54+
55+
int socknum = -1;
56+
esp_tls_t* tcp_handle = NULL;
57+
if (socket_type == SOCK_DGRAM || socket_type == SOCK_RAW) {
58+
socknum = lwip_socket(addr_family, socket_type, ipproto);
59+
} else {
60+
tcp_handle = esp_tls_init();
61+
}
62+
if (socknum < 0 && tcp_handle == NULL) {
63+
mp_raise_RuntimeError(translate("Out of sockets"));
64+
}
65+
66+
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
67+
sock->base.type = &socketpool_socket_type;
68+
sock->num = socknum;
69+
sock->tcp = tcp_handle;
70+
sock->ssl_context = NULL;
71+
sock->pool = self;
72+
return sock;
73+
}
74+
75+
76+
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t* self,
77+
const char* host) {
78+
79+
const struct addrinfo hints = {
80+
.ai_family = AF_INET,
81+
.ai_socktype = SOCK_STREAM,
82+
};
83+
struct addrinfo *res;
84+
int err = getaddrinfo(host, NULL, &hints, &res);
85+
if (err != 0 || res == NULL) {
86+
return mp_const_none;
87+
}
88+
89+
#pragma GCC diagnostic push
90+
#pragma GCC diagnostic ignored "-Wcast-align"
91+
struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
92+
#pragma GCC diagnostic pop
93+
char ip_str[IP4ADDR_STRLEN_MAX];
94+
inet_ntoa_r(*addr, ip_str, IP4ADDR_STRLEN_MAX);
95+
mp_obj_t ip_obj = mp_obj_new_str(ip_str, strlen(ip_str));
96+
freeaddrinfo(res);
97+
98+
return ip_obj;
99+
}

ports/esp32s2/common-hal/socketpool/SocketPool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKETPOOL_H
2828
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKETPOOL_H
2929

30+
#include "py/obj.h"
31+
3032
typedef struct {
3133
mp_obj_base_t base;
3234
} socketpool_socketpool_obj_t;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/socketpool/SocketPool.h"
28+
29+
#include "py/runtime.h"
30+
31+
#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
32+
33+
socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
34+
socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) {
35+
36+
37+
// Should we store server hostname on the socket in case connect is called with an ip?
38+
return socket;
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLCONTEXT_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLCONTEXT_H
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
esp_tls_cfg_t ssl_config;
35+
} ssl_sslcontext_obj_t;
36+
37+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSL_CONTEXT_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
29+
30+
31+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
29+
30+
31+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H

0 commit comments

Comments
 (0)