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/settings_mgmt/settings_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_SETTINGS),
21 .nh_seq = 1,
22 .nh_id = type,
23 .nh_version = 1,
24 };
25 }
26
create_settings_mgmt_read_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size,char * name,uint32_t max_size)27 bool create_settings_mgmt_read_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
28 uint16_t *buffer_size, char *name, uint32_t max_size)
29 {
30 bool ok;
31
32 ok = zcbor_map_start_encode(zse, 2) &&
33 zcbor_tstr_put_lit(zse, "name") &&
34 zcbor_tstr_put_term(zse, name, CONFIG_ZCBOR_MAX_STR_LEN) &&
35 (max_size == 0 || (zcbor_tstr_put_lit(zse, "max_size") &&
36 zcbor_uint32_put(zse, max_size))) &&
37 zcbor_map_end_encode(zse, 2);
38
39 *buffer_size = (zse->payload_mut - buffer);
40 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_READ_WRITE,
41 false);
42 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
43 *buffer_size += sizeof(struct smp_hdr);
44
45 return ok;
46 }
47
create_settings_mgmt_write_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size,char * name,const uint8_t * val,size_t val_size)48 bool create_settings_mgmt_write_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
49 uint16_t *buffer_size, char *name, const uint8_t *val,
50 size_t val_size)
51 {
52 bool ok;
53
54 ok = zcbor_map_start_encode(zse, 2) &&
55 zcbor_tstr_put_lit(zse, "name") &&
56 zcbor_tstr_put_term(zse, name, CONFIG_ZCBOR_MAX_STR_LEN) &&
57 zcbor_tstr_put_lit(zse, "val") &&
58 zcbor_bstr_encode_ptr(zse, val, val_size) &&
59 zcbor_map_end_encode(zse, 2);
60
61 *buffer_size = (zse->payload_mut - buffer);
62 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_READ_WRITE,
63 true);
64 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
65 *buffer_size += sizeof(struct smp_hdr);
66
67 return ok;
68 }
69
create_settings_mgmt_delete_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size,char * name)70 bool create_settings_mgmt_delete_packet(zcbor_state_t *zse, uint8_t *buffer,
71 uint8_t *output_buffer, uint16_t *buffer_size, char *name)
72 {
73 bool ok;
74
75 ok = zcbor_map_start_encode(zse, 2) &&
76 zcbor_tstr_put_lit(zse, "name") &&
77 zcbor_tstr_put_term(zse, name, CONFIG_ZCBOR_MAX_STR_LEN) &&
78 zcbor_map_end_encode(zse, 2);
79
80 *buffer_size = (zse->payload_mut - buffer);
81 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_DELETE, true);
82 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
83 *buffer_size += sizeof(struct smp_hdr);
84
85 return ok;
86 }
87
create_settings_mgmt_commit_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)88 bool create_settings_mgmt_commit_packet(zcbor_state_t *zse, uint8_t *buffer,
89 uint8_t *output_buffer, uint16_t *buffer_size)
90 {
91 bool ok;
92
93 ok = zcbor_map_start_encode(zse, 2) &&
94 zcbor_map_end_encode(zse, 2);
95
96 *buffer_size = (zse->payload_mut - buffer);
97 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_COMMIT, true);
98 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
99 *buffer_size += sizeof(struct smp_hdr);
100
101 return ok;
102 }
103
create_settings_mgmt_load_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)104 bool create_settings_mgmt_load_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
105 uint16_t *buffer_size)
106 {
107 bool ok;
108
109 ok = zcbor_map_start_encode(zse, 2) &&
110 zcbor_map_end_encode(zse, 2);
111
112 *buffer_size = (zse->payload_mut - buffer);
113 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_LOAD_SAVE,
114 false);
115 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
116 *buffer_size += sizeof(struct smp_hdr);
117
118 return ok;
119 }
120
create_settings_mgmt_save_packet(zcbor_state_t * zse,uint8_t * buffer,uint8_t * output_buffer,uint16_t * buffer_size)121 bool create_settings_mgmt_save_packet(zcbor_state_t *zse, uint8_t *buffer, uint8_t *output_buffer,
122 uint16_t *buffer_size)
123 {
124 bool ok;
125
126 ok = zcbor_map_start_encode(zse, 2) &&
127 zcbor_map_end_encode(zse, 2);
128
129 *buffer_size = (zse->payload_mut - buffer);
130 smp_make_hdr((struct smp_hdr *)output_buffer, *buffer_size, SETTINGS_MGMT_ID_LOAD_SAVE,
131 true);
132 memcpy(&output_buffer[sizeof(struct smp_hdr)], buffer, *buffer_size);
133 *buffer_size += sizeof(struct smp_hdr);
134
135 return ok;
136 }
137