extern_interface/tests/test_host_com.cpp

124 lines
3.9 KiB
C++

#include <thread>
#include <pty.h>
#include <fcntl.h>
#include <unistd.h>
#include "comframe.h"
#include "telemetry.h"
#include "dataqueue.hpp"
#include "exint/detail.h"
#include "c_testcase.h"
static int g_telemetry_request_count = 0;
static DataQueue<std::tuple<uint32_t, size_t, void*>> g_data_queue;
static int host_com_master_id;
static int telemetry_com_master_id;
void exint_data_callback(uint32_t type, size_t size, void* data) {
if (type == ET_TYPE_TELEMETRY_REQUEST) {
g_telemetry_request_count++;
memset(data, 0, size);
((TelemetryRequestData*)data)->app_ver_high = 1;
((TelemetryRequestData*)data)->app_ver_low = 2;
} else {
g_data_queue.Push(std::make_tuple(type, size, data));
}
}
SETUP {
int host_com_slave_id, telemetry_com_slave_id;
if (openpty(&host_com_master_id, &host_com_slave_id, NULL, NULL, NULL) < 0) {
return 1;
}
if (openpty(&telemetry_com_master_id, &telemetry_com_slave_id, NULL, NULL, NULL) < 0) {
return 1;
}
init_tty(host_com_master_id, 115200);
init_tty(telemetry_com_master_id, 115200);
// init_tty(host_com_slave_id, 115200);
// init_tty(telemetry_com_slave_id, 115200);
exint_init_from_tty(host_com_slave_id, telemetry_com_slave_id, exint_data_callback);
g_telemetry_request_count = 0;
return 0;
}
TEARDOWN {
exint_finialize();
close(host_com_master_id);
close(telemetry_com_master_id);
g_data_queue.Clear();
return 0;
}
TEST_CASE(test_init) {
assert(host_com_master_id);
assert(telemetry_com_master_id);
END_TEST;
}
TEST_CASE(test_host_telemetry) {
auto request_msg = NewTelemetryRequestMsg(COM_FRAME_ADDRESS_VOIX, true);
ComFrame_Send(telemetry_com_master_id, request_msg, true);
ComFrame_Del(request_msg);
bool timeout_flag = true;
std::thread([&]() {
std::this_thread::sleep_for(std::chrono::seconds(1));
timeout_flag = false;
}).detach();
size_t offset = 0;
size_t cached_size = 0;
auto telemetry_reply = ComFrame_ReceiveEx(telemetry_com_master_id, NULL, 100, &offset, &cached_size, &timeout_flag, true);
assert(telemetry_reply);
assert_eq(g_telemetry_request_count, 1);
TelemetryData* telemetry_data = (TelemetryData*)ComFrame_PAYLOAD(telemetry_reply);
assert_eq(ComFrame_TYPE(telemetry_reply), COM_FRAME_TYPE_TELEMETRY_ANSWER);
assert_eq(ComFrame_PAYLOAD_LENGTH(telemetry_reply), sizeof(TelemetryData));
assert_eq(telemetry_data->application_version_high, 1);
END_TEST;
}
TEST_CASE(test_callback) {
uint8_t command[] = { 0x00, 0x01, 0x02, 0x03, 0xAA, 0xAA};
auto command_msg = ComFrame_New(COM_FRAME_ADDRESS_VOIX, COM_FRAME_TYPE_COMMAND, command, sizeof(command), true);
assert(!ComFrame_Send(host_com_master_id, command_msg, true));
uint32_t tp;
size_t len;
void* pack;
std::tie(tp, len, pack) = g_data_queue.Pop(std::chrono::milliseconds(1000));
assert_eq(tp, ET_TYPE_COMMAND);
assert_eq(len, sizeof(command));
assert_mem_eq(command, pack, len);
END_TEST;
}
TEST_CASE(test_send_command) {
ComFrame* command_frame = NULL;
bool timeout_flag = true;
std::thread([&]() {
size_t offset = 0;
size_t cached_size = 0;
command_frame = ComFrame_ReceiveEx(host_com_master_id, NULL, 100, &offset, &cached_size, &timeout_flag, true);
}).detach();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
char command_data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
exint_send(ET_TYPE_COMMAND, 6, command_data );
for (int i = 0; i < 100; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (command_frame) {
break;
}
}
timeout_flag = false;
assert(command_frame);
assert_eq(ComFrame_TYPE(command_frame), COM_FRAME_TYPE_REQUEST);
assert_eq(ComFrame_PAYLOAD_LENGTH(command_frame), 6);
assert_mem_eq(ComFrame_PAYLOAD(command_frame), command_data, 6);
END_TEST;
}