Initial UDP work. Need to test
This commit is contained in:
parent
7cbc50962b
commit
7fd73c7d39
|
@ -121,16 +121,77 @@ 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) {
|
||||
|
||||
struct sockaddr_in dest_addr;
|
||||
dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);
|
||||
dest_addr.sin_family = AF_INET;
|
||||
dest_addr.sin_port = htons(port);
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
|
||||
int err = lwip_sendto(self->num, buf, len, 0 /* flags */,
|
||||
(struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||
}
|
||||
|
||||
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t* self,
|
||||
const uint8_t* buf, mp_uint_t len, uint8_t* ip, uint8_t port) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
|
|
@ -274,79 +274,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) -> 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 (!ok) {
|
||||
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) -> Tuple[int, tuple]:
|
||||
//| """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, 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);
|
||||
}
|
||||
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 +433,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 uint8_t* 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,
|
||||
const uint8_t* buf, mp_uint_t len, uint8_t* ip, uint8_t 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