2017-03-30 17:04:31 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
2017-04-25 18:05:54 -04:00
|
|
|
#include "py/stream.h"
|
2017-03-30 17:04:31 -04:00
|
|
|
|
2017-04-01 02:20:07 -04:00
|
|
|
#include <stdio.h>
|
2017-03-30 17:04:31 -04:00
|
|
|
#include <zephyr.h>
|
2017-04-05 06:34:37 -04:00
|
|
|
// Zephyr's generated version header
|
|
|
|
#include <version.h>
|
2017-03-30 17:04:31 -04:00
|
|
|
#include <net/net_context.h>
|
2017-05-13 08:47:47 -04:00
|
|
|
#include <net/net_pkt.h>
|
2017-03-30 17:04:31 -04:00
|
|
|
|
2017-04-11 08:24:33 -04:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG // print debugging info
|
2017-04-01 02:20:07 -04:00
|
|
|
#define DEBUG_printf printf
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
typedef struct _socket_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
struct net_context *ctx;
|
2017-04-07 17:33:09 -04:00
|
|
|
union {
|
|
|
|
struct k_fifo recv_q;
|
|
|
|
struct k_fifo accept_q;
|
|
|
|
};
|
2017-05-13 08:47:47 -04:00
|
|
|
struct net_pkt *cur_pkt;
|
2017-04-02 09:27:09 -04:00
|
|
|
|
|
|
|
#define STATE_NEW 0
|
|
|
|
#define STATE_CONNECTING 1
|
|
|
|
#define STATE_CONNECTED 2
|
|
|
|
#define STATE_PEER_CLOSED 3
|
|
|
|
int8_t state;
|
2017-03-30 17:04:31 -04:00
|
|
|
} socket_obj_t;
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t socket_type;
|
|
|
|
|
2017-04-05 06:34:37 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
|
|
|
|
|
2017-03-31 02:10:30 -04:00
|
|
|
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]));
|
|
|
|
}
|
|
|
|
|
2017-04-01 02:20:07 -04:00
|
|
|
// Copy data from Zephyr net_buf chain into linear buffer.
|
2017-05-13 08:47:47 -04:00
|
|
|
// 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
|
2017-04-01 02:20:07 -04:00
|
|
|
// the whole fragment chain to do its deeds, and that's minor comparing
|
|
|
|
// to the fact that it copies data byte by byte).
|
2017-05-13 08:47:47 -04:00
|
|
|
static char *net_pkt_gather(struct net_pkt *pkt, char *to, unsigned max_len) {
|
|
|
|
struct net_buf *tmp = pkt->frags;
|
2017-04-01 02:20:07 -04:00
|
|
|
|
|
|
|
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;
|
2017-05-13 08:47:47 -04:00
|
|
|
tmp = net_pkt_frag_del(pkt, NULL, tmp);
|
2017-04-01 02:20:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for incoming packets.
|
2017-05-13 08:47:47 -04:00
|
|
|
static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, int status, void *user_data) {
|
2017-04-01 02:20:07 -04:00
|
|
|
socket_obj_t *socket = (socket_obj_t*)user_data;
|
2017-05-13 08:47:47 -04:00
|
|
|
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));
|
2017-04-01 02:20:07 -04:00
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
2017-04-11 08:24:33 -04:00
|
|
|
#if DEBUG > 1
|
2017-05-13 08:47:47 -04:00
|
|
|
net_pkt_print_frags(pkt);
|
2017-04-11 08:24:33 -04:00
|
|
|
#endif
|
2017-04-01 02:20:07 -04:00
|
|
|
|
2017-04-02 09:27:09 -04:00
|
|
|
// if net_buf == NULL, EOF
|
2017-05-13 08:47:47 -04:00
|
|
|
if (pkt == NULL) {
|
|
|
|
struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q);
|
|
|
|
if (last_pkt == NULL) {
|
2017-04-06 03:11:04 -04:00
|
|
|
socket->state = STATE_PEER_CLOSED;
|
|
|
|
DEBUG_printf("Marked socket %p as peer-closed\n", socket);
|
|
|
|
} else {
|
|
|
|
// We abuse "buf_sent" flag to store EOF flag
|
2017-05-13 08:47:47 -04:00
|
|
|
net_pkt_set_sent(last_pkt, true);
|
|
|
|
DEBUG_printf("Set EOF flag on %p\n", last_pkt);
|
2017-04-06 03:11:04 -04:00
|
|
|
}
|
2017-04-02 09:27:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that "EOF flag" is not set
|
2017-05-13 08:47:47 -04:00
|
|
|
net_pkt_set_sent(pkt, false);
|
2017-04-02 09:27:09 -04:00
|
|
|
|
2017-04-13 15:19:16 -04:00
|
|
|
// We don't care about packet header, so get rid of it asap
|
2017-05-13 08:47:47 -04:00
|
|
|
unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data;
|
|
|
|
net_buf_pull(pkt->frags, header_len);
|
2017-04-13 15:19:16 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
k_fifo_put(&socket->recv_q, pkt);
|
2017-04-01 02:20:07 -04:00
|
|
|
}
|
|
|
|
|
2017-04-07 17:33:09 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-04-03 23:28:14 -04:00
|
|
|
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);
|
2017-05-13 08:47:47 -04:00
|
|
|
socket->cur_pkt = NULL;
|
2017-04-03 23:28:14 -04:00
|
|
|
socket->state = STATE_NEW;
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
// 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);
|
|
|
|
|
2017-04-03 23:28:14 -04:00
|
|
|
socket_obj_t *socket = socket_new();
|
2017-03-30 17:04:31 -04:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2017-04-03 23:19:23 -04:00
|
|
|
return MP_OBJ_FROM_PTR(socket);
|
2017-03-30 17:04:31 -04:00
|
|
|
}
|
|
|
|
|
2017-03-31 02:10:30 -04:00
|
|
|
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)));
|
2017-04-07 17:30:17 -04:00
|
|
|
// 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));
|
|
|
|
}
|
2017-03-31 02:10:30 -04:00
|
|
|
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));
|
2017-04-01 02:20:07 -04:00
|
|
|
DEBUG_printf("Setting recv cb after connect()\n");
|
|
|
|
RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
|
2017-03-31 02:10:30 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
|
|
|
|
|
2017-04-07 09:47:10 -04:00
|
|
|
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));
|
2017-04-07 17:33:09 -04:00
|
|
|
RAISE_ERRNO(net_context_accept(socket->ctx, sock_accepted_cb, K_NO_WAIT, socket));
|
2017-04-07 09:47:10 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);
|
|
|
|
|
2017-04-07 17:33:09 -04:00
|
|
|
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);
|
|
|
|
|
2017-04-25 18:05:54 -04:00
|
|
|
STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
|
2017-03-31 16:13:12 -04:00
|
|
|
socket_obj_t *socket = self_in;
|
2017-04-25 18:05:54 -04:00
|
|
|
if (socket->ctx == NULL) {
|
|
|
|
// already closed
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-03-31 16:13:12 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
struct net_pkt *send_pkt = net_pkt_get_tx(socket->ctx, K_FOREVER);
|
2017-04-14 12:46:27 -04:00
|
|
|
|
|
|
|
unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx));
|
|
|
|
// Arbitrary value to account for protocol headers
|
|
|
|
len -= 64;
|
2017-04-25 18:05:54 -04:00
|
|
|
if (len > size) {
|
|
|
|
len = size;
|
2017-04-14 12:46:27 -04:00
|
|
|
}
|
|
|
|
|
2017-05-13 09:05:10 -04:00
|
|
|
// 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);
|
2017-03-31 16:13:12 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
int err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL);
|
2017-04-25 18:05:54 -04:00
|
|
|
if (err < 0) {
|
|
|
|
*errcode = -err;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-03-31 16:13:12 -04:00
|
|
|
|
2017-04-25 18:05:54 -04:00
|
|
|
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);
|
|
|
|
}
|
2017-04-14 12:46:27 -04:00
|
|
|
return mp_obj_new_int_from_uint(len);
|
2017-03-31 16:13:12 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
|
|
|
|
|
2017-04-26 01:42:19 -04:00
|
|
|
STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) {
|
2017-04-01 02:20:07 -04:00
|
|
|
socket_obj_t *socket = self_in;
|
2017-04-26 01:42:19 -04:00
|
|
|
if (socket->ctx == NULL) {
|
|
|
|
// already closed
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2017-04-01 02:20:07 -04:00
|
|
|
|
|
|
|
enum net_sock_type sock_type = net_context_get_type(socket->ctx);
|
|
|
|
unsigned recv_len;
|
|
|
|
|
|
|
|
if (sock_type == SOCK_DGRAM) {
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
|
2017-04-01 02:20:07 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
recv_len = net_pkt_appdatalen(pkt);
|
|
|
|
DEBUG_printf("recv: pkt=%p, appdatalen: %d\n", pkt, recv_len);
|
2017-04-01 02:20:07 -04:00
|
|
|
|
|
|
|
if (recv_len > max_len) {
|
|
|
|
recv_len = max_len;
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
net_pkt_gather(pkt, buf, recv_len);
|
|
|
|
net_pkt_unref(pkt);
|
2017-04-01 02:20:07 -04:00
|
|
|
|
2017-04-02 09:27:09 -04:00
|
|
|
} else if (sock_type == SOCK_STREAM) {
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
if (socket->cur_pkt == NULL) {
|
2017-04-26 02:06:58 -04:00
|
|
|
if (socket->state == STATE_PEER_CLOSED) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
DEBUG_printf("TCP recv: no cur_pkt, getting\n");
|
|
|
|
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
|
2017-04-02 09:27:09 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt);
|
|
|
|
socket->cur_pkt = pkt;
|
2017-04-02 09:27:09 -04:00
|
|
|
}
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
struct net_buf *frag = socket->cur_pkt->frags;
|
2017-04-02 09:27:09 -04:00
|
|
|
if (frag == NULL) {
|
2017-05-13 08:47:47 -04:00
|
|
|
printf("net_pkt has empty fragments on start!\n");
|
2017-04-02 09:27:09 -04:00
|
|
|
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);
|
|
|
|
|
2017-04-26 01:42:19 -04:00
|
|
|
memcpy(buf, frag->data, recv_len);
|
2017-04-02 09:27:09 -04:00
|
|
|
|
|
|
|
if (recv_len != frag_len) {
|
|
|
|
net_buf_pull(frag, recv_len);
|
|
|
|
} else {
|
2017-05-13 08:47:47 -04:00
|
|
|
frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag);
|
2017-04-02 09:27:09 -04:00
|
|
|
if (frag == NULL) {
|
2017-05-13 08:47:47 -04:00
|
|
|
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)) {
|
2017-04-02 09:27:09 -04:00
|
|
|
socket->state = STATE_PEER_CLOSED;
|
|
|
|
}
|
2017-05-13 08:47:47 -04:00
|
|
|
net_pkt_unref(socket->cur_pkt);
|
|
|
|
socket->cur_pkt = NULL;
|
2017-04-02 09:27:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Keep repeating while we're getting empty fragments
|
2017-05-13 08:47:47 -04:00
|
|
|
// Zephyr IP stack appears to have fed empty net_buf's with empty
|
|
|
|
// frags for various TCP control packets - in previous versions.
|
2017-04-02 09:27:09 -04:00
|
|
|
} while (recv_len == 0);
|
2017-04-26 01:42:19 -04:00
|
|
|
}
|
2017-04-02 09:27:09 -04:00
|
|
|
|
2017-04-26 01:42:19 -04:00
|
|
|
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;
|
2017-04-01 02:20:07 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 01:42:19 -04:00
|
|
|
vstr.len = len;
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2017-04-01 02:20:07 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);
|
|
|
|
|
2017-05-12 14:54:32 -04:00
|
|
|
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);
|
|
|
|
|
2017-04-27 07:57:49 -04:00
|
|
|
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);
|
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
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 },
|
2017-03-31 02:10:30 -04:00
|
|
|
{ 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 },
|
2017-04-07 09:47:10 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
|
2017-04-07 17:33:09 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
|
2017-03-31 16:13:12 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
|
2017-04-01 02:20:07 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
|
2017-05-12 14:54:32 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj },
|
2017-04-25 18:06:42 -04:00
|
|
|
|
2017-04-26 01:42:48 -04:00
|
|
|
{ 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},
|
2017-04-25 18:06:42 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
|
2017-04-27 07:57:49 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj },
|
2017-03-30 17:04:31 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
|
|
|
|
|
2017-04-25 18:05:54 -04:00
|
|
|
STATIC const mp_stream_p_t socket_stream_p = {
|
2017-04-26 01:42:19 -04:00
|
|
|
.read = sock_read,
|
2017-04-25 18:05:54 -04:00
|
|
|
.write = sock_write,
|
|
|
|
//.ioctl = sock_ioctl,
|
|
|
|
};
|
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
STATIC const mp_obj_type_t socket_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_socket,
|
|
|
|
.print = socket_print,
|
|
|
|
.make_new = socket_make_new,
|
2017-04-25 18:05:54 -04:00
|
|
|
.protocol = &socket_stream_p,
|
2017-03-30 17:04:31 -04:00
|
|
|
.locals_dict = (mp_obj_t)&socket_locals_dict,
|
|
|
|
};
|
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
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);
|
2017-05-13 08:22:22 -04:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL));
|
2017-05-13 08:47:47 -04:00
|
|
|
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));
|
2017-05-13 08:22:22 -04:00
|
|
|
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);
|
|
|
|
}
|
2017-05-13 08:47:47 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pkt_get_info_obj, pkt_get_info);
|
2017-05-13 08:22:22 -04:00
|
|
|
|
2017-03-30 17:04:31 -04:00
|
|
|
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) },
|
2017-05-12 14:55:42 -04:00
|
|
|
|
|
|
|
{ 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) },
|
2017-05-13 08:22:22 -04:00
|
|
|
|
2017-05-13 08:47:47 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_pkt_get_info), (mp_obj_t)&pkt_get_info_obj },
|
2017-03-30 17:04:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|