From 50f31cc902d284db9f6ddbd60101608cfdbdbe7b Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 2 Nov 2023 09:10:38 +0100 Subject: [PATCH] extmod/modnetwork: Add deinit function to NIC protocol. This is usually called on soft-reboot, a NIC can implement this to do any necessary cleaning up (such as invalidating root pointers). Signed-off-by: iabdalkader --- extmod/modnetwork.c | 9 +++++++++ extmod/modnetwork.h | 1 + 2 files changed, 10 insertions(+) diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c index f5734af9b5..527d1729e6 100644 --- a/extmod/modnetwork.c +++ b/extmod/modnetwork.c @@ -65,6 +65,15 @@ void mod_network_init(void) { } void mod_network_deinit(void) { + #if !MICROPY_PY_LWIP + 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]; + const mod_network_nic_protocol_t *nic_protocol = MP_OBJ_TYPE_GET_SLOT(mp_obj_get_type(nic), protocol); + if (nic_protocol->deinit) { + nic_protocol->deinit(); + } + } + #endif } void mod_network_register_nic(mp_obj_t nic) { diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h index e3239c2a05..0a7897faaa 100644 --- a/extmod/modnetwork.h +++ b/extmod/modnetwork.h @@ -80,6 +80,7 @@ struct _mod_network_socket_obj_t; typedef struct _mod_network_nic_protocol_t { // API for non-socket operations int (*gethostbyname)(mp_obj_t nic, const char *name, mp_uint_t len, uint8_t *ip_out); + void (*deinit)(void); // API for socket operations; return -1 on error int (*socket)(struct _mod_network_socket_obj_t *socket, int *_errno);