Path: blob/master/libraries/AP_CANManager/AP_CANManager.h
9539 views
/*1* This file is free software: you can redistribute it and/or modify it2* under the terms of the GNU General Public License as published by the3* Free Software Foundation, either version 3 of the License, or4* (at your option) any later version.5*6* This file is distributed in the hope that it will be useful, but7* WITHOUT ANY WARRANTY; without even the implied warranty of8* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.9* See the GNU General Public License for more details.10*11* You should have received a copy of the GNU General Public License along12* with this program. If not, see <http://www.gnu.org/licenses/>.13*14* Code by Siddharth Bharat Purohit15*/1617#pragma once1819#include "AP_CANManager_config.h"2021#if HAL_CANMANAGER_ENABLED2223#include <AP_HAL/AP_HAL.h>2425#include <AP_Param/AP_Param.h>26#include "AP_SLCANIface.h"27#include "AP_CANDriver.h"2829#include "AP_CAN.h"3031class CANSensor;3233class AP_CANManager34{35public:36AP_CANManager();3738/* Do not allow copies */39CLASS_NO_COPY(AP_CANManager);4041static AP_CANManager* get_singleton()42{43if (_singleton == nullptr) {44AP_HAL::panic("CANManager used before allocation.");45}46return _singleton;47}4849enum LogLevel : uint8_t {50LOG_NONE,51LOG_ERROR,52LOG_WARNING,53LOG_INFO,54LOG_DEBUG,55};5657__INITFUNC__ void init(void);5859// register a new driver60bool register_driver(AP_CAN::Protocol dtype, AP_CANDriver *driver);6162// register a new auxillary sensor driver for 11 bit address frames63bool register_11bit_driver(AP_CAN::Protocol dtype, CANSensor *sensor, uint8_t &driver_index);6465// returns number of active CAN Drivers66uint8_t get_num_drivers(void) const67{68return HAL_MAX_CAN_PROTOCOL_DRIVERS;69}7071// return driver for index i72AP_CANDriver* get_driver(uint8_t i) const73{74if (i < ARRAY_SIZE(_drivers)) {75return _drivers[i];76}77return nullptr;78}7980// returns current log level81LogLevel get_log_level(void) const82{83return LogLevel(_loglevel.get());84}8586// Method to log status and debug information for review while debugging87void log_text(AP_CANManager::LogLevel loglevel, const char *tag, const char *fmt, ...) FMT_PRINTF(4,5);8889void log_retrieve(ExpandingString &str) const;9091// return driver type index i92AP_CAN::Protocol get_driver_type(uint8_t i) const93{94if (i < ARRAY_SIZE(_driver_type_cache)) {95return _driver_type_cache[i];96}97return AP_CAN::Protocol::None;98}99100static const struct AP_Param::GroupInfo var_info[];101102private:103104// Parameter interface for CANIfaces105class CANIface_Params106{107friend class AP_CANManager;108109public:110CANIface_Params()111{112AP_Param::setup_object_defaults(this, var_info);113}114115static const struct AP_Param::GroupInfo var_info[];116117enum class Options : uint32_t {118LOG_ALL_FRAMES = (1U<<0),119};120121bool option_is_set(Options option) const {122return (_options & uint32_t(option)) != 0;123}124125private:126AP_Int8 _driver_number;127AP_Int32 _bitrate;128AP_Int32 _fdbitrate;129AP_Int32 _options;130131#if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED132uint8_t logging_id;133#endif134};135136//Parameter Interface for CANDrivers137class CANDriver_Params138{139friend class AP_CANManager;140141public:142CANDriver_Params()143{144AP_Param::setup_object_defaults(this, var_info);145}146static const struct AP_Param::GroupInfo var_info[];147148private:149AP_Int8 _driver_type;150AP_Int8 _driver_type_11bit;151AP_CANDriver* _uavcan;152AP_CANDriver* _piccolocan;153};154155CANIface_Params _interfaces[HAL_NUM_CAN_IFACES];156AP_CANDriver* _drivers[HAL_MAX_CAN_PROTOCOL_DRIVERS];157CANDriver_Params _drv_param[HAL_MAX_CAN_PROTOCOL_DRIVERS];158AP_CAN::Protocol _driver_type_cache[HAL_MAX_CAN_PROTOCOL_DRIVERS];159160AP_Int8 _loglevel;161uint8_t _num_drivers;162#if AP_CAN_SLCAN_ENABLED163SLCAN::CANIface _slcan_interface;164#endif165166static AP_CANManager *_singleton;167168char* _log_buf;169uint32_t _log_pos;170171HAL_Semaphore _sem;172173#if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED174/*175handler for CAN frames for logging176*/177void can_logging_callback(uint8_t bus, const AP_HAL::CANFrame &frame, AP_HAL::CANIface::CanIOFlags flags);178void check_logging_enable(void);179#endif180};181182namespace AP183{184AP_CANManager& can();185}186187#endif // HAL_CANMANAGER_ENABLED188189190