Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : /* 16 : Scripting CANSensor class, for easy scripting CAN support 17 : */ 18 : 19 : #pragma once 20 : 21 : #include <AP_HAL/AP_HAL.h> 22 : 23 : #if defined(HAL_BUILD_AP_PERIPH) 24 : // Must have at least two CAN ports on Periph 25 : #define AP_SCRIPTING_CAN_SENSOR_ENABLED (HAL_MAX_CAN_PROTOCOL_DRIVERS > 1) 26 : #else 27 : #define AP_SCRIPTING_CAN_SENSOR_ENABLED HAL_MAX_CAN_PROTOCOL_DRIVERS 28 : #endif 29 : 30 : #if AP_SCRIPTING_CAN_SENSOR_ENABLED 31 : 32 : #include <AP_CANManager/AP_CANSensor.h> 33 : 34 : class ScriptingCANBuffer; 35 : class ScriptingCANSensor : public CANSensor { 36 : public: 37 : 38 0 : ScriptingCANSensor(AP_CAN::Protocol dtype) 39 0 : : CANSensor("Script") { 40 0 : register_driver(dtype); 41 0 : } 42 : 43 : // handler for outgoing frames, using uint32 44 : bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us); 45 : 46 : // handler for incoming frames, add to buffers 47 : void handle_frame(AP_HAL::CANFrame &frame) override; 48 : 49 : // add a new buffer to this sensor 50 : ScriptingCANBuffer* add_buffer(uint32_t buffer_len); 51 : 52 : private: 53 : 54 : HAL_Semaphore sem; 55 : 56 : ScriptingCANBuffer *buffer_list; 57 : 58 : }; 59 : 60 : class ScriptingCANBuffer { 61 : public: 62 : 63 0 : ScriptingCANBuffer(ScriptingCANSensor &_sensor, uint32_t buffer_size): 64 0 : buffer(buffer_size), 65 0 : sensor(_sensor) 66 0 : {}; 67 : 68 : // Call main sensor write method 69 : bool write_frame(AP_HAL::CANFrame &out_frame, const uint32_t timeout_us); 70 : 71 : // read a frame from the buffer 72 : bool read_frame(AP_HAL::CANFrame &frame); 73 : 74 : // recursively add frame to buffer 75 : void handle_frame(AP_HAL::CANFrame &frame); 76 : 77 : // recursively add new buffer 78 : void add_buffer(ScriptingCANBuffer* new_buff); 79 : 80 : // Add a filter to this buffer 81 : bool add_filter(uint32_t mask, uint32_t value); 82 : 83 : private: 84 : 85 : ObjectBuffer<AP_HAL::CANFrame> buffer; 86 : 87 : ScriptingCANSensor &sensor; 88 : 89 : ScriptingCANBuffer *next; 90 : 91 : HAL_Semaphore sem; 92 : 93 : struct { 94 : uint32_t mask; 95 : uint32_t value; 96 : } filter[8]; 97 : uint8_t num_filters; 98 : 99 : }; 100 : 101 : #endif // AP_SCRIPTING_CAN_SENSOR_ENABLED