1 /*
2  * Copyright (c) 2022-2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "smp_test_util.h"
8 #include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
9 #include <zephyr/net_buf.h>
10 #include <zephyr/sys/byteorder.h>
11 #include <zcbor_encode.h>
12 
smp_make_hdr(struct smp_hdr * rsp_hdr,size_t len)13 void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len)
14 {
15 	*rsp_hdr = (struct smp_hdr) {
16 		.nh_len = sys_cpu_to_be16(len),
17 		.nh_flags = 0,
18 		.nh_op = 0,
19 		.nh_group = sys_cpu_to_be16(MGMT_GROUP_ID_OS),
20 		.nh_seq = 1,
21 		.nh_id = OS_MGMT_ID_ECHO,
22 	};
23 }
24 
create_mcumgr_format_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)25 bool create_mcumgr_format_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
26 				 uint16_t *buffer_size)
27 {
28 	bool ok;
29 
30 	ok = zcbor_map_start_encode(zse, 2)		&&
31 	     zcbor_tstr_put_lit(zse, "d")		&&
32 	     zcbor_tstr_put_lit(zse, "some test data")	&&
33 	     zcbor_map_end_encode(zse, 2);
34 
35 	*buffer_size = (zse->payload_mut - buffer);
36 	smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size);
37 	memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
38 	*buffer_size += sizeof(struct smp_hdr);
39 
40 	return ok;
41 }
42