From f04c7e1091d7be83ed3842f5ee2d5d807e3e499b Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:38:03 -0600 Subject: [PATCH 01/10] print and assert macros redefine --- Core/Command.cpp | 2 +- Core/CubeUtils.cpp | 4 ++-- Core/Inc/PQueue.hpp | 6 +++--- Core/Mutex.cpp | 2 +- Core/Queue.cpp | 4 ++-- Core/Timer.cpp | 16 ++++++++-------- CubeDefines.cpp | 2 +- CubeDefines.hpp | 10 +++++----- CubeTask.cpp | 8 ++++---- Drivers/Inc/UARTDriver.hpp | 22 ---------------------- Drivers/UARTDriver_README.md | 2 +- README.md | 6 +++--- 12 files changed, 31 insertions(+), 53 deletions(-) diff --git a/Core/Command.cpp b/Core/Command.cpp index 035a080..9b8f8ab 100644 --- a/Core/Command.cpp +++ b/Core/Command.cpp @@ -100,7 +100,7 @@ uint8_t* Command::AllocateData(uint16_t dataSize) statAllocationCounter += 1; //TODO: May want to print out whenever we have an imbalance in statAllocationCounter by more than ~5 or so. - CUBE_ASSERT(statAllocationCounter < MAX_NUMBER_OF_COMMAND_ALLOCATIONS); + SOAR_ASSERT(statAllocationCounter < MAX_NUMBER_OF_COMMAND_ALLOCATIONS); return this->data; } return nullptr; diff --git a/Core/CubeUtils.cpp b/Core/CubeUtils.cpp index 33b7d67..f7a929b 100644 --- a/Core/CubeUtils.cpp +++ b/Core/CubeUtils.cpp @@ -170,14 +170,14 @@ int32_t Utils::ExtractIntParameter(const char* msg, uint16_t identifierLen) { // Handle a command with an int parameter at the end if (static_cast(strlen(msg)) < identifierLen+1) { - CUBE_PRINT("Int parameter insufficient length\r\n"); + SOAR_PRINT("Int parameter insufficient length\r\n"); return ERRVAL; } // Extract the value and attempt conversion to integer const int32_t val = Utils::StringToLong(&msg[identifierLen]); if (val == ERRVAL) { - CUBE_PRINT("Int parameter invalid value\r\n"); + SOAR_PRINT("Int parameter invalid value\r\n"); } return val; diff --git a/Core/Inc/PQueue.hpp b/Core/Inc/PQueue.hpp index c751b1a..b44687e 100644 --- a/Core/Inc/PQueue.hpp +++ b/Core/Inc/PQueue.hpp @@ -107,7 +107,7 @@ class PQueue { bool operator<(const PriorityQueueItem& other) const { if(priority_ == other.priority_) { #ifdef PQUEUE_ENABLE_SEQN_CIRCULAR_CHECK - CUBE_ASSERT(pSeqN_ != nullptr, "PQueue null seqn pointer"); + SOAR_ASSERT(pSeqN_ != nullptr, "PQueue null seqn pointer"); seq_t seqN = *pSeqN_; if((order_ < seqN && other.order_ < seqN) || (order_ >= seqN && other.order_ >= seqN)) { @@ -267,10 +267,10 @@ bool PQueue::ReceiveWait(T& item) { template void PQueue::HandleConsistencyError() { // Print an error - CUBE_PRINT("ERROR: PQueue Data Consistency\r\n"); + SOAR_PRINT("ERROR: PQueue Data Consistency\r\n"); // Count the error, if it exceeds the max, we must reset the system - CUBE_ASSERT(++errCount_ > PQUEUE_ERROR_COUNT_MAX, + SOAR_ASSERT(++errCount_ > PQUEUE_ERROR_COUNT_MAX, "PQueue data consistency faults exceeded limits"); // Pop/Add items to the RT queue until it matches that of the priority queue diff --git a/Core/Mutex.cpp b/Core/Mutex.cpp index b016faf..b2ff5e9 100644 --- a/Core/Mutex.cpp +++ b/Core/Mutex.cpp @@ -18,7 +18,7 @@ Mutex::Mutex() { rtSemaphoreHandle = xSemaphoreCreateMutex(); - CUBE_ASSERT(rtSemaphoreHandle != NULL, "Semaphore creation failed."); + SOAR_ASSERT(rtSemaphoreHandle != NULL, "Semaphore creation failed."); } diff --git a/Core/Queue.cpp b/Core/Queue.cpp index 33c3d94..c216b07 100644 --- a/Core/Queue.cpp +++ b/Core/Queue.cpp @@ -59,7 +59,7 @@ bool Queue::SendToFront(Command& command) if (xQueueSendToFront(rtQueueHandle, &command, DEFAULT_QUEUE_SEND_WAIT_TICKS) == pdPASS) return true; - CUBE_PRINT("Could not send data to front of queue!\n"); + SOAR_PRINT("Could not send data to front of queue!\n"); command.Reset(); return false; @@ -79,7 +79,7 @@ bool Queue::Send(Command& command, bool reportFull) if (xQueueSend(rtQueueHandle, &command, DEFAULT_QUEUE_SEND_WAIT_TICKS) == pdPASS) return true; - if (reportFull) CUBE_PRINT("Could not send data to queue!\n"); + if (reportFull) SOAR_PRINT("Could not send data to queue!\n"); command.Reset(); diff --git a/Core/Timer.cpp b/Core/Timer.cpp index 25c1cb3..0a3df33 100644 --- a/Core/Timer.cpp +++ b/Core/Timer.cpp @@ -22,7 +22,7 @@ Timer::Timer() // The timer ID is specified as (void *)this to provide a unique ID for each timer object - however this is not necessary for polling timers. // The timer is created in the dormant state. rtTimerHandle = xTimerCreate("Timer", timerPeriod, pdFALSE, (void *)this, DefaultCallback); - CUBE_ASSERT(rtTimerHandle, "Error Occurred, Timer not created"); + SOAR_ASSERT(rtTimerHandle, "Error Occurred, Timer not created"); timerState = UNINITIALIZED; } @@ -36,7 +36,7 @@ Timer::Timer() Timer::Timer(void (*TimerDefaultCallback_t)( TimerHandle_t xTimer )) { rtTimerHandle = xTimerCreate("Timer", timerPeriod, pdFALSE, (void *)this, TimerDefaultCallback_t); - CUBE_ASSERT(rtTimerHandle, "Error Occurred, Timer not created"); + SOAR_ASSERT(rtTimerHandle, "Error Occurred, Timer not created"); timerState = UNINITIALIZED; } @@ -47,10 +47,10 @@ Timer::Timer(void (*TimerDefaultCallback_t)( TimerHandle_t xTimer )) Timer::~Timer() { if (xTimerDelete(rtTimerHandle, DEFAULT_TIMER_COMMAND_WAIT_PERIOD*2) == pdPASS) { - CUBE_PRINT("Timer has been deleted \n\n"); + SOAR_PRINT("Timer has been deleted \n\n"); } else { - CUBE_PRINT("WARNING, FAILED TO DELETE TIMER! \n\n"); + SOAR_PRINT("WARNING, FAILED TO DELETE TIMER! \n\n"); } } @@ -142,7 +142,7 @@ bool Timer::Stop() bool Timer::ResetTimer() { if (timerState == UNINITIALIZED) { - CUBE_PRINT("Cannot Restart as timer has not yet started!"); + SOAR_PRINT("Cannot Restart as timer has not yet started!"); return false; } if (ChangePeriodMs(timerPeriod) == true) { @@ -158,7 +158,7 @@ bool Timer::ResetTimer() bool Timer::ResetTimerAndStart() { if (timerState == UNINITIALIZED) { - CUBE_PRINT("Cannot Restart as timer has not yet started!"); + SOAR_PRINT("Cannot Restart as timer has not yet started!"); return false; } if (ChangePeriodMsAndStart(timerPeriod) == true) { @@ -223,12 +223,12 @@ void Timer::SetAutoReload(bool setReloadOn) if (setReloadOn == true){ vTimerSetReloadMode(rtTimerHandle, pdTRUE); //Testing purposes - CUBE_PRINT("Set to Auto Reload\n\n"); + SOAR_PRINT("Set to Auto Reload\n\n"); } if (setReloadOn == false){ vTimerSetReloadMode(rtTimerHandle, pdFALSE); //Testing purposes - CUBE_PRINT("Set to One Shot\n\n"); + SOAR_PRINT("Set to One Shot\n\n"); } } diff --git a/CubeDefines.cpp b/CubeDefines.cpp index 362dfee..43cdb65 100644 --- a/CubeDefines.cpp +++ b/CubeDefines.cpp @@ -53,7 +53,7 @@ void cube_print(const char* str, ...) else { // Print out that we could not acquire the VA list mutex - CUBE_ASSERT(false, "Could not acquire VA_LIST mutex"); + SOAR_ASSERT(false, "Could not acquire VA_LIST mutex"); } #endif } diff --git a/CubeDefines.hpp b/CubeDefines.hpp index e075e76..80b53a4 100644 --- a/CubeDefines.hpp +++ b/CubeDefines.hpp @@ -51,11 +51,11 @@ constexpr uint16_t ASSERT_TAKE_MAX_TIME_MS = 500; // Max time in ms to ta // Assert macro, use this for checking all possible program errors eg. malloc success etc. supports a custom message in printf format // This is our version of the stm32f4xx_hal_conf.h 'assert_param' macro with support for optional messages -// Example Usage: CUBE_ASSERT(ptr != 0, "Pointer on loop index %d is null!", index); -#define CUBE_ASSERT(expr, ...) ((expr) ? (void)0U : cube_assert_debug(false, (const char *)__FILE__, __LINE__, ##__VA_ARGS__)) +// Example Usage: SOAR_ASSERT(ptr != 0, "Pointer on loop index %d is null!", index); +#define SOAR_ASSERT(expr, ...) ((expr) ? (void)0U : cube_assert_debug(false, (const char *)__FILE__, __LINE__, ##__VA_ARGS__)) -// CUBE_PRINT macro, acts as an interface to the print function which sends a packet to the UART Task to print data -#define CUBE_PRINT(str, ...) (cube_print(str, ##__VA_ARGS__)) +// SOAR_PRINT macro, acts as an interface to the print function which sends a packet to the UART Task to print data +#define SOAR_PRINT(str, ...) (cube_print(str, ##__VA_ARGS__)) /** * @brief Malloc inline function, wraps malloc for multi-platform support, asserts successful allocation @@ -68,7 +68,7 @@ inline uint8_t* cube_malloc(uint32_t size) { #else uint8_t* ret = (uint8_t*)pvPortMalloc(size); #endif - CUBE_ASSERT(ret, "cube_malloc failed"); + SOAR_ASSERT(ret, "cube_malloc failed"); return ret; } diff --git a/CubeTask.cpp b/CubeTask.cpp index d7590f3..894c093 100644 --- a/CubeTask.cpp +++ b/CubeTask.cpp @@ -14,7 +14,7 @@ void CubeTask::InitTask() { // Make sure the task is not already initialized - CUBE_ASSERT(rtTaskHandle == nullptr, "Cannot initialize UART task twice"); + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize UART task twice"); // Start the task BaseType_t rtValue = @@ -26,7 +26,7 @@ void CubeTask::InitTask() (TaskHandle_t*)&rtTaskHandle); //Ensure creation succeded - CUBE_ASSERT(rtValue == pdPASS, "CUBETask::InitTask() - xTaskCreate() failed"); + SOAR_ASSERT(rtValue == pdPASS, "CUBETask::InitTask() - xTaskCreate() failed"); } /** @@ -65,13 +65,13 @@ void CubeTask::HandleCommand(Command& cm) #endif break; default: - CUBE_PRINT("CUBETask - Received Unsupported DATA_COMMAND {%d}\n", cm.GetTaskCommand()); + SOAR_PRINT("CUBETask - Received Unsupported DATA_COMMAND {%d}\n", cm.GetTaskCommand()); break; } break; } default: - CUBE_PRINT("CUBETask - Received Unsupported Command {%d}\n", cm.GetCommand()); + SOAR_PRINT("CUBETask - Received Unsupported Command {%d}\n", cm.GetCommand()); break; } diff --git a/Drivers/Inc/UARTDriver.hpp b/Drivers/Inc/UARTDriver.hpp index e278339..29174f9 100644 --- a/Drivers/Inc/UARTDriver.hpp +++ b/Drivers/Inc/UARTDriver.hpp @@ -13,28 +13,6 @@ #include "SystemDefines.hpp" #include "cmsis_os.h" -// Example code on how to declare UART Driver Instances in the HPP -#if EXAMPLE_CODE -/* UART Driver Instances ------------------------------------------------------------------*/ -class UARTDriver; - -namespace Driver { - extern UARTDriver uart1; - extern UARTDriver uart2; - extern UARTDriver uart3; - extern UARTDriver uart5; -} - -/* UART Driver Aliases ------------------------------------------------------------------*/ -namespace UART { - constexpr UARTDriver* Umbilical_RCU = &Driver::uart1; - constexpr UARTDriver* Radio = &Driver::uart2; - constexpr UARTDriver* Conduit_PBB = &Driver::uart3; - // UART 4 (GPS) uses HAL - constexpr UARTDriver* Debug = &Driver::uart5; -} -#endif - /* UART Receiver Base Class ------------------------------------------------------------------*/ /** * @brief Any classes that are expected to receive using a UART driver diff --git a/Drivers/UARTDriver_README.md b/Drivers/UARTDriver_README.md index 121091f..2c48dfd 100644 --- a/Drivers/UARTDriver_README.md +++ b/Drivers/UARTDriver_README.md @@ -50,7 +50,7 @@ something like cpp_USART5_IRQHandler(), for example: * RunInterface.cpp */ -#include "main_system.hpp" +#include "main_avionics.hpp" #include "UARTDriver.hpp" extern "C" { diff --git a/README.md b/README.md index 984ae31..398f741 100644 --- a/README.md +++ b/README.md @@ -90,13 +90,13 @@ Please ensure the following folders are not in the exclude from build option: - (none for this version) ### Codebase Setup -An example project utilizing Cube++ with basic CUBE_PRINT support, in addition to a Debug receive task for parsing input data on the debug line can be found at https://github.com/cjchanx/cubeplusplus-examples/tree/main/Basic_Debug +An example project utilizing Cube++ with basic SOAR_PRINT support, in addition to a Debug receive task for parsing input data on the debug line can be found at https://github.com/cjchanx/cubeplusplus-examples/tree/main/Basic_Debug - It is recommended to setup a new folder called `Components` or `Modules` in the root where all the code goes. - There are a few files that you should have in `Components`/`Modules`: - SystemDefines.hpp : An example can be found [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/SystemDefines.hpp) - - main_system.cpp : This can be named anything you want, but should contain the run_main() function that is the entry point for your codebase, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/main_system.cpp) - - main_system.hpp : Header file for main_system.cpp, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/main_system.hpp) + - main_avionics.cpp : This can be named anything you want, but should contain the run_main() function that is the entry point for your codebase, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/main_avionics.cpp) + - main_avionics.hpp : Header file for main_avionics.cpp, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/main_avionics.hpp) - SysCore/Inc/RunInterface.hpp : Header file for the run interface which allows C code to call into the C++ codebase without errors, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/Core/Inc/RunInterface.hpp) - SysCore/RunInterface.cpp : Code file for the run interface, example [here](https://github.com/cjchanx/cubeplusplus-examples/blob/main/Basic_Debug/Components/Core/RunInterface.cpp) - Setup Debug UART From 038a687ec48f894996084122b590058398fffc7d Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:30:36 -0600 Subject: [PATCH 02/10] removed header autosort --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4869f27..859e8b6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 hooks: - id: clang-format - args: [--style=Google] + args: ['--style={BasedOnStyle: Google, SortIncludes: false}'] files: \.(cpp|hpp)$ stages: [commit] From da7af04abb2fc07f4657a34544f32a7959811325 Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sat, 30 Nov 2024 09:46:59 -0700 Subject: [PATCH 03/10] Implement Publisher Subscribe classes --- .pre-commit-config.yaml | 16 ++-- Components/DataBroker/DataBroker.cpp | 30 +++++++ Components/DataBroker/Inc/DataBroker.hpp | 86 +++++++++++++++++++ .../DataBroker/Inc/DataBrokerMessageTypes.hpp | 38 ++++++++ Components/DataBroker/Inc/Publisher.hpp | 57 ++++++++++++ Components/DataBroker/Inc/Subscriber.hpp | 60 +++++++++++++ Components/DataBroker/Publisher.cpp | 83 ++++++++++++++++++ 7 files changed, 363 insertions(+), 7 deletions(-) create mode 100644 Components/DataBroker/DataBroker.cpp create mode 100644 Components/DataBroker/Inc/DataBroker.hpp create mode 100644 Components/DataBroker/Inc/DataBrokerMessageTypes.hpp create mode 100644 Components/DataBroker/Inc/Publisher.hpp create mode 100644 Components/DataBroker/Inc/Subscriber.hpp create mode 100644 Components/DataBroker/Publisher.cpp diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 859e8b6..c4767f4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,14 @@ repos: - - repo: https://github.com/pre-commit/mirrors-clang-format rev: 7d85583be209cb547946c82fbe51f4bc5dd1d017 hooks: - - id: clang-format - args: ['--style={BasedOnStyle: Google, SortIncludes: false}'] - files: \.(cpp|hpp)$ - stages: [commit] + - id: clang-format + args: + [ + "--style={BasedOnStyle: Google, SortIncludes: false, ColumnLimit: 120}", + ] + files: \.(cpp|hpp)$ + stages: [commit] - repo: https://github.com/pre-commit/mirrors-prettier rev: v2.5.1 @@ -18,8 +20,8 @@ repos: rev: v0.4.1 hooks: - id: ruff - types_or: [ python, pyi ] - args: [ --fix ] + types_or: [python, pyi] + args: [--fix] stages: [commit] - id: ruff-format stages: [commit] diff --git a/Components/DataBroker/DataBroker.cpp b/Components/DataBroker/DataBroker.cpp new file mode 100644 index 0000000..70e1750 --- /dev/null +++ b/Components/DataBroker/DataBroker.cpp @@ -0,0 +1,30 @@ +/** + ******************************************************************************** + * @file DataBroker.cpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "DataBroker.hpp" +#include "Publisher.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp new file mode 100644 index 0000000..4c9fdf5 --- /dev/null +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -0,0 +1,86 @@ +/** + ******************************************************************************** + * @file DataBroker.hpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef DATA_BROKER_HPP_ +#define DATA_BROKER_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "DataBroker.hpp" +#include "Publisher.hpp" +#include "SensorDataTypes.hpp" +#include "Command.hpp" +#include "DataBrokerMessageTypes.hpp" +#include "CubeDefines.hpp" +#include + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ +template +struct PublisherInformation { + Publisher publisher; + DataBrokerMessageTypes messageType; +}; + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class DataBroker { + public: + // Deleting the copy constructor to prevent copies + DataBroker(const DataBroker& obj) = delete; + + // Deleting assignment operator to prevent assignment operations + void operator=(DataBroker const&) = delete; + + // publish system message + template + static void PublishData(T* dataToPublish) { + PublisherInformation publisherInformation = getPublisherInformation(); + publisherInformation.publisher->Publish(dataToPublish, publisherInformation.messageType); + } + + private: + // matcher - match template type with publisher type + template + static constexpr bool match() { + return std::is_same_v; + } + + // get data publisher + template + PublisherInformation getPublisherInformation() { + if constexpr (match()) { + PublisherInformation publisherInfo{.publisher = &IMU_Data_publisher, + .messageType = DataBrokerMessageTypes::IMU_DATA}; + return publisherInfo; + } else if constexpr (match()) { + PublisherInformation publisherInfo{.publisher = &Thermocouple_Data_publisher, + .messageType = DataBrokerMessageTypes::THERMOCOUPLE_DATA}; + return publisherInfo; + } else { + SOAR_ASSERT(false, "This publisher type does not exist, you must create it"); + } + } + + // list of publishers + Publisher IMU_Data_publisher; + Publisher Thermocouple_Data_publisher; +}; +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* DATA_BROKER_HPP_ */ diff --git a/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp b/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp new file mode 100644 index 0000000..0ce5268 --- /dev/null +++ b/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp @@ -0,0 +1,38 @@ +/** + ******************************************************************************** + * @file DataBrokerMessageTypes.hpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef DATA_BROKER_MESSAGE_TYPES_HPP_ +#define DATA_BROKER_MESSAGE_TYPES_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ +enum class DataBrokerMessageTypes : uint8_t { + IMU_DATA = 0, + THERMOCOUPLE_DATA, +}; + +/************************************ + * CLASS DEFINITIONS + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* DATA_BROKER_MESSAGE_TYPES_HPP_ */ diff --git a/Components/DataBroker/Inc/Publisher.hpp b/Components/DataBroker/Inc/Publisher.hpp new file mode 100644 index 0000000..2e7e9d5 --- /dev/null +++ b/Components/DataBroker/Inc/Publisher.hpp @@ -0,0 +1,57 @@ +/** + ******************************************************************************** + * @file Publisher.hpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef PUBLISHER_HPP_ +#define PUBLISHER_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include +#include +#include "Task.hpp" +#include "Subscriber.hpp" +#include "DataBrokerMessageTypes.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +template +class Publisher { + public: + // Constructor + Publisher(); + + // subscribe + void Subscribe(Task* taskToSubscribe); + + // unsubscribe + void Unsubscribe(Task* taskToUnsubscribe); + + // publish + void Publish(T* dataToPublish, DataBrokerMessageTypes messageType); + + private: + // list of subscribers + Subscriber subscribersList[MaxSubscribers] = {}; +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* PUBLISHER_HPP_ */ diff --git a/Components/DataBroker/Inc/Subscriber.hpp b/Components/DataBroker/Inc/Subscriber.hpp new file mode 100644 index 0000000..870f166 --- /dev/null +++ b/Components/DataBroker/Inc/Subscriber.hpp @@ -0,0 +1,60 @@ +/** + ******************************************************************************** + * @file Subscriber.hpp + * @author shiva + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef SUBSCRIBER_HPP_ +#define SUBSCRIBER_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "Task.hpp" +#include "CubeDefines.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class Subscriber { + public: + void Init(Task* subscriberTaskHandle) { + if (taskHandle != nullptr || taskQueue != nullptr) { + SOAR_ASSERT(false, "You cannot overwrite a subscriber"); + return; + } + taskHandle = subscriberTaskHandle; + taskQueue = taskHandle->GetEventQueue(); + } + + void Delete() { + taskHandle = nullptr; + taskQueue = nullptr; + } + + inline const Task* getSubscriberTaskHandle() const { return taskHandle; } + + inline Queue* getSubscriberQueueHandle() const { return taskQueue; } + + private: + Subscriber() {} + Task* taskHandle = nullptr; + Queue* taskQueue = nullptr; +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* SUBSCRIBER_HPP_ */ diff --git a/Components/DataBroker/Publisher.cpp b/Components/DataBroker/Publisher.cpp new file mode 100644 index 0000000..5dc44de --- /dev/null +++ b/Components/DataBroker/Publisher.cpp @@ -0,0 +1,83 @@ +/** + ******************************************************************************** + * @file Publisher.cpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "Publisher.hpp" +#include "CubeDefines.hpp" +// #include "Command.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ + +/***************************************************************** + *****************************************************************/ +template +void Publisher::Subscribe(Task* taskToSubscribe) { + bool subscriberAdded = false; + for (Subscriber subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() == nullptr) { + subscriber.Init(taskToSubscribe); + subscriberAdded = true; + } + } + + SOAR_ASSERT(subscriberAdded, "Failed to add subscriber"); + return; +} + +/***************************************************************** + *****************************************************************/ +template +void Publisher::Unsubscribe(Task* taskToUnsubscribe) { + bool subscriberDeleted = false; + for (Subscriber subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() == taskToUnsubscribe) { + subscriber.Delete(); + subscriberDeleted = true; + } + } + + SOAR_ASSERT(subscriberDeleted, "Subscriber not Deleted"); +} + +/***************************************************************** + *****************************************************************/ +template +void Publisher::Publish(T* dataToPublish, DataBrokerMessageTypes messageType) { + // create command + Command brokerData(DATA_BROKER_COMMAND, messageType); + + // copy data to command + brokerData.CopyDataToCommand(dataToPublish, sizeof(dataToPublish)); + + // add command to task queue for all subscribers + bool messageSent = false; + for (Subscriber subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() != nullptr) { + subscriber.getSubscriberQueueHandle()->Send(brokerData); + } + } + + SOAR_ASSERT(messageSent, "The message was sent to no subscribers"); +} From 1d4dee27609f11039784789901b9368ac2cb607e Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Fri, 6 Dec 2024 16:50:30 -0700 Subject: [PATCH 04/10] Clang-format --- Components/DataBroker/DataBrokerParser.cpp | 29 ++++++++++++++++++++++ Components/DataBroker/Inc/DataBroker.hpp | 6 ++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 Components/DataBroker/DataBrokerParser.cpp diff --git a/Components/DataBroker/DataBrokerParser.cpp b/Components/DataBroker/DataBrokerParser.cpp new file mode 100644 index 0000000..0a7b494 --- /dev/null +++ b/Components/DataBroker/DataBrokerParser.cpp @@ -0,0 +1,29 @@ +/** + ******************************************************************************** + * @file DataBrokerParser.cpp + * @author shiva + * @date Nov 30, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "DataBroker.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp index 4c9fdf5..4d86284 100644 --- a/Components/DataBroker/Inc/DataBroker.hpp +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -13,7 +13,6 @@ /************************************ * INCLUDES ************************************/ -#include "DataBroker.hpp" #include "Publisher.hpp" #include "SensorDataTypes.hpp" #include "Command.hpp" @@ -52,6 +51,11 @@ class DataBroker { publisherInformation.publisher->Publish(dataToPublish, publisherInformation.messageType); } + // void ExtractDataFromCommand(Command dataCommand) { + // DataBrokerMessageTypes messageType = static_cast(dataCommand.GetTaskCommand()); + // + // } + private: // matcher - match template type with publisher type template From 2bdcab7fff6670431184001ed6c23624e9f0afa1 Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:03:58 -0700 Subject: [PATCH 05/10] Working Pub Sub --- Components/DataBroker/DataBroker.cpp | 30 ---- Components/DataBroker/DataBrokerParser.cpp | 29 ---- Components/DataBroker/Inc/DataBroker.hpp | 136 ++++++++++++++---- .../DataBroker/Inc/DataBrokerMessageTypes.hpp | 38 ----- Components/DataBroker/Inc/Publisher.hpp | 58 +++++++- Components/DataBroker/Inc/Subscriber.hpp | 3 +- Components/DataBroker/Publisher.cpp | 83 ----------- Components/PubSubTest/Inc/PubSubReceive.hpp | 58 ++++++++ Components/PubSubTest/Inc/PubSubSend.hpp | 57 ++++++++ Components/PubSubTest/PubSubReceive.cpp | 119 +++++++++++++++ Components/PubSubTest/PubSubSend.cpp | 94 ++++++++++++ 11 files changed, 488 insertions(+), 217 deletions(-) delete mode 100644 Components/DataBroker/DataBroker.cpp delete mode 100644 Components/DataBroker/DataBrokerParser.cpp delete mode 100644 Components/DataBroker/Inc/DataBrokerMessageTypes.hpp delete mode 100644 Components/DataBroker/Publisher.cpp create mode 100644 Components/PubSubTest/Inc/PubSubReceive.hpp create mode 100644 Components/PubSubTest/Inc/PubSubSend.hpp create mode 100644 Components/PubSubTest/PubSubReceive.cpp create mode 100644 Components/PubSubTest/PubSubSend.cpp diff --git a/Components/DataBroker/DataBroker.cpp b/Components/DataBroker/DataBroker.cpp deleted file mode 100644 index 70e1750..0000000 --- a/Components/DataBroker/DataBroker.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/** - ******************************************************************************** - * @file DataBroker.cpp - * @author shivam - * @date Nov 23, 2024 - * @brief - ******************************************************************************** - */ - -/************************************ - * INCLUDES - ************************************/ -#include "DataBroker.hpp" -#include "Publisher.hpp" - -/************************************ - * PRIVATE MACROS AND DEFINES - ************************************/ - -/************************************ - * VARIABLES - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -/************************************ - * FUNCTION DEFINITIONS - ************************************/ diff --git a/Components/DataBroker/DataBrokerParser.cpp b/Components/DataBroker/DataBrokerParser.cpp deleted file mode 100644 index 0a7b494..0000000 --- a/Components/DataBroker/DataBrokerParser.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/** - ******************************************************************************** - * @file DataBrokerParser.cpp - * @author shiva - * @date Nov 30, 2024 - * @brief - ******************************************************************************** - */ - -/************************************ - * INCLUDES - ************************************/ -#include "DataBroker.hpp" - -/************************************ - * PRIVATE MACROS AND DEFINES - ************************************/ - -/************************************ - * VARIABLES - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -/************************************ - * FUNCTION DEFINITIONS - ************************************/ diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp index 4d86284..ef723cb 100644 --- a/Components/DataBroker/Inc/DataBroker.hpp +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -17,8 +17,10 @@ #include "SensorDataTypes.hpp" #include "Command.hpp" #include "DataBrokerMessageTypes.hpp" -#include "CubeDefines.hpp" +#include "SystemDefines.hpp" +#include "Mutex.hpp" #include +#include /************************************ * MACROS AND DEFINES @@ -27,61 +29,135 @@ /************************************ * TYPEDEFS ************************************/ -template -struct PublisherInformation { - Publisher publisher; - DataBrokerMessageTypes messageType; -}; /************************************ * CLASS DEFINITIONS ************************************/ class DataBroker { public: - // Deleting the copy constructor to prevent copies - DataBroker(const DataBroker& obj) = delete; + /** + * @brief Publish data of a certain type + * NOTE: You must ensure that there is a publisher for that type + */ + template + static void Publish(T* dataToPublish) { + if (subscriberListLock.Lock(SUBSCRIBER_LIST_MUTEX_TIMEOUT)) { + Publisher* publisher = getPublisher(); + if (publisher != nullptr) { + publisher->Publish(dataToPublish); + } else { + SOAR_ASSERT("Data Publisher not found \n"); + } + subscriberListLock.Unlock(); + return; + } else { + SOAR_PRINT("Could Not Subscribe to Data Broker Publisher \n"); + } + return; + } - // Deleting assignment operator to prevent assignment operations - void operator=(DataBroker const&) = delete; + /** + * @brief Subscribe to a certain type of data in the system + * @param taskToSubscribe Task Handle of the task that will receive + * and handle the data. (i.e. -> Subscribe(this)) + */ + template + static void Subscribe(Task* taskToSubscribe) { + if (subscriberListLock.Lock(SUBSCRIBER_LIST_MUTEX_TIMEOUT)) { + Publisher* publisher = getPublisher(); + if (publisher != nullptr) { + publisher->Subscribe(taskToSubscribe); + } else { + SOAR_ASSERT("Data Publisher not found \n"); + } + subscriberListLock.Unlock(); + return; + } else { + SOAR_PRINT("Could Not Subscribe to Data Broker Publisher \n"); + } + return; + } + + /** + * @brief Unsubscribe to a certain type of data in the system + * @param taskToUnsubscribe Task Handle of the task that will stop + * receiving the data. (i.e. -> Unsubscribe(this)) + */ + template + static void Unsubscribe(Task* taskToUnsubscribe) { + if (subscriberListLock.Lock(SUBSCRIBER_LIST_MUTEX_TIMEOUT)) { + Publisher* publisher = getPublisher(); + if (publisher != nullptr) { + publisher->Unsubscribe(taskToUnsubscribe); + } else { + SOAR_ASSERT("Data Publisher not found \n"); + } + subscriberListLock.Unlock(); + return; + } else { + SOAR_PRINT("Could Not Unsubscribe to Data Broker Publisher \n"); + } + return; + } - // publish system message template - static void PublishData(T* dataToPublish) { - PublisherInformation publisherInformation = getPublisherInformation(); - publisherInformation.publisher->Publish(dataToPublish, publisherInformation.messageType); + static constexpr T ExtractData(const Command& cm) { + if (cm.GetCommand() != DATA_BROKER_COMMAND) { + SOAR_ASSERT("Not a Data Broker Command!\n"); + } + + // The data allocated by this command ptr will be freed when cm.Reset()] + // is called. So we do not have to free this memory here + T* dataPtr = reinterpret_cast(cm.GetDataPointer()); + + T data{}; + + std::memcpy(&data, dataPtr, sizeof(T)); + + return data; } - // void ExtractDataFromCommand(Command dataCommand) { - // DataBrokerMessageTypes messageType = static_cast(dataCommand.GetTaskCommand()); - // - // } + static DataBrokerMessageTypes getMessageType(const Command& cm) { + return static_cast(cm.GetTaskCommand()); + } private: + // Deleting the default constructor as this class is not + // instanceable + DataBroker() = delete; + + // Deleting the copy constructor to prevent copies + DataBroker(const DataBroker& obj) = delete; + + // Deleting assignment operator to prevent assignment operations + DataBroker& operator=(DataBroker const&) = delete; + + // Mutex to access the Subscriber List + inline static Mutex subscriberListLock{}; + // Mutex lock wait time + static constexpr uint16_t SUBSCRIBER_LIST_MUTEX_TIMEOUT = 1000; + // matcher - match template type with publisher type template - static constexpr bool match() { + static constexpr bool matchType() { return std::is_same_v; } // get data publisher template - PublisherInformation getPublisherInformation() { - if constexpr (match()) { - PublisherInformation publisherInfo{.publisher = &IMU_Data_publisher, - .messageType = DataBrokerMessageTypes::IMU_DATA}; - return publisherInfo; - } else if constexpr (match()) { - PublisherInformation publisherInfo{.publisher = &Thermocouple_Data_publisher, - .messageType = DataBrokerMessageTypes::THERMOCOUPLE_DATA}; - return publisherInfo; + static constexpr auto getPublisher(void) { + if constexpr (matchType()) { + return &IMU_Data_publisher; + } else if constexpr (matchType()) { + return &Thermocouple_Data_publisher; } else { SOAR_ASSERT(false, "This publisher type does not exist, you must create it"); } } // list of publishers - Publisher IMU_Data_publisher; - Publisher Thermocouple_Data_publisher; + inline static Publisher IMU_Data_publisher{DataBrokerMessageTypes::IMU_DATA}; + inline static Publisher Thermocouple_Data_publisher{DataBrokerMessageTypes::THERMOCOUPLE_DATA}; }; /************************************ * FUNCTION DECLARATIONS diff --git a/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp b/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp deleted file mode 100644 index 0ce5268..0000000 --- a/Components/DataBroker/Inc/DataBrokerMessageTypes.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/** - ******************************************************************************** - * @file DataBrokerMessageTypes.hpp - * @author shivam - * @date Nov 23, 2024 - * @brief - ******************************************************************************** - */ - -#ifndef DATA_BROKER_MESSAGE_TYPES_HPP_ -#define DATA_BROKER_MESSAGE_TYPES_HPP_ - -/************************************ - * INCLUDES - ************************************/ -#include - -/************************************ - * MACROS AND DEFINES - ************************************/ - -/************************************ - * TYPEDEFS - ************************************/ -enum class DataBrokerMessageTypes : uint8_t { - IMU_DATA = 0, - THERMOCOUPLE_DATA, -}; - -/************************************ - * CLASS DEFINITIONS - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -#endif /* DATA_BROKER_MESSAGE_TYPES_HPP_ */ diff --git a/Components/DataBroker/Inc/Publisher.hpp b/Components/DataBroker/Inc/Publisher.hpp index 2e7e9d5..fb4995d 100644 --- a/Components/DataBroker/Inc/Publisher.hpp +++ b/Components/DataBroker/Inc/Publisher.hpp @@ -13,11 +13,12 @@ /************************************ * INCLUDES ************************************/ +#include #include #include #include "Task.hpp" #include "Subscriber.hpp" -#include "DataBrokerMessageTypes.hpp" +#include "SystemDefines.hpp" /************************************ * MACROS AND DEFINES @@ -34,20 +35,67 @@ template class Publisher { public: // Constructor - Publisher(); + Publisher(DataBrokerMessageTypes messageType) { publisherMessageType = messageType; } // subscribe - void Subscribe(Task* taskToSubscribe); + bool Subscribe(Task* taskToSubscribe) { + // Check if subscriber already exists + for (Subscriber& subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() == taskToSubscribe) { + return true; + } + } + + // Add the subscriber + for (Subscriber& subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() == nullptr) { + subscriber.Init(taskToSubscribe); + return true; + } + } + + SOAR_ASSERT(true, "Failed to add subscriber\n"); + return false; + } // unsubscribe - void Unsubscribe(Task* taskToUnsubscribe); + bool Unsubscribe(Task* taskToUnsubscribe) { + for (Subscriber& subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() == taskToUnsubscribe) { + subscriber.Delete(); + return true; + } + } + + SOAR_ASSERT(true, "Subscriber not Deleted\n"); + return false; + } // publish - void Publish(T* dataToPublish, DataBrokerMessageTypes messageType); + void Publish(T* dataToPublish) { + for (const Subscriber& subscriber : subscribersList) { + if (subscriber.getSubscriberTaskHandle() != nullptr) { + // create command + uint16_t messageType = static_cast(publisherMessageType); + + Command brokerData(DATA_BROKER_COMMAND, messageType); + + uint8_t* messsageData = reinterpret_cast(dataToPublish); + + // copy data to command + brokerData.CopyDataToCommand(messsageData, sizeof(T)); + + subscriber.getSubscriberQueueHandle()->Send(brokerData); + } + } + } private: // list of subscribers Subscriber subscribersList[MaxSubscribers] = {}; + + // message type for system routing + DataBrokerMessageTypes publisherMessageType = DataBrokerMessageTypes::INVALID; }; /************************************ diff --git a/Components/DataBroker/Inc/Subscriber.hpp b/Components/DataBroker/Inc/Subscriber.hpp index 870f166..c3ec88b 100644 --- a/Components/DataBroker/Inc/Subscriber.hpp +++ b/Components/DataBroker/Inc/Subscriber.hpp @@ -14,7 +14,7 @@ * INCLUDES ************************************/ #include "Task.hpp" -#include "CubeDefines.hpp" +#include "SystemDefines.hpp" /************************************ * MACROS AND DEFINES @@ -48,7 +48,6 @@ class Subscriber { inline Queue* getSubscriberQueueHandle() const { return taskQueue; } private: - Subscriber() {} Task* taskHandle = nullptr; Queue* taskQueue = nullptr; }; diff --git a/Components/DataBroker/Publisher.cpp b/Components/DataBroker/Publisher.cpp deleted file mode 100644 index 5dc44de..0000000 --- a/Components/DataBroker/Publisher.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - ******************************************************************************** - * @file Publisher.cpp - * @author shivam - * @date Nov 23, 2024 - * @brief - ******************************************************************************** - */ - -/************************************ - * INCLUDES - ************************************/ -#include "Publisher.hpp" -#include "CubeDefines.hpp" -// #include "Command.hpp" - -/************************************ - * PRIVATE MACROS AND DEFINES - ************************************/ - -/************************************ - * VARIABLES - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -/************************************ - * FUNCTION DEFINITIONS - ************************************/ - -/***************************************************************** - *****************************************************************/ -template -void Publisher::Subscribe(Task* taskToSubscribe) { - bool subscriberAdded = false; - for (Subscriber subscriber : subscribersList) { - if (subscriber.getSubscriberTaskHandle() == nullptr) { - subscriber.Init(taskToSubscribe); - subscriberAdded = true; - } - } - - SOAR_ASSERT(subscriberAdded, "Failed to add subscriber"); - return; -} - -/***************************************************************** - *****************************************************************/ -template -void Publisher::Unsubscribe(Task* taskToUnsubscribe) { - bool subscriberDeleted = false; - for (Subscriber subscriber : subscribersList) { - if (subscriber.getSubscriberTaskHandle() == taskToUnsubscribe) { - subscriber.Delete(); - subscriberDeleted = true; - } - } - - SOAR_ASSERT(subscriberDeleted, "Subscriber not Deleted"); -} - -/***************************************************************** - *****************************************************************/ -template -void Publisher::Publish(T* dataToPublish, DataBrokerMessageTypes messageType) { - // create command - Command brokerData(DATA_BROKER_COMMAND, messageType); - - // copy data to command - brokerData.CopyDataToCommand(dataToPublish, sizeof(dataToPublish)); - - // add command to task queue for all subscribers - bool messageSent = false; - for (Subscriber subscriber : subscribersList) { - if (subscriber.getSubscriberTaskHandle() != nullptr) { - subscriber.getSubscriberQueueHandle()->Send(brokerData); - } - } - - SOAR_ASSERT(messageSent, "The message was sent to no subscribers"); -} diff --git a/Components/PubSubTest/Inc/PubSubReceive.hpp b/Components/PubSubTest/Inc/PubSubReceive.hpp new file mode 100644 index 0000000..df94e6c --- /dev/null +++ b/Components/PubSubTest/Inc/PubSubReceive.hpp @@ -0,0 +1,58 @@ +/** + ******************************************************************************** + * @file PubSubReceive.hpp + * @author shiva + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef PUBSUBRECEIEVE_HPP_ +#define PUBSUBRECEIEVE_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "Task.hpp" +#include "SystemDefines.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class PubSubReceive : public Task { + public: + static PubSubReceive& Inst() { + static PubSubReceive inst; + return inst; + } + + void InitTask(); + + protected: + static void RunTask(void* pvParams) { + PubSubReceive::Inst().Run(pvParams); + } // Static Task Interface, passes control to the instance Run(); + void Run(void* pvParams); // Main run code + void HandleCommand(Command& cm); + void HandleDataBrokerCommand(const Command& cm); + + private: + // Private Functions + PubSubReceive(); // Private constructor + PubSubReceive(const PubSubReceive&); // Prevent copy-construction + PubSubReceive& operator=(const PubSubReceive&); // Prevent assignment +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* PUBSUBRECEIEVE_HPP_ */ diff --git a/Components/PubSubTest/Inc/PubSubSend.hpp b/Components/PubSubTest/Inc/PubSubSend.hpp new file mode 100644 index 0000000..8874e65 --- /dev/null +++ b/Components/PubSubTest/Inc/PubSubSend.hpp @@ -0,0 +1,57 @@ +/** + ******************************************************************************** + * @file PubSubSend.hpp + * @author shiva + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef PUBSUBSEND_HPP_ +#define PUBSUBSEND_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include "Task.hpp" +#include "SystemDefines.hpp" + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/************************************ + * CLASS DEFINITIONS + ************************************/ +class PubSubSend : public Task { + public: + static PubSubSend& Inst() { + static PubSubSend inst; + return inst; + } + + void InitTask(); + + protected: + static void RunTask(void* pvParams) { + PubSubSend::Inst().Run(pvParams); + } // Static Task Interface, passes control to the instance Run(); + void Run(void* pvParams); // Main run code + void HandleCommand(Command& cm); + + private: + // Private Functions + PubSubSend(); // Private constructor + PubSubSend(const PubSubSend&); // Prevent copy-construction + PubSubSend& operator=(const PubSubSend&); // Prevent assignment +}; + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* PUBSUBSEND_HPP_ */ diff --git a/Components/PubSubTest/PubSubReceive.cpp b/Components/PubSubTest/PubSubReceive.cpp new file mode 100644 index 0000000..aed3929 --- /dev/null +++ b/Components/PubSubTest/PubSubReceive.cpp @@ -0,0 +1,119 @@ +/** + ******************************************************************************** + * @file PubSubReceive.cpp + * @author shiva + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "PubSubReceive.hpp" +#include "SystemDefines.hpp" +#include "SensorDataTypes.hpp" +#include "DataBroker.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ + +/** + * @brief Constructor for PubSubReceive + */ +PubSubReceive::PubSubReceive() : Task(PUBSUB_RECEIVE_TASK_QUEUE_DEPTH_OBJS) {} + +/** + * @brief Initialize the PubSubReceive + * Do not modify this function aside from adding the task name + */ +void PubSubReceive::InitTask() { + // Make sure the task is not already initialized + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); + + BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubReceive::RunTask, (const char*)"PubSubReceive", + (uint16_t)PUBSUB_RECEIVE_TASK_STACK_DEPTH_WORDS, (void*)this, + (UBaseType_t)PUBSUB_RECEIVE_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); + + SOAR_ASSERT(rtValue == pdPASS, "PubSubReceive::InitTask() - xTaskCreate() failed"); +} + +/** + * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. + * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used + */ +void PubSubReceive::Run(void* pvParams) { + // SOAR_PRINT("PUBSUB RECIEVE STARTED\n"); + DataBroker::Subscribe(this); + // DataBroker::Unsubscribe(this); + while (1) { + /* Process commands in blocking mode */ + Command cm; + bool res = qEvtQueue->ReceiveWait(cm); + if (res) { + HandleCommand(cm); + } + } +} + +/** + * @brief Handles a command + * @param cm Command reference to handle + */ +void PubSubReceive::HandleCommand(Command& cm) { + switch (cm.GetCommand()) { + case DATA_BROKER_COMMAND: + HandleDataBrokerCommand(cm); + break; + + default: + SOAR_PRINT("PubSubReceive - Received Unsupported Command {%d}\n", cm.GetCommand()); + break; + } + + // No matter what we happens, we must reset allocated data + cm.Reset(); +} + +/** + * @brief Handle all data broker commands + * @param cm The command object with the data + * Use cm.GetTaskCommand() to get the message type + * Message types must be cast back into DataBrokerMessageTypes enum + * Use cm.GetDataPointer() to get the pointer to the data + */ +void PubSubReceive::HandleDataBrokerCommand(const Command& cm) { + DataBrokerMessageTypes messageType = DataBroker::getMessageType(cm); + switch (messageType) { + case DataBrokerMessageTypes::IMU_DATA: { + IMUData imu_data = DataBroker::ExtractData(cm); + SOAR_PRINT("\n IMU DATA : \n"); + SOAR_PRINT(" X -> %d \n", imu_data.accelX); + SOAR_PRINT(" Y -> %d \n", imu_data.accelY); + SOAR_PRINT(" Z -> %d \n", imu_data.accelZ); + SOAR_PRINT("--DATA_END--\n\n"); + break; + } + + case DataBrokerMessageTypes::THERMOCOUPLE_DATA: + break; + + case DataBrokerMessageTypes::INVALID: + [[fallthrough]]; + default: + break; + } +} diff --git a/Components/PubSubTest/PubSubSend.cpp b/Components/PubSubTest/PubSubSend.cpp new file mode 100644 index 0000000..248139b --- /dev/null +++ b/Components/PubSubTest/PubSubSend.cpp @@ -0,0 +1,94 @@ +/** + ******************************************************************************** + * @file PubSubSend.cpp + * @author shiva + * @date Dec 14, 2024 + * @brief + ******************************************************************************** + */ + +/************************************ + * INCLUDES + ************************************/ +#include "PubSubSend.hpp" +#include "SystemDefines.hpp" +#include "SensorDataTypes.hpp" +#include "DataBroker.hpp" + +/************************************ + * PRIVATE MACROS AND DEFINES + ************************************/ + +/************************************ + * VARIABLES + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +/************************************ + * FUNCTION DEFINITIONS + ************************************/ + +/** + * @brief Constructor for PubSubSend + */ +PubSubSend::PubSubSend() : Task(PUBSUB_SEND_TASK_QUEUE_DEPTH_OBJS) {} + +/** + * @brief Initialize the PubSubSend + * Do not modify this function aside from adding the task name + */ +void PubSubSend::InitTask() { + // Make sure the task is not already initialized + SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); + + BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubSend::RunTask, (const char*)"PubSubSend", + (uint16_t)PUBSUB_SEND_TASK_STACK_DEPTH_WORDS, (void*)this, + (UBaseType_t)PUBSUB_SEND_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); + + SOAR_ASSERT(rtValue == pdPASS, "PubSubSend::InitTask() - xTaskCreate() failed"); +} + +/** + * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. + * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used + */ +void PubSubSend::Run(void* pvParams) { + // SOAR_PRINT("\nPUBSUB SEND STARTED\n"); + + while (1) { + Command cm; + if (qEvtQueue->Receive(cm, 5000)) { + HandleCommand(cm); + } else { + IMUData imuData = { + .accelX = 1, + .accelY = 2, + .accelZ = 3, + }; + DataBroker::Publish(&imuData); + + ThermocoupleData thermData = { + .temperature = -52, + }; + DataBroker::Publish(&thermData); + } + } +} + +/** + * @brief Handles a command + * @param cm Command reference to handle + */ +void PubSubSend::HandleCommand(Command& cm) { + switch (cm.GetCommand()) { + default: + SOAR_PRINT("PubSubSend - Received Unsupported Command {%d}\n", cm.GetCommand()); + break; + } + + // No matter what we happens, we must reset allocated data + cm.Reset(); +} From 3bc83f2da3fe81c313a4cc62b50fb51b9f133f6b Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Fri, 3 Jan 2025 22:07:16 -0700 Subject: [PATCH 06/10] Moved System Types to SOAROS --- .../SystemTypes/DataBrokerMessageTypes.hpp | 39 +++++++++++++++++ Components/SystemTypes/SensorDataTypes.hpp | 42 +++++++++++++++++++ .../SystemTypes/SystemCommunicationTypes.hpp | 22 ++++++++++ Components/SystemTypes/SystemConfigTypes.hpp | 18 ++++++++ 4 files changed, 121 insertions(+) create mode 100644 Components/SystemTypes/DataBrokerMessageTypes.hpp create mode 100644 Components/SystemTypes/SensorDataTypes.hpp create mode 100644 Components/SystemTypes/SystemCommunicationTypes.hpp create mode 100644 Components/SystemTypes/SystemConfigTypes.hpp diff --git a/Components/SystemTypes/DataBrokerMessageTypes.hpp b/Components/SystemTypes/DataBrokerMessageTypes.hpp new file mode 100644 index 0000000..d43ede0 --- /dev/null +++ b/Components/SystemTypes/DataBrokerMessageTypes.hpp @@ -0,0 +1,39 @@ +/** + ******************************************************************************** + * @file DataBrokerMessageTypes.hpp + * @author shivam + * @date Nov 23, 2024 + * @brief + ******************************************************************************** + */ + +#ifndef DATA_BROKER_MESSAGE_TYPES_HPP_ +#define DATA_BROKER_MESSAGE_TYPES_HPP_ + +/************************************ + * INCLUDES + ************************************/ +#include + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ +enum class DataBrokerMessageTypes : uint8_t { + INVALID = 0, + IMU_DATA, + THERMOCOUPLE_DATA, +}; + +/************************************ + * CLASS DEFINITIONS + ************************************/ + +/************************************ + * FUNCTION DECLARATIONS + ************************************/ + +#endif /* DATA_BROKER_MESSAGE_TYPES_HPP_ */ diff --git a/Components/SystemTypes/SensorDataTypes.hpp b/Components/SystemTypes/SensorDataTypes.hpp new file mode 100644 index 0000000..1fd8ad5 --- /dev/null +++ b/Components/SystemTypes/SensorDataTypes.hpp @@ -0,0 +1,42 @@ +/** + ******************************************************************************** + * @file SensorDataTypes.hpp + * @author Shivam Desai + * @date Nov 3, 2024 + * @brief General sensor data structure to pass around in the system or + *log it to flash memory + ******************************************************************************** + */ + +#ifndef SENSORDATATYPES_HPP_ +#define SENSORDATATYPES_HPP_ + +#include + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +/** + * @param accelX The acceleration in the X axis relative to the sensor + * @param accelY The acceleration in the Y axis relative to the sensor + * @param accelZ The acceleration in the Z axis relative to the sensor + */ +struct IMUData { + uint32_t accelX; + uint32_t accelY; + uint32_t accelZ; +}; + +/** + * @param Temperature. Can be any where from -2147483648 to 2147483647 + */ +struct ThermocoupleData { + int32_t temperature; +}; + +#endif /* SENSORDATATYPES_HPP_ */ diff --git a/Components/SystemTypes/SystemCommunicationTypes.hpp b/Components/SystemTypes/SystemCommunicationTypes.hpp new file mode 100644 index 0000000..e3d046b --- /dev/null +++ b/Components/SystemTypes/SystemCommunicationTypes.hpp @@ -0,0 +1,22 @@ +/** + ******************************************************************************** + * @file SystemCommunicationTypes.hpp + * @author Shivam Desai + * @date Nov 3, 2024 + * @brief Data structures to pass around other system information such as + * commands, events or state information + ******************************************************************************** + */ + +#ifndef SYSTEMCOMMUNICATIONTYPES_HPP_ +#define SYSTEMCOMMUNICATIONTYPES_HPP_ + +/************************************ + * MACROS AND DEFINES + ************************************/ + +/************************************ + * TYPEDEFS + ************************************/ + +#endif /* SYSTEMCOMMUNICATIONTYPES_HPP_ */ diff --git a/Components/SystemTypes/SystemConfigTypes.hpp b/Components/SystemTypes/SystemConfigTypes.hpp new file mode 100644 index 0000000..578f097 --- /dev/null +++ b/Components/SystemTypes/SystemConfigTypes.hpp @@ -0,0 +1,18 @@ +/** + ******************************************************************************** + * @file SystemConfigTypes.hpp + * @author Shivam Desai + * @date Nov 3, 2024 + * @brief Defines that allow the system to build with different + *functionality by changing the value of the define in this file + ******************************************************************************** + */ + +#ifndef SYSTEMCONFIGTYPES_HPP_ +#define SYSTEMCONFIGTYPES_HPP_ + +/************************************ + * MACROS AND DEFINES + ************************************/ + +#endif /* SYSTEMCONFIGTYPES_HPP_ */ From 3871370b5c596737a7c4bd9bea98ef1cf979f650 Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:02:40 -0700 Subject: [PATCH 07/10] Moved PubSub Test to Avionics template repo --- Components/PubSubTest/Inc/PubSubReceive.hpp | 58 ---------- Components/PubSubTest/Inc/PubSubSend.hpp | 57 ---------- Components/PubSubTest/PubSubReceive.cpp | 119 -------------------- Components/PubSubTest/PubSubSend.cpp | 94 ---------------- 4 files changed, 328 deletions(-) delete mode 100644 Components/PubSubTest/Inc/PubSubReceive.hpp delete mode 100644 Components/PubSubTest/Inc/PubSubSend.hpp delete mode 100644 Components/PubSubTest/PubSubReceive.cpp delete mode 100644 Components/PubSubTest/PubSubSend.cpp diff --git a/Components/PubSubTest/Inc/PubSubReceive.hpp b/Components/PubSubTest/Inc/PubSubReceive.hpp deleted file mode 100644 index df94e6c..0000000 --- a/Components/PubSubTest/Inc/PubSubReceive.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/** - ******************************************************************************** - * @file PubSubReceive.hpp - * @author shiva - * @date Dec 14, 2024 - * @brief - ******************************************************************************** - */ - -#ifndef PUBSUBRECEIEVE_HPP_ -#define PUBSUBRECEIEVE_HPP_ - -/************************************ - * INCLUDES - ************************************/ -#include "Task.hpp" -#include "SystemDefines.hpp" - -/************************************ - * MACROS AND DEFINES - ************************************/ - -/************************************ - * TYPEDEFS - ************************************/ - -/************************************ - * CLASS DEFINITIONS - ************************************/ -class PubSubReceive : public Task { - public: - static PubSubReceive& Inst() { - static PubSubReceive inst; - return inst; - } - - void InitTask(); - - protected: - static void RunTask(void* pvParams) { - PubSubReceive::Inst().Run(pvParams); - } // Static Task Interface, passes control to the instance Run(); - void Run(void* pvParams); // Main run code - void HandleCommand(Command& cm); - void HandleDataBrokerCommand(const Command& cm); - - private: - // Private Functions - PubSubReceive(); // Private constructor - PubSubReceive(const PubSubReceive&); // Prevent copy-construction - PubSubReceive& operator=(const PubSubReceive&); // Prevent assignment -}; - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -#endif /* PUBSUBRECEIEVE_HPP_ */ diff --git a/Components/PubSubTest/Inc/PubSubSend.hpp b/Components/PubSubTest/Inc/PubSubSend.hpp deleted file mode 100644 index 8874e65..0000000 --- a/Components/PubSubTest/Inc/PubSubSend.hpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - ******************************************************************************** - * @file PubSubSend.hpp - * @author shiva - * @date Dec 14, 2024 - * @brief - ******************************************************************************** - */ - -#ifndef PUBSUBSEND_HPP_ -#define PUBSUBSEND_HPP_ - -/************************************ - * INCLUDES - ************************************/ -#include "Task.hpp" -#include "SystemDefines.hpp" - -/************************************ - * MACROS AND DEFINES - ************************************/ - -/************************************ - * TYPEDEFS - ************************************/ - -/************************************ - * CLASS DEFINITIONS - ************************************/ -class PubSubSend : public Task { - public: - static PubSubSend& Inst() { - static PubSubSend inst; - return inst; - } - - void InitTask(); - - protected: - static void RunTask(void* pvParams) { - PubSubSend::Inst().Run(pvParams); - } // Static Task Interface, passes control to the instance Run(); - void Run(void* pvParams); // Main run code - void HandleCommand(Command& cm); - - private: - // Private Functions - PubSubSend(); // Private constructor - PubSubSend(const PubSubSend&); // Prevent copy-construction - PubSubSend& operator=(const PubSubSend&); // Prevent assignment -}; - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -#endif /* PUBSUBSEND_HPP_ */ diff --git a/Components/PubSubTest/PubSubReceive.cpp b/Components/PubSubTest/PubSubReceive.cpp deleted file mode 100644 index aed3929..0000000 --- a/Components/PubSubTest/PubSubReceive.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/** - ******************************************************************************** - * @file PubSubReceive.cpp - * @author shiva - * @date Dec 14, 2024 - * @brief - ******************************************************************************** - */ - -/************************************ - * INCLUDES - ************************************/ -#include "PubSubReceive.hpp" -#include "SystemDefines.hpp" -#include "SensorDataTypes.hpp" -#include "DataBroker.hpp" - -/************************************ - * PRIVATE MACROS AND DEFINES - ************************************/ - -/************************************ - * VARIABLES - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -/************************************ - * FUNCTION DEFINITIONS - ************************************/ - -/** - * @brief Constructor for PubSubReceive - */ -PubSubReceive::PubSubReceive() : Task(PUBSUB_RECEIVE_TASK_QUEUE_DEPTH_OBJS) {} - -/** - * @brief Initialize the PubSubReceive - * Do not modify this function aside from adding the task name - */ -void PubSubReceive::InitTask() { - // Make sure the task is not already initialized - SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); - - BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubReceive::RunTask, (const char*)"PubSubReceive", - (uint16_t)PUBSUB_RECEIVE_TASK_STACK_DEPTH_WORDS, (void*)this, - (UBaseType_t)PUBSUB_RECEIVE_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); - - SOAR_ASSERT(rtValue == pdPASS, "PubSubReceive::InitTask() - xTaskCreate() failed"); -} - -/** - * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. - * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used - */ -void PubSubReceive::Run(void* pvParams) { - // SOAR_PRINT("PUBSUB RECIEVE STARTED\n"); - DataBroker::Subscribe(this); - // DataBroker::Unsubscribe(this); - while (1) { - /* Process commands in blocking mode */ - Command cm; - bool res = qEvtQueue->ReceiveWait(cm); - if (res) { - HandleCommand(cm); - } - } -} - -/** - * @brief Handles a command - * @param cm Command reference to handle - */ -void PubSubReceive::HandleCommand(Command& cm) { - switch (cm.GetCommand()) { - case DATA_BROKER_COMMAND: - HandleDataBrokerCommand(cm); - break; - - default: - SOAR_PRINT("PubSubReceive - Received Unsupported Command {%d}\n", cm.GetCommand()); - break; - } - - // No matter what we happens, we must reset allocated data - cm.Reset(); -} - -/** - * @brief Handle all data broker commands - * @param cm The command object with the data - * Use cm.GetTaskCommand() to get the message type - * Message types must be cast back into DataBrokerMessageTypes enum - * Use cm.GetDataPointer() to get the pointer to the data - */ -void PubSubReceive::HandleDataBrokerCommand(const Command& cm) { - DataBrokerMessageTypes messageType = DataBroker::getMessageType(cm); - switch (messageType) { - case DataBrokerMessageTypes::IMU_DATA: { - IMUData imu_data = DataBroker::ExtractData(cm); - SOAR_PRINT("\n IMU DATA : \n"); - SOAR_PRINT(" X -> %d \n", imu_data.accelX); - SOAR_PRINT(" Y -> %d \n", imu_data.accelY); - SOAR_PRINT(" Z -> %d \n", imu_data.accelZ); - SOAR_PRINT("--DATA_END--\n\n"); - break; - } - - case DataBrokerMessageTypes::THERMOCOUPLE_DATA: - break; - - case DataBrokerMessageTypes::INVALID: - [[fallthrough]]; - default: - break; - } -} diff --git a/Components/PubSubTest/PubSubSend.cpp b/Components/PubSubTest/PubSubSend.cpp deleted file mode 100644 index 248139b..0000000 --- a/Components/PubSubTest/PubSubSend.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/** - ******************************************************************************** - * @file PubSubSend.cpp - * @author shiva - * @date Dec 14, 2024 - * @brief - ******************************************************************************** - */ - -/************************************ - * INCLUDES - ************************************/ -#include "PubSubSend.hpp" -#include "SystemDefines.hpp" -#include "SensorDataTypes.hpp" -#include "DataBroker.hpp" - -/************************************ - * PRIVATE MACROS AND DEFINES - ************************************/ - -/************************************ - * VARIABLES - ************************************/ - -/************************************ - * FUNCTION DECLARATIONS - ************************************/ - -/************************************ - * FUNCTION DEFINITIONS - ************************************/ - -/** - * @brief Constructor for PubSubSend - */ -PubSubSend::PubSubSend() : Task(PUBSUB_SEND_TASK_QUEUE_DEPTH_OBJS) {} - -/** - * @brief Initialize the PubSubSend - * Do not modify this function aside from adding the task name - */ -void PubSubSend::InitTask() { - // Make sure the task is not already initialized - SOAR_ASSERT(rtTaskHandle == nullptr, "Cannot initialize watchdog task twice"); - - BaseType_t rtValue = xTaskCreate((TaskFunction_t)PubSubSend::RunTask, (const char*)"PubSubSend", - (uint16_t)PUBSUB_SEND_TASK_STACK_DEPTH_WORDS, (void*)this, - (UBaseType_t)PUBSUB_SEND_TASK_RTOS_PRIORITY, (TaskHandle_t*)&rtTaskHandle); - - SOAR_ASSERT(rtValue == pdPASS, "PubSubSend::InitTask() - xTaskCreate() failed"); -} - -/** - * @brief Instance Run loop for the Task, runs on scheduler start as long as the task is initialized. - * @param pvParams RTOS Passed void parameters, contains a pointer to the object instance, should not be used - */ -void PubSubSend::Run(void* pvParams) { - // SOAR_PRINT("\nPUBSUB SEND STARTED\n"); - - while (1) { - Command cm; - if (qEvtQueue->Receive(cm, 5000)) { - HandleCommand(cm); - } else { - IMUData imuData = { - .accelX = 1, - .accelY = 2, - .accelZ = 3, - }; - DataBroker::Publish(&imuData); - - ThermocoupleData thermData = { - .temperature = -52, - }; - DataBroker::Publish(&thermData); - } - } -} - -/** - * @brief Handles a command - * @param cm Command reference to handle - */ -void PubSubSend::HandleCommand(Command& cm) { - switch (cm.GetCommand()) { - default: - SOAR_PRINT("PubSubSend - Received Unsupported Command {%d}\n", cm.GetCommand()); - break; - } - - // No matter what we happens, we must reset allocated data - cm.Reset(); -} From c0a96fdbc27bdba18b2ac79a02a7587cfe1b591b Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:44:38 -0700 Subject: [PATCH 08/10] Updated the Data Broker Documentation --- Components/DataBroker/Inc/DataBroker.hpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp index ef723cb..54daa72 100644 --- a/Components/DataBroker/Inc/DataBroker.hpp +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -100,6 +100,11 @@ class DataBroker { return; } + /** + * @brief This API can be used to offload the data from the databroker message + * into a new object in the receiving task + * @param cm the Command object that contains the databroker message + */ template static constexpr T ExtractData(const Command& cm) { if (cm.GetCommand() != DATA_BROKER_COMMAND) { @@ -117,6 +122,12 @@ class DataBroker { return data; } + /** + * @brief This API can be use to get the type of data broker message contained + * in the message. + * All the message types can be found in DataBrokerMessageTypes.hpp + * @param cm the Command object that contains the databroker message + */ static DataBrokerMessageTypes getMessageType(const Command& cm) { return static_cast(cm.GetTaskCommand()); } @@ -137,13 +148,17 @@ class DataBroker { // Mutex lock wait time static constexpr uint16_t SUBSCRIBER_LIST_MUTEX_TIMEOUT = 1000; - // matcher - match template type with publisher type + /** + * @brief Checks if the 2 template types are the same + */ template static constexpr bool matchType() { return std::is_same_v; } - // get data publisher + /** + * @brief Returns the correct Publisher object for a template type + */ template static constexpr auto getPublisher(void) { if constexpr (matchType()) { @@ -155,7 +170,7 @@ class DataBroker { } } - // list of publishers + // List of Publishers inline static Publisher IMU_Data_publisher{DataBrokerMessageTypes::IMU_DATA}; inline static Publisher Thermocouple_Data_publisher{DataBrokerMessageTypes::THERMOCOUPLE_DATA}; }; From 2796fa848c102683b4b35c48c7ba8e3ea3872d6d Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sat, 4 Jan 2025 13:34:08 -0700 Subject: [PATCH 09/10] Check if types are compatible when extracting data --- Components/DataBroker/Inc/DataBroker.hpp | 17 ++++++++++- Components/DataBroker/Inc/Publisher.hpp | 4 ++- Components/DataBroker/Inc/Subscriber.hpp | 2 +- .../SystemTypes/DataBrokerMessageTypes.hpp | 28 ++++++++++++++++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp index 54daa72..1e57b01 100644 --- a/Components/DataBroker/Inc/DataBroker.hpp +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -1,7 +1,7 @@ /** ******************************************************************************** * @file DataBroker.hpp - * @author shivam + * @author Shivam Desai * @date Nov 23, 2024 * @brief ******************************************************************************** @@ -21,6 +21,8 @@ #include "Mutex.hpp" #include #include +#include +#include /************************************ * MACROS AND DEFINES @@ -111,6 +113,19 @@ class DataBroker { SOAR_ASSERT("Not a Data Broker Command!\n"); } + Publisher* publisher = getPublisher(); + DataBrokerMessageTypes messageType = DataBroker::getMessageType(cm); + + if (messageType != publisher->GetPublisherMessageType()) { + const std::string errorMessage = "Trying to unpack the wrong type of message. You are trying to use " + + DataBrokerMessageType::ToString(publisher->GetPublisherMessageType()) + + " instead of " + DataBrokerMessageType::ToString(messageType) + "\n"; + + const char* messageCStr = errorMessage.c_str(); + + SOAR_ASSERT(false, messageCStr); + } + // The data allocated by this command ptr will be freed when cm.Reset()] // is called. So we do not have to free this memory here T* dataPtr = reinterpret_cast(cm.GetDataPointer()); diff --git a/Components/DataBroker/Inc/Publisher.hpp b/Components/DataBroker/Inc/Publisher.hpp index fb4995d..c664c44 100644 --- a/Components/DataBroker/Inc/Publisher.hpp +++ b/Components/DataBroker/Inc/Publisher.hpp @@ -1,7 +1,7 @@ /** ******************************************************************************** * @file Publisher.hpp - * @author shivam + * @author Shivam Desai * @date Nov 23, 2024 * @brief ******************************************************************************** @@ -90,6 +90,8 @@ class Publisher { } } + DataBrokerMessageTypes GetPublisherMessageType() { return publisherMessageType; } + private: // list of subscribers Subscriber subscribersList[MaxSubscribers] = {}; diff --git a/Components/DataBroker/Inc/Subscriber.hpp b/Components/DataBroker/Inc/Subscriber.hpp index c3ec88b..3352b80 100644 --- a/Components/DataBroker/Inc/Subscriber.hpp +++ b/Components/DataBroker/Inc/Subscriber.hpp @@ -1,7 +1,7 @@ /** ******************************************************************************** * @file Subscriber.hpp - * @author shiva + * @author Shivam Desai * @date Nov 23, 2024 * @brief ******************************************************************************** diff --git a/Components/SystemTypes/DataBrokerMessageTypes.hpp b/Components/SystemTypes/DataBrokerMessageTypes.hpp index d43ede0..fa80330 100644 --- a/Components/SystemTypes/DataBrokerMessageTypes.hpp +++ b/Components/SystemTypes/DataBrokerMessageTypes.hpp @@ -1,7 +1,7 @@ /** ******************************************************************************** * @file DataBrokerMessageTypes.hpp - * @author shivam + * @author Shivam Desai * @date Nov 23, 2024 * @brief ******************************************************************************** @@ -14,6 +14,7 @@ * INCLUDES ************************************/ #include +#include /************************************ * MACROS AND DEFINES @@ -28,6 +29,7 @@ enum class DataBrokerMessageTypes : uint8_t { THERMOCOUPLE_DATA, }; +namespace DataBrokerMessageType { /************************************ * CLASS DEFINITIONS ************************************/ @@ -35,5 +37,29 @@ enum class DataBrokerMessageTypes : uint8_t { /************************************ * FUNCTION DECLARATIONS ************************************/ +std::string ToString(DataBrokerMessageTypes messageType); + +inline std::string ToString(DataBrokerMessageTypes messageType) { + switch (messageType) { + case DataBrokerMessageTypes::IMU_DATA: { + std::string type{"IMU_DATA"}; + return type; + } + + case DataBrokerMessageTypes::THERMOCOUPLE_DATA: { + std::string type{"THERMOCOUPLE_DATA"}; + return type; + } + + case DataBrokerMessageTypes::INVALID: + [[fallthrough]]; + default: { + std::string type{"INVALID"}; + return type; + } + } +} + +} // namespace DataBrokerMessageType #endif /* DATA_BROKER_MESSAGE_TYPES_HPP_ */ From 35484847eb9796223014863b2044d413c7719a77 Mon Sep 17 00:00:00 2001 From: shivamdesai04 <101665374+shivamdesai04@users.noreply.github.com> Date: Sat, 4 Jan 2025 13:52:52 -0700 Subject: [PATCH 10/10] print error message before assert when extracting data if there is an error --- Components/DataBroker/Inc/DataBroker.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Components/DataBroker/Inc/DataBroker.hpp b/Components/DataBroker/Inc/DataBroker.hpp index 1e57b01..dd436c0 100644 --- a/Components/DataBroker/Inc/DataBroker.hpp +++ b/Components/DataBroker/Inc/DataBroker.hpp @@ -119,11 +119,11 @@ class DataBroker { if (messageType != publisher->GetPublisherMessageType()) { const std::string errorMessage = "Trying to unpack the wrong type of message. You are trying to use " + DataBrokerMessageType::ToString(publisher->GetPublisherMessageType()) + - " instead of " + DataBrokerMessageType::ToString(messageType) + "\n"; + " instead of " + DataBrokerMessageType::ToString(messageType) + "\n\n"; const char* messageCStr = errorMessage.c_str(); - - SOAR_ASSERT(false, messageCStr); + SOAR_PRINT(messageCStr); + SOAR_ASSERT(false, ""); } // The data allocated by this command ptr will be freed when cm.Reset()]