diff --git a/features/nanostack/mbed-mesh-api/source/NanostackEMACInterface.cpp b/features/nanostack/mbed-mesh-api/source/NanostackEMACInterface.cpp index 989b6f8c88..a62acd69ff 100644 --- a/features/nanostack/mbed-mesh-api/source/NanostackEMACInterface.cpp +++ b/features/nanostack/mbed-mesh-api/source/NanostackEMACInterface.cpp @@ -7,6 +7,7 @@ #include "nsdynmemLIB.h" #include "arm_hal_phy.h" #include "EMAC.h" +#include "enet_tasklet.h" class EMACPhy : public NanostackEthernetPhy { public: @@ -137,6 +138,7 @@ int8_t EMACPhy::phy_register() emac.set_memory_manager(memory_manager); emac.set_link_input_cb(mbed::callback(this, &EMACPhy::emac_phy_rx)); + emac.set_link_state_cb(enet_tasklet_link_state_changed); if (!emac.power_up()) { return -1; diff --git a/features/nanostack/mbed-mesh-api/source/ethernet_tasklet.c b/features/nanostack/mbed-mesh-api/source/ethernet_tasklet.c index d86b98a36d..818772ccba 100644 --- a/features/nanostack/mbed-mesh-api/source/ethernet_tasklet.c +++ b/features/nanostack/mbed-mesh-api/source/ethernet_tasklet.c @@ -119,6 +119,25 @@ void enet_tasklet_main(arm_event_s *event) case APPLICATION_EVENT: if (event->event_id == APPL_EVENT_CONNECT) { enet_tasklet_configure_and_connect_to_network(); + } else if (event->event_id == APPL_BACKHAUL_INTERFACE_PHY_UP) { + // Ethernet cable has been plugged in + arm_nwk_interface_configure_ipv6_bootstrap_set( + tasklet_data_ptr->network_interface_id, NET_IPV6_BOOTSTRAP_AUTONOMOUS, NULL); + enet_tasklet_configure_and_connect_to_network(); + + if (tasklet_data_ptr->poll_network_status_timeout != NULL) { + // Restart poll timer + eventOS_timeout_cancel(tasklet_data_ptr->poll_network_status_timeout); + } + tasklet_data_ptr->poll_network_status_timeout = + eventOS_timeout_every_ms(enet_tasklet_poll_network_status, 2000, NULL); + } else if (event->event_id == APPL_BACKHAUL_INTERFACE_PHY_DOWN) { + // Ethernet cable has been removed + arm_nwk_interface_down(tasklet_data_ptr->network_interface_id); + eventOS_timeout_cancel(tasklet_data_ptr->poll_network_status_timeout); + tasklet_data_ptr->poll_network_status_timeout = NULL; + memset(tasklet_data_ptr->ip, 0x0, 16); + enet_tasklet_network_state_changed(MESH_BOOTSTRAP_STARTED); } break; @@ -300,3 +319,15 @@ int8_t enet_tasklet_network_init(int8_t device_id) tasklet_data_ptr->network_interface_id, NET_IPV6_BOOTSTRAP_AUTONOMOUS, NULL); return tasklet_data_ptr->network_interface_id; } + +void enet_tasklet_link_state_changed(bool up) +{ + arm_event_s event = { + .receiver = tasklet_data_ptr->tasklet, + .sender = tasklet_data_ptr->tasklet, + .event_type = APPLICATION_EVENT, + .priority = ARM_LIB_LOW_PRIORITY_EVENT, + .event_id = up ? APPL_BACKHAUL_INTERFACE_PHY_UP : APPL_BACKHAUL_INTERFACE_PHY_DOWN, + }; + eventOS_event_send(&event); +} diff --git a/features/nanostack/mbed-mesh-api/source/include/enet_tasklet.h b/features/nanostack/mbed-mesh-api/source/include/enet_tasklet.h index f8a8edf3b1..e04752856e 100644 --- a/features/nanostack/mbed-mesh-api/source/include/enet_tasklet.h +++ b/features/nanostack/mbed-mesh-api/source/include/enet_tasklet.h @@ -26,6 +26,7 @@ void enet_tasklet_init(void); uint8_t enet_tasklet_network_init(int8_t); int8_t enet_tasklet_connect(void (*)(mesh_connection_status_t mesh_status), int8_t nwk_interface_id); void enet_tasklet_disconnect(); +void enet_tasklet_link_state_changed(bool up); #ifdef __cplusplus } diff --git a/features/nanostack/mbed-mesh-api/source/include/mesh_system.h b/features/nanostack/mbed-mesh-api/source/include/mesh_system.h index 43fef4c4f0..66b0a9bbaf 100644 --- a/features/nanostack/mbed-mesh-api/source/include/mesh_system.h +++ b/features/nanostack/mbed-mesh-api/source/include/mesh_system.h @@ -25,7 +25,11 @@ extern "C" { /* * Event type for connecting */ -#define APPL_EVENT_CONNECT 0x01 +enum { + APPL_EVENT_CONNECT = 0x01, + APPL_BACKHAUL_INTERFACE_PHY_DOWN, + APPL_BACKHAUL_INTERFACE_PHY_UP +}; /* * \brief Send application connect event to receiver tasklet to