ncm: add weak callback for initial link state

netd_init resets link_is_up to a compile-time default, which is
incorrect when the host reboots without power-cycling the device.
Add tud_network_default_link_state_cb() so applications can return
the actual physical link state. The weak default preserves existing
CFG_TUD_NCM_DEFAULT_LINK_UP behaviour.
This commit is contained in:
Cedric Van den Bergh
2026-06-16 23:28:07 +01:00
parent d9f736dcf9
commit 6d3d33d997
2 changed files with 13 additions and 6 deletions

View File

@ -152,6 +152,14 @@ TU_ATTR_WEAK void tud_network_set_packet_filter_cb(uint16_t packet_filter) {
(void) packet_filter;
}
TU_ATTR_WEAK bool tud_network_default_link_state_cb(void) {
#ifdef CFG_TUD_NCM_DEFAULT_LINK_UP
return CFG_TUD_NCM_DEFAULT_LINK_UP;
#else
return true;
#endif
}
/**
* This is the NTB parameter structure
*
@ -852,12 +860,7 @@ void netd_init(void) {
for (int i = 0; i < RECV_NTB_N; ++i) {
ncm_interface.recv_free_ntb[i] = &ncm_epbuf.recv[i].ntb;
}
// Default link state - can be configured via CFG_TUD_NCM_DEFAULT_LINK_UP
#ifdef CFG_TUD_NCM_DEFAULT_LINK_UP
ncm_interface.link_is_up = CFG_TUD_NCM_DEFAULT_LINK_UP;
#else
ncm_interface.link_is_up = true; // Default to link up if not set.
#endif
ncm_interface.link_is_up = tud_network_default_link_state_cb();
} // netd_init
/**

View File

@ -106,6 +106,10 @@ extern uint8_t tud_network_mac_address[6];
// Optional callback: informs the application about host requested packet filter bits
void tud_network_set_packet_filter_cb(uint16_t packet_filter);
// Optional callback: called during netd_init() to get the initial link state.
// Override to return the actual physical link state instead of the compile-time default.
bool tud_network_default_link_state_cb(void);
// Set the network link state (up/down) and notify the host
void tud_network_link_state(uint8_t rhport, bool is_up);