2018-05-24 03:10:21 -03:00
|
|
|
/*
|
|
|
|
simple UAVCAN network sniffer as an ArduPilot firmware
|
|
|
|
*/
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
|
2018-05-24 03:10:21 -03:00
|
|
|
|
|
|
|
#include <AP_HAL/Semaphores.h>
|
|
|
|
|
2018-02-27 20:02:09 -04:00
|
|
|
#include <AP_UAVCAN/AP_UAVCAN.h>
|
|
|
|
|
|
|
|
#include <uavcan/uavcan.hpp>
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
#include <uavcan/helpers/heap_based_pool_allocator.hpp>
|
|
|
|
|
2022-09-27 01:53:55 -03:00
|
|
|
#include <uavcan/equipment/gnss/Fix2.hpp>
|
2018-05-24 03:10:21 -03:00
|
|
|
#include <uavcan/equipment/gnss/Auxiliary.hpp>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/ahrs/MagneticFieldStrength.hpp>
|
|
|
|
#include <uavcan/equipment/ahrs/MagneticFieldStrength2.hpp>
|
|
|
|
#include <uavcan/equipment/air_data/StaticPressure.hpp>
|
|
|
|
#include <uavcan/equipment/air_data/StaticTemperature.hpp>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/actuator/ArrayCommand.hpp>
|
|
|
|
#include <uavcan/equipment/actuator/Command.hpp>
|
|
|
|
#include <uavcan/equipment/actuator/Status.hpp>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/esc/RawCommand.hpp>
|
|
|
|
#include <uavcan/equipment/indication/LightsCommand.hpp>
|
|
|
|
#include <uavcan/equipment/indication/SingleLightCommand.hpp>
|
|
|
|
#include <uavcan/equipment/indication/RGB565.hpp>
|
|
|
|
|
|
|
|
#include <uavcan/equipment/power/BatteryInfo.hpp>
|
|
|
|
|
2019-07-05 02:19:18 -03:00
|
|
|
#include <com/hex/equipment/flow/Measurement.hpp>
|
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
|
|
|
|
#include <AP_HAL_Linux/CANSocketIface.h>
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
|
|
|
#include <AP_HAL_SITL/CANSocketIface.h>
|
|
|
|
#elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
|
|
|
|
#include <AP_HAL_ChibiOS/CANIface.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
void setup();
|
|
|
|
void loop();
|
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
|
|
|
#define UAVCAN_NODE_POOL_SIZE 8192
|
2019-05-27 23:18:36 -03:00
|
|
|
#ifdef UAVCAN_NODE_POOL_BLOCK_SIZE
|
|
|
|
#undef UAVCAN_NODE_POOL_BLOCK_SIZE
|
|
|
|
#endif
|
2018-05-24 03:10:21 -03:00
|
|
|
#define UAVCAN_NODE_POOL_BLOCK_SIZE 256
|
|
|
|
|
2018-02-27 20:02:09 -04:00
|
|
|
#define debug_uavcan(fmt, args...) do { hal.console->printf(fmt, ##args); } while (0)
|
2018-05-24 03:10:21 -03:00
|
|
|
|
|
|
|
class UAVCAN_sniffer {
|
|
|
|
public:
|
|
|
|
UAVCAN_sniffer();
|
|
|
|
~UAVCAN_sniffer();
|
|
|
|
|
|
|
|
void init(void);
|
|
|
|
void loop(void);
|
|
|
|
void print_stats(void);
|
2018-02-27 20:02:09 -04:00
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
private:
|
2018-02-27 20:02:09 -04:00
|
|
|
uint8_t driver_index = 0;
|
2018-05-24 03:10:21 -03:00
|
|
|
uavcan::Node<0> *_node;
|
|
|
|
|
|
|
|
// This will be needed to implement if UAVCAN is used with multithreading
|
|
|
|
// Such cases will be firmware update, etc.
|
|
|
|
class RaiiSynchronizer {
|
|
|
|
public:
|
|
|
|
RaiiSynchronizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~RaiiSynchronizer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uavcan::HeapBasedPoolAllocator<UAVCAN_NODE_POOL_BLOCK_SIZE, UAVCAN_sniffer::RaiiSynchronizer> _node_allocator;
|
2020-08-19 08:09:59 -03:00
|
|
|
|
|
|
|
AP_CANManager can_mgr;
|
2018-05-24 03:10:21 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *msg_name;
|
|
|
|
uint32_t count;
|
|
|
|
} counters[100];
|
|
|
|
|
|
|
|
static void count_msg(const char *name)
|
|
|
|
{
|
2018-08-02 20:27:17 -03:00
|
|
|
for (uint16_t i=0; i<ARRAY_SIZE(counters); i++) {
|
2018-05-24 03:10:21 -03:00
|
|
|
if (counters[i].msg_name == name) {
|
|
|
|
counters[i].count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (counters[i].msg_name == nullptr) {
|
|
|
|
counters[i].msg_name = name;
|
|
|
|
counters[i].count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MSG_CB(mtype, cbname) \
|
|
|
|
static void cb_ ## cbname(const uavcan::ReceivedDataStructure<mtype>& msg) { count_msg(msg.getDataTypeFullName()); }
|
|
|
|
|
|
|
|
MSG_CB(uavcan::protocol::NodeStatus, NodeStatus)
|
2022-09-27 01:53:55 -03:00
|
|
|
MSG_CB(uavcan::equipment::gnss::Fix2, Fix2)
|
2018-02-27 20:02:09 -04:00
|
|
|
MSG_CB(uavcan::equipment::gnss::Auxiliary, Auxiliary)
|
2018-05-24 03:10:21 -03:00
|
|
|
MSG_CB(uavcan::equipment::ahrs::MagneticFieldStrength, MagneticFieldStrength)
|
2018-02-27 20:02:09 -04:00
|
|
|
MSG_CB(uavcan::equipment::ahrs::MagneticFieldStrength2, MagneticFieldStrength2);
|
2018-05-24 03:10:21 -03:00
|
|
|
MSG_CB(uavcan::equipment::air_data::StaticPressure, StaticPressure)
|
|
|
|
MSG_CB(uavcan::equipment::air_data::StaticTemperature, StaticTemperature)
|
2018-02-27 20:02:09 -04:00
|
|
|
MSG_CB(uavcan::equipment::power::BatteryInfo, BatteryInfo);
|
2018-05-24 03:10:21 -03:00
|
|
|
MSG_CB(uavcan::equipment::actuator::ArrayCommand, ArrayCommand)
|
|
|
|
MSG_CB(uavcan::equipment::esc::RawCommand, RawCommand)
|
2018-02-27 20:02:09 -04:00
|
|
|
MSG_CB(uavcan::equipment::indication::LightsCommand, LightsCommand);
|
2019-07-05 02:19:18 -03:00
|
|
|
MSG_CB(com::hex::equipment::flow::Measurement, Measurement);
|
2018-05-24 03:10:21 -03:00
|
|
|
|
|
|
|
void UAVCAN_sniffer::init(void)
|
|
|
|
{
|
2020-08-19 08:09:59 -03:00
|
|
|
const_cast <AP_HAL::HAL&> (hal).can[driver_index] = new HAL_CANIface(driver_index);
|
2018-05-24 03:10:21 -03:00
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
if (hal.can[driver_index] == nullptr) {
|
2018-02-27 20:02:09 -04:00
|
|
|
AP_HAL::panic("Couldn't allocate CANManager, something is very wrong");
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
hal.can[driver_index]->init(1000000, AP_HAL::CANIface::NormalMode);
|
2018-02-27 20:02:09 -04:00
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
if (!hal.can[driver_index]->is_initialized()) {
|
2018-02-27 20:02:09 -04:00
|
|
|
debug_uavcan("Can not initialised\n");
|
|
|
|
return;
|
|
|
|
}
|
2020-08-19 08:09:59 -03:00
|
|
|
uavcan::CanIfaceMgr *_uavcan_iface_mgr = new uavcan::CanIfaceMgr;
|
2018-02-27 20:02:09 -04:00
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
if (_uavcan_iface_mgr == nullptr) {
|
2018-02-27 20:02:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:09:59 -03:00
|
|
|
if (!_uavcan_iface_mgr->add_interface(hal.can[driver_index])) {
|
|
|
|
debug_uavcan("Failed to add iface");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_node = new uavcan::Node<0>(*_uavcan_iface_mgr, uavcan::SystemClock::instance(), _node_allocator);
|
2018-02-27 20:02:09 -04:00
|
|
|
|
|
|
|
if (_node == nullptr) {
|
2018-05-24 03:10:21 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-27 20:02:09 -04:00
|
|
|
if (_node->isStarted()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-24 03:10:21 -03:00
|
|
|
|
|
|
|
uavcan::NodeID self_node_id(9);
|
2018-02-27 20:02:09 -04:00
|
|
|
_node->setNodeID(self_node_id);
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
char ndname[20];
|
2018-02-27 20:02:09 -04:00
|
|
|
snprintf(ndname, sizeof(ndname), "org.ardupilot:%u", driver_index);
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
uavcan::NodeStatusProvider::NodeName name(ndname);
|
2018-02-27 20:02:09 -04:00
|
|
|
_node->setName(name);
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
uavcan::protocol::SoftwareVersion sw_version; // Standard type uavcan.protocol.SoftwareVersion
|
|
|
|
sw_version.major = AP_UAVCAN_SW_VERS_MAJOR;
|
|
|
|
sw_version.minor = AP_UAVCAN_SW_VERS_MINOR;
|
2018-02-27 20:02:09 -04:00
|
|
|
_node->setSoftwareVersion(sw_version);
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
uavcan::protocol::HardwareVersion hw_version; // Standard type uavcan.protocol.HardwareVersion
|
2018-02-27 20:02:09 -04:00
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
hw_version.major = AP_UAVCAN_HW_VERS_MAJOR;
|
|
|
|
hw_version.minor = AP_UAVCAN_HW_VERS_MINOR;
|
2018-02-27 20:02:09 -04:00
|
|
|
_node->setHardwareVersion(hw_version);
|
|
|
|
|
|
|
|
int start_res = _node->start();
|
|
|
|
if (start_res < 0) {
|
|
|
|
debug_uavcan("UAVCAN: node start problem\n\r");
|
|
|
|
return;
|
2018-05-24 03:10:21 -03:00
|
|
|
}
|
|
|
|
|
2018-02-27 20:02:09 -04:00
|
|
|
#define START_CB(mtype, cbname) (new uavcan::Subscriber<mtype>(*_node))->start(cb_ ## cbname)
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
START_CB(uavcan::protocol::NodeStatus, NodeStatus);
|
2022-09-27 01:53:55 -03:00
|
|
|
START_CB(uavcan::equipment::gnss::Fix2, Fix2);
|
2018-02-27 20:02:09 -04:00
|
|
|
START_CB(uavcan::equipment::gnss::Auxiliary, Auxiliary);
|
2018-05-24 03:10:21 -03:00
|
|
|
START_CB(uavcan::equipment::ahrs::MagneticFieldStrength, MagneticFieldStrength);
|
2018-02-27 20:02:09 -04:00
|
|
|
START_CB(uavcan::equipment::ahrs::MagneticFieldStrength2, MagneticFieldStrength2);
|
2018-05-24 03:10:21 -03:00
|
|
|
START_CB(uavcan::equipment::air_data::StaticPressure, StaticPressure);
|
|
|
|
START_CB(uavcan::equipment::air_data::StaticTemperature, StaticTemperature);
|
2018-02-27 20:02:09 -04:00
|
|
|
START_CB(uavcan::equipment::power::BatteryInfo, BatteryInfo);
|
2018-05-24 03:10:21 -03:00
|
|
|
START_CB(uavcan::equipment::actuator::ArrayCommand, ArrayCommand);
|
|
|
|
START_CB(uavcan::equipment::esc::RawCommand, RawCommand);
|
2018-02-27 20:02:09 -04:00
|
|
|
START_CB(uavcan::equipment::indication::LightsCommand, LightsCommand);
|
2019-07-05 02:19:18 -03:00
|
|
|
START_CB(com::hex::equipment::flow::Measurement, Measurement);
|
2018-02-27 20:02:09 -04:00
|
|
|
|
|
|
|
|
2018-05-24 03:10:21 -03:00
|
|
|
/*
|
|
|
|
* Informing other nodes that we're ready to work.
|
|
|
|
* Default mode is INITIALIZING.
|
|
|
|
*/
|
2018-02-27 20:02:09 -04:00
|
|
|
_node->setModeOperational();
|
2018-05-24 03:10:21 -03:00
|
|
|
|
2018-02-27 20:02:09 -04:00
|
|
|
debug_uavcan("UAVCAN: init done\n\r");
|
2018-05-24 03:10:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UAVCAN_sniffer::loop(void)
|
|
|
|
{
|
2018-02-27 20:02:09 -04:00
|
|
|
if (_node == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_node->spin(uavcan::MonotonicDuration::fromMSec(1));
|
2018-05-24 03:10:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UAVCAN_sniffer::print_stats(void)
|
|
|
|
{
|
2020-08-19 08:09:59 -03:00
|
|
|
hal.console->printf("%lu\n", (unsigned long)AP_HAL::micros());
|
2018-05-24 03:10:21 -03:00
|
|
|
for (uint16_t i=0;i<100;i++) {
|
|
|
|
if (counters[i].msg_name == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
hal.console->printf("%s: %u\n", counters[i].msg_name, unsigned(counters[i].count));
|
|
|
|
counters[i].count = 0;
|
|
|
|
}
|
|
|
|
hal.console->printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static UAVCAN_sniffer sniffer;
|
|
|
|
|
|
|
|
UAVCAN_sniffer::UAVCAN_sniffer() :
|
|
|
|
_node_allocator(UAVCAN_NODE_POOL_SIZE, UAVCAN_NODE_POOL_SIZE)
|
|
|
|
{}
|
|
|
|
|
|
|
|
UAVCAN_sniffer::~UAVCAN_sniffer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
hal.scheduler->delay(2000);
|
|
|
|
hal.console->printf("Starting UAVCAN sniffer\n");
|
|
|
|
sniffer.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void)
|
|
|
|
{
|
|
|
|
sniffer.loop();
|
|
|
|
static uint32_t last_print_ms;
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - last_print_ms >= 1000) {
|
|
|
|
last_print_ms = now;
|
|
|
|
sniffer.print_stats();
|
|
|
|
}
|
|
|
|
|
|
|
|
// auto-reboot for --upload
|
|
|
|
if (hal.console->available() > 50) {
|
|
|
|
hal.console->printf("rebooting\n");
|
2020-05-22 21:24:33 -03:00
|
|
|
hal.console->discard_input();
|
2018-05-24 03:10:21 -03:00
|
|
|
hal.scheduler->reboot(false);
|
|
|
|
}
|
2020-05-22 21:24:33 -03:00
|
|
|
hal.console->discard_input();
|
2018-05-24 03:10:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|
|
|
|
|
2018-05-24 19:56:15 -03:00
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
const AP_HAL::HAL& hal = AP_HAL::get_HAL();
|
|
|
|
|
|
|
|
static void loop() { }
|
|
|
|
static void setup()
|
|
|
|
{
|
|
|
|
printf("Board not currently supported\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
AP_HAL_MAIN();
|
2018-05-24 03:10:21 -03:00
|
|
|
#endif
|