1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/net_buf.h>
9 #include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
10 #include <zephyr/mgmt/mcumgr/transport/smp_dummy.h>
11 #include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
12 
13 #define SMP_RESPONSE_WAIT_TIME 3
14 
15 /* Test os_mgmt echo command with 40 bytes of data: "short MCUMGR test application message..." */
16 static const uint8_t command[] = {
17 	0x02, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x01, 0x00,
18 	0xbf, 0x61, 0x64, 0x78, 0x28, 0x73, 0x68, 0x6f,
19 	0x72, 0x74, 0x20, 0x4d, 0x43, 0x55, 0x4d, 0x47,
20 	0x52, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x61,
21 	0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69,
22 	0x6f, 0x6e, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
23 	0x67, 0x65, 0x2e, 0x2e, 0x2e, 0xff,
24 };
25 
26 /* Expected response from mcumgr */
27 static const uint8_t expected_response[] = {
28 	0x03, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x01, 0x00,
29 	0xbf, 0x61, 0x72, 0x78, 0x28, 0x73, 0x68, 0x6f,
30 	0x72, 0x74, 0x20, 0x4d, 0x43, 0x55, 0x4d, 0x47,
31 	0x52, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x61,
32 	0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69,
33 	0x6f, 0x6e, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61,
34 	0x67, 0x65, 0x2e, 0x2e, 0x2e, 0xff
35 };
36 
ZTEST(os_mgmt_echo,test_echo)37 ZTEST(os_mgmt_echo, test_echo)
38 {
39 	struct net_buf *nb;
40 
41 	/* Enable dummy SMP backend and ready for usage */
42 	smp_dummy_enable();
43 	smp_dummy_clear_state();
44 
45 	/* Send test echo command to dummy SMP backend */
46 	(void)smp_dummy_tx_pkt(command, sizeof(command));
47 	smp_dummy_add_data();
48 
49 	/* For a short duration to see if response has been received */
50 	bool received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
51 
52 	zassert_true(received, "Expected to receive data but timed out\n");
53 
54 	/* Retrieve response buffer and ensure validity */
55 	nb = smp_dummy_get_outgoing();
56 	smp_dummy_disable();
57 
58 	zassert_equal(sizeof(expected_response), nb->len,
59 		      "Expected to receive %d bytes but got %d\n", sizeof(expected_response),
60 		      nb->len);
61 
62 	zassert_mem_equal(expected_response, nb->data, nb->len,
63 			  "Expected received data mismatch");
64 }
65 
66 ZTEST_SUITE(os_mgmt_echo, NULL, NULL, NULL, NULL, NULL);
67