|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Lucian Copeland for Adafruit Industries |
| 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 "shared-bindings/socketpool/Socket.h" |
| 28 | + |
| 29 | +#include "shared/runtime/interrupt_char.h" |
| 30 | +#include "py/mperrno.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "shared-bindings/socketpool/SocketPool.h" |
| 33 | +#include "supervisor/port.h" |
| 34 | +#include "supervisor/shared/tick.h" |
| 35 | +#include "supervisor/workflow.h" |
| 36 | + |
| 37 | +#include "lwip/err.h" |
| 38 | +#include "lwip/sys.h" |
| 39 | +#include "lwip/netdb.h" |
| 40 | + |
| 41 | +#define MAX_SOCKETS (8) |
| 42 | + |
| 43 | +STATIC mp_obj_t open_socket_objs[MAX_SOCKETS]; |
| 44 | +STATIC bool user_socket[MAX_SOCKETS]; |
| 45 | + |
| 46 | +void socket_user_reset(void) { |
| 47 | + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { |
| 48 | + if (open_socket_objs[i] && user_socket[i]) { |
| 49 | + // shut 'er down |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// The writes below send an event to the socket select task so that it redoes the |
| 55 | +// select with the new open socket set. |
| 56 | + |
| 57 | +STATIC bool register_open_socket(mp_obj_t obj) { |
| 58 | + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { |
| 59 | + if (!open_socket_objs[i]) { |
| 60 | + open_socket_objs[i] = obj; |
| 61 | + user_socket[i] = false; |
| 62 | + return true; |
| 63 | + } |
| 64 | + } |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +STATIC void unregister_open_socket(mp_obj_t obj) { |
| 69 | + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { |
| 70 | + if (open_socket_objs[i] == obj) { |
| 71 | + open_socket_objs[i] = NULL; |
| 72 | + user_socket[i] = false; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +STATIC void mark_user_socket(mp_obj_t obj) { |
| 78 | + for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_objs); i++) { |
| 79 | + if (open_socket_objs[i] == obj) { |
| 80 | + user_socket[i] = true; |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +bool socketpool_socket(socketpool_socketpool_obj_t *self, |
| 87 | + socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type, |
| 88 | + socketpool_socket_obj_t *sock) { |
| 89 | + mp_raise_NotImplementedError(NULL); |
| 90 | +} |
| 91 | + |
| 92 | +socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self, |
| 93 | + socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) { |
| 94 | + mp_raise_NotImplementedError(NULL); |
| 95 | +} |
| 96 | + |
| 97 | +int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_t *port) { |
| 98 | + mp_raise_NotImplementedError(NULL); |
| 99 | +} |
| 100 | + |
| 101 | +socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_obj_t *self, |
| 102 | + uint8_t *ip, uint32_t *port) { |
| 103 | + mp_raise_NotImplementedError(NULL); |
| 104 | +} |
| 105 | + |
| 106 | +bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self, |
| 107 | + const char *host, size_t hostlen, uint32_t port) { |
| 108 | + mp_raise_NotImplementedError(NULL); |
| 109 | +} |
| 110 | + |
| 111 | +void socketpool_socket_close(socketpool_socket_obj_t *self) { |
| 112 | + mp_raise_NotImplementedError(NULL); |
| 113 | +} |
| 114 | + |
| 115 | +void common_hal_socketpool_socket_close(socketpool_socket_obj_t *self) { |
| 116 | + mp_raise_NotImplementedError(NULL); |
| 117 | +} |
| 118 | + |
| 119 | +void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *self, |
| 120 | + const char *host, size_t hostlen, uint32_t port) { |
| 121 | + mp_raise_NotImplementedError(NULL); |
| 122 | +} |
| 123 | + |
| 124 | +bool common_hal_socketpool_socket_get_closed(socketpool_socket_obj_t *self) { |
| 125 | + mp_raise_NotImplementedError(NULL); |
| 126 | +} |
| 127 | + |
| 128 | +bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *self) { |
| 129 | + mp_raise_NotImplementedError(NULL); |
| 130 | +} |
| 131 | + |
| 132 | +bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t *self, int backlog) { |
| 133 | + mp_raise_NotImplementedError(NULL); |
| 134 | +} |
| 135 | + |
| 136 | +mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *self, |
| 137 | + uint8_t *buf, uint32_t len, uint8_t *ip, uint32_t *port) { |
| 138 | + mp_raise_NotImplementedError(NULL); |
| 139 | +} |
| 140 | + |
| 141 | +int socketpool_socket_recv_into(socketpool_socket_obj_t *self, |
| 142 | + const uint8_t *buf, uint32_t len) { |
| 143 | + mp_raise_NotImplementedError(NULL); |
| 144 | +} |
| 145 | + |
| 146 | +mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { |
| 147 | + mp_raise_NotImplementedError(NULL); |
| 148 | +} |
| 149 | + |
| 150 | +int socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { |
| 151 | + mp_raise_NotImplementedError(NULL); |
| 152 | +} |
| 153 | + |
| 154 | +mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) { |
| 155 | + mp_raise_NotImplementedError(NULL); |
| 156 | +} |
| 157 | + |
| 158 | +mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *self, |
| 159 | + const char *host, size_t hostlen, uint32_t port, const uint8_t *buf, uint32_t len) { |
| 160 | + mp_raise_NotImplementedError(NULL); |
| 161 | +} |
| 162 | + |
| 163 | +void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t *self, uint32_t timeout_ms) { |
| 164 | + mp_raise_NotImplementedError(NULL); |
| 165 | +} |
0 commit comments