Line data Source code
1 : #pragma once 2 : #include <AP_HAL/AP_HAL.h> 3 : #if HAL_ENABLE_DRONECAN_DRIVERS 4 : #include <canard/interface.h> 5 : #include <dronecan_msgs.h> 6 : 7 : class AP_DroneCAN; 8 : class CANSensor; 9 : 10 : class CanardInterface : public Canard::Interface { 11 : friend class AP_DroneCAN; 12 : public: 13 : 14 : /// @brief delete copy constructor and assignment operator 15 : CanardInterface(const CanardInterface&) = delete; 16 : CanardInterface& operator=(const CanardInterface&) = delete; 17 : 18 : CanardInterface(uint8_t driver_index); 19 : 20 : void init(void* mem_arena, size_t mem_arena_size, uint8_t node_id); 21 : 22 : /// @brief broadcast message to all listeners on Interface 23 : /// @param bc_transfer 24 : /// @return true if message was added to the queue 25 : bool broadcast(const Canard::Transfer &bcast_transfer) override; 26 : 27 : /// @brief request message from 28 : /// @param destination_node_id 29 : /// @param req_transfer 30 : /// @return true if request was added to the queue 31 : bool request(uint8_t destination_node_id, const Canard::Transfer &req_transfer) override; 32 : 33 : /// @brief respond to a request 34 : /// @param destination_node_id 35 : /// @param res_transfer 36 : /// @return true if response was added to the queue 37 : bool respond(uint8_t destination_node_id, const Canard::Transfer &res_transfer) override; 38 : 39 : void processTx(bool raw_commands_only); 40 : void processRx(); 41 : 42 : void process(uint32_t duration); 43 : 44 : static void onTransferReception(CanardInstance* ins, CanardRxTransfer* transfer); 45 : static bool shouldAcceptTransfer(const CanardInstance* ins, 46 : uint64_t* out_data_type_signature, 47 : uint16_t data_type_id, 48 : CanardTransferType transfer_type, 49 : uint8_t source_node_id); 50 : 51 : bool add_interface(AP_HAL::CANIface *can_drv); 52 : 53 : // add an auxillary driver for 11 bit frames 54 : bool add_11bit_driver(CANSensor *sensor); 55 : 56 : // handler for outgoing frames for auxillary drivers 57 : bool write_aux_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us); 58 : 59 : #if AP_TEST_DRONECAN_DRIVERS 60 : static CanardInterface& get_test_iface() { return test_iface; } 61 : static void processTestRx(); 62 : #endif 63 : 64 : void update_rx_protocol_stats(int16_t res); 65 : 66 10221 : uint8_t get_node_id() const override { return canard.node_id; } 67 : 68 : // get reference to the semaphore that is held during message receive 69 0 : HAL_Semaphore &get_sem_rx(void) { return _sem_rx; } 70 : 71 : private: 72 : CanardInstance canard; 73 : AP_HAL::CANIface* ifaces[HAL_NUM_CAN_IFACES]; 74 : #if AP_TEST_DRONECAN_DRIVERS 75 : static CanardInterface* canard_ifaces[3]; 76 : static CanardInterface test_iface; 77 : #endif 78 : uint8_t num_ifaces; 79 : HAL_BinarySemaphore sem_handle; 80 : bool initialized; 81 : HAL_Semaphore _sem_tx; 82 : HAL_Semaphore _sem_rx; 83 : CanardTxTransfer tx_transfer; 84 : dronecan_protocol_Stats protocol_stats; 85 : 86 : // auxillary 11 bit CANSensor 87 : CANSensor *aux_11bit_driver; 88 : }; 89 : #endif // HAL_ENABLE_DRONECAN_DRIVERS