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