diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f0c2b5..8d56ed3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,10 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") option(WITH_TESTS "Enable unit testing." OFF) +add_subdirectory(Utility) +add_subdirectory(Messaging) + if(WITH_TESTS) enable_testing() -endif() - -add_subdirectory(Utility) + add_subdirectory(Tests) +endif() \ No newline at end of file diff --git a/Messaging/CMakeLists.txt b/Messaging/CMakeLists.txt new file mode 100644 index 0000000..11088ad --- /dev/null +++ b/Messaging/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +add_library(messaging INTERFACE) + +add_library(skullc::messaging ALIAS messaging) + +target_include_directories(messaging + INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}/Inc +) diff --git a/Messaging/Inc/messaging_packet.hpp b/Messaging/Inc/messaging_packet.hpp new file mode 100644 index 0000000..8394977 --- /dev/null +++ b/Messaging/Inc/messaging_packet.hpp @@ -0,0 +1,72 @@ +/* + * messaging_packet.hpp + * + * Created on: Mar 27, 2021 + * Author: erki + */ + +#ifndef MESSAGING_INC_MESSAGING_PACKET_HPP_ +#define MESSAGING_INC_MESSAGING_PACKET_HPP_ + +#include +#include + +namespace Messaging +{ + +template +struct Packet +{ + static constexpr std::uint8_t preamble[] = { 'A', 'A' }; + std::uint8_t data[N]; + std::uint32_t data_length = 0; + + const std::uint32_t max_data_length = N; + const std::uint32_t preamble_length = sizeof(preamble); + const std::uint32_t data_length_length = sizeof(data_length); + + bool serialize(std::uint8_t* buffer, const std::uint32_t max_length) + { + const std::uint32_t required_size = preamble_length + data_length_length + data_length; + + if (max_length < required_size) + return false; + + std::memcpy(buffer, preamble, preamble_length); + buffer += preamble_length; + + std::memcpy(buffer, &data_length, data_length_length); + buffer += data_length_length; + + std::memcpy(buffer, data, data_length); + + return true; + } + + bool deserialize(std::uint8_t* buffer, const std::uint32_t length) + { + const std::uint32_t header_length = preamble_length + data_length_length; + + if (length < header_length) + return false; + + if (std::memcmp(buffer, preamble, preamble_length) != 0) + return false; + + std::memcpy(&data_length, buffer + preamble_length, preamble_length); + + const std::uint32_t final_length = header_length + data_length; + + if (length != final_length) + return false; + + std::memcpy(data, buffer + header_length, data_length); + + return true; + } +}; + +} + + +#endif /* MESSAGING_INC_MESSAGING_PACKET_HPP_ */ diff --git a/Peripherals/Inc/peripherals_serial.hpp b/Peripherals/Inc/peripherals_serial.hpp new file mode 100644 index 0000000..30115f4 --- /dev/null +++ b/Peripherals/Inc/peripherals_serial.hpp @@ -0,0 +1,35 @@ +/* + * peripherals_serial.hpp + * + * Created on: Mar 22, 2021 + * Author: erki + */ + +#ifndef SKULLC_PERIPHERALS_SERIAL_HPP_ +#define SKULLC_PERIPHERALS_SERIAL_HPP_ + +#include + +namespace Peripherals +{ + +template +struct SerialPeripheral +{ + void transmit(char* data, const std::uint32_t data_length) + { + tx_function(handle, data, data_length); + } + + void receive(char* output, const std::uint32_t data_length) + { + rx_function(handle, data, data_length); + } +}; + +} + + +#endif /* SKULLC_PERIPHERALS_SERIAL_HPP_ */ diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..102b9b6 --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +find_package(Catch2 REQUIRED) + +add_executable(tests + main.cpp + ringbuffer.cpp +) + +target_link_libraries(tests + PUBLIC + skullc::utility + Catch2::Catch2 +) + +include(CTest) +include(Catch) +catch_discover_tests(tests) \ No newline at end of file diff --git a/Utility/Tests/main.cpp b/Tests/main.cpp similarity index 100% rename from Utility/Tests/main.cpp rename to Tests/main.cpp diff --git a/Utility/Tests/ringbuffer.cpp b/Tests/ringbuffer.cpp similarity index 100% rename from Utility/Tests/ringbuffer.cpp rename to Tests/ringbuffer.cpp diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index e28bacd..2ae5354 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -10,24 +10,3 @@ target_include_directories(utility PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/Inc ) - -if(WITH_TESTS) - -find_package(Catch2 REQUIRED) - -add_executable(utility-tests - Tests/main.cpp - Tests/ringbuffer.cpp -) - -target_link_libraries(utility-tests - PUBLIC - skullc::utility - Catch2::Catch2 -) - -include(CTest) -include(Catch) -catch_discover_tests(utility-tests) - -endif()