HTTP works with my adafruit_requests

This commit is contained in:
Scott Shawcroft 2020-08-12 16:57:39 -07:00
parent c9ece21c28
commit eb2c38825e
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
21 changed files with 680 additions and 360 deletions

View File

@ -3,7 +3,10 @@
cmake_minimum_required(VERSION 3.13)
set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)
set(COMPONENTS esptool_py soc driver log main)
# The component list here determines what options we get in menuconfig and what the ninja file
# can build.
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls esp_event esp_netif esp_wifi lwip wpa_supplicant freertos)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(circuitpython)

View File

@ -85,6 +85,8 @@ INC += -Iesp-idf/components/esp_rom/include
INC += -Iesp-idf/components/esp_wifi/include
INC += -Iesp-idf/components/xtensa/include
INC += -Iesp-idf/components/esp_timer/include
INC += -Iesp-idf/components/mbedtls/mbedtls/include
INC += -Iesp-idf/components/mbedtls/port/include/
INC += -Iesp-idf/components/newlib/platform_include
INC += -Iesp-idf/components/lwip/lwip/src/include
INC += -Iesp-idf/components/lwip/port/esp32/include
@ -253,7 +255,7 @@ menuconfig: $(BUILD)/esp-idf/config
$(HEADER_BUILD)/qstr.i.last: | $(BUILD)/esp-idf/config/sdkconfig.h
# Order here matters
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_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
ESP_IDF_COMPONENTS_INCLUDE = driver freertos log soc
@ -281,6 +283,7 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
$(Q)ninja -C $(BUILD)/esp-idf \
bootloader/bootloader.bin \
esp-idf/bootloader_support/libbootloader_support.a \
esp-idf/esp-tls/libesp-tls.a \
esp-idf/esp32s2/ld/esp32s2.project.ld \
esp-idf/esp_event/libesp_event.a \
esp-idf/esp_netif/libesp_netif.a \

View File

@ -23,3 +23,57 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/socketpool/Socket.h"
#include "esp_log.h"
static const char *TAG = "socket";
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
self->timeout_ms = timeout_ms;
}
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, mp_uint_t hostlen, mp_int_t port) {
// For simplicity we use esp_tls for all TCP connections. If it's not SSL, ssl_context will be
// NULL and should still work. This makes regular TCP connections more memory expensive but TLS
// should become more and more common. Therefore, we optimize for the TLS case.
ESP_LOGI(TAG, "connecting to %s:%d %p", host, port, self->ssl_context);
int result = esp_tls_conn_new_sync(host, hostlen, port, self->ssl_context, self->tcp);
ESP_LOGI(TAG, "result %d", result);
return result >= 0;
}
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
size_t sent = esp_tls_conn_write(self->tcp, buf, len);
ESP_LOGI(TAG, "sent %d bytes", sent);
if (sent < 0) {
// raise an error
}
return sent;
}
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len) {
size_t received = esp_tls_conn_read(self->tcp, (void*) buf, len);
ESP_LOGI(TAG, "received %d bytes", received);
if (received == 0) {
// socket closed
}
if (received < 0) {
// raise an error
}
return received;
}
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
if (self->tcp != NULL) {
int status = esp_tls_conn_destroy(self->tcp);
if (status < 0) {
// raise an error
}
self->tcp = NULL;
}
}

View File

@ -29,8 +29,17 @@
#include "py/obj.h"
#include "common-hal/socketpool/SocketPool.h"
#include "esp-idf/components/esp-tls/esp_tls.h"
typedef struct {
mp_obj_base_t base;
int num;
esp_tls_t* tcp;
esp_tls_cfg_t* ssl_context;
socketpool_socketpool_obj_t* pool;
mp_uint_t timeout_ms;
} socketpool_socket_obj_t;
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKET_H

View File

@ -23,3 +23,77 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "shared-bindings/socketpool/SocketPool.h"
#include "py/runtime.h"
#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_t* self,
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
int addr_family;
int ipproto;
if (family == SOCKETPOOL_AF_INET) {
addr_family = AF_INET;
ipproto = IPPROTO_IP;
} else { // INET6
addr_family = AF_INET6;
ipproto = IPPROTO_IPV6;
}
int socket_type;
if (type == SOCKETPOOL_SOCK_STREAM) {
socket_type = SOCK_STREAM;
} else if (type == SOCKETPOOL_SOCK_DGRAM) {
socket_type = SOCK_DGRAM;
} else { // SOCKETPOOL_SOCK_RAW
socket_type = SOCK_RAW;
}
int socknum = -1;
esp_tls_t* tcp_handle = NULL;
if (socket_type == SOCK_DGRAM || socket_type == SOCK_RAW) {
socknum = lwip_socket(addr_family, socket_type, ipproto);
} else {
tcp_handle = esp_tls_init();
}
if (socknum < 0 && tcp_handle == NULL) {
mp_raise_RuntimeError(translate("Out of sockets"));
}
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
sock->base.type = &socketpool_socket_type;
sock->num = socknum;
sock->tcp = tcp_handle;
sock->ssl_context = NULL;
sock->pool = self;
return sock;
}
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t* self,
const char* host) {
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
int err = getaddrinfo(host, NULL, &hints, &res);
if (err != 0 || res == NULL) {
return mp_const_none;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
#pragma GCC diagnostic pop
char ip_str[IP4ADDR_STRLEN_MAX];
inet_ntoa_r(*addr, ip_str, IP4ADDR_STRLEN_MAX);
mp_obj_t ip_obj = mp_obj_new_str(ip_str, strlen(ip_str));
freeaddrinfo(res);
return ip_obj;
}

View File

@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKETPOOL_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL_SOCKETPOOL_H
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
} socketpool_socketpool_obj_t;

View File

@ -0,0 +1,39 @@
/*
* 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 "shared-bindings/socketpool/SocketPool.h"
#include "py/runtime.h"
#include "esp-idf/components/lwip/lwip/src/include/lwip/netdb.h"
socketpool_socket_obj_t* common_hal_ssl_sslcontext_wrap_socket(ssl_sslcontext_obj_t* self,
socketpool_socket_obj_t* socket, bool server_side, const char* server_hostname) {
// Should we store server hostname on the socket in case connect is called with an ip?
return socket;
}

View File

@ -0,0 +1,37 @@
/*
* 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.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLCONTEXT_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSLCONTEXT_H
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
esp_tls_cfg_t ssl_config;
} ssl_sslcontext_obj_t;
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SSL_SSL_CONTEXT_H

View File

@ -0,0 +1,31 @@
/*
* 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.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H

View File

@ -0,0 +1,31 @@
/*
* 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.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_SOCKETPOOL___INIT___H

View File

@ -113,12 +113,12 @@ void common_hal_wifi_init(void) {
ESP_EVENT_ANY_ID,
&event_handler,
self,
self->handler_instance_all_wifi));
&self->handler_instance_all_wifi));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
self,
self->handler_instance_got_ip));
&self->handler_instance_got_ip));
ESP_EARLY_LOGI(TAG, "wifi init");

@ -1 +1 @@
Subproject commit 01c86ed7d6f4bbbaeda0c461c5f20e3d9872b44b
Subproject commit 83595f76591aebf0cca9a578258276e67f628b06

View File

@ -13,22 +13,17 @@ USB_SERIAL_NUMBER_LENGTH = 12
LONGINT_IMPL = MPZ
# These modules are implemented in ports/<port>/common-hal:
CIRCUITPY_FULL_BUILD = 1
CIRCUITPY_ANALOGIO = 0
CIRCUITPY_NVM = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_COUNTIO = 0
# These modules are implemented in shared-module/ - they can be included in
# any port once their prerequisites in common-hal are complete.
CIRCUITPY_RANDOM = 0 # Requires OS
CIRCUITPY_USB_MIDI = 0 # Requires USB
CIRCUITPY_ULAB = 0 # No requirements, but takes extra flash
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_NVM = 0
CIRCUITPY_USB_MIDI = 0 # We don't have enough endpoints to include MIDI.
CIRCUITPY_WIFI = 1
CIRCUITPY_MODULE ?= none

View File

@ -3,8 +3,7 @@
# bootloader.bin 0x1000
# partition table 0x8000, 0xC00
otadata, data, ota, 0xd000, 0x2000,
ota_0, 0, ota_0, 0x10000, 0x100000,
ota_1, 0, ota_1, 0x110000, 0x100000,
phy_init, data, phy, 0x210000, 0x1000,
nvs, data, nvs, 0x211000, 0x6000,
user_fs, data, fat, 0x217000, 0x1e9000,
ota_0, 0, ota_0, 0x10000, 0x120000,
ota_1, 0, ota_1, 0x130000, 0x120000,
phy_init, data, phy, 0x250000, 0x1000,
user_fs, data, fat, 0x251000, 0x1af000,

1 # ESP-IDF Partition Table
3 # bootloader.bin 0x1000
4 # partition table 0x8000, 0xC00
5 otadata, data, ota, 0xd000, 0x2000,
6 ota_0, 0, ota_0, 0x10000, 0x100000, ota_0, 0, ota_0, 0x10000, 0x120000,
7 ota_1, 0, ota_1, 0x110000, 0x100000, ota_1, 0, ota_1, 0x130000, 0x120000,
8 phy_init, data, phy, 0x210000, 0x1000, phy_init, data, phy, 0x250000, 0x1000,
9 nvs, data, nvs, 0x211000, 0x6000, user_fs, data, fat, 0x251000, 0x1af000,
user_fs, data, fat, 0x217000, 0x1e9000,

View File

@ -37,6 +37,7 @@ CONFIG_APP_RETRIEVE_LEN_ELF_SHA=16
#
# Bootloader config
#
CONFIG_BOOTLOADER_OFFSET_IN_FLASH=0x1000
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG is not set
# CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF is not set
@ -64,15 +65,19 @@ CONFIG_BOOTLOADER_RESERVE_RTC_SIZE=0
#
# Security features
#
CONFIG_SECURE_TARGET_HAS_SECURE_ROM_DL_MODE=y
# CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT is not set
# CONFIG_SECURE_BOOT is not set
# CONFIG_SECURE_FLASH_ENC_ENABLED is not set
# CONFIG_SECURE_DISABLE_ROM_DL_MODE is not set
# CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE is not set
# end of Security features
#
# Serial flasher config
#
CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200
CONFIG_ESPTOOLPY_WITH_STUB=y
# CONFIG_ESPTOOLPY_FLASHMODE_QIO is not set
# CONFIG_ESPTOOLPY_FLASHMODE_QOUT is not set
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
@ -96,6 +101,7 @@ CONFIG_ESPTOOLPY_BEFORE="default_reset"
CONFIG_ESPTOOLPY_AFTER_RESET=y
# CONFIG_ESPTOOLPY_AFTER_NORESET is not set
CONFIG_ESPTOOLPY_AFTER="hard_reset"
# CONFIG_ESPTOOLPY_MONITOR_BAUD_CONSOLE is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_9600B is not set
# CONFIG_ESPTOOLPY_MONITOR_BAUD_57600B is not set
CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y
@ -186,6 +192,14 @@ CONFIG_SPI_SLAVE_ISR_IN_IRAM=y
CONFIG_EFUSE_MAX_BLK_LEN=256
# end of eFuse Bit Manager
#
# ESP-TLS
#
CONFIG_ESP_TLS_USING_MBEDTLS=y
CONFIG_ESP_TLS_SERVER=y
# CONFIG_ESP_TLS_PSK_VERIFICATION is not set
# end of ESP-TLS
#
# ESP32S2-specific
#
@ -245,6 +259,10 @@ CONFIG_ESP32S2_RTC_CLK_SRC_INT_RC=y
# CONFIG_ESP32S2_RTC_CLK_SRC_INT_8MD256 is not set
CONFIG_ESP32S2_RTC_CLK_CAL_CYCLES=576
# CONFIG_ESP32S2_NO_BLOBS is not set
# CONFIG_ESP32S2_KEEP_USB_ALIVE is not set
# CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM is not set
# CONFIG_ESP32S2_USE_FIXED_STATIC_RAM_SIZE is not set
CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP=y
# end of ESP32S2-specific
#
@ -263,15 +281,16 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_ESP_IPC_TASK_STACK_SIZE=1024
CONFIG_ESP_MINIMAL_SHARED_STACK_SIZE=2048
CONFIG_ESP_CONSOLE_UART_DEFAULT=y
# CONFIG_ESP_CONSOLE_USB_CDC is not set
# CONFIG_ESP_CONSOLE_UART_CUSTOM is not set
# CONFIG_ESP_CONSOLE_UART_NONE is not set
# CONFIG_ESP_CONSOLE_NONE is not set
CONFIG_ESP_CONSOLE_UART=y
CONFIG_ESP_CONSOLE_UART_NUM=0
CONFIG_ESP_CONSOLE_UART_TX_GPIO=1
CONFIG_ESP_CONSOLE_UART_RX_GPIO=3
CONFIG_ESP_CONSOLE_UART_BAUDRATE=115200
CONFIG_ESP_INT_WDT=y
CONFIG_ESP_INT_WDT_TIMEOUT_MS=300
# CONFIG_ESP_TASK_WDT is not set
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_STA=y
CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP=y
# end of Common ESP-related
@ -307,6 +326,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
# CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT is not set
# CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT is not set
# CONFIG_ESP_SYSTEM_PANIC_GDBSTUB is not set
CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE=y
# end of ESP System Settings
#
@ -436,12 +456,15 @@ CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y
CONFIG_LWIP_TIMERS_ONDEMAND=y
CONFIG_LWIP_MAX_SOCKETS=10
# CONFIG_LWIP_USE_ONLY_LWIP_SELECT is not set
# CONFIG_LWIP_SO_LINGER is not set
CONFIG_LWIP_SO_REUSE=y
CONFIG_LWIP_SO_REUSE_RXTOALL=y
# CONFIG_LWIP_SO_RCVBUF is not set
# CONFIG_LWIP_NETBUF_RECVINFO is not set
CONFIG_LWIP_IP_FRAG=y
# CONFIG_LWIP_IP_REASSEMBLY is not set
CONFIG_LWIP_IP4_FRAG=y
CONFIG_LWIP_IP6_FRAG=y
# CONFIG_LWIP_IP4_REASSEMBLY is not set
# CONFIG_LWIP_IP6_REASSEMBLY is not set
# CONFIG_LWIP_IP_FORWARD is not set
# CONFIG_LWIP_STATS is not set
# CONFIG_LWIP_ETHARP_TRUST_IP_MAC is not set
@ -482,6 +505,7 @@ CONFIG_LWIP_TCP_QUEUE_OOSEQ=y
CONFIG_LWIP_TCP_OVERSIZE_MSS=y
# CONFIG_LWIP_TCP_OVERSIZE_QUARTER_MSS is not set
# CONFIG_LWIP_TCP_OVERSIZE_DISABLE is not set
CONFIG_LWIP_TCP_RTO_TIME=3000
# end of TCP
#
@ -496,6 +520,7 @@ CONFIG_LWIP_TCPIP_TASK_AFFINITY_NO_AFFINITY=y
# CONFIG_LWIP_TCPIP_TASK_AFFINITY_CPU0 is not set
CONFIG_LWIP_TCPIP_TASK_AFFINITY=0x7FFFFFFF
# CONFIG_LWIP_PPP_SUPPORT is not set
# CONFIG_LWIP_SLIP_SUPPORT is not set
#
# ICMP
@ -518,6 +543,20 @@ CONFIG_LWIP_SNTP_UPDATE_DELAY=3600000
# end of SNTP
CONFIG_LWIP_ESP_LWIP_ASSERT=y
#
# Debug
#
# CONFIG_LWIP_NETIF_DEBUG is not set
# CONFIG_LWIP_PBUF_DEBUG is not set
# CONFIG_LWIP_ETHARP_DEBUG is not set
# CONFIG_LWIP_API_LIB_DEBUG is not set
# CONFIG_LWIP_SOCKETS_DEBUG is not set
# CONFIG_LWIP_IP_DEBUG is not set
# CONFIG_LWIP_ICMP_DEBUG is not set
# CONFIG_LWIP_IP6_DEBUG is not set
# CONFIG_LWIP_ICMP6_DEBUG is not set
# end of Debug
# end of LWIP
#
@ -681,6 +720,7 @@ CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS=y
CONFIG_SPI_FLASH_YIELD_DURING_ERASE=y
CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS=20
CONFIG_SPI_FLASH_ERASE_YIELD_TICKS=1
CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE=8192
#
# Auto-detect flash chips
@ -714,7 +754,6 @@ CONFIG_VFS_SEMIHOSTFS_HOST_PATH_MAX_LEN=128
CONFIG_WPA_MBEDTLS_CRYPTO=y
# CONFIG_WPA_DEBUG_PRINT is not set
# CONFIG_WPA_TESTING_OPTIONS is not set
CONFIG_WPA_TLS_V12=y
# CONFIG_WPA_WPS_WARS is not set
# end of Supplicant
# end of Component config
@ -771,10 +810,9 @@ CONFIG_MAIN_TASK_STACK_SIZE=8192
CONFIG_IPC_TASK_STACK_SIZE=1024
CONFIG_CONSOLE_UART_DEFAULT=y
# CONFIG_CONSOLE_UART_CUSTOM is not set
# CONFIG_CONSOLE_UART_NONE is not set
# CONFIG_ESP_CONSOLE_UART_NONE is not set
CONFIG_CONSOLE_UART=y
CONFIG_CONSOLE_UART_NUM=0
CONFIG_CONSOLE_UART_TX_GPIO=1
CONFIG_CONSOLE_UART_RX_GPIO=3
CONFIG_CONSOLE_UART_BAUDRATE=115200
CONFIG_INT_WDT=y
CONFIG_INT_WDT_TIMEOUT_MS=300

View File

@ -37,17 +37,8 @@
//| """
//|
//| def ip_address(obj: Union[int]) -> IPv4Address:
//| """Return a corresponding IP address object or raise ValueError if not possible."""
//| ...
//|
STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
uint32_t value;
if (mp_obj_get_int_maybe(ip_in, (mp_int_t*) &value)) {
// We're done.
} else if (MP_OBJ_IS_STR(ip_in)) {
GET_STR_DATA_LEN(ip_in, str_data, str_len);
bool ipaddress_parse_ipv4address(const char* str_data, size_t str_len, uint32_t* ip_out) {
size_t period_count = 0;
size_t period_index[4] = {0, 0, 0, str_len};
for (size_t i = 0; i < str_len; i++) {
@ -59,16 +50,45 @@ STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
}
}
if (period_count > 3) {
mp_raise_ValueError(translate("Not a valid IP string."));
return false;
}
size_t last_period = 0;
value = 0;
if (ip_out != NULL) {
*ip_out = 0;
}
for (size_t i = 0; i < 4; i++) {
mp_obj_t octet = mp_parse_num_integer((const char*) str_data + last_period, period_index[i] - last_period, 10, NULL);
// Catch exceptions thrown by mp_parse_num_integer
nlr_buf_t nlr;
mp_obj_t octet;
if (nlr_push(&nlr) == 0) {
octet = mp_parse_num_integer((const char*) str_data + last_period, period_index[i] - last_period, 10, NULL);
nlr_pop();
} else {
return false;
}
last_period = period_index[i] + 1;
if (ip_out != NULL) {
mp_int_t int_octet = MP_OBJ_SMALL_INT_VALUE(octet);
value |= int_octet << (i * 8);
*ip_out |= int_octet << (i * 8);
}
}
return true;
}
//| def ip_address(obj: Union[int]) -> IPv4Address:
//| """Return a corresponding IP address object or raise ValueError if not possible."""
//| ...
//|
STATIC mp_obj_t ipaddress_ip_address(mp_obj_t ip_in) {
uint32_t value;
if (mp_obj_get_int_maybe(ip_in, (mp_int_t*) &value)) {
// We're done.
} else if (MP_OBJ_IS_STR(ip_in)) {
GET_STR_DATA_LEN(ip_in, str_data, str_len);
if (!ipaddress_parse_ipv4address((const char*) str_data, str_len, &value)) {
mp_raise_ValueError(translate("Not a valid IP string."));
}
} else {
mp_raise_ValueError(translate("Only raw int supported for ip."));

View File

@ -29,6 +29,8 @@
#include "shared-module/ipaddress/__init__.h"
bool ipaddress_parse_ipv4address(const char* ip_str, size_t len, uint32_t* ip_out);
mp_obj_t common_hal_ipaddress_new_ipv4address(uint32_t value);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_IPADDRESS___INIT___H

View File

@ -62,94 +62,94 @@ STATIC mp_obj_t socketpool_socket___exit__(size_t n_args, const mp_obj_t *args)
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4, socketpool_socket___exit__);
//| def bind(self, address: tuple) -> None:
//| """Bind a socket to an address
//|
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
//| ...
//|
// //| def bind(self, address: tuple) -> None:
// //| """Bind a socket to an address
// //|
// //| :param ~tuple address: tuple of (remote_address, remote_port)"""
// //| ...
// //|
STATIC mp_obj_t socketpool_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// STATIC mp_obj_t socketpool_socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // get address
// uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
// // // get address
// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// // mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
// // check if we need to select a NIC
// socket_select_nic(self, ip);
// // // check if we need to select a NIC
// // socket_select_nic(self, ip);
// // call the NIC to bind the socket
// int _errno;
// if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
// mp_raise_OSError(_errno);
// // // call the NIC to bind the socket
// // int _errno;
// // if (self->nic_type->bind(self, ip, port, &_errno) != 0) {
// // mp_raise_OSError(_errno);
// // }
// return mp_const_none;
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_bind);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_bind);
// //| def listen(self, backlog: int) -> None:
// //| """Set socket to listen for incoming connections
// //|
// //| :param ~int backlog: length of backlog queue for waiting connetions"""
// //| ...
// //|
//| def listen(self, backlog: int) -> None:
//| """Set socket to listen for incoming connections
//|
//| :param ~int backlog: length of backlog queue for waiting connetions"""
//| ...
//|
// STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC mp_obj_t socketpool_socket_listen(mp_obj_t self_in, mp_obj_t backlog) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // if (self->nic == MP_OBJ_NULL) {
// // // not connected
// // // TODO I think we can listen even if not bound...
// // mp_raise_OSError(MP_ENOTCONN);
// // }
// if (self->nic == MP_OBJ_NULL) {
// // not connected
// // TODO I think we can listen even if not bound...
// mp_raise_OSError(MP_ENOTCONN);
// // int _errno;
// // if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
// // mp_raise_OSError(_errno);
// // }
// return mp_const_none;
// }
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_listen_obj, socketpool_socket_listen);
// int _errno;
// if (self->nic_type->listen(self, mp_obj_get_int(backlog), &_errno) != 0) {
// mp_raise_OSError(_errno);
// //| def accept(self) -> tuple:
// //| """Accept a connection on a listening socket of type SOCK_STREAM,
// //| creating a new socket of type SOCK_STREAM.
// //| Returns a tuple of (new_socket, remote_address)"""
// //|
// STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // // create new socket object
// // // starts with empty NIC so that finaliser doesn't run close() method if accept() fails
// // mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
// // socket2->base.type = &socket_type;
// // socket2->nic = MP_OBJ_NULL;
// // socket2->nic_type = NULL;
// // // accept incoming connection
// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// // mp_uint_t port;
// // int _errno;
// // if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
// // mp_raise_OSError(_errno);
// // }
// // // new socket has valid state, so set the NIC to the same as parent
// // socket2->nic = self->nic;
// // socket2->nic_type = self->nic_type;
// // // make the return value
// // mp_obj_tuple_t *client = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
// // client->items[0] = MP_OBJ_FROM_PTR(socket2);
// // client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
// return mp_const_none;
// }
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_listen_obj, socketpool_socket_listen);
//| def accept(self) -> tuple:
//| """Accept a connection on a listening socket of type SOCK_STREAM,
//| creating a new socket of type SOCK_STREAM.
//| Returns a tuple of (new_socket, remote_address)"""
//|
STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // create new socket object
// // starts with empty NIC so that finaliser doesn't run close() method if accept() fails
// mod_network_socket_obj_t *socket2 = m_new_obj_with_finaliser(mod_network_socket_obj_t);
// socket2->base.type = &socket_type;
// socket2->nic = MP_OBJ_NULL;
// socket2->nic_type = NULL;
// // accept incoming connection
// uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// mp_uint_t port;
// int _errno;
// if (self->nic_type->accept(self, socket2, ip, &port, &_errno) != 0) {
// mp_raise_OSError(_errno);
// }
// // new socket has valid state, so set the NIC to the same as parent
// socket2->nic = self->nic;
// socket2->nic_type = self->nic_type;
// // make the return value
// mp_obj_tuple_t *client = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
// client->items[0] = MP_OBJ_FROM_PTR(socket2);
// client->items[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
// STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
//| def close(self) -> None:
//| """Closes this Socket and makes its resources available to its SocketPool."""
@ -193,20 +193,19 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, socketpool_socket_
//|
STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // get address
// uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
mp_obj_t *addr_items;
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
// // check if we need to select a NIC
// socket_select_nic(self, ip);
size_t hostlen;
const char* host = mp_obj_str_get_data(addr_items[0], &hostlen);
mp_int_t port = mp_obj_get_int(addr_items[1]);
// // call the NIC to connect the socket
// int _errno;
// if (self->nic_type->connect(self, ip, port, &_errno) != 0) {
// mp_raise_OSError(_errno);
// }
bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, port);
if (!ok) {
mp_raise_OSError(0);
}
return mp_const_none;
}
@ -221,19 +220,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_connect_obj, socketpool_socke
//|
STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
mp_int_t ret = 0;
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// if (self->nic == MP_OBJ_NULL) {
// // not connected
// mp_raise_OSError(MP_EPIPE);
// }
// mp_buffer_info_t bufinfo;
// mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
// int _errno;
// mp_int_t ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno);
// if (ret == -1) {
// mp_raise_OSError(_errno);
// }
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
mp_int_t ret = common_hal_socketpool_socket_send(self, bufinfo.buf, bufinfo.len);
if (ret == -1) {
mp_raise_OSError(0);
}
return mp_obj_new_int_from_uint(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_send);
@ -267,134 +264,133 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_s
//|
STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
// if (self->nic == MP_OBJ_NULL) {
// // not connected
// mp_raise_OSError(MP_ENOTCONN);
// }
// mp_buffer_info_t bufinfo;
// mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
// mp_int_t len = bufinfo.len;
// if (n_args == 3) {
// mp_int_t given_len = mp_obj_get_int(args[2]);
// if (given_len < len) {
// len = given_len;
// }
// }
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
mp_int_t len = bufinfo.len;
if (n_args == 3) {
mp_int_t given_len = mp_obj_get_int(args[2]);
if (given_len < len) {
len = given_len;
}
}
// mp_int_t ret = _socket_recv_into(self, (byte*)bufinfo.buf, len);
mp_int_t ret = 0;
mp_int_t ret = common_hal_socketpool_socket_recv_into(self, (byte*)bufinfo.buf, len);
return mp_obj_new_int_from_uint(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, socketpool_socket_recv_into);
//| def sendto(self, bytes: ReadableBuffer, address: tuple) -> int:
//| """Send some bytes to a specific address.
//| Suits sockets of type SOCK_DGRAM
//|
//| :param ~bytes bytes: some bytes to send
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
//| ...
//|
// //| def sendto(self, bytes: ReadableBuffer, address: tuple) -> int:
// //| """Send some bytes to a specific address.
// //| Suits sockets of type SOCK_DGRAM
// //|
// //| :param ~bytes bytes: some bytes to send
// //| :param ~tuple address: tuple of (remote_address, remote_port)"""
// //| ...
// //|
STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// STATIC mp_obj_t socketpool_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // get the data
// mp_buffer_info_t bufinfo;
// mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
// // // get the data
// // mp_buffer_info_t bufinfo;
// // mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
// // get address
// uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
// // // get address
// // uint8_t ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// // mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG);
// // check if we need to select a NIC
// socket_select_nic(self, ip);
// // // check if we need to select a NIC
// // socket_select_nic(self, ip);
// // call the NIC to sendto
// int _errno;
// mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
// if (ret == -1) {
// mp_raise_OSError(_errno);
// // // call the NIC to sendto
// // int _errno;
// // mp_int_t ret = self->nic_type->sendto(self, bufinfo.buf, bufinfo.len, ip, port, &_errno);
// // if (ret == -1) {
// // mp_raise_OSError(_errno);
// // }
// mp_int_t ret = 0;
// return mp_obj_new_int(ret);
// }
mp_int_t ret = 0;
// STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto);
return mp_obj_new_int(ret);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto);
// //| def recvfrom(self, bufsize: int) -> Tuple[bytes, tuple]:
// //| """Reads some bytes from the connected remote address.
// //| Suits sockets of type SOCK_STREAM
// //|
// //| Returns a tuple containing
// //| * a bytes() of length <= bufsize
// //| * a remote_address, which is a tuple of ip address and port number
// //|
// //| :param ~int bufsize: maximum number of bytes to receive"""
// //| ...
// //|
//| def recvfrom(self, bufsize: int) -> Tuple[bytes, tuple]:
//| """Reads some bytes from the connected remote address.
//| Suits sockets of type SOCK_STREAM
//|
//| Returns a tuple containing
//| * a bytes() of length <= bufsize
//| * a remote_address, which is a tuple of ip address and port number
//|
//| :param ~int bufsize: maximum number of bytes to receive"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t len_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// if (self->nic == MP_OBJ_NULL) {
// // not connected
// mp_raise_OSError(MP_ENOTCONN);
// STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t len_in) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// // if (self->nic == MP_OBJ_NULL) {
// // // not connected
// // mp_raise_OSError(MP_ENOTCONN);
// // }
// // vstr_t vstr;
// // vstr_init_len(&vstr, mp_obj_get_int(len_in));
// // byte ip[4];
// // mp_uint_t port;
// // int _errno;
// // mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
// // if (ret == -1) {
// // mp_raise_OSError(_errno);
// // }
// mp_obj_t tuple[2];
// // if (ret == 0) {
// // tuple[0] = mp_const_empty_bytes;
// // } else {
// // vstr.len = ret;
// // tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
// // }
// // tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
// return mp_obj_new_tuple(2, tuple);
// }
// vstr_t vstr;
// vstr_init_len(&vstr, mp_obj_get_int(len_in));
// byte ip[4];
// mp_uint_t port;
// int _errno;
// mp_int_t ret = self->nic_type->recvfrom(self, (byte*)vstr.buf, vstr.len, ip, &port, &_errno);
// if (ret == -1) {
// mp_raise_OSError(_errno);
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into);
// //| def setsockopt(self, level: int, optname: int, value: int) -> None:
// //| """Sets socket options"""
// //| ...
// //|
// STATIC mp_obj_t socketpool_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
// // mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
// // mp_int_t level = mp_obj_get_int(args[1]);
// // mp_int_t opt = mp_obj_get_int(args[2]);
// // const void *optval;
// // mp_uint_t optlen;
// // mp_int_t val;
// // if (mp_obj_is_integer(args[3])) {
// // val = mp_obj_get_int_truncated(args[3]);
// // optval = &val;
// // optlen = sizeof(val);
// // } else {
// // mp_buffer_info_t bufinfo;
// // mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
// // optval = bufinfo.buf;
// // optlen = bufinfo.len;
// // }
// // int _errno;
// // if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
// // mp_raise_OSError(_errno);
// // }
// return mp_const_none;
// }
mp_obj_t tuple[2];
// if (ret == 0) {
// tuple[0] = mp_const_empty_bytes;
// } else {
// vstr.len = ret;
// tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
// }
// tuple[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
return mp_obj_new_tuple(2, tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into);
//| def setsockopt(self, level: int, optname: int, value: int) -> None:
//| """Sets socket options"""
//| ...
//|
STATIC mp_obj_t socketpool_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
// mp_int_t level = mp_obj_get_int(args[1]);
// mp_int_t opt = mp_obj_get_int(args[2]);
// const void *optval;
// mp_uint_t optlen;
// mp_int_t val;
// if (mp_obj_is_integer(args[3])) {
// val = mp_obj_get_int_truncated(args[3]);
// optval = &val;
// optlen = sizeof(val);
// } else {
// mp_buffer_info_t bufinfo;
// mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
// optval = bufinfo.buf;
// optlen = bufinfo.len;
// }
// int _errno;
// if (self->nic_type->setsockopt(self, level, opt, optval, optlen, &_errno) != 0) {
// mp_raise_OSError(_errno);
// }
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4, 4, socketpool_socket_setsockopt);
// STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4, 4, socketpool_socket_setsockopt);
//| def settimeout(self, value: int) -> None:
//| """Set the timeout value for this socket.
@ -404,46 +400,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_setsockopt_obj, 4,
//|
STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_in) {
// mod_network_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
// if (self->nic == MP_OBJ_NULL) {
// // not connected
// mp_raise_OSError(MP_ENOTCONN);
// }
// mp_uint_t timeout;
// if (timeout_in == mp_const_none) {
// timeout = -1;
// } else {
// #if MICROPY_PY_BUILTINS_FLOAT
// timeout = 1000 * mp_obj_get_float(timeout_in);
// #else
// timeout = 1000 * mp_obj_get_int(timeout_in);
// #endif
// }
// int _errno;
// if (self->nic_type->settimeout(self, timeout, &_errno) != 0) {
// mp_raise_OSError(_errno);
// }
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t timeout_ms;
if (timeout_in == mp_const_none) {
timeout_ms = -1;
} else {
#if MICROPY_PY_BUILTINS_FLOAT
timeout_ms = 1000 * mp_obj_get_float(timeout_in);
#else
timeout_ms = 1000 * mp_obj_get_int(timeout_in);
#endif
}
common_hal_socketpool_socket_settimeout(self, timeout_ms);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_socket_settimeout);
//| def setblocking(self, flag: bool) -> Optional[int]:
//| """Set the blocking behaviour of this socket.
//|
//| :param ~bool flag: False means non-blocking, True means block indefinitely."""
//| ...
//|
// //| def setblocking(self, flag: bool) -> Optional[int]:
// //| """Set the blocking behaviour of this socket.
// //|
// //| :param ~bool flag: False means non-blocking, True means block indefinitely."""
// //| ...
// //|
// method socket.setblocking(flag)
STATIC mp_obj_t socketpool_socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
// if (mp_obj_is_true(blocking)) {
// return socket_settimeout(self_in, mp_const_none);
// } else {
// return socket_settimeout(self_in, MP_OBJ_NEW_SMALL_INT(0));
// // method socket.setblocking(flag)
// STATIC mp_obj_t socketpool_socket_setblocking(mp_obj_t self_in, mp_obj_t blocking) {
// // if (mp_obj_is_true(blocking)) {
// // return socket_settimeout(self_in, mp_const_none);
// // } else {
// // return socket_settimeout(self_in, MP_OBJ_NEW_SMALL_INT(0));
// // }
// return mp_const_none;
// }
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_setblocking_obj, socketpool_socket_setblocking);
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_setblocking_obj, socketpool_socket_setblocking);
STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
@ -451,17 +440,17 @@ STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&socketpool_socket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&socketpool_socket_close_obj) },
{ MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socketpool_socket_bind_obj) },
{ MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socketpool_socket_listen_obj) },
{ MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socketpool_socket_accept_obj) },
// { MP_ROM_QSTR(MP_QSTR_bind), MP_ROM_PTR(&socketpool_socket_bind_obj) },
// { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&socketpool_socket_listen_obj) },
// { MP_ROM_QSTR(MP_QSTR_accept), MP_ROM_PTR(&socketpool_socket_accept_obj) },
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&socketpool_socket_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&socketpool_socket_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socketpool_socket_sendto_obj) },
{ MP_ROM_QSTR(MP_QSTR_recvfrom_into), MP_ROM_PTR(&socketpool_socket_recvfrom_into_obj) },
// { MP_ROM_QSTR(MP_QSTR_sendto), MP_ROM_PTR(&socketpool_socket_sendto_obj) },
// { MP_ROM_QSTR(MP_QSTR_recvfrom_into), MP_ROM_PTR(&socketpool_socket_recvfrom_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_recv_into), MP_ROM_PTR(&socketpool_socket_recv_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socketpool_socket_setsockopt_obj) },
// { MP_ROM_QSTR(MP_QSTR_setsockopt), MP_ROM_PTR(&socketpool_socket_setsockopt_obj) },
{ MP_ROM_QSTR(MP_QSTR_settimeout), MP_ROM_PTR(&socketpool_socket_settimeout_obj) },
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socketpool_socket_setblocking_obj) },
// { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socketpool_socket_setblocking_obj) },
};
STATIC MP_DEFINE_CONST_DICT(socketpool_socket_locals_dict, socketpool_socket_locals_dict_table);

View File

@ -31,8 +31,9 @@
extern const mp_obj_type_t socketpool_socket_type;
// mp_obj_t common_hal_ipaddress_new_ipv4address(uint32_t value);
// void common_hal_ipaddress_ipv4address_construct(ipaddress_ipv4address_obj_t* self, uint8_t* buf, size_t len);
// mp_obj_t common_hal_ipaddress_ipv4address_get_packed(ipaddress_ipv4address_obj_t* self);
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms);
bool common_hal_socketpool_socket_connect(socketpool_socket_obj_t* self, const char* host, size_t hostlen, mp_int_t port);
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self, const uint8_t* buf, mp_uint_t len);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H

View File

@ -33,6 +33,7 @@
#include "py/runtime.h"
#include "py/mperrno.h"
#include "shared-bindings/ipaddress/__init__.h"
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/socketpool/SocketPool.h"
@ -45,6 +46,8 @@ STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t
socketpool_socketpool_obj_t *s = m_new_obj_with_finaliser(socketpool_socketpool_obj_t);
s->base.type = &socketpool_socketpool_type;
// common_hal_socketpool_socketpool_construct(s, );
return MP_OBJ_FROM_PTR(s);
}
@ -58,31 +61,22 @@ STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t
//| ...
//|
STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 0, 4, false);
mp_arg_check_num(n_args, kw_args, 0, 5, false);
// create socket object (not bound to any NIC yet)
socketpool_socket_obj_t *s = m_new_obj_with_finaliser(socketpool_socket_obj_t);
s->base.type = &socketpool_socket_type;
// s->nic = MP_OBJ_NULL;
// s->nic_type = NULL;
// s->u_param.domain = MOD_NETWORK_AF_INET;
// s->u_param.type = MOD_NETWORK_SOCK_STREAM;
// s->u_param.fileno = -1;
// if (n_args >= 1) {
// s->u_param.domain = mp_obj_get_int(args[0]);
// if (n_args >= 2) {
// s->u_param.type = mp_obj_get_int(args[1]);
// if (n_args >= 4) {
// s->u_param.fileno = mp_obj_get_int(args[3]);
// }
// }
// }
return MP_OBJ_FROM_PTR(s);
socketpool_socketpool_obj_t *self = pos_args[0];
socketpool_socketpool_addressfamily_t family = SOCKETPOOL_AF_INET;
socketpool_socketpool_sock_t type = SOCKETPOOL_SOCK_STREAM;
if (n_args >= 2) {
family = mp_obj_get_int(pos_args[1]);
if (n_args >= 3) {
type = mp_obj_get_int(pos_args[2]);
}
}
return common_hal_socketpool_socket(self, family, type);
}
MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socketpool_socket);
//| def getaddrinfo(host: str, port: int) -> tuple:
//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> tuple:
//| """Gets the address information for a hostname and port
//|
//| Returns the appropriate family, socket type, socket protocol and
@ -91,55 +85,49 @@ MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socke
//| ...
//|
STATIC mp_obj_t socketpool_socketpool_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
// size_t hlen;
// const char *host = mp_obj_str_get_data(host_in, &hlen);
// mp_int_t port = mp_obj_get_int(port_in);
// uint8_t out_ip[MOD_NETWORK_IPADDR_BUF_SIZE];
// bool have_ip = false;
STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_host, ARG_port, ARG_family, ARG_type, ARG_proto, ARG_flags };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_host, MP_ARG_OBJ | MP_ARG_REQUIRED },
{ MP_QSTR_port, MP_ARG_INT | MP_ARG_REQUIRED },
{ MP_QSTR_family, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_type, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_port, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_flags, MP_ARG_INT, {.u_int = 0} },
};
socketpool_socketpool_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
// if (hlen > 0) {
// // check if host is already in IP form
// nlr_buf_t nlr;
// if (nlr_push(&nlr) == 0) {
// netutils_parse_ipv4_addr(host_in, out_ip, NETUTILS_BIG);
// have_ip = true;
// nlr_pop();
// } else {
// // swallow exception: host was not in IP form so need to do DNS lookup
// }
// }
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// if (!have_ip) {
// // find a NIC that can do a name lookup
// for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
// mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
// mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
// if (nic_type->gethostbyname != NULL) {
// int ret = nic_type->gethostbyname(nic, host, hlen, out_ip);
// if (ret != 0) {
// mp_raise_OSError(ret);
// }
// have_ip = true;
// break;
// }
// }
// }
const char *host = mp_obj_str_get_str(args[ARG_host].u_obj);
mp_int_t port = args[ARG_port].u_int;
mp_obj_t ip_str = mp_const_none;
// if (!have_ip) {
// nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, translate("no available NIC")));
// }
// mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
// tuple->items[0] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_AF_INET);
// tuple->items[1] = MP_OBJ_NEW_SMALL_INT(MOD_NETWORK_SOCK_STREAM);
// tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
// tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
// tuple->items[4] = netutils_format_inet_addr(out_ip, port, NETUTILS_BIG);
// return mp_obj_new_list(1, (mp_obj_t*)&tuple);
return mp_const_none;
if (strlen(host) > 0 && ipaddress_parse_ipv4address(host, strlen(host), NULL)) {
ip_str = args[ARG_host].u_obj;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socketpool_getaddrinfo_obj, socketpool_socketpool_getaddrinfo);
if (ip_str == mp_const_none) {
ip_str = common_hal_socketpool_socketpool_gethostbyname(self, host);
}
if (ip_str == mp_const_none) {
mp_raise_OSError(0);
}
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
tuple->items[0] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_AF_INET);
tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_SOCK_STREAM);
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0);
tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_);
mp_obj_tuple_t *sockaddr = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
sockaddr->items[0] = ip_str;
sockaddr->items[1] = MP_OBJ_NEW_SMALL_INT(port);
tuple->items[4] = MP_OBJ_FROM_PTR(sockaddr);
return mp_obj_new_list(1, (mp_obj_t*)&tuple);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_getaddrinfo_obj, 3, socketpool_socketpool_getaddrinfo);
STATIC const mp_rom_map_elem_t socketpool_socketpool_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&socketpool_socketpool_socket_obj) },

View File

@ -29,11 +29,10 @@
#include "common-hal/socketpool/SocketPool.h"
#include "shared-bindings/socketpool/Socket.h"
extern const mp_obj_type_t socketpool_socketpool_type;
// mp_obj_t common_hal_ipaddress_new_ipv4address(uint32_t value);
// void common_hal_ipaddress_ipv4address_construct(ipaddress_ipv4address_obj_t* self, uint8_t* buf, size_t len);
// mp_obj_t common_hal_ipaddress_ipv4address_get_packed(ipaddress_ipv4address_obj_t* self);
typedef enum {
SOCKETPOOL_SOCK_STREAM,
SOCKETPOOL_SOCK_DGRAM,
@ -43,6 +42,12 @@ typedef enum {
typedef enum {
SOCKETPOOL_AF_INET,
SOCKETPOOL_AF_INET6
} socketpool_socketpool_af_t;
} socketpool_socketpool_addressfamily_t;
socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_t* self,
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type);
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t* self,
const char* host);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKETPOOL_H