unleashed-firmware/applications/debug/unit_tests/expansion/expansion_test.c
MX c27494ac39
[FL-3669] Expansion module protocol (#3250)
* ApiSymbols: add furi_record_destroy
* FuriHal: cleanup serial API, add logging configuration in RTC
* FuriHal: hide private part in _i header. Toolbox: cleanup value index. SystemSettings: logging device and baudrate.
* FuriHal: RTC logging method documentation
* Synchronize API Symbols
* Furi: mark HEAP_PRINT_DEBUG as broken
* FuriHal: furi_hal_serial, add custom IRQ func
* Fix PR review issues
* Implement basic external module detection and echo
* Update api symbols for f18
* Minimally working implementation (can create directory via rpc)
* Make expansion protocol parser a header-only library
* Rename a function
* Improve thread syncronisation
* Implement multi-packet transmissions
* Improve test application
* Clean up expansion worker code
* Send heartbeat when host is ready
* Update API symbols
* Add draft documentation
* Expansion worker: proper timeout and error handling
* Expansion worker: correct TX, do not disable expansion callback
* Expansion protocol: pc side test script
* PC side expansion test: trying to change baudrate
* Working comms between 2 flippers
* Cleaner exit from expansion worker thread
* Better checks
* Add debug logs
* Remove unneeded delays
* Use USART as default expansion port
* Refactor furi_hal_serial_control, fix crash
* Improve furi_hal abstraction, wait for stable rx pin
* Remove rogue include
* Set proper exit reason on RPC error
* Remove rogue comment
* Remove RX stability check as potentially problematic
* Improve expansion_test application
* Remove rogue define
* Give up on TODO
* Implement expansion protocol checksum support
* Update ExpansionModules.md
* RPC: reverse input
* Assets: sync protobuf
* Fix typos
* FuriHal: UART add reception DMA (#3220)
* FuriHal: add DMA serial rx mode
* usb_uart_bridge: switch to working with DMA
* Sync api symbol versions
* FuriHal: update serial docs and api
* FuriHal: Selial added similar API for simple reception mode as with DMA
* FuriHal: Update API target H18
* API: ver API H7
* FuriHal: Serial error processing
* FuriHal: fix furi_hal_serial set baudrate
* Sync api symbols
* FuriHal: cleanup serial isr and various flag handling procedures
* FuriHal: cleanup and simplify serial API
* Debug: update UART Echo serial related flags
* FuriHal: update serial API symbols naming
* Make expansion_test compile
* Remove unneeded file
* Make PVS-studio happy
* Optimise stack usage
* Optimise heap usage, improve api signature
* Fix typo
* Clean up code
* Update expansion_protocol.h
* Fix unit tests
* Add doxygen comments to expansion.h
* Update/add doxygen comments
* Update ExpansionModules.md
* Github: new global code owner
* FuriHal: naming in serial control
* Expansion: check mutex acquire return result

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: SkorP <skorpionm@yandex.ru>
Co-authored-by: SG <who.just.the.doctor@gmail.com>
Co-authored-by: Skorpionm <85568270+Skorpionm@users.noreply.github.com>
2024-01-19 23:22:48 +03:00

157 lines
5.7 KiB
C

#include "../minunit.h"
#include <furi.h>
#include <expansion/expansion_protocol.h>
MU_TEST(test_expansion_encoded_size) {
ExpansionFrame frame = {};
frame.header.type = ExpansionFrameTypeHeartbeat;
mu_assert_int_eq(1, expansion_frame_get_encoded_size(&frame));
frame.header.type = ExpansionFrameTypeStatus;
mu_assert_int_eq(2, expansion_frame_get_encoded_size(&frame));
frame.header.type = ExpansionFrameTypeBaudRate;
mu_assert_int_eq(5, expansion_frame_get_encoded_size(&frame));
frame.header.type = ExpansionFrameTypeControl;
mu_assert_int_eq(2, expansion_frame_get_encoded_size(&frame));
frame.header.type = ExpansionFrameTypeData;
for(size_t i = 0; i <= EXPANSION_PROTOCOL_MAX_DATA_SIZE; ++i) {
frame.content.data.size = i;
mu_assert_int_eq(i + 2, expansion_frame_get_encoded_size(&frame));
}
}
MU_TEST(test_expansion_remaining_size) {
ExpansionFrame frame = {};
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
frame.header.type = ExpansionFrameTypeHeartbeat;
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 1));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 100));
frame.header.type = ExpansionFrameTypeStatus;
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 1));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 2));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 100));
frame.header.type = ExpansionFrameTypeBaudRate;
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
mu_assert_int_eq(4, expansion_frame_get_remaining_size(&frame, 1));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 5));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 100));
frame.header.type = ExpansionFrameTypeControl;
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 1));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 2));
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 100));
frame.header.type = ExpansionFrameTypeData;
frame.content.data.size = EXPANSION_PROTOCOL_MAX_DATA_SIZE;
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 0));
mu_assert_int_eq(1, expansion_frame_get_remaining_size(&frame, 1));
mu_assert_int_eq(
EXPANSION_PROTOCOL_MAX_DATA_SIZE, expansion_frame_get_remaining_size(&frame, 2));
for(size_t i = 0; i <= EXPANSION_PROTOCOL_MAX_DATA_SIZE; ++i) {
mu_assert_int_eq(
EXPANSION_PROTOCOL_MAX_DATA_SIZE - i,
expansion_frame_get_remaining_size(&frame, i + 2));
}
mu_assert_int_eq(0, expansion_frame_get_remaining_size(&frame, 100));
}
typedef struct {
void* data_out;
size_t size_available;
size_t size_sent;
} TestExpansionSendStream;
static size_t test_expansion_send_callback(const uint8_t* data, size_t data_size, void* context) {
TestExpansionSendStream* stream = context;
const size_t size_sent = MIN(data_size, stream->size_available);
memcpy(stream->data_out + stream->size_sent, data, size_sent);
stream->size_available -= size_sent;
stream->size_sent += size_sent;
return size_sent;
}
typedef struct {
const void* data_in;
size_t size_available;
size_t size_received;
} TestExpansionReceiveStream;
static size_t test_expansion_receive_callback(uint8_t* data, size_t data_size, void* context) {
TestExpansionReceiveStream* stream = context;
const size_t size_received = MIN(data_size, stream->size_available);
memcpy(data, stream->data_in + stream->size_received, size_received);
stream->size_available -= size_received;
stream->size_received += size_received;
return size_received;
}
MU_TEST(test_expansion_encode_decode_frame) {
const ExpansionFrame frame_in = {
.header.type = ExpansionFrameTypeData,
.content.data.size = 8,
.content.data.bytes = {0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xca, 0xfe},
};
uint8_t encoded_data[sizeof(ExpansionFrame) + sizeof(ExpansionFrameChecksum)];
memset(encoded_data, 0, sizeof(encoded_data));
TestExpansionSendStream send_stream = {
.data_out = &encoded_data,
.size_available = sizeof(encoded_data),
.size_sent = 0,
};
const size_t encoded_size = expansion_frame_get_encoded_size(&frame_in);
mu_assert_int_eq(
expansion_protocol_encode(&frame_in, test_expansion_send_callback, &send_stream),
ExpansionProtocolStatusOk);
mu_assert_int_eq(encoded_size + sizeof(ExpansionFrameChecksum), send_stream.size_sent);
mu_assert_int_eq(
expansion_protocol_get_checksum((const uint8_t*)&frame_in, encoded_size),
encoded_data[encoded_size]);
mu_assert_mem_eq(&frame_in, &encoded_data, encoded_size);
TestExpansionReceiveStream stream = {
.data_in = encoded_data,
.size_available = send_stream.size_sent,
.size_received = 0,
};
ExpansionFrame frame_out;
mu_assert_int_eq(
expansion_protocol_decode(&frame_out, test_expansion_receive_callback, &stream),
ExpansionProtocolStatusOk);
mu_assert_int_eq(encoded_size + sizeof(ExpansionFrameChecksum), stream.size_received);
mu_assert_mem_eq(&frame_in, &frame_out, encoded_size);
}
MU_TEST_SUITE(test_expansion_suite) {
MU_RUN_TEST(test_expansion_encoded_size);
MU_RUN_TEST(test_expansion_remaining_size);
MU_RUN_TEST(test_expansion_encode_decode_frame);
}
int run_minunit_test_expansion() {
MU_RUN_SUITE(test_expansion_suite);
return MU_EXIT_CODE;
}