diff options
Diffstat (limited to 'shared-module/network')
| -rw-r--r-- | shared-module/network/__init__.c | 18 | ||||
| -rw-r--r-- | shared-module/network/__init__.h | 6 |
2 files changed, 19 insertions, 5 deletions
diff --git a/shared-module/network/__init__.c b/shared-module/network/__init__.c index a674e8478..a45191d8c 100644 --- a/shared-module/network/__init__.c +++ b/shared-module/network/__init__.c @@ -31,11 +31,13 @@ #include "py/mphal.h" #include "py/mperrno.h" +#include "supervisor/shared/tick.h" + #include "shared-bindings/random/__init__.h" #include "shared-module/network/__init__.h" -// mod_network_nic_list needs to be declared in mpconfigport.h +// mod_network_nic_list needs to be declared in mpconfigport.h void network_module_init(void) { @@ -43,17 +45,23 @@ void network_module_init(void) { } void network_module_deinit(void) { + for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { + mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; + mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); + if (nic_type->deinit != NULL) nic_type->deinit(nic); + } + mp_obj_list_set_len(&MP_STATE_PORT(mod_network_nic_list), 0); } void network_module_background(void) { static uint32_t next_tick = 0; - uint32_t this_tick = ticks_ms; + uint32_t this_tick = supervisor_ticks_ms32(); if (this_tick < next_tick) return; next_tick = this_tick + 1000; for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i]; - mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); + mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic); if (nic_type->timer_tick != NULL) nic_type->timer_tick(nic); } } @@ -93,3 +101,7 @@ void network_module_create_random_mac_address(uint8_t *mac) { mac[4] = (uint8_t)(rb2 >> 8); mac[5] = (uint8_t)(rb2); } + +uint16_t network_module_create_random_source_tcp_port(void) { + return 0xc000 | shared_modules_random_getrandbits(14); +} diff --git a/shared-module/network/__init__.h b/shared-module/network/__init__.h index 00a3c3957..f4eb05bb5 100644 --- a/shared-module/network/__init__.h +++ b/shared-module/network/__init__.h @@ -25,11 +25,12 @@ * THE SOFTWARE. */ -void network_module_create_random_mac_address(uint8_t *mac); - #ifndef MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H #define MICROPY_INCLUDED_SHARED_MODULE_NETWORK___INIT___H +void network_module_create_random_mac_address(uint8_t *mac); +uint16_t network_module_create_random_source_tcp_port(void); + #define MOD_NETWORK_IPADDR_BUF_SIZE (4) #define MOD_NETWORK_AF_INET (2) @@ -62,6 +63,7 @@ typedef struct _mod_network_nic_type_t { int (*settimeout)(struct _mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno); int (*ioctl)(struct _mod_network_socket_obj_t *socket, mp_uint_t request, mp_uint_t arg, int *_errno); void (*timer_tick)(struct _mod_network_socket_obj_t *socket); + void (*deinit)(struct _mod_network_socket_obj_t *socket); } mod_network_nic_type_t; typedef struct _mod_network_socket_obj_t { |
