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 : * Code by Siddharth Bharat Purohit 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "AP_CANManager_config.h" 21 : 22 : #if HAL_CANMANAGER_ENABLED 23 : 24 : #include <AP_HAL/AP_HAL.h> 25 : 26 : #include <AP_Param/AP_Param.h> 27 : #include "AP_SLCANIface.h" 28 : #include "AP_CANDriver.h" 29 : 30 : #include "AP_CAN.h" 31 : 32 : class CANSensor; 33 : 34 : class AP_CANManager 35 : { 36 : public: 37 : AP_CANManager(); 38 : 39 : /* Do not allow copies */ 40 : CLASS_NO_COPY(AP_CANManager); 41 : 42 125953352 : static AP_CANManager* get_singleton() 43 : { 44 125953352 : if (_singleton == nullptr) { 45 0 : AP_HAL::panic("CANManager used before allocation."); 46 : } 47 125953352 : return _singleton; 48 : } 49 : 50 : enum LogLevel : uint8_t { 51 : LOG_NONE, 52 : LOG_ERROR, 53 : LOG_WARNING, 54 : LOG_INFO, 55 : LOG_DEBUG, 56 : }; 57 : 58 : __INITFUNC__ void init(void); 59 : 60 : // register a new driver 61 : bool register_driver(AP_CAN::Protocol dtype, AP_CANDriver *driver); 62 : 63 : // register a new auxillary sensor driver for 11 bit address frames 64 : bool register_11bit_driver(AP_CAN::Protocol dtype, CANSensor *sensor, uint8_t &driver_index); 65 : 66 : // returns number of active CAN Drivers 67 41983060 : uint8_t get_num_drivers(void) const 68 : { 69 41983060 : return HAL_MAX_CAN_PROTOCOL_DRIVERS; 70 : } 71 : 72 : // return driver for index i 73 157816 : AP_CANDriver* get_driver(uint8_t i) const 74 : { 75 157816 : if (i < ARRAY_SIZE(_drivers)) { 76 157816 : return _drivers[i]; 77 : } 78 0 : return nullptr; 79 : } 80 : 81 : // returns current log level 82 0 : LogLevel get_log_level(void) const 83 : { 84 0 : return LogLevel(_loglevel.get()); 85 : } 86 : 87 : // return driver type index i 88 83812476 : AP_CAN::Protocol get_driver_type(uint8_t i) const 89 : { 90 83812476 : if (i < ARRAY_SIZE(_driver_type_cache)) { 91 83812476 : return _driver_type_cache[i]; 92 : } 93 0 : return AP_CAN::Protocol::None; 94 : } 95 : 96 : static const struct AP_Param::GroupInfo var_info[]; 97 : 98 : private: 99 : 100 : // Parameter interface for CANIfaces 101 : class CANIface_Params 102 : { 103 : friend class AP_CANManager; 104 : 105 : public: 106 4196 : CANIface_Params() 107 4196 : { 108 4196 : AP_Param::setup_object_defaults(this, var_info); 109 4196 : } 110 : 111 : static const struct AP_Param::GroupInfo var_info[]; 112 : 113 : enum class Options : uint32_t { 114 : LOG_ALL_FRAMES = (1U<<0), 115 : }; 116 : 117 486285500 : bool option_is_set(Options option) const { 118 486285500 : return (_options & uint32_t(option)) != 0; 119 : } 120 : 121 : private: 122 : AP_Int8 _driver_number; 123 : AP_Int32 _bitrate; 124 : AP_Int32 _fdbitrate; 125 : AP_Int32 _options; 126 : 127 : #if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED 128 : uint8_t logging_id; 129 : #endif 130 : }; 131 : 132 : //Parameter Interface for CANDrivers 133 : class CANDriver_Params 134 : { 135 : friend class AP_CANManager; 136 : 137 : public: 138 4196 : CANDriver_Params() 139 4196 : { 140 4196 : AP_Param::setup_object_defaults(this, var_info); 141 4196 : } 142 : static const struct AP_Param::GroupInfo var_info[]; 143 : 144 : private: 145 : AP_Int8 _driver_type; 146 : AP_Int8 _driver_type_11bit; 147 : AP_CANDriver* _uavcan; 148 : AP_CANDriver* _piccolocan; 149 : }; 150 : 151 : CANIface_Params _interfaces[HAL_NUM_CAN_IFACES]; 152 : AP_CANDriver* _drivers[HAL_MAX_CAN_PROTOCOL_DRIVERS]; 153 : CANDriver_Params _drv_param[HAL_MAX_CAN_PROTOCOL_DRIVERS]; 154 : AP_CAN::Protocol _driver_type_cache[HAL_MAX_CAN_PROTOCOL_DRIVERS]; 155 : 156 : AP_Int8 _loglevel; 157 : uint8_t _num_drivers; 158 : #if AP_CAN_SLCAN_ENABLED 159 : SLCAN::CANIface _slcan_interface; 160 : #endif 161 : 162 : static AP_CANManager *_singleton; 163 : 164 : HAL_Semaphore _sem; 165 : 166 : #if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED 167 : /* 168 : handler for CAN frames for logging 169 : */ 170 : void can_logging_callback(uint8_t bus, const AP_HAL::CANFrame &frame, AP_HAL::CANIface::CanIOFlags flags); 171 : void check_logging_enable(void); 172 : #endif 173 : }; 174 : 175 : namespace AP 176 : { 177 : AP_CANManager& can(); 178 : } 179 : 180 : #endif // HAL_CANMANAGER_ENABLED