|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Linaro Limited |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/mpconfig.h" |
| 28 | +#ifdef MICROPY_PY_USOCKET |
| 29 | + |
| 30 | +#include "py/runtime.h" |
| 31 | + |
| 32 | +#include <zephyr.h> |
| 33 | +#include <net/net_context.h> |
| 34 | +#include <net/nbuf.h> |
| 35 | + |
| 36 | +typedef struct _socket_obj_t { |
| 37 | + mp_obj_base_t base; |
| 38 | + struct net_context *ctx; |
| 39 | +} socket_obj_t; |
| 40 | + |
| 41 | +STATIC const mp_obj_type_t socket_type; |
| 42 | + |
| 43 | +// Helper functions |
| 44 | + |
| 45 | +#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); } |
| 46 | + |
| 47 | +// Methods |
| 48 | + |
| 49 | +STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 50 | + socket_obj_t *self = self_in; |
| 51 | + if (self->ctx == NULL) { |
| 52 | + mp_printf(print, "<socket NULL>"); |
| 53 | + } else { |
| 54 | + struct net_context *ctx = self->ctx; |
| 55 | + mp_printf(print, "<socket %p type=%d>", ctx, net_context_get_type(ctx)); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +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) { |
| 60 | + mp_arg_check_num(n_args, n_kw, 0, 4, false); |
| 61 | + |
| 62 | + socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); |
| 63 | + socket->base.type = type; |
| 64 | + |
| 65 | + int family = AF_INET; |
| 66 | + int socktype = SOCK_STREAM; |
| 67 | + int proto = -1; |
| 68 | + |
| 69 | + if (n_args >= 1) { |
| 70 | + family = mp_obj_get_int(args[0]); |
| 71 | + if (n_args >= 2) { |
| 72 | + socktype = mp_obj_get_int(args[1]); |
| 73 | + if (n_args >= 3) { |
| 74 | + proto = mp_obj_get_int(args[2]); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (proto == -1) { |
| 80 | + proto = IPPROTO_TCP; |
| 81 | + if (socktype != SOCK_STREAM) { |
| 82 | + proto = IPPROTO_UDP; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx)); |
| 87 | + |
| 88 | + return socket; |
| 89 | +} |
| 90 | + |
| 91 | +STATIC mp_obj_t socket_close(mp_obj_t self_in) { |
| 92 | + socket_obj_t *socket = self_in; |
| 93 | + if (socket->ctx != NULL) { |
| 94 | + RAISE_ERRNO(net_context_put(socket->ctx)); |
| 95 | + socket->ctx = NULL; |
| 96 | + } |
| 97 | + return mp_const_none; |
| 98 | +} |
| 99 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close); |
| 100 | + |
| 101 | +STATIC const mp_map_elem_t socket_locals_dict_table[] = { |
| 102 | + { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj }, |
| 103 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, |
| 104 | +}; |
| 105 | +STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); |
| 106 | + |
| 107 | +STATIC const mp_obj_type_t socket_type = { |
| 108 | + { &mp_type_type }, |
| 109 | + .name = MP_QSTR_socket, |
| 110 | + .print = socket_print, |
| 111 | + .make_new = socket_make_new, |
| 112 | + //.protocol = &socket_stream_p, |
| 113 | + .locals_dict = (mp_obj_t)&socket_locals_dict, |
| 114 | +}; |
| 115 | + |
| 116 | +STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { |
| 117 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) }, |
| 118 | + // objects |
| 119 | + { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type }, |
| 120 | + // class constants |
| 121 | + { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) }, |
| 122 | + { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) }, |
| 123 | + |
| 124 | + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, |
| 125 | + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, |
| 126 | +}; |
| 127 | + |
| 128 | +STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); |
| 129 | + |
| 130 | +const mp_obj_module_t mp_module_usocket = { |
| 131 | + .base = { &mp_type_module }, |
| 132 | + .globals = (mp_obj_dict_t*)&mp_module_usocket_globals, |
| 133 | +}; |
| 134 | + |
| 135 | +#endif // MICROPY_PY_USOCKET |
0 commit comments