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

141 lines
3.4 KiB
C++

/**
* @file tests/can_rpc.cpp
* @brief brief
* @date Aug 25, 2015
* @author rheijden
* @copyright 2015 Dual Inventive Technology Centre B.V.
*
* descr
*/
#include <cstdio>
#include <di/device/uid.h>
#include "fixtures/CANTest.hpp"
extern "C" {
#include "can.c"
}
/**
* Set nodeid based on "0..." device:uid
*/
TEST_F(DI_CAN, set_nodeid) {
ASSERT_STREQ("00000000000000000000000000000000", di_device_uid_get());
di_can_set_nodeid(&_ctx, di_device_uid_get());
printf("nodeid(\"...0\"): 0x%08x\n", _ctx.nodeid);
ASSERT_EQ(0xa29886da, _ctx.nodeid);
di_device_uid_set("00000000000000000000000000000001");
di_can_set_nodeid(&_ctx, di_device_uid_get());
printf("nodeid(\"...1\"): 0x%08x\n", _ctx.nodeid);
ASSERT_EQ(0xa096fec5, _ctx.nodeid);
}
/**
* Test sending invalid message
* 1. msg = NULL
* 2. *msg = NULL
* 3. (*msg)->ctx = NULL
*/
TEST_F(DI_CAN, send_invalid) {
struct di_can_msg *msg = NULL;
struct di_can_msg m;
ASSERT_EQ(DNE_PARAM, di_can_send(NULL));
ASSERT_EQ(DNE_PARAM, di_can_send(&msg));
m.size = 0;
m.size_written = 0;
m.msg = NULL;
m.buf = NULL;
m.ctx = NULL;
msg = &m;
ASSERT_EQ(DNE_PARAM, di_can_send(&msg));
}
/**
* Send CAN message (this will be pushed to the lowlevel
* layer and then returned to the tx pool).
*/
static int test_broadcast_recv_cb_cnt = 0;
static int test_broadcast_send_cb_cnt = 0;
static void test_broadcast_reset_cnt(void)
{
test_broadcast_recv_cb_cnt = 0;
test_broadcast_send_cb_cnt = 0;
}
static di_errno_t test_broadcast_can_recv_cb(struct di_can_frame_rx *frame)
{
(void)frame;
test_broadcast_recv_cb_cnt++;
return DNOK;
}
static di_errno_t test_broadcast_can_send_cb(const struct di_can_frame_tx *frame)
{
printf("[%08x] ", frame->canid);
for (size_t n = 0; n < frame->size; n++)
printf("%02x ", ((uint8_t *)frame->buf)[n]);
printf("\n");
test_broadcast_send_cb_cnt++;
return DNOK;
}
TEST_F(DI_CAN, send_broadcast) {
static uint8_t test_data8[8] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
static uint8_t test_data16[16] = {
0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x88,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88
};
struct di_can_msg *msg;
struct di_can_msg m;
di_can_callback_set_send(&_ctx, test_broadcast_can_send_cb);
di_can_callback_set_recv(&_ctx, test_broadcast_can_recv_cb);
/* Send can message (which is exactly one can frame) */
memset(&m, 0, sizeof(m));
m.size = sizeof(test_data8);
m.msg = test_data8;
m.dst_id = DI_CAN_NODEID_BROADCAST; /* Broadcast the message */
m.buf = NULL; /* Notify can stack the msg is allocated by the application */
m.ctx = &_ctx;
msg = &m;
ASSERT_EQ(DNOK, di_can_send(&msg));
ASSERT_EQ(3, test_broadcast_send_cb_cnt);
test_broadcast_reset_cnt();
/* Send can message (which are two frames */
m.size = sizeof(test_data16);
m.msg = test_data16;
ASSERT_EQ(DNOK, di_can_send(&msg));
ASSERT_EQ(4, test_broadcast_send_cb_cnt);
}
/* Send message allocated from pool, it will be returned to the pool */
TEST_F(DI_CAN, send_broadcast_from_msg_pool) {
struct di_can_msg *msg;
msg = di_can_msg_alloc(&_ctx);
msg->canid = 0x12345678;
msg->size = 0;
msg->dst_id = DI_CAN_NODEID_BROADCAST;
ASSERT_NE((void *)NULL, msg);
di_can_send(&msg);
}
TEST_F(DI_CAN, recv_invalid) {
ASSERT_EQ((void *)NULL, di_can_recv(&_ctx, DI_CAN_MSGTYPE_RAW));
ASSERT_EQ((void *)NULL, di_can_recv(&_ctx, DI_CAN_MSGTYPE_RPC));
ASSERT_EQ((void *)NULL, di_can_recv(&_ctx, DI_CAN_MSGTYPE_NET));
}