From db02ded027a63cc9a3c8405de79362536dd2d0d9 Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Sat, 31 Oct 2026 08:25:48 -0800 Subject: [PATCH] fix(drivers/uavcannode): make CANNODE_NODE_ID the sole node ID policy (#27644) (#29532) * fix(drivers/uavcannode): make CANNODE_NODE_ID the sole node ID policy The startup bootloader_app_shared_write() never reached the bootloader: it only accepts App-signed data and invalidates the shared region on every boot. The only consumer was uavcannode itself on a restart without a reset (nsh stop/start), where the persisted bus_speed=0 made the CAN driver init fail and left the node off the bus. The bootloader learns our node ID exclusively through the firmware update handoff in cb_beginfirmware_update, which already carries the static ID. CANNODE_NODE_ID now overrides the bootloader handoff only when nonzero, so the default (0) reuses the ID the bootloader already allocated instead of running allocation a second time. The handoff is ignored unless it carries a valid bitrate, healing regions poisoned by firmware that wrote bus_speed=0. Max is 125 because 126/127 are reserved for debug tools by DroneCAN convention, matching UAVCAN_NODE_ID. * fix(drivers/uavcannode): harden bootloader handoff validation Invalidate the shared region whenever it was read valid, not only when the handoff is accepted, so stale data cannot linger on boards whose bootloader never invalidates it. Require a plausible bitrate (the CANNODE_BITRATE minimum) rather than nonzero: the canbootloader alt-app update path can hand off bus_speed = CAN_UNDEFINED (999). Reword the invalid-param error, which claimed dynamic allocation while keeping the bootloader handoff. --------- (cherry picked from commit 0524a374d91c5f057abd9b065f84aaf35e40a4df) Signed-off-by: Jacob Dahl --- docs/en/dronecan/ark_cannode.md | 2 +- docs/en/dronecan/ark_flow.md | 2 +- docs/en/dronecan/ark_flow_mr.md | 2 +- docs/en/dronecan/ark_gps.md | 2 +- docs/en/dronecan/ark_rtk_gps.md | 2 +- docs/en/dronecan/ark_rtk_gps_l1_l2.md | 2 +- docs/en/dronecan/ark_x20_rtk_gps.md | 2 +- docs/en/dronecan/index.md | 4 +-- docs/en/dronecan/px4_cannode_fw.md | 2 +- src/drivers/uavcannode/UavcanNode.cpp | 32 ++++++++++--------- src/drivers/uavcannode/uavcannode_params.yaml | 6 +++- 11 files changed, 32 insertions(+), 26 deletions(-) diff --git a/docs/en/dronecan/ark_cannode.md b/docs/en/dronecan/ark_cannode.md index 0c04ae1bcfd..bba9dbc9a0c 100644 --- a/docs/en/dronecan/ark_cannode.md +++ b/docs/en/dronecan/ark_cannode.md @@ -85,7 +85,7 @@ On the ARK CANnode, you may need to configure the following parameters: | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. | ## LED Meanings diff --git a/docs/en/dronecan/ark_flow.md b/docs/en/dronecan/ark_flow.md index c3c7906c87e..6ff572f3b02 100644 --- a/docs/en/dronecan/ark_flow.md +++ b/docs/en/dronecan/ark_flow.md @@ -112,7 +112,7 @@ On the ARK Flow, you may need to configure the following parameters: | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. | ## LED Meanings diff --git a/docs/en/dronecan/ark_flow_mr.md b/docs/en/dronecan/ark_flow_mr.md index f6207cae731..f8ba7b48801 100644 --- a/docs/en/dronecan/ark_flow_mr.md +++ b/docs/en/dronecan/ark_flow_mr.md @@ -107,7 +107,7 @@ You may need to [configure the following parameters](../dronecan/index.md#qgc-ca | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. | ## LED Meanings diff --git a/docs/en/dronecan/ark_gps.md b/docs/en/dronecan/ark_gps.md index 7bd487c1cc8..3d1a0b060f4 100644 --- a/docs/en/dronecan/ark_gps.md +++ b/docs/en/dronecan/ark_gps.md @@ -99,7 +99,7 @@ You may need to [configure the following parameters](../dronecan/index.md#qgc-ca | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. Set to `1` if this is the last node on the CAN bus. | ## LED Meanings diff --git a/docs/en/dronecan/ark_rtk_gps.md b/docs/en/dronecan/ark_rtk_gps.md index 8954430d819..97a9195a689 100644 --- a/docs/en/dronecan/ark_rtk_gps.md +++ b/docs/en/dronecan/ark_rtk_gps.md @@ -94,7 +94,7 @@ You may need to [configure the following parameters](../dronecan/index.md#qgc-ca | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. Set to `1` if this is the last node on the CAN bus. | ### Setting Up Rover and Fixed Base diff --git a/docs/en/dronecan/ark_rtk_gps_l1_l2.md b/docs/en/dronecan/ark_rtk_gps_l1_l2.md index 1ea1fb494da..f8149b233aa 100644 --- a/docs/en/dronecan/ark_rtk_gps_l1_l2.md +++ b/docs/en/dronecan/ark_rtk_gps_l1_l2.md @@ -93,7 +93,7 @@ You may need to [configure the following parameters](../dronecan/index.md#qgc-ca | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. Set to `1` if this is the last node on the CAN bus. | ### Setting Up Rover and Fixed Base diff --git a/docs/en/dronecan/ark_x20_rtk_gps.md b/docs/en/dronecan/ark_x20_rtk_gps.md index 59628851032..67e0fb65f40 100644 --- a/docs/en/dronecan/ark_x20_rtk_gps.md +++ b/docs/en/dronecan/ark_x20_rtk_gps.md @@ -96,7 +96,7 @@ You may need to [configure the following parameters](../dronecan/index.md#qgc-ca | Parameter | Description | | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-127 to use a static node ID. | +| [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) | CAN node ID (0 for dynamic allocation). If set to 0 (default), dynamic node allocation is used. Set to 1-125 to use a static node ID. | | [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM) | CAN built-in bus termination. Set to `1` if this is the last node on the CAN bus. | ### Setting Up Rover and Fixed Base diff --git a/docs/en/dronecan/index.md b/docs/en/dronecan/index.md index 6bcdf313533..9e105910929 100644 --- a/docs/en/dronecan/index.md +++ b/docs/en/dronecan/index.md @@ -103,7 +103,7 @@ The parameter is set to 1 by default. Devices running the [PX4 DroneCAN firmware](px4_cannode_fw.md) (such as [ARK CANnode](ark_cannode.md)) can use the [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) parameter to set a static node ID. -Set it to 0 (default) for dynamic allocation, or to a value between 1-127 to use a specific static node ID. +Set it to 0 (default) for dynamic allocation, or to a value between 1-125 to use a specific static node ID. ::: :::warning @@ -300,7 +300,7 @@ For example, the screenshot below shows the parameters for a CAN GPS with node i Common CANNODE parameters that you can configure include: -- [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID): Set a static node ID (1-127) or use 0 for dynamic allocation. See [PX4 DroneCAN Firmware > Static Node ID](px4_cannode_fw.md#static-node-id) for more information. +- [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID): Set a static node ID (1-125) or use 0 for dynamic allocation. See [PX4 DroneCAN Firmware > Static Node ID](px4_cannode_fw.md#static-node-id) for more information. - [CANNODE_TERM](../advanced_config/parameter_reference.md#CANNODE_TERM): Enable CAN bus termination on the last node in the bus. ## Device Specific Setup diff --git a/docs/en/dronecan/px4_cannode_fw.md b/docs/en/dronecan/px4_cannode_fw.md index f946bc8b4f4..599003e36d1 100644 --- a/docs/en/dronecan/px4_cannode_fw.md +++ b/docs/en/dronecan/px4_cannode_fw.md @@ -38,7 +38,7 @@ However, you can configure a static node ID using the [CANNODE_NODE_ID](../advan To configure a static node ID: -1. Set [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) to a value between 1-127 using [QGroundControl](index.md#qgc-cannode-parameter-configuration) +1. Set [CANNODE_NODE_ID](../advanced_config/parameter_reference.md#CANNODE_NODE_ID) to a value between 1-125 using [QGroundControl](index.md#qgc-cannode-parameter-configuration) 2. Reboot the device To return to dynamic allocation, set `CANNODE_NODE_ID` back to 0. diff --git a/src/drivers/uavcannode/UavcanNode.cpp b/src/drivers/uavcannode/UavcanNode.cpp index 86a350d27cc..84fc4bc69ad 100644 --- a/src/drivers/uavcannode/UavcanNode.cpp +++ b/src/drivers/uavcannode/UavcanNode.cpp @@ -360,7 +360,8 @@ int UavcanNode::init(uavcan::NodeID node_id, UAVCAN_DRIVER::BusEvent &bus_events { _node.setName(board_get_uavcan_hw_name()); - // Was the node_id supplied by the bootloader? + // Nonzero node_id came from the bootloader handoff or CANNODE_NODE_ID; + // zero means dynamic node ID allocation runs in Run() if (node_id != 0) { _node.setNodeID(node_id); @@ -836,14 +837,19 @@ extern "C" int uavcannode_start(int argc, char *argv[]) valid = bootloader_app_shared_read(&shared, BootLoader); } + // Consume the region even when the handoff is rejected below: on boards + // booted by a non-PX4 bootloader nothing else ever invalidates it if (valid == 0) { + bootloader_app_shared_invalidate(); + } + + // Trust the handoff only if it carries a plausible bitrate (CANNODE_BITRATE + // minimum), healing regions poisoned by firmware running previous code + if (valid == 0 && shared.bus_speed >= 20000) { bitrate = shared.bus_speed; node_id = shared.node_id; - // Invalidate to prevent deja vu - bootloader_app_shared_invalidate(); - } else { // Node ID #if defined(SUPPORT_ALT_CAN_BOOTLOADER) @@ -858,24 +864,20 @@ extern "C" int uavcannode_start(int argc, char *argv[]) } } - // Use a static node ID if the parameter is set and in range + // CANNODE_NODE_ID == 0 (default) keeps dynamic node ID allocation, reusing an ID the + // bootloader may have already allocated. 1..125 sets a static node ID, overriding + // anything the bootloader negotiated. The bootloader never sees this parameter: it + // learns our node ID only through the firmware update handoff in cb_beginfirmware_update. int32_t cannode_node_id = 0; param_get(param_find("CANNODE_NODE_ID"), &cannode_node_id); - if (cannode_node_id < 0 || cannode_node_id > uavcan::NodeID::Max) { - PX4_ERR("Invalid static node ID %ld, using dynamic allocation", cannode_node_id); - node_id = 0; + if (cannode_node_id < 0 || cannode_node_id > uavcan::NodeID::MaxRecommendedForRegularNodes) { + PX4_ERR("Invalid CANNODE_NODE_ID %" PRId32 ", ignoring", cannode_node_id); - } else { + } else if (cannode_node_id > 0) { node_id = cannode_node_id; } - // Persist the node ID for the bootloader - bootloader_app_shared_t shared_write = {}; - shared_write.node_id = node_id; - shared_write.bus_speed = 0; // we always want to autobaud - bootloader_app_shared_write(&shared_write, BootLoader); - if ( #if defined(SUPPORT_ALT_CAN_BOOTLOADER) board_booted_by_px4() && diff --git a/src/drivers/uavcannode/uavcannode_params.yaml b/src/drivers/uavcannode/uavcannode_params.yaml index c612db923c4..ae3aa3ec6d3 100644 --- a/src/drivers/uavcannode/uavcannode_params.yaml +++ b/src/drivers/uavcannode/uavcannode_params.yaml @@ -5,10 +5,14 @@ parameters: CANNODE_NODE_ID: description: short: UAVCAN CAN node ID (0 for dynamic allocation) + long: |- + Set to 0 (default) to use dynamic node ID allocation (DNA). + Set to 1-125 to use a static node ID, which must be unique on the bus. type: int32 default: 0 min: 0 - max: 127 + max: 125 + reboot_required: true CANNODE_BITRATE: description: short: UAVCAN CAN bus bitrate