Skip to content

Commit

Permalink
canard: fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bugobliterator committed May 15, 2023
1 parent 1ad9620 commit 8303516
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 137 deletions.
2 changes: 1 addition & 1 deletion canard/handler_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class HandlerList {
HandlerList(const HandlerList&) = delete;

// destructor, remove the entry from the singly-linked list
~HandlerList() NOINLINE_FUNC {
virtual ~HandlerList() NOINLINE_FUNC {
#ifdef WITH_SEMAPHORE
WITH_SEMAPHORE(sem[index]);
#endif
Expand Down
5 changes: 4 additions & 1 deletion canard/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct Transfer {
uint8_t* inout_transfer_id; ///< Transfer ID reference
uint8_t priority; ///< Priority of the transfer
const void* payload; ///< Pointer to the payload
uint16_t payload_len; ///< Length of the payload
uint32_t payload_len; ///< Length of the payload
uint8_t iface_mask; ///< Bitmask of interfaces to send the transfer on
bool canfd; ///< true if the transfer is CAN FD
uint32_t timeout_ms; ///< timeout in ms
Expand All @@ -52,6 +52,9 @@ class Interface {
canfd(_canfd)
{}

/// @brief Interface destructor
virtual ~Interface() {}

/// @brief broadcast message
/// @param bcast_transfer transfer to broadcast
/// @return true if broadcast was added to the queue
Expand Down
8 changes: 7 additions & 1 deletion canard/publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Sender {
transfer.priority = priority;
transfer.timeout_ms = timeout;
return interface.request(destination_node_id, transfer);
case CanardTransferTypeResponse:
default:
return false;
}
Expand Down Expand Up @@ -100,8 +101,13 @@ class Publisher : public Sender {
/// @param canfd true if the message should be sent as CAN FD
/// @return true if the message was put into the queue successfully
bool broadcast(msgtype& msg, bool canfd) {
#if !CANARD_ENABLE_CANFD
if (canfd) {
return false;
}
#endif
// encode the message
uint16_t len = msgtype::cxx_iface::encode(&msg, msg_buf
uint32_t len = msgtype::cxx_iface::encode(&msg, msg_buf
#if CANARD_ENABLE_CANFD
, !canfd
#elif CANARD_ENABLE_TAO_OPTION
Expand Down
7 changes: 6 additions & 1 deletion canard/service_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@ class Client : public HandlerList, public Sender {
/// @param canfd true if CAN FD is to be used
/// @return true if the request was put into the queue successfully
bool request(uint8_t destination_node_id, typename rsptype::cxx_iface::reqtype& msg, bool canfd) {
#if !CANARD_ENABLE_CANFD
if (canfd) {
return false;
}
#endif
// encode the message
uint16_t len = rsptype::cxx_iface::req_encode(&msg, req_buf
uint32_t len = rsptype::cxx_iface::req_encode(&msg, req_buf
#if CANARD_ENABLE_CANFD
, !canfd
#elif CANARD_ENABLE_TAO_OPTION
Expand Down
2 changes: 1 addition & 1 deletion canard/service_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Server : public HandlerList {
/// @return true if the response was put into the queue successfully
bool respond(const CanardRxTransfer& transfer, typename reqtype::cxx_iface::rsptype& msg) {
// encode the message
uint16_t len = reqtype::cxx_iface::rsp_encode(&msg, rsp_buf
uint32_t len = reqtype::cxx_iface::rsp_encode(&msg, rsp_buf
#if CANARD_ENABLE_CANFD
, !transfer.canfd
#elif CANARD_ENABLE_TAO_OPTION
Expand Down
98 changes: 44 additions & 54 deletions canard/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,77 +1,67 @@
cmake_minimum_required (VERSION 3.5)
project(CxxCanard)
include(CTest)

include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.13.0
)

FetchContent_Declare(
DSDL
GIT_REPOSITORY https://github.com/DroneCAN/DSDL.git
GIT_TAG master
)

FetchContent_Declare(
dronecan_dsdlc
GIT_REPOSITORY https://github.com/DroneCAN/dronecan_dsdlc.git
GIT_TAG master
)

FetchContent_MakeAvailable(googletest DSDL dronecan_dsdlc)

# execute process to generate libcanard headers using dronecan_dsdlc/dronecan_dsdlc.py
execute_process(COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/dronecan_dsdlc/dronecan_dsdlc.py
execute_process(COMMAND python3 ${dronecan_dsdlc_SOURCE_DIR}/dronecan_dsdlc.py
-O ${CMAKE_CURRENT_BINARY_DIR}/dsdlc_generated
DSDL/uavcan
DSDL/dronecan
DSDL/ardupilot
DSDL/com
${dsdl_SOURCE_DIR}/uavcan
${dsdl_SOURCE_DIR}/dronecan
${dsdl_SOURCE_DIR}/ardupilot
${dsdl_SOURCE_DIR}/com
)

if (CANARD_ENABLE_CANFD)
add_definitions(-DCANARD_ENABLE_CANFD=1)
endif()

# generate dsdl messages
include_directories(${CMAKE_CURRENT_BINARY_DIR}/dsdlc_generated/include)

# add local directory to include path
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../)

# add libcanard source files
file(GLOB LIBCANARD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../*.c)
list(APPEND SRC_FILES ${LIBCANARD_SOURCES})

# glob all generated dsdlc files
file(GLOB DSDL_GENERATED_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dsdlc_generated/src/*.c*)

# append DSDL_GENERATED_SOURCES_C to SRC_FILES
list(APPEND SRC_FILES ${DSDL_GENERATED_SOURCES})

add_compile_options(-Wall -m32 -g)
add_link_options(-m32)

add_subdirectory(googletest)

enable_testing()

set(SRC_FILES_TEST ${SRC_FILES}
cxx_test_interface.cpp
set(SRC_FILES_TEST cxx_test_interface.cpp
test_cxx_wrappers.cpp)
set(SRC_FILES_CANARD ${SRC_FILES}
canard_interface.cpp
set(SRC_FILES_CANARD canard_interface.cpp
test_canard_interface.cpp)

include_directories(googletest/googletest/include)
# create test target
add_executable(${PROJECT_NAME}_test_cf ${SRC_FILES} ${SRC_FILES_TEST})
add_executable(${PROJECT_NAME}_test_canard ${SRC_FILES} ${SRC_FILES_CANARD})

if (CANARD_ENABLE_COVERAGE)
add_compile_options(-fprofile-arcs -ftest-coverage)
add_link_options(-fprofile-arcs -ftest-coverage)
endif()
set(DSDL_SOURCE_CFLAGS "-Wno-gnu-empty-struct -Wno-implicit-int-conversion -Wno-unused-parameter -Wno-unused-const-variable -Wno-sign-conversion -Wno-missing-declarations -Wno-conversion -Wno-pedantic")

# create test target
add_executable(test_cf ${SRC_FILES_TEST})
add_executable(test_canard ${SRC_FILES_CANARD})
set_source_files_properties(${SRC_FILES} PROPERTIES COMPILE_FLAGS "${CANARD_C_FLAGS} ${DSDL_SOURCE_CFLAGS}")
set_source_files_properties(${SRC_FILES_TEST} PROPERTIES COMPILE_FLAGS "${CANARD_CXX_FLAGS}")
set_source_files_properties(${SRC_FILES_CANARD} PROPERTIES COMPILE_FLAGS "${CANARD_CXX_FLAGS}")

target_link_libraries(test_cf GTest::gtest_main)
include(GoogleTest)
gtest_discover_tests(test_cf)
if (CANARD_LINK_FLAGS)
set_target_properties(${PROJECT_NAME}_test_cf PROPERTIES LINK_FLAGS "${CANARD_LINK_FLAGS}")
set_target_properties(${PROJECT_NAME}_test_canard PROPERTIES LINK_FLAGS "${CANARD_LINK_FLAGS}")
endif()

target_link_libraries(test_canard GTest::gtest_main)
target_link_libraries(${PROJECT_NAME}_test_cf GTest::gtest_main canard_tgt canard_private_tgt pthread)
include(GoogleTest)
gtest_discover_tests(test_canard)
gtest_discover_tests(${PROJECT_NAME}_test_cf)

if (CANARD_ENABLE_COVERAGE)
add_custom_target(coverage
COMMAND lcov --directory . --capture --output-file coverage.info
COMMAND lcov --remove coverage.info '/usr/*' '*gtest*' '*tests/*' '*dsdlc_generated/*' --output-file coverage.info
COMMAND lcov --list coverage.info
COMMAND genhtml coverage.info --output-directory coverage
COMMENT echo "Coverage report generated at ${CMAKE_BINARY_DIR}/coverage/index.html"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_command(TARGET coverage POST_BUILD COMMAND ;)
endif()
target_link_libraries(${PROJECT_NAME}_test_canard GTest::gtest_main canard_tgt canard_private_tgt pthread)
include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME}_test_canard)
15 changes: 10 additions & 5 deletions canard/tests/canard_interface.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "canard_interface.h"
#include <iostream>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"

using namespace Canard;

Expand All @@ -16,7 +18,7 @@ bool CanardInterface::broadcast(const Transfer &bcast_transfer) {
.inout_transfer_id = bcast_transfer.inout_transfer_id,
.priority = bcast_transfer.priority,
.payload = (const uint8_t*)bcast_transfer.payload,
.payload_len = bcast_transfer.payload_len,
.payload_len = (uint16_t)bcast_transfer.payload_len,
#if CANARD_ENABLE_CANFD
.canfd = bcast_transfer.canfd,
#endif
Expand All @@ -37,7 +39,7 @@ bool CanardInterface::request(uint8_t destination_node_id, const Transfer &req_t
.inout_transfer_id = req_transfer.inout_transfer_id,
.priority = req_transfer.priority,
.payload = (const uint8_t*)req_transfer.payload,
.payload_len = req_transfer.payload_len,
.payload_len = (uint16_t)req_transfer.payload_len,
#if CANARD_ENABLE_CANFD
.canfd = req_transfer.canfd,
#endif
Expand All @@ -57,7 +59,7 @@ bool CanardInterface::respond(uint8_t destination_node_id, const Transfer &res_t
.inout_transfer_id = res_transfer.inout_transfer_id,
.priority = res_transfer.priority,
.payload = (const uint8_t*)res_transfer.payload,
.payload_len = res_transfer.payload_len,
.payload_len = (uint16_t)res_transfer.payload_len,
#if CANARD_ENABLE_CANFD
.canfd = res_transfer.canfd,
#endif
Expand Down Expand Up @@ -86,6 +88,8 @@ bool CanardInterface::shouldAcceptTransfer(const CanardInstance* ins,
uint16_t data_type_id,
CanardTransferType transfer_type,
uint8_t source_node_id) {
(void)transfer_type;
(void)source_node_id;
CanardInterface* iface = (CanardInterface*) ins->user_reference;
return iface->accept_message(data_type_id, *out_data_type_signature);
}
Expand All @@ -99,12 +103,13 @@ void CanardTestNetwork::route_frame(CanardTestInterface *send_iface, const Canar
}

void CanardTestInterface::update_tx(uint64_t timestamp_usec) {
for (const CanardCANFrame* txf = canardPeekTxQueue(&canard); txf != NULL; txf = canardPeekTxQueue(&canard)) {
for (const CanardCANFrame* txf = canardPeekTxQueue(&canard); txf != nullptr; txf = canardPeekTxQueue(&canard)) {
CanardTestNetwork::get_network().route_frame(this, *txf, timestamp_usec);
canardPopTxQueue(&canard);
}
}

void CanardTestInterface::set_node_id(uint8_t node_id) {
canardSetLocalNodeID(&canard, node_id);
}
}
#pragma GCC diagnostic pop
2 changes: 1 addition & 1 deletion canard/tests/canard_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CanardTestNetwork {

class CanardTestInterface : public CanardInterface {
public:
CanardTestInterface(int index) :
CanardTestInterface(uint8_t index) :
CanardInterface(index) {
CanardTestNetwork::get_network().ifaces[index] = this;
}
Expand Down
3 changes: 2 additions & 1 deletion canard/tests/cxx_test_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using namespace Canard;

void TestNetwork::route_msg(CoreTestInterface *send_iface, uint8_t source_node_id, uint8_t destination_node_id, Transfer transfer) {
(void)destination_node_id;
// prepare CanardRxTransfer
CanardRxTransfer rx_transfer {};
rx_transfer.data_type_id = transfer.data_type_id;
rx_transfer.payload_len = transfer.payload_len;
rx_transfer.payload_len = (uint16_t)transfer.payload_len;
rx_transfer.payload_head = (uint8_t*)transfer.payload;
rx_transfer.transfer_id = *transfer.inout_transfer_id;
rx_transfer.source_node_id = source_node_id;
Expand Down
4 changes: 3 additions & 1 deletion canard/tests/cxx_test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TestNetwork {

class CoreTestInterface : public Interface {
public:
CoreTestInterface(int index) :
CoreTestInterface(uint8_t index) :
Interface(index) {
TestNetwork::get_network().ifaces[index] = this;
}
Expand All @@ -34,6 +34,8 @@ class CoreTestInterface : public Interface {
CoreTestInterface& operator=(const CoreTestInterface&) = delete;
CoreTestInterface() = delete;

virtual ~CoreTestInterface() {}

/// @brief broadcast message to all listeners on Interface
/// @param bc_transfer
/// @return true if message was added to the queue
Expand Down
61 changes: 0 additions & 61 deletions canard/tests/run_test.sh

This file was deleted.

Loading

0 comments on commit 8303516

Please sign in to comment.