Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_CANManager/AP_MAVLinkCAN.h
4182 views
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_HAL/AP_HAL.h>
19
#include <AP_HAL/utility/RingBuffer.h>
20
#include "AP_CANManager_config.h"
21
22
#if HAL_CANMANAGER_ENABLED && HAL_GCS_ENABLED
23
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
AP_MAVLinkCAN() {}
32
33
// Process CAN frame forwarding
34
void process_frame_buffer();
35
36
// Handle commands to forward CAN frames to GCS
37
bool handle_can_forward(mavlink_channel_t chan, const mavlink_command_int_t &packet, const mavlink_message_t &msg);
38
39
// Handle received MAVLink CAN frames
40
void handle_can_frame(const mavlink_message_t &msg);
41
42
// Handle CAN filter modification
43
void handle_can_filter_modify(const mavlink_message_t &msg);
44
45
private:
46
// Callback for receiving CAN frames from CAN bus and sending to GCS
47
void can_frame_callback(uint8_t bus, const AP_HAL::CANFrame &frame, AP_HAL::CANIface::CanIOFlags flags);
48
49
/*
50
* Structure to maintain forwarding state
51
*/
52
struct {
53
mavlink_channel_t chan;
54
uint8_t system_id;
55
uint8_t component_id;
56
uint8_t frame_counter;
57
uint32_t last_callback_enable_ms;
58
HAL_Semaphore sem;
59
uint16_t num_filter_ids;
60
uint16_t *filter_ids;
61
uint8_t callback_id;
62
uint8_t callback_bus;
63
} can_forward;
64
65
// Buffer for storing CAN frames to be sent
66
struct BufferFrame {
67
uint8_t bus;
68
AP_HAL::CANFrame frame;
69
};
70
71
// Frame buffer for queuing frames
72
HAL_Semaphore frame_buffer_sem;
73
ObjectBuffer<BufferFrame> *frame_buffer;
74
};
75
76
#endif // HAL_CANMANAGER_ENABLED && HAL_GCS_ENABLED
77
78