1 /*
2  * Copyright (c) 2022 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 
13 /* SMP header function for generating os_mgmt info command header with sequence number set to 1 */
smp_make_hdr(struct smp_hdr * rsp_hdr,size_t len)14 void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len)
15 {
16 	*rsp_hdr = (struct smp_hdr) {
17 		.nh_len = sys_cpu_to_be16(len),
18 		.nh_flags = 0,
19 		.nh_op = 0,
20 		.nh_group = sys_cpu_to_be16(MGMT_GROUP_ID_OS),
21 		.nh_seq = 1,
22 		.nh_id = OS_MGMT_ID_INFO,
23 	};
24 }
25 
26 /* Function for creating an os_mgmt info command */
create_mcumgr_format_packet(zcbor_state_t * zse,const uint8_t * format,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)27 bool create_mcumgr_format_packet(zcbor_state_t *zse, const uint8_t *format, uint8_t *buffer,
28 				 uint8_t *output_buffer, uint16_t *buffer_size)
29 {
30 	bool ok;
31 
32 	ok = zcbor_map_start_encode(zse, 2)	&&
33 	     zcbor_tstr_put_lit(zse, "format")	&&
34 	     zcbor_tstr_put_term(zse, format, CONFIG_ZCBOR_MAX_STR_LEN)	&&
35 	     zcbor_map_end_encode(zse, 2);
36 
37 	*buffer_size = (zse->payload_mut - buffer);
38 	smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size);
39 	memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
40 	*buffer_size += sizeof(struct smp_hdr);
41 
42 	return ok;
43 }
44