From 9e92b9a9b8a7b04a95da9f20e15f9ab6ab3a07c1 Mon Sep 17 00:00:00 2001 From: Alessandro Conforti Date: Mon, 11 Dec 2023 14:23:54 +0100 Subject: [PATCH] feat(can-manager): added tests minor: clang format --- can-manager/can_manager.c | 64 +++++++++++++++++++++++++++++++++------ can-manager/can_manager.h | 14 ++++++++- can-manager/test/test.c | 43 ++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 can-manager/test/test.c diff --git a/can-manager/can_manager.c b/can-manager/can_manager.c index 2105f85..ef33a5a 100644 --- a/can-manager/can_manager.c +++ b/can-manager/can_manager.c @@ -82,15 +82,30 @@ #include "can_manager.h" -int _n_active_can = 0; -int can_manager_error_code = 0; +int _n_active_can = 0; +int can_manager_error_code = 0; +#ifndef TEST HAL_StatusTypeDef can_manager_hal_status_retval = HAL_OK; +#endif generic_queue_t _rx_queues[CAN_MGR_MAX_CAN_BUSES]; generic_queue_t _tx_queues[CAN_MGR_MAX_CAN_BUSES]; uint8_t _rx_queues_data[CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t) * CAN_MGR_MAX_CAN_BUSES]; uint8_t _tx_queues_data[CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t) * CAN_MGR_MAX_CAN_BUSES]; void (*can_rx_msg_handlers[CAN_MGR_MAX_CAN_BUSES])(can_manager_message_t *); +#ifdef TEST +void print_message(can_manager_message_t *msg) { + printf("id: %d, size: %d, data: [", msg->id, msg->size); + if (msg->size > 0) { + printf("%d", msg->data[0]); + for (int i = 1; i < msg->size && i < 8; ++i) { + printf(", %d", msg->data[i]); + } + } + printf("]\n"); +} +#endif + #if FDCAN_MGR_ENABLED == 1 FDCAN_HandleTypeDef *fdcan_buses[MAX_CAN_BUSES]; @@ -106,14 +121,17 @@ int fdcan_init( can_rx_msg_handlers[assigned_id] = can_rx_msg_handler; GENQ_init( - &_rx_queues[assigned_id], sizeof(can_manager_message_t), CAN_MGR_MAX_QUEUE_ELEMENTS, &_rx_queues_data[assigned_id]); + &_rx_queues[assigned_id], sizeof(can_manager_message_t), CAN_MGR_MAX_QUEUE_ELEMENTS, + &_rx_queues_data[assigned_id]); GENQ_init( - &_tx_queues[assigned_id], sizeof(can_manager_message_t), CAN_MGR_MAX_QUEUE_ELEMENTS, &_tx_queues_data[assigned_id]); + &_tx_queues[assigned_id], sizeof(can_manager_message_t), CAN_MGR_MAX_QUEUE_ELEMENTS, + &_tx_queues_data[assigned_id]); fdcan_buses[assigned_id] = hcan; _n_active_can++; +#ifndef TEST if ((can_manager_hal_status_retval = HAL_FDCAN_ConfigFilter(hcan, filter)) != HAL_OK) { can_manager_hal_status_retval = CAN_MGR_FILTER_ERROR_CODE; return -1; @@ -126,9 +144,11 @@ int fdcan_init( can_manager_hal_status_retval = CAN_MGR_CAN_START_ERROR_CODE; return -1; } +#endif return assigned_id; } +#ifndef TEST void _fdcan_wait(FDCAN_HandleTypeDef *hcan) { uint32_t start_timestamp = HAL_GetTick(); while (HAL_FDCAN_GetTxFifoFreeLevel(hcan) == 0) { @@ -137,11 +157,13 @@ void _fdcan_wait(FDCAN_HandleTypeDef *hcan) { } } } +#endif int _fdcan_send(int can_id, can_manager_message_t *msg) { CAN_MGR_ID_CHECK(can_id); FDCAN_HandleTypeDef *hcan = fdcan_buses[can_id]; +#ifndef TEST uint32_t dlc_len = 0; switch (msg->size) { case 0: @@ -192,6 +214,10 @@ int _fdcan_send(int can_id, can_manager_message_t *msg) { if ((can_manager_hal_status_retval = HAL_FDCAN_AddMessageToTxFifoQ(hcan, &header, msg->data)) != HAL_OK) { return 0; } +#else + printf("SENDING fdcan: %s, ", hcan->name); + print_message(msg); +#endif return 1; } @@ -210,13 +236,16 @@ int can_init( can_rx_msg_handlers[assigned_id] = can_rx_msg_handler; GENQ_init( - &_rx_queues[assigned_id], CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t), sizeof(can_manager_message_t), &_rx_queues_data[assigned_id]); + &_rx_queues[assigned_id], CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t), + sizeof(can_manager_message_t), &_rx_queues_data[assigned_id]); GENQ_init( - &_tx_queues[assigned_id], CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t), sizeof(can_manager_message_t), &_tx_queues_data[assigned_id]); + &_tx_queues[assigned_id], CAN_MGR_MAX_QUEUE_ELEMENTS * sizeof(can_manager_message_t), + sizeof(can_manager_message_t), &_tx_queues_data[assigned_id]); can_buses[assigned_id] = hcan; _n_active_can++; +#ifndef TEST if ((can_manager_hal_status_retval = HAL_CAN_ConfigFilter(hcan, filter)) != HAL_OK) { can_manager_error_code = CAN_MGR_FILTER_ERROR_CODE; return -1; @@ -229,29 +258,38 @@ int can_init( can_manager_error_code = CAN_MGR_CAN_START_ERROR_CODE; return -1; } +#endif return assigned_id; } +#ifndef TEST void _can_wait(CAN_HandleTypeDef *hcan) { uint32_t start_timestamp = HAL_GetTick(); while (HAL_CAN_GetTxMailboxesFreeLevel(hcan) == 0) if (HAL_GetTick() > start_timestamp + 5) return; } +#endif int _can_send(int can_id, can_manager_message_t *msg) { CAN_MGR_ID_CHECK(can_id); - CAN_HandleTypeDef *hcan = can_buses[can_id]; + CAN_HandleTypeDef *hcan = can_buses[can_id]; +#ifndef TEST CAN_TxHeaderTypeDef header = { .StdId = msg->id, .IDE = CAN_ID_STD, .RTR = CAN_RTR_DATA, .DLC = msg->size, .TransmitGlobalTime = DISABLE}; #if CAN_WAIT_ENABLED == 1 _can_wait(hcan); #endif + uint32_t mlb = CAN_TX_MAILBOX0; if (HAL_CAN_AddTxMessage(hcan, &header, msg->data, &mlb) != HAL_OK) { return 0; } +#else + printf("SENDING can: %s, ", hcan->name); + print_message(msg); +#endif return 1; } #endif @@ -279,12 +317,18 @@ int consume_rx_queue(int can_id) { int flush_tx_queue(int can_id) { CAN_MGR_ID_CHECK(can_id); can_manager_message_t msg; + int sent_messages = 0; while (GENQ_pop(&_tx_queues[can_id], (uint8_t *)&msg)) { #ifdef FDCAN_MGR - return _fdcan_send(can_id, &msg); + if (!_fdcan_send(can_id, &msg)) { + return -1; + } #else - return _can_send(can_id, &msg); + if (!_can_send(can_id, &msg)) { + return -1; + } #endif + ++sent_messages; } - return 0; + return sent_messages; } diff --git a/can-manager/can_manager.h b/can-manager/can_manager.h index 3a7b6f6..f1ce1ec 100644 --- a/can-manager/can_manager.h +++ b/can-manager/can_manager.h @@ -3,9 +3,21 @@ #include "can_types.h" #include "generic_queue.h" -#include "main.h" #include "stdbool.h" +#ifndef TEST +#include "main.h" +#else +#include "stdio.h" +#include "string.h" +typedef struct { + char *name; +} CAN_HandleTypeDef; +typedef struct { +} CAN_FilterTypeDef; +void print_message(can_manager_message_t *msg); +#endif + #define CAN_MGR_MAX_CAN_BUSES 2 #define CAN_MGR_MAX_QUEUE_ELEMENTS 10 #define CAN_MGR_CAN_WAIT_ENABLED 1 diff --git a/can-manager/test/test.c b/can-manager/test/test.c new file mode 100644 index 0000000..2868e3e --- /dev/null +++ b/can-manager/test/test.c @@ -0,0 +1,43 @@ +#include "../can_manager.h" + +void consume_primary_rx_queue(can_manager_message_t *msg) { + printf("RECEIVED can: primary, "); + print_message(msg); +} + +void consume_secondary_rx_queue(can_manager_message_t *msg) { + printf("RECEIVED can: secondary, "); + print_message(msg); +} + +int main() { + CAN_HandleTypeDef primary_can_handle; + CAN_HandleTypeDef secondary_can_handle; + primary_can_handle.name = "primary"; + secondary_can_handle.name = "secondary"; + + int can_primary_id = can_init(&primary_can_handle, consume_primary_rx_queue, 0, 0); + int can_secondary_id = can_init(&secondary_can_handle, consume_secondary_rx_queue, 0, 0); + + can_manager_message_t msg1 = {.id = 1, .size = 8, .data = {4, 6, 2, 8, 2, 4, 8, 6}}; + can_manager_message_t msg2 = {.id = 2, .size = 3, .data = {9, 1, 0, 0, 3, 2, 5, 4}}; + can_manager_message_t msg3 = {.id = 3, .size = 7, .data = {8, 2, 3, 4, 5, 6, 7, 1}}; + can_manager_message_t msg4 = {.id = 4, .size = 5, .data = {3, 7, 3, 6, 9, 5, 8, 5}}; + + add_to_tx_queue(can_primary_id, &msg1); + add_to_rx_queue(can_secondary_id, &msg2); + add_to_tx_queue(can_primary_id, &msg3); + add_to_rx_queue(can_secondary_id, &msg4); + + while (consume_rx_queue(can_primary_id)) + ; + + printf("sent %d messages primary\n", flush_tx_queue(can_primary_id)); + + while (consume_rx_queue(can_secondary_id)) + ; + + printf("sent %d messages secondary\n", flush_tx_queue(can_secondary_id)); + + return 0; +} \ No newline at end of file