Merge pull request #3708 from hierophect/esp32s2-udp
ESP32-S2: Add UDP with recvfrom_into and sendto
This commit is contained in:
commit
7923aa0a62
|
@ -8,7 +8,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2020-11-11 16:30+0530\n"
|
||||
"POT-Creation-Date: 2020-11-24 15:40-0500\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
|
@ -297,6 +297,7 @@ msgid "All I2C peripherals are in use"
|
|||
msgstr ""
|
||||
|
||||
#: ports/esp32s2/common-hal/countio/Counter.c
|
||||
#: ports/esp32s2/common-hal/frequencyio/FrequencyIn.c
|
||||
#: ports/esp32s2/common-hal/rotaryio/IncrementalEncoder.c
|
||||
msgid "All PCNT units in use"
|
||||
msgstr ""
|
||||
|
@ -333,6 +334,7 @@ msgstr ""
|
|||
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
|
||||
#: ports/atmel-samd/common-hal/pulseio/PulseOut.c
|
||||
#: ports/cxd56/common-hal/pulseio/PulseOut.c
|
||||
#: ports/esp32s2/common-hal/frequencyio/FrequencyIn.c
|
||||
#: ports/esp32s2/common-hal/neopixel_write/__init__.c
|
||||
#: ports/esp32s2/common-hal/pulseio/PulseIn.c
|
||||
#: ports/esp32s2/common-hal/pulseio/PulseOut.c
|
||||
|
@ -1086,6 +1088,7 @@ msgid "Invalid byteorder string"
|
|||
msgstr ""
|
||||
|
||||
#: ports/atmel-samd/common-hal/frequencyio/FrequencyIn.c
|
||||
#: ports/esp32s2/common-hal/frequencyio/FrequencyIn.c
|
||||
msgid "Invalid capture period. Valid range: 1 - 500"
|
||||
msgstr ""
|
||||
|
||||
|
@ -1416,14 +1419,14 @@ msgstr ""
|
|||
msgid "Only 8 or 16 bit mono with "
|
||||
msgstr ""
|
||||
|
||||
#: ports/esp32s2/common-hal/socketpool/SocketPool.c
|
||||
msgid "Only IPv4 SOCK_STREAM sockets supported"
|
||||
msgstr ""
|
||||
|
||||
#: ports/esp32s2/common-hal/wifi/__init__.c
|
||||
msgid "Only IPv4 addresses supported"
|
||||
msgstr ""
|
||||
|
||||
#: ports/esp32s2/common-hal/socketpool/SocketPool.c
|
||||
msgid "Only IPv4 sockets supported"
|
||||
msgstr ""
|
||||
|
||||
#: shared-module/displayio/OnDiskBitmap.c
|
||||
#, c-format
|
||||
msgid ""
|
||||
|
|
|
@ -190,6 +190,7 @@ SRC_C += \
|
|||
lib/utils/pyexec.c \
|
||||
lib/utils/stdout_helpers.c \
|
||||
lib/utils/sys_stdio_mphal.c \
|
||||
lib/netutils/netutils.c \
|
||||
peripherals/timer.c \
|
||||
peripherals/pcnt.c \
|
||||
peripherals/pins.c \
|
||||
|
|
|
@ -32,6 +32,11 @@
|
|||
#include "py/runtime.h"
|
||||
#include "supervisor/shared/tick.h"
|
||||
|
||||
#include "components/lwip/lwip/src/include/lwip/err.h"
|
||||
#include "components/lwip/lwip/src/include/lwip/sockets.h"
|
||||
#include "components/lwip/lwip/src/include/lwip/sys.h"
|
||||
#include "components/lwip/lwip/src/include/lwip/netdb.h"
|
||||
|
||||
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_uint_t timeout_ms) {
|
||||
self->timeout_ms = timeout_ms;
|
||||
}
|
||||
|
@ -124,16 +129,75 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t* self,
|
|||
return received;
|
||||
}
|
||||
|
||||
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
|
||||
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len) {
|
||||
|
||||
// Get the IP address string
|
||||
const struct addrinfo hints = {
|
||||
.ai_family = AF_INET,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
};
|
||||
struct addrinfo *result;
|
||||
int error = lwip_getaddrinfo(host, NULL, &hints, &result);
|
||||
if (error != 0 || result == NULL) {
|
||||
return 0;
|
||||
}
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wcast-align"
|
||||
struct in_addr *addr = &((struct sockaddr_in *)result->ai_addr)->sin_addr;
|
||||
#pragma GCC diagnostic pop
|
||||
char ip_str[IP4ADDR_STRLEN_MAX];
|
||||
inet_ntoa_r(*addr, ip_str, IP4ADDR_STRLEN_MAX);
|
||||
freeaddrinfo(result);
|
||||
|
||||
// Set parameters
|
||||
struct sockaddr_in dest_addr;
|
||||
dest_addr.sin_addr.s_addr = inet_addr((const char *)ip_str);
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(port);
|
||||
|
||||
int bytes_sent = lwip_sendto(self->num, buf, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||
if (bytes_sent < 0) {
|
||||
mp_raise_BrokenPipeError();
|
||||
return 0;
|
||||
}
|
||||
return bytes_sent;
|
||||
}
|
||||
|
||||
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
|
||||
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port) {
|
||||
|
||||
struct sockaddr_in source_addr;
|
||||
socklen_t socklen = sizeof(source_addr);
|
||||
int bytes_received = lwip_recvfrom(self->num, buf, len - 1, 0, (struct sockaddr *)&source_addr, &socklen);
|
||||
|
||||
memcpy((void *)ip, (void*)&source_addr.sin_addr.s_addr, sizeof source_addr.sin_addr.s_addr);
|
||||
*port = source_addr.sin_port;
|
||||
|
||||
if (bytes_received < 0) {
|
||||
mp_raise_BrokenPipeError();
|
||||
return 0;
|
||||
} else {
|
||||
buf[bytes_received] = 0; // Null-terminate whatever we received
|
||||
return bytes_received;
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self) {
|
||||
self->connected = false;
|
||||
if (self->tcp != NULL) {
|
||||
esp_tls_conn_destroy(self->tcp);
|
||||
self->tcp = NULL;
|
||||
}
|
||||
if (self->num >= 0) {
|
||||
lwip_shutdown(self->num, 0);
|
||||
lwip_close(self->num);
|
||||
self->num = -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self) {
|
||||
return self->tcp == NULL;
|
||||
return self->tcp == NULL && self->num < 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -61,15 +61,14 @@ socketpool_socket_obj_t* common_hal_socketpool_socket(socketpool_socketpool_obj_
|
|||
socket_type = SOCK_RAW;
|
||||
}
|
||||
|
||||
if (socket_type == SOCK_DGRAM || socket_type == SOCK_RAW ||
|
||||
addr_family == AF_INET6 || ipproto == IPPROTO_IPV6) {
|
||||
mp_raise_NotImplementedError(translate("Only IPv4 SOCK_STREAM sockets supported"));
|
||||
if (addr_family == AF_INET6 || ipproto == IPPROTO_IPV6) {
|
||||
mp_raise_NotImplementedError(translate("Only IPv4 sockets supported"));
|
||||
}
|
||||
|
||||
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);
|
||||
socknum = lwip_socket(addr_family, socket_type, ipproto);
|
||||
} else {
|
||||
tcp_handle = esp_tls_init();
|
||||
|
||||
|
|
|
@ -36,6 +36,8 @@
|
|||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
|
||||
#include "lib/netutils/netutils.h"
|
||||
|
||||
//| class Socket:
|
||||
//| """TCP, UDP and RAW socket. Cannot be created directly. Instead, call
|
||||
//| `SocketPool.socket()`.
|
||||
|
@ -274,79 +276,63 @@ STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args)
|
|||
}
|
||||
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[str, int]) -> 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) {
|
||||
socketpool_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);
|
||||
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 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;
|
||||
mp_int_t ret = common_hal_socketpool_socket_sendto(self, host, hostlen, port, bufinfo.buf, bufinfo.len);
|
||||
if (!ret) {
|
||||
mp_raise_OSError(0);
|
||||
}
|
||||
|
||||
// return mp_obj_new_int(ret);
|
||||
// }
|
||||
// STATIC MP_DEFINE_CONST_FUN_OBJ_3(socketpool_socket_sendto_obj, socketpool_socket_sendto);
|
||||
return mp_obj_new_int_from_uint(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_into(self, buffer: WriteableBuffer) -> Tuple[int, Tuple[str, int]]:
|
||||
//| """Reads some bytes from a remote address.
|
||||
//|
|
||||
//| Returns a tuple containing
|
||||
//| * the number of bytes received into the given buffer
|
||||
//| * a remote_address, which is a tuple of ip address and port number
|
||||
//|
|
||||
//| :param object buffer: buffer to read into"""
|
||||
//| ...
|
||||
//|
|
||||
STATIC mp_obj_t socketpool_socket_recvfrom_into(mp_obj_t self_in, mp_obj_t data_in) {
|
||||
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_WRITE);
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool_socket_recvfrom_into);
|
||||
byte ip[4];
|
||||
mp_uint_t port;
|
||||
mp_int_t ret = common_hal_socketpool_socket_recvfrom_into(self,
|
||||
(byte*)bufinfo.buf, bufinfo.len, ip, &port);
|
||||
mp_obj_t tuple_contents[2];
|
||||
tuple_contents[0] = mp_obj_new_int_from_uint(ret);
|
||||
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
|
||||
return mp_obj_new_tuple(2, tuple_contents);
|
||||
}
|
||||
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"""
|
||||
|
@ -449,8 +435,8 @@ STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = {
|
|||
// { 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_settimeout), MP_ROM_PTR(&socketpool_socket_settimeout_obj) },
|
||||
|
|
|
@ -35,6 +35,10 @@ void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t* self, mp_u
|
|||
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);
|
||||
mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t* self,
|
||||
const char* host, size_t hostlen, uint8_t port, const uint8_t* buf, mp_uint_t len);
|
||||
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
|
||||
uint8_t* buf, mp_uint_t len, uint8_t* ip, uint *port);
|
||||
void common_hal_socketpool_socket_close(socketpool_socket_obj_t* self);
|
||||
bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t* self);
|
||||
bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t* self);
|
||||
|
|
Loading…
Reference in New Issue