circuitpython/zephyr/modusocket.c

548 lines
18 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Linaro Limited
*
* 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 "py/mpconfig.h"
#ifdef MICROPY_PY_USOCKET
#include "py/runtime.h"
#include "py/stream.h"
#include <stdio.h>
#include <zephyr.h>
// Zephyr's generated version header
#include <version.h>
#include <net/net_context.h>
#include <net/net_pkt.h>
#define DEBUG 0
#if DEBUG // print debugging info
#define DEBUG_printf printf
#else // don't print debugging info
#define DEBUG_printf(...) (void)0
#endif
typedef struct _socket_obj_t {
mp_obj_base_t base;
struct net_context *ctx;
union {
struct k_fifo recv_q;
struct k_fifo accept_q;
};
struct net_pkt *cur_pkt;
#define STATE_NEW 0
#define STATE_CONNECTING 1
#define STATE_CONNECTED 2
#define STATE_PEER_CLOSED 3
int8_t state;
} socket_obj_t;
STATIC const mp_obj_type_t socket_type;
// k_fifo extended API
static inline void *_k_fifo_peek_head(struct k_fifo *fifo)
{
#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */
return sys_slist_peek_head(&fifo->data_q);
#else
return sys_slist_peek_head(&fifo->_queue.data_q);
#endif
}
static inline void *_k_fifo_peek_tail(struct k_fifo *fifo)
{
#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */
return sys_slist_peek_tail(&fifo->data_q);
#else
return sys_slist_peek_tail(&fifo->_queue.data_q);
#endif
}
static inline void _k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
{
struct k_poll_event events[] = {
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, fifo),
};
k_poll(events, MP_ARRAY_SIZE(events), timeout);
DEBUG_printf("poll res: %d\n", events[0].state);
}
// Helper functions
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
STATIC void socket_check_closed(socket_obj_t *socket) {
if (socket->ctx == NULL) {
// already closed
mp_raise_OSError(EBADF);
}
}
STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct sockaddr *sockaddr) {
// We employ the fact that port and address offsets are the same for IPv4 & IPv6
struct sockaddr_in *sockaddr_in = (struct sockaddr_in*)sockaddr;
mp_obj_t *addr_items;
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
sockaddr_in->sin_family = net_context_get_family(socket->ctx);
RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr));
sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1]));
}
// Copy data from Zephyr net_buf chain into linear buffer.
// We don't use net_pkt_read(), because it's weird (e.g., we'd like to
// free processed data fragment ASAP, while net_pkt_read() holds onto
// the whole fragment chain to do its deeds, and that's minor comparing
// to the fact that it copies data byte by byte).
static char *net_pkt_gather(struct net_pkt *pkt, char *to, unsigned max_len) {
struct net_buf *tmp = pkt->frags;
while (tmp && max_len) {
unsigned len = tmp->len;
if (len > max_len) {
len = max_len;
}
memcpy(to, tmp->data, len);
to += len;
max_len -= len;
tmp = net_pkt_frag_del(pkt, NULL, tmp);
}
return to;
}
// Callback for incoming packets.
static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, int status, void *user_data) {
socket_obj_t *socket = (socket_obj_t*)user_data;
DEBUG_printf("recv cb: context: %p, status: %d, pkt: %p", context, status, pkt);
if (pkt) {
DEBUG_printf(" (appdatalen=%d), token: %p", pkt->appdatalen, net_pkt_token(pkt));
}
DEBUG_printf("\n");
#if DEBUG > 1
net_pkt_print_frags(pkt);
#endif
// if net_buf == NULL, EOF
if (pkt == NULL) {
struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q);
if (last_pkt == NULL) {
socket->state = STATE_PEER_CLOSED;
k_fifo_cancel_wait(&socket->recv_q);
DEBUG_printf("Marked socket %p as peer-closed\n", socket);
} else {
// We abuse "buf_sent" flag to store EOF flag
net_pkt_set_sent(last_pkt, true);
DEBUG_printf("Set EOF flag on %p\n", last_pkt);
}
return;
}
// Make sure that "EOF flag" is not set
net_pkt_set_sent(pkt, false);
// We don't care about packet header, so get rid of it asap
unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data;
net_buf_pull(pkt->frags, header_len);
k_fifo_put(&socket->recv_q, pkt);
}
// Callback for incoming connections.
static void sock_accepted_cb(struct net_context *new_ctx, struct sockaddr *addr, socklen_t addrlen, int status, void *user_data) {
socket_obj_t *socket = (socket_obj_t*)user_data;
DEBUG_printf("accept cb: context: %p, status: %d, new ctx: %p\n", socket->ctx, status, new_ctx);
DEBUG_printf("new_ctx ref_cnt: %d\n", new_ctx->refcount);
k_fifo_put(&socket->accept_q, new_ctx);
}
socket_obj_t *socket_new(void) {
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
socket->base.type = (mp_obj_t)&socket_type;
k_fifo_init(&socket->recv_q);
socket->cur_pkt = NULL;
socket->state = STATE_NEW;
return socket;
}
// Methods
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
socket_obj_t *self = self_in;
if (self->ctx == NULL) {
mp_printf(print, "<socket NULL>");
} else {
struct net_context *ctx = self->ctx;
mp_printf(print, "<socket %p type=%d>", ctx, net_context_get_type(ctx));
}
}
STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 4, false);
socket_obj_t *socket = socket_new();
int family = AF_INET;
int socktype = SOCK_STREAM;
int proto = -1;
if (n_args >= 1) {
family = mp_obj_get_int(args[0]);
if (n_args >= 2) {
socktype = mp_obj_get_int(args[1]);
if (n_args >= 3) {
proto = mp_obj_get_int(args[2]);
}
}
}
if (proto == -1) {
proto = IPPROTO_TCP;
if (socktype != SOCK_STREAM) {
proto = IPPROTO_UDP;
}
}
RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx));
return MP_OBJ_FROM_PTR(socket);
}
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
socket_obj_t *socket = self_in;
socket_check_closed(socket);
struct sockaddr sockaddr;
parse_inet_addr(socket, addr_in, &sockaddr);
RAISE_ERRNO(net_context_bind(socket->ctx, &sockaddr, sizeof(sockaddr)));
// For DGRAM socket, we expect to receive packets after call to bind(),
// but for STREAM socket, next expected operation is listen(), which
// doesn't work if recv callback is set.
if (net_context_get_type(socket->ctx) == SOCK_DGRAM) {
DEBUG_printf("Setting recv cb after bind\n");
RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
socket_obj_t *socket = self_in;
socket_check_closed(socket);
struct sockaddr sockaddr;
parse_inet_addr(socket, addr_in, &sockaddr);
RAISE_ERRNO(net_context_connect(socket->ctx, &sockaddr, sizeof(sockaddr), NULL, K_FOREVER, NULL));
DEBUG_printf("Setting recv cb after connect()\n");
RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) {
socket_obj_t *socket = self_in;
socket_check_closed(socket);
mp_int_t backlog = mp_obj_get_int(backlog_in);
RAISE_ERRNO(net_context_listen(socket->ctx, backlog));
RAISE_ERRNO(net_context_accept(socket->ctx, sock_accepted_cb, K_NO_WAIT, socket));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
socket_obj_t *socket = self_in;
socket_check_closed(socket);
struct net_context *ctx = k_fifo_get(&socket->accept_q, K_FOREVER);
// Was overwritten by fifo
ctx->refcount = 1;
socket_obj_t *socket2 = socket_new();
socket2->ctx = ctx;
DEBUG_printf("Setting recv cb after accept()\n");
RAISE_ERRNO(net_context_recv(ctx, sock_received_cb, K_NO_WAIT, socket2));
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
client->items[0] = MP_OBJ_FROM_PTR(socket2);
// TODO
client->items[1] = mp_const_none;
return MP_OBJ_FROM_PTR(client);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
socket_obj_t *socket = self_in;
if (socket->ctx == NULL) {
// already closed
*errcode = EBADF;
return MP_STREAM_ERROR;
}
struct net_pkt *send_pkt = net_pkt_get_tx(socket->ctx, K_FOREVER);
unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx));
// Arbitrary value to account for protocol headers
len -= 64;
if (len > size) {
len = size;
}
// TODO: Return value of 0 is a hard case (as we wait forever, should
// not happen).
len = net_pkt_append(send_pkt, len, buf, K_FOREVER);
int err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL);
if (err < 0) {
*errcode = -err;
return MP_STREAM_ERROR;
}
return len;
}
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
int err = 0;
mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err);
if (len == MP_STREAM_ERROR) {
mp_raise_OSError(err);
}
return mp_obj_new_int_from_uint(len);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) {
socket_obj_t *socket = self_in;
if (socket->ctx == NULL) {
// already closed
*errcode = EBADF;
return MP_STREAM_ERROR;
}
enum net_sock_type sock_type = net_context_get_type(socket->ctx);
unsigned recv_len;
if (sock_type == SOCK_DGRAM) {
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
recv_len = net_pkt_appdatalen(pkt);
DEBUG_printf("recv: pkt=%p, appdatalen: %d\n", pkt, recv_len);
if (recv_len > max_len) {
recv_len = max_len;
}
net_pkt_gather(pkt, buf, recv_len);
net_pkt_unref(pkt);
} else if (sock_type == SOCK_STREAM) {
do {
if (socket->cur_pkt == NULL) {
if (socket->state == STATE_PEER_CLOSED) {
return 0;
}
DEBUG_printf("TCP recv: no cur_pkt, getting\n");
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
if (pkt == NULL) {
DEBUG_printf("TCP recv: NULL return from fifo\n");
continue;
}
DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt);
socket->cur_pkt = pkt;
}
struct net_buf *frag = socket->cur_pkt->frags;
if (frag == NULL) {
printf("net_pkt has empty fragments on start!\n");
assert(0);
}
unsigned frag_len = frag->len;
recv_len = frag_len;
if (recv_len > max_len) {
recv_len = max_len;
}
DEBUG_printf("%d data bytes in head frag, going to read %d\n", frag_len, recv_len);
memcpy(buf, frag->data, recv_len);
if (recv_len != frag_len) {
net_buf_pull(frag, recv_len);
} else {
frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag);
if (frag == NULL) {
DEBUG_printf("Finished processing pkt %p\n", socket->cur_pkt);
// If "sent" flag was set, it's last packet and we reached EOF
if (net_pkt_sent(socket->cur_pkt)) {
socket->state = STATE_PEER_CLOSED;
}
net_pkt_unref(socket->cur_pkt);
socket->cur_pkt = NULL;
}
}
// Keep repeating while we're getting empty fragments
// Zephyr IP stack appears to have fed empty net_buf's with empty
// frags for various TCP control packets - in previous versions.
} while (recv_len == 0);
}
return recv_len;
}
STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) {
mp_int_t max_len = mp_obj_get_int(len_in);
vstr_t vstr;
// +1 to accommodate for trailing \0
vstr_init_len(&vstr, max_len + 1);
int err;
mp_uint_t len = sock_read(self_in, vstr.buf, max_len, &err);
if (len == MP_STREAM_ERROR) {
vstr_clear(&vstr);
mp_raise_OSError(err);
}
if (len == 0) {
vstr_clear(&vstr);
return mp_const_empty_bytes;
}
vstr.len = len;
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) {
(void)n_args; // always 4
mp_warning("setsockopt() not implemented");
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt);
STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) {
(void)n_args;
return args[0];
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile);
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
socket_obj_t *socket = self_in;
if (socket->ctx != NULL) {
RAISE_ERRNO(net_context_put(socket->ctx));
socket->ctx = NULL;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
STATIC const mp_map_elem_t socket_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj},
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj },
};
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
STATIC const mp_stream_p_t socket_stream_p = {
.read = sock_read,
.write = sock_write,
//.ioctl = sock_ioctl,
};
STATIC const mp_obj_type_t socket_type = {
{ &mp_type_type },
.name = MP_QSTR_socket,
.print = socket_print,
.make_new = socket_make_new,
.protocol = &socket_stream_p,
.locals_dict = (mp_obj_t)&socket_locals_dict,
};
STATIC mp_obj_t pkt_get_info(void) {
struct k_mem_slab *rx, *tx;
struct net_buf_pool *rx_data, *tx_data;
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
t->items[0] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(rx));
t->items[1] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(tx));
t->items[2] = MP_OBJ_NEW_SMALL_INT(rx_data->avail_count);
t->items[3] = MP_OBJ_NEW_SMALL_INT(tx_data->avail_count);
return MP_OBJ_FROM_PTR(t);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pkt_get_info_obj, pkt_get_info);
STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) },
// objects
{ MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type },
// class constants
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_pkt_get_info), (mp_obj_t)&pkt_get_info_obj },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table);
const mp_obj_module_t mp_module_usocket = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_usocket_globals,
};
#endif // MICROPY_PY_USOCKET