Skip to content

Commit

Permalink
AP_DroneCAN: DNA_Server: move allocation operation to db
Browse files Browse the repository at this point in the history
Must be locked for the whole operation due to the find free/add
read-modify-write.

Preserves the previous behavior of sending back an ID of 0 in case of
allocation failure, for better or worse.
  • Loading branch information
tpwrules committed Aug 5, 2024
1 parent 545e4fb commit bb43a8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
33 changes: 21 additions & 12 deletions libraries/AP_DroneCAN/AP_DroneCAN_DNA_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,7 @@ void AP_DroneCAN_DNA_Server::handleAllocation(const CanardRxTransfer& transfer,

if (rcvd_unique_id_offset == 16) {
//We have received the full Unique ID, time to do allocation
uint8_t resp_node_id = db.getNodeIDForUniqueID((const uint8_t*)rcvd_unique_id, 16);
if (resp_node_id == 0) {
resp_node_id = db.findFreeNodeID(msg.node_id > MAX_NODE_ID ? 0 : msg.node_id);
if (resp_node_id != 0) {
db.addNodeIDForUniqueID(resp_node_id, (const uint8_t*)rcvd_unique_id, 16);
rsp.node_id = resp_node_id;
} else {
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "UC Node Alloc Failed!");
}
} else {
rsp.node_id = resp_node_id;
}
rsp.node_id = db.handleAllocation(msg.node_id, (const uint8_t*)rcvd_unique_id);
//reset states as well
rcvd_unique_id_offset = 0;
memset(rcvd_unique_id, 0, sizeof(rcvd_unique_id));
Expand All @@ -532,6 +521,26 @@ void AP_DroneCAN_DNA_Server::handleAllocation(const CanardRxTransfer& transfer,
allocation_pub.broadcast(rsp, false); // never publish allocation message with CAN FD
}

// handle the allocation message. returns the new node ID.
uint8_t AP_DroneCAN_DNA_Server::Database::handleAllocation(uint8_t node_id, const uint8_t unique_id[])
{
WITH_SEMAPHORE(sem);

uint8_t resp_node_id = getNodeIDForUniqueID(unique_id, 16);
if (resp_node_id == 0) {
resp_node_id = findFreeNodeID(node_id > MAX_NODE_ID ? 0 : node_id);
if (resp_node_id != 0) {
addNodeIDForUniqueID(resp_node_id, unique_id, 16);
return resp_node_id;
} else {
GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "UC Node Alloc Failed!");
return 0;
}
} else {
return resp_node_id;
}
}

//report the server state, along with failure message if any
bool AP_DroneCAN_DNA_Server::prearm_check(char* fail_msg, uint8_t fail_msg_len) const
{
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_DroneCAN/AP_DroneCAN_DNA_Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class AP_DroneCAN_DNA_Server
// handle processing the node info message. returns true if duplicate.
bool handleNodeInfo(uint8_t source_node_id, const uint8_t unique_id[]);

// handle the allocation message. returns the new node ID.
uint8_t handleAllocation(uint8_t node_id, const uint8_t unique_id[]);

//Generates 6Byte long hash from the specified unique_id
void getHash(NodeData &node_data, const uint8_t unique_id[], uint8_t size) const;

Expand Down

0 comments on commit bb43a8b

Please sign in to comment.