Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | /* * Copyright (c) 2022 Vestas Wind Systems A/S * * SPDX-License-Identifier: Apache-2.0 */ #include "fake_can.h" #include <string.h> #include <zephyr/drivers/can.h> #include <zephyr/fff.h> #include <zephyr/shell/shell.h> #include <zephyr/shell/shell_dummy.h> #include <zephyr/ztest.h> /** * @addtogroup t_can_driver * @{ * @defgroup t_can_api test_can_shell * @} */ #define FAKE_CAN_NAME DEVICE_DT_NAME(DT_NODELABEL(fake_can)) /* Global variables */ static const struct device *const fake_can_dev = DEVICE_DT_GET(DT_NODELABEL(fake_can)); static struct can_timing timing_capture; static struct can_filter filter_capture; static struct can_frame frame_capture; DEFINE_FFF_GLOBALS; static void assert_can_timing_equal(const struct can_timing *t1, const struct can_timing *t2) { zassert_equal(t1->sjw, t2->sjw, "sjw mismatch"); zassert_equal(t1->prop_seg, t2->prop_seg, "prop_seg mismatch"); zassert_equal(t1->phase_seg1, t2->phase_seg1, "hase_seg1 mismatch"); zassert_equal(t1->phase_seg2, t2->phase_seg2, "phase_seg2 mismatch"); zassert_equal(t1->prescaler, t2->prescaler, "prescaler mismatch"); } static void assert_can_filter_equal(const struct can_filter *f1, const struct can_filter *f2) { zassert_equal(f1->id_type, f2->id_type, "id_type mismatch"); zassert_equal(f1->id, f2->id, "id mismatch"); zassert_equal(f1->id_mask, f2->id_mask, "id_mask mismatch"); zassert_equal(f1->rtr, f2->rtr, "rtr mismatch"); zassert_equal(f1->rtr_mask, f2->rtr_mask, "rtr_mask mismatch"); } static void assert_can_frame_equal(const struct can_frame *f1, const struct can_frame *f2) { zassert_equal(f1->id_type, f2->id_type, "id_type mismatch"); zassert_equal(f1->id, f2->id, "id mismatch"); zassert_equal(f1->rtr, f2->rtr, "rtr mismatch"); zassert_equal(f1->fd, f2->fd, "fd mismatch"); zassert_equal(f1->brs, f2->brs, "brs mismatch"); zassert_equal(f1->dlc, f2->dlc, "dlc mismatch"); zassert_mem_equal(f1->data, f2->data, can_dlc_to_bytes(f1->dlc), "data mismatch"); } static int can_shell_test_capture_timing(const struct device *dev, const struct can_timing *timing) { ARG_UNUSED(dev); memcpy(&timing_capture, timing, sizeof(timing_capture)); return 0; } static int can_shell_test_capture_filter(const struct device *dev, can_rx_callback_t callback, void *user_data, const struct can_filter *filter) { ARG_UNUSED(dev); ARG_UNUSED(callback); ARG_UNUSED(user_data); memcpy(&filter_capture, filter, sizeof(filter_capture)); return 0; } static int can_shell_test_capture_frame(const struct device *dev, const struct can_frame *frame, k_timeout_t timeout, can_tx_callback_t callback, void *user_data) { ARG_UNUSED(dev); ARG_UNUSED(timeout); ARG_UNUSED(callback); ARG_UNUSED(user_data); memcpy(&frame_capture, frame, sizeof(frame_capture)); return 0; } ZTEST(can_shell, test_can_show) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can show " FAKE_CAN_NAME); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_get_max_filters_fake.call_count, 2, "get_max_filters function not called twice"); zassert_equal(fake_can_get_capabilities_fake.call_count, 1, "get_capabilities function not called"); zassert_equal(fake_can_get_state_fake.call_count, 1, "get_state function not called"); } ZTEST(can_shell, test_can_bitrate_missing_value) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can bitrate " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without bitrate"); zassert_equal(fake_can_set_timing_fake.call_count, 0, "set_timing function called"); } static void can_shell_test_bitrate(const char *cmd, uint32_t expected_bitrate, uint16_t expected_sample_pnt) { const struct shell *sh = shell_backend_dummy_get_ptr(); struct can_timing expected; int err; expected.sjw = CAN_SJW_NO_CHANGE; err = can_calc_timing(fake_can_dev, &expected, expected_bitrate, expected_sample_pnt); zassert_ok(err, "failed to calculate reference timing (err %d)", err); fake_can_set_timing_fake.custom_fake = can_shell_test_capture_timing; err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_set_timing_fake.call_count, 1, "set_timing function not called"); zassert_equal(fake_can_set_timing_fake.arg0_val, fake_can_dev, "wrong device pointer"); assert_can_timing_equal(&expected, &timing_capture); } ZTEST(can_shell, test_can_bitrate) { can_shell_test_bitrate("can bitrate " FAKE_CAN_NAME " 125000", 125000, 875); } ZTEST(can_shell, test_can_bitrate_sample_point) { can_shell_test_bitrate("can bitrate " FAKE_CAN_NAME " 125000 750", 125000, 750); } ZTEST(can_shell, test_can_dbitrate_missing_value) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; Z_TEST_SKIP_IFNDEF(CONFIG_CAN_FD_MODE); err = shell_execute_cmd(sh, "can dbitrate " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without dbitrate"); zassert_equal(fake_can_set_timing_data_fake.call_count, 0, "set_timing_data function called"); } static void can_shell_test_dbitrate(const char *cmd, uint32_t expected_bitrate, uint16_t expected_sample_pnt) { const struct shell *sh = shell_backend_dummy_get_ptr(); struct can_timing expected; int err; Z_TEST_SKIP_IFNDEF(CONFIG_CAN_FD_MODE); expected.sjw = CAN_SJW_NO_CHANGE; err = can_calc_timing_data(fake_can_dev, &expected, expected_bitrate, expected_sample_pnt); zassert_ok(err, "failed to calculate reference timing (err %d)", err); fake_can_set_timing_data_fake.custom_fake = can_shell_test_capture_timing; err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_set_timing_data_fake.call_count, 1, "set_timing_data function not called"); zassert_equal(fake_can_set_timing_data_fake.arg0_val, fake_can_dev, "wrong device pointer"); assert_can_timing_equal(&expected, &timing_capture); } ZTEST(can_shell, test_can_dbitrate) { can_shell_test_dbitrate("can dbitrate " FAKE_CAN_NAME " 1000000", 1000000, 750); } ZTEST(can_shell, test_can_dbitrate_sample_point) { can_shell_test_dbitrate("can dbitrate " FAKE_CAN_NAME " 1000000 875", 1000000, 875); } ZTEST(can_shell, test_can_mode_missing_value) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can mode " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without mode value"); zassert_equal(fake_can_set_mode_fake.call_count, 0, "set_mode function called"); } ZTEST(can_shell, test_can_mode_unknown) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can mode " FAKE_CAN_NAME " foobarbaz"); zassert_not_equal(err, 0, " executed shell command with unknown mode value"); zassert_equal(fake_can_set_mode_fake.call_count, 0, "set_mode function called"); } static void can_shell_test_mode(const char *cmd, can_mode_t expected) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_set_mode_fake.call_count, 1, "set_mode function not called"); zassert_equal(fake_can_set_mode_fake.arg0_val, fake_can_dev, "wrong device pointer"); zassert_equal(fake_can_set_mode_fake.arg1_val, expected, "wrong mode value"); } ZTEST(can_shell, test_can_mode_raw_value) { can_shell_test_mode("can mode " FAKE_CAN_NAME " 0xaabbccdd", 0xaabbccdd); } ZTEST(can_shell, test_can_mode_fd) { can_shell_test_mode("can mode " FAKE_CAN_NAME " fd", CAN_MODE_FD); } ZTEST(can_shell, test_can_mode_listen_only) { can_shell_test_mode("can mode " FAKE_CAN_NAME " listen-only", CAN_MODE_LISTENONLY); } ZTEST(can_shell, test_can_mode_loopback) { can_shell_test_mode("can mode " FAKE_CAN_NAME " loopback", CAN_MODE_LOOPBACK); } ZTEST(can_shell, test_can_mode_normal) { can_shell_test_mode("can mode " FAKE_CAN_NAME " normal", CAN_MODE_NORMAL); } ZTEST(can_shell, test_can_mode_one_shot) { can_shell_test_mode("can mode " FAKE_CAN_NAME " one-shot", CAN_MODE_ONE_SHOT); } ZTEST(can_shell, test_can_mode_triple_sampling) { can_shell_test_mode("can mode " FAKE_CAN_NAME " triple-sampling", CAN_MODE_3_SAMPLES); } ZTEST(can_shell, test_can_mode_combined) { can_shell_test_mode("can mode " FAKE_CAN_NAME " listen-only loopback", CAN_MODE_LISTENONLY | CAN_MODE_LOOPBACK); } ZTEST(can_shell, test_can_send_missing_id) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can send " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without CAN ID"); zassert_equal(fake_can_send_fake.call_count, 0, "send function called"); } static void can_shell_test_send(const char *cmd, const struct can_frame *expected) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; fake_can_send_fake.custom_fake = can_shell_test_capture_frame; err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_send_fake.call_count, 1, "send function not called"); zassert_equal(fake_can_send_fake.arg0_val, fake_can_dev, "wrong device pointer"); assert_can_frame_equal(expected, &frame_capture); } ZTEST(can_shell, test_can_send_std_id) { const struct can_frame expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x010, .rtr = CAN_DATAFRAME, .fd = 0, .brs = 0, .dlc = can_bytes_to_dlc(2), .data = { 0xaa, 0x55 }, }; can_shell_test_send("can send " FAKE_CAN_NAME " 010 aa 55", &expected); } ZTEST(can_shell, test_can_send_ext_id) { const struct can_frame expected = { .id_type = CAN_EXTENDED_IDENTIFIER, .id = 0x1024, .rtr = CAN_DATAFRAME, .fd = 0, .brs = 0, .dlc = can_bytes_to_dlc(4), .data = { 0xde, 0xad, 0xbe, 0xef }, }; can_shell_test_send("can send " FAKE_CAN_NAME " -e 1024 de ad be ef", &expected); } ZTEST(can_shell, test_can_send_no_data) { const struct can_frame expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x133, .rtr = CAN_DATAFRAME, .fd = 0, .brs = 0, .dlc = can_bytes_to_dlc(0), .data = { }, }; can_shell_test_send("can send " FAKE_CAN_NAME " 133", &expected); } ZTEST(can_shell, test_can_send_rtr) { const struct can_frame expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x7ff, .rtr = CAN_REMOTEREQUEST, .fd = 0, .brs = 0, .dlc = can_bytes_to_dlc(0), .data = { }, }; can_shell_test_send("can send " FAKE_CAN_NAME " -r 7ff", &expected); } ZTEST(can_shell, test_can_send_fd) { const struct can_frame expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x123, .rtr = CAN_DATAFRAME, .fd = 1, .brs = 0, .dlc = can_bytes_to_dlc(8), .data = { 0xaa, 0x55, 0xaa, 0x55, 0x11, 0x22, 0x33, 0x44 }, }; can_shell_test_send("can send " FAKE_CAN_NAME " -f 123 aa 55 aa 55 11 22 33 44", &expected); } ZTEST(can_shell, test_can_send_fd_brs) { const struct can_frame expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x321, .rtr = CAN_DATAFRAME, .fd = 1, .brs = 1, .dlc = can_bytes_to_dlc(7), .data = { 0xaa, 0x55, 0xaa, 0x55, 0x11, 0x22, 0x33 }, }; can_shell_test_send("can send " FAKE_CAN_NAME " -f -b 321 aa 55 aa 55 11 22 33", &expected); } ZTEST(can_shell, test_can_send_data_all_options) { const struct can_frame expected = { .id_type = CAN_EXTENDED_IDENTIFIER, .id = 0x1024, .rtr = CAN_REMOTEREQUEST, .fd = 1, .brs = 1, .dlc = can_bytes_to_dlc(0), .data = { }, }; can_shell_test_send("can send " FAKE_CAN_NAME " -r -e -f -b 1024", &expected); } ZTEST(can_shell, test_can_filter_add_missing_id) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can filter add " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without CAN ID"); zassert_equal(fake_can_add_rx_filter_fake.call_count, 0, "add_rx_filter function called"); } static void can_shell_test_filter_add(const char *cmd, const struct can_filter *expected) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; fake_can_add_rx_filter_fake.custom_fake = can_shell_test_capture_filter; err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_add_rx_filter_fake.call_count, 1, "add_rx_filter function not called"); zassert_equal(fake_can_add_rx_filter_fake.arg0_val, fake_can_dev, "wrong device pointer"); assert_can_filter_equal(expected, &filter_capture); } ZTEST(can_shell, test_can_filter_add_std_id) { struct can_filter expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x010, .id_mask = CAN_STD_ID_MASK, .rtr = CAN_DATAFRAME, .rtr_mask = 0, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " 010", &expected); } ZTEST(can_shell, test_can_filter_add_std_id_mask) { struct can_filter expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x010, .id_mask = 0x020, .rtr = CAN_DATAFRAME, .rtr_mask = 0, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " 010 020", &expected); } ZTEST(can_shell, test_can_filter_add_ext_id) { struct can_filter expected = { .id_type = CAN_EXTENDED_IDENTIFIER, .id = 0x1024, .id_mask = CAN_EXT_ID_MASK, .rtr = CAN_DATAFRAME, .rtr_mask = 0, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " -e 1024", &expected); } ZTEST(can_shell, test_can_filter_add_ext_id_mask) { struct can_filter expected = { .id_type = CAN_EXTENDED_IDENTIFIER, .id = 0x1024, .id_mask = 0x2048, .rtr = CAN_DATAFRAME, .rtr_mask = 0, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " -e 1024 2048", &expected); } ZTEST(can_shell, test_can_filter_add_rtr) { struct can_filter expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x022, .id_mask = CAN_STD_ID_MASK, .rtr = CAN_REMOTEREQUEST, .rtr_mask = 0, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " -r 022", &expected); } ZTEST(can_shell, test_can_filter_add_rtr_mask) { struct can_filter expected = { .id_type = CAN_STANDARD_IDENTIFIER, .id = 0x322, .id_mask = CAN_STD_ID_MASK, .rtr = CAN_DATAFRAME, .rtr_mask = 1, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " -R 322", &expected); } ZTEST(can_shell, test_can_filter_add_all_options) { struct can_filter expected = { .id_type = CAN_EXTENDED_IDENTIFIER, .id = 0x2048, .id_mask = 0x4096, .rtr = CAN_REMOTEREQUEST, .rtr_mask = 1, }; can_shell_test_filter_add("can filter add " FAKE_CAN_NAME " -e -r -R 2048 4096", &expected); } ZTEST(can_shell, test_can_filter_remove_missing_value) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can filter remove " FAKE_CAN_NAME); zassert_not_equal(err, 0, " executed shell command without filter ID"); zassert_equal(fake_can_remove_rx_filter_fake.call_count, 0, "remove_rx_filter function called"); } ZTEST(can_shell, test_can_filter_remove) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; err = shell_execute_cmd(sh, "can filter remove " FAKE_CAN_NAME " 1234"); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_remove_rx_filter_fake.call_count, 1, "remove_rx_filter function not called"); zassert_equal(fake_can_remove_rx_filter_fake.arg0_val, fake_can_dev, "wrong device pointer"); zassert_equal(fake_can_remove_rx_filter_fake.arg1_val, 1234, "wrong filter ID"); } static void can_shell_test_recover(const char *cmd, k_timeout_t expected) { const struct shell *sh = shell_backend_dummy_get_ptr(); int err; Z_TEST_SKIP_IFDEF(CONFIG_CAN_AUTO_BUS_OFF_RECOVERY); err = shell_execute_cmd(sh, cmd); zassert_ok(err, "failed to execute shell command (err %d)", err); zassert_equal(fake_can_recover_fake.call_count, 1, "recover function not called"); zassert_equal(fake_can_recover_fake.arg0_val, fake_can_dev, "wrong device pointer"); zassert_true(K_TIMEOUT_EQ(fake_can_recover_fake.arg1_val, expected), "wrong timeout value"); } ZTEST(can_shell, test_can_recover) { can_shell_test_recover("can recover " FAKE_CAN_NAME, K_FOREVER); } ZTEST(can_shell, test_can_recover_timeout) { can_shell_test_recover("can recover " FAKE_CAN_NAME " 100", K_MSEC(100)); } static void can_shell_before(void *fixture) { ARG_UNUSED(fixture); memset(&timing_capture, 0, sizeof(timing_capture)); memset(&filter_capture, 0, sizeof(filter_capture)); memset(&frame_capture, 0, sizeof(frame_capture)); } static void *can_shell_setup(void) { const struct shell *sh = shell_backend_dummy_get_ptr(); /* Wait for the initialization of the shell dummy backend. */ WAIT_FOR(shell_ready(sh), 20000, k_msleep(1)); zassert_true(shell_ready(sh), "timed out waiting for dummy shell backend"); return NULL; } ZTEST_SUITE(can_shell, NULL, can_shell_setup, can_shell_before, NULL, NULL); |