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/enum_mgmt/enum_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_ENUM),
21 .nh_seq = 1,
22 .nh_id = type,
23 .nh_version = 1,
24 };
25 }
26
create_enum_mgmt_count_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)27 bool create_enum_mgmt_count_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, ENUM_MGMT_ID_COUNT, 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
create_enum_mgmt_list_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)47 bool create_enum_mgmt_list_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
48 uint16_t *buffer_size)
49 {
50 bool ok;
51
52 ok = zcbor_map_start_encode(zse, 2) &&
53 zcbor_map_end_encode(zse, 2);
54
55 if (!ok) {
56 return false;
57 }
58
59 *buffer_size = (zse->payload_mut - buffer);
60 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, ENUM_MGMT_ID_LIST, false);
61 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
62 *buffer_size += sizeof(struct smp_hdr);
63
64 return true;
65 }
66
create_enum_mgmt_single_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size,uint32_t index)67 bool create_enum_mgmt_single_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
68 uint16_t *buffer_size, uint32_t index)
69 {
70 bool ok;
71
72 ok = zcbor_map_start_encode(zse, 2) &&
73 zcbor_tstr_put_lit(zse, "index") &&
74 zcbor_uint32_put(zse, index) &&
75 zcbor_map_end_encode(zse, 2);
76
77 if (!ok) {
78 return false;
79 }
80
81 *buffer_size = (zse->payload_mut - buffer);
82 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, ENUM_MGMT_ID_SINGLE, false);
83 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
84 *buffer_size += sizeof(struct smp_hdr);
85
86 return true;
87 }
88
create_enum_mgmt_details_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size,uint16_t * groups,uint8_t groups_size)89 bool create_enum_mgmt_details_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
90 uint16_t *buffer_size, uint16_t *groups, uint8_t groups_size)
91 {
92 bool ok;
93
94 ok = zcbor_map_start_encode(zse, 2);
95
96 if (groups_size > 0) {
97 uint8_t i = 0;
98
99 ok &= zcbor_tstr_put_lit(zse, "groups") &&
100 zcbor_list_start_encode(zse, groups_size);
101
102 while (i < groups_size) {
103 uint32_t group = (uint32_t)groups[i];
104
105 ok &= zcbor_uint32_encode(zse, &group);
106 }
107
108 ok &= zcbor_list_end_encode(zse, groups_size);
109 }
110
111 ok &= zcbor_map_end_encode(zse, 2);
112
113 if (!ok) {
114 return false;
115 }
116
117 *buffer_size = (zse->payload_mut - buffer);
118 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, ENUM_MGMT_ID_DETAILS, false);
119 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
120 *buffer_size += sizeof(struct smp_hdr);
121
122 return true;
123 }
124