2019-09-21 00:16:06 -03:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2023-04-08 01:09:10 -03:00
|
|
|
#if HAL_ENABLE_DRONECAN_DRIVERS
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2023-04-06 21:18:01 -03:00
|
|
|
#include "AP_DroneCAN_DNA_Server.h"
|
|
|
|
#include "AP_DroneCAN.h"
|
2019-09-21 00:16:06 -03:00
|
|
|
#include <StorageManager/StorageManager.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2020-12-31 17:04:08 -04:00
|
|
|
#include <AP_Logger/AP_Logger.h>
|
2021-09-29 04:36:41 -03:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
2023-01-04 21:09:23 -04:00
|
|
|
#include <stdio.h>
|
2019-09-21 00:16:06 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2024-08-17 14:24:17 -03:00
|
|
|
// FORMAT REVISION DREAMS (things to address if the NodeRecord needs to be changed substantially)
|
2024-07-13 18:12:52 -03:00
|
|
|
// * have DNA server accept only a 16 byte local UID to avoid overhead from variable sized hash
|
|
|
|
// * have a real empty flag for entries and/or use a CRC which is not zero for an input of all zeros
|
2024-09-01 00:04:36 -03:00
|
|
|
// * fix FNV-1a hash folding to be to 48 bits (6 bytes) instead of 56
|
2024-07-13 18:12:52 -03:00
|
|
|
|
2024-08-17 14:24:17 -03:00
|
|
|
#define NODERECORD_MAGIC 0xAC01
|
|
|
|
#define NODERECORD_MAGIC_LEN 2 // uint16_t
|
2019-09-21 00:16:06 -03:00
|
|
|
#define MAX_NODE_ID 125
|
2024-08-17 14:24:17 -03:00
|
|
|
#define NODERECORD_LOC(node_id) ((node_id * sizeof(NodeRecord)) + NODERECORD_MAGIC_LEN)
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2023-04-08 01:27:51 -03:00
|
|
|
#define debug_dronecan(level_debug, fmt, args...) do { AP::can().log_text(level_debug, "DroneCAN", fmt, ##args); } while (0)
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-05 12:31:01 -03:00
|
|
|
// database is currently shared by all DNA servers
|
|
|
|
AP_DroneCAN_DNA_Server::Database AP_DroneCAN_DNA_Server::db;
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// initialize database (storage accessor is always replaced with the one supplied)
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::init(StorageAccess *storage_)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 16:49:32 -03:00
|
|
|
// storage size must be synced with StorageCANDNA entry in StorageManager.cpp
|
|
|
|
static_assert(NODERECORD_LOC(MAX_NODE_ID+1) <= 1024, "DNA storage too small");
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// might be called from multiple threads if multiple servers use the same database
|
|
|
|
WITH_SEMAPHORE(sem);
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
storage = storage_; // use supplied accessor
|
|
|
|
|
|
|
|
// validate magic number
|
|
|
|
uint16_t magic = storage->read_uint16(0);
|
|
|
|
if (magic != NODERECORD_MAGIC) {
|
|
|
|
reset(); // resetting the database will put the magic back
|
|
|
|
}
|
|
|
|
|
|
|
|
// check and note each possible node ID's registration's presence
|
|
|
|
for (uint8_t i = 1; i <= MAX_NODE_ID; i++) {
|
|
|
|
if (check_registration(i)) {
|
|
|
|
node_registered.set(i);
|
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// remove all registrations from the database
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::reset()
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 16:49:32 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
|
|
|
NodeRecord record;
|
|
|
|
memset(&record, 0, sizeof(record));
|
|
|
|
node_registered.clearall();
|
|
|
|
|
|
|
|
// all-zero record means no registration
|
|
|
|
// ensure node ID 0 is cleared even if we can't use it so we know the state
|
|
|
|
for (uint8_t i = 0; i <= MAX_NODE_ID; i++) {
|
|
|
|
write_record(record, i);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2024-07-13 15:55:01 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// mark the magic at the start to indicate a valid (and reset) database
|
|
|
|
storage->write_uint16(0, NODERECORD_MAGIC);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// handle initializing the server with its own node ID and unique ID
|
2024-09-08 21:09:16 -03:00
|
|
|
void AP_DroneCAN_DNA_Server::Database::init_server(uint8_t own_node_id, const uint8_t own_unique_id[], uint8_t own_unique_id_len)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 16:49:32 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
2024-07-13 15:55:01 -03:00
|
|
|
|
2024-09-08 21:09:16 -03:00
|
|
|
// register server node ID and unique ID if not correctly registered. note
|
|
|
|
// that ardupilot mixes the node ID into the unique ID so changing the node
|
|
|
|
// ID will "leak" the old node ID
|
|
|
|
if (own_node_id != find_node_id(own_unique_id, own_unique_id_len)) {
|
|
|
|
register_uid(own_node_id, own_unique_id, own_unique_id_len);
|
2024-08-17 16:49:32 -03:00
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// handle processing the node info message. returns true if from a duplicate node
|
|
|
|
bool AP_DroneCAN_DNA_Server::Database::handle_node_info(uint8_t source_node_id, const uint8_t unique_id[])
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 16:49:32 -03:00
|
|
|
WITH_SEMAPHORE(sem);
|
|
|
|
|
|
|
|
if (is_registered(source_node_id)) {
|
2024-09-01 00:04:36 -03:00
|
|
|
// this device's node ID is associated with a different unique ID
|
|
|
|
if (source_node_id != find_node_id(unique_id, 16)) {
|
|
|
|
return true; // so raise as duplicate
|
2024-08-17 16:49:32 -03:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-08 21:09:16 -03:00
|
|
|
register_uid(source_node_id, unique_id, 16); // we don't know about this node ID, let's register it
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2024-09-01 00:04:36 -03:00
|
|
|
return false; // not a duplicate
|
2024-08-17 16:49:32 -03:00
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-09-01 00:04:36 -03:00
|
|
|
// handle the allocation message. returns the allocated node ID, or 0 if allocation failed
|
2024-08-17 16:49:32 -03:00
|
|
|
uint8_t AP_DroneCAN_DNA_Server::Database::handle_allocation(uint8_t node_id, const uint8_t unique_id[])
|
|
|
|
{
|
|
|
|
WITH_SEMAPHORE(sem);
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
uint8_t resp_node_id = find_node_id(unique_id, 16);
|
|
|
|
if (resp_node_id == 0) {
|
|
|
|
resp_node_id = find_free_node_id(node_id > MAX_NODE_ID ? 0 : node_id);
|
|
|
|
if (resp_node_id != 0) {
|
|
|
|
create_registration(resp_node_id, unique_id, 16);
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 00:04:36 -03:00
|
|
|
return resp_node_id; // will be 0 if not found and not created
|
2024-08-17 16:49:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// search for a free node ID, starting at the preferred ID (which can be 0 if
|
|
|
|
// none are preferred). returns 0 if none found. based on pseudocode in
|
|
|
|
// uavcan/protocol/dynamic_node_id/1.Allocation.uavcan
|
|
|
|
uint8_t AP_DroneCAN_DNA_Server::Database::find_free_node_id(uint8_t preferred)
|
|
|
|
{
|
|
|
|
if (preferred == 0) {
|
2024-09-01 00:04:36 -03:00
|
|
|
preferred = MAX_NODE_ID;
|
2024-08-17 16:49:32 -03:00
|
|
|
}
|
2024-09-01 00:04:36 -03:00
|
|
|
// search for an ID >= preferred
|
2024-08-17 16:49:32 -03:00
|
|
|
uint8_t candidate = preferred;
|
2024-09-01 00:04:36 -03:00
|
|
|
while (candidate <= MAX_NODE_ID) {
|
2024-08-17 16:49:32 -03:00
|
|
|
if (!node_registered.get(candidate)) {
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
candidate++;
|
|
|
|
}
|
2024-09-01 00:04:36 -03:00
|
|
|
// search for an ID <= preferred
|
2024-08-17 16:49:32 -03:00
|
|
|
candidate = preferred;
|
|
|
|
while (candidate > 0) {
|
|
|
|
if (!node_registered.get(candidate)) {
|
|
|
|
return candidate;
|
|
|
|
}
|
|
|
|
candidate--;
|
|
|
|
}
|
2024-09-01 00:04:36 -03:00
|
|
|
// no IDs free
|
2024-08-17 16:49:32 -03:00
|
|
|
return 0;
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 15:17:42 -03:00
|
|
|
// retrieve node ID that matches the given unique ID. returns 0 if not found
|
2024-08-17 14:51:24 -03:00
|
|
|
uint8_t AP_DroneCAN_DNA_Server::Database::find_node_id(const uint8_t unique_id[], uint8_t size)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 14:24:17 -03:00
|
|
|
NodeRecord record, cmp_record;
|
2024-08-17 14:51:24 -03:00
|
|
|
compute_uid_hash(cmp_record, unique_id, size);
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-07-13 16:48:43 -03:00
|
|
|
for (int i = MAX_NODE_ID; i > 0; i--) {
|
2024-08-17 14:51:24 -03:00
|
|
|
if (node_registered.get(i)) {
|
|
|
|
read_record(record, i);
|
2024-08-17 14:24:17 -03:00
|
|
|
if (memcmp(record.uid_hash, cmp_record.uid_hash, sizeof(NodeRecord::uid_hash)) == 0) {
|
2024-07-13 18:12:52 -03:00
|
|
|
return i; // node ID found
|
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
}
|
2024-07-13 18:12:52 -03:00
|
|
|
return 0; // not found
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// fill the given record with the hash of the given unique ID
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::compute_uid_hash(NodeRecord &record, const uint8_t unique_id[], uint8_t size) const
|
|
|
|
{
|
|
|
|
uint64_t hash = FNV_1_OFFSET_BASIS_64;
|
|
|
|
hash_fnv_1a(size, unique_id, &hash);
|
|
|
|
|
|
|
|
// xor-folding per http://www.isthe.com/chongo/tech/comp/fnv/
|
2024-09-01 00:04:36 -03:00
|
|
|
hash = (hash>>56) ^ (hash&(((uint64_t)1<<56)-1)); // 56 should be 48 since we use 6 bytes
|
2024-08-17 16:49:32 -03:00
|
|
|
|
|
|
|
// write it to ret
|
|
|
|
for (uint8_t i=0; i<6; i++) {
|
|
|
|
record.uid_hash[i] = (hash >> (8*i)) & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 21:09:16 -03:00
|
|
|
// register a given unique ID to a given node ID, deleting any existing registration for the unique ID
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::register_uid(uint8_t node_id, const uint8_t unique_id[], uint8_t size)
|
|
|
|
{
|
|
|
|
uint8_t prev_node_id = find_node_id(unique_id, size); // have we registered this unique ID under a different node ID?
|
|
|
|
if (prev_node_id != 0) {
|
|
|
|
delete_registration(prev_node_id); // yes, delete old node ID's registration
|
|
|
|
}
|
|
|
|
// overwrite an existing registration with this node ID, if any
|
|
|
|
create_registration(node_id, unique_id, size);
|
|
|
|
}
|
|
|
|
|
2024-08-17 15:17:42 -03:00
|
|
|
// create the registration for the given node ID and set its record's unique ID
|
2024-08-17 14:51:24 -03:00
|
|
|
void AP_DroneCAN_DNA_Server::Database::create_registration(uint8_t node_id, const uint8_t unique_id[], uint8_t size)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 14:24:17 -03:00
|
|
|
NodeRecord record;
|
2024-08-17 14:51:24 -03:00
|
|
|
compute_uid_hash(record, unique_id, size);
|
2024-08-17 15:17:42 -03:00
|
|
|
// compute and store CRC of the record's data to validate it
|
2024-08-17 14:24:17 -03:00
|
|
|
record.crc = crc_crc8(record.uid_hash, sizeof(record.uid_hash));
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-17 14:51:24 -03:00
|
|
|
write_record(record, node_id);
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-08-17 14:51:24 -03:00
|
|
|
node_registered.set(node_id);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// delete the given node ID's registration
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::delete_registration(uint8_t node_id)
|
|
|
|
{
|
|
|
|
if (node_id > MAX_NODE_ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeRecord record;
|
|
|
|
|
|
|
|
// all-zero record means no registration
|
|
|
|
memset(&record, 0, sizeof(record));
|
|
|
|
write_record(record, node_id);
|
|
|
|
node_registered.clear(node_id);
|
|
|
|
}
|
|
|
|
|
2024-08-17 15:17:42 -03:00
|
|
|
// return true if the given node ID has a registration
|
2024-08-17 14:51:24 -03:00
|
|
|
bool AP_DroneCAN_DNA_Server::Database::check_registration(uint8_t node_id)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 14:24:17 -03:00
|
|
|
NodeRecord record;
|
2024-08-17 14:51:24 -03:00
|
|
|
read_record(record, node_id);
|
2024-07-13 17:53:11 -03:00
|
|
|
|
2024-09-01 00:04:36 -03:00
|
|
|
uint8_t empty_uid[sizeof(NodeRecord::uid_hash)] {};
|
2024-08-17 14:24:17 -03:00
|
|
|
uint8_t crc = crc_crc8(record.uid_hash, sizeof(record.uid_hash));
|
|
|
|
if (crc == record.crc && memcmp(&record.uid_hash[0], &empty_uid[0], sizeof(empty_uid)) != 0) {
|
2024-08-17 15:17:42 -03:00
|
|
|
return true; // CRC matches and UID hash is not all zero
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// read the given node ID's registration's record
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::read_record(NodeRecord &record, uint8_t node_id)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-08-17 16:49:32 -03:00
|
|
|
if (node_id > MAX_NODE_ID) {
|
|
|
|
return;
|
|
|
|
}
|
2024-08-05 12:31:01 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
storage->read_block(&record, NODERECORD_LOC(node_id), sizeof(NodeRecord));
|
|
|
|
}
|
2024-08-05 12:31:01 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
// write the given node ID's registration's record
|
|
|
|
void AP_DroneCAN_DNA_Server::Database::write_record(const NodeRecord &record, uint8_t node_id)
|
|
|
|
{
|
|
|
|
if (node_id > MAX_NODE_ID) {
|
|
|
|
return;
|
2021-09-10 23:47:02 -03:00
|
|
|
}
|
2024-07-13 14:50:11 -03:00
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
storage->write_block(NODERECORD_LOC(node_id), &record, sizeof(NodeRecord));
|
2024-08-05 12:31:01 -03:00
|
|
|
}
|
|
|
|
|
2024-08-17 16:49:32 -03:00
|
|
|
|
|
|
|
AP_DroneCAN_DNA_Server::AP_DroneCAN_DNA_Server(AP_DroneCAN &ap_dronecan, CanardInterface &canard_iface, uint8_t driver_index) :
|
|
|
|
_ap_dronecan(ap_dronecan),
|
|
|
|
_canard_iface(canard_iface),
|
|
|
|
storage(StorageManager::StorageCANDNA),
|
|
|
|
allocation_sub(allocation_cb, driver_index),
|
|
|
|
node_status_sub(node_status_cb, driver_index),
|
|
|
|
node_info_client(_canard_iface, node_info_cb) {}
|
|
|
|
|
2024-08-05 12:31:01 -03:00
|
|
|
/* Initialises Publishers for respective UAVCAN Instance
|
|
|
|
Also resets the Server Record in case there is a mismatch
|
|
|
|
between specified node id and unique id against the existing
|
|
|
|
Server Record. */
|
|
|
|
bool AP_DroneCAN_DNA_Server::init(uint8_t own_unique_id[], uint8_t own_unique_id_len, uint8_t node_id)
|
|
|
|
{
|
|
|
|
//Read the details from AP_DroneCAN
|
|
|
|
server_state = HEALTHY;
|
|
|
|
|
|
|
|
db.init(&storage); // initialize the database with our accessor
|
|
|
|
|
|
|
|
if (_ap_dronecan.check_and_reset_option(AP_DroneCAN::Options::DNA_CLEAR_DATABASE)) {
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "UC DNA database reset");
|
|
|
|
db.reset();
|
|
|
|
}
|
2024-07-13 14:50:11 -03:00
|
|
|
|
2024-08-17 14:51:24 -03:00
|
|
|
db.init_server(node_id, own_unique_id, own_unique_id_len);
|
2024-08-05 12:43:53 -03:00
|
|
|
|
|
|
|
/* Also add to seen node id this is to verify
|
|
|
|
if any duplicates are on the bus carrying our Node ID */
|
|
|
|
node_seen.set(node_id);
|
|
|
|
node_verified.set(node_id);
|
|
|
|
node_healthy.set(node_id);
|
|
|
|
self_node_id = node_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-21 00:16:06 -03:00
|
|
|
/* Run through the list of seen node ids for verification no more
|
|
|
|
than once per 5 second. We continually verify the nodes in our
|
|
|
|
seen list, So that we can raise issue if there are duplicates
|
|
|
|
on the bus. */
|
2023-04-06 21:18:01 -03:00
|
|
|
void AP_DroneCAN_DNA_Server::verify_nodes()
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2023-01-04 21:09:23 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2019-09-21 00:16:06 -03:00
|
|
|
if ((now - last_verification_request) < 5000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-13 21:58:05 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2020-12-31 17:04:08 -04:00
|
|
|
uint8_t log_count = AP::logger().get_log_start_count();
|
|
|
|
if (log_count != last_logging_count) {
|
|
|
|
last_logging_count = log_count;
|
2024-07-13 16:59:20 -03:00
|
|
|
node_logged.clearall();
|
2020-12-31 17:04:08 -04:00
|
|
|
}
|
2023-07-13 21:58:05 -03:00
|
|
|
#endif
|
|
|
|
|
2019-09-21 00:16:06 -03:00
|
|
|
//Check if we got acknowledgement from previous request
|
|
|
|
//except for requests using our own node_id
|
2023-01-04 21:09:23 -04:00
|
|
|
if (curr_verifying_node == self_node_id) {
|
2022-04-04 07:33:33 -03:00
|
|
|
nodeInfo_resp_rcvd = true;
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2022-04-04 07:33:33 -03:00
|
|
|
|
2019-09-21 00:16:06 -03:00
|
|
|
if (!nodeInfo_resp_rcvd) {
|
|
|
|
/* Also notify GCS about this
|
|
|
|
Reason for this could be either the node was disconnected
|
|
|
|
Or a node with conflicting ID appeared and is sending response
|
|
|
|
at the same time. */
|
2024-07-13 16:59:20 -03:00
|
|
|
node_verified.clear(curr_verifying_node);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
last_verification_request = now;
|
|
|
|
//Find the next registered Node ID to be verified.
|
|
|
|
for (uint8_t i = 0; i <= MAX_NODE_ID; i++) {
|
|
|
|
curr_verifying_node = (curr_verifying_node + 1) % (MAX_NODE_ID + 1);
|
2023-01-04 21:09:23 -04:00
|
|
|
if ((curr_verifying_node == self_node_id) || (curr_verifying_node == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-13 16:59:20 -03:00
|
|
|
if (node_seen.get(curr_verifying_node)) {
|
2019-09-21 00:16:06 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-17 14:51:24 -03:00
|
|
|
if (db.is_registered(curr_verifying_node)) {
|
2023-01-04 21:09:23 -04:00
|
|
|
uavcan_protocol_GetNodeInfoRequest request;
|
|
|
|
node_info_client.request(curr_verifying_node, request);
|
2022-04-04 07:33:33 -03:00
|
|
|
nodeInfo_resp_rcvd = false;
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handles Node Status Message, adds to the Seen Node list
|
|
|
|
Also starts the Service call for Node Info to complete the
|
|
|
|
Verification process. */
|
2023-04-06 21:18:01 -03:00
|
|
|
void AP_DroneCAN_DNA_Server::handleNodeStatus(const CanardRxTransfer& transfer, const uavcan_protocol_NodeStatus& msg)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2023-01-04 21:09:23 -04:00
|
|
|
if (transfer.source_node_id > MAX_NODE_ID || transfer.source_node_id == 0) {
|
2019-09-21 00:16:06 -03:00
|
|
|
return;
|
|
|
|
}
|
2023-01-04 21:09:23 -04:00
|
|
|
if ((msg.health != UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK ||
|
|
|
|
msg.mode != UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL) &&
|
2023-04-06 21:18:01 -03:00
|
|
|
!_ap_dronecan.option_is_set(AP_DroneCAN::Options::DNA_IGNORE_UNHEALTHY_NODE)) {
|
2022-04-29 08:47:51 -03:00
|
|
|
//if node is not healthy or operational, clear resp health mask, and set fault_node_id
|
2023-01-04 21:09:23 -04:00
|
|
|
fault_node_id = transfer.source_node_id;
|
2022-04-29 08:47:51 -03:00
|
|
|
server_state = NODE_STATUS_UNHEALTHY;
|
2024-07-13 16:59:20 -03:00
|
|
|
node_healthy.clear(transfer.source_node_id);
|
2022-04-29 08:47:51 -03:00
|
|
|
} else {
|
2024-07-13 16:59:20 -03:00
|
|
|
node_healthy.set(transfer.source_node_id);
|
|
|
|
if (node_healthy == node_verified) {
|
2022-04-29 08:47:51 -03:00
|
|
|
server_state = HEALTHY;
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 16:59:20 -03:00
|
|
|
if (!node_verified.get(transfer.source_node_id)) {
|
2019-09-21 00:16:06 -03:00
|
|
|
//immediately begin verification of the node_id
|
2023-01-04 21:09:23 -04:00
|
|
|
uavcan_protocol_GetNodeInfoRequest request;
|
|
|
|
node_info_client.request(transfer.source_node_id, request);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
//Add node to seen list if not seen before
|
2024-07-13 16:59:20 -03:00
|
|
|
node_seen.set(transfer.source_node_id);
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Node Info message handler
|
|
|
|
Handle responses from GetNodeInfo Request. We verify the node info
|
|
|
|
against our records. Marks Verification mask if already recorded,
|
|
|
|
Or register if the node id is available and not recorded for the
|
|
|
|
received Unique ID */
|
2023-04-06 21:18:01 -03:00
|
|
|
void AP_DroneCAN_DNA_Server::handleNodeInfo(const CanardRxTransfer& transfer, const uavcan_protocol_GetNodeInfoResponse& rsp)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2024-07-13 16:48:43 -03:00
|
|
|
if (transfer.source_node_id > MAX_NODE_ID || transfer.source_node_id == 0) {
|
2019-09-21 00:16:06 -03:00
|
|
|
return;
|
|
|
|
}
|
2020-12-31 17:04:08 -04:00
|
|
|
/*
|
|
|
|
if we haven't logged this node then log it now
|
|
|
|
*/
|
2023-07-13 21:58:05 -03:00
|
|
|
#if HAL_LOGGING_ENABLED
|
2024-07-13 16:59:20 -03:00
|
|
|
if (!node_logged.get(transfer.source_node_id) && AP::logger().logging_started()) {
|
|
|
|
node_logged.set(transfer.source_node_id);
|
2020-12-31 17:04:08 -04:00
|
|
|
uint64_t uid[2];
|
2023-01-04 21:09:23 -04:00
|
|
|
memcpy(uid, rsp.hardware_version.unique_id, sizeof(rsp.hardware_version.unique_id));
|
2022-02-12 09:52:09 -04:00
|
|
|
// @LoggerMessage: CAND
|
|
|
|
// @Description: Info from GetNodeInfo request
|
|
|
|
// @Field: TimeUS: Time since system startup
|
2024-02-14 13:04:59 -04:00
|
|
|
// @Field: Driver: Driver index
|
2022-02-12 09:52:09 -04:00
|
|
|
// @Field: NodeId: Node ID
|
|
|
|
// @Field: UID1: Hardware ID, part 1
|
|
|
|
// @Field: UID2: Hardware ID, part 2
|
|
|
|
// @Field: Name: Name string
|
|
|
|
// @Field: Major: major revision id
|
|
|
|
// @Field: Minor: minor revision id
|
|
|
|
// @Field: Version: AP_Periph git hash
|
2024-02-14 13:04:59 -04:00
|
|
|
AP::logger().Write("CAND", "TimeUS,Driver,NodeId,UID1,UID2,Name,Major,Minor,Version",
|
|
|
|
"s-#------", "F--------", "QBBQQZBBI",
|
2020-12-31 17:04:08 -04:00
|
|
|
AP_HAL::micros64(),
|
2024-02-14 13:04:59 -04:00
|
|
|
_ap_dronecan.get_driver_index(),
|
2023-01-04 21:09:23 -04:00
|
|
|
transfer.source_node_id,
|
2020-12-31 17:04:08 -04:00
|
|
|
uid[0], uid[1],
|
2023-01-04 21:09:23 -04:00
|
|
|
rsp.name.data,
|
|
|
|
rsp.software_version.major,
|
|
|
|
rsp.software_version.minor,
|
|
|
|
rsp.software_version.vcs_commit);
|
2020-12-31 17:04:08 -04:00
|
|
|
}
|
2023-07-13 21:58:05 -03:00
|
|
|
#endif
|
2020-12-31 17:04:08 -04:00
|
|
|
|
2024-08-17 14:51:24 -03:00
|
|
|
bool duplicate = db.handle_node_info(transfer.source_node_id, rsp.hardware_version.unique_id);
|
2024-08-05 12:50:55 -03:00
|
|
|
if (duplicate) {
|
|
|
|
if (!_ap_dronecan.option_is_set(AP_DroneCAN::Options::DNA_IGNORE_DUPLICATE_NODE)) {
|
2019-09-21 00:16:06 -03:00
|
|
|
/* This is a device with node_id already registered
|
|
|
|
for another device */
|
|
|
|
server_state = DUPLICATE_NODES;
|
2023-01-04 21:09:23 -04:00
|
|
|
fault_node_id = transfer.source_node_id;
|
|
|
|
memcpy(fault_node_name, rsp.name.data, sizeof(fault_node_name));
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2024-08-05 12:50:55 -03:00
|
|
|
} else {
|
|
|
|
//Verify as well
|
|
|
|
node_verified.set(transfer.source_node_id);
|
|
|
|
if (transfer.source_node_id == curr_verifying_node) {
|
|
|
|
nodeInfo_resp_rcvd = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 23:37:41 -03:00
|
|
|
// process node ID allocation messages for DNA
|
|
|
|
void AP_DroneCAN_DNA_Server::handle_allocation(const CanardRxTransfer& transfer, const uavcan_protocol_dynamic_node_id_Allocation& msg)
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
2023-01-04 21:09:23 -04:00
|
|
|
if (transfer.source_node_id != 0) {
|
2024-09-08 23:37:41 -03:00
|
|
|
return; // ignore allocation messages that are not DNA requests
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2023-01-04 21:09:23 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2020-05-31 09:47:45 -03:00
|
|
|
if (rcvd_unique_id_offset == 0 ||
|
2024-09-08 23:37:41 -03:00
|
|
|
(now - last_alloc_msg_ms) > UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_FOLLOWUP_TIMEOUT_MS) {
|
2023-01-04 21:09:23 -04:00
|
|
|
if (msg.first_part_of_unique_id) {
|
2020-05-31 09:47:45 -03:00
|
|
|
rcvd_unique_id_offset = 0;
|
|
|
|
} else {
|
2024-09-08 23:37:41 -03:00
|
|
|
return; // only accepting the first part
|
2020-05-31 09:47:45 -03:00
|
|
|
}
|
2023-01-04 21:09:23 -04:00
|
|
|
} else if (msg.first_part_of_unique_id) {
|
2024-09-08 23:37:41 -03:00
|
|
|
return; // only accepting follow up messages
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2020-05-31 09:47:45 -03:00
|
|
|
if (rcvd_unique_id_offset) {
|
2024-09-08 23:37:41 -03:00
|
|
|
debug_dronecan(AP_CANManager::LOG_DEBUG, "TIME: %lu -- Accepting Followup part! %u\n",
|
2024-09-08 23:57:58 -03:00
|
|
|
(unsigned long)now,
|
2021-08-03 23:44:31 -03:00
|
|
|
unsigned((now - last_alloc_msg_ms)));
|
2020-05-31 09:47:45 -03:00
|
|
|
} else {
|
2024-09-08 23:37:41 -03:00
|
|
|
debug_dronecan(AP_CANManager::LOG_DEBUG, "TIME: %lu -- Accepting First part! %u\n",
|
2024-09-08 23:57:58 -03:00
|
|
|
(unsigned long)now,
|
2021-08-03 23:44:31 -03:00
|
|
|
unsigned((now - last_alloc_msg_ms)));
|
2020-05-31 09:47:45 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
last_alloc_msg_ms = now;
|
2024-09-08 23:37:41 -03:00
|
|
|
if ((rcvd_unique_id_offset + msg.unique_id.len) > sizeof(rcvd_unique_id)) {
|
|
|
|
rcvd_unique_id_offset = 0; // reset state, request contains an over-long ID
|
2019-09-21 00:16:06 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-08 23:37:41 -03:00
|
|
|
// save the new portion of the unique ID
|
|
|
|
memcpy(&rcvd_unique_id[rcvd_unique_id_offset], msg.unique_id.data, msg.unique_id.len);
|
2023-01-04 21:09:23 -04:00
|
|
|
rcvd_unique_id_offset += msg.unique_id.len;
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2024-09-08 23:37:41 -03:00
|
|
|
// respond with the message containing the received unique ID so far, or
|
|
|
|
// with the node ID if we successfully allocated one
|
2023-04-07 04:43:08 -03:00
|
|
|
uavcan_protocol_dynamic_node_id_Allocation rsp {};
|
2023-01-04 21:09:23 -04:00
|
|
|
memcpy(rsp.unique_id.data, rcvd_unique_id, rcvd_unique_id_offset);
|
|
|
|
rsp.unique_id.len = rcvd_unique_id_offset;
|
2020-05-31 09:47:45 -03:00
|
|
|
|
2024-09-08 23:37:41 -03:00
|
|
|
if (rcvd_unique_id_offset == sizeof(rcvd_unique_id)) { // full unique ID received, allocate it!
|
|
|
|
rsp.node_id = db.handle_allocation(msg.node_id, rcvd_unique_id);
|
|
|
|
rcvd_unique_id_offset = 0; // reset state for next allocation
|
2024-09-08 23:57:58 -03:00
|
|
|
if (rsp.node_id == 0) { // allocation failed
|
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "DroneCAN DNA allocation failed; database full");
|
|
|
|
// don't send reply with a failed ID in case the allocatee does
|
|
|
|
// silly things, though it is technically legal. the allocatee will
|
|
|
|
// then time out and try again (though we still won't have an ID!)
|
|
|
|
return;
|
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
2023-01-04 21:09:23 -04:00
|
|
|
allocation_pub.broadcast(rsp, false); // never publish allocation message with CAN FD
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//report the server state, along with failure message if any
|
2023-04-06 21:18:01 -03:00
|
|
|
bool AP_DroneCAN_DNA_Server::prearm_check(char* fail_msg, uint8_t fail_msg_len) const
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
|
|
|
switch (server_state) {
|
2020-05-01 02:20:00 -03:00
|
|
|
case HEALTHY:
|
|
|
|
return true;
|
2019-09-21 00:16:06 -03:00
|
|
|
case DUPLICATE_NODES: {
|
2023-04-06 21:18:01 -03:00
|
|
|
if (_ap_dronecan.option_is_set(AP_DroneCAN::Options::DNA_IGNORE_DUPLICATE_NODE)) {
|
2021-09-10 23:47:02 -03:00
|
|
|
// ignore error
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-01 02:20:00 -03:00
|
|
|
snprintf(fail_msg, fail_msg_len, "Duplicate Node %s../%d!", fault_node_name, fault_node_id);
|
2019-09-21 00:16:06 -03:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-29 08:47:51 -03:00
|
|
|
case NODE_STATUS_UNHEALTHY: {
|
2023-04-06 21:18:01 -03:00
|
|
|
if (_ap_dronecan.option_is_set(AP_DroneCAN::Options::DNA_IGNORE_UNHEALTHY_NODE)) {
|
2022-07-25 00:30:50 -03:00
|
|
|
// ignore error
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-29 08:47:51 -03:00
|
|
|
snprintf(fail_msg, fail_msg_len, "Node %d unhealthy!", fault_node_id);
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-21 00:16:06 -03:00
|
|
|
}
|
2020-05-01 02:20:00 -03:00
|
|
|
// should never get; compiler should enforce all server_states are covered
|
2019-09-21 00:16:06 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:07:28 -03:00
|
|
|
#endif //HAL_NUM_CAN_IFACES
|