1 /*
2  * Copyright (c) 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 
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,uint8_t version)14 void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len, uint8_t version)
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 		.nh_version = version,
24 	};
25 }
26 
27 /* 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,uint8_t version)28 bool create_mcumgr_format_packet(zcbor_state_t *zse, const uint8_t *format, uint8_t *buffer,
29 				 uint8_t *output_buffer, uint16_t *buffer_size, uint8_t version)
30 {
31 	bool ok;
32 
33 	ok = zcbor_map_start_encode(zse, 2)	&&
34 	     zcbor_tstr_put_lit(zse, "format")	&&
35 	     zcbor_tstr_put_term(zse, format, CONFIG_ZCBOR_MAX_STR_LEN)	&&
36 	     zcbor_map_end_encode(zse, 2);
37 
38 	*buffer_size = (zse->payload_mut - buffer);
39 	smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, version);
40 	memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
41 	*buffer_size += sizeof(struct smp_hdr);
42 
43 	return ok;
44 }
45