Init Commit: Moved bproto to seperate repo

This commit is contained in:
AlexanderHD27
2025-04-14 14:43:03 +02:00
commit 45bfc724fc
125 changed files with 10822 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.25)
project(bproto_test)
enable_testing()
include("../../backend_output/c/HCP.cmake")
# include CTest support
include(CTest)
set(CMAKE_BUILD_TYPE "Debug")
# Adding Unity test framework
add_library(unity STATIC Unity/src/unity.c)
target_include_directories(unity PUBLIC Unity/src)
# Normal test target
add_executable(test_runner
src/main.c
src/test_messages.c
)
target_link_libraries(test_runner PUBLIC
bproto
unity
gcov
)
target_include_directories(test_runner PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src
)
target_compile_options(test_runner PRIVATE -fprofile-arcs -ftest-coverage -fPIC -O0 --coverage)
target_compile_options(bproto PRIVATE -fprofile-arcs -ftest-coverage -fPIC -O0 --coverage)
target_link_options(test_runner PRIVATE -fprofile-arcs -ftest-coverage -fPIC --coverage)
target_link_options(bproto PRIVATE -fprofile-arcs -ftest-coverage -fPIC --coverage)
# Python Integration test build
add_executable(test_integration_python
src/test_python_integration.c
)
target_link_libraries(test_integration_python PUBLIC
bproto
gcov
)
target_include_directories(test_integration_python PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src
)

17
test/output/c/src/main.c Normal file
View File

@@ -0,0 +1,17 @@
#include "unity.h"
#include "suit.h"
void setUp(void) {};
void tearDown(void) {};
int main() {
UNITY_BEGIN();
RUN_TEST(test_encdec_generic_Error);
RUN_TEST(test_encdec_generic_MotionUpdate);
RUN_TEST(test_encdec_generic_Error_invalid_id);
RUN_TEST(test_encdec_generic_Error_invalid_size);
RUN_TEST(test_encdec_generic_Error_large_size);
RUN_TEST(test_encdec_generic_Error_invalid_crc);
return UNITY_END();
}

9
test/output/c/src/suit.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
// Enc/Dec with generic data buffer
void test_encdec_generic_MotionUpdate();
void test_encdec_generic_Error();
void test_encdec_generic_Error_invalid_id();
void test_encdec_generic_Error_invalid_size();
void test_encdec_generic_Error_large_size();
void test_encdec_generic_Error_invalid_crc();

View File

@@ -0,0 +1,224 @@
#include "suit.h"
#include <stdint.h>
#include "unity.h"
#include "HCP_message.h"
void test_encdec_generic_Error() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_MotionUpdate msg = {
.speed = {3, 6, 9},
.stearing = 0.5,
.name = "This is a test",
.heading = 'B',
.enable = {true, false, true, false},
.enables = {
.a = false, .b = true, .c = false
},
.dangerLvl = HCP_ENUM_INLINE_MOTION_UPDATE_DANGER_LVL_VALUE_MELTDOWN
};
size_t length = toBytes_Hcp_message_MotionUpdate(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 58);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x01);
Hcp_message_MotionUpdate rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
TEST_ASSERT_EQUAL(parsed_length, 58);
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_MOTION_UPDATE_ID);
TEST_ASSERT_EQUAL(msg.stearing, rx_msg.stearing);
TEST_ASSERT_EQUAL_STRING(msg.name, rx_msg.name);
TEST_ASSERT_EQUAL(msg.heading, rx_msg.heading);
TEST_ASSERT_EQUAL(msg.dangerLvl, rx_msg.dangerLvl);
TEST_ASSERT_EQUAL(msg.speed[0], rx_msg.speed[0]);
TEST_ASSERT_EQUAL(msg.speed[1], rx_msg.speed[1]);
TEST_ASSERT_EQUAL(msg.speed[2], rx_msg.speed[2]);
TEST_ASSERT_EQUAL(msg.enable[0], rx_msg.enable[0]);
TEST_ASSERT_EQUAL(msg.enable[1], rx_msg.enable[1]);
TEST_ASSERT_EQUAL(msg.enable[2], rx_msg.enable[2]);
TEST_ASSERT_EQUAL(msg.enable[3], rx_msg.enable[3]);
TEST_ASSERT_EQUAL(msg.enables.a, rx_msg.enables.a);
TEST_ASSERT_EQUAL(msg.enables.b, rx_msg.enables.b);
TEST_ASSERT_EQUAL(msg.enables.c, rx_msg.enables.c);
}
void test_encdec_generic_MotionUpdate() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_Error msg = {
.enables = {
.aa = true,
.asd = false,
.b = true,
.c = false,
.test = true
},
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
};
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 5);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
Hcp_message_Error rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
TEST_ASSERT_EQUAL(parsed_length, 5);
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_ERROR_ID);
TEST_ASSERT_EQUAL(rx_msg.enables.aa, msg.enables.aa);
TEST_ASSERT_EQUAL(rx_msg.enables.asd, msg.enables.asd);
TEST_ASSERT_EQUAL(rx_msg.enables.b, msg.enables.b);
TEST_ASSERT_EQUAL(rx_msg.enables.c, msg.enables.c);
TEST_ASSERT_EQUAL(rx_msg.enables.test, msg.enables.test);
TEST_ASSERT_EQUAL(rx_msg.recoveryStatus, msg.recoveryStatus);
}
void test_encdec_generic_Error_invalid_id() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_Error msg = {
.enables = {
.aa = true,
.asd = false,
.b = true,
.c = false,
.test = true
},
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
};
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 5);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
// Mofiy data
buffer_msg_data[0] = 0xf2;
Hcp_message_Error rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(parsed_length, 0);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_ID);
TEST_ASSERT_EQUAL(msg_id, 0xff);
}
void test_encdec_generic_Error_invalid_size() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_Error msg = {
.enables = {
.aa = true,
.asd = false,
.b = true,
.c = false,
.test = true
},
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
};
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 5);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
// Mofiy data
length = 2;
Hcp_message_Error rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(parsed_length, 0);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_SIZE);
TEST_ASSERT_EQUAL(msg_id, 0xff);
}
void test_encdec_generic_Error_large_size() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_Error msg = {
.enables = {
.aa = true,
.asd = false,
.b = true,
.c = false,
.test = true
},
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
};
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 5);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
length += 5;
Hcp_message_Error rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_OK);
TEST_ASSERT_EQUAL(parsed_length, 5);
TEST_ASSERT_EQUAL(msg_id, HCP_MESSAGE_ERROR_ID);
TEST_ASSERT_EQUAL(rx_msg.enables.aa, msg.enables.aa);
TEST_ASSERT_EQUAL(rx_msg.enables.asd, msg.enables.asd);
TEST_ASSERT_EQUAL(rx_msg.enables.b, msg.enables.b);
TEST_ASSERT_EQUAL(rx_msg.enables.c, msg.enables.c);
TEST_ASSERT_EQUAL(rx_msg.enables.test, msg.enables.test);
TEST_ASSERT_EQUAL(rx_msg.recoveryStatus, msg.recoveryStatus);
}
void test_encdec_generic_Error_invalid_crc() {
uint8_t buffer_msg_data[MAX_MESSAGE_DATA_SIZE];
Hcp_message_Error msg = {
.enables = {
.aa = true,
.asd = false,
.b = true,
.c = false,
.test = true
},
.recoveryStatus = HCP_ENUM_INLINE_ERROR_RECOVERY_STATUS_VALUE_NO
};
size_t length = toBytes_Hcp_message_Error(&msg, buffer_msg_data);
TEST_ASSERT_EQUAL(length, 5);
TEST_ASSERT_EQUAL(buffer_msg_data[0], 0x02);
// Mofiy data
buffer_msg_data[3] = 0xff;
Hcp_message_Error rx_msg;
size_t parsed_length = 0;
HCP_MSG_ID msg_id = 0xff;
HCP_PARSER_RESULT result = fromBytes(&rx_msg, buffer_msg_data, length, &parsed_length, &msg_id);
TEST_ASSERT_EQUAL(parsed_length, 0);
TEST_ASSERT_EQUAL(result, HCP_PARSER_RESULT_ERROR_INVALID_CRC);
TEST_ASSERT_EQUAL(msg_id, 0xff);
}

View File

@@ -0,0 +1,84 @@
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "HCP_message.h"
#include "HCP_enum.h"
int main() {
char hex_string[1024];
uint8_t data[sizeof(hex_string)/2];
size_t bytesRead = read(STDIN_FILENO, hex_string, sizeof(hex_string));
for(int i=0; i<(bytesRead/2); i+=1) {
sscanf(&hex_string[i*2], "%02hhx", &data[i]);
}
// Decode the package
char package_buffer[MAX_MESSAGE_STRUCT_SIZE];
size_t parsed_size = 0;
HCP_MSG_ID msg_id;
HCP_PARSER_RESULT result = fromBytes(
(void *)(package_buffer), data, bytesRead/2, &parsed_size, &msg_id
);
// Early exit if the decode failed
if(result != HCP_PARSER_RESULT_OK) {
if(result == HCP_PARSER_RESULT_ERROR_INVALID_ID) {
printf("ID\n");
return -1;
} else if(result == HCP_PARSER_RESULT_ERROR_INVALID_CRC) {
printf("CRC\n");
return -1;
} else if(result == HCP_PARSER_RESULT_ERROR_INVALID_SIZE) {
printf("SIZE\n");
return -1;
} else {
printf("???\n");
return -1;
}
}
// Modify and encode the package
char send_buffer[MAX_MESSAGE_DATA_SIZE];
size_t send_size;
switch (msg_id) {
case HCP_MESSAGE_MOTION_UPDATE_ID: {
Hcp_message_MotionUpdate *msg = (struct Hcp_message_MotionUpdate *)package_buffer;
msg->speed[0] += 1;
msg->speed[1] += 2;
msg->speed[2] += 3;
msg->stearing *= 0.5;
msg->name[0] = 'A';
msg->enables.a = !msg->enables.a;
msg->enables.b = !msg->enables.b;
msg->enables.c = !msg->enables.c;
msg->dangerLvl = HCP_ENUM_INLINE_MOTION_UPDATE_DANGER_LVL_VALUE_MELTDOWN;
send_size = toBytes_Hcp_message_MotionUpdate(msg, send_buffer);
break;
}
case HCP_MESSAGE_ERROR_ID: {
Hcp_message_Error *msg = (struct Hcp_message_Error *)package_buffer;
msg->enables.b = !msg->enables.b;
msg->enables.c = !msg->enables.c;
send_size = toBytes_Hcp_message_Error(msg, send_buffer);
break;
}
default:
break;
}
// Print the encoded package
printf("OK\n");
for(int i=0; i<send_size; i++) {
uint8_t byte = send_buffer[i];
printf("%02x", byte);
}
printf("\n");
return 0;
}