modusocket.c 17.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * 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"
31
#include "py/stream.h"
32

33
#include <stdio.h>
34
#include <zephyr.h>
35
36
// Zephyr's generated version header
#include <version.h>
37
38
39
#include <net/net_context.h>
#include <net/nbuf.h>

40
41
#define DEBUG 0
#if DEBUG // print debugging info
42
43
44
45
46
#define DEBUG_printf printf
#else // don't print debugging info
#define DEBUG_printf(...) (void)0
#endif

47
48
49
typedef struct _socket_obj_t {
    mp_obj_base_t base;
    struct net_context *ctx;
50
51
52
53
    union {
        struct k_fifo recv_q;
        struct k_fifo accept_q;
    };
54
55
56
57
58
59
60
    struct net_buf *cur_buf;

    #define STATE_NEW 0
    #define STATE_CONNECTING 1
    #define STATE_CONNECTED 2
    #define STATE_PEER_CLOSED 3
    int8_t state;
61
62
63
64
} socket_obj_t;

STATIC const mp_obj_type_t socket_type;

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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);
}

95
96
97
98
// Helper functions

#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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]));
}

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copy data from Zephyr net_buf chain into linear buffer.
// We don't use net_nbuf_read(), because it's weird (e.g., we'd like to
// free processed data fragment ASAP, while net_nbuf_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_buf_gather(struct net_buf *buf, char *to, unsigned max_len) {
    struct net_buf *tmp = buf->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_buf_frag_del(buf, tmp);
    }

    return to;
}

// Callback for incoming packets.
static void sock_received_cb(struct net_context *context, struct net_buf *net_buf, int status, void *user_data) {
    socket_obj_t *socket = (socket_obj_t*)user_data;
    DEBUG_printf("recv cb: context: %p, status: %d, buf: %p", context, status, net_buf);
    if (net_buf) {
        DEBUG_printf(" (sz=%d, l=%d), token: %p", net_buf->size, net_buf->len, net_nbuf_token(net_buf));
    }
    DEBUG_printf("\n");
147
148
149
    #if DEBUG > 1
    net_nbuf_print_frags(net_buf);
    #endif
150

151
152
    // if net_buf == NULL, EOF
    if (net_buf == NULL) {
153
        struct net_buf *last_buf = _k_fifo_peek_tail(&socket->recv_q);
154
155
156
157
158
159
160
161
        if (last_buf == NULL) {
            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
            net_nbuf_set_buf_sent(last_buf, true);
            DEBUG_printf("Set EOF flag on %p\n", last_buf);
        }
162
163
164
165
166
167
        return;
    }

    // Make sure that "EOF flag" is not set
    net_nbuf_set_buf_sent(net_buf, false);

168
169
170
171
    // We don't care about packet header, so get rid of it asap
    unsigned header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data;
    net_buf_pull(net_buf->frags, header_len);

172
173
174
175
176
    // net_buf->frags will be overwritten by fifo, so save it
    net_nbuf_set_token(net_buf, net_buf->frags);
    k_fifo_put(&socket->recv_q, net_buf);
}

177
178
179
180
181
182
183
184
185
// 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);
}

186
187
188
189
190
191
192
193
194
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_buf = NULL;
    socket->state = STATE_NEW;
    return socket;
}

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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);

210
    socket_obj_t *socket = socket_new();
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

    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));

235
    return MP_OBJ_FROM_PTR(socket);
236
237
}

238
239
240
241
242
243
244
245
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)));
246
247
248
249
250
251
252
    // 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));
    }
253
254
255
256
257
258
259
260
261
262
263
264
    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));
265
266
    DEBUG_printf("Setting recv cb after connect()\n");
    RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
267
268
269
270
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);

271
272
273
274
275
276
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));
277
    RAISE_ERRNO(net_context_accept(socket->ctx, sock_accepted_cb, K_NO_WAIT, socket));
278
279
280
281
    return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen);

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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);

304
STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
305
    socket_obj_t *socket = self_in;
306
307
308
309
310
    if (socket->ctx == NULL) {
        // already closed
        *errcode = EBADF;
        return MP_STREAM_ERROR;
    }
311
312

    struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER);
313
314
315
316

    unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx));
    // Arbitrary value to account for protocol headers
    len -= 64;
317
318
    if (len > size) {
        len = size;
319
320
    }

321
    if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) {
322
        len = net_buf_frags_len(send_buf);
323
324
    }

325
326
327
328
329
    int err = net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL);
    if (err < 0) {
        *errcode = -err;
        return MP_STREAM_ERROR;
    }
330

331
332
333
334
335
336
337
338
339
340
341
    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);
    }
342
    return mp_obj_new_int_from_uint(len);
343
344
345
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);

346
STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) {
347
    socket_obj_t *socket = self_in;
348
349
350
351
352
    if (socket->ctx == NULL) {
        // already closed
        *errcode = EBADF;
        return MP_STREAM_ERROR;
    }
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

    enum net_sock_type sock_type = net_context_get_type(socket->ctx);
    unsigned recv_len;

    if (sock_type == SOCK_DGRAM) {

        struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER);
        // Restore ->frags overwritten by fifo
        net_buf->frags = net_nbuf_token(net_buf);

        recv_len = net_nbuf_appdatalen(net_buf);
        DEBUG_printf("recv: net_buf=%p, appdatalen: %d\n", net_buf, recv_len);

        if (recv_len > max_len) {
            recv_len = max_len;
        }

370
        net_buf_gather(net_buf, buf, recv_len);
371
372
        net_nbuf_unref(net_buf);

373
374
375
376
377
    } else if (sock_type == SOCK_STREAM) {

        do {

            if (socket->cur_buf == NULL) {
378
379
380
381
                if (socket->state == STATE_PEER_CLOSED) {
                    return 0;
                }

382
383
384
385
386
                DEBUG_printf("TCP recv: no cur_buf, getting\n");
                struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER);
                // Restore ->frags overwritten by fifo
                net_buf->frags = net_nbuf_token(net_buf);

387
                DEBUG_printf("TCP recv: new cur_buf: %p\n", net_buf);
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
                socket->cur_buf = net_buf;
            }

            struct net_buf *frag = socket->cur_buf->frags;
            if (frag == NULL) {
                printf("net_buf 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);

404
            memcpy(buf, frag->data, recv_len);
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

            if (recv_len != frag_len) {
                net_buf_pull(frag, recv_len);
            } else {
                frag = net_buf_frag_del(socket->cur_buf, frag);
                if (frag == NULL) {
                    DEBUG_printf("Finished processing net_buf %p\n", socket->cur_buf);
                    // If "buf_sent" flag was set, it's last packet and we reached EOF
                    if (net_nbuf_buf_sent(socket->cur_buf)) {
                        socket->state = STATE_PEER_CLOSED;
                    }
                    net_nbuf_unref(socket->cur_buf);
                    socket->cur_buf = NULL;
                }
            }
        // Keep repeating while we're getting empty fragments
        // Zephyr IP stack appears to feed empty net_buf's with empty
        // frags for various TCP control packets.
        } while (recv_len == 0);
424
    }
425

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    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;
446
447
    }

448
449
    vstr.len = len;
    return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
450
451
452
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv);

453
454
455
456
457
458
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);

459
460
461
462
463
464
465
466
467
468
469
470
471
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 },
472
473
    { 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 },
474
    { MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj },
475
    { MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj },
476
    { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj },
477
    { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj },
478

479
480
481
    { 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},
482
    { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
483
    { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj },
484
485
486
};
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);

487
STATIC const mp_stream_p_t socket_stream_p = {
488
    .read = sock_read,
489
490
491
492
    .write = sock_write,
    //.ioctl = sock_ioctl,
};

493
494
495
496
497
STATIC const mp_obj_type_t socket_type = {
    { &mp_type_type },
    .name = MP_QSTR_socket,
    .print = socket_print,
    .make_new = socket_make_new,
498
    .protocol = &socket_stream_p,
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
    .locals_dict = (mp_obj_t)&socket_locals_dict,
};

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) },
};

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