Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ jobs:
source ".venv/bin/activate"
make ${{ matrix.target }}-build

- name: Run unit tests
run: |
source ".venv/bin/activate"
make tests

- name: Run SITL and smoke test
timeout-minutes: 3
run: |
Expand Down
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ target_include_directories(libdcnode PUBLIC
${DSDL_OUT_DIR}/include
)

target_compile_definitions(libdcnode PUBLIC
DRONECAN_CXX_WRAPPERS=1
)


set_source_files_properties(${SERIALIZATION_SOURCES}
PROPERTIES
Expand All @@ -47,3 +51,25 @@ set_source_files_properties(${SERIALIZATION_SOURCES}
target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:GNU,Clang,AppleClang>:-Wno-address-of-packed-member>
)

option(LIBDCNODE_BUILD_TESTS "Build libdcnode host tests" OFF)
if(LIBDCNODE_BUILD_TESTS)
enable_testing()
add_executable(libdcnode_pub_sub_test
${CMAKE_CURRENT_LIST_DIR}/tests/pub_sub_test.cpp
${CMAKE_CURRENT_LIST_DIR}/Libs/libcanard_v0/canard.c
${DSDL_OUT_DIR}/src/uavcan.protocol.Panic.c
)
target_include_directories(libdcnode_pub_sub_test PRIVATE
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/Libs
${DSDL_OUT_DIR}/include
)
target_compile_definitions(libdcnode_pub_sub_test PRIVATE
DRONECAN_CXX_WRAPPERS=1
)
target_compile_options(libdcnode_pub_sub_test PRIVATE
$<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Werror>
)
add_test(NAME libdcnode_pub_sub_test COMMAND libdcnode_pub_sub_test)
endif()
93 changes: 93 additions & 0 deletions Libs/canard/cxx_wrappers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2022 Siddharth B Purohit, CubePilot Pty Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

// The upstream header includes helpers.h for the complete Canard C++ API.
// libdcnode uses only these generated type metadata macros, which do not need
// the allocator helpers.

/// @brief Broadcast message interface.
/// @param MSGTYPE message type name
/// @param MSG_ID message id
/// @param MSG_SIGNATURE message signature
/// @param MSG_MAX_SIZE maximum message size
#if CANARD_ENABLE_CANFD || CANARD_ENABLE_TAO_OPTION
#define BROADCAST_MESSAGE_CXX_IFACE(MSGTYPE, MSG_ID, MSG_SIGNATURE, MSG_MAX_SIZE) \
class MSGTYPE##_cxx_iface { \
public: \
typedef MSGTYPE msgtype; \
static constexpr uint32_t (*encode)(msgtype*, uint8_t*, bool) = MSGTYPE##_encode; \
static constexpr bool (*decode)(const CanardRxTransfer* transfer, msgtype*) = MSGTYPE##_decode; \
static constexpr uint16_t ID = MSG_ID; \
static constexpr uint64_t SIGNATURE = MSG_SIGNATURE; \
static constexpr uint16_t MAX_SIZE = MSG_MAX_SIZE; \
};
#else
#define BROADCAST_MESSAGE_CXX_IFACE(MSGTYPE, MSG_ID, MSG_SIGNATURE, MSG_MAX_SIZE) \
class MSGTYPE##_cxx_iface { \
public: \
typedef MSGTYPE msgtype; \
static constexpr uint32_t (*encode)(msgtype*, uint8_t*) = MSGTYPE##_encode; \
static constexpr bool (*decode)(const CanardRxTransfer* transfer, msgtype*) = MSGTYPE##_decode; \
static constexpr uint16_t ID = MSG_ID; \
static constexpr uint64_t SIGNATURE = MSG_SIGNATURE; \
static constexpr uint16_t MAX_SIZE = MSG_MAX_SIZE; \
};
#endif

#if CANARD_ENABLE_CANFD || CANARD_ENABLE_TAO_OPTION
#define SERVICE_MESSAGE_CXX_IFACE(SVCTYPE, SVC_ID, SVC_SIGNATURE, SVC_REQUEST_MAX_SIZE, SVC_RESPONSE_MAX_SIZE) \
class SVCTYPE##_cxx_iface { \
public: \
typedef SVCTYPE##Request reqtype; \
typedef SVCTYPE##Response rsptype; \
static constexpr uint32_t (*req_encode)(reqtype*, uint8_t*, bool) = SVCTYPE##Request_encode; \
static constexpr uint32_t (*rsp_encode)(rsptype*, uint8_t*, bool) = SVCTYPE##Response_encode; \
static constexpr bool (*req_decode)(const CanardRxTransfer* transfer, reqtype*) = SVCTYPE##Request_decode; \
static constexpr bool (*rsp_decode)(const CanardRxTransfer* transfer, rsptype*) = SVCTYPE##Response_decode; \
static constexpr uint16_t ID = SVC_ID; \
static constexpr uint64_t SIGNATURE = SVC_SIGNATURE; \
static constexpr uint16_t REQ_MAX_SIZE = SVC_REQUEST_MAX_SIZE; \
static constexpr uint16_t RSP_MAX_SIZE = SVC_RESPONSE_MAX_SIZE; \
};
#else
/// @brief Service message interface.
/// @param SVCTYPE message type name
/// @param SVC_ID message id
/// @param SVC_SIGNATURE message signature
/// @param SVC_REQUEST_MAX_SIZE maximum request size
/// @param SVC_RESPONSE_MAX_SIZE maximum response size
#define SERVICE_MESSAGE_CXX_IFACE(SVCTYPE, SVC_ID, SVC_SIGNATURE, SVC_REQUEST_MAX_SIZE, SVC_RESPONSE_MAX_SIZE) \
class SVCTYPE##_cxx_iface { \
public: \
typedef SVCTYPE##Request reqtype; \
typedef SVCTYPE##Response rsptype; \
static constexpr uint32_t (*req_encode)(reqtype*, uint8_t*) = SVCTYPE##Request_encode; \
static constexpr uint32_t (*rsp_encode)(rsptype*, uint8_t*) = SVCTYPE##Response_encode; \
static constexpr bool (*req_decode)(const CanardRxTransfer* transfer, reqtype*) = SVCTYPE##Request_decode; \
static constexpr bool (*rsp_decode)(const CanardRxTransfer* transfer, rsptype*) = SVCTYPE##Response_decode; \
static constexpr uint16_t ID = SVC_ID; \
static constexpr uint64_t SIGNATURE = SVC_SIGNATURE; \
static constexpr uint16_t REQ_MAX_SIZE = SVC_REQUEST_MAX_SIZE; \
static constexpr uint16_t RSP_MAX_SIZE = SVC_RESPONSE_MAX_SIZE; \
};
#endif
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
BUILD_DIR:=$(ROOT_DIR)/build
BUILD_EXAMPLES_DIR:=$(BUILD_DIR)/src/examples
BUILD_TESTS_DIR:=$(BUILD_DIR)/tests

define build_sitl
$(info Build example $(1)...)
Expand All @@ -17,7 +18,7 @@ define run_sitl
$(BUILD_EXAMPLES_DIR)/$(1)/ubuntu
endef

.PHONY: ubuntu ubuntu-build ubuntu-run
.PHONY: ubuntu ubuntu-build ubuntu-run tests

ubuntu-build:
$(call build_sitl,ubuntu)
Expand All @@ -31,14 +32,19 @@ ubuntu-run:
ubuntu: ubuntu-build
$(call run_sitl,ubuntu)

tests:
cmake -S $(ROOT_DIR) -B $(BUILD_TESTS_DIR) -DLIBDCNODE_BUILD_TESTS=ON
cmake --build $(BUILD_TESTS_DIR) --target libdcnode_pub_sub_test
ctest --test-dir $(BUILD_TESTS_DIR) --output-on-failure

clean:
rm -rf build/examples/

code_style: cpplint cppcheck crlf
astyle:
./scripts/code_style/check_astyle.py src include --astylerc scripts/code_style/astylerc
cpplint:
cpplint src/*.cpp include/libdcnode/*.h include/libdcnode/*.hpp
cpplint src/*.cpp tests/*.cpp include/libdcnode/*.h include/libdcnode/*.hpp
cppcheck:
./scripts/code_style/cppcheck.sh
crlf:
Expand Down
74 changes: 17 additions & 57 deletions include/libdcnode/pub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@

#include <cstdint>
#include <algorithm>
#include <type_traits>
#include <utility>
#include "libdcnode/dronecan.h"
#include "libdcnode/platform.hpp"
#include "dronecan_msgs.h"

// Max encoded message size (bytes) for publisher stack buffer.
// Override via -DLIBDCNODE_MAX_PUB_MESSAGE_SIZE=... if your DSDL set requires more.
#ifndef LIBDCNODE_MAX_PUB_MESSAGE_SIZE
#define LIBDCNODE_MAX_PUB_MESSAGE_SIZE 600U
#endif

// Initial delay before first publish after boot/reset (ms).
// Intended to reduce CAN bus flooding during rapid reboot loops (e.g., watchdog resets).
// Override via -DLIBDCNODE_INITIAL_PUB_DELAY_MS=...
Expand All @@ -31,48 +23,6 @@

namespace libdcnode
{

template <typename MessageType>
struct DronecanPublisherTraits;

#define LIBDCNODE_PUB_ENCODE(MessageType) MessageType##_encode
#define LIBDCNODE_PUB_BROADCAST(buffer, Prefix, inout_transfer_id, size) \
uavcanPublish(Prefix##_SIGNATURE, Prefix##_ID, inout_transfer_id, CANARD_TRANSFER_PRIORITY_MEDIUM, \
buffer, size)
#define LIBDCNODE_DEFINE_PUB_TRAITS(MessageType, MessagePrefix) \
template <> \
struct DronecanPublisherTraits<MessageType> \
{ \
using EncodeRet = decltype(LIBDCNODE_PUB_ENCODE(MessageType)( \
std::declval<MessageType *>(), std::declval<std::uint8_t *>())); \
static_assert(std::is_same_v<EncodeRet, std::uint32_t>, "*_encode() must return uint32_t exactly."); \
static inline int16_t publish_once(MessageType *msg, uint8_t *inout_transfer_id) \
{ \
uint8_t buffer[LIBDCNODE_MAX_PUB_MESSAGE_SIZE]; \
auto bytes_needed = LIBDCNODE_PUB_ENCODE(MessageType)(msg, buffer); \
if (bytes_needed == 0U || bytes_needed > LIBDCNODE_MAX_PUB_MESSAGE_SIZE) \
return (int16_t)0; \
return LIBDCNODE_PUB_BROADCAST(buffer, MessagePrefix, inout_transfer_id, bytes_needed); \
} \
};

LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_power_CircuitStatus, UAVCAN_EQUIPMENT_POWER_CIRCUITSTATUS)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_power_BatteryInfo, UAVCAN_EQUIPMENT_POWER_BATTERYINFO)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_ahrs_RawIMU, UAVCAN_EQUIPMENT_AHRS_RAWIMU)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_ahrs_MagneticFieldStrength2,
UAVCAN_EQUIPMENT_AHRS_MAGNETICFIELDSTRENGTH2)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_camera_gimbal_Status, UAVCAN_EQUIPMENT_CAMERA_GIMBAL_STATUS)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_actuator_ArrayCommand,
UAVCAN_EQUIPMENT_ACTUATOR_ARRAYCOMMAND)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_actuator_Status, UAVCAN_EQUIPMENT_ACTUATOR_STATUS)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_esc_Status, UAVCAN_EQUIPMENT_ESC_STATUS)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_hardpoint_Status, UAVCAN_EQUIPMENT_HARDPOINT_STATUS)
LIBDCNODE_DEFINE_PUB_TRAITS(::ardupilot_equipment_power_BatteryInfoAux, ARDUPILOT_EQUIPMENT_POWER_BATTERYINFOAUX)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_air_data_StaticPressure,
UAVCAN_EQUIPMENT_AIR_DATA_STATICPRESSURE)
LIBDCNODE_DEFINE_PUB_TRAITS(::uavcan_equipment_range_sensor_Measurement,
UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT)

template <typename MessageType>
class DronecanPub
{
Expand All @@ -81,8 +31,23 @@ namespace libdcnode

inline void publish()
{
DronecanPublisherTraits<MessageType>::publish_once(&msg, &inout_transfer_id);
inout_transfer_id++;
using Interface = typename MessageType::cxx_iface;
uint8_t buffer[Interface::MAX_SIZE];
#if CANARD_ENABLE_CANFD || CANARD_ENABLE_TAO_OPTION
const auto bytes_needed = Interface::encode(&msg, buffer, true);
#else
const auto bytes_needed = Interface::encode(&msg, buffer);
#endif
if (bytes_needed == 0U || bytes_needed > Interface::MAX_SIZE)
{
return;
}
uavcanPublish(Interface::SIGNATURE,
Interface::ID,
&inout_transfer_id,
CANARD_TRANSFER_PRIORITY_MEDIUM,
buffer,
bytes_needed);
}

MessageType msg{};
Expand Down Expand Up @@ -120,9 +85,4 @@ namespace libdcnode

} // namespace libdcnode

// Undef internal macros
#undef LIBDCNODE_PUB_ENCODE
#undef LIBDCNODE_PUB_BROADCAST
#undef LIBDCNODE_DEFINE_PUB_TRAITS

#endif // LIBDCNODE_PUB_HPP_
53 changes: 4 additions & 49 deletions include/libdcnode/sub.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,8 @@
#include "libdcnode/dronecan.h"
#include "dronecan_msgs.h"

// Max encoded message size (bytes) for subscriber stack buffer.
// Override via -DLIBDCNODE_MAX_SUB_MESSAGE_SIZE=... if your DSDL set requires more.
#ifndef LIBDCNODE_MAX_SUB_MESSAGE_SIZE
#define LIBDCNODE_MAX_SUB_MESSAGE_SIZE 250
#endif

// Initial delay before first subscribe after boot/reset (ms).
// Intended to reduce CAN bus flooding during rapid reboot loops (e.g., watchdog resets).
// Override via -DLIBDCNODE_INITIAL_SUB_DELAY_MS=...
#ifndef LIBDCNODE_INITIAL_SUB_DELAY_MS
#define LIBDCNODE_INITIAL_SUB_DELAY_MS 500U
#endif

namespace libdcnode
{

template <typename MessageType>
struct DronecanSubscriberTraits;

#define LIBDCNODE_DEFINE_SUB_TRAITS(MessageType, Prefix) \
template <> \
struct DronecanSubscriberTraits<MessageType> \
{ \
static inline int8_t subscribe(void (*callback)(CanardRxTransfer *)) \
{ \
return uavcanSubscribe(Prefix##_SIGNATURE, Prefix##_ID, callback); \
} \
static inline int8_t deserialize(CanardRxTransfer *transfer, MessageType *msg) \
{ \
return MessageType##_decode(transfer, msg); \
} \
};

LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_hardpoint_Command,
UAVCAN_EQUIPMENT_HARDPOINT_COMMAND)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_hardpoint_Status,
UAVCAN_EQUIPMENT_HARDPOINT_STATUS)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_indication_LightsCommand, UAVCAN_EQUIPMENT_INDICATION_LIGHTSCOMMAND)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_actuator_ArrayCommand, UAVCAN_EQUIPMENT_ACTUATOR_ARRAYCOMMAND)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_camera_gimbal_AngularCommand,
UAVCAN_EQUIPMENT_CAMERA_GIMBAL_ANGULARCOMMAND)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_ahrs_Solution, UAVCAN_EQUIPMENT_AHRS_SOLUTION)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_esc_RawCommand, UAVCAN_EQUIPMENT_ESC_RAWCOMMAND)
LIBDCNODE_DEFINE_SUB_TRAITS(::uavcan_equipment_safety_ArmingStatus, UAVCAN_EQUIPMENT_SAFETY_ARMINGSTATUS)

template <typename MessageType>
class DronecanSub
{
Expand All @@ -73,7 +30,8 @@ namespace libdcnode
{
user_callback = callback;
filter = filter_;
auto sub_id = DronecanSubscriberTraits<MessageType>::subscribe(transfer_callback);
using Interface = typename MessageType::cxx_iface;
auto sub_id = uavcanSubscribe(Interface::SIGNATURE, Interface::ID, transfer_callback);
if (sub_id < 0) {
return sub_id;
}
Expand All @@ -83,8 +41,8 @@ namespace libdcnode

static inline void transfer_callback(CanardRxTransfer *transfer)
{
int8_t res = DronecanSubscriberTraits<MessageType>::deserialize(transfer, &msg);
if (res < 0)
using Interface = typename MessageType::cxx_iface;
if (Interface::decode(transfer, &msg))
{
return;
}
Expand All @@ -111,7 +69,4 @@ namespace libdcnode

} // namespace libdcnode

// Undef internal macros
#undef LIBDCNODE_DEFINE_SUB_TRAITS

#endif // LIBDCNODE_SUB_HPP_
Loading