1 /*
2  * Copyright (c) 2022-2024 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/img_mgmt/img_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 MCUmgr command header with sequence number set to 1 */
smp_make_hdr(struct smp_hdr * rsp_hdr,size_t len,uint8_t type,bool write)14 static void smp_make_hdr(struct smp_hdr *rsp_hdr, size_t len, uint8_t type, bool write)
15 {
16 	*rsp_hdr = (struct smp_hdr) {
17 		.nh_len = sys_cpu_to_be16(len),
18 		.nh_flags = 0,
19 		.nh_op = (write ? MGMT_OP_WRITE : MGMT_OP_READ),
20 		.nh_group = sys_cpu_to_be16(MGMT_GROUP_ID_IMAGE),
21 		.nh_seq = 1,
22 		.nh_id = type,
23 		.nh_version = 1,
24 	};
25 }
26 
create_img_mgmt_slot_info_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)27 bool create_img_mgmt_slot_info_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
28 				      uint16_t *buffer_size)
29 {
30 	bool ok;
31 
32 	ok = zcbor_map_start_encode(zse, 2) &&
33 	     zcbor_map_end_encode(zse, 2);
34 
35 	if (!ok) {
36 		return false;
37 	}
38 
39 	*buffer_size = (zse->payload_mut - buffer);
40 	smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, IMG_MGMT_ID_SLOT_INFO, false);
41 	memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
42 	*buffer_size += sizeof(struct smp_hdr);
43 
44 	return true;
45 }
46