1 /*
2 * Copyright (c) 2022 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Opcode aggregator test
7 */
8
9 #include "mesh_test.h"
10
11 #include <string.h>
12
13 #include <zephyr/logging/log.h>
14
15 LOG_MODULE_REGISTER(test_op_agg, LOG_LEVEL_INF);
16
17 #define CLI_ADDR 0x7728
18 #define SRV_ADDR 0x18f8
19 #define WAIT_TIME 15 /* seconds */
20 #define SEM_TIMEOUT K_SECONDS(10)
21
22 #define BT_MESH_DUMMY_VND_MOD_GET_OP BT_MESH_MODEL_OP_3(0xDC, TEST_VND_COMPANY_ID)
23 #define BT_MESH_DUMMY_VND_MOD_STATUS_OP BT_MESH_MODEL_OP_3(0xCD, TEST_VND_COMPANY_ID)
24
25 #define BT_MESH_DUMMY_VND_MOD_MSG_MINLEN 7
26 #define BT_MESH_DUMMY_VND_MOD_MSG_MAXLEN 8
27
28 /* The 34 messages make up the aggregated message sequence, expecting a 380 byte status response. */
29 #define TEST_SEND_ITR 34
30
31 /* Spec: 4.3.9.4: Table 4.273 defines the structure of the OPCODES_AGGREGATOR_STATUS message. */
32 #define OPCODES_AGG_STATUS_MSG_BASE_STRUCTURE_LEN 5
33 /* SPEC: 4.3.9.1: Length_format + Length_Short.*/
34 #define OPCODES_AGG_ITEM_SHORT_FORMAT_LEN 1
35 /* SPEC: 4.3.9.1: The structure of an Aggregator Item field is defined in Table 4.270 */
36 #define OPCODES_STATUS_ITEM_LEN(param_len) \
37 (OPCODES_AGG_ITEM_SHORT_FORMAT_LEN + \
38 BT_MESH_MODEL_OP_LEN(BT_MESH_DUMMY_VND_MOD_STATUS_OP) + param_len)
39 /* Spec: 4.3.9.3 OPCODES_AGGREGATOR_STATUS. The test initiates 33+1 get/status message iterations.*/
40 #define OP_AGG_STATUS_ACCESS_PAYLOAD \
41 (OPCODES_AGG_STATUS_MSG_BASE_STRUCTURE_LEN + \
42 (OPCODES_STATUS_ITEM_LEN(BT_MESH_DUMMY_VND_MOD_MSG_MINLEN) * (TEST_SEND_ITR - 1)) + \
43 OPCODES_STATUS_ITEM_LEN(BT_MESH_DUMMY_VND_MOD_MSG_MAXLEN))
44
45 /* Ensure that a 380-byte opcode aggregator get/status access payload is being sent. */
46 BUILD_ASSERT(OP_AGG_STATUS_ACCESS_PAYLOAD == (BT_MESH_TX_SDU_MAX - BT_MESH_MIC_SHORT));
47
48 static int status_rcvd_count;
49 static int get_rcvd_count;
50 static struct k_sem cli_suspend_sem;
51 static struct k_sem srv_suspend_sem;
52 static const uint8_t dev_key[16] = {0xaa};
53 static uint8_t cli_sent_array[TEST_SEND_ITR], cli_rcvd_array[TEST_SEND_ITR];
54
55 static struct bt_mesh_msg_ctx test_ctx = {
56 .net_idx = 0,
57 .app_idx = 0,
58 .addr = SRV_ADDR,
59 };
60
61 static struct bt_mesh_prov prov;
62 static struct bt_mesh_cfg_cli cfg_cli;
63
get_handler(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)64 static int get_handler(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
65 struct net_buf_simple *buf)
66 {
67 uint8_t seq = net_buf_simple_pull_u8(buf);
68
69 get_rcvd_count++;
70
71 BT_MESH_MODEL_BUF_DEFINE(msg, BT_MESH_DUMMY_VND_MOD_STATUS_OP,
72 BT_MESH_DUMMY_VND_MOD_MSG_MAXLEN);
73 bt_mesh_model_msg_init(&msg, BT_MESH_DUMMY_VND_MOD_STATUS_OP);
74
75 net_buf_simple_add_u8(&msg, seq);
76 memset(net_buf_simple_add(&msg, BT_MESH_DUMMY_VND_MOD_MSG_MINLEN - 1), 0,
77 BT_MESH_DUMMY_VND_MOD_MSG_MINLEN);
78
79 /* Last message: One additional byte is added to fill the available access payload.*/
80 if (get_rcvd_count >= TEST_SEND_ITR) {
81 net_buf_simple_add(&msg, 1);
82 k_sem_give(&srv_suspend_sem);
83 }
84
85 return bt_mesh_model_send(model, ctx, &msg, NULL, NULL);
86 }
87
status_handler(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)88 static int status_handler(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
89 struct net_buf_simple *buf)
90 {
91 uint8_t seq = net_buf_simple_pull_u8(buf);
92
93 status_rcvd_count++;
94 cli_rcvd_array[status_rcvd_count - 1] = seq;
95
96 if (status_rcvd_count >= TEST_SEND_ITR) {
97 k_sem_give(&cli_suspend_sem);
98 }
99
100 return 0;
101 }
102
dummy_vnd_mod_get(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint8_t seq)103 static int dummy_vnd_mod_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, uint8_t seq)
104 {
105 BT_MESH_MODEL_BUF_DEFINE(msg, BT_MESH_DUMMY_VND_MOD_GET_OP,
106 BT_MESH_DUMMY_VND_MOD_MSG_MAXLEN);
107
108 bt_mesh_model_msg_init(&msg, BT_MESH_DUMMY_VND_MOD_GET_OP);
109
110 net_buf_simple_add_u8(&msg, seq);
111 memset(net_buf_simple_add(&msg, BT_MESH_DUMMY_VND_MOD_MSG_MINLEN - 1), 0,
112 BT_MESH_DUMMY_VND_MOD_MSG_MINLEN);
113
114 /* Last message: One additional byte is added to fill the available access payload.*/
115 if (seq >= TEST_SEND_ITR - 1) {
116 net_buf_simple_add(&msg, 1);
117 }
118
119 return bt_mesh_model_send(model, ctx, &msg, NULL, NULL);
120 }
121
122 const struct bt_mesh_model_op _dummy_vnd_mod_op[] = {
123 {BT_MESH_DUMMY_VND_MOD_GET_OP, BT_MESH_DUMMY_VND_MOD_MSG_MINLEN, get_handler},
124 {BT_MESH_DUMMY_VND_MOD_STATUS_OP, BT_MESH_DUMMY_VND_MOD_MSG_MINLEN, status_handler},
125 BT_MESH_MODEL_OP_END,
126 };
127
128 static struct bt_mesh_elem elements[] = {BT_MESH_ELEM(
129 0,
130 MODEL_LIST(BT_MESH_MODEL_CFG_SRV, BT_MESH_MODEL_CFG_CLI(&cfg_cli), BT_MESH_MODEL_OP_AGG_SRV,
131 BT_MESH_MODEL_OP_AGG_CLI),
132 MODEL_LIST(BT_MESH_MODEL_VND_CB(TEST_VND_COMPANY_ID, TEST_VND_MOD_ID, _dummy_vnd_mod_op,
133 NULL, NULL, NULL)))};
134
135 static const struct bt_mesh_comp comp = {
136 .cid = TEST_VND_COMPANY_ID,
137 .elem = elements,
138 .elem_count = ARRAY_SIZE(elements),
139 };
140
op_agg_test_prov_and_conf(uint16_t addr)141 static void op_agg_test_prov_and_conf(uint16_t addr)
142 {
143 uint8_t status;
144 int err;
145
146 ASSERT_OK(bt_mesh_provision(test_net_key, 0, 0, 0, addr, dev_key));
147
148 err = bt_mesh_cfg_cli_app_key_add(0, addr, 0, 0, test_app_key, &status);
149 if (err || status) {
150 FAIL("AppKey add failed (err %d, status %u)", err, status);
151 }
152
153 err = bt_mesh_cfg_cli_mod_app_bind(0, addr, addr, 0, BT_MESH_MODEL_ID_OP_AGG_CLI,
154 &status);
155 if (err || status) {
156 FAIL("Failed to bind OP_AGG_CLI to application (err %d, status %u)", err, status);
157 }
158 err = bt_mesh_cfg_cli_mod_app_bind(0, addr, addr, 0, BT_MESH_MODEL_ID_OP_AGG_SRV,
159 &status);
160 if (err || status) {
161 FAIL("Failed to bind OP_AGG_SRV to application (err %d, status %u)", err, status);
162 }
163 err = bt_mesh_cfg_cli_mod_app_bind_vnd(0, addr, addr, 0, TEST_VND_MOD_ID,
164 TEST_VND_COMPANY_ID, &status);
165 if (err || status) {
166 FAIL("Failed to bind OP_AGG_TEST_MOD to application (err %d, status %u)", err,
167 status);
168 }
169 }
170
test_cli_max_len_sequence_msg_send(void)171 static void test_cli_max_len_sequence_msg_send(void)
172 {
173 struct bt_mesh_model *dummy_vnd_model = &elements[0].vnd_models[0];
174 uint8_t seq;
175
176 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
177 bt_mesh_device_setup(&prov, &comp);
178 op_agg_test_prov_and_conf(CLI_ADDR);
179
180 ASSERT_OK(k_sem_init(&cli_suspend_sem, 0, 1));
181 ASSERT_OK(bt_mesh_op_agg_cli_seq_start(0, 0, SRV_ADDR, SRV_ADDR));
182
183 for (int i = 0; i < TEST_SEND_ITR; i++) {
184 seq = cli_sent_array[i] = i;
185 ASSERT_OK(dummy_vnd_mod_get(dummy_vnd_model, &test_ctx, seq));
186 }
187
188 ASSERT_OK(bt_mesh_op_agg_cli_seq_send());
189
190 /* Wait for all expected STATUS messages to be received */
191 if (k_sem_take(&cli_suspend_sem, SEM_TIMEOUT)) {
192 FAIL("Client suspension timed out. Status-messages received: %d",
193 status_rcvd_count);
194 }
195
196 if (memcmp(cli_sent_array, cli_rcvd_array, ARRAY_SIZE(cli_rcvd_array))) {
197 FAIL("Message arrays (sent / rcvd) are not equal.");
198 }
199
200 PASS();
201 }
202
test_srv_max_len_status_msg_send(void)203 static void test_srv_max_len_status_msg_send(void)
204 {
205 bt_mesh_test_cfg_set(NULL, WAIT_TIME);
206 bt_mesh_device_setup(&prov, &comp);
207 op_agg_test_prov_and_conf(SRV_ADDR);
208
209 ASSERT_OK(k_sem_init(&srv_suspend_sem, 0, 1));
210
211 /* Wait for all expected GET messages to be received */
212 if (k_sem_take(&srv_suspend_sem, SEM_TIMEOUT)) {
213 FAIL("Server suspension timed out. Get-messages received: %d", get_rcvd_count);
214 }
215
216 PASS();
217 }
218
219 #define TEST_CASE(role, name, description) \
220 { \
221 .test_id = "op_agg_" #role "_" #name, \
222 .test_descr = description, \
223 .test_tick_f = bt_mesh_test_timeout, \
224 .test_main_f = test_##role##_##name, \
225 }
226
227 static const struct bst_test_instance test_op_agg[] = {
228 TEST_CASE(cli, max_len_sequence_msg_send,
229 "OpAggCli composes a sequence request list, expecting a 380 Byte status message "
230 "in return."),
231 TEST_CASE(srv, max_len_status_msg_send,
232 "OpAggSrv will respond with a 380 Byte status message. "),
233
234 BSTEST_END_MARKER};
235
test_op_agg_install(struct bst_test_list * tests)236 struct bst_test_list *test_op_agg_install(struct bst_test_list *tests)
237 {
238 tests = bst_add_tests(tests, test_op_agg);
239 return tests;
240 }
241