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

144 lines
2.9 KiB
C++

/**
* @file tests/can_msg_read.cpp
* @brief Test CAN message field and payload read functions
* @date Aug 4, 2016
* @author jjacobs
* @copyright 2015 Dual Inventive Technology Centre B.V.
*/
#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
/**
* Read Source Node ID field
*/
TEST(can_msg_read, src_id)
{
struct di_can_msg *msg = di_can_msg_alloc(&can_ctx);
msg->src_id = 0;
di_can_msg_write_src_id(msg, 0x12345678);
msg->src_id = di_can_msg_read_src_id(msg);
ASSERT_EQ(0x12345678, msg->src_id);
di_can_msg_free(&msg);
}
/**
* Read Destination Node ID field
*/
TEST(can_msg_read, dst_id)
{
struct di_can_msg *msg = di_can_msg_alloc(&can_ctx);
msg->dst_id = 0;
di_can_msg_write_dst_id(msg, 0xdeadbeef);
msg->dst_id = di_can_msg_read_dst_id(msg);
ASSERT_EQ(0xdeadbeef, msg->dst_id);
di_can_msg_free(&msg);
}
/**
* Read Payload Type field
*/
TEST(can_msg_read, ptype)
{
struct di_can_msg *msg = di_can_msg_alloc(&can_ctx);
msg->ptype = DI_CAN_PTYPE_NONE;
di_can_msg_write_ptype(msg, DI_CAN_PTYPE_STRING);
msg->ptype = di_can_msg_read_ptype(msg);
ASSERT_EQ(DI_CAN_PTYPE_STRING, msg->ptype);
di_can_msg_free(&msg);
}
/**
* Read Payload CRC field
*/
TEST(can_msg_read, crc)
{
uint16_t crc = 0;
struct di_can_msg *msg = di_can_msg_alloc(&can_ctx);
di_can_msg_write_crc(msg, 0x3456);
crc = di_can_msg_read_crc(msg);
ASSERT_EQ(0x3456, crc);
di_can_msg_free(&msg);
}
/**
* Read Payload Size field
*/
TEST(can_msg_read, psize)
{
uint16_t psize = 0;
struct di_can_msg *msg = di_can_msg_alloc(&can_ctx);
di_can_msg_write_psize(msg, 1234);
psize = di_can_msg_read_psize(msg);
ASSERT_EQ(1234, psize);
di_can_msg_free(&msg);
}
/**
* Read bool payload
*/
TEST(can_msg_read, data_u64)
{
uint64_t value = 0;
struct di_can_msg msg;
struct di_buffer buf;
uint8_t data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFT Source/Dest NodeID
DI_CAN_PTYPE_U64, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, // MFT-meta ptype,psize,pcrc (3 bytes zero fill, unused)
0x00, 0x00, 0x01, 0x56, 0xff, 0xee, 0xbc, 0x9a // 1473172651162
};
buf.data = data;
buf.size = sizeof(data);
msg.size = sizeof(data);
msg.msg = data;
msg.buf = &buf;
ASSERT_EQ(DNOK, di_can_msg_read_data_u64(&msg, &value));
ASSERT_EQ(1473172651162U, value);
}
/**
* Read bool payload
*/
TEST(can_msg_read, data_bool)
{
bool value = false;
struct di_can_msg msg;
struct di_buffer buf;
uint8_t data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFT Source/Dest NodeID
DI_CAN_PTYPE_BOOL, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // MFT-meta ptype,psize,pcrc (3 bytes zero fill, unused)
0x01 // BOOL is true
};
buf.data = data;
buf.size = sizeof(data);
msg.size = sizeof(data);
msg.msg = data;
msg.buf = &buf;
ASSERT_EQ(DNOK, di_can_msg_read_data_bool(&msg, &value));
ASSERT_TRUE(value);
}