|
| 1 | +/* main.c - Application main entry point */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2019 Intel Corporation |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <logging/log.h> |
| 10 | +LOG_MODULE_REGISTER(can_test, LOG_LEVEL_ERR); |
| 11 | + |
| 12 | +#include <zephyr/types.h> |
| 13 | +#include <stddef.h> |
| 14 | +#include <string.h> |
| 15 | +#include <can.h> |
| 16 | + |
| 17 | +#include <ztest.h> |
| 18 | + |
| 19 | +static void test_can_frame_to_msg(void) |
| 20 | +{ |
| 21 | + struct can_frame frame = { 0 }; |
| 22 | + struct can_msg expected = { 0 }; |
| 23 | + struct can_msg msg; |
| 24 | + const u8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04, |
| 25 | + 0x05, 0x06, 0x07, 0x08 }; |
| 26 | + |
| 27 | + frame.can_id = BIT(31) | BIT(30) | 1234; |
| 28 | + frame.can_dlc = sizeof(data); |
| 29 | + memcpy(frame.data, data, sizeof(frame.data)); |
| 30 | + |
| 31 | + expected.rtr = 1; |
| 32 | + expected.id_type = 1; |
| 33 | + expected.std_id = 1234; |
| 34 | + expected.dlc = sizeof(data); |
| 35 | + |
| 36 | + can_copy_frame_to_msg(&frame, &msg); |
| 37 | + |
| 38 | + LOG_HEXDUMP_DBG((const u8_t *)&frame, sizeof(frame), "frame"); |
| 39 | + LOG_HEXDUMP_DBG((const u8_t *)&msg, sizeof(msg), "msg"); |
| 40 | + LOG_HEXDUMP_DBG((const u8_t *)&expected, sizeof(expected), "expected"); |
| 41 | + |
| 42 | + zassert_equal(msg.rtr, expected.rtr, "RTR bit not set"); |
| 43 | + zassert_equal(msg.id_type, expected.id_type, "Id-type bit not set"); |
| 44 | + zassert_equal(msg.std_id, expected.std_id, "Std CAN id invalid"); |
| 45 | + zassert_equal(msg.dlc, expected.dlc, "Msg length invalid"); |
| 46 | +} |
| 47 | + |
| 48 | +static void test_can_msg_to_frame(void) |
| 49 | +{ |
| 50 | + struct can_frame frame = { 0 }; |
| 51 | + struct can_frame expected = { 0 }; |
| 52 | + struct can_msg msg = { 0 }; |
| 53 | + const u8_t data[CAN_MAX_DLEN] = { 0x01, 0x02, 0x03, 0x04, |
| 54 | + 0x05, 0x06, 0x07, 0x08 }; |
| 55 | + |
| 56 | + expected.can_id = BIT(31) | BIT(30) | 1234; |
| 57 | + expected.can_dlc = sizeof(data); |
| 58 | + memcpy(expected.data, data, sizeof(expected.data)); |
| 59 | + |
| 60 | + msg.rtr = 1; |
| 61 | + msg.id_type = 1; |
| 62 | + msg.std_id = 1234; |
| 63 | + msg.dlc = sizeof(data); |
| 64 | + memcpy(msg.data, data, sizeof(data)); |
| 65 | + |
| 66 | + can_copy_msg_to_frame(&msg, &frame); |
| 67 | + |
| 68 | + LOG_HEXDUMP_DBG((const u8_t *)&frame, sizeof(frame), "frame"); |
| 69 | + LOG_HEXDUMP_DBG((const u8_t *)&msg, sizeof(msg), "msg"); |
| 70 | + LOG_HEXDUMP_DBG((const u8_t *)&expected, sizeof(expected), "expected"); |
| 71 | + |
| 72 | + zassert_mem_equal(&frame.can_id, &expected.can_id, sizeof(frame.can_id), |
| 73 | + "CAN ID not same"); |
| 74 | + zassert_mem_equal(&frame.data, &expected.data, sizeof(frame.data), |
| 75 | + "CAN data not same"); |
| 76 | + zassert_equal(frame.can_dlc, expected.can_dlc, |
| 77 | + "CAN msg length not same"); |
| 78 | +} |
| 79 | + |
| 80 | +static void test_can_filter_to_msg_filter(void) |
| 81 | +{ |
| 82 | + struct can_filter filter = { 0 }; |
| 83 | + struct can_msg_filter expected = { 0 }; |
| 84 | + struct can_msg_filter msg_filter = { 0 }; |
| 85 | + |
| 86 | + filter.can_id = BIT(31) | BIT(30) | 1234; |
| 87 | + filter.can_mask = BIT(31) | BIT(30) | 1234; |
| 88 | + |
| 89 | + expected.rtr = 1; |
| 90 | + expected.id_type = 1; |
| 91 | + expected.std_id = 1234; |
| 92 | + expected.rtr_mask = 1; |
| 93 | + expected.std_id_mask = 1234; |
| 94 | + |
| 95 | + can_copy_filter_to_msg_filter(&filter, &msg_filter); |
| 96 | + |
| 97 | + LOG_HEXDUMP_DBG((const u8_t *)&msg_filter, sizeof(msg_filter), |
| 98 | + "msg_filter"); |
| 99 | + LOG_HEXDUMP_DBG((const u8_t *)&filter, sizeof(filter), "filter"); |
| 100 | + LOG_HEXDUMP_DBG((const u8_t *)&expected, sizeof(expected), "expected"); |
| 101 | + |
| 102 | + zassert_equal(msg_filter.rtr, expected.rtr, "RTR bit not set"); |
| 103 | + zassert_equal(msg_filter.id_type, expected.id_type, |
| 104 | + "Id-type bit not set"); |
| 105 | + zassert_equal(msg_filter.std_id, expected.std_id, |
| 106 | + "Std CAN id invalid"); |
| 107 | + zassert_equal(msg_filter.rtr_mask, expected.rtr_mask, |
| 108 | + "RTR mask bit not set"); |
| 109 | + zassert_equal(msg_filter.std_id_mask, expected.std_id_mask, |
| 110 | + "Std id mask not set"); |
| 111 | +} |
| 112 | + |
| 113 | +static void test_can_msg_filter_to_filter(void) |
| 114 | +{ |
| 115 | + struct can_filter filter = { 0 }; |
| 116 | + struct can_filter expected = { 0 }; |
| 117 | + struct can_msg_filter msg_filter = { 0 }; |
| 118 | + |
| 119 | + expected.can_id = BIT(31) | BIT(30) | 1234; |
| 120 | + expected.can_mask = BIT(31) | BIT(30) | 1234; |
| 121 | + |
| 122 | + msg_filter.rtr = 1; |
| 123 | + msg_filter.id_type = 1; |
| 124 | + msg_filter.std_id = 1234; |
| 125 | + msg_filter.rtr_mask = 1; |
| 126 | + msg_filter.std_id_mask = 1234; |
| 127 | + |
| 128 | + can_copy_msg_filter_to_filter(&msg_filter, &filter); |
| 129 | + |
| 130 | + LOG_HEXDUMP_DBG((const u8_t *)&msg_filter, sizeof(msg_filter), |
| 131 | + "msg_filter"); |
| 132 | + LOG_HEXDUMP_DBG((const u8_t *)&filter, sizeof(filter), "filter"); |
| 133 | + LOG_HEXDUMP_DBG((const u8_t *)&expected, sizeof(expected), "expected"); |
| 134 | + |
| 135 | + zassert_mem_equal(&filter.can_id, &expected.can_id, |
| 136 | + sizeof(filter.can_id), "CAN ID not same"); |
| 137 | + zassert_mem_equal(&filter.can_mask, &expected.can_mask, |
| 138 | + sizeof(filter.can_mask), "CAN mask not same"); |
| 139 | +} |
| 140 | + |
| 141 | +void test_main(void) |
| 142 | +{ |
| 143 | + ztest_test_suite(test_can_frame, |
| 144 | + ztest_unit_test(test_can_frame_to_msg), |
| 145 | + ztest_unit_test(test_can_msg_to_frame), |
| 146 | + ztest_unit_test(test_can_filter_to_msg_filter), |
| 147 | + ztest_unit_test(test_can_msg_filter_to_filter)); |
| 148 | + |
| 149 | + ztest_run_test_suite(test_can_frame); |
| 150 | +} |
0 commit comments