Line data Source code
1 : /* 2 : * This file is free software: you can redistribute it and/or modify it 3 : * under the terms of the GNU General Public License as published by the 4 : * Free Software Foundation, either version 3 of the License, or 5 : * (at your option) any later version. 6 : * 7 : * This file is distributed in the hope that it will be useful, but 8 : * WITHOUT ANY WARRANTY; without even the implied warranty of 9 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 : * See the GNU General Public License for more details. 11 : * 12 : * You should have received a copy of the GNU General Public License along 13 : * with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #pragma once 17 : 18 : #include "AP_CANManager_config.h" 19 : 20 : #if AP_MAVLINKCAN_ENABLED 21 : 22 : #include <AP_HAL/AP_HAL.h> 23 : #include <AP_HAL/utility/RingBuffer.h> 24 : #include <GCS_MAVLink/GCS_MAVLink.h> 25 : #include <AP_HAL/CANIface.h> 26 : #include <AP_HAL/utility/OwnPtr.h> 27 : #include <GCS_MAVLink/GCS.h> 28 : 29 : class AP_MAVLinkCAN { 30 : public: 31 0 : AP_MAVLinkCAN() {} 32 : 33 : // Handle commands to forward CAN frames to GCS 34 : static bool handle_can_forward(mavlink_channel_t chan, const mavlink_command_int_t &packet, const mavlink_message_t &msg); 35 : 36 : // Handle received MAVLink CAN frames 37 : static void handle_can_frame(const mavlink_message_t &msg); 38 : 39 : // Handle CAN filter modification 40 : static void handle_can_filter_modify(const mavlink_message_t &msg); 41 : 42 : private: 43 : // Callback for receiving CAN frames from CAN bus and sending to GCS 44 : void can_frame_callback(uint8_t bus, const AP_HAL::CANFrame &frame, AP_HAL::CANIface::CanIOFlags flags); 45 : 46 : /* 47 : * Structure to maintain forwarding state 48 : */ 49 : struct { 50 : mavlink_channel_t chan; 51 : uint8_t system_id; 52 : uint8_t component_id; 53 : uint8_t frame_counter; 54 : uint32_t last_callback_enable_ms; 55 : HAL_Semaphore sem; 56 : uint16_t num_filter_ids; 57 : uint16_t *filter_ids; 58 : uint8_t callback_id; 59 : uint8_t callback_bus; 60 : } can_forward; 61 : 62 : // Buffer for storing CAN frames to be sent 63 : struct BufferFrame { 64 : uint8_t bus; 65 : AP_HAL::CANFrame frame; 66 : }; 67 : 68 : // Frame buffer for queuing frames 69 : HAL_Semaphore frame_buffer_sem; 70 : ObjectBuffer<BufferFrame> *frame_buffer; 71 : 72 : static AP_MAVLinkCAN *ensure_singleton(); 73 : 74 : // Process CAN frame forwarding 75 : void process_frame_buffer(); 76 : 77 : // Handle commands to forward CAN frames to GCS 78 : bool _handle_can_forward(mavlink_channel_t chan, const mavlink_command_int_t &packet, const mavlink_message_t &msg); 79 : 80 : // Handle received MAVLink CAN frames 81 : void _handle_can_frame(const mavlink_message_t &msg); 82 : 83 : // Handle CAN filter modification 84 : void _handle_can_filter_modify(const mavlink_message_t &msg); 85 : }; 86 : 87 : #endif // AP_MAVLINKCAN_ENABLED