1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <bluetooth/bluetooth.h>
8 
9 #include <assert.h>
10 #include <errno.h>
11 #include <bluetooth/mesh.h>
12 #include <bluetooth/testing.h>
13 #include <bluetooth/mesh/cfg.h>
14 #include <sys/byteorder.h>
15 #include <app_keys.h>
16 
17 #include <logging/log.h>
18 #define LOG_MODULE_NAME bttester_mesh
19 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
20 
21 #include "bttester.h"
22 
23 #define CONTROLLER_INDEX 0
24 #define CID_LOCAL 0x05F1
25 
26 /* Health server data */
27 #define CUR_FAULTS_MAX 4
28 #define HEALTH_TEST_ID 0x00
29 
30 static uint8_t cur_faults[CUR_FAULTS_MAX];
31 static uint8_t reg_faults[CUR_FAULTS_MAX * 2];
32 
33 /* Provision node data */
34 static uint8_t net_key[16];
35 static uint16_t net_key_idx;
36 static uint8_t flags;
37 static uint32_t iv_index;
38 static uint16_t addr;
39 static uint8_t dev_key[16];
40 static uint8_t input_size;
41 static uint8_t pub_key[64];
42 static uint8_t priv_key[32];
43 
44 /* Configured provisioning data */
45 static uint8_t dev_uuid[16];
46 static uint8_t static_auth[16];
47 
48 /* Vendor Model data */
49 #define VND_MODEL_ID_1 0x1234
50 static uint8_t vnd_app_key[16];
51 static uint16_t vnd_app_key_idx = 0x000f;
52 
53 /* Model send data */
54 #define MODEL_BOUNDS_MAX 2
55 
56 /* Model Authentication Method */
57 #define AUTH_METHOD_STATIC 0x01
58 #define AUTH_METHOD_OUTPUT 0x02
59 #define AUTH_METHOD_INPUT 0x03
60 
61 static struct model_data {
62 	struct bt_mesh_model *model;
63 	uint16_t addr;
64 	uint16_t appkey_idx;
65 } model_bound[MODEL_BOUNDS_MAX];
66 
67 static struct {
68 	uint16_t local;
69 	uint16_t dst;
70 	uint16_t net_idx;
71 } net = {
72 	.local = BT_MESH_ADDR_UNASSIGNED,
73 	.dst = BT_MESH_ADDR_UNASSIGNED,
74 };
75 
supported_commands(uint8_t * data,uint16_t len)76 static void supported_commands(uint8_t *data, uint16_t len)
77 {
78 	struct net_buf_simple *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE);
79 
80 	net_buf_simple_init(buf, 0);
81 	net_buf_simple_add_u8(buf, MESH_READ_SUPPORTED_COMMANDS);
82 	net_buf_simple_add_u8(buf, MESH_CONFIG_PROVISIONING);
83 	net_buf_simple_add_u8(buf, MESH_PROVISION_NODE);
84 	net_buf_simple_add_u8(buf, MESH_INIT);
85 	net_buf_simple_add_u8(buf, MESH_RESET);
86 	net_buf_simple_add_u8(buf, MESH_INPUT_NUMBER);
87 	net_buf_simple_add_u8(buf, MESH_INPUT_STRING);
88 	net_buf_simple_add_u8(buf, MESH_IVU_TEST_MODE);
89 	net_buf_simple_add_u8(buf, MESH_IVU_TOGGLE_STATE);
90 	net_buf_simple_add_u8(buf, MESH_NET_SEND);
91 	net_buf_simple_add_u8(buf, MESH_HEALTH_GENERATE_FAULTS);
92 	net_buf_simple_add_u8(buf, MESH_HEALTH_CLEAR_FAULTS);
93 	net_buf_simple_add_u8(buf, MESH_LPN);
94 	net_buf_simple_add_u8(buf, MESH_LPN_POLL);
95 	net_buf_simple_add_u8(buf, MESH_MODEL_SEND);
96 #if defined(CONFIG_BT_TESTING)
97 	net_buf_simple_add_u8(buf, MESH_LPN_SUBSCRIBE);
98 	net_buf_simple_add_u8(buf, MESH_LPN_UNSUBSCRIBE);
99 	net_buf_simple_add_u8(buf, MESH_RPL_CLEAR);
100 #endif /* CONFIG_BT_TESTING */
101 	net_buf_simple_add_u8(buf, MESH_PROXY_IDENTITY);
102 	net_buf_simple_add_u8(buf, MESH_COMP_DATA_GET);
103 	net_buf_simple_add_u8(buf, MESH_CFG_BEACON_GET);
104 	net_buf_simple_add_u8(buf, MESH_CFG_BEACON_SET);
105 	net_buf_simple_add_u8(buf, MESH_CFG_DEFAULT_TTL_GET);
106 	net_buf_simple_add_u8(buf, MESH_CFG_DEFAULT_TTL_SET);
107 	net_buf_simple_add_u8(buf, MESH_CFG_GATT_PROXY_GET);
108 	net_buf_simple_add_u8(buf, MESH_CFG_GATT_PROXY_SET);
109 	net_buf_simple_add_u8(buf, MESH_CFG_FRIEND_GET);
110 	net_buf_simple_add_u8(buf, MESH_CFG_FRIEND_SET);
111 	net_buf_simple_add_u8(buf, MESH_CFG_RELAY_GET);
112 	net_buf_simple_add_u8(buf, MESH_CFG_RELAY_SET);
113 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_PUB_GET);
114 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_PUB_SET);
115 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_ADD);
116 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_DEL);
117 	net_buf_simple_add_u8(buf, MESH_CFG_NETKEY_ADD);
118 	net_buf_simple_add_u8(buf, MESH_CFG_NETKEY_GET);
119 	net_buf_simple_add_u8(buf, MESH_CFG_NETKEY_DEL);
120 	net_buf_simple_add_u8(buf, MESH_CFG_APPKEY_ADD);
121 	net_buf_simple_add_u8(buf, MESH_CFG_APPKEY_DEL);
122 	net_buf_simple_add_u8(buf, MESH_CFG_APPKEY_GET);
123 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_APP_BIND);
124 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_APP_UNBIND);
125 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_APP_GET);
126 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_APP_VND_GET);
127 	net_buf_simple_add_u8(buf, MESH_CFG_HEARTBEAT_PUB_SET);
128 	net_buf_simple_add_u8(buf, MESH_CFG_HEARTBEAT_PUB_GET);
129 	net_buf_simple_add_u8(buf, MESH_CFG_HEARTBEAT_SUB_SET);
130 	net_buf_simple_add_u8(buf, MESH_CFG_HEARTBEAT_SUB_GET);
131 	net_buf_simple_add_u8(buf, MESH_CFG_NET_TRANS_GET);
132 	net_buf_simple_add_u8(buf, MESH_CFG_NET_TRANS_SET);
133 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_OVW);
134 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_DEL_ALL);
135 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_GET);
136 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_GET_VND);
137 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_VA_ADD);
138 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_VA_DEL);
139 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_SUB_VA_OVW);
140 	net_buf_simple_add_u8(buf, MESH_CFG_NETKEY_UPDATE);
141 	net_buf_simple_add_u8(buf, MESH_CFG_APPKEY_UPDATE);
142 	net_buf_simple_add_u8(buf, MESH_CFG_NODE_IDT_SET);
143 	net_buf_simple_add_u8(buf, MESH_CFG_NODE_IDT_GET);
144 	net_buf_simple_add_u8(buf, MESH_CFG_NODE_RESET);
145 	net_buf_simple_add_u8(buf, MESH_CFG_LPN_TIMEOUT_GET);
146 	net_buf_simple_add_u8(buf, MESH_CFG_MODEL_APP_BIND_VND);
147 	net_buf_simple_add_u8(buf, MESH_HEALTH_FAULT_GET);
148 	net_buf_simple_add_u8(buf, MESH_HEALTH_FAULT_CLEAR);
149 	net_buf_simple_add_u8(buf, MESH_HEALTH_PERIOD_GET);
150 	net_buf_simple_add_u8(buf, MESH_HEALTH_PERIOD_SET);
151 	net_buf_simple_add_u8(buf, MESH_HEALTH_ATTENTION_GET);
152 	net_buf_simple_add_u8(buf, MESH_HEALTH_ATTENTION_SET);
153 	net_buf_simple_add_u8(buf, MESH_PROVISION_ADV);
154 	net_buf_simple_add_u8(buf, MESH_CFG_KRP_GET);
155 	net_buf_simple_add_u8(buf, MESH_CFG_KRP_SET);
156 
157 	tester_send(BTP_SERVICE_ID_MESH, MESH_READ_SUPPORTED_COMMANDS,
158 		    CONTROLLER_INDEX, buf->data, buf->len);
159 }
160 
get_faults(uint8_t * faults,uint8_t faults_size,uint8_t * dst,uint8_t * count)161 static void get_faults(uint8_t *faults, uint8_t faults_size, uint8_t *dst, uint8_t *count)
162 {
163 	uint8_t i, limit = *count;
164 
165 	for (i = 0U, *count = 0U; i < faults_size && *count < limit; i++) {
166 		if (faults[i]) {
167 			*dst++ = faults[i];
168 			(*count)++;
169 		}
170 	}
171 }
172 
fault_get_cur(struct bt_mesh_model * model,uint8_t * test_id,uint16_t * company_id,uint8_t * faults,uint8_t * fault_count)173 static int fault_get_cur(struct bt_mesh_model *model, uint8_t *test_id,
174 			 uint16_t *company_id, uint8_t *faults, uint8_t *fault_count)
175 {
176 	LOG_DBG("");
177 
178 	*test_id = HEALTH_TEST_ID;
179 	*company_id = CID_LOCAL;
180 
181 	get_faults(cur_faults, sizeof(cur_faults), faults, fault_count);
182 
183 	return 0;
184 }
185 
fault_get_reg(struct bt_mesh_model * model,uint16_t company_id,uint8_t * test_id,uint8_t * faults,uint8_t * fault_count)186 static int fault_get_reg(struct bt_mesh_model *model, uint16_t company_id,
187 			 uint8_t *test_id, uint8_t *faults, uint8_t *fault_count)
188 {
189 	LOG_DBG("company_id 0x%04x", company_id);
190 
191 	if (company_id != CID_LOCAL) {
192 		return -EINVAL;
193 	}
194 
195 	*test_id = HEALTH_TEST_ID;
196 
197 	get_faults(reg_faults, sizeof(reg_faults), faults, fault_count);
198 
199 	return 0;
200 }
201 
fault_clear(struct bt_mesh_model * model,uint16_t company_id)202 static int fault_clear(struct bt_mesh_model *model, uint16_t company_id)
203 {
204 	LOG_DBG("company_id 0x%04x", company_id);
205 
206 	if (company_id != CID_LOCAL) {
207 		return -EINVAL;
208 	}
209 
210 	(void)memset(reg_faults, 0, sizeof(reg_faults));
211 
212 	return 0;
213 }
214 
fault_test(struct bt_mesh_model * model,uint8_t test_id,uint16_t company_id)215 static int fault_test(struct bt_mesh_model *model, uint8_t test_id,
216 		      uint16_t company_id)
217 {
218 	LOG_DBG("test_id 0x%02x company_id 0x%04x", test_id, company_id);
219 
220 	if (company_id != CID_LOCAL || test_id != HEALTH_TEST_ID) {
221 		return -EINVAL;
222 	}
223 
224 	return 0;
225 }
226 
227 static const struct bt_mesh_health_srv_cb health_srv_cb = {
228 	.fault_get_cur = fault_get_cur,
229 	.fault_get_reg = fault_get_reg,
230 	.fault_clear = fault_clear,
231 	.fault_test = fault_test,
232 };
233 
234 static struct bt_mesh_health_srv health_srv = {
235 	.cb = &health_srv_cb,
236 };
237 
238 BT_MESH_HEALTH_PUB_DEFINE(health_pub, CUR_FAULTS_MAX);
239 
240 static struct bt_mesh_cfg_cli cfg_cli = {
241 };
242 
show_faults(uint8_t test_id,uint16_t cid,uint8_t * faults,size_t fault_count)243 static void show_faults(uint8_t test_id, uint16_t cid, uint8_t *faults, size_t fault_count)
244 {
245 	size_t i;
246 
247 	if (!fault_count) {
248 		LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x: no faults",
249 			test_id, cid);
250 		return;
251 	}
252 
253 	LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x Fault Count %zu: ",
254 		test_id, cid, fault_count);
255 
256 	for (i = 0; i < fault_count; i++) {
257 		LOG_DBG("0x%02x", faults[i]);
258 	}
259 }
260 
health_current_status(struct bt_mesh_health_cli * cli,uint16_t addr,uint8_t test_id,uint16_t cid,uint8_t * faults,size_t fault_count)261 static void health_current_status(struct bt_mesh_health_cli *cli, uint16_t addr,
262 				  uint8_t test_id, uint16_t cid, uint8_t *faults,
263 				  size_t fault_count)
264 {
265 	LOG_DBG("Health Current Status from 0x%04x", addr);
266 	show_faults(test_id, cid, faults, fault_count);
267 }
268 
269 static struct bt_mesh_health_cli health_cli = {
270 	.current_status = health_current_status,
271 };
272 
273 static struct bt_mesh_model root_models[] = {
274 	BT_MESH_MODEL_CFG_SRV,
275 	BT_MESH_MODEL_CFG_CLI(&cfg_cli),
276 	BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
277 	BT_MESH_MODEL_HEALTH_CLI(&health_cli),
278 };
279 
280 static struct bt_mesh_model vnd_models[] = {
281 	BT_MESH_MODEL_VND(CID_LOCAL, VND_MODEL_ID_1, BT_MESH_MODEL_NO_OPS, NULL,
282 			  NULL),
283 };
284 
285 static struct bt_mesh_elem elements[] = {
286 	BT_MESH_ELEM(0, root_models, vnd_models),
287 };
288 
link_open(bt_mesh_prov_bearer_t bearer)289 static void link_open(bt_mesh_prov_bearer_t bearer)
290 {
291 	struct mesh_prov_link_open_ev ev;
292 
293 	LOG_DBG("bearer 0x%02x", bearer);
294 
295 	switch (bearer) {
296 	case BT_MESH_PROV_ADV:
297 		ev.bearer = MESH_PROV_BEARER_PB_ADV;
298 		break;
299 	case BT_MESH_PROV_GATT:
300 		ev.bearer = MESH_PROV_BEARER_PB_GATT;
301 		break;
302 	default:
303 		LOG_ERR("Invalid bearer");
304 
305 		return;
306 	}
307 
308 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROV_LINK_OPEN,
309 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
310 }
311 
link_close(bt_mesh_prov_bearer_t bearer)312 static void link_close(bt_mesh_prov_bearer_t bearer)
313 {
314 	struct mesh_prov_link_closed_ev ev;
315 
316 	LOG_DBG("bearer 0x%02x", bearer);
317 
318 	switch (bearer) {
319 	case BT_MESH_PROV_ADV:
320 		ev.bearer = MESH_PROV_BEARER_PB_ADV;
321 		break;
322 	case BT_MESH_PROV_GATT:
323 		ev.bearer = MESH_PROV_BEARER_PB_GATT;
324 		break;
325 	default:
326 		LOG_ERR("Invalid bearer");
327 
328 		return;
329 	}
330 
331 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROV_LINK_CLOSED,
332 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
333 }
334 
output_number(bt_mesh_output_action_t action,uint32_t number)335 static int output_number(bt_mesh_output_action_t action, uint32_t number)
336 {
337 	struct mesh_out_number_action_ev ev;
338 
339 	LOG_DBG("action 0x%04x number 0x%08x", action, number);
340 
341 	ev.action = sys_cpu_to_le16(action);
342 	ev.number = sys_cpu_to_le32(number);
343 
344 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_OUT_NUMBER_ACTION,
345 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
346 
347 	return 0;
348 }
349 
output_string(const char * str)350 static int output_string(const char *str)
351 {
352 	struct mesh_out_string_action_ev *ev;
353 	struct net_buf_simple *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE);
354 
355 	LOG_DBG("str %s", log_strdup(str));
356 
357 	net_buf_simple_init(buf, 0);
358 
359 	ev = net_buf_simple_add(buf, sizeof(*ev));
360 	ev->string_len = strlen(str);
361 
362 	net_buf_simple_add_mem(buf, str, ev->string_len);
363 
364 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_OUT_STRING_ACTION,
365 		    CONTROLLER_INDEX, buf->data, buf->len);
366 
367 	return 0;
368 }
369 
input(bt_mesh_input_action_t action,uint8_t size)370 static int input(bt_mesh_input_action_t action, uint8_t size)
371 {
372 	struct mesh_in_action_ev ev;
373 
374 	LOG_DBG("action 0x%04x number 0x%02x", action, size);
375 
376 	input_size = size;
377 
378 	ev.action = sys_cpu_to_le16(action);
379 	ev.size = size;
380 
381 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_IN_ACTION, CONTROLLER_INDEX,
382 		    (uint8_t *) &ev, sizeof(ev));
383 
384 	return 0;
385 }
386 
prov_complete(uint16_t net_idx,uint16_t addr)387 static void prov_complete(uint16_t net_idx, uint16_t addr)
388 {
389 	LOG_DBG("net_idx 0x%04x addr 0x%04x", net_idx, addr);
390 
391 	net.net_idx = net_idx,
392 	net.local = addr;
393 	net.dst = addr;
394 
395 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROVISIONED, CONTROLLER_INDEX,
396 		    NULL, 0);
397 }
398 
prov_node_added(uint16_t net_idx,uint8_t uuid[16],uint16_t addr,uint8_t num_elem)399 static void prov_node_added(uint16_t net_idx, uint8_t uuid[16], uint16_t addr,
400 			    uint8_t num_elem)
401 {
402 	struct mesh_prov_node_added_ev ev;
403 
404 	LOG_DBG("net_idx 0x%04x addr 0x%04x num_elem %d", net_idx, addr,
405 		num_elem);
406 
407 	ev.net_idx = net_idx;
408 	ev.addr = addr;
409 	ev.num_elems = num_elem;
410 	memcpy(&ev.uuid, uuid, sizeof(ev.uuid));
411 
412 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_PROV_NODE_ADDED,
413 		    CONTROLLER_INDEX, (void *)&ev, sizeof(ev));
414 }
415 
prov_reset(void)416 static void prov_reset(void)
417 {
418 	LOG_DBG("");
419 
420 	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
421 }
422 
423 static const struct bt_mesh_comp comp = {
424 	.cid = CID_LOCAL,
425 	.elem = elements,
426 	.elem_count = ARRAY_SIZE(elements),
427 };
428 
429 static struct bt_mesh_prov prov = {
430 	.uuid = dev_uuid,
431 	.static_val = static_auth,
432 	.static_val_len = sizeof(static_auth),
433 	.output_number = output_number,
434 	.output_string = output_string,
435 	.input = input,
436 	.link_open = link_open,
437 	.link_close = link_close,
438 	.complete = prov_complete,
439 	.node_added = prov_node_added,
440 	.reset = prov_reset,
441 };
442 
config_prov(uint8_t * data,uint16_t len)443 static void config_prov(uint8_t *data, uint16_t len)
444 {
445 	const struct mesh_config_provisioning_cmd *cmd = (void *) data;
446 	int err = 0;
447 
448 	LOG_DBG("");
449 
450 	memcpy(dev_uuid, cmd->uuid, sizeof(dev_uuid));
451 	memcpy(static_auth, cmd->static_auth, sizeof(static_auth));
452 
453 	prov.output_size = cmd->out_size;
454 	prov.output_actions = sys_le16_to_cpu(cmd->out_actions);
455 	prov.input_size = cmd->in_size;
456 	prov.input_actions = sys_le16_to_cpu(cmd->in_actions);
457 
458 	if (cmd->auth_method == AUTH_METHOD_OUTPUT) {
459 		err = bt_mesh_auth_method_set_output(prov.output_actions, prov.output_size);
460 	} else if (cmd->auth_method == AUTH_METHOD_INPUT) {
461 		err = bt_mesh_auth_method_set_input(prov.input_actions, prov.input_size);
462 	} else if (cmd->auth_method == AUTH_METHOD_STATIC) {
463 		err = bt_mesh_auth_method_set_static(static_auth, sizeof(static_auth));
464 	}
465 
466 	if (len > sizeof(*cmd)) {
467 		memcpy(pub_key, cmd->set_keys->pub_key, sizeof(cmd->set_keys->pub_key));
468 		memcpy(priv_key, cmd->set_keys->priv_key, sizeof(cmd->set_keys->priv_key));
469 		prov.public_key_be = pub_key;
470 		prov.private_key_be = priv_key;
471 	}
472 
473 	if (err) {
474 		LOG_ERR("err %d", err);
475 	}
476 
477 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CONFIG_PROVISIONING, CONTROLLER_INDEX,
478 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
479 }
480 
provision_node(uint8_t * data,uint16_t len)481 static void provision_node(uint8_t *data, uint16_t len)
482 {
483 	const struct mesh_provision_node_cmd *cmd = (void *)data;
484 	int err;
485 
486 	LOG_DBG("");
487 
488 	memcpy(dev_key, cmd->dev_key, sizeof(dev_key));
489 	memcpy(net_key, cmd->net_key, sizeof(net_key));
490 
491 	addr = sys_le16_to_cpu(cmd->addr);
492 	flags = cmd->flags;
493 	iv_index = sys_le32_to_cpu(cmd->iv_index);
494 	net_key_idx = sys_le16_to_cpu(cmd->net_key_idx);
495 
496 	if (len > sizeof(*cmd)) {
497 		memcpy(pub_key, cmd->pub_key, sizeof(pub_key));
498 
499 		err = bt_mesh_prov_remote_pub_key_set(pub_key);
500 		if (err) {
501 			LOG_ERR("err %d", err);
502 			goto fail;
503 		}
504 	}
505 #if defined(CONFIG_BT_MESH_PROVISIONER)
506 	err = bt_mesh_cdb_create(net_key);
507 	if (err) {
508 		LOG_ERR("err %d", err);
509 		goto fail;
510 	}
511 #endif
512 	err = bt_mesh_provision(net_key, net_key_idx, flags, iv_index, addr,
513 				dev_key);
514 	if (err) {
515 		LOG_ERR("err %d", err);
516 		goto fail;
517 	}
518 
519 fail:
520 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_PROVISION_NODE, CONTROLLER_INDEX,
521 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
522 }
523 
provision_adv(uint8_t * data,uint16_t len)524 static void provision_adv(uint8_t *data, uint16_t len)
525 {
526 	const struct mesh_provision_adv_cmd *cmd = (void *)data;
527 	int err;
528 
529 	LOG_DBG("");
530 
531 	err = bt_mesh_provision_adv(cmd->uuid, cmd->net_idx, cmd->address,
532 				    cmd->attention_duration);
533 	if (err) {
534 		LOG_ERR("err %d", err);
535 		goto fail;
536 	}
537 
538 fail:
539 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_PROVISION_ADV, CONTROLLER_INDEX,
540 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
541 }
542 
init(uint8_t * data,uint16_t len)543 static void init(uint8_t *data, uint16_t len)
544 {
545 	uint8_t status = BTP_STATUS_SUCCESS;
546 	int err;
547 
548 	LOG_DBG("");
549 
550 	err = bt_mesh_init(&prov, &comp);
551 	if (err) {
552 		status = BTP_STATUS_FAILED;
553 
554 		goto rsp;
555 	}
556 
557 	if (addr) {
558 		err = bt_mesh_provision(net_key, net_key_idx, flags, iv_index,
559 					addr, dev_key);
560 		if (err) {
561 			status = BTP_STATUS_FAILED;
562 		}
563 	} else {
564 		err = bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
565 		if (err) {
566 			status = BTP_STATUS_FAILED;
567 		}
568 	}
569 
570 rsp:
571 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_INIT, CONTROLLER_INDEX,
572 		   status);
573 }
574 
reset(uint8_t * data,uint16_t len)575 static void reset(uint8_t *data, uint16_t len)
576 {
577 	LOG_DBG("");
578 
579 	bt_mesh_reset();
580 
581 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_RESET, CONTROLLER_INDEX,
582 		   BTP_STATUS_SUCCESS);
583 }
584 
input_number(uint8_t * data,uint16_t len)585 static void input_number(uint8_t *data, uint16_t len)
586 {
587 	const struct mesh_input_number_cmd *cmd = (void *) data;
588 	uint8_t status = BTP_STATUS_SUCCESS;
589 	uint32_t number;
590 	int err;
591 
592 	number = sys_le32_to_cpu(cmd->number);
593 
594 	LOG_DBG("number 0x%04x", number);
595 
596 	err = bt_mesh_input_number(number);
597 	if (err) {
598 		status = BTP_STATUS_FAILED;
599 	}
600 
601 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_INPUT_NUMBER, CONTROLLER_INDEX,
602 		   status);
603 }
604 
input_string(uint8_t * data,uint16_t len)605 static void input_string(uint8_t *data, uint16_t len)
606 {
607 	const struct mesh_input_string_cmd *cmd = (void *) data;
608 	uint8_t status = BTP_STATUS_SUCCESS;
609 	uint8_t str_auth[16];
610 	int err;
611 
612 	LOG_DBG("");
613 
614 	if (cmd->string_len > sizeof(str_auth)) {
615 		LOG_ERR("Too long input (%u chars required)", input_size);
616 		status = BTP_STATUS_FAILED;
617 		goto rsp;
618 	} else if (cmd->string_len < input_size) {
619 		LOG_ERR("Too short input (%u chars required)", input_size);
620 		status = BTP_STATUS_FAILED;
621 		goto rsp;
622 	}
623 
624 	strncpy(str_auth, cmd->string, cmd->string_len);
625 
626 	err = bt_mesh_input_string(str_auth);
627 	if (err) {
628 		status = BTP_STATUS_FAILED;
629 	}
630 
631 rsp:
632 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_INPUT_STRING, CONTROLLER_INDEX,
633 		   status);
634 }
635 
ivu_test_mode(uint8_t * data,uint16_t len)636 static void ivu_test_mode(uint8_t *data, uint16_t len)
637 {
638 	const struct mesh_ivu_test_mode_cmd *cmd = (void *) data;
639 
640 	LOG_DBG("enable 0x%02x", cmd->enable);
641 
642 	bt_mesh_iv_update_test(cmd->enable ? true : false);
643 
644 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_IVU_TEST_MODE, CONTROLLER_INDEX,
645 		   BTP_STATUS_SUCCESS);
646 }
647 
ivu_toggle_state(uint8_t * data,uint16_t len)648 static void ivu_toggle_state(uint8_t *data, uint16_t len)
649 {
650 	bool result;
651 
652 	LOG_DBG("");
653 
654 	result = bt_mesh_iv_update();
655 	if (!result) {
656 		LOG_ERR("Failed to toggle the IV Update state");
657 	}
658 
659 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_IVU_TOGGLE_STATE, CONTROLLER_INDEX,
660 		   result ? BTP_STATUS_SUCCESS : BTP_STATUS_FAILED);
661 }
662 
lpn(uint8_t * data,uint16_t len)663 static void lpn(uint8_t *data, uint16_t len)
664 {
665 	struct mesh_lpn_set_cmd *cmd = (void *) data;
666 	bool enable;
667 	int err;
668 
669 	LOG_DBG("enable 0x%02x", cmd->enable);
670 
671 	enable = cmd->enable ? true : false;
672 	err = bt_mesh_lpn_set(enable);
673 	if (err) {
674 		LOG_ERR("Failed to toggle LPN (err %d)", err);
675 	}
676 
677 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN, CONTROLLER_INDEX,
678 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
679 }
680 
lpn_poll(uint8_t * data,uint16_t len)681 static void lpn_poll(uint8_t *data, uint16_t len)
682 {
683 	int err;
684 
685 	LOG_DBG("");
686 
687 	err = bt_mesh_lpn_poll();
688 	if (err) {
689 		LOG_ERR("Failed to send poll msg (err %d)", err);
690 	}
691 
692 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_POLL, CONTROLLER_INDEX,
693 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
694 }
695 
net_send(uint8_t * data,uint16_t len)696 static void net_send(uint8_t *data, uint16_t len)
697 {
698 	struct mesh_net_send_cmd *cmd = (void *) data;
699 	NET_BUF_SIMPLE_DEFINE(msg, UINT8_MAX);
700 	struct bt_mesh_msg_ctx ctx = {
701 		.net_idx = net.net_idx,
702 		.app_idx = vnd_app_key_idx,
703 		.addr = sys_le16_to_cpu(cmd->dst),
704 		.send_ttl = cmd->ttl,
705 	};
706 	int err;
707 
708 	LOG_DBG("ttl 0x%02x dst 0x%04x payload_len %d", ctx.send_ttl,
709 		ctx.addr, cmd->payload_len);
710 
711 	if (!bt_mesh_app_key_exists(vnd_app_key_idx)) {
712 		(void)bt_mesh_app_key_add(vnd_app_key_idx, net.net_idx,
713 					  vnd_app_key);
714 		vnd_models[0].keys[0] = vnd_app_key_idx;
715 	}
716 
717 	net_buf_simple_add_mem(&msg, cmd->payload, cmd->payload_len);
718 
719 	err = bt_mesh_model_send(&vnd_models[0], &ctx, &msg, NULL, NULL);
720 	if (err) {
721 		LOG_ERR("Failed to send (err %d)", err);
722 	}
723 
724 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_NET_SEND, CONTROLLER_INDEX,
725 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
726 }
727 
health_generate_faults(uint8_t * data,uint16_t len)728 static void health_generate_faults(uint8_t *data, uint16_t len)
729 {
730 	struct mesh_health_generate_faults_rp *rp;
731 	NET_BUF_SIMPLE_DEFINE(buf, sizeof(*rp) + sizeof(cur_faults) +
732 			      sizeof(reg_faults));
733 	uint8_t some_faults[] = { 0x01, 0x02, 0x03, 0xff, 0x06 };
734 	uint8_t cur_faults_count, reg_faults_count;
735 
736 	rp = net_buf_simple_add(&buf, sizeof(*rp));
737 
738 	cur_faults_count = MIN(sizeof(cur_faults), sizeof(some_faults));
739 	memcpy(cur_faults, some_faults, cur_faults_count);
740 	net_buf_simple_add_mem(&buf, cur_faults, cur_faults_count);
741 	rp->cur_faults_count = cur_faults_count;
742 
743 	reg_faults_count = MIN(sizeof(reg_faults), sizeof(some_faults));
744 	memcpy(reg_faults, some_faults, reg_faults_count);
745 	net_buf_simple_add_mem(&buf, reg_faults, reg_faults_count);
746 	rp->reg_faults_count = reg_faults_count;
747 
748 	bt_mesh_fault_update(&elements[0]);
749 
750 	tester_send(BTP_SERVICE_ID_MESH, MESH_HEALTH_GENERATE_FAULTS,
751 		    CONTROLLER_INDEX, buf.data, buf.len);
752 }
753 
health_clear_faults(uint8_t * data,uint16_t len)754 static void health_clear_faults(uint8_t *data, uint16_t len)
755 {
756 	LOG_DBG("");
757 
758 	(void)memset(cur_faults, 0, sizeof(cur_faults));
759 	(void)memset(reg_faults, 0, sizeof(reg_faults));
760 
761 	bt_mesh_fault_update(&elements[0]);
762 
763 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_CLEAR_FAULTS,
764 		   CONTROLLER_INDEX, BTP_STATUS_SUCCESS);
765 }
766 
model_send(uint8_t * data,uint16_t len)767 static void model_send(uint8_t *data, uint16_t len)
768 {
769 	struct mesh_model_send_cmd *cmd = (void *) data;
770 	NET_BUF_SIMPLE_DEFINE(msg, UINT8_MAX);
771 	struct bt_mesh_msg_ctx ctx = {
772 		.net_idx = net.net_idx,
773 		.app_idx = BT_MESH_KEY_DEV,
774 		.addr = sys_le16_to_cpu(cmd->dst),
775 		.send_ttl = BT_MESH_TTL_DEFAULT,
776 	};
777 	struct bt_mesh_model *model = NULL;
778 	int err, i;
779 	uint16_t src = sys_le16_to_cpu(cmd->src);
780 
781 	/* Lookup source address */
782 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
783 		if (bt_mesh_model_elem(model_bound[i].model)->addr == src) {
784 			model = model_bound[i].model;
785 			ctx.app_idx = model_bound[i].appkey_idx;
786 
787 			break;
788 		}
789 	}
790 
791 	if (!model) {
792 		LOG_ERR("Model not found");
793 		err = -EINVAL;
794 
795 		goto fail;
796 	}
797 
798 	LOG_DBG("src 0x%04x dst 0x%04x model %p payload_len %d", src,
799 		ctx.addr, model, cmd->payload_len);
800 
801 	net_buf_simple_add_mem(&msg, cmd->payload, cmd->payload_len);
802 
803 	err = bt_mesh_model_send(model, &ctx, &msg, NULL, NULL);
804 	if (err) {
805 		LOG_ERR("Failed to send (err %d)", err);
806 	}
807 
808 fail:
809 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_MODEL_SEND, CONTROLLER_INDEX,
810 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
811 }
812 
813 #if defined(CONFIG_BT_TESTING)
lpn_subscribe(uint8_t * data,uint16_t len)814 static void lpn_subscribe(uint8_t *data, uint16_t len)
815 {
816 	struct mesh_lpn_subscribe_cmd *cmd = (void *) data;
817 	uint16_t address = sys_le16_to_cpu(cmd->address);
818 	int err;
819 
820 	LOG_DBG("address 0x%04x", address);
821 
822 	err = bt_test_mesh_lpn_group_add(address);
823 	if (err) {
824 		LOG_ERR("Failed to subscribe (err %d)", err);
825 	}
826 
827 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_SUBSCRIBE, CONTROLLER_INDEX,
828 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
829 }
830 
lpn_unsubscribe(uint8_t * data,uint16_t len)831 static void lpn_unsubscribe(uint8_t *data, uint16_t len)
832 {
833 	struct mesh_lpn_unsubscribe_cmd *cmd = (void *) data;
834 	uint16_t address = sys_le16_to_cpu(cmd->address);
835 	int err;
836 
837 	LOG_DBG("address 0x%04x", address);
838 
839 	err = bt_test_mesh_lpn_group_remove(&address, 1);
840 	if (err) {
841 		LOG_ERR("Failed to unsubscribe (err %d)", err);
842 	}
843 
844 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_LPN_UNSUBSCRIBE, CONTROLLER_INDEX,
845 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
846 }
847 
rpl_clear(uint8_t * data,uint16_t len)848 static void rpl_clear(uint8_t *data, uint16_t len)
849 {
850 	int err;
851 
852 	LOG_DBG("");
853 
854 	err = bt_test_mesh_rpl_clear();
855 	if (err) {
856 		LOG_ERR("Failed to clear RPL (err %d)", err);
857 	}
858 
859 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_RPL_CLEAR, CONTROLLER_INDEX,
860 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
861 }
862 #endif /* CONFIG_BT_TESTING */
863 
proxy_identity_enable(uint8_t * data,uint16_t len)864 static void proxy_identity_enable(uint8_t *data, uint16_t len)
865 {
866 	int err;
867 
868 	LOG_DBG("");
869 
870 	err = bt_mesh_proxy_identity_enable();
871 	if (err) {
872 		LOG_ERR("Failed to enable proxy identity (err %d)", err);
873 	}
874 
875 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_PROXY_IDENTITY, CONTROLLER_INDEX,
876 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
877 }
878 
composition_data_get(uint8_t * data,uint16_t len)879 static void composition_data_get(uint8_t *data, uint16_t len)
880 {
881 	struct mesh_comp_data_get_cmd *cmd = (void *)data;
882 	uint8_t page;
883 	struct net_buf_simple *comp = NET_BUF_SIMPLE(128);
884 	int err;
885 
886 	LOG_DBG("");
887 
888 	net_buf_simple_init(comp, 0);
889 
890 	err = bt_mesh_cfg_comp_data_get(cmd->net_idx, cmd->address, cmd->page,
891 					&page, comp);
892 	if (err) {
893 		LOG_ERR("err %d", err);
894 		goto fail;
895 	}
896 
897 	tester_send(BTP_SERVICE_ID_MESH, MESH_COMP_DATA_GET, CONTROLLER_INDEX,
898 		    comp->data, comp->len);
899 	return;
900 
901 fail:
902 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_COMP_DATA_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
903 }
904 
config_krp_get(uint8_t * data,uint16_t len)905 static void config_krp_get(uint8_t *data, uint16_t len)
906 {
907 	struct mesh_cfg_krp_get_cmd *cmd = (void *)data;
908 	struct net_buf_simple *buf = NET_BUF_SIMPLE(2);
909 	uint8_t status;
910 	uint8_t phase;
911 	int err;
912 
913 	LOG_DBG("");
914 
915 	err = bt_mesh_cfg_krp_get(cmd->net_idx, cmd->address, cmd->key_net_idx, &status, &phase);
916 
917 	if (err) {
918 		LOG_ERR("err %d", err);
919 		goto fail;
920 	}
921 
922 	net_buf_simple_init(buf, 0);
923 	net_buf_simple_add_u8(buf, status);
924 	net_buf_simple_add_u8(buf, phase);
925 
926 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_KRP_GET, CONTROLLER_INDEX, buf->data, buf->len);
927 	return;
928 
929 fail:
930 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_KRP_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
931 }
932 
config_krp_set(uint8_t * data,uint16_t len)933 static void config_krp_set(uint8_t *data, uint16_t len)
934 {
935 	struct mesh_cfg_krp_set_cmd *cmd = (void *)data;
936 	struct net_buf_simple *buf = NET_BUF_SIMPLE(2);
937 	uint8_t status;
938 	uint8_t phase;
939 	int err;
940 
941 	LOG_DBG("");
942 
943 	err = bt_mesh_cfg_krp_set(cmd->net_idx, cmd->address, cmd->key_net_idx, cmd->transition,
944 				  &status, &phase);
945 	if (err) {
946 		LOG_ERR("err %d", err);
947 		goto fail;
948 	}
949 
950 	net_buf_simple_init(buf, 0);
951 	net_buf_simple_add_u8(buf, status);
952 	net_buf_simple_add_u8(buf, phase);
953 
954 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_KRP_SET, CONTROLLER_INDEX, buf->data, buf->len);
955 	return;
956 
957 fail:
958 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_KRP_SET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
959 }
960 
config_beacon_get(uint8_t * data,uint16_t len)961 static void config_beacon_get(uint8_t *data, uint16_t len)
962 {
963 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
964 	uint8_t status;
965 	int err;
966 
967 	LOG_DBG("");
968 
969 	err = bt_mesh_cfg_beacon_get(cmd->net_idx, cmd->address, &status);
970 	if (err) {
971 		LOG_ERR("err %d", err);
972 		goto fail;
973 	}
974 
975 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_BEACON_GET, CONTROLLER_INDEX,
976 		    &status, sizeof(status));
977 	return;
978 
979 fail:
980 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_BEACON_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
981 }
982 
config_beacon_set(uint8_t * data,uint16_t len)983 static void config_beacon_set(uint8_t *data, uint16_t len)
984 {
985 	struct mesh_cfg_beacon_set_cmd *cmd = (void *)data;
986 	uint8_t status;
987 	int err;
988 
989 	LOG_DBG("");
990 
991 	err = bt_mesh_cfg_beacon_set(cmd->net_idx, cmd->address, cmd->val,
992 				     &status);
993 	if (err) {
994 		LOG_ERR("err %d", err);
995 		goto fail;
996 	}
997 
998 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_BEACON_SET, CONTROLLER_INDEX,
999 		    &status, sizeof(status));
1000 	return;
1001 
1002 fail:
1003 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_BEACON_SET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1004 }
1005 
config_default_ttl_get(uint8_t * data,uint16_t len)1006 static void config_default_ttl_get(uint8_t *data, uint16_t len)
1007 {
1008 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1009 	uint8_t status;
1010 	int err;
1011 
1012 	LOG_DBG("");
1013 	err = bt_mesh_cfg_ttl_get(cmd->net_idx, cmd->address, &status);
1014 
1015 	if (err) {
1016 		LOG_ERR("err %d", err);
1017 		goto fail;
1018 	}
1019 
1020 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_DEFAULT_TTL_GET,
1021 		    CONTROLLER_INDEX, &status, sizeof(status));
1022 	return;
1023 
1024 fail:
1025 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_DEFAULT_TTL_GET, CONTROLLER_INDEX,
1026 		   BTP_STATUS_FAILED);
1027 }
1028 
config_default_ttl_set(uint8_t * data,uint16_t len)1029 static void config_default_ttl_set(uint8_t *data, uint16_t len)
1030 {
1031 	struct mesh_cfg_default_ttl_set_cmd *cmd = (void *)data;
1032 	uint8_t status;
1033 	int err;
1034 
1035 	LOG_DBG("");
1036 	err = bt_mesh_cfg_ttl_set(cmd->net_idx, cmd->address, cmd->val,
1037 				  &status);
1038 
1039 	if (err) {
1040 		LOG_ERR("err %d", err);
1041 		goto fail;
1042 	}
1043 
1044 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_DEFAULT_TTL_SET,
1045 		    CONTROLLER_INDEX, &status, sizeof(status));
1046 	return;
1047 
1048 fail:
1049 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_DEFAULT_TTL_SET, CONTROLLER_INDEX,
1050 		   BTP_STATUS_FAILED);
1051 }
1052 
config_gatt_proxy_get(uint8_t * data,uint16_t len)1053 static void config_gatt_proxy_get(uint8_t *data, uint16_t len)
1054 {
1055 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1056 	uint8_t status;
1057 	int err;
1058 
1059 	LOG_DBG("");
1060 	err = bt_mesh_cfg_gatt_proxy_get(cmd->net_idx, cmd->address, &status);
1061 
1062 	if (err) {
1063 		LOG_ERR("err %d", err);
1064 		goto fail;
1065 	}
1066 
1067 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_GATT_PROXY_GET,
1068 		    CONTROLLER_INDEX, &status, sizeof(status));
1069 	return;
1070 
1071 fail:
1072 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_GATT_PROXY_GET, CONTROLLER_INDEX,
1073 		   BTP_STATUS_FAILED);
1074 }
1075 
config_gatt_proxy_set(uint8_t * data,uint16_t len)1076 static void config_gatt_proxy_set(uint8_t *data, uint16_t len)
1077 {
1078 	struct mesh_cfg_gatt_proxy_set_cmd *cmd = (void *)data;
1079 	uint8_t status;
1080 	int err;
1081 
1082 	LOG_DBG("");
1083 	err = bt_mesh_cfg_gatt_proxy_set(cmd->net_idx, cmd->address, cmd->val,
1084 					 &status);
1085 
1086 	if (err) {
1087 		LOG_ERR("err %d", err);
1088 		goto fail;
1089 	}
1090 
1091 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_GATT_PROXY_SET,
1092 		    CONTROLLER_INDEX, &status, sizeof(status));
1093 	return;
1094 
1095 fail:
1096 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_GATT_PROXY_SET, CONTROLLER_INDEX,
1097 		   BTP_STATUS_FAILED);
1098 }
1099 
config_friend_get(uint8_t * data,uint16_t len)1100 static void config_friend_get(uint8_t *data, uint16_t len)
1101 {
1102 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1103 	uint8_t status;
1104 	int err;
1105 
1106 	LOG_DBG("");
1107 	err = bt_mesh_cfg_friend_get(cmd->net_idx, cmd->address, &status);
1108 
1109 	if (err) {
1110 		LOG_ERR("err %d", err);
1111 		goto fail;
1112 	}
1113 
1114 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_FRIEND_GET, CONTROLLER_INDEX,
1115 		    &status, sizeof(status));
1116 	return;
1117 
1118 fail:
1119 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_FRIEND_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1120 }
1121 
config_friend_set(uint8_t * data,uint16_t len)1122 static void config_friend_set(uint8_t *data, uint16_t len)
1123 {
1124 	struct mesh_cfg_friend_set_cmd *cmd = (void *)data;
1125 	uint8_t status;
1126 	int err;
1127 
1128 	LOG_DBG("");
1129 	err = bt_mesh_cfg_friend_set(cmd->net_idx, cmd->address, cmd->val,
1130 				     &status);
1131 
1132 	if (err) {
1133 		LOG_ERR("err %d", err);
1134 		goto fail;
1135 	}
1136 
1137 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_FRIEND_SET, CONTROLLER_INDEX,
1138 		    &status, sizeof(status));
1139 	return;
1140 
1141 fail:
1142 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_FRIEND_SET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1143 }
1144 
config_relay_get(uint8_t * data,uint16_t len)1145 static void config_relay_get(uint8_t *data, uint16_t len)
1146 {
1147 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1148 	uint8_t status;
1149 	uint8_t transmit;
1150 	int err;
1151 
1152 	LOG_DBG("");
1153 	err = bt_mesh_cfg_relay_get(cmd->net_idx, cmd->address, &status,
1154 				    &transmit);
1155 
1156 	if (err) {
1157 		LOG_ERR("err %d", err);
1158 		goto fail;
1159 	}
1160 
1161 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_RELAY_GET, CONTROLLER_INDEX,
1162 		    &status, sizeof(status));
1163 	return;
1164 
1165 fail:
1166 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_RELAY_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1167 }
1168 
config_relay_set(uint8_t * data,uint16_t len)1169 static void config_relay_set(uint8_t *data, uint16_t len)
1170 {
1171 	struct mesh_cfg_relay_set_cmd *cmd = (void *)data;
1172 	uint8_t status;
1173 	uint8_t transmit;
1174 	int err;
1175 
1176 	LOG_DBG("");
1177 	err = bt_mesh_cfg_relay_set(cmd->net_idx, cmd->address, cmd->new_relay,
1178 				    cmd->new_transmit, &status, &transmit);
1179 
1180 	if (err) {
1181 		LOG_ERR("err %d", err);
1182 		goto fail;
1183 	}
1184 
1185 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_RELAY_SET, CONTROLLER_INDEX,
1186 		    &status, sizeof(status));
1187 	return;
1188 
1189 fail:
1190 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_RELAY_SET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1191 }
1192 
config_mod_pub_get(uint8_t * data,uint16_t len)1193 static void config_mod_pub_get(uint8_t *data, uint16_t len)
1194 {
1195 	struct mesh_cfg_model_pub_get_cmd *cmd = (void *)data;
1196 	struct bt_mesh_cfg_mod_pub pub;
1197 	uint8_t status;
1198 	int err;
1199 
1200 	LOG_DBG("");
1201 	err = bt_mesh_cfg_mod_pub_get(cmd->net_idx, cmd->address,
1202 				      cmd->elem_address, cmd->model_id, &pub,
1203 				      &status);
1204 
1205 	if (err) {
1206 		LOG_ERR("err %d", err);
1207 		goto fail;
1208 	}
1209 
1210 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_GET,
1211 		    CONTROLLER_INDEX, &status, sizeof(status));
1212 	return;
1213 
1214 fail:
1215 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_GET, CONTROLLER_INDEX,
1216 		   BTP_STATUS_FAILED);
1217 }
1218 
config_mod_pub_set(uint8_t * data,uint16_t len)1219 static void config_mod_pub_set(uint8_t *data, uint16_t len)
1220 {
1221 	struct mesh_cfg_model_pub_set_cmd *cmd = (void *)data;
1222 	uint8_t status;
1223 	struct bt_mesh_cfg_mod_pub pub;
1224 	int err;
1225 
1226 	LOG_DBG("");
1227 
1228 	pub.addr = cmd->pub_addr;
1229 	pub.uuid = NULL;
1230 	pub.app_idx = cmd->app_idx;
1231 	pub.cred_flag = cmd->cred_flag;
1232 	pub.ttl = cmd->ttl;
1233 	pub.period = cmd->period;
1234 	pub.transmit = cmd->transmit;
1235 
1236 	err = bt_mesh_cfg_mod_pub_set(cmd->net_idx, cmd->address,
1237 				      cmd->elem_address, cmd->model_id, &pub,
1238 				      &status);
1239 
1240 	if (err) {
1241 		LOG_ERR("err %d", err);
1242 		goto fail;
1243 	}
1244 
1245 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_SET,
1246 		    CONTROLLER_INDEX, &status, sizeof(status));
1247 	return;
1248 
1249 fail:
1250 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_SET, CONTROLLER_INDEX,
1251 		   BTP_STATUS_FAILED);
1252 }
1253 
config_mod_pub_va_set(uint8_t * data,uint16_t len)1254 static void config_mod_pub_va_set(uint8_t *data, uint16_t len)
1255 {
1256 	struct mesh_cfg_model_pub_va_set_cmd *cmd = (void *)data;
1257 	uint8_t status;
1258 	struct bt_mesh_cfg_mod_pub pub;
1259 	int err;
1260 
1261 	LOG_DBG("");
1262 
1263 	pub.uuid = cmd->uuid;
1264 	pub.app_idx = cmd->app_idx;
1265 	pub.cred_flag = cmd->cred_flag;
1266 	pub.ttl = cmd->ttl;
1267 	pub.period = cmd->period;
1268 	pub.transmit = cmd->transmit;
1269 
1270 	err = bt_mesh_cfg_mod_pub_set(cmd->net_idx, cmd->address,
1271 				      cmd->elem_address, cmd->model_id,
1272 				      &pub, &status);
1273 
1274 	if (err) {
1275 		LOG_ERR("err %d", err);
1276 		goto fail;
1277 	}
1278 
1279 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_VA_SET,
1280 		    CONTROLLER_INDEX, &status, sizeof(status));
1281 	return;
1282 
1283 fail:
1284 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_PUB_VA_SET, CONTROLLER_INDEX,
1285 		   BTP_STATUS_FAILED);
1286 }
1287 
config_mod_sub_add(uint8_t * data,uint16_t len)1288 static void config_mod_sub_add(uint8_t *data, uint16_t len)
1289 {
1290 	struct mesh_cfg_model_sub_cmd *cmd = (void *)data;
1291 	uint8_t status;
1292 	int err;
1293 
1294 	LOG_DBG("");
1295 
1296 	err = bt_mesh_cfg_mod_sub_add(cmd->net_idx, cmd->address,
1297 				      cmd->elem_address, cmd->sub_addr,
1298 				      cmd->model_id, &status);
1299 
1300 	if (err) {
1301 		LOG_ERR("err %d", err);
1302 		goto fail;
1303 	}
1304 
1305 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_ADD,
1306 		    CONTROLLER_INDEX, &status, sizeof(status));
1307 	return;
1308 
1309 fail:
1310 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_ADD, CONTROLLER_INDEX,
1311 		   BTP_STATUS_FAILED);
1312 }
1313 
config_mod_sub_ovw(uint8_t * data,uint16_t len)1314 static void config_mod_sub_ovw(uint8_t *data, uint16_t len)
1315 {
1316 	struct mesh_cfg_model_sub_cmd *cmd = (void *)data;
1317 	uint8_t status;
1318 	int err;
1319 
1320 	LOG_DBG("");
1321 
1322 	err = bt_mesh_cfg_mod_sub_overwrite(cmd->net_idx, cmd->address,
1323 					    cmd->elem_address, cmd->sub_addr,
1324 					    cmd->model_id, &status);
1325 
1326 	if (err) {
1327 		LOG_ERR("err %d", err);
1328 		goto fail;
1329 	}
1330 
1331 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_OVW,
1332 		    CONTROLLER_INDEX, &status, sizeof(status));
1333 	return;
1334 
1335 fail:
1336 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_OVW, CONTROLLER_INDEX,
1337 		   BTP_STATUS_FAILED);
1338 }
1339 
config_mod_sub_del(uint8_t * data,uint16_t len)1340 static void config_mod_sub_del(uint8_t *data, uint16_t len)
1341 {
1342 	struct mesh_cfg_model_sub_cmd *cmd = (void *)data;
1343 	uint8_t status;
1344 	int err;
1345 
1346 	LOG_DBG("");
1347 
1348 	err = bt_mesh_cfg_mod_sub_del(cmd->net_idx, cmd->address,
1349 				      cmd->elem_address, cmd->sub_addr,
1350 				      cmd->model_id, &status);
1351 
1352 	if (err) {
1353 		LOG_ERR("err %d", err);
1354 		goto fail;
1355 	}
1356 
1357 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_DEL,
1358 		    CONTROLLER_INDEX, &status, sizeof(status));
1359 	return;
1360 
1361 fail:
1362 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_DEL, CONTROLLER_INDEX,
1363 		   BTP_STATUS_FAILED);
1364 }
1365 
config_mod_sub_del_all(uint8_t * data,uint16_t len)1366 static void config_mod_sub_del_all(uint8_t *data, uint16_t len)
1367 {
1368 	struct mesh_cfg_model_sub_del_all_cmd *cmd = (void *)data;
1369 	uint8_t status;
1370 	int err;
1371 
1372 	LOG_DBG("");
1373 
1374 	err = bt_mesh_cfg_mod_sub_del_all(cmd->net_idx, cmd->address,
1375 					  cmd->elem_address, cmd->model_id,
1376 					  &status);
1377 
1378 	if (err) {
1379 		LOG_ERR("err %d", err);
1380 		goto fail;
1381 	}
1382 
1383 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_DEL_ALL,
1384 		    CONTROLLER_INDEX, &status, sizeof(status));
1385 	return;
1386 
1387 fail:
1388 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_DEL_ALL, CONTROLLER_INDEX,
1389 		   BTP_STATUS_FAILED);
1390 }
1391 
config_mod_sub_get(uint8_t * data,uint16_t len)1392 static void config_mod_sub_get(uint8_t *data, uint16_t len)
1393 {
1394 	struct mesh_cfg_model_sub_get_cmd *cmd = (void *)data;
1395 	uint8_t status;
1396 	int16_t subs;
1397 	size_t sub_cn;
1398 	int err;
1399 
1400 	LOG_DBG("");
1401 
1402 	err = bt_mesh_cfg_mod_sub_get(cmd->net_idx, cmd->address,
1403 				      cmd->elem_address, cmd->model_id, &status,
1404 				      &subs, &sub_cn);
1405 
1406 	if (err) {
1407 		LOG_ERR("err %d", err);
1408 		goto fail;
1409 	}
1410 
1411 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_GET,
1412 		    CONTROLLER_INDEX, &status, sizeof(status));
1413 	return;
1414 
1415 fail:
1416 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_GET, CONTROLLER_INDEX,
1417 		   BTP_STATUS_FAILED);
1418 }
1419 
config_mod_sub_get_vnd(uint8_t * data,uint16_t len)1420 static void config_mod_sub_get_vnd(uint8_t *data, uint16_t len)
1421 {
1422 	struct mesh_cfg_model_sub_get_vnd_cmd *cmd = (void *)data;
1423 	uint8_t status;
1424 	uint16_t subs;
1425 	size_t sub_cn;
1426 	int err;
1427 
1428 	LOG_DBG("");
1429 
1430 	err = bt_mesh_cfg_mod_sub_get_vnd(cmd->net_idx, cmd->address,
1431 					  cmd->elem_address, cmd->model_id,
1432 					  cmd->cid, &status, &subs, &sub_cn);
1433 
1434 	if (err) {
1435 		LOG_ERR("err %d", err);
1436 		goto fail;
1437 	}
1438 
1439 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_GET_VND,
1440 		    CONTROLLER_INDEX, &status, sizeof(status));
1441 	return;
1442 
1443 fail:
1444 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_GET_VND, CONTROLLER_INDEX,
1445 		   BTP_STATUS_FAILED);
1446 }
1447 
config_mod_sub_va_add(uint8_t * data,uint16_t len)1448 static void config_mod_sub_va_add(uint8_t *data, uint16_t len)
1449 {
1450 	struct mesh_cfg_model_sub_va_cmd *cmd = (void *)data;
1451 	uint8_t status;
1452 	uint16_t virt_addr_rcv;
1453 	int err;
1454 
1455 	LOG_DBG("");
1456 
1457 	err = bt_mesh_cfg_mod_sub_va_add(cmd->net_idx, cmd->address,
1458 					 cmd->elem_address, cmd->uuid,
1459 					 cmd->model_id, &virt_addr_rcv,
1460 					 &status);
1461 
1462 	if (err) {
1463 		LOG_ERR("err %d", err);
1464 		goto fail;
1465 	}
1466 
1467 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_ADD,
1468 		    CONTROLLER_INDEX, &status, sizeof(status));
1469 	return;
1470 
1471 fail:
1472 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_ADD, CONTROLLER_INDEX,
1473 		   BTP_STATUS_FAILED);
1474 }
1475 
config_mod_sub_va_del(uint8_t * data,uint16_t len)1476 static void config_mod_sub_va_del(uint8_t *data, uint16_t len)
1477 {
1478 	struct mesh_cfg_model_sub_va_cmd *cmd = (void *)data;
1479 	uint8_t status;
1480 	uint16_t virt_addr_rcv;
1481 	int err;
1482 
1483 	LOG_DBG("");
1484 
1485 	err = bt_mesh_cfg_mod_sub_va_del(cmd->net_idx, cmd->address,
1486 					 cmd->elem_address, cmd->uuid,
1487 					 cmd->model_id, &virt_addr_rcv,
1488 					 &status);
1489 
1490 	if (err) {
1491 		LOG_ERR("err %d", err);
1492 		goto fail;
1493 	}
1494 
1495 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_DEL,
1496 		    CONTROLLER_INDEX, &status, sizeof(status));
1497 	return;
1498 
1499 fail:
1500 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_DEL, CONTROLLER_INDEX,
1501 		   BTP_STATUS_FAILED);
1502 }
1503 
config_mod_sub_va_ovw(uint8_t * data,uint16_t len)1504 static void config_mod_sub_va_ovw(uint8_t *data, uint16_t len)
1505 {
1506 	struct mesh_cfg_model_sub_va_cmd *cmd = (void *)data;
1507 	uint8_t status;
1508 	uint16_t virt_addr_rcv;
1509 	int err;
1510 
1511 	LOG_DBG("");
1512 
1513 	err = bt_mesh_cfg_mod_sub_va_overwrite(cmd->net_idx, cmd->address,
1514 					       cmd->elem_address,
1515 					       cmd->uuid, cmd->model_id,
1516 					       &virt_addr_rcv, &status);
1517 
1518 	if (err) {
1519 		LOG_ERR("err %d", err);
1520 		goto fail;
1521 	}
1522 
1523 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_OVW,
1524 		    CONTROLLER_INDEX, &status, sizeof(status));
1525 	return;
1526 
1527 fail:
1528 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_SUB_VA_OVW, CONTROLLER_INDEX,
1529 		   BTP_STATUS_FAILED);
1530 }
1531 
config_netkey_add(uint8_t * data,uint16_t len)1532 static void config_netkey_add(uint8_t *data, uint16_t len)
1533 {
1534 	struct mesh_cfg_netkey_add_cmd *cmd = (void *)data;
1535 	uint8_t status;
1536 	int err;
1537 
1538 	LOG_DBG("");
1539 
1540 	err = bt_mesh_cfg_net_key_add(cmd->net_idx, cmd->address,
1541 				      cmd->net_key_idx, cmd->net_key, &status);
1542 
1543 	if (err) {
1544 		LOG_ERR("err %d", err);
1545 		goto fail;
1546 	}
1547 
1548 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_ADD, CONTROLLER_INDEX,
1549 		    &status, sizeof(status));
1550 	return;
1551 
1552 fail:
1553 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_ADD, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1554 }
1555 
config_netkey_update(uint8_t * data,uint16_t len)1556 static void config_netkey_update(uint8_t *data, uint16_t len)
1557 {
1558 	struct mesh_cfg_netkey_add_cmd *cmd = (void *)data;
1559 	uint8_t status;
1560 	int err;
1561 
1562 	LOG_DBG("");
1563 
1564 	err = bt_mesh_cfg_net_key_update(cmd->net_idx, cmd->address,
1565 					 cmd->net_key_idx, cmd->net_key,
1566 					 &status);
1567 
1568 	if (err) {
1569 		LOG_ERR("err %d", err);
1570 		goto fail;
1571 	}
1572 
1573 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_UPDATE,
1574 		    CONTROLLER_INDEX, &status, sizeof(status));
1575 	return;
1576 
1577 fail:
1578 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_UPDATE, CONTROLLER_INDEX,
1579 		   BTP_STATUS_FAILED);
1580 }
1581 
config_netkey_get(uint8_t * data,uint16_t len)1582 static void config_netkey_get(uint8_t *data, uint16_t len)
1583 {
1584 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1585 	uint16_t keys;
1586 	size_t key_cnt;
1587 	int err;
1588 
1589 	LOG_DBG("");
1590 
1591 	err = bt_mesh_cfg_net_key_get(cmd->net_idx, cmd->address, &keys,
1592 				      &key_cnt);
1593 
1594 	if (err) {
1595 		LOG_ERR("err %d", err);
1596 		goto fail;
1597 	}
1598 
1599 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_GET, CONTROLLER_INDEX,
1600 		    (uint8_t *)&keys, key_cnt);
1601 	return;
1602 
1603 fail:
1604 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1605 }
1606 
config_netkey_del(uint8_t * data,uint16_t len)1607 static void config_netkey_del(uint8_t *data, uint16_t len)
1608 {
1609 	struct mesh_cfg_netkey_del_cmd *cmd = (void *)data;
1610 	uint8_t status;
1611 	int err;
1612 
1613 	LOG_DBG("");
1614 
1615 	err = bt_mesh_cfg_net_key_del(cmd->net_idx, cmd->address,
1616 				      cmd->net_key_idx, &status);
1617 
1618 	if (err) {
1619 		LOG_ERR("err %d", err);
1620 		goto fail;
1621 	}
1622 
1623 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_DEL, CONTROLLER_INDEX,
1624 		    &status, sizeof(status));
1625 	return;
1626 
1627 fail:
1628 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NETKEY_DEL, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1629 }
1630 
config_appkey_add(uint8_t * data,uint16_t len)1631 static void config_appkey_add(uint8_t *data, uint16_t len)
1632 {
1633 	struct mesh_cfg_appkey_add_cmd *cmd = (void *)data;
1634 	uint8_t status;
1635 	int err;
1636 
1637 	LOG_DBG("");
1638 
1639 	err = bt_mesh_cfg_app_key_add(cmd->net_idx, cmd->address,
1640 				      cmd->net_key_idx, cmd->app_key_idx,
1641 				      cmd->app_key, &status);
1642 
1643 	if (err) {
1644 		LOG_ERR("err %d", err);
1645 		goto fail;
1646 	}
1647 
1648 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_ADD, CONTROLLER_INDEX,
1649 		    &status, sizeof(status));
1650 	return;
1651 
1652 fail:
1653 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_ADD, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1654 }
1655 
config_appkey_update(uint8_t * data,uint16_t len)1656 static void config_appkey_update(uint8_t *data, uint16_t len)
1657 {
1658 	struct mesh_cfg_appkey_add_cmd *cmd = (void *)data;
1659 	uint8_t status;
1660 	int err;
1661 
1662 	LOG_DBG("");
1663 
1664 	err = bt_mesh_cfg_app_key_update(cmd->net_idx, cmd->address,
1665 					 cmd->net_key_idx, cmd->app_key_idx,
1666 					 cmd->app_key, &status);
1667 
1668 	if (err) {
1669 		LOG_ERR("err %d", err);
1670 		goto fail;
1671 	}
1672 
1673 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_UPDATE,
1674 		    CONTROLLER_INDEX, &status, sizeof(status));
1675 	return;
1676 
1677 fail:
1678 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_UPDATE, CONTROLLER_INDEX,
1679 		   BTP_STATUS_FAILED);
1680 }
1681 
config_appkey_del(uint8_t * data,uint16_t len)1682 static void config_appkey_del(uint8_t *data, uint16_t len)
1683 {
1684 	struct mesh_cfg_appkey_del_cmd *cmd = (void *)data;
1685 	uint8_t status;
1686 	int err;
1687 
1688 	LOG_DBG("");
1689 
1690 	err = bt_mesh_cfg_app_key_del(cmd->net_idx, cmd->address,
1691 				      cmd->net_key_idx, cmd->app_key_idx,
1692 				      &status);
1693 
1694 	if (err) {
1695 		LOG_ERR("err %d", err);
1696 		goto fail;
1697 	}
1698 
1699 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_DEL, CONTROLLER_INDEX,
1700 		    &status, sizeof(status));
1701 	return;
1702 
1703 fail:
1704 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_DEL, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1705 }
1706 
config_appkey_get(uint8_t * data,uint16_t len)1707 static void config_appkey_get(uint8_t *data, uint16_t len)
1708 {
1709 	struct mesh_cfg_appkey_get_cmd *cmd = (void *)data;
1710 	uint8_t status;
1711 	uint16_t keys;
1712 	size_t key_cnt;
1713 	int err;
1714 
1715 	LOG_DBG("");
1716 
1717 	err = bt_mesh_cfg_app_key_get(cmd->net_idx, cmd->address,
1718 				      cmd->net_key_idx, &status, &keys,
1719 				      &key_cnt);
1720 
1721 	if (err) {
1722 		LOG_ERR("err %d", err);
1723 		goto fail;
1724 	}
1725 
1726 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_GET, CONTROLLER_INDEX,
1727 		    &status, sizeof(status));
1728 	return;
1729 
1730 fail:
1731 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_APPKEY_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
1732 }
1733 
config_model_app_bind(uint8_t * data,uint16_t len)1734 static void config_model_app_bind(uint8_t *data, uint16_t len)
1735 {
1736 	struct mesh_cfg_model_app_bind_cmd *cmd = (void *)data;
1737 	uint8_t status;
1738 	int err;
1739 
1740 	LOG_DBG("");
1741 
1742 	err = bt_mesh_cfg_mod_app_bind(cmd->net_idx, cmd->address,
1743 				       cmd->elem_address, cmd->app_key_idx,
1744 				       cmd->mod_id, &status);
1745 
1746 	if (err) {
1747 		LOG_ERR("err %d", err);
1748 		goto fail;
1749 	}
1750 
1751 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_BIND,
1752 		    CONTROLLER_INDEX, &status, sizeof(status));
1753 	return;
1754 
1755 fail:
1756 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_BIND, CONTROLLER_INDEX,
1757 		   BTP_STATUS_FAILED);
1758 }
1759 
config_model_app_bind_vnd(uint8_t * data,uint16_t len)1760 static void config_model_app_bind_vnd(uint8_t *data, uint16_t len)
1761 {
1762 	struct mesh_cfg_model_app_bind_vnd_cmd *cmd = (void *)data;
1763 	uint8_t status;
1764 	int err;
1765 
1766 	LOG_DBG("");
1767 
1768 	err = bt_mesh_cfg_mod_app_bind_vnd(cmd->net_idx, cmd->address,
1769 					   cmd->elem_address, cmd->app_key_idx,
1770 					   cmd->mod_id, cmd->cid, &status);
1771 
1772 	if (err) {
1773 		LOG_ERR("err %d", err);
1774 		goto fail;
1775 	}
1776 
1777 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_BIND_VND,
1778 		    CONTROLLER_INDEX, &status, sizeof(status));
1779 	return;
1780 
1781 fail:
1782 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_BIND_VND, CONTROLLER_INDEX,
1783 		   BTP_STATUS_FAILED);
1784 }
1785 
config_model_app_unbind(uint8_t * data,uint16_t len)1786 static void config_model_app_unbind(uint8_t *data, uint16_t len)
1787 {
1788 	struct mesh_cfg_model_app_bind_cmd *cmd = (void *)data;
1789 	uint8_t status;
1790 	int err;
1791 
1792 	LOG_DBG("");
1793 
1794 	err = bt_mesh_cfg_mod_app_unbind(cmd->net_idx, cmd->address,
1795 					 cmd->elem_address, cmd->app_key_idx,
1796 					 cmd->mod_id, &status);
1797 
1798 	if (err) {
1799 		LOG_ERR("err %d", err);
1800 		goto fail;
1801 	}
1802 
1803 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_UNBIND,
1804 		    CONTROLLER_INDEX, &status, sizeof(status));
1805 	return;
1806 
1807 fail:
1808 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_UNBIND, CONTROLLER_INDEX,
1809 		   BTP_STATUS_FAILED);
1810 }
1811 
config_model_app_get(uint8_t * data,uint16_t len)1812 static void config_model_app_get(uint8_t *data, uint16_t len)
1813 {
1814 	struct mesh_cfg_model_app_get_cmd *cmd = (void *)data;
1815 	uint8_t status;
1816 	uint16_t apps;
1817 	size_t app_cnt;
1818 	int err;
1819 
1820 	LOG_DBG("");
1821 
1822 	err = bt_mesh_cfg_mod_app_get(cmd->net_idx, cmd->address,
1823 				      cmd->elem_address, cmd->mod_id, &status,
1824 				      &apps, &app_cnt);
1825 
1826 	if (err) {
1827 		LOG_ERR("err %d", err);
1828 		goto fail;
1829 	}
1830 
1831 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_GET,
1832 		    CONTROLLER_INDEX, &status, sizeof(status));
1833 	return;
1834 
1835 fail:
1836 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_GET, CONTROLLER_INDEX,
1837 		   BTP_STATUS_FAILED);
1838 }
1839 
config_model_app_vnd_get(uint8_t * data,uint16_t len)1840 static void config_model_app_vnd_get(uint8_t *data, uint16_t len)
1841 {
1842 	struct mesh_cfg_model_app_get_cmd *cmd = (void *)data;
1843 	uint8_t status;
1844 	uint16_t apps;
1845 	size_t app_cnt;
1846 	int err;
1847 
1848 	LOG_DBG("");
1849 
1850 	err = bt_mesh_cfg_mod_app_get_vnd(cmd->net_idx, cmd->address,
1851 					  cmd->elem_address, cmd->mod_id,
1852 					  cmd->cid, &status, &apps, &app_cnt);
1853 
1854 	if (err) {
1855 		LOG_ERR("err %d", err);
1856 		goto fail;
1857 	}
1858 
1859 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_VND_GET,
1860 		    CONTROLLER_INDEX, &status, sizeof(status));
1861 	return;
1862 
1863 fail:
1864 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_MODEL_APP_VND_GET, CONTROLLER_INDEX,
1865 		   BTP_STATUS_FAILED);
1866 }
1867 
config_hb_pub_set(uint8_t * data,uint16_t len)1868 static void config_hb_pub_set(uint8_t *data, uint16_t len)
1869 {
1870 	struct mesh_cfg_heartbeat_pub_set_cmd *cmd = (void *)data;
1871 	uint8_t status;
1872 	struct bt_mesh_cfg_hb_pub pub;
1873 	int err;
1874 
1875 	LOG_DBG("");
1876 
1877 	pub.net_idx = cmd->net_key_idx;
1878 	pub.dst = cmd->destination;
1879 	pub.count = cmd->count_log;
1880 	pub.period = cmd->period_log;
1881 	pub.ttl = cmd->ttl;
1882 	pub.feat = cmd->features;
1883 
1884 	err = bt_mesh_cfg_hb_pub_set(cmd->net_idx, cmd->address, &pub, &status);
1885 
1886 	if (err) {
1887 		LOG_ERR("err %d", err);
1888 		goto fail;
1889 	}
1890 
1891 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_PUB_SET,
1892 		    CONTROLLER_INDEX, &status, sizeof(status));
1893 	return;
1894 
1895 fail:
1896 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_PUB_SET, CONTROLLER_INDEX,
1897 		   BTP_STATUS_FAILED);
1898 }
1899 
config_hb_pub_get(uint8_t * data,uint16_t len)1900 static void config_hb_pub_get(uint8_t *data, uint16_t len)
1901 {
1902 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1903 	uint8_t status;
1904 	struct bt_mesh_cfg_hb_pub pub;
1905 	int err;
1906 
1907 	LOG_DBG("");
1908 
1909 	err = bt_mesh_cfg_hb_pub_get(cmd->net_idx, cmd->address, &pub, &status);
1910 
1911 	if (err) {
1912 		LOG_ERR("err %d", err);
1913 		goto fail;
1914 	}
1915 
1916 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_PUB_GET,
1917 		    CONTROLLER_INDEX, &status, sizeof(status));
1918 	return;
1919 
1920 fail:
1921 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_PUB_GET, CONTROLLER_INDEX,
1922 		   BTP_STATUS_FAILED);
1923 }
1924 
config_hb_sub_set(uint8_t * data,uint16_t len)1925 static void config_hb_sub_set(uint8_t *data, uint16_t len)
1926 {
1927 	struct mesh_cfg_heartbeat_sub_set_cmd *cmd = (void *)data;
1928 	uint8_t status;
1929 	struct bt_mesh_cfg_hb_sub sub;
1930 	int err;
1931 
1932 	LOG_DBG("");
1933 
1934 	sub.src = cmd->source;
1935 	sub.dst = cmd->destination;
1936 	sub.period = cmd->period_log;
1937 
1938 	err = bt_mesh_cfg_hb_sub_set(cmd->net_idx, cmd->address, &sub, &status);
1939 
1940 	if (err) {
1941 		LOG_ERR("err %d", err);
1942 		goto fail;
1943 	}
1944 
1945 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_SUB_SET,
1946 		    CONTROLLER_INDEX, &status, sizeof(status));
1947 	return;
1948 
1949 fail:
1950 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_SUB_SET, CONTROLLER_INDEX,
1951 		   BTP_STATUS_FAILED);
1952 }
1953 
config_hb_sub_get(uint8_t * data,uint16_t len)1954 static void config_hb_sub_get(uint8_t *data, uint16_t len)
1955 {
1956 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1957 	uint8_t status;
1958 	struct bt_mesh_cfg_hb_sub sub;
1959 	int err;
1960 
1961 	LOG_DBG("");
1962 
1963 	err = bt_mesh_cfg_hb_sub_get(cmd->net_idx, cmd->address, &sub, &status);
1964 
1965 	if (err) {
1966 		LOG_ERR("err %d", err);
1967 		goto fail;
1968 	}
1969 
1970 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_SUB_GET,
1971 		    CONTROLLER_INDEX, &status, sizeof(status));
1972 	return;
1973 
1974 fail:
1975 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_HEARTBEAT_SUB_GET, CONTROLLER_INDEX,
1976 		   BTP_STATUS_FAILED);
1977 }
1978 
config_net_trans_get(uint8_t * data,uint16_t len)1979 static void config_net_trans_get(uint8_t *data, uint16_t len)
1980 {
1981 	struct mesh_cfg_val_get_cmd *cmd = (void *)data;
1982 	uint8_t transmit;
1983 	int err;
1984 
1985 	LOG_DBG("");
1986 
1987 	err = bt_mesh_cfg_net_transmit_get(cmd->net_idx, cmd->address,
1988 					   &transmit);
1989 
1990 	if (err) {
1991 		LOG_ERR("err %d", err);
1992 		goto fail;
1993 	}
1994 
1995 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NET_TRANS_GET,
1996 		    CONTROLLER_INDEX, &transmit, sizeof(transmit));
1997 	return;
1998 
1999 fail:
2000 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NET_TRANS_GET, CONTROLLER_INDEX,
2001 		   BTP_STATUS_FAILED);
2002 }
2003 
config_net_trans_set(uint8_t * data,uint16_t len)2004 static void config_net_trans_set(uint8_t *data, uint16_t len)
2005 {
2006 	struct mesh_cfg_net_trans_set_cmd *cmd = (void *)data;
2007 	uint8_t transmit;
2008 	int err;
2009 
2010 	LOG_DBG("");
2011 
2012 	err = bt_mesh_cfg_net_transmit_set(cmd->net_idx, cmd->address,
2013 					   cmd->transmit, &transmit);
2014 
2015 	if (err) {
2016 		LOG_ERR("err %d", err);
2017 		goto fail;
2018 	}
2019 
2020 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NET_TRANS_SET,
2021 		    CONTROLLER_INDEX, &transmit, sizeof(transmit));
2022 	return;
2023 
2024 fail:
2025 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NET_TRANS_SET, CONTROLLER_INDEX,
2026 		   BTP_STATUS_FAILED);
2027 }
2028 
config_node_identity_set(uint8_t * data,uint16_t len)2029 static void config_node_identity_set(uint8_t *data, uint16_t len)
2030 {
2031 	struct mesh_cfg_node_idt_set_cmd *cmd = (void *)data;
2032 	struct net_buf_simple *buf = NET_BUF_SIMPLE(2);
2033 	uint8_t identity;
2034 	uint8_t status;
2035 	int err;
2036 
2037 	LOG_DBG("");
2038 
2039 	err = bt_mesh_cfg_node_identity_set(cmd->net_idx, cmd->address,
2040 					    cmd->net_key_idx, cmd->new_identity,
2041 					    &status, &identity);
2042 
2043 	if (err) {
2044 		LOG_ERR("err %d", err);
2045 		goto fail;
2046 	}
2047 
2048 	net_buf_simple_init(buf, 0);
2049 	net_buf_simple_add_u8(buf, status);
2050 	net_buf_simple_add_u8(buf, identity);
2051 
2052 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_IDT_SET,
2053 		    CONTROLLER_INDEX, buf->data, buf->len);
2054 	return;
2055 
2056 fail:
2057 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_IDT_SET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
2058 }
2059 
config_node_identity_get(uint8_t * data,uint16_t len)2060 static void config_node_identity_get(uint8_t *data, uint16_t len)
2061 {
2062 	struct mesh_cfg_node_idt_get_cmd *cmd = (void *)data;
2063 	struct net_buf_simple *buf = NET_BUF_SIMPLE(2);
2064 	uint8_t identity;
2065 	uint8_t status;
2066 	int err;
2067 
2068 	LOG_DBG("");
2069 
2070 	err = bt_mesh_cfg_node_identity_get(cmd->net_idx, cmd->address,
2071 					    cmd->net_key_idx, &status,
2072 					    &identity);
2073 
2074 	if (err) {
2075 		LOG_ERR("err %d", err);
2076 		goto fail;
2077 	}
2078 
2079 	net_buf_simple_init(buf, 0);
2080 	net_buf_simple_add_u8(buf, status);
2081 	net_buf_simple_add_u8(buf, identity);
2082 
2083 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_IDT_GET,
2084 		    CONTROLLER_INDEX, buf->data, buf->len);
2085 	return;
2086 
2087 fail:
2088 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_IDT_GET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
2089 }
2090 
config_node_reset(uint8_t * data,uint16_t len)2091 static void config_node_reset(uint8_t *data, uint16_t len)
2092 {
2093 	struct mesh_cfg_node_reset_cmd *cmd = (void *)data;
2094 	bool status;
2095 	int err;
2096 
2097 	LOG_DBG("");
2098 
2099 	err = bt_mesh_cfg_node_reset(cmd->net_idx, cmd->address, &status);
2100 
2101 	if (err) {
2102 		LOG_ERR("err %d", err);
2103 		goto fail;
2104 	}
2105 
2106 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_RESET, CONTROLLER_INDEX,
2107 		    (uint8_t *)&status, sizeof(status));
2108 	return;
2109 
2110 fail:
2111 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_NODE_RESET, CONTROLLER_INDEX, BTP_STATUS_FAILED);
2112 }
2113 
config_lpn_timeout_get(uint8_t * data,uint16_t len)2114 static void config_lpn_timeout_get(uint8_t *data, uint16_t len)
2115 {
2116 	struct mesh_cfg_lpn_timeout_cmd *cmd = (void *)data;
2117 	int32_t polltimeout;
2118 	int err;
2119 
2120 	LOG_DBG("");
2121 
2122 	err = bt_mesh_cfg_lpn_timeout_get(cmd->net_idx, cmd->address,
2123 					  cmd->unicast_addr, &polltimeout);
2124 
2125 	if (err) {
2126 		LOG_ERR("err %d", err);
2127 		goto fail;
2128 	}
2129 
2130 	tester_send(BTP_SERVICE_ID_MESH, MESH_CFG_LPN_TIMEOUT_GET,
2131 		    CONTROLLER_INDEX, (uint8_t *)&polltimeout,
2132 		    sizeof(polltimeout));
2133 	return;
2134 
2135 fail:
2136 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_CFG_LPN_TIMEOUT_GET, CONTROLLER_INDEX,
2137 		   BTP_STATUS_FAILED);
2138 }
2139 
health_fault_get(uint8_t * data,uint16_t len)2140 static void health_fault_get(uint8_t *data, uint16_t len)
2141 {
2142 	struct mesh_health_fault_get_cmd *cmd = (void *)data;
2143 	uint8_t test_id;
2144 	size_t fault_count = 16;
2145 	uint8_t faults[fault_count];
2146 	int err;
2147 
2148 	LOG_DBG("");
2149 
2150 	err = bt_mesh_health_fault_get(cmd->address, cmd->app_idx, cmd->cid,
2151 				       &test_id, faults, &fault_count);
2152 
2153 	if (err) {
2154 		LOG_ERR("err %d", err);
2155 		goto fail;
2156 	}
2157 
2158 fail:
2159 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_FAULT_GET, CONTROLLER_INDEX,
2160 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2161 }
2162 
health_fault_clear(uint8_t * data,uint16_t len)2163 static void health_fault_clear(uint8_t *data, uint16_t len)
2164 {
2165 	struct mesh_health_fault_clear_cmd *cmd = (void *)data;
2166 	uint8_t test_id;
2167 	size_t fault_count = 16;
2168 	uint8_t faults[fault_count];
2169 	int err;
2170 
2171 	LOG_DBG("");
2172 
2173 	if (cmd->ack) {
2174 		err = bt_mesh_health_fault_clear(cmd->address, cmd->app_idx,
2175 						 cmd->cid, &test_id, faults,
2176 						 &fault_count);
2177 	} else {
2178 		err = bt_mesh_health_fault_clear(cmd->address, cmd->app_idx,
2179 						 cmd->cid, NULL, faults,
2180 						 &fault_count);
2181 	}
2182 
2183 	if (err) {
2184 		LOG_ERR("err %d", err);
2185 		goto fail;
2186 	}
2187 
2188 	if (cmd->ack) {
2189 		tester_send(BTP_SERVICE_ID_MESH, MESH_HEALTH_FAULT_CLEAR,
2190 			    CONTROLLER_INDEX, &test_id, sizeof(test_id));
2191 		return;
2192 	}
2193 
2194 fail:
2195 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_FAULT_CLEAR,
2196 		   CONTROLLER_INDEX,
2197 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2198 }
2199 
health_fault_test(uint8_t * data,uint16_t len)2200 static void health_fault_test(uint8_t *data, uint16_t len)
2201 {
2202 	struct mesh_health_fault_test_cmd *cmd = (void *)data;
2203 	struct net_buf_simple *buf = NET_BUF_SIMPLE(6);
2204 	size_t fault_count = 16;
2205 	uint8_t faults[fault_count];
2206 	uint8_t test_id;
2207 	uint16_t cid;
2208 	int err;
2209 
2210 	LOG_DBG("");
2211 
2212 	test_id = cmd->test_id;
2213 	cid = cmd->cid;
2214 
2215 	if (cmd->ack) {
2216 		err = bt_mesh_health_fault_test(cmd->address, cmd->app_idx,
2217 						cid, test_id, faults,
2218 						&fault_count);
2219 	} else {
2220 		err = bt_mesh_health_fault_test(cmd->address, cmd->app_idx,
2221 						cid, test_id, NULL,
2222 						&fault_count);
2223 	}
2224 
2225 	if (err) {
2226 		LOG_ERR("err %d", err);
2227 		goto fail;
2228 	}
2229 
2230 	if (cmd->ack) {
2231 		net_buf_simple_init(buf, 0);
2232 		net_buf_simple_add_u8(buf, test_id);
2233 		net_buf_simple_add_le16(buf, cid);
2234 		net_buf_simple_add_mem(buf, faults, fault_count);
2235 
2236 		tester_send(BTP_SERVICE_ID_MESH, MESH_HEALTH_FAULT_TEST,
2237 			    CONTROLLER_INDEX, buf->data, buf->len);
2238 		return;
2239 	}
2240 
2241 fail:
2242 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_FAULT_TEST,
2243 		   CONTROLLER_INDEX,
2244 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2245 }
2246 
health_period_get(uint8_t * data,uint16_t len)2247 static void health_period_get(uint8_t *data, uint16_t len)
2248 {
2249 	struct mesh_health_period_get_cmd *cmd = (void *)data;
2250 	uint8_t divisor;
2251 	int err;
2252 
2253 	LOG_DBG("");
2254 
2255 	err = bt_mesh_health_period_get(cmd->address, cmd->app_idx, &divisor);
2256 
2257 	if (err) {
2258 		LOG_ERR("err %d", err);
2259 		goto fail;
2260 	}
2261 
2262 fail:
2263 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_PERIOD_GET,
2264 		   CONTROLLER_INDEX,
2265 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2266 }
2267 
health_period_set(uint8_t * data,uint16_t len)2268 static void health_period_set(uint8_t *data, uint16_t len)
2269 {
2270 	struct mesh_health_period_set_cmd *cmd = (void *)data;
2271 	uint8_t updated_divisor;
2272 	int err;
2273 
2274 	LOG_DBG("");
2275 
2276 	if (cmd->ack) {
2277 		err = bt_mesh_health_period_set(cmd->address, cmd->app_idx,
2278 						cmd->divisor, &updated_divisor);
2279 	} else {
2280 		err = bt_mesh_health_period_set(cmd->address, cmd->app_idx,
2281 						cmd->divisor, NULL);
2282 	}
2283 
2284 	if (err) {
2285 		LOG_ERR("err %d", err);
2286 		goto fail;
2287 	}
2288 
2289 	if (cmd->ack) {
2290 		tester_send(BTP_SERVICE_ID_MESH, MESH_HEALTH_PERIOD_SET,
2291 			    CONTROLLER_INDEX, &updated_divisor,
2292 			    sizeof(updated_divisor));
2293 		return;
2294 	}
2295 
2296 fail:
2297 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_PERIOD_SET,
2298 		   CONTROLLER_INDEX,
2299 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2300 }
2301 
health_attention_get(uint8_t * data,uint16_t len)2302 static void health_attention_get(uint8_t *data, uint16_t len)
2303 {
2304 	struct mesh_health_attention_get_cmd *cmd = (void *)data;
2305 	uint8_t attention;
2306 	int err;
2307 
2308 	LOG_DBG("");
2309 
2310 	err = bt_mesh_health_attention_get(cmd->address, cmd->app_idx,
2311 					   &attention);
2312 
2313 	if (err) {
2314 		LOG_ERR("err %d", err);
2315 		goto fail;
2316 	}
2317 
2318 fail:
2319 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_ATTENTION_GET,
2320 		   CONTROLLER_INDEX,
2321 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2322 }
2323 
health_attention_set(uint8_t * data,uint16_t len)2324 static void health_attention_set(uint8_t *data, uint16_t len)
2325 {
2326 	struct mesh_health_attention_set_cmd *cmd = (void *)data;
2327 	uint8_t updated_attention;
2328 	int err;
2329 
2330 	LOG_DBG("");
2331 
2332 	if (cmd->ack) {
2333 		err = bt_mesh_health_attention_set(cmd->address, cmd->app_idx,
2334 						   cmd->attention,
2335 						   &updated_attention);
2336 	} else {
2337 		err = bt_mesh_health_attention_set(cmd->address, cmd->app_idx,
2338 						   cmd->attention, NULL);
2339 	}
2340 
2341 	if (err) {
2342 		LOG_ERR("err %d", err);
2343 		goto fail;
2344 	}
2345 
2346 	if (cmd->ack) {
2347 		tester_send(BTP_SERVICE_ID_MESH, MESH_HEALTH_ATTENTION_SET,
2348 			    CONTROLLER_INDEX, &updated_attention,
2349 			    sizeof(updated_attention));
2350 		return;
2351 	}
2352 
2353 fail:
2354 	tester_rsp(BTP_SERVICE_ID_MESH, MESH_HEALTH_ATTENTION_SET,
2355 		   CONTROLLER_INDEX,
2356 		   err ? BTP_STATUS_FAILED : BTP_STATUS_SUCCESS);
2357 }
2358 
tester_handle_mesh(uint8_t opcode,uint8_t index,uint8_t * data,uint16_t len)2359 void tester_handle_mesh(uint8_t opcode, uint8_t index, uint8_t *data, uint16_t len)
2360 {
2361 	switch (opcode) {
2362 	case MESH_READ_SUPPORTED_COMMANDS:
2363 		supported_commands(data, len);
2364 		break;
2365 	case MESH_CONFIG_PROVISIONING:
2366 		config_prov(data, len);
2367 		break;
2368 	case MESH_PROVISION_NODE:
2369 		provision_node(data, len);
2370 		break;
2371 	case MESH_INIT:
2372 		init(data, len);
2373 		break;
2374 	case MESH_RESET:
2375 		reset(data, len);
2376 		break;
2377 	case MESH_INPUT_NUMBER:
2378 		input_number(data, len);
2379 		break;
2380 	case MESH_INPUT_STRING:
2381 		input_string(data, len);
2382 		break;
2383 	case MESH_IVU_TEST_MODE:
2384 		ivu_test_mode(data, len);
2385 		break;
2386 	case MESH_IVU_TOGGLE_STATE:
2387 		ivu_toggle_state(data, len);
2388 		break;
2389 	case MESH_LPN:
2390 		lpn(data, len);
2391 		break;
2392 	case MESH_LPN_POLL:
2393 		lpn_poll(data, len);
2394 		break;
2395 	case MESH_NET_SEND:
2396 		net_send(data, len);
2397 		break;
2398 	case MESH_HEALTH_GENERATE_FAULTS:
2399 		health_generate_faults(data, len);
2400 		break;
2401 	case MESH_HEALTH_CLEAR_FAULTS:
2402 		health_clear_faults(data, len);
2403 		break;
2404 	case MESH_MODEL_SEND:
2405 		model_send(data, len);
2406 		break;
2407 	case MESH_COMP_DATA_GET:
2408 		composition_data_get(data, len);
2409 		break;
2410 	case MESH_CFG_BEACON_GET:
2411 		config_beacon_get(data, len);
2412 		break;
2413 	case MESH_CFG_BEACON_SET:
2414 		config_beacon_set(data, len);
2415 		break;
2416 	case MESH_CFG_DEFAULT_TTL_GET:
2417 		config_default_ttl_get(data, len);
2418 		break;
2419 	case MESH_CFG_DEFAULT_TTL_SET:
2420 		config_default_ttl_set(data, len);
2421 		break;
2422 	case MESH_CFG_GATT_PROXY_GET:
2423 		config_gatt_proxy_get(data, len);
2424 		break;
2425 	case MESH_CFG_GATT_PROXY_SET:
2426 		config_gatt_proxy_set(data, len);
2427 		break;
2428 	case MESH_CFG_FRIEND_GET:
2429 		config_friend_get(data, len);
2430 		break;
2431 	case MESH_CFG_FRIEND_SET:
2432 		config_friend_set(data, len);
2433 		break;
2434 	case MESH_CFG_RELAY_GET:
2435 		config_relay_get(data, len);
2436 		break;
2437 	case MESH_CFG_RELAY_SET:
2438 		config_relay_set(data, len);
2439 		break;
2440 	case MESH_CFG_MODEL_PUB_GET:
2441 		config_mod_pub_get(data, len);
2442 		break;
2443 	case MESH_CFG_MODEL_PUB_SET:
2444 		config_mod_pub_set(data, len);
2445 		break;
2446 	case MESH_CFG_MODEL_SUB_ADD:
2447 		config_mod_sub_add(data, len);
2448 		break;
2449 	case MESH_CFG_MODEL_SUB_DEL:
2450 		config_mod_sub_del(data, len);
2451 		break;
2452 	case MESH_CFG_MODEL_SUB_OVW:
2453 		config_mod_sub_ovw(data, len);
2454 		break;
2455 	case MESH_CFG_MODEL_SUB_DEL_ALL:
2456 		config_mod_sub_del_all(data, len);
2457 		break;
2458 	case MESH_CFG_MODEL_SUB_GET:
2459 		config_mod_sub_get(data, len);
2460 		break;
2461 	case MESH_CFG_MODEL_SUB_GET_VND:
2462 		config_mod_sub_get_vnd(data, len);
2463 		break;
2464 	case MESH_CFG_MODEL_SUB_VA_ADD:
2465 		config_mod_sub_va_add(data, len);
2466 		break;
2467 	case MESH_CFG_MODEL_SUB_VA_DEL:
2468 		config_mod_sub_va_del(data, len);
2469 		break;
2470 	case MESH_CFG_MODEL_SUB_VA_OVW:
2471 		config_mod_sub_va_ovw(data, len);
2472 		break;
2473 	case MESH_CFG_NETKEY_ADD:
2474 		config_netkey_add(data, len);
2475 		break;
2476 	case MESH_CFG_NETKEY_GET:
2477 		config_netkey_get(data, len);
2478 		break;
2479 	case MESH_CFG_NETKEY_DEL:
2480 		config_netkey_del(data, len);
2481 		break;
2482 	case MESH_CFG_NETKEY_UPDATE:
2483 		config_netkey_update(data, len);
2484 		break;
2485 	case MESH_CFG_APPKEY_ADD:
2486 		config_appkey_add(data, len);
2487 		break;
2488 	case MESH_CFG_APPKEY_DEL:
2489 		config_appkey_del(data, len);
2490 		break;
2491 	case MESH_CFG_APPKEY_GET:
2492 		config_appkey_get(data, len);
2493 		break;
2494 	case MESH_CFG_APPKEY_UPDATE:
2495 		config_appkey_update(data, len);
2496 		break;
2497 	case MESH_CFG_MODEL_APP_BIND:
2498 		config_model_app_bind(data, len);
2499 		break;
2500 	case MESH_CFG_MODEL_APP_UNBIND:
2501 		config_model_app_unbind(data, len);
2502 		break;
2503 	case MESH_CFG_MODEL_APP_GET:
2504 		config_model_app_get(data, len);
2505 		break;
2506 	case MESH_CFG_MODEL_APP_VND_GET:
2507 		config_model_app_vnd_get(data, len);
2508 		break;
2509 	case MESH_CFG_HEARTBEAT_PUB_SET:
2510 		config_hb_pub_set(data, len);
2511 		break;
2512 	case MESH_CFG_HEARTBEAT_PUB_GET:
2513 		config_hb_pub_get(data, len);
2514 		break;
2515 	case MESH_CFG_HEARTBEAT_SUB_SET:
2516 		config_hb_sub_set(data, len);
2517 		break;
2518 	case MESH_CFG_HEARTBEAT_SUB_GET:
2519 		config_hb_sub_get(data, len);
2520 		break;
2521 	case MESH_CFG_NET_TRANS_GET:
2522 		config_net_trans_get(data, len);
2523 		break;
2524 	case MESH_CFG_NET_TRANS_SET:
2525 		config_net_trans_set(data, len);
2526 		break;
2527 	case MESH_CFG_NODE_IDT_SET:
2528 		config_node_identity_set(data, len);
2529 		break;
2530 	case MESH_CFG_NODE_IDT_GET:
2531 		config_node_identity_get(data, len);
2532 		break;
2533 	case MESH_CFG_NODE_RESET:
2534 		config_node_reset(data, len);
2535 		break;
2536 	case MESH_CFG_LPN_TIMEOUT_GET:
2537 		config_lpn_timeout_get(data, len);
2538 		break;
2539 	case MESH_CFG_MODEL_PUB_VA_SET:
2540 		config_mod_pub_va_set(data, len);
2541 		break;
2542 	case MESH_CFG_MODEL_APP_BIND_VND:
2543 		config_model_app_bind_vnd(data, len);
2544 		break;
2545 	case MESH_HEALTH_FAULT_GET:
2546 		health_fault_get(data, len);
2547 		break;
2548 	case MESH_HEALTH_FAULT_CLEAR:
2549 		health_fault_clear(data, len);
2550 		break;
2551 	case MESH_HEALTH_FAULT_TEST:
2552 		health_fault_test(data, len);
2553 		break;
2554 	case MESH_HEALTH_PERIOD_GET:
2555 		health_period_get(data, len);
2556 		break;
2557 	case MESH_HEALTH_PERIOD_SET:
2558 		health_period_set(data, len);
2559 		break;
2560 	case MESH_HEALTH_ATTENTION_GET:
2561 		health_attention_get(data, len);
2562 		break;
2563 	case MESH_HEALTH_ATTENTION_SET:
2564 		health_attention_set(data, len);
2565 		break;
2566 	case MESH_PROVISION_ADV:
2567 		provision_adv(data, len);
2568 		break;
2569 	case MESH_CFG_KRP_GET:
2570 		config_krp_get(data, len);
2571 		break;
2572 	case MESH_CFG_KRP_SET:
2573 		config_krp_set(data, len);
2574 		break;
2575 #if defined(CONFIG_BT_TESTING)
2576 	case MESH_LPN_SUBSCRIBE:
2577 		lpn_subscribe(data, len);
2578 		break;
2579 	case MESH_LPN_UNSUBSCRIBE:
2580 		lpn_unsubscribe(data, len);
2581 		break;
2582 	case MESH_RPL_CLEAR:
2583 		rpl_clear(data, len);
2584 		break;
2585 #endif /* CONFIG_BT_TESTING */
2586 	case MESH_PROXY_IDENTITY:
2587 		proxy_identity_enable(data, len);
2588 		break;
2589 	default:
2590 		tester_rsp(BTP_SERVICE_ID_MESH, opcode, index,
2591 			   BTP_STATUS_UNKNOWN_CMD);
2592 		break;
2593 	}
2594 }
2595 
net_recv_ev(uint8_t ttl,uint8_t ctl,uint16_t src,uint16_t dst,const void * payload,size_t payload_len)2596 void net_recv_ev(uint8_t ttl, uint8_t ctl, uint16_t src, uint16_t dst, const void *payload,
2597 		 size_t payload_len)
2598 {
2599 	NET_BUF_SIMPLE_DEFINE(buf, UINT8_MAX);
2600 	struct mesh_net_recv_ev *ev;
2601 
2602 	LOG_DBG("ttl 0x%02x ctl 0x%02x src 0x%04x dst 0x%04x payload_len %zu",
2603 		ttl, ctl, src, dst, payload_len);
2604 
2605 	if (payload_len > net_buf_simple_tailroom(&buf)) {
2606 		LOG_ERR("Payload size exceeds buffer size");
2607 		return;
2608 	}
2609 
2610 	ev = net_buf_simple_add(&buf, sizeof(*ev));
2611 	ev->ttl = ttl;
2612 	ev->ctl = ctl;
2613 	ev->src = sys_cpu_to_le16(src);
2614 	ev->dst = sys_cpu_to_le16(dst);
2615 	ev->payload_len = payload_len;
2616 	net_buf_simple_add_mem(&buf, payload, payload_len);
2617 
2618 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_NET_RECV, CONTROLLER_INDEX,
2619 		    buf.data, buf.len);
2620 }
2621 
model_bound_cb(uint16_t addr,struct bt_mesh_model * model,uint16_t key_idx)2622 static void model_bound_cb(uint16_t addr, struct bt_mesh_model *model,
2623 			   uint16_t key_idx)
2624 {
2625 	int i;
2626 
2627 	LOG_DBG("remote addr 0x%04x key_idx 0x%04x model %p",
2628 		addr, key_idx, model);
2629 
2630 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
2631 		if (!model_bound[i].model) {
2632 			model_bound[i].model = model;
2633 			model_bound[i].addr = addr;
2634 			model_bound[i].appkey_idx = key_idx;
2635 
2636 			return;
2637 		}
2638 	}
2639 
2640 	LOG_ERR("model_bound is full");
2641 }
2642 
model_unbound_cb(uint16_t addr,struct bt_mesh_model * model,uint16_t key_idx)2643 static void model_unbound_cb(uint16_t addr, struct bt_mesh_model *model,
2644 			     uint16_t key_idx)
2645 {
2646 	int i;
2647 
2648 	LOG_DBG("remote addr 0x%04x key_idx 0x%04x model %p",
2649 		addr, key_idx, model);
2650 
2651 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
2652 		if (model_bound[i].model == model) {
2653 			model_bound[i].model = NULL;
2654 			model_bound[i].addr = 0x0000;
2655 			model_bound[i].appkey_idx = BT_MESH_KEY_UNUSED;
2656 
2657 			return;
2658 		}
2659 	}
2660 
2661 	LOG_INF("model not found");
2662 }
2663 
invalid_bearer_cb(uint8_t opcode)2664 static void invalid_bearer_cb(uint8_t opcode)
2665 {
2666 	struct mesh_invalid_bearer_ev ev = {
2667 		.opcode = opcode,
2668 	};
2669 
2670 	LOG_DBG("opcode 0x%02x", opcode);
2671 
2672 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_INVALID_BEARER,
2673 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2674 }
2675 
incomp_timer_exp_cb(void)2676 static void incomp_timer_exp_cb(void)
2677 {
2678 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_INCOMP_TIMER_EXP,
2679 		    CONTROLLER_INDEX, NULL, 0);
2680 }
2681 
2682 static struct bt_test_cb bt_test_cb = {
2683 	.mesh_net_recv = net_recv_ev,
2684 	.mesh_model_bound = model_bound_cb,
2685 	.mesh_model_unbound = model_unbound_cb,
2686 	.mesh_prov_invalid_bearer = invalid_bearer_cb,
2687 	.mesh_trans_incomp_timer_exp = incomp_timer_exp_cb,
2688 };
2689 
friend_established(uint16_t net_idx,uint16_t lpn_addr,uint8_t recv_delay,uint32_t polltimeout)2690 static void friend_established(uint16_t net_idx, uint16_t lpn_addr,
2691 			       uint8_t recv_delay, uint32_t polltimeout)
2692 {
2693 	struct mesh_frnd_established_ev ev = { net_idx, lpn_addr, recv_delay,
2694 					       polltimeout };
2695 
2696 	LOG_DBG("Friendship (as Friend) established with "
2697 			"LPN 0x%04x Receive Delay %u Poll Timeout %u",
2698 			lpn_addr, recv_delay, polltimeout);
2699 
2700 
2701 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_FRND_ESTABLISHED,
2702 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2703 }
2704 
friend_terminated(uint16_t net_idx,uint16_t lpn_addr)2705 static void friend_terminated(uint16_t net_idx, uint16_t lpn_addr)
2706 {
2707 	struct mesh_frnd_terminated_ev ev = { net_idx, lpn_addr };
2708 
2709 	LOG_DBG("Friendship (as Friend) lost with LPN "
2710 			"0x%04x", lpn_addr);
2711 
2712 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_FRND_TERMINATED,
2713 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2714 }
2715 
2716 BT_MESH_FRIEND_CB_DEFINE(friend_cb) = {
2717 	.established = friend_established,
2718 	.terminated = friend_terminated,
2719 };
2720 
lpn_established(uint16_t net_idx,uint16_t friend_addr,uint8_t queue_size,uint8_t recv_win)2721 static void lpn_established(uint16_t net_idx, uint16_t friend_addr,
2722 					uint8_t queue_size, uint8_t recv_win)
2723 {
2724 	struct mesh_lpn_established_ev ev = { net_idx, friend_addr, queue_size,
2725 					      recv_win };
2726 
2727 	LOG_DBG("Friendship (as LPN) established with "
2728 			"Friend 0x%04x Queue Size %d Receive Window %d",
2729 			friend_addr, queue_size, recv_win);
2730 
2731 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_LPN_ESTABLISHED,
2732 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2733 }
2734 
lpn_terminated(uint16_t net_idx,uint16_t friend_addr)2735 static void lpn_terminated(uint16_t net_idx, uint16_t friend_addr)
2736 {
2737 	struct mesh_lpn_polled_ev ev = { net_idx, friend_addr };
2738 
2739 	LOG_DBG("Friendship (as LPN) lost with Friend "
2740 			"0x%04x", friend_addr);
2741 
2742 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_LPN_TERMINATED,
2743 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2744 }
2745 
lpn_polled(uint16_t net_idx,uint16_t friend_addr,bool retry)2746 static void lpn_polled(uint16_t net_idx, uint16_t friend_addr, bool retry)
2747 {
2748 	struct mesh_lpn_polled_ev ev = { net_idx, friend_addr, (uint8_t)retry };
2749 
2750 	LOG_DBG("LPN polled 0x%04x %s", friend_addr, retry ? "(retry)" : "");
2751 
2752 	tester_send(BTP_SERVICE_ID_MESH, MESH_EV_LPN_POLLED,
2753 		    CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
2754 }
2755 
2756 BT_MESH_LPN_CB_DEFINE(lpn_cb) = {
2757 	.established = lpn_established,
2758 	.terminated = lpn_terminated,
2759 	.polled = lpn_polled,
2760 };
2761 
tester_init_mesh(void)2762 uint8_t tester_init_mesh(void)
2763 {
2764 	if (IS_ENABLED(CONFIG_BT_TESTING)) {
2765 		bt_test_cb_register(&bt_test_cb);
2766 	}
2767 
2768 	return BTP_STATUS_SUCCESS;
2769 }
2770 
tester_unregister_mesh(void)2771 uint8_t tester_unregister_mesh(void)
2772 {
2773 	return BTP_STATUS_SUCCESS;
2774 }
2775