src.dualinventive.com/dinet/libdi/tests/can_raw_dncm.cpp

106 lines
2.8 KiB
C++

/**
* @file tests/can_msg.cpp
* @brief CAN message unit test
* @date Sep 30, 2015
* @author jjacobs
* @copyright 2015 Dual Inventive Technology Centre B.V.
*
* CAN Message unit test
*/
#include <thread>
#include "fixtures/CANTest.hpp"
static di_errno_t g_ret = DNE_IOFAILED;
static bool g_on_change = false;
static uint16_t g_interval_sec = 0;
void reqrep_dncm_communication_status_thread(struct di_can_ctx *ctx) {
struct di_can_msg *msg;
while (true) {
msg = di_can_recv(ctx, DI_CAN_MSGTYPE_ANY);
if (!msg)
continue;
/* Send reply on request */
if (DI_CAN_GET_TRANSFERTYPE(msg->canid) == DI_CAN_TRANSFERTYPE_REQUEST) {
struct di_can_msg *rep = di_can_msg_alloc_with_canid(ctx,
DI_CAN_MSGTYPE_RAW,
DI_CAN_TRANSFERTYPE_REPLY,
DI_CAN_RAW_DTYPE_DNCM_COMMUNICATION_STATUS);
g_ret = di_can_raw_deserialize_dncm_communication_status_req(&g_on_change, &g_interval_sec, msg);
rep->dst_id = msg->src_id;
DI_CAN_SET_TRANSACTION(rep->canid, DI_CAN_GET_TRANSACTION(msg->canid));
di_can_send(&rep);
return;
}
di_can_msg_free(&msg);
di_can_msg_gc(ctx, DI_CAN_GC_ALL);
di_can_frame_gc(ctx, DI_CAN_GC_ALL);
}
}
/** Check if DNOK is returned when a reply is received with a broadcast
* * Test msg->broadcast = true
* * Test msg->dst_id = DI_CAN_NODEID_BROADCAST
*/
TEST_F(DI_CAN, raw_req_dncm_communication_status) {
auto thr = std::thread(reqrep_dncm_communication_status_thread, &_ctx);
// Request DNCM communication status
ASSERT_EQ(DNOK, di_can_raw_req_dncm_communication_status(&_ctx, true, 1234, 10));
// Check if message is correctly deserialized
ASSERT_TRUE(g_on_change);
ASSERT_EQ(1234, g_interval_sec);
thr.join();
}
static void reqrep_dncm_gps_sensor_trigger_thread(struct di_can_ctx *ctx) {
struct di_can_msg *msg;
while (true) {
msg = di_can_recv(ctx, DI_CAN_MSGTYPE_ANY);
if (!msg)
continue;
/* Send reply on request */
if (DI_CAN_GET_TRANSFERTYPE(msg->canid) == DI_CAN_TRANSFERTYPE_REQUEST) {
struct di_can_msg *rep = di_can_msg_alloc_with_canid(ctx,
DI_CAN_MSGTYPE_RAW,
DI_CAN_TRANSFERTYPE_REPLY,
DI_CAN_RAW_DTYPE_DNCM_GPS_SENSOR_TRIGGER);
rep->dst_id = msg->src_id;
DI_CAN_SET_TRANSACTION(rep->canid, DI_CAN_GET_TRANSACTION(msg->canid));
di_can_send(&rep);
return;
}
di_can_msg_free(&msg);
di_can_msg_gc(ctx, DI_CAN_GC_ALL);
di_can_frame_gc(ctx, DI_CAN_GC_ALL);
}
}
/** Check if DNOK is returned when a reply is received with a broadcast
* * Test msg->broadcast = true
* * Test msg->dst_id = DI_CAN_NODEID_BROADCAST
*/
TEST_F(DI_CAN, req_dncm_gps_sensor_trigger) {
auto thr = std::thread(reqrep_dncm_gps_sensor_trigger_thread, &_ctx);
// Request DNCM GPS sensor trigger
ASSERT_EQ(DNOK, di_can_raw_req_dncm_gps_sensor_trigger(&_ctx, 10));
thr.join();
}