Remake folder structure, add messaging library start
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
faa1685e18
commit
ed1509809e
@ -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()
|
||||
10
Messaging/CMakeLists.txt
Normal file
10
Messaging/CMakeLists.txt
Normal file
@ -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
|
||||
)
|
||||
72
Messaging/Inc/messaging_packet.hpp
Normal file
72
Messaging/Inc/messaging_packet.hpp
Normal file
@ -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 <cstring>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Messaging
|
||||
{
|
||||
|
||||
template<size_t N>
|
||||
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_ */
|
||||
35
Peripherals/Inc/peripherals_serial.hpp
Normal file
35
Peripherals/Inc/peripherals_serial.hpp
Normal file
@ -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 <cstdint>
|
||||
|
||||
namespace Peripherals
|
||||
{
|
||||
|
||||
template<typename HT, HT* Handle,
|
||||
void (*tx_function)(HT*, char*, std::uint32_t),
|
||||
void (*rx_function)(HT*, char*, std::uint32_t)>
|
||||
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_ */
|
||||
18
Tests/CMakeLists.txt
Normal file
18
Tests/CMakeLists.txt
Normal file
@ -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)
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user