src.dualinventive.com/fw/dncm/libdi/tests/can_msg_timesync.cpp

62 lines
1.7 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 <stdio.h>
#include "tests/can.h"
#include <gtest/gtest.h>
#include <di/time.h>
#include <di/buffer.h>
#include <di/can/msg.h>
TEST_CAN_INIT_DECL
/**
* Check if MFT message with DINET_TIME is correctly reassembled and time stamp is set
*/
static di_errno_t recv_cb_mft_raw_timesync(struct di_can_frame_rx *frame)
{
/* Msgtype : DI_CAN_MSGTYPE_RAW
* Transfertype : DI_CAN_TRANSFERTYPE_PUBLISH
* di_rpc_types : DI_CAN_RAW_DTYPE_DINET_TIME
* Source Node ID : 0x00000000
* Destination Node ID : 0xffffffff (DI_CAN_NODEID_BROADCAST)
* Payload type : 0x09 (DI_CAN_PTYPE_U64)
* Payload size : 0x8
* Payload CRC : 0x97df
* Payload : ffff ffff ffff ffff
*/
static unsigned int idx = 0;
static const struct di_can_frame_rx tvs[] = {
{ 0, 0, 8, 0x0000c000, {0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff}, NULL },
{ 0, 0, 8, 0x0000c020, {0x09, 0x00, 0x08, 0x97, 0xdf, 0xff, 0xff, 0xff}, NULL },
{ 0, 0, 8, 0x0000c041, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, NULL }
};
memcpy(frame, &tvs[idx], sizeof(tvs[0]));
idx++;
if (idx >= DI_ARRAY_SIZE(tvs))
idx = 0;
return DNOK;
}
TEST(can_msg, timesync) {
ASSERT_EQ(0, di_time_get_stamp());
di_can_msg_enable_timesync(&can_ctx, true);
di_can_callback_set_recv(&can_ctx, recv_cb_mft_raw_timesync);
for (size_t n = 0; n < 3; n++)
di_can_lowlevel_recv(&can_ctx);
di_can_reassemble(&can_ctx);
ASSERT_EQ(0xffffffffffffffff, di_time_get_stamp());
}