2019-09-21 00:16:06 -03:00
|
|
|
#pragma once
|
2022-02-28 21:19:20 -04:00
|
|
|
#include <AP_HAL/AP_HAL_Boards.h>
|
|
|
|
#include <AP_HAL/Semaphores.h>
|
2019-09-21 00:16:06 -03:00
|
|
|
|
2021-05-19 04:29:55 -03:00
|
|
|
#if HAL_ENABLE_LIBUAVCAN_DRIVERS
|
2019-09-21 00:16:06 -03:00
|
|
|
#include <uavcan/uavcan.hpp>
|
|
|
|
#include <AP_Common/Bitmask.h>
|
|
|
|
#include <StorageManager/StorageManager.h>
|
2020-05-31 09:47:45 -03:00
|
|
|
#include <AP_CANManager/AP_CANManager.h>
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Forward declaring classes
|
|
|
|
class AllocationCb;
|
|
|
|
class NodeStatusCb;
|
|
|
|
class NodeInfoCb;
|
2022-04-04 07:33:33 -03:00
|
|
|
class GetNodeInfoCb;
|
2019-09-21 00:16:06 -03:00
|
|
|
class AP_UAVCAN;
|
|
|
|
|
2019-10-25 00:03:16 -03:00
|
|
|
class AP_UAVCAN_DNA_Server
|
2019-09-21 00:16:06 -03:00
|
|
|
{
|
|
|
|
StorageAccess storage;
|
|
|
|
|
|
|
|
struct NodeData {
|
|
|
|
uint8_t hwid_hash[6];
|
|
|
|
uint8_t crc;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ServerState {
|
2022-04-29 08:47:51 -03:00
|
|
|
NODE_STATUS_UNHEALTHY = -5,
|
2019-09-21 00:16:06 -03:00
|
|
|
STORAGE_FAILURE = -3,
|
|
|
|
DUPLICATE_NODES = -2,
|
|
|
|
FAILED_TO_ADD_NODE = -1,
|
|
|
|
HEALTHY = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t last_verification_request;
|
|
|
|
uint8_t curr_verifying_node;
|
2020-06-24 09:07:28 -03:00
|
|
|
uint8_t self_node_id[HAL_MAX_CAN_PROTOCOL_DRIVERS];
|
2019-09-21 00:16:06 -03:00
|
|
|
bool nodeInfo_resp_rcvd;
|
|
|
|
|
|
|
|
Bitmask<128> occupation_mask;
|
|
|
|
Bitmask<128> verified_mask;
|
|
|
|
Bitmask<128> node_seen_mask;
|
2020-12-31 17:04:08 -04:00
|
|
|
Bitmask<128> logged;
|
2022-04-29 08:47:51 -03:00
|
|
|
Bitmask<128> node_healthy_mask;
|
2020-12-31 17:04:08 -04:00
|
|
|
|
|
|
|
uint8_t last_logging_count;
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Error State
|
|
|
|
enum ServerState server_state;
|
|
|
|
uint8_t fault_node_id;
|
|
|
|
char fault_node_name[15];
|
|
|
|
|
|
|
|
|
|
|
|
//Allocation params
|
|
|
|
uint8_t rcvd_unique_id[16];
|
|
|
|
uint8_t rcvd_unique_id_offset;
|
2020-05-31 09:47:45 -03:00
|
|
|
uint32_t last_alloc_msg_ms;
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Methods to handle and report Node IDs seen on the bus
|
|
|
|
void addToSeenNodeMask(uint8_t node_id);
|
|
|
|
bool isNodeSeen(uint8_t node_id);
|
|
|
|
|
|
|
|
//Generates 6Byte long hash from the specified unique_id
|
|
|
|
void getHash(NodeData &node_data, const uint8_t unique_id[], uint8_t size) const;
|
|
|
|
|
|
|
|
//Reads the Server Record from storage for specified node id
|
|
|
|
bool readNodeData(NodeData &data, uint8_t node_id);
|
|
|
|
|
|
|
|
//Writes the Server Record from storage for specified node id
|
|
|
|
bool writeNodeData(const NodeData &data, uint8_t node_id);
|
|
|
|
|
|
|
|
//Methods to set, clear and report NodeIDs allocated/registered so far
|
|
|
|
bool setOccupationMask(uint8_t node_id);
|
|
|
|
bool isNodeIDOccupied(uint8_t node_id) const;
|
|
|
|
bool freeNodeID(uint8_t node_id);
|
|
|
|
|
|
|
|
//Set the mask to report that the unique id matches the record
|
|
|
|
void setVerificationMask(uint8_t node_id);
|
|
|
|
|
|
|
|
//Go through List to find node id for specified unique id
|
|
|
|
uint8_t getNodeIDForUniqueID(const uint8_t unique_id[], uint8_t size);
|
|
|
|
|
|
|
|
//Add Node ID info to the record and setup necessary mask fields
|
|
|
|
bool addNodeIDForUniqueID(uint8_t node_id, const uint8_t unique_id[], uint8_t size);
|
|
|
|
|
|
|
|
//Finds next available free Node, starting from preferred NodeID
|
|
|
|
uint8_t findFreeNodeID(uint8_t preferred);
|
|
|
|
|
|
|
|
//Look in the storage and check if there's a valid Server Record there
|
|
|
|
bool isValidNodeDataAvailable(uint8_t node_id);
|
|
|
|
|
2022-04-04 07:33:33 -03:00
|
|
|
static void trampoline_handleNodeInfo(AP_UAVCAN* ap_uavcan, uint8_t node_id, const GetNodeInfoCb& resp);
|
|
|
|
static void trampoline_handleAllocation(AP_UAVCAN* ap_uavcan, uint8_t node_id, const AllocationCb &cb);
|
|
|
|
static void trampoline_handleNodeStatus(AP_UAVCAN* ap_uavcan, uint8_t node_id, const NodeStatusCb &cb);
|
|
|
|
|
|
|
|
|
|
|
|
HAL_Semaphore storage_sem;
|
2021-09-10 23:47:02 -03:00
|
|
|
AP_UAVCAN *_ap_uavcan;
|
2022-04-04 07:33:33 -03:00
|
|
|
uint8_t driver_index;
|
2020-01-15 19:49:41 -04:00
|
|
|
|
2019-09-21 00:16:06 -03:00
|
|
|
public:
|
2022-04-04 07:33:33 -03:00
|
|
|
AP_UAVCAN_DNA_Server(AP_UAVCAN *ap_uavcan, StorageAccess _storage);
|
|
|
|
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
// Do not allow copies
|
2019-10-25 00:03:16 -03:00
|
|
|
AP_UAVCAN_DNA_Server(const AP_UAVCAN_DNA_Server &other) = delete;
|
|
|
|
AP_UAVCAN_DNA_Server &operator=(const AP_UAVCAN_DNA_Server&) = delete;
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Initialises publisher and Server Record for specified uavcan driver
|
2022-04-04 07:33:33 -03:00
|
|
|
bool init();
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Reset the Server Record
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/* Checks if the node id has been verified against the record
|
|
|
|
Specific CAN drivers are expected to check use this method to
|
|
|
|
verify if the node is healthy and has static node_id against
|
|
|
|
hwid in the records */
|
|
|
|
bool isNodeIDVerified(uint8_t node_id) const;
|
|
|
|
|
|
|
|
/* Subscribe to the messages to be handled for maintaining and allocating
|
|
|
|
Node ID list */
|
|
|
|
static void subscribe_msgs(AP_UAVCAN* ap_uavcan);
|
|
|
|
|
|
|
|
//report the server state, along with failure message if any
|
|
|
|
bool prearm_check(char* fail_msg, uint8_t fail_msg_len) const;
|
|
|
|
|
|
|
|
//Callbacks
|
2022-04-04 07:33:33 -03:00
|
|
|
void handleAllocation(uint8_t node_id, const AllocationCb &cb);
|
2019-09-21 00:16:06 -03:00
|
|
|
void handleNodeStatus(uint8_t node_id, const NodeStatusCb &cb);
|
2020-12-31 17:04:08 -04:00
|
|
|
void handleNodeInfo(uint8_t node_id, uint8_t unique_id[], char name[], uint8_t major, uint8_t minor, uint32_t vcs_commit);
|
2019-09-21 00:16:06 -03:00
|
|
|
|
|
|
|
//Run through the list of seen node ids for verification
|
2022-04-04 07:33:33 -03:00
|
|
|
void verify_nodes();
|
2019-09-21 00:16:06 -03:00
|
|
|
};
|
|
|
|
|
2019-10-25 00:03:16 -03:00
|
|
|
#endif
|