1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/bluetooth.h>
8 
9 #include <assert.h>
10 #include <errno.h>
11 #include <zephyr/bluetooth/mesh.h>
12 #include <zephyr/bluetooth/testing.h>
13 #include <zephyr/bluetooth/mesh/cfg.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/settings/settings.h>
16 #include <app_keys.h>
17 #include <va.h>
18 #include <sar_cfg_internal.h>
19 #include <string.h>
20 #include "mesh/access.h"
21 
22 #include <zephyr/logging/log.h>
23 #define LOG_MODULE_NAME bttester_mesh
24 LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
25 
26 #include "btp/btp.h"
27 #include "dfu_slot.h"
28 
29 #define CID_LOCAL 0x05F1
30 #define COMPANY_ID_LF 0x05F1
31 #define COMPANY_ID_NORDIC_SEMI 0x05F9
32 
33 /* Health server data */
34 #define CUR_FAULTS_MAX 4
35 #define HEALTH_TEST_ID 0x00
36 
37 static uint8_t cur_faults[CUR_FAULTS_MAX];
38 static uint8_t reg_faults[CUR_FAULTS_MAX * 2];
39 
40 /* Provision node data */
41 static uint8_t net_key[16];
42 static uint16_t net_key_idx;
43 static uint8_t flags;
44 static uint32_t iv_index;
45 static uint16_t addr;
46 static uint8_t dev_key[16];
47 static uint8_t input_size;
48 static uint8_t pub_key[64];
49 static uint8_t priv_key[32];
50 
51 /* Configured provisioning data */
52 static uint8_t dev_uuid[16];
53 static uint8_t static_auth[BTP_MESH_PROV_AUTH_MAX_LEN];
54 static uint8_t static_auth_size;
55 
56 /* Vendor Model data */
57 #define VND_MODEL_ID_1 0x1234
58 static uint8_t vnd_app_key[16];
59 static uint16_t vnd_app_key_idx = 0x000f;
60 
61 /* Model send data */
62 #define MODEL_BOUNDS_MAX 100
63 
64 #if defined(CONFIG_BT_MESH_BLOB_SRV) || defined(CONFIG_BT_MESH_BLOB_CLI)
65 /* BLOB Model data*/
66 static uint8_t blob_rx_sum;
67 static bool blob_valid;
68 static const char *blob_data = "11111111111111111111111111111111";
69 
blob_io_open(const struct bt_mesh_blob_io * io,const struct bt_mesh_blob_xfer * xfer,enum bt_mesh_blob_io_mode mode)70 static int blob_io_open(const struct bt_mesh_blob_io *io,
71 			const struct bt_mesh_blob_xfer *xfer,
72 			enum bt_mesh_blob_io_mode mode)
73 {
74 	blob_rx_sum = 0;
75 	blob_valid = true;
76 	return 0;
77 }
78 
blob_chunk_wr(const struct bt_mesh_blob_io * io,const struct bt_mesh_blob_xfer * xfer,const struct bt_mesh_blob_block * block,const struct bt_mesh_blob_chunk * chunk)79 static int blob_chunk_wr(const struct bt_mesh_blob_io *io,
80 			 const struct bt_mesh_blob_xfer *xfer,
81 			 const struct bt_mesh_blob_block *block,
82 			 const struct bt_mesh_blob_chunk *chunk)
83 {
84 	for (int i = 0; i < chunk->size; ++i) {
85 		blob_rx_sum += chunk->data[i];
86 		if (chunk->data[i] !=
87 		    blob_data[(i + chunk->offset) % strlen(blob_data)]) {
88 			blob_valid = false;
89 		}
90 	}
91 
92 	return 0;
93 }
94 
blob_chunk_rd(const struct bt_mesh_blob_io * io,const struct bt_mesh_blob_xfer * xfer,const struct bt_mesh_blob_block * block,const struct bt_mesh_blob_chunk * chunk)95 static int blob_chunk_rd(const struct bt_mesh_blob_io *io,
96 			 const struct bt_mesh_blob_xfer *xfer,
97 			 const struct bt_mesh_blob_block *block,
98 			 const struct bt_mesh_blob_chunk *chunk)
99 {
100 	for (int i = 0; i < chunk->size; ++i) {
101 		chunk->data[i] =
102 			blob_data[(i + chunk->offset) % strlen(blob_data)];
103 	}
104 
105 	return 0;
106 }
107 
108 static const struct bt_mesh_blob_io dummy_blob_io = {
109 	.open = blob_io_open,
110 	.rd = blob_chunk_rd,
111 	.wr = blob_chunk_wr,
112 };
113 #endif
114 
115 #if defined(CONFIG_BT_MESH_DFD_SRV)
116 static const struct bt_mesh_dfu_slot *dfu_self_update_slot;
117 
is_self_update(struct bt_mesh_dfd_srv * srv)118 static bool is_self_update(struct bt_mesh_dfd_srv *srv)
119 {
120 	for (int i = 0; i < ARRAY_SIZE(srv->targets); i++) {
121 		if (bt_mesh_has_addr(srv->targets[i].blob.addr)) {
122 			return true;
123 		}
124 	}
125 
126 	return false;
127 }
128 
129 /* DFD Model data*/
dfd_srv_recv(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot,const struct bt_mesh_blob_io ** io)130 static int dfd_srv_recv(struct bt_mesh_dfd_srv *srv,
131 			const struct bt_mesh_dfu_slot *slot,
132 			const struct bt_mesh_blob_io **io)
133 {
134 	LOG_DBG("Uploading new firmware image to the distributor.");
135 
136 	*io = &dummy_blob_io;
137 
138 	return 0;
139 }
140 
dfd_srv_del(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot)141 static void dfd_srv_del(struct bt_mesh_dfd_srv *srv,
142 			const struct bt_mesh_dfu_slot *slot)
143 {
144 	LOG_DBG("Deleting the firmware image from the distributor.");
145 }
146 
dfd_srv_send(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot,const struct bt_mesh_blob_io ** io)147 static int dfd_srv_send(struct bt_mesh_dfd_srv *srv,
148 			const struct bt_mesh_dfu_slot *slot,
149 			const struct bt_mesh_blob_io **io)
150 {
151 	LOG_DBG("Starting the firmware distribution.");
152 
153 	*io = &dummy_blob_io;
154 
155 	dfu_self_update_slot = NULL;
156 
157 	if (is_self_update(srv)) {
158 		LOG_DBG("DFD server starts self-update...");
159 		dfu_self_update_slot = slot;
160 	}
161 
162 	return 0;
163 }
164 
165 #ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
166 static struct {
167 	uint8_t uri[CONFIG_BT_MESH_DFU_URI_MAXLEN];
168 	uint8_t uri_len;
169 	uint8_t fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN];
170 	uint8_t fwid_len;
171 	const struct bt_mesh_dfu_slot *slot;
172 	uint8_t progress;
173 	bool started;
174 } dfd_srv_oob_ctx;
175 
176 static void oob_check_handler(struct k_work *work);
177 static K_WORK_DEFINE(oob_check, oob_check_handler);
178 static void oob_store_handler(struct k_work *work);
179 static K_WORK_DEFINE(oob_store, oob_store_handler);
180 
dfd_srv_start_oob_upload(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot,const char * uri,uint8_t uri_len,const uint8_t * fwid,uint16_t fwid_len)181 static int dfd_srv_start_oob_upload(struct bt_mesh_dfd_srv *srv,
182 				    const struct bt_mesh_dfu_slot *slot,
183 				    const char *uri, uint8_t uri_len,
184 				    const uint8_t *fwid, uint16_t fwid_len)
185 {
186 	LOG_DBG("Start OOB Upload");
187 
188 	memcpy(dfd_srv_oob_ctx.uri, uri, uri_len);
189 	dfd_srv_oob_ctx.uri_len = uri_len;
190 	memcpy(dfd_srv_oob_ctx.fwid, fwid, fwid_len);
191 	dfd_srv_oob_ctx.fwid_len = fwid_len;
192 	dfd_srv_oob_ctx.slot = slot;
193 	dfd_srv_oob_ctx.progress = 0;
194 	dfd_srv_oob_ctx.started = true;
195 
196 	k_work_submit(&oob_check);
197 
198 	return BT_MESH_DFD_SUCCESS;
199 }
200 
dfd_srv_cancel_oob_upload(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot)201 static void dfd_srv_cancel_oob_upload(struct bt_mesh_dfd_srv *srv,
202 				      const struct bt_mesh_dfu_slot *slot)
203 {
204 	LOG_DBG("Cancel OOB Upload");
205 
206 	dfd_srv_oob_ctx.started = false;
207 }
208 
dfd_srv_oob_progress_get(struct bt_mesh_dfd_srv * srv,const struct bt_mesh_dfu_slot * slot)209 static uint8_t dfd_srv_oob_progress_get(struct bt_mesh_dfd_srv *srv,
210 					const struct bt_mesh_dfu_slot *slot)
211 {
212 	uint8_t progress;
213 
214 	if (dfd_srv_oob_ctx.started) {
215 		progress = dfd_srv_oob_ctx.progress;
216 
217 		dfd_srv_oob_ctx.progress = MIN(dfd_srv_oob_ctx.progress + 25, 99);
218 
219 		if (dfd_srv_oob_ctx.progress == 99) {
220 			k_work_submit(&oob_store);
221 		}
222 	} else {
223 		progress = 0;
224 	}
225 
226 	LOG_DBG("OOB Progress Get (%sstarted: %d %%)", dfd_srv_oob_ctx.started ? "" : "not ",
227 		progress);
228 	return progress;
229 }
230 #endif /* CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD */
231 
232 static struct bt_mesh_dfd_srv_cb dfd_srv_cb = {
233 	.recv = dfd_srv_recv,
234 	.del = dfd_srv_del,
235 	.send = dfd_srv_send,
236 #ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
237 	.start_oob_upload = dfd_srv_start_oob_upload,
238 	.cancel_oob_upload = dfd_srv_cancel_oob_upload,
239 	.oob_progress_get = dfd_srv_oob_progress_get,
240 #endif
241 };
242 
243 static struct bt_mesh_dfd_srv dfd_srv = BT_MESH_DFD_SRV_INIT(&dfd_srv_cb);
244 
245 #ifdef CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD
246 #define SUPPORTED_SCHEME "http"
247 
oob_check_handler(struct k_work * work)248 static void oob_check_handler(struct k_work *work)
249 {
250 	uint8_t scheme[10];
251 	int i;
252 	int status;
253 	int err;
254 
255 	for (i = 0; i < MIN(dfd_srv_oob_ctx.uri_len, sizeof(scheme)); i++) {
256 		if (IN_RANGE(dfd_srv_oob_ctx.uri[i], 48, 57) || /* DIGIT */
257 		    IN_RANGE(dfd_srv_oob_ctx.uri[i], 65, 90) || /* ALPHA UPPER CASE */
258 		    IN_RANGE(dfd_srv_oob_ctx.uri[i], 97, 122) || /* ALPHA LOWER CASE */
259 		    dfd_srv_oob_ctx.uri[i] == '.' ||
260 		    dfd_srv_oob_ctx.uri[i] == '+' ||
261 		    dfd_srv_oob_ctx.uri[i] == '-') {
262 			scheme[i] = dfd_srv_oob_ctx.uri[i];
263 		} else {
264 			break;
265 		}
266 	}
267 
268 	if (i == dfd_srv_oob_ctx.uri_len || dfd_srv_oob_ctx.uri[i] != ':') {
269 		status = BT_MESH_DFD_ERR_URI_MALFORMED;
270 	} else if (i != strlen(SUPPORTED_SCHEME) ||
271 		   memcmp(scheme, SUPPORTED_SCHEME, strlen(SUPPORTED_SCHEME))) {
272 		status = BT_MESH_DFD_ERR_URI_NOT_SUPPORTED;
273 	} else {
274 		status = BT_MESH_DFD_SUCCESS;
275 	}
276 
277 	err = bt_mesh_dfd_srv_oob_check_complete(&dfd_srv, dfd_srv_oob_ctx.slot, status,
278 						 dfd_srv_oob_ctx.fwid, dfd_srv_oob_ctx.fwid_len);
279 	LOG_DBG("OOB check completed (err %d)", err);
280 }
281 
oob_store_handler(struct k_work * work)282 static void oob_store_handler(struct k_work *work)
283 {
284 	int err;
285 
286 	err = bt_mesh_dfd_srv_oob_store_complete(&dfd_srv, dfd_srv_oob_ctx.slot, true,
287 						 10000, "metadata", 8);
288 	LOG_DBG("OOB store completed (err %d)", err);
289 }
290 #endif /* CONFIG_BT_MESH_DFD_SRV_OOB_UPLOAD */
291 #endif
292 
293 #if defined(CONFIG_BT_MESH_BLOB_CLI) && !defined(CONFIG_BT_MESH_DFD_SRV)
294 static struct {
295 	struct bt_mesh_blob_cli_inputs inputs;
296 	struct bt_mesh_blob_target targets[32];
297 	struct bt_mesh_blob_target_pull pull[32];
298 	uint8_t target_count;
299 	struct bt_mesh_blob_xfer xfer;
300 } blob_cli_xfer;
301 
blob_cli_lost_target(struct bt_mesh_blob_cli * cli,struct bt_mesh_blob_target * target,enum bt_mesh_blob_status reason)302 static void blob_cli_lost_target(struct bt_mesh_blob_cli *cli,
303 				 struct bt_mesh_blob_target *target,
304 				 enum bt_mesh_blob_status reason)
305 {
306 	LOG_DBG("Mesh Blob: Lost target 0x%04x (reason: %u)", target->addr,
307 		reason);
308 	tester_event(BTP_SERVICE_ID_MESH, MESH_EV_BLOB_LOST_TARGET, NULL, 0);
309 }
310 
blob_cli_caps(struct bt_mesh_blob_cli * cli,const struct bt_mesh_blob_cli_caps * caps)311 static void blob_cli_caps(struct bt_mesh_blob_cli *cli,
312 			  const struct bt_mesh_blob_cli_caps *caps)
313 {
314 	const char *const modes[] = {
315 		"none",
316 		"push",
317 		"pull",
318 		"all",
319 	};
320 
321 	if (!caps) {
322 		LOG_DBG("None of the targets can be used for BLOB transfer");
323 		return;
324 	}
325 
326 	LOG_DBG("Mesh BLOB: capabilities:");
327 	LOG_DBG("\tMax BLOB size: %u bytes", caps->max_size);
328 	LOG_DBG("\tBlock size: %u-%u (%u-%u bytes)", caps->min_block_size_log,
329 		caps->max_block_size_log, 1 << caps->min_block_size_log,
330 		1 << caps->max_block_size_log);
331 	LOG_DBG("\tMax chunks: %u", caps->max_chunks);
332 	LOG_DBG("\tChunk size: %u", caps->max_chunk_size);
333 	LOG_DBG("\tMTU size: %u", caps->mtu_size);
334 	LOG_DBG("\tModes: %s", modes[caps->modes]);
335 }
336 
blob_cli_end(struct bt_mesh_blob_cli * cli,const struct bt_mesh_blob_xfer * xfer,bool success)337 static void blob_cli_end(struct bt_mesh_blob_cli *cli,
338 			 const struct bt_mesh_blob_xfer *xfer, bool success)
339 {
340 	if (success) {
341 		LOG_DBG("Mesh BLOB transfer complete.");
342 	} else {
343 		LOG_DBG("Mesh BLOB transfer failed.");
344 	}
345 }
346 
347 static const struct bt_mesh_blob_cli_cb blob_cli_handlers = {
348 	.lost_target = blob_cli_lost_target,
349 	.caps = blob_cli_caps,
350 	.end = blob_cli_end,
351 };
352 
353 static struct bt_mesh_blob_cli blob_cli = { .cb = &blob_cli_handlers };
354 #endif
355 
356 #if defined(CONFIG_BT_MESH_DFU_SRV)
357 const char *metadata_data = "1100000000000011";
358 
359 static uint8_t dfu_fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN] = {
360 	0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
361 };
362 
363 static struct bt_mesh_dfu_img dfu_imgs[] = { {
364 	.fwid = &dfu_fwid,
365 	.fwid_len = sizeof(dfu_fwid),
366 } };
367 
dfu_meta_check(struct bt_mesh_dfu_srv * srv,const struct bt_mesh_dfu_img * img,struct net_buf_simple * metadata,enum bt_mesh_dfu_effect * effect)368 static int dfu_meta_check(struct bt_mesh_dfu_srv *srv,
369 			  const struct bt_mesh_dfu_img *img,
370 			  struct net_buf_simple *metadata,
371 			  enum bt_mesh_dfu_effect *effect)
372 {
373 	char string[2 * CONFIG_BT_MESH_DFU_METADATA_MAXLEN + 1];
374 	int i;
375 	size_t len;
376 
377 	len = bin2hex(metadata->data, metadata->len, string, sizeof(string));
378 	string[len] = '\0';
379 
380 	for (i = 0; i <= len; i++) {
381 		if (string[i] != metadata_data[i]) {
382 			LOG_ERR("Wrong Firmware Metadata");
383 			return -EINVAL;
384 		}
385 	}
386 
387 	return 0;
388 }
389 
dfu_start(struct bt_mesh_dfu_srv * srv,const struct bt_mesh_dfu_img * img,struct net_buf_simple * metadata,const struct bt_mesh_blob_io ** io)390 static int dfu_start(struct bt_mesh_dfu_srv *srv,
391 		     const struct bt_mesh_dfu_img *img,
392 		     struct net_buf_simple *metadata,
393 		     const struct bt_mesh_blob_io **io)
394 {
395 	LOG_DBG("DFU setup");
396 
397 	*io = &dummy_blob_io;
398 
399 	return 0;
400 }
401 
dfu_end(struct bt_mesh_dfu_srv * srv,const struct bt_mesh_dfu_img * img,bool success)402 static void dfu_end(struct bt_mesh_dfu_srv *srv,
403 		    const struct bt_mesh_dfu_img *img, bool success)
404 {
405 	if (!success) {
406 		LOG_ERR("DFU failed");
407 		return;
408 	}
409 
410 	if (!blob_valid) {
411 		bt_mesh_dfu_srv_rejected(srv);
412 		return;
413 	}
414 
415 	bt_mesh_dfu_srv_verified(srv);
416 }
417 
dfu_apply(struct bt_mesh_dfu_srv * srv,const struct bt_mesh_dfu_img * img)418 static int dfu_apply(struct bt_mesh_dfu_srv *srv,
419 		     const struct bt_mesh_dfu_img *img)
420 {
421 	if (!blob_valid) {
422 		return -EINVAL;
423 	}
424 
425 	LOG_DBG("Applying DFU transfer...");
426 
427 #if defined(CONFIG_BT_MESH_DFD_SRV)
428 	if (is_self_update(&dfd_srv) && dfu_self_update_slot != NULL) {
429 		LOG_DBG("Swapping fwid for self-update");
430 		/* Simulate self-update by swapping fwid. */
431 		memcpy(&dfu_fwid[0], dfu_self_update_slot->fwid, dfu_self_update_slot->fwid_len);
432 		dfu_imgs[0].fwid_len = dfu_self_update_slot->fwid_len;
433 	}
434 
435 #endif
436 
437 	return 0;
438 }
439 
440 static const struct bt_mesh_dfu_srv_cb dfu_handlers = {
441 	.check = dfu_meta_check,
442 	.start = dfu_start,
443 	.end = dfu_end,
444 	.apply = dfu_apply,
445 };
446 
447 static struct bt_mesh_dfu_srv dfu_srv =
448 	BT_MESH_DFU_SRV_INIT(&dfu_handlers, dfu_imgs, ARRAY_SIZE(dfu_imgs));
449 #endif /* CONFIG_BT_MESH_DFU_SRV */
450 
451 /* Model Authentication Method */
452 #define AUTH_METHOD_STATIC 0x01
453 #define AUTH_METHOD_OUTPUT 0x02
454 #define AUTH_METHOD_INPUT 0x03
455 
456 static struct model_data {
457 	const struct bt_mesh_model *model;
458 	uint16_t addr;
459 	uint16_t appkey_idx;
460 } model_bound[MODEL_BOUNDS_MAX];
461 
462 static struct {
463 	uint16_t local;
464 	uint16_t dst;
465 	uint16_t net_idx;
466 } net = {
467 	.local = BT_MESH_ADDR_UNASSIGNED,
468 	.dst = BT_MESH_ADDR_UNASSIGNED,
469 };
470 
supported_commands(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)471 static uint8_t supported_commands(const void *cmd, uint16_t cmd_len,
472 				  void *rsp, uint16_t *rsp_len)
473 {
474 	struct btp_mesh_read_supported_commands_rp *rp = rsp;
475 
476 	/* octet 0 */
477 	tester_set_bit(rp->data, BTP_MESH_READ_SUPPORTED_COMMANDS);
478 	tester_set_bit(rp->data, BTP_MESH_CONFIG_PROVISIONING);
479 	tester_set_bit(rp->data, BTP_MESH_PROVISION_NODE);
480 	tester_set_bit(rp->data, BTP_MESH_INIT);
481 	tester_set_bit(rp->data, BTP_MESH_RESET);
482 	tester_set_bit(rp->data, BTP_MESH_INPUT_NUMBER);
483 	tester_set_bit(rp->data, BTP_MESH_INPUT_STRING);
484 
485 	/* octet 1 */
486 	tester_set_bit(rp->data, BTP_MESH_IVU_TEST_MODE);
487 	tester_set_bit(rp->data, BTP_MESH_IVU_TOGGLE_STATE);
488 	tester_set_bit(rp->data, BTP_MESH_NET_SEND);
489 	tester_set_bit(rp->data, BTP_MESH_HEALTH_GENERATE_FAULTS);
490 	tester_set_bit(rp->data, BTP_MESH_HEALTH_CLEAR_FAULTS);
491 	tester_set_bit(rp->data, BTP_MESH_LPN);
492 	tester_set_bit(rp->data, BTP_MESH_LPN_POLL);
493 	tester_set_bit(rp->data, BTP_MESH_MODEL_SEND);
494 
495 	/* octet 2 */
496 #if defined(CONFIG_BT_TESTING)
497 	tester_set_bit(rp->data, BTP_MESH_LPN_SUBSCRIBE);
498 	tester_set_bit(rp->data, BTP_MESH_LPN_UNSUBSCRIBE);
499 	tester_set_bit(rp->data, BTP_MESH_RPL_CLEAR);
500 #endif /* CONFIG_BT_TESTING */
501 	tester_set_bit(rp->data, BTP_MESH_PROXY_IDENTITY);
502 	tester_set_bit(rp->data, BTP_MESH_COMP_DATA_GET);
503 	tester_set_bit(rp->data, BTP_MESH_CFG_BEACON_GET);
504 	tester_set_bit(rp->data, BTP_MESH_CFG_BEACON_SET);
505 
506 	/* octet 3 */
507 	tester_set_bit(rp->data, BTP_MESH_CFG_DEFAULT_TTL_GET);
508 	tester_set_bit(rp->data, BTP_MESH_CFG_DEFAULT_TTL_SET);
509 	tester_set_bit(rp->data, BTP_MESH_CFG_GATT_PROXY_GET);
510 	tester_set_bit(rp->data, BTP_MESH_CFG_GATT_PROXY_SET);
511 	tester_set_bit(rp->data, BTP_MESH_CFG_FRIEND_GET);
512 	tester_set_bit(rp->data, BTP_MESH_CFG_FRIEND_SET);
513 	tester_set_bit(rp->data, BTP_MESH_CFG_RELAY_GET);
514 	tester_set_bit(rp->data, BTP_MESH_CFG_RELAY_SET);
515 
516 	/* octet 4 */
517 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_PUB_GET);
518 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_PUB_SET);
519 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_ADD);
520 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_DEL);
521 	tester_set_bit(rp->data, BTP_MESH_CFG_NETKEY_ADD);
522 	tester_set_bit(rp->data, BTP_MESH_CFG_NETKEY_GET);
523 	tester_set_bit(rp->data, BTP_MESH_CFG_NETKEY_DEL);
524 	tester_set_bit(rp->data, BTP_MESH_CFG_APPKEY_ADD);
525 
526 	/* octet 5 */
527 	tester_set_bit(rp->data, BTP_MESH_CFG_APPKEY_DEL);
528 	tester_set_bit(rp->data, BTP_MESH_CFG_APPKEY_GET);
529 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_APP_BIND);
530 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_APP_UNBIND);
531 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_APP_GET);
532 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_APP_VND_GET);
533 	tester_set_bit(rp->data, BTP_MESH_CFG_HEARTBEAT_PUB_SET);
534 	tester_set_bit(rp->data, BTP_MESH_CFG_HEARTBEAT_PUB_GET);
535 
536 	/* octet 6 */
537 	tester_set_bit(rp->data, BTP_MESH_CFG_HEARTBEAT_SUB_SET);
538 	tester_set_bit(rp->data, BTP_MESH_CFG_HEARTBEAT_SUB_GET);
539 	tester_set_bit(rp->data, BTP_MESH_CFG_NET_TRANS_GET);
540 	tester_set_bit(rp->data, BTP_MESH_CFG_NET_TRANS_SET);
541 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_OVW);
542 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_DEL_ALL);
543 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_GET);
544 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_GET_VND);
545 
546 	/* octet 7 */
547 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_VA_ADD);
548 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_VA_DEL);
549 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_SUB_VA_OVW);
550 	tester_set_bit(rp->data, BTP_MESH_CFG_NETKEY_UPDATE);
551 	tester_set_bit(rp->data, BTP_MESH_CFG_APPKEY_UPDATE);
552 	tester_set_bit(rp->data, BTP_MESH_CFG_NODE_IDT_SET);
553 	tester_set_bit(rp->data, BTP_MESH_CFG_NODE_IDT_GET);
554 	tester_set_bit(rp->data, BTP_MESH_CFG_NODE_RESET);
555 
556 	/* octet 8 */
557 	tester_set_bit(rp->data, BTP_MESH_CFG_LPN_TIMEOUT_GET);
558 	tester_set_bit(rp->data, BTP_MESH_CFG_MODEL_APP_BIND_VND);
559 	tester_set_bit(rp->data, BTP_MESH_HEALTH_FAULT_GET);
560 	tester_set_bit(rp->data, BTP_MESH_HEALTH_FAULT_CLEAR);
561 	tester_set_bit(rp->data, BTP_MESH_HEALTH_PERIOD_GET);
562 	tester_set_bit(rp->data, BTP_MESH_HEALTH_PERIOD_SET);
563 
564 	/* octet 9 */
565 	tester_set_bit(rp->data, BTP_MESH_HEALTH_ATTENTION_GET);
566 	tester_set_bit(rp->data, BTP_MESH_HEALTH_ATTENTION_SET);
567 	tester_set_bit(rp->data, BTP_MESH_PROVISION_ADV);
568 	tester_set_bit(rp->data, BTP_MESH_CFG_KRP_GET);
569 	tester_set_bit(rp->data, BTP_MESH_CFG_KRP_SET);
570 	tester_set_bit(rp->data, BTP_MESH_VA_ADD);
571 	tester_set_bit(rp->data, BTP_MESH_VA_DEL);
572 
573 	*rsp_len = sizeof(*rp) + 10;
574 
575 	return BTP_STATUS_SUCCESS;
576 }
577 
get_faults(uint8_t * faults,uint8_t faults_size,uint8_t * dst,uint8_t * count)578 static void get_faults(uint8_t *faults, uint8_t faults_size, uint8_t *dst, uint8_t *count)
579 {
580 	uint8_t i, limit = *count;
581 
582 	for (i = 0U, *count = 0U; i < faults_size && *count < limit; i++) {
583 		if (faults[i]) {
584 			*dst++ = faults[i];
585 			(*count)++;
586 		}
587 	}
588 }
589 
fault_get_cur(const struct bt_mesh_model * model,uint8_t * test_id,uint16_t * company_id,uint8_t * faults,uint8_t * fault_count)590 static int fault_get_cur(const struct bt_mesh_model *model, uint8_t *test_id,
591 			 uint16_t *company_id, uint8_t *faults, uint8_t *fault_count)
592 {
593 	LOG_DBG("");
594 
595 	*test_id = HEALTH_TEST_ID;
596 	*company_id = CID_LOCAL;
597 
598 	get_faults(cur_faults, sizeof(cur_faults), faults, fault_count);
599 
600 	return 0;
601 }
602 
fault_get_reg(const struct bt_mesh_model * model,uint16_t company_id,uint8_t * test_id,uint8_t * faults,uint8_t * fault_count)603 static int fault_get_reg(const struct bt_mesh_model *model, uint16_t company_id,
604 			 uint8_t *test_id, uint8_t *faults, uint8_t *fault_count)
605 {
606 	LOG_DBG("company_id 0x%04x", company_id);
607 
608 	if (company_id != CID_LOCAL) {
609 		return -EINVAL;
610 	}
611 
612 	*test_id = HEALTH_TEST_ID;
613 
614 	get_faults(reg_faults, sizeof(reg_faults), faults, fault_count);
615 
616 	return 0;
617 }
618 
fault_clear(const struct bt_mesh_model * model,uint16_t company_id)619 static int fault_clear(const struct bt_mesh_model *model, uint16_t company_id)
620 {
621 	LOG_DBG("company_id 0x%04x", company_id);
622 
623 	if (company_id != CID_LOCAL) {
624 		return -EINVAL;
625 	}
626 
627 	(void)memset(reg_faults, 0, sizeof(reg_faults));
628 
629 	return 0;
630 }
631 
fault_test(const struct bt_mesh_model * model,uint8_t test_id,uint16_t company_id)632 static int fault_test(const struct bt_mesh_model *model, uint8_t test_id,
633 		      uint16_t company_id)
634 {
635 	LOG_DBG("test_id 0x%02x company_id 0x%04x", test_id, company_id);
636 
637 	if (company_id != CID_LOCAL || test_id != HEALTH_TEST_ID) {
638 		return -EINVAL;
639 	}
640 
641 	return 0;
642 }
643 
644 static const struct bt_mesh_health_srv_cb health_srv_cb = {
645 	.fault_get_cur = fault_get_cur,
646 	.fault_get_reg = fault_get_reg,
647 	.fault_clear = fault_clear,
648 	.fault_test = fault_test,
649 };
650 
show_faults(uint8_t test_id,uint16_t cid,uint8_t * faults,size_t fault_count)651 static void show_faults(uint8_t test_id, uint16_t cid, uint8_t *faults, size_t fault_count)
652 {
653 	size_t i;
654 
655 	if (!fault_count) {
656 		LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x: no faults",
657 			test_id, cid);
658 		return;
659 	}
660 
661 	LOG_DBG("Health Test ID 0x%02x Company ID 0x%04x Fault Count %zu: ",
662 		test_id, cid, fault_count);
663 
664 	for (i = 0; i < fault_count; i++) {
665 		LOG_DBG("0x%02x", faults[i]);
666 	}
667 }
668 
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)669 static void health_current_status(struct bt_mesh_health_cli *cli, uint16_t addr,
670 				  uint8_t test_id, uint16_t cid, uint8_t *faults,
671 				  size_t fault_count)
672 {
673 	LOG_DBG("Health Current Status from 0x%04x", addr);
674 	show_faults(test_id, cid, faults, fault_count);
675 }
676 
677 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
678 static struct bt_mesh_large_comp_data_cli lcd_cli = {
679 };
680 #endif
681 
682 static struct bt_mesh_health_cli health_cli = {
683 	.current_status = health_current_status,
684 };
685 
686 
687 static uint8_t health_tests[] = {
688 	BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_LF, 6, 0x01, 0x02, 0x03, 0x04, 0x34,
689 				 0x15),
690 	BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_NORDIC_SEMI, 3, 0x01, 0x02, 0x03),
691 };
692 
693 static const uint8_t zero_metadata[100];
694 
695 static const struct bt_mesh_models_metadata_entry health_srv_meta[] = {
696 	BT_MESH_HEALTH_TEST_INFO_METADATA(health_tests),
697 	{
698 		.len = ARRAY_SIZE(zero_metadata),
699 		.id = 0xABCD,
700 		.data = zero_metadata,
701 	},
702 	BT_MESH_MODELS_METADATA_END,
703 };
704 
705 static const uint8_t health_tests_alt[] = {
706 	BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_LF, 6, 0x11, 0x22, 0x33, 0x44, 0x55,
707 				 0x66),
708 	BT_MESH_HEALTH_TEST_INFO(COMPANY_ID_NORDIC_SEMI, 3, 0x11, 0x22, 0x33),
709 };
710 
711 static const struct bt_mesh_models_metadata_entry health_srv_meta_alt[] = {
712 	BT_MESH_HEALTH_TEST_INFO_METADATA(health_tests_alt),
713 	{
714 		.len = ARRAY_SIZE(zero_metadata),
715 		.id = 0xFEED,
716 		.data = zero_metadata,
717 	},
718 	BT_MESH_MODELS_METADATA_END,
719 };
720 
721 static struct bt_mesh_health_srv health_srv = {
722 	.cb = &health_srv_cb,
723 };
724 
725 BT_MESH_HEALTH_PUB_DEFINE(health_pub, CUR_FAULTS_MAX);
726 
727 static struct bt_mesh_cfg_cli cfg_cli = {
728 };
729 
730 #if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
731 static struct bt_mesh_sar_cfg_cli sar_cfg_cli;
732 #endif
733 
734 #if defined(CONFIG_BT_MESH_RPR_CLI)
rpr_scan_report(struct bt_mesh_rpr_cli * cli,const struct bt_mesh_rpr_node * srv,struct bt_mesh_rpr_unprov * unprov,struct net_buf_simple * adv_data)735 static void rpr_scan_report(struct bt_mesh_rpr_cli *cli,
736 			    const struct bt_mesh_rpr_node *srv,
737 			    struct bt_mesh_rpr_unprov *unprov,
738 			    struct net_buf_simple *adv_data)
739 {
740 	char uuid_hex_str[32 + 1];
741 
742 	bin2hex(unprov->uuid, 16, uuid_hex_str, sizeof(uuid_hex_str));
743 
744 	LOG_DBG("Server 0x%04x:\n"
745 		    "\tuuid:   %s\n"
746 		    "\tOOB:    0x%04x",
747 		    srv->addr, uuid_hex_str, unprov->oob);
748 
749 	while (adv_data && adv_data->len > 2) {
750 		uint8_t len, type;
751 		uint8_t data[31];
752 
753 		len = net_buf_simple_pull_u8(adv_data) - 1;
754 		type = net_buf_simple_pull_u8(adv_data);
755 		memcpy(data, net_buf_simple_pull_mem(adv_data, len), len);
756 		data[len] = '\0';
757 
758 		if (type == BT_DATA_URI) {
759 			LOG_DBG("\tURI:    \"\\x%02x%s\"",
760 				data[0], &data[1]);
761 		} else if (type == BT_DATA_NAME_COMPLETE) {
762 			LOG_DBG("\tName:   \"%s\"", data);
763 		} else {
764 			char string[64 + 1];
765 
766 			bin2hex(data, len, string, sizeof(string));
767 			LOG_DBG("\t0x%02x:  %s", type, string);
768 		}
769 	}
770 }
771 
772 static struct bt_mesh_rpr_cli rpr_cli = {
773 	.scan_report = rpr_scan_report,
774 };
775 #endif
776 
777 #if defined(CONFIG_BT_MESH_DFU_SRV)
dfu_srv_apply(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)778 static uint8_t dfu_srv_apply(const void *cmd, uint16_t cmd_len,
779 			     void *rsp, uint16_t *rsp_len)
780 {
781 	LOG_DBG("Applying image on server");
782 	bt_mesh_dfu_srv_applied(&dfu_srv);
783 	return BTP_STATUS_SUCCESS;
784 }
785 #endif
786 
787 #ifdef CONFIG_BT_MESH_PRIV_BEACON_CLI
788 static struct bt_mesh_priv_beacon_cli priv_beacon_cli;
789 
priv_beacon_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)790 static uint8_t priv_beacon_get(const void *cmd, uint16_t cmd_len,
791 			       void *rsp, uint16_t *rsp_len)
792 {
793 	const struct btp_priv_beacon_get_cmd *cp = cmd;
794 
795 	struct bt_mesh_priv_beacon val;
796 	int err;
797 
798 	err = bt_mesh_priv_beacon_cli_get(net.net_idx, cp->dst, &val);
799 	if (err) {
800 		LOG_ERR("Failed to send Private Beacon Get (err %d)", err);
801 		return BTP_STATUS_FAILED;
802 	}
803 
804 	LOG_DBG("Private Beacon state: %u, %u", val.enabled, val.rand_interval);
805 	return BTP_STATUS_SUCCESS;
806 }
807 
priv_beacon_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)808 static uint8_t priv_beacon_set(const void *cmd, uint16_t cmd_len,
809 			       void *rsp, uint16_t *rsp_len)
810 {
811 	const struct btp_priv_beacon_set_cmd *cp = cmd;
812 	struct bt_mesh_priv_beacon val;
813 	int err;
814 
815 	val.enabled = cp->enabled;
816 	val.rand_interval = cp->rand_interval;
817 
818 	err = bt_mesh_priv_beacon_cli_set(net.net_idx, cp->dst, &val, &val);
819 	if (err) {
820 		LOG_ERR("Failed to send Private Beacon Set (err %d)", err);
821 		return BTP_STATUS_FAILED;
822 	}
823 
824 	return BTP_STATUS_SUCCESS;
825 }
826 
priv_gatt_proxy_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)827 static uint8_t priv_gatt_proxy_get(const void *cmd, uint16_t cmd_len,
828 				   void *rsp, uint16_t *rsp_len)
829 {
830 	const struct btp_priv_gatt_proxy_get_cmd *cp = cmd;
831 
832 	uint8_t state;
833 	int err;
834 
835 	err = bt_mesh_priv_beacon_cli_gatt_proxy_get(net.net_idx, cp->dst, &state);
836 	if (err) {
837 		LOG_ERR("Failed to send Private GATT Proxy Get (err %d)", err);
838 		return BTP_STATUS_FAILED;
839 	}
840 
841 	LOG_DBG("Private GATT Proxy state: %u", state);
842 	return BTP_STATUS_SUCCESS;
843 }
844 
priv_gatt_proxy_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)845 static uint8_t priv_gatt_proxy_set(const void *cmd, uint16_t cmd_len,
846 				   void *rsp, uint16_t *rsp_len)
847 {
848 	const struct btp_priv_gatt_proxy_set_cmd *cp = cmd;
849 
850 	uint8_t state;
851 	int err;
852 
853 	state = cp->state;
854 
855 	err = bt_mesh_priv_beacon_cli_gatt_proxy_set(net.net_idx, cp->dst, state, &state);
856 	if (err) {
857 		LOG_ERR("Failed to send Private GATT Proxy Set (err %d)", err);
858 		return BTP_STATUS_FAILED;
859 	}
860 
861 	return BTP_STATUS_SUCCESS;
862 }
863 
priv_node_id_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)864 static uint8_t priv_node_id_get(const void *cmd, uint16_t cmd_len,
865 				void *rsp, uint16_t *rsp_len)
866 {
867 	const struct btp_priv_node_id_get_cmd *cp = cmd;
868 	struct bt_mesh_priv_node_id val;
869 	uint16_t key_net_idx;
870 	int err;
871 
872 	key_net_idx = cp->key_net_idx;
873 
874 	err = bt_mesh_priv_beacon_cli_node_id_get(net.net_idx, cp->dst, key_net_idx, &val);
875 	if (err) {
876 		LOG_ERR("Failed to send Private Node Identity Get (err %d)", err);
877 		return BTP_STATUS_FAILED;
878 	}
879 
880 	LOG_DBG("Private Node Identity state: %u %u %u", val.net_idx, val.state, val.status);
881 	return BTP_STATUS_SUCCESS;
882 }
883 
priv_node_id_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)884 static uint8_t priv_node_id_set(const void *cmd, uint16_t cmd_len,
885 				void *rsp, uint16_t *rsp_len)
886 {
887 	const struct btp_priv_node_id_set_cmd *cp = cmd;
888 	struct bt_mesh_priv_node_id val;
889 	int err;
890 
891 	val.net_idx = cp->net_idx;
892 	val.state = cp->state;
893 
894 	err = bt_mesh_priv_beacon_cli_node_id_set(net.net_idx, cp->dst, &val, &val);
895 	if (err) {
896 		LOG_ERR("Failed to send Private Node Identity Set (err %d)", err);
897 		return BTP_STATUS_FAILED;
898 	}
899 
900 	return BTP_STATUS_SUCCESS;
901 }
902 
proxy_private_identity_enable(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)903 static uint8_t proxy_private_identity_enable(const void *cmd, uint16_t cmd_len,
904 					     void *rsp, uint16_t *rsp_len)
905 {
906 	int err;
907 
908 	LOG_DBG("");
909 
910 	err = bt_mesh_proxy_private_identity_enable();
911 	if (err) {
912 		LOG_ERR("Failed to enable proxy private identity (err %d)", err);
913 		return BTP_STATUS_FAILED;
914 	}
915 
916 	return BTP_STATUS_SUCCESS;
917 }
918 #endif
919 
920 #if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
921 static struct bt_mesh_sol_pdu_rpl_cli srpl_cli;
922 #endif
923 
924 
925 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
926 static struct bt_mesh_od_priv_proxy_cli od_priv_proxy_cli;
927 
od_priv_proxy_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)928 static uint8_t od_priv_proxy_get(const void *cmd, uint16_t cmd_len,
929 				 void *rsp, uint16_t *rsp_len)
930 {
931 	const struct btp_od_priv_proxy_get_cmd *cp = cmd;
932 	uint8_t val_rsp;
933 	int err;
934 
935 	LOG_DBG("");
936 
937 	err = bt_mesh_od_priv_proxy_cli_get(net.net_idx, cp->dst, &val_rsp);
938 	if (err) {
939 		LOG_ERR("Failed to get On-Demand Private Proxy state (err %d)", err);
940 		return BTP_STATUS_FAILED;
941 	}
942 
943 	LOG_DBG("On-Demand Private Proxy state: %u", val_rsp);
944 
945 	return BTP_STATUS_SUCCESS;
946 }
947 
od_priv_proxy_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)948 static uint8_t od_priv_proxy_set(const void *cmd, uint16_t cmd_len,
949 				 void *rsp, uint16_t *rsp_len)
950 {
951 	const struct btp_od_priv_proxy_set_cmd *cp = cmd;
952 	uint8_t val_rsp;
953 	int err;
954 
955 	LOG_DBG("");
956 
957 	err = bt_mesh_od_priv_proxy_cli_set(net.net_idx, cp->dst, cp->val, &val_rsp);
958 	if (err) {
959 		LOG_ERR("Failed to set On-Demand Private Proxy state (err %d)", err);
960 		return BTP_STATUS_FAILED;
961 	}
962 
963 	LOG_DBG("On-Demand Private Proxy set state: %u", val_rsp);
964 
965 	return BTP_STATUS_SUCCESS;
966 }
967 
968 #endif
969 
970 #if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
srpl_clear(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)971 static uint8_t srpl_clear(const void *cmd, uint16_t cmd_len,
972 			  void *rsp, uint16_t *rsp_len)
973 {
974 	const struct btp_srpl_clear_cmd *cp = cmd;
975 	uint16_t app_idx = BT_MESH_KEY_UNUSED;
976 	uint16_t start_rsp;
977 	uint8_t len_rsp;
978 	int err;
979 
980 	/* Lookup source address */
981 	for (int i = 0; i < ARRAY_SIZE(model_bound); i++) {
982 		if (model_bound[i].model->id == BT_MESH_MODEL_ID_SOL_PDU_RPL_CLI) {
983 			app_idx = model_bound[i].appkey_idx;
984 			break;
985 		}
986 	}
987 
988 	struct bt_mesh_msg_ctx ctx = BT_MESH_MSG_CTX_INIT_APP(app_idx, cp->dst);
989 
990 	if (cp->acked) {
991 		err = bt_mesh_sol_pdu_rpl_clear(&ctx, cp->range_start, cp->range_len, &start_rsp,
992 						&len_rsp);
993 	} else {
994 		err = bt_mesh_sol_pdu_rpl_clear_unack(&ctx, cp->range_start, cp->range_len);
995 	}
996 	if (err) {
997 		LOG_ERR("Failed to clear SRPL (err %d)", err);
998 		return BTP_STATUS_FAILED;
999 	}
1000 
1001 	return BTP_STATUS_SUCCESS;
1002 }
1003 #endif
1004 
1005 #if defined(CONFIG_BT_MESH_PROXY_SOLICITATION)
proxy_solicit(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1006 static uint8_t proxy_solicit(const void *cmd, uint16_t cmd_len,
1007 			     void *rsp, uint16_t *rsp_len)
1008 {
1009 	const struct btp_proxy_solicit_cmd *cp = cmd;
1010 	int err;
1011 
1012 	err = bt_mesh_proxy_solicit(cp->net_idx);
1013 	if (err) {
1014 		LOG_ERR("Failed to advertise solicitation PDU (err %d)", err);
1015 		return BTP_STATUS_FAILED;
1016 	}
1017 
1018 	return BTP_STATUS_SUCCESS;
1019 }
1020 #endif /* CONFIG_BT_MESH_PROXY_SOLICITATION */
1021 
1022 static const struct bt_mesh_model root_models[] = {
1023 	BT_MESH_MODEL_CFG_SRV,
1024 	BT_MESH_MODEL_CFG_CLI(&cfg_cli),
1025 	BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub, health_srv_meta),
1026 	BT_MESH_MODEL_HEALTH_CLI(&health_cli),
1027 #if defined(CONFIG_BT_MESH_SAR_CFG_SRV)
1028 	BT_MESH_MODEL_SAR_CFG_SRV,
1029 #endif
1030 #if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
1031 	BT_MESH_MODEL_SAR_CFG_CLI(&sar_cfg_cli),
1032 #endif
1033 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV)
1034 	BT_MESH_MODEL_LARGE_COMP_DATA_SRV,
1035 #endif
1036 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
1037 	BT_MESH_MODEL_LARGE_COMP_DATA_CLI(&lcd_cli),
1038 #endif
1039 #if defined(CONFIG_BT_MESH_OP_AGG_SRV)
1040 	BT_MESH_MODEL_OP_AGG_SRV,
1041 #endif
1042 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
1043 	BT_MESH_MODEL_OP_AGG_CLI,
1044 #endif
1045 #if defined(CONFIG_BT_MESH_RPR_CLI)
1046 	BT_MESH_MODEL_RPR_CLI(&rpr_cli),
1047 #endif
1048 #if defined(CONFIG_BT_MESH_RPR_SRV)
1049 	BT_MESH_MODEL_RPR_SRV,
1050 #endif
1051 #if defined(CONFIG_BT_MESH_DFD_SRV)
1052 	BT_MESH_MODEL_DFD_SRV(&dfd_srv),
1053 #endif
1054 #if defined(CONFIG_BT_MESH_DFU_SRV)
1055 	BT_MESH_MODEL_DFU_SRV(&dfu_srv),
1056 #endif
1057 #if defined(CONFIG_BT_MESH_BLOB_CLI) && !defined(CONFIG_BT_MESH_DFD_SRV)
1058 	BT_MESH_MODEL_BLOB_CLI(&blob_cli),
1059 #endif
1060 #if defined(CONFIG_BT_MESH_PRIV_BEACON_SRV)
1061 	BT_MESH_MODEL_PRIV_BEACON_SRV,
1062 #endif
1063 #if defined(CONFIG_BT_MESH_PRIV_BEACON_CLI)
1064 	BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli),
1065 #endif
1066 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
1067 	BT_MESH_MODEL_OD_PRIV_PROXY_CLI(&od_priv_proxy_cli),
1068 #endif
1069 #if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
1070 	BT_MESH_MODEL_SOL_PDU_RPL_CLI(&srpl_cli),
1071 #endif
1072 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
1073 	BT_MESH_MODEL_OD_PRIV_PROXY_SRV,
1074 #endif
1075 
1076 };
1077 
1078 static const struct bt_mesh_model root_models_alt[] = {
1079 	BT_MESH_MODEL_CFG_SRV,
1080 	BT_MESH_MODEL_CFG_CLI(&cfg_cli),
1081 	BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub, health_srv_meta_alt),
1082 	BT_MESH_MODEL_HEALTH_CLI(&health_cli),
1083 #if defined(CONFIG_BT_MESH_SAR_CFG_SRV)
1084 	BT_MESH_MODEL_SAR_CFG_SRV,
1085 #endif
1086 #if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
1087 	BT_MESH_MODEL_SAR_CFG_CLI(&sar_cfg_cli),
1088 #endif
1089 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_SRV)
1090 	BT_MESH_MODEL_LARGE_COMP_DATA_SRV,
1091 #endif
1092 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
1093 	BT_MESH_MODEL_LARGE_COMP_DATA_CLI(&lcd_cli),
1094 #endif
1095 #if defined(CONFIG_BT_MESH_OP_AGG_SRV)
1096 	BT_MESH_MODEL_OP_AGG_SRV,
1097 #endif
1098 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
1099 	BT_MESH_MODEL_OP_AGG_CLI,
1100 #endif
1101 #if defined(CONFIG_BT_MESH_RPR_CLI)
1102 	BT_MESH_MODEL_RPR_CLI(&rpr_cli),
1103 #endif
1104 #if defined(CONFIG_BT_MESH_RPR_SRV)
1105 	BT_MESH_MODEL_RPR_SRV,
1106 #endif
1107 #if defined(CONFIG_BT_MESH_DFD_SRV)
1108 	BT_MESH_MODEL_DFD_SRV(&dfd_srv),
1109 #endif
1110 #if defined(CONFIG_BT_MESH_DFU_SRV)
1111 	BT_MESH_MODEL_DFU_SRV(&dfu_srv),
1112 #endif
1113 #if defined(CONFIG_BT_MESH_BLOB_CLI) && !defined(CONFIG_BT_MESH_DFD_SRV)
1114 	BT_MESH_MODEL_BLOB_CLI(&blob_cli),
1115 #endif
1116 #if defined(CONFIG_BT_MESH_PRIV_BEACON_SRV)
1117 	BT_MESH_MODEL_PRIV_BEACON_SRV,
1118 #endif
1119 #if defined(CONFIG_BT_MESH_PRIV_BEACON_CLI)
1120 	BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli),
1121 #endif
1122 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
1123 	BT_MESH_MODEL_OD_PRIV_PROXY_CLI(&od_priv_proxy_cli),
1124 #endif
1125 #if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
1126 	BT_MESH_MODEL_SOL_PDU_RPL_CLI(&srpl_cli),
1127 #endif
1128 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
1129 	BT_MESH_MODEL_OD_PRIV_PROXY_SRV,
1130 #endif
1131 
1132 };
lookup_model_bound(uint16_t id)1133 struct model_data *lookup_model_bound(uint16_t id)
1134 {
1135 	int i;
1136 
1137 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
1138 		if (model_bound[i].model && model_bound[i].model->id == id) {
1139 			return &model_bound[i];
1140 		}
1141 	}
1142 
1143 	return NULL;
1144 }
1145 static const struct bt_mesh_model vnd_models[] = {
1146 	BT_MESH_MODEL_VND(CID_LOCAL, VND_MODEL_ID_1, BT_MESH_MODEL_NO_OPS, NULL,
1147 			  NULL),
1148 };
1149 
1150 static const struct bt_mesh_elem elements[] = {
1151 	BT_MESH_ELEM(0, root_models, vnd_models),
1152 };
1153 
1154 static const struct bt_mesh_elem elements_alt[] = {
1155 	BT_MESH_ELEM(0, root_models_alt, vnd_models),
1156 };
1157 
link_open(bt_mesh_prov_bearer_t bearer)1158 static void link_open(bt_mesh_prov_bearer_t bearer)
1159 {
1160 	struct btp_mesh_prov_link_open_ev ev;
1161 
1162 	LOG_DBG("bearer 0x%02x", bearer);
1163 
1164 	switch (bearer) {
1165 	case BT_MESH_PROV_ADV:
1166 		ev.bearer = BTP_MESH_PROV_BEARER_PB_ADV;
1167 		break;
1168 	case BT_MESH_PROV_GATT:
1169 		ev.bearer = BTP_MESH_PROV_BEARER_PB_GATT;
1170 		break;
1171 	case BT_MESH_PROV_REMOTE:
1172 		ev.bearer = BTP_MESH_PROV_BEARER_REMOTE;
1173 		break;
1174 	default:
1175 		LOG_ERR("Invalid bearer");
1176 
1177 		return;
1178 	}
1179 
1180 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_OPEN, &ev, sizeof(ev));
1181 }
1182 
link_close(bt_mesh_prov_bearer_t bearer)1183 static void link_close(bt_mesh_prov_bearer_t bearer)
1184 {
1185 	struct btp_mesh_prov_link_closed_ev ev;
1186 
1187 	LOG_DBG("bearer 0x%02x", bearer);
1188 
1189 	switch (bearer) {
1190 	case BT_MESH_PROV_ADV:
1191 		ev.bearer = BTP_MESH_PROV_BEARER_PB_ADV;
1192 		break;
1193 	case BT_MESH_PROV_GATT:
1194 		ev.bearer = BTP_MESH_PROV_BEARER_PB_GATT;
1195 		break;
1196 	case BT_MESH_PROV_REMOTE:
1197 		ev.bearer = BTP_MESH_PROV_BEARER_REMOTE;
1198 		break;
1199 	default:
1200 		LOG_ERR("Invalid bearer");
1201 
1202 		return;
1203 	}
1204 
1205 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_LINK_CLOSED, &ev, sizeof(ev));
1206 }
1207 
output_number(bt_mesh_output_action_t action,uint32_t number)1208 static int output_number(bt_mesh_output_action_t action, uint32_t number)
1209 {
1210 	struct btp_mesh_out_number_action_ev ev;
1211 
1212 	LOG_DBG("action 0x%04x number 0x%08x", action, number);
1213 
1214 	ev.action = sys_cpu_to_le16(action);
1215 	ev.number = sys_cpu_to_le32(number);
1216 
1217 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_NUMBER_ACTION, &ev, sizeof(ev));
1218 
1219 	return 0;
1220 }
1221 
output_string(const char * str)1222 static int output_string(const char *str)
1223 {
1224 	struct btp_mesh_out_string_action_ev *ev;
1225 	struct net_buf_simple *buf = NET_BUF_SIMPLE(BTP_DATA_MAX_SIZE);
1226 
1227 	LOG_DBG("str %s", str);
1228 
1229 	net_buf_simple_init(buf, 0);
1230 
1231 	ev = net_buf_simple_add(buf, sizeof(*ev));
1232 	ev->string_len = strlen(str);
1233 
1234 	net_buf_simple_add_mem(buf, str, ev->string_len);
1235 
1236 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_OUT_STRING_ACTION,
1237 		    buf->data, buf->len);
1238 
1239 	return 0;
1240 }
1241 
input(bt_mesh_input_action_t action,uint8_t size)1242 static int input(bt_mesh_input_action_t action, uint8_t size)
1243 {
1244 	struct btp_mesh_in_action_ev ev;
1245 
1246 	LOG_DBG("action 0x%04x number 0x%02x", action, size);
1247 
1248 	input_size = size;
1249 
1250 	ev.action = sys_cpu_to_le16(action);
1251 	ev.size = size;
1252 
1253 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_IN_ACTION, &ev, sizeof(ev));
1254 
1255 	return 0;
1256 }
1257 
prov_complete(uint16_t net_idx,uint16_t addr)1258 static void prov_complete(uint16_t net_idx, uint16_t addr)
1259 {
1260 	LOG_DBG("net_idx 0x%04x addr 0x%04x", net_idx, addr);
1261 
1262 	net.net_idx = net_idx,
1263 	net.local = addr;
1264 	net.dst = addr;
1265 
1266 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROVISIONED, NULL, 0);
1267 }
1268 
prov_node_added(uint16_t net_idx,uint8_t uuid[16],uint16_t addr,uint8_t num_elem)1269 static void prov_node_added(uint16_t net_idx, uint8_t uuid[16], uint16_t addr,
1270 			    uint8_t num_elem)
1271 {
1272 	struct btp_mesh_prov_node_added_ev ev;
1273 
1274 	LOG_DBG("net_idx 0x%04x addr 0x%04x num_elem %d", net_idx, addr,
1275 		num_elem);
1276 
1277 	ev.net_idx = net_idx;
1278 	ev.addr = addr;
1279 	ev.num_elems = num_elem;
1280 	memcpy(&ev.uuid, uuid, sizeof(ev.uuid));
1281 
1282 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_PROV_NODE_ADDED, &ev, sizeof(ev));
1283 }
1284 
prov_reset(void)1285 static void prov_reset(void)
1286 {
1287 	LOG_DBG("");
1288 
1289 	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
1290 
1291 	if (IS_ENABLED(CONFIG_BT_MESH_RPR_SRV)) {
1292 		bt_mesh_prov_enable(BT_MESH_PROV_REMOTE);
1293 	}
1294 }
1295 
1296 static const struct bt_mesh_comp comp = {
1297 	.cid = CID_LOCAL,
1298 	.elem = elements,
1299 	.elem_count = ARRAY_SIZE(elements),
1300 	.vid = 1,
1301 };
1302 
1303 static const struct bt_mesh_comp comp_alt = {
1304 	.cid = CID_LOCAL,
1305 	.elem = elements_alt,
1306 	.elem_count = ARRAY_SIZE(elements_alt),
1307 	.vid = 2,
1308 };
1309 
1310 #if defined(CONFIG_BT_MESH_COMP_PAGE_2)
1311 static const uint8_t cmp2_elem_offset[1] = {0};
1312 
1313 static const struct bt_mesh_comp2_record comp_rec = {
1314 	.id = 0x1600,
1315 	.version.x = 1,
1316 	.version.y = 0,
1317 	.version.z = 0,
1318 	.elem_offset_cnt = 1,
1319 	.elem_offset = cmp2_elem_offset,
1320 	.data_len = 0
1321 };
1322 
1323 static const struct bt_mesh_comp2 comp_p2 = {.record_cnt = 1, .record = &comp_rec};
1324 #endif
1325 
1326 static struct bt_mesh_prov prov = {
1327 	.uuid = dev_uuid,
1328 	.static_val = static_auth,
1329 	.static_val_len = sizeof(static_auth),
1330 	.output_number = output_number,
1331 	.output_string = output_string,
1332 	.input = input,
1333 	.link_open = link_open,
1334 	.link_close = link_close,
1335 	.complete = prov_complete,
1336 	.node_added = prov_node_added,
1337 	.reset = prov_reset,
1338 	.uri = "Tester",
1339 };
1340 
config_prov(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1341 static uint8_t config_prov(const void *cmd, uint16_t cmd_len,
1342 			   void *rsp, uint16_t *rsp_len)
1343 {
1344 	const struct btp_mesh_config_provisioning_cmd *cp = cmd;
1345 	const struct btp_mesh_config_provisioning_cmd_v2 *cp2 = cmd;
1346 	int err = 0;
1347 
1348 	/* TODO consider fix BTP commands to avoid this */
1349 	if (cmd_len != sizeof(*cp) && cmd_len != (sizeof(*cp2))) {
1350 		LOG_DBG("wrong cmd size");
1351 		return BTP_STATUS_FAILED;
1352 	}
1353 
1354 	LOG_DBG("");
1355 
1356 	static_auth_size = cp->static_auth_size;
1357 
1358 	if (static_auth_size > BTP_MESH_PROV_AUTH_MAX_LEN || static_auth_size == 0) {
1359 		LOG_DBG("wrong static auth length");
1360 		return BTP_STATUS_FAILED;
1361 	}
1362 
1363 	memcpy(dev_uuid, cp->uuid, sizeof(dev_uuid));
1364 	memcpy(static_auth, cp->static_auth, cp->static_auth_size);
1365 
1366 	prov.output_size = cp->out_size;
1367 	prov.output_actions = sys_le16_to_cpu(cp->out_actions);
1368 	prov.input_size = cp->in_size;
1369 	prov.input_actions = sys_le16_to_cpu(cp->in_actions);
1370 
1371 	if (cmd_len == sizeof(*cp2)) {
1372 		memcpy(pub_key, cp2->set_pub_key, sizeof(cp2->set_pub_key));
1373 		memcpy(priv_key, cp2->set_priv_key, sizeof(cp2->set_priv_key));
1374 		prov.public_key_be = pub_key;
1375 		prov.private_key_be = priv_key;
1376 	}
1377 
1378 	if (cp->auth_method == AUTH_METHOD_OUTPUT) {
1379 		err = bt_mesh_auth_method_set_output(prov.output_actions, prov.output_size);
1380 	} else if (cp->auth_method == AUTH_METHOD_INPUT) {
1381 		err = bt_mesh_auth_method_set_input(prov.input_actions, prov.input_size);
1382 	} else if (cp->auth_method == AUTH_METHOD_STATIC) {
1383 		err = bt_mesh_auth_method_set_static(static_auth, static_auth_size);
1384 	}
1385 
1386 	if (err) {
1387 		LOG_ERR("err %d", err);
1388 		return BTP_STATUS_FAILED;
1389 	}
1390 
1391 	return BTP_STATUS_SUCCESS;
1392 }
1393 
provision_node(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1394 static uint8_t provision_node(const void *cmd, uint16_t cmd_len,
1395 			      void *rsp, uint16_t *rsp_len)
1396 {
1397 	const struct btp_mesh_provision_node_cmd *cp = cmd;
1398 	const struct btp_mesh_provision_node_cmd_v2 *cp2 = cmd;
1399 	int err;
1400 
1401 	/* TODO consider fix BTP commands to avoid this */
1402 	if (cmd_len != sizeof(*cp) && cmd_len != (sizeof(*cp2))) {
1403 		return BTP_STATUS_FAILED;
1404 	}
1405 
1406 	LOG_DBG("");
1407 
1408 	memcpy(dev_key, cp->dev_key, sizeof(dev_key));
1409 	memcpy(net_key, cp->net_key, sizeof(net_key));
1410 
1411 	addr = sys_le16_to_cpu(cp->addr);
1412 	flags = cp->flags;
1413 	iv_index = sys_le32_to_cpu(cp->iv_index);
1414 	net_key_idx = sys_le16_to_cpu(cp->net_key_idx);
1415 
1416 	if (cmd_len == sizeof(*cp2)) {
1417 		memcpy(pub_key, cp2->pub_key, sizeof(pub_key));
1418 
1419 		err = bt_mesh_prov_remote_pub_key_set(pub_key);
1420 		if (err) {
1421 			LOG_ERR("err %d", err);
1422 			return BTP_STATUS_FAILED;
1423 		}
1424 	}
1425 #if defined(CONFIG_BT_MESH_PROVISIONER)
1426 	err = bt_mesh_cdb_create(net_key);
1427 	if (err) {
1428 		LOG_ERR("err %d", err);
1429 		return BTP_STATUS_FAILED;
1430 	}
1431 #endif
1432 	err = bt_mesh_provision(net_key, net_key_idx, flags, iv_index, addr,
1433 				dev_key);
1434 	if (err) {
1435 		LOG_ERR("err %d", err);
1436 		return BTP_STATUS_FAILED;
1437 	}
1438 
1439 	return BTP_STATUS_SUCCESS;
1440 }
1441 
provision_adv(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1442 static uint8_t provision_adv(const void *cmd, uint16_t cmd_len,
1443 			     void *rsp, uint16_t *rsp_len)
1444 {
1445 	const struct btp_mesh_provision_adv_cmd *cp = cmd;
1446 	int err;
1447 
1448 	LOG_DBG("");
1449 
1450 	err = bt_mesh_provision_adv(cp->uuid, sys_le16_to_cpu(cp->net_idx),
1451 				    sys_le16_to_cpu(cp->address),
1452 				    cp->attention_duration);
1453 	if (err) {
1454 		LOG_ERR("err %d", err);
1455 		return BTP_STATUS_FAILED;
1456 	}
1457 
1458 	return BTP_STATUS_SUCCESS;
1459 }
1460 
init(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1461 static uint8_t init(const void *cmd, uint16_t cmd_len,
1462 		    void *rsp, uint16_t *rsp_len)
1463 {
1464 	const struct btp_mesh_init_cmd *cp = cmd;
1465 	int err;
1466 
1467 	if (cp->comp == 0) {
1468 		LOG_WRN("Loading default comp data");
1469 		err = bt_mesh_init(&prov, &comp);
1470 	} else {
1471 		LOG_WRN("Loading alternative comp data");
1472 		err = bt_mesh_init(&prov, &comp_alt);
1473 	}
1474 
1475 	if (err) {
1476 		return BTP_STATUS_FAILED;
1477 	}
1478 
1479 	return BTP_STATUS_SUCCESS;
1480 }
1481 
start(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1482 static uint8_t start(const void *cmd, uint16_t cmd_len,
1483 		     void *rsp, uint16_t *rsp_len)
1484 {
1485 	int err;
1486 
1487 	LOG_DBG("");
1488 
1489 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1490 		printk("Loading stored settings\n");
1491 		settings_load();
1492 	}
1493 
1494 	if (addr) {
1495 		err = bt_mesh_provision(net_key, net_key_idx, flags, iv_index,
1496 					addr, dev_key);
1497 		if (err && err != -EALREADY) {
1498 			return BTP_STATUS_FAILED;
1499 		}
1500 	} else {
1501 		err = bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
1502 		if (err && err != -EALREADY) {
1503 			return BTP_STATUS_FAILED;
1504 		}
1505 	}
1506 
1507 	if (IS_ENABLED(CONFIG_BT_MESH_RPR_SRV)) {
1508 		err = bt_mesh_prov_enable(BT_MESH_PROV_REMOTE);
1509 		if (err) {
1510 			return BTP_STATUS_FAILED;
1511 		}
1512 	}
1513 
1514 	return BTP_STATUS_SUCCESS;
1515 }
1516 
reset(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1517 static uint8_t reset(const void *cmd, uint16_t cmd_len,
1518 		     void *rsp, uint16_t *rsp_len)
1519 {
1520 	LOG_DBG("");
1521 
1522 	bt_mesh_reset();
1523 
1524 	return BTP_STATUS_SUCCESS;
1525 }
1526 
input_number(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1527 static uint8_t input_number(const void *cmd, uint16_t cmd_len,
1528 			    void *rsp, uint16_t *rsp_len)
1529 {
1530 	const struct btp_mesh_input_number_cmd *cp = cmd;
1531 	uint32_t number;
1532 	int err;
1533 
1534 	number = sys_le32_to_cpu(cp->number);
1535 
1536 	LOG_DBG("number 0x%04x", number);
1537 
1538 	err = bt_mesh_input_number(number);
1539 	if (err) {
1540 		return BTP_STATUS_FAILED;
1541 	}
1542 
1543 	return BTP_STATUS_SUCCESS;
1544 }
1545 
input_string(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1546 static uint8_t input_string(const void *cmd, uint16_t cmd_len,
1547 			    void *rsp, uint16_t *rsp_len)
1548 {
1549 	const struct btp_mesh_input_string_cmd *cp = cmd;
1550 	uint8_t status = BTP_STATUS_SUCCESS;
1551 	int err;
1552 
1553 	LOG_DBG("");
1554 
1555 	if (cmd_len < sizeof(*cp) &&
1556 	    cmd_len != (sizeof(*cp) + cp->string_len)) {
1557 		return BTP_STATUS_FAILED;
1558 	}
1559 
1560 	/* for historical reasons this commands must send NULL terminated
1561 	 * string
1562 	 */
1563 	if (cp->string[cp->string_len] != '\0') {
1564 		return BTP_STATUS_FAILED;
1565 	}
1566 
1567 	if (strlen(cp->string) < input_size) {
1568 		LOG_ERR("Too short input (%u chars required)", input_size);
1569 		return BTP_STATUS_FAILED;
1570 	}
1571 
1572 	err = bt_mesh_input_string(cp->string);
1573 	if (err) {
1574 		status = BTP_STATUS_FAILED;
1575 	}
1576 
1577 	return BTP_STATUS_SUCCESS;
1578 }
1579 
ivu_test_mode(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1580 static uint8_t ivu_test_mode(const void *cmd, uint16_t cmd_len,
1581 			     void *rsp, uint16_t *rsp_len)
1582 {
1583 	const struct btp_mesh_ivu_test_mode_cmd *cp = cmd;
1584 
1585 	LOG_DBG("enable 0x%02x", cp->enable);
1586 
1587 	bt_mesh_iv_update_test(cp->enable ? true : false);
1588 
1589 	return BTP_STATUS_SUCCESS;
1590 }
1591 
ivu_toggle_state(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1592 static uint8_t ivu_toggle_state(const void *cmd, uint16_t cmd_len,
1593 				void *rsp, uint16_t *rsp_len)
1594 {
1595 	bool result;
1596 
1597 	LOG_DBG("");
1598 
1599 	result = bt_mesh_iv_update();
1600 	if (!result) {
1601 		LOG_ERR("Failed to toggle the IV Update state");
1602 		return BTP_STATUS_FAILED;
1603 	}
1604 
1605 	return BTP_STATUS_SUCCESS;
1606 }
1607 
1608 #if defined(CONFIG_BT_MESH_LOW_POWER)
lpn(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1609 static uint8_t lpn(const void *cmd, uint16_t cmd_len,
1610 		   void *rsp, uint16_t *rsp_len)
1611 {
1612 	const struct btp_mesh_lpn_set_cmd *cp = cmd;
1613 	int err;
1614 
1615 	LOG_DBG("enable 0x%02x", cp->enable);
1616 
1617 	err = bt_mesh_lpn_set(cp->enable ? true : false);
1618 	if (err) {
1619 		LOG_ERR("Failed to toggle LPN (err %d)", err);
1620 		return BTP_STATUS_FAILED;
1621 	}
1622 
1623 	return BTP_STATUS_SUCCESS;
1624 }
1625 
lpn_poll(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1626 static uint8_t lpn_poll(const void *cmd, uint16_t cmd_len,
1627 			void *rsp, uint16_t *rsp_len)
1628 {
1629 	int err;
1630 
1631 	LOG_DBG("");
1632 
1633 	err = bt_mesh_lpn_poll();
1634 	if (err) {
1635 		LOG_ERR("Failed to send poll msg (err %d)", err);
1636 	}
1637 
1638 	return BTP_STATUS_SUCCESS;
1639 }
1640 #endif /* CONFIG_BT_MESH_LOW_POWER */
1641 
net_send(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1642 static uint8_t net_send(const void *cmd, uint16_t cmd_len,
1643 			void *rsp, uint16_t *rsp_len)
1644 {
1645 	const struct btp_mesh_net_send_cmd *cp = cmd;
1646 	NET_BUF_SIMPLE_DEFINE(msg, UINT8_MAX);
1647 	int err;
1648 
1649 	if (cmd_len < sizeof(*cp) &&
1650 	    cmd_len != (sizeof(*cp) + cp->payload_len)) {
1651 		return BTP_STATUS_FAILED;
1652 	}
1653 
1654 	struct bt_mesh_msg_ctx ctx = {
1655 		.net_idx = net.net_idx,
1656 		.app_idx = vnd_app_key_idx,
1657 		.addr = sys_le16_to_cpu(cp->dst),
1658 		.send_ttl = cp->ttl,
1659 	};
1660 
1661 	if (BT_MESH_ADDR_IS_VIRTUAL(ctx.addr)) {
1662 		ctx.uuid = bt_mesh_va_uuid_get(ctx.addr, NULL, NULL);
1663 	}
1664 
1665 	LOG_DBG("ttl 0x%02x dst 0x%04x payload_len %d", ctx.send_ttl,
1666 		ctx.addr, cp->payload_len);
1667 
1668 	if (!bt_mesh_app_key_exists(vnd_app_key_idx)) {
1669 		(void)bt_mesh_app_key_add(vnd_app_key_idx, net.net_idx,
1670 					  vnd_app_key);
1671 		vnd_models[0].keys[0] = vnd_app_key_idx;
1672 	}
1673 
1674 	net_buf_simple_add_mem(&msg, cp->payload, cp->payload_len);
1675 
1676 	err = bt_mesh_model_send(&vnd_models[0], &ctx, &msg, NULL, NULL);
1677 	if (err) {
1678 		LOG_ERR("Failed to send (err %d)", err);
1679 		return BTP_STATUS_FAILED;
1680 	}
1681 
1682 	return BTP_STATUS_SUCCESS;
1683 }
1684 
va_add(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1685 static uint8_t va_add(const void *cmd, uint16_t cmd_len,
1686 		      void *rsp, uint16_t *rsp_len)
1687 {
1688 	const struct btp_mesh_va_add_cmd *cp = cmd;
1689 	struct btp_mesh_va_add_rp *rp = rsp;
1690 	const struct bt_mesh_va *va;
1691 	int err;
1692 
1693 	err = bt_mesh_va_add(cp->label_uuid, &va);
1694 	if (err) {
1695 		LOG_ERR("Failed to add Label UUID (err %d)", err);
1696 		return BTP_STATUS_FAILED;
1697 	}
1698 
1699 	rp->addr = sys_cpu_to_le16(va->addr);
1700 	*rsp_len = sizeof(*rp);
1701 
1702 	return BTP_STATUS_SUCCESS;
1703 }
1704 
va_del(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1705 static uint8_t va_del(const void *cmd, uint16_t cmd_len,
1706 		      void *rsp, uint16_t *rsp_len)
1707 {
1708 	const struct btp_mesh_va_del_cmd *cp = cmd;
1709 	const struct bt_mesh_va *va;
1710 	int err;
1711 
1712 	va = bt_mesh_va_find(cp->label_uuid);
1713 	if (!va) {
1714 		LOG_ERR("Failed to find Label UUID");
1715 		return BTP_STATUS_FAILED;
1716 	}
1717 
1718 	err = bt_mesh_va_del(va->uuid);
1719 	if (err) {
1720 		LOG_ERR("Failed to delete Label UUID (err %d)", err);
1721 		return BTP_STATUS_FAILED;
1722 	}
1723 
1724 	return BTP_STATUS_SUCCESS;
1725 }
1726 
health_generate_faults(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1727 static uint8_t health_generate_faults(const void *cmd, uint16_t cmd_len,
1728 				      void *rsp, uint16_t *rsp_len)
1729 {
1730 	struct btp_mesh_health_generate_faults_rp *rp = rsp;
1731 	uint8_t some_faults[] = { 0x01, 0x02, 0x03, 0xff, 0x06 };
1732 	uint8_t cur_faults_count;
1733 	uint8_t reg_faults_count;
1734 
1735 	cur_faults_count = MIN(sizeof(cur_faults), sizeof(some_faults));
1736 	memcpy(cur_faults, some_faults, cur_faults_count);
1737 	memcpy(rp->faults, cur_faults, cur_faults_count);
1738 	rp->cur_faults_count = cur_faults_count;
1739 
1740 	reg_faults_count = MIN(sizeof(reg_faults), sizeof(some_faults));
1741 	memcpy(reg_faults, some_faults, reg_faults_count);
1742 	memcpy(rp->faults + cur_faults_count, reg_faults, reg_faults_count);
1743 	rp->reg_faults_count = reg_faults_count;
1744 
1745 	bt_mesh_health_srv_fault_update(&elements[0]);
1746 
1747 	*rsp_len = sizeof(*rp) + cur_faults_count + reg_faults_count;
1748 
1749 	return BTP_STATUS_SUCCESS;
1750 }
1751 
health_clear_faults(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1752 static uint8_t health_clear_faults(const void *cmd, uint16_t cmd_len,
1753 				   void *rsp, uint16_t *rsp_len)
1754 {
1755 	LOG_DBG("");
1756 
1757 	(void)memset(cur_faults, 0, sizeof(cur_faults));
1758 	(void)memset(reg_faults, 0, sizeof(reg_faults));
1759 
1760 	bt_mesh_health_srv_fault_update(&elements[0]);
1761 
1762 	return BTP_STATUS_SUCCESS;
1763 }
1764 
model_send(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1765 static uint8_t model_send(const void *cmd, uint16_t cmd_len,
1766 			  void *rsp, uint16_t *rsp_len)
1767 {
1768 	const struct btp_mesh_model_send_cmd *cp = cmd;
1769 	NET_BUF_SIMPLE_DEFINE(msg, UINT8_MAX);
1770 	const struct bt_mesh_model *model = NULL;
1771 	uint16_t src;
1772 	int err;
1773 
1774 	if (cmd_len < sizeof(*cp) &&
1775 	    cmd_len != (sizeof(*cp) + cp->payload_len)) {
1776 		return BTP_STATUS_FAILED;
1777 	}
1778 
1779 	struct bt_mesh_msg_ctx ctx = {
1780 		.net_idx = net.net_idx,
1781 		.app_idx = BT_MESH_KEY_DEV,
1782 		.addr = sys_le16_to_cpu(cp->dst),
1783 		.send_ttl = cp->ttl,
1784 	};
1785 
1786 	if (BT_MESH_ADDR_IS_VIRTUAL(ctx.addr)) {
1787 		ctx.uuid = bt_mesh_va_uuid_get(ctx.addr, NULL, NULL);
1788 	}
1789 
1790 	src = sys_le16_to_cpu(cp->src);
1791 
1792 	/* Lookup source address */
1793 	for (int i = 0; i < ARRAY_SIZE(model_bound); i++) {
1794 		if (bt_mesh_model_elem(model_bound[i].model)->rt->addr == src) {
1795 			model = model_bound[i].model;
1796 			ctx.app_idx = model_bound[i].appkey_idx;
1797 
1798 			break;
1799 		}
1800 	}
1801 
1802 	if (!model) {
1803 		LOG_ERR("Model not found");
1804 		return BTP_STATUS_FAILED;
1805 	}
1806 
1807 	LOG_DBG("src 0x%04x dst 0x%04x model %p payload_len %d", src,
1808 		ctx.addr, model, cp->payload_len);
1809 
1810 	net_buf_simple_add_mem(&msg, cp->payload, cp->payload_len);
1811 
1812 	err = bt_mesh_model_send(model, &ctx, &msg, NULL, NULL);
1813 	if (err) {
1814 		LOG_ERR("Failed to send (err %d)", err);
1815 		return BTP_STATUS_FAILED;
1816 	}
1817 
1818 	return BTP_STATUS_SUCCESS;
1819 }
1820 
1821 #if defined(CONFIG_BT_TESTING)
1822 #if defined(CONFIG_BT_MESH_LOW_POWER)
lpn_subscribe(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1823 static uint8_t lpn_subscribe(const void *cmd, uint16_t cmd_len,
1824 			     void *rsp, uint16_t *rsp_len)
1825 {
1826 	const struct btp_mesh_lpn_subscribe_cmd *cp = cmd;
1827 	uint16_t address = sys_le16_to_cpu(cp->address);
1828 	int err;
1829 
1830 	LOG_DBG("address 0x%04x", address);
1831 
1832 	err = bt_test_mesh_lpn_group_add(address);
1833 	if (err) {
1834 		LOG_ERR("Failed to subscribe (err %d)", err);
1835 		return BTP_STATUS_FAILED;
1836 	}
1837 
1838 	return BTP_STATUS_SUCCESS;
1839 }
1840 
lpn_unsubscribe(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1841 static uint8_t lpn_unsubscribe(const void *cmd, uint16_t cmd_len,
1842 			       void *rsp, uint16_t *rsp_len)
1843 {
1844 	const struct btp_mesh_lpn_unsubscribe_cmd *cp = cmd;
1845 	uint16_t address = sys_le16_to_cpu(cp->address);
1846 	int err;
1847 
1848 	LOG_DBG("address 0x%04x", address);
1849 
1850 	err = bt_test_mesh_lpn_group_remove(&address, 1);
1851 	if (err) {
1852 		LOG_ERR("Failed to unsubscribe (err %d)", err);
1853 		return BTP_STATUS_FAILED;
1854 	}
1855 
1856 	return BTP_STATUS_SUCCESS;
1857 }
1858 #endif /* CONFIG_BT_MESH_LOW_POWER */
1859 
rpl_clear(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1860 static uint8_t rpl_clear(const void *cmd, uint16_t cmd_len,
1861 			 void *rsp, uint16_t *rsp_len)
1862 {
1863 	int err;
1864 
1865 	LOG_DBG("");
1866 
1867 	err = bt_test_mesh_rpl_clear();
1868 	if (err) {
1869 		LOG_ERR("Failed to clear RPL (err %d)", err);
1870 		return BTP_STATUS_FAILED;
1871 	}
1872 
1873 	return BTP_STATUS_SUCCESS;
1874 }
1875 #endif /* CONFIG_BT_TESTING */
1876 
proxy_identity_enable(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1877 static uint8_t proxy_identity_enable(const void *cmd, uint16_t cmd_len,
1878 				     void *rsp, uint16_t *rsp_len)
1879 {
1880 	int err;
1881 
1882 	LOG_DBG("");
1883 
1884 	err = bt_mesh_proxy_identity_enable();
1885 	if (err) {
1886 		LOG_ERR("Failed to enable proxy identity (err %d)", err);
1887 		return BTP_STATUS_FAILED;
1888 	}
1889 
1890 	return BTP_STATUS_SUCCESS;
1891 }
1892 
1893 #if defined(CONFIG_BT_MESH_PROXY_CLIENT)
proxy_connect(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1894 static uint8_t proxy_connect(const void *cmd, uint16_t cmd_len,
1895 			     void *rsp, uint16_t *rsp_len)
1896 {
1897 	const struct btp_proxy_connect_cmd *cp = cmd;
1898 	int err;
1899 
1900 	err = bt_mesh_proxy_connect(cp->net_idx);
1901 	if (err) {
1902 		LOG_ERR("Failed to connect to GATT Proxy (err %d)", err);
1903 		return BTP_STATUS_FAILED;
1904 	}
1905 
1906 	return BTP_STATUS_SUCCESS;
1907 }
1908 #endif
1909 
1910 #if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
sar_transmitter_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1911 static uint8_t sar_transmitter_get(const void *cmd, uint16_t cmd_len,
1912 				   void *rsp, uint16_t *rsp_len)
1913 {
1914 	const struct btp_mesh_sar_transmitter_get_cmd *cp = cmd;
1915 	struct bt_mesh_sar_tx tx_rsp;
1916 	int err;
1917 
1918 	LOG_DBG("");
1919 
1920 	bt_mesh_sar_cfg_cli_timeout_set(5000);
1921 
1922 	err = bt_mesh_sar_cfg_cli_transmitter_get(
1923 		net_key_idx, sys_le16_to_cpu(cp->dst), &tx_rsp);
1924 	if (err) {
1925 		LOG_ERR("err=%d", err);
1926 		return BTP_STATUS_FAILED;
1927 	}
1928 
1929 	return BTP_STATUS_SUCCESS;
1930 }
1931 
sar_transmitter_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1932 static uint8_t sar_transmitter_set(const void *cmd, uint16_t cmd_len,
1933 				   void *rsp, uint16_t *rsp_len)
1934 {
1935 	const struct btp_mesh_sar_transmitter_set_cmd *cp = cmd;
1936 	struct bt_mesh_sar_tx set, tx_rsp;
1937 	int err;
1938 
1939 	LOG_DBG("");
1940 
1941 	bt_mesh_sar_cfg_cli_timeout_set(5000);
1942 
1943 	set.seg_int_step = cp->tx.seg_int_step;
1944 	set.unicast_retrans_count = cp->tx.unicast_retrans_count;
1945 	set.unicast_retrans_int_inc = cp->tx.unicast_retrans_int_inc;
1946 	set.unicast_retrans_int_step = cp->tx.unicast_retrans_int_step;
1947 	set.unicast_retrans_without_prog_count =
1948 		cp->tx.unicast_retrans_without_prog_count;
1949 	set.multicast_retrans_count = cp->tx.multicast_retrans_count;
1950 	set.multicast_retrans_int = cp->tx.multicast_retrans_int;
1951 
1952 	err = bt_mesh_sar_cfg_cli_transmitter_set(net_key_idx,
1953 						  sys_le16_to_cpu(cp->dst),
1954 						  &set, &tx_rsp);
1955 	if (err) {
1956 		LOG_ERR("err=%d", err);
1957 		return BTP_STATUS_FAILED;
1958 	}
1959 
1960 	return BTP_STATUS_SUCCESS;
1961 }
1962 
sar_receiver_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1963 static uint8_t sar_receiver_get(const void *cmd, uint16_t cmd_len,
1964 				void *rsp, uint16_t *rsp_len)
1965 {
1966 	const struct btp_mesh_sar_receiver_get_cmd *cp = cmd;
1967 	struct bt_mesh_sar_rx rx_rsp;
1968 	int err;
1969 
1970 	LOG_DBG("");
1971 
1972 	err = bt_mesh_sar_cfg_cli_receiver_get(net_key_idx,
1973 					       sys_le16_to_cpu(cp->dst), &rx_rsp);
1974 	if (err) {
1975 		LOG_ERR("err=%d", err);
1976 		return BTP_STATUS_FAILED;
1977 	}
1978 
1979 	return BTP_STATUS_SUCCESS;
1980 }
1981 
sar_receiver_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)1982 static uint8_t sar_receiver_set(const void *cmd, uint16_t cmd_len,
1983 				void *rsp, uint16_t *rsp_len)
1984 {
1985 	const struct btp_mesh_sar_receiver_set_cmd *cp = cmd;
1986 	struct bt_mesh_sar_rx set, rx_rsp;
1987 	int err;
1988 
1989 	LOG_DBG("");
1990 
1991 	set.ack_delay_inc = cp->rx.ack_delay_inc;
1992 	set.ack_retrans_count = cp->rx.ack_retrans_count;
1993 	set.discard_timeout = cp->rx.discard_timeout;
1994 	set.seg_thresh = cp->rx.seg_thresh;
1995 	set.rx_seg_int_step = cp->rx.rx_seg_int_step;
1996 
1997 	err = bt_mesh_sar_cfg_cli_receiver_set(net_key_idx,
1998 					       sys_le16_to_cpu(cp->dst), &set,
1999 					       &rx_rsp);
2000 	if (err) {
2001 		LOG_ERR("err=%d", err);
2002 		return BTP_STATUS_FAILED;
2003 	}
2004 
2005 	return BTP_STATUS_SUCCESS;
2006 }
2007 #endif
2008 
2009 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
large_comp_data_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2010 static uint8_t large_comp_data_get(const void *cmd, uint16_t cmd_len,
2011 				   void *rsp, uint16_t *rsp_len)
2012 {
2013 	const struct btp_mesh_large_comp_data_get_cmd *cp = cmd;
2014 	struct btp_mesh_large_comp_data_get_rp *rp = rsp;
2015 	int err;
2016 
2017 	NET_BUF_SIMPLE_DEFINE(data, 500);
2018 	net_buf_simple_init(&data, 0);
2019 
2020 	struct bt_mesh_large_comp_data_rsp comp = {
2021 		.data = &data,
2022 	};
2023 
2024 	err = bt_mesh_large_comp_data_get(sys_le16_to_cpu(cp->net_idx),
2025 				    sys_le16_to_cpu(cp->addr), cp->page,
2026 				    sys_le16_to_cpu(cp->offset), &comp);
2027 	if (err) {
2028 		LOG_ERR("Large Composition Data Get failed (err %d)", err);
2029 
2030 		return BTP_STATUS_FAILED;
2031 	}
2032 
2033 	memcpy(rp->data, comp.data->data, comp.data->len);
2034 	*rsp_len = comp.data->len;
2035 
2036 	return BTP_STATUS_SUCCESS;
2037 }
2038 
models_metadata_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2039 static uint8_t models_metadata_get(const void *cmd, uint16_t cmd_len,
2040 				   void *rsp, uint16_t *rsp_len)
2041 {
2042 	const struct btp_mesh_models_metadata_get_cmd *cp = cmd;
2043 	struct btp_mesh_models_metadata_get_rp *rp = rsp;
2044 	int err;
2045 
2046 	NET_BUF_SIMPLE_DEFINE(data, 500);
2047 	net_buf_simple_init(&data, 0);
2048 
2049 	struct bt_mesh_large_comp_data_rsp metadata = {
2050 		.data = &data,
2051 	};
2052 
2053 	err = bt_mesh_models_metadata_get(sys_le16_to_cpu(cp->net_idx),
2054 					  sys_le16_to_cpu(cp->addr), cp->page,
2055 					  sys_le16_to_cpu(cp->offset), &metadata);
2056 
2057 	if (err) {
2058 		LOG_ERR("Models Metadata Get failed (err %d)", err);
2059 		return BTP_STATUS_FAILED;
2060 	}
2061 
2062 	memcpy(rp->data, metadata.data->data, metadata.data->len);
2063 	*rsp_len = metadata.data->len;
2064 
2065 	return BTP_STATUS_SUCCESS;
2066 }
2067 #endif
2068 
composition_data_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2069 static uint8_t composition_data_get(const void *cmd, uint16_t cmd_len,
2070 				    void *rsp, uint16_t *rsp_len)
2071 {
2072 	const struct btp_mesh_comp_data_get_cmd *cp = cmd;
2073 	struct btp_mesh_comp_data_get_rp *rp = rsp;
2074 	uint8_t page;
2075 	struct net_buf_simple *comp = NET_BUF_SIMPLE(128);
2076 	int err;
2077 
2078 	LOG_DBG("");
2079 
2080 	bt_mesh_cfg_cli_timeout_set(10 * MSEC_PER_SEC);
2081 
2082 	net_buf_simple_init(comp, 0);
2083 
2084 	err = bt_mesh_cfg_cli_comp_data_get(sys_le16_to_cpu(cp->net_idx),
2085 					    sys_le16_to_cpu(cp->address),
2086 					    cp->page, &page, comp);
2087 	if (err) {
2088 		LOG_ERR("err %d", err);
2089 		return BTP_STATUS_FAILED;
2090 	}
2091 
2092 	rp->data[0] = page;
2093 	memcpy(rp->data + 1, comp->data, comp->len);
2094 	*rsp_len = comp->len + 1;
2095 
2096 	return BTP_STATUS_SUCCESS;
2097 }
2098 
change_prepare(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2099 static uint8_t change_prepare(const void *cmd, uint16_t cmd_len,
2100 			      void *rsp, uint16_t *rsp_len)
2101 {
2102 	int err;
2103 
2104 	LOG_DBG("");
2105 
2106 	err = bt_mesh_comp_change_prepare();
2107 	if (err < 0) {
2108 		return BTP_STATUS_FAILED;
2109 	}
2110 
2111 #if CONFIG_BT_MESH_LARGE_COMP_DATA_SRV
2112 	err = bt_mesh_models_metadata_change_prepare();
2113 	if (err < 0) {
2114 		return BTP_STATUS_FAILED;
2115 	}
2116 #endif
2117 
2118 	return BTP_STATUS_SUCCESS;
2119 }
2120 
config_krp_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2121 static uint8_t config_krp_get(const void *cmd, uint16_t cmd_len,
2122 			      void *rsp, uint16_t *rsp_len)
2123 {
2124 	const struct btp_mesh_cfg_krp_get_cmd *cp = cmd;
2125 	struct btp_mesh_cfg_krp_get_rp *rp = rsp;
2126 	uint8_t status;
2127 	uint8_t phase;
2128 	int err;
2129 
2130 	LOG_DBG("");
2131 
2132 	err = bt_mesh_cfg_cli_krp_get(sys_le16_to_cpu(cp->net_idx),
2133 				      sys_le16_to_cpu(cp->address),
2134 				      sys_le16_to_cpu(cp->key_net_idx),
2135 				      &status, &phase);
2136 
2137 	if (err) {
2138 		LOG_ERR("err %d", err);
2139 		return BTP_STATUS_FAILED;
2140 	}
2141 
2142 	rp->status = status;
2143 	rp->phase = phase;
2144 
2145 	*rsp_len = sizeof(*rp);
2146 
2147 	return BTP_STATUS_SUCCESS;
2148 }
2149 
config_krp_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2150 static uint8_t config_krp_set(const void *cmd, uint16_t cmd_len,
2151 			      void *rsp, uint16_t *rsp_len)
2152 {
2153 	const struct btp_mesh_cfg_krp_set_cmd *cp = cmd;
2154 	struct btp_mesh_cfg_krp_set_rp *rp = rsp;
2155 	uint8_t status;
2156 	uint8_t phase;
2157 	int err;
2158 
2159 	LOG_DBG("");
2160 
2161 	err = bt_mesh_cfg_cli_krp_set(sys_le16_to_cpu(cp->net_idx),
2162 				      sys_le16_to_cpu(cp->address),
2163 				      sys_le16_to_cpu(cp->key_net_idx),
2164 				      cp->transition, &status, &phase);
2165 	if (err) {
2166 		LOG_ERR("err %d", err);
2167 		return BTP_STATUS_FAILED;
2168 	}
2169 
2170 	rp->status = status;
2171 	rp->phase = phase;
2172 
2173 	*rsp_len = sizeof(*rp);
2174 
2175 	return BTP_STATUS_SUCCESS;
2176 }
2177 
config_beacon_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2178 static uint8_t config_beacon_get(const void *cmd, uint16_t cmd_len,
2179 				 void *rsp, uint16_t *rsp_len)
2180 {
2181 	const struct btp_mesh_cfg_beacon_get_cmd *cp = cmd;
2182 	struct btp_mesh_cfg_beacon_get_rp *rp = rsp;
2183 	uint8_t status;
2184 	int err;
2185 
2186 	LOG_DBG("");
2187 
2188 	err = bt_mesh_cfg_cli_beacon_get(sys_le16_to_cpu(cp->net_idx),
2189 					 sys_le16_to_cpu(cp->address), &status);
2190 	if (err) {
2191 		LOG_ERR("err %d", err);
2192 		return BTP_STATUS_FAILED;
2193 	}
2194 
2195 	rp->status = status;
2196 	*rsp_len = sizeof(*rp);
2197 
2198 	return BTP_STATUS_SUCCESS;
2199 }
2200 
config_beacon_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2201 static uint8_t config_beacon_set(const void *cmd, uint16_t cmd_len,
2202 				 void *rsp, uint16_t *rsp_len)
2203 {
2204 	const struct btp_mesh_cfg_beacon_set_cmd *cp = cmd;
2205 	struct btp_mesh_cfg_beacon_set_rp *rp = rsp;
2206 	uint8_t status;
2207 	int err;
2208 
2209 	LOG_DBG("");
2210 
2211 	err = bt_mesh_cfg_cli_beacon_set(sys_le16_to_cpu(cp->net_idx),
2212 					 sys_le16_to_cpu(cp->address), cp->val,
2213 					 &status);
2214 	if (err) {
2215 		LOG_ERR("err %d", err);
2216 		return BTP_STATUS_FAILED;
2217 	}
2218 
2219 	rp->status = status;
2220 	*rsp_len = sizeof(*rp);
2221 
2222 	return BTP_STATUS_SUCCESS;
2223 }
2224 
config_default_ttl_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2225 static uint8_t config_default_ttl_get(const void *cmd, uint16_t cmd_len,
2226 				      void *rsp, uint16_t *rsp_len)
2227 {
2228 	const struct btp_mesh_cfg_default_ttl_get_cmd *cp = cmd;
2229 	struct btp_mesh_cfg_default_ttl_get_rp *rp = rsp;
2230 	uint8_t status;
2231 	int err;
2232 
2233 	LOG_DBG("");
2234 
2235 	err = bt_mesh_cfg_cli_ttl_get(sys_le16_to_cpu(cp->net_idx),
2236 				      sys_le16_to_cpu(cp->address), &status);
2237 
2238 	if (err) {
2239 		LOG_ERR("err %d", err);
2240 		return BTP_STATUS_FAILED;
2241 	}
2242 
2243 	rp->status = status;
2244 	*rsp_len = sizeof(*rp);
2245 
2246 	return BTP_STATUS_SUCCESS;
2247 }
2248 
config_default_ttl_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2249 static uint8_t config_default_ttl_set(const void *cmd, uint16_t cmd_len,
2250 				      void *rsp, uint16_t *rsp_len)
2251 {
2252 	const struct btp_mesh_cfg_default_ttl_set_cmd *cp = cmd;
2253 	struct btp_mesh_cfg_default_ttl_set_rp *rp = rsp;
2254 	uint8_t status;
2255 	int err;
2256 
2257 	LOG_DBG("");
2258 
2259 	err = bt_mesh_cfg_cli_ttl_set(sys_le16_to_cpu(cp->net_idx),
2260 				      sys_le16_to_cpu(cp->address), cp->val,
2261 				      &status);
2262 
2263 	if (err) {
2264 		LOG_ERR("err %d", err);
2265 		return BTP_STATUS_FAILED;
2266 	}
2267 
2268 	rp->status = status;
2269 	*rsp_len = sizeof(*rp);
2270 
2271 	return BTP_STATUS_SUCCESS;
2272 }
2273 
config_gatt_proxy_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2274 static uint8_t config_gatt_proxy_get(const void *cmd, uint16_t cmd_len,
2275 				     void *rsp, uint16_t *rsp_len)
2276 {
2277 	const struct btp_mesh_cfg_gatt_proxy_get_cmd *cp = cmd;
2278 	struct btp_mesh_cfg_gatt_proxy_get_rp *rp = rsp;
2279 	uint8_t status;
2280 	int err;
2281 
2282 	LOG_DBG("");
2283 
2284 	err = bt_mesh_cfg_cli_gatt_proxy_get(sys_le16_to_cpu(cp->net_idx),
2285 					     sys_le16_to_cpu(cp->address),
2286 					     &status);
2287 	if (err) {
2288 		LOG_ERR("err %d", err);
2289 		return BTP_STATUS_FAILED;
2290 	}
2291 
2292 	rp->status = status;
2293 	*rsp_len = sizeof(*rp);
2294 
2295 	return BTP_STATUS_SUCCESS;
2296 }
2297 
config_gatt_proxy_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2298 static uint8_t config_gatt_proxy_set(const void *cmd, uint16_t cmd_len,
2299 				     void *rsp, uint16_t *rsp_len)
2300 {
2301 	const struct btp_mesh_cfg_gatt_proxy_set_cmd *cp = cmd;
2302 	struct btp_mesh_cfg_gatt_proxy_set_rp *rp = rsp;
2303 	uint8_t status;
2304 	int err;
2305 
2306 	LOG_DBG("");
2307 
2308 	err = bt_mesh_cfg_cli_gatt_proxy_set(sys_le16_to_cpu(cp->net_idx),
2309 					     sys_le16_to_cpu(cp->address),
2310 					     cp->val, &status);
2311 	if (err) {
2312 		LOG_ERR("err %d", err);
2313 		return BTP_STATUS_FAILED;
2314 	}
2315 
2316 	rp->status = status;
2317 	*rsp_len = sizeof(*rp);
2318 
2319 	return BTP_STATUS_SUCCESS;
2320 }
2321 
config_friend_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2322 static uint8_t config_friend_get(const void *cmd, uint16_t cmd_len,
2323 				 void *rsp, uint16_t *rsp_len)
2324 {
2325 	const struct btp_mesh_cfg_friend_get_cmd *cp = cmd;
2326 	struct btp_mesh_cfg_friend_get_rp *rp = rsp;
2327 	uint8_t status;
2328 	int err;
2329 
2330 	LOG_DBG("");
2331 
2332 	err = bt_mesh_cfg_cli_friend_get(sys_le16_to_cpu(cp->net_idx),
2333 					 sys_le16_to_cpu(cp->address),
2334 					 &status);
2335 
2336 	if (err) {
2337 		LOG_ERR("err %d", err);
2338 		return BTP_STATUS_FAILED;
2339 	}
2340 
2341 	rp->status = status;
2342 	*rsp_len = sizeof(*rp);
2343 
2344 	return BTP_STATUS_SUCCESS;
2345 }
2346 
config_friend_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2347 static uint8_t config_friend_set(const void *cmd, uint16_t cmd_len,
2348 				 void *rsp, uint16_t *rsp_len)
2349 {
2350 	const struct btp_mesh_cfg_friend_set_cmd *cp = cmd;
2351 	struct btp_mesh_cfg_friend_set_rp *rp = rsp;
2352 	uint8_t status;
2353 	int err;
2354 
2355 	LOG_DBG("");
2356 
2357 	err = bt_mesh_cfg_cli_friend_set(sys_le16_to_cpu(cp->net_idx),
2358 					 sys_le16_to_cpu(cp->address),
2359 					 cp->val, &status);
2360 
2361 	if (err) {
2362 		LOG_ERR("err %d", err);
2363 		return BTP_STATUS_FAILED;
2364 	}
2365 
2366 	rp->status = status;
2367 	*rsp_len = sizeof(*rp);
2368 
2369 	return BTP_STATUS_SUCCESS;
2370 }
2371 
config_relay_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2372 static uint8_t config_relay_get(const void *cmd, uint16_t cmd_len,
2373 				void *rsp, uint16_t *rsp_len)
2374 {
2375 	const struct btp_mesh_cfg_relay_get_cmd *cp = cmd;
2376 	struct btp_mesh_cfg_relay_get_rp *rp = rsp;
2377 	uint8_t status;
2378 	uint8_t transmit;
2379 	int err;
2380 
2381 	LOG_DBG("");
2382 
2383 	err = bt_mesh_cfg_cli_relay_get(sys_le16_to_cpu(cp->net_idx),
2384 					sys_le16_to_cpu(cp->address), &status,
2385 					&transmit);
2386 
2387 	if (err) {
2388 		LOG_ERR("err %d", err);
2389 		return BTP_STATUS_FAILED;
2390 	}
2391 
2392 	rp->status = status;
2393 	*rsp_len = sizeof(*rp);
2394 
2395 	return BTP_STATUS_SUCCESS;
2396 }
2397 
config_relay_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2398 static uint8_t config_relay_set(const void *cmd, uint16_t cmd_len,
2399 				void *rsp, uint16_t *rsp_len)
2400 {
2401 	const struct btp_mesh_cfg_relay_set_cmd *cp = cmd;
2402 	struct btp_mesh_cfg_relay_set_rp *rp = rsp;
2403 	uint8_t status;
2404 	uint8_t transmit;
2405 	int err;
2406 
2407 	LOG_DBG("");
2408 
2409 	err = bt_mesh_cfg_cli_relay_set(sys_le16_to_cpu(cp->net_idx),
2410 					sys_le16_to_cpu(cp->address),
2411 					cp->new_relay, cp->new_transmit,
2412 					&status, &transmit);
2413 
2414 	if (err) {
2415 		LOG_ERR("err %d", err);
2416 		return BTP_STATUS_FAILED;
2417 	}
2418 
2419 	rp->status = status;
2420 	*rsp_len = sizeof(*rp);
2421 
2422 	return BTP_STATUS_SUCCESS;
2423 }
2424 
config_mod_pub_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2425 static uint8_t config_mod_pub_get(const void *cmd, uint16_t cmd_len,
2426 				  void *rsp, uint16_t *rsp_len)
2427 {
2428 	const struct btp_mesh_cfg_model_pub_get_cmd *cp = cmd;
2429 	struct btp_mesh_cfg_model_pub_get_rp *rp = rsp;
2430 	struct bt_mesh_cfg_cli_mod_pub pub;
2431 	uint8_t status;
2432 	int err;
2433 
2434 	LOG_DBG("");
2435 
2436 	err = bt_mesh_cfg_cli_mod_pub_get(sys_le16_to_cpu(cp->net_idx),
2437 					  sys_le16_to_cpu(cp->address),
2438 					  sys_le16_to_cpu(cp->elem_address),
2439 					  sys_le16_to_cpu(cp->model_id),
2440 					  &pub, &status);
2441 
2442 	if (err) {
2443 		LOG_ERR("err %d", err);
2444 		return BTP_STATUS_FAILED;
2445 	}
2446 
2447 	rp->status = status;
2448 	*rsp_len = sizeof(*rp);
2449 
2450 	return BTP_STATUS_SUCCESS;
2451 }
2452 
config_mod_pub_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2453 static uint8_t config_mod_pub_set(const void *cmd, uint16_t cmd_len,
2454 				  void *rsp, uint16_t *rsp_len)
2455 {
2456 	const struct btp_mesh_cfg_model_pub_set_cmd *cp = cmd;
2457 	struct btp_mesh_cfg_model_pub_set_rp *rp = rsp;
2458 	struct bt_mesh_cfg_cli_mod_pub pub;
2459 	uint8_t status;
2460 	int err;
2461 
2462 	LOG_DBG("");
2463 
2464 	pub.addr = sys_le16_to_cpu(cp->pub_addr);
2465 	pub.uuid = NULL;
2466 	pub.app_idx = sys_le16_to_cpu(cp->app_idx);
2467 	pub.cred_flag = cp->cred_flag;
2468 	pub.ttl = cp->ttl;
2469 	pub.period = cp->period;
2470 	pub.transmit = cp->transmit;
2471 
2472 	err = bt_mesh_cfg_cli_mod_pub_set(sys_le16_to_cpu(cp->net_idx),
2473 					  sys_le16_to_cpu(cp->address),
2474 					  sys_le16_to_cpu(cp->elem_address),
2475 					  sys_le16_to_cpu(cp->model_id),
2476 					  &pub, &status);
2477 
2478 	if (err) {
2479 		LOG_ERR("err %d", err);
2480 		return BTP_STATUS_FAILED;
2481 	}
2482 
2483 	rp->status = status;
2484 	*rsp_len = sizeof(*rp);
2485 
2486 	return BTP_STATUS_SUCCESS;
2487 }
2488 
config_mod_pub_va_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2489 static uint8_t config_mod_pub_va_set(const void *cmd, uint16_t cmd_len,
2490 				     void *rsp, uint16_t *rsp_len)
2491 {
2492 	const struct btp_mesh_cfg_model_pub_va_set_cmd *cp = cmd;
2493 	struct btp_mesh_cfg_model_pub_va_set_rp *rp = rsp;
2494 	uint8_t status;
2495 	struct bt_mesh_cfg_cli_mod_pub pub;
2496 	int err;
2497 
2498 	LOG_DBG("");
2499 
2500 	pub.uuid = cp->uuid;
2501 	pub.app_idx = sys_le16_to_cpu(cp->app_idx);
2502 	pub.cred_flag = cp->cred_flag;
2503 	pub.ttl = cp->ttl;
2504 	pub.period = cp->period;
2505 	pub.transmit = cp->transmit;
2506 
2507 	err = bt_mesh_cfg_cli_mod_pub_set(sys_le16_to_cpu(cp->net_idx),
2508 					  sys_le16_to_cpu(cp->address),
2509 					  sys_le16_to_cpu(cp->elem_address),
2510 					  sys_le16_to_cpu(cp->model_id),
2511 					  &pub, &status);
2512 
2513 	if (err) {
2514 		LOG_ERR("err %d", err);
2515 		return BTP_STATUS_FAILED;
2516 	}
2517 
2518 	rp->status = status;
2519 	*rsp_len = sizeof(*rp);
2520 
2521 	return BTP_STATUS_SUCCESS;
2522 }
2523 
config_mod_sub_add(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2524 static uint8_t config_mod_sub_add(const void *cmd, uint16_t cmd_len,
2525 				  void *rsp, uint16_t *rsp_len)
2526 {
2527 	const struct btp_mesh_cfg_model_sub_add_cmd *cp = cmd;
2528 	struct btp_mesh_cfg_model_sub_add_rp *rp = rsp;
2529 	uint8_t status;
2530 	int err;
2531 
2532 	LOG_DBG("");
2533 
2534 	err = bt_mesh_cfg_cli_mod_sub_add(sys_le16_to_cpu(cp->net_idx),
2535 					  sys_le16_to_cpu(cp->address),
2536 					  sys_le16_to_cpu(cp->elem_address),
2537 					  sys_le16_to_cpu(cp->sub_addr),
2538 					  sys_le16_to_cpu(cp->model_id),
2539 					  &status);
2540 
2541 	if (err) {
2542 		LOG_ERR("err %d", err);
2543 		return BTP_STATUS_FAILED;
2544 	}
2545 
2546 	rp->status = status;
2547 	*rsp_len = sizeof(*rp);
2548 
2549 	return BTP_STATUS_SUCCESS;
2550 }
2551 
config_mod_sub_ovw(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2552 static uint8_t config_mod_sub_ovw(const void *cmd, uint16_t cmd_len,
2553 				  void *rsp, uint16_t *rsp_len)
2554 {
2555 	const struct btp_mesh_cfg_model_sub_ovw_cmd *cp = cmd;
2556 	struct btp_mesh_cfg_model_sub_add_rp *rp = rsp;
2557 	uint8_t status;
2558 	int err;
2559 
2560 	LOG_DBG("");
2561 
2562 	err = bt_mesh_cfg_cli_mod_sub_overwrite(sys_le16_to_cpu(cp->net_idx),
2563 						sys_le16_to_cpu(cp->address),
2564 						sys_le16_to_cpu(cp->elem_address),
2565 						sys_le16_to_cpu(cp->sub_addr),
2566 						sys_le16_to_cpu(cp->model_id),
2567 						&status);
2568 	if (err) {
2569 		LOG_ERR("err %d", err);
2570 		return BTP_STATUS_FAILED;
2571 	}
2572 
2573 	rp->status = status;
2574 	*rsp_len = sizeof(*rp);
2575 
2576 	return BTP_STATUS_SUCCESS;
2577 }
2578 
config_mod_sub_del(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2579 static uint8_t config_mod_sub_del(const void *cmd, uint16_t cmd_len,
2580 				  void *rsp, uint16_t *rsp_len)
2581 {
2582 	const struct btp_mesh_cfg_model_sub_del_cmd *cp = cmd;
2583 	struct btp_mesh_cfg_model_sub_del_rp *rp = rsp;
2584 	uint8_t status;
2585 	int err;
2586 
2587 	LOG_DBG("");
2588 
2589 	err = bt_mesh_cfg_cli_mod_sub_del(sys_le16_to_cpu(cp->net_idx),
2590 					  sys_le16_to_cpu(cp->address),
2591 					  sys_le16_to_cpu(cp->elem_address),
2592 					  sys_le16_to_cpu(cp->sub_addr),
2593 					  sys_le16_to_cpu(cp->model_id),
2594 					  &status);
2595 	if (err) {
2596 		LOG_ERR("err %d", err);
2597 		return BTP_STATUS_FAILED;
2598 	}
2599 
2600 	rp->status = status;
2601 	*rsp_len = sizeof(*rp);
2602 
2603 	return BTP_STATUS_SUCCESS;
2604 }
2605 
config_mod_sub_del_all(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2606 static uint8_t config_mod_sub_del_all(const void *cmd, uint16_t cmd_len,
2607 				      void *rsp, uint16_t *rsp_len)
2608 {
2609 	const struct btp_mesh_cfg_model_sub_del_all_cmd *cp = cmd;
2610 	struct btp_mesh_cfg_model_sub_del_all_rp *rp = rsp;
2611 	uint8_t status;
2612 	int err;
2613 
2614 	LOG_DBG("");
2615 
2616 	err = bt_mesh_cfg_cli_mod_sub_del_all(sys_le16_to_cpu(cp->net_idx),
2617 					      sys_le16_to_cpu(cp->address),
2618 					      sys_le16_to_cpu(cp->elem_address),
2619 					      sys_le16_to_cpu(cp->model_id),
2620 					      &status);
2621 
2622 	if (err) {
2623 		LOG_ERR("err %d", err);
2624 		return BTP_STATUS_FAILED;
2625 	}
2626 
2627 	rp->status = status;
2628 	*rsp_len = sizeof(*rp);
2629 
2630 	return BTP_STATUS_SUCCESS;
2631 }
2632 
config_mod_sub_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2633 static uint8_t config_mod_sub_get(const void *cmd, uint16_t cmd_len,
2634 				  void *rsp, uint16_t *rsp_len)
2635 {
2636 	const struct btp_mesh_cfg_model_sub_get_cmd *cp = cmd;
2637 	struct btp_mesh_cfg_model_sub_get_rp *rp = rsp;
2638 	uint8_t status;
2639 	int16_t subs;
2640 	size_t sub_cn;
2641 	int err;
2642 
2643 	LOG_DBG("");
2644 
2645 	err = bt_mesh_cfg_cli_mod_sub_get(sys_le16_to_cpu(cp->net_idx),
2646 					  sys_le16_to_cpu(cp->address),
2647 					  sys_le16_to_cpu(cp->elem_address),
2648 					  sys_le16_to_cpu(cp->model_id),
2649 					  &status, &subs, &sub_cn);
2650 	if (err) {
2651 		LOG_ERR("err %d", err);
2652 		return BTP_STATUS_FAILED;
2653 	}
2654 
2655 	rp->status = status;
2656 	*rsp_len = sizeof(*rp);
2657 
2658 	return BTP_STATUS_SUCCESS;
2659 }
2660 
config_mod_sub_get_vnd(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2661 static uint8_t config_mod_sub_get_vnd(const void *cmd, uint16_t cmd_len,
2662 				      void *rsp, uint16_t *rsp_len)
2663 {
2664 	const struct btp_mesh_cfg_model_sub_get_vnd_cmd *cp = cmd;
2665 	struct btp_mesh_cfg_model_sub_get_vnd_rp *rp = rsp;
2666 	uint8_t status;
2667 	uint16_t subs;
2668 	size_t sub_cn;
2669 	int err;
2670 
2671 	LOG_DBG("");
2672 
2673 	err = bt_mesh_cfg_cli_mod_sub_get_vnd(sys_le16_to_cpu(cp->net_idx),
2674 					      sys_le16_to_cpu(cp->address),
2675 					      sys_le16_to_cpu(cp->elem_address),
2676 					      sys_le16_to_cpu(cp->model_id),
2677 					      sys_le16_to_cpu(cp->cid),
2678 					      &status, &subs, &sub_cn);
2679 	if (err) {
2680 		LOG_ERR("err %d", err);
2681 		return BTP_STATUS_FAILED;
2682 	}
2683 
2684 	rp->status = status;
2685 	*rsp_len = sizeof(*rp);
2686 
2687 	return BTP_STATUS_SUCCESS;
2688 }
2689 
config_mod_sub_va_add(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2690 static uint8_t config_mod_sub_va_add(const void *cmd, uint16_t cmd_len,
2691 				     void *rsp, uint16_t *rsp_len)
2692 {
2693 	const struct btp_mesh_cfg_model_sub_va_add_cmd *cp = cmd;
2694 	struct btp_mesh_cfg_model_sub_va_add_rp *rp = rsp;
2695 	uint8_t status;
2696 	uint16_t virt_addr_rcv;
2697 	int err;
2698 
2699 	LOG_DBG("");
2700 
2701 	err = bt_mesh_cfg_cli_mod_sub_va_add(sys_le16_to_cpu(cp->net_idx),
2702 					     sys_le16_to_cpu(cp->address),
2703 					     sys_le16_to_cpu(cp->elem_address),
2704 					     sys_le16_to_cpu(cp->uuid),
2705 					     sys_le16_to_cpu(cp->model_id),
2706 					     &virt_addr_rcv, &status);
2707 
2708 	if (err) {
2709 		LOG_ERR("err %d", err);
2710 		return BTP_STATUS_FAILED;
2711 	}
2712 
2713 	rp->status = status;
2714 	*rsp_len = sizeof(*rp);
2715 
2716 	return BTP_STATUS_SUCCESS;
2717 }
2718 
config_mod_sub_va_del(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2719 static uint8_t config_mod_sub_va_del(const void *cmd, uint16_t cmd_len,
2720 				     void *rsp, uint16_t *rsp_len)
2721 {
2722 	const struct btp_mesh_cfg_model_sub_va_del_cmd *cp = cmd;
2723 	struct btp_mesh_cfg_model_sub_va_del_rp *rp = rsp;
2724 	uint8_t status;
2725 	uint16_t virt_addr_rcv;
2726 	int err;
2727 
2728 	LOG_DBG("");
2729 
2730 	err = bt_mesh_cfg_cli_mod_sub_va_del(sys_le16_to_cpu(cp->net_idx),
2731 					     sys_le16_to_cpu(cp->address),
2732 					     sys_le16_to_cpu(cp->elem_address),
2733 					     sys_le16_to_cpu(cp->uuid),
2734 					     sys_le16_to_cpu(cp->model_id),
2735 					     &virt_addr_rcv, &status);
2736 
2737 	if (err) {
2738 		LOG_ERR("err %d", err);
2739 		return BTP_STATUS_FAILED;
2740 	}
2741 
2742 	rp->status = status;
2743 	*rsp_len = sizeof(*rp);
2744 
2745 	return BTP_STATUS_SUCCESS;
2746 }
2747 
config_mod_sub_va_ovw(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2748 static uint8_t config_mod_sub_va_ovw(const void *cmd, uint16_t cmd_len,
2749 				     void *rsp, uint16_t *rsp_len)
2750 {
2751 	const struct btp_mesh_cfg_model_sub_va_ovw_cmd *cp = cmd;
2752 	struct btp_mesh_cfg_model_sub_va_ovw_rp *rp = rsp;
2753 	uint8_t status;
2754 	uint16_t virt_addr_rcv;
2755 	int err;
2756 
2757 	LOG_DBG("");
2758 
2759 	err = bt_mesh_cfg_cli_mod_sub_va_overwrite(sys_le16_to_cpu(cp->net_idx),
2760 						   sys_le16_to_cpu(cp->address),
2761 						   sys_le16_to_cpu(cp->elem_address),
2762 						   sys_le16_to_cpu(cp->uuid),
2763 						   sys_le16_to_cpu(cp->model_id),
2764 						   &virt_addr_rcv, &status);
2765 
2766 	if (err) {
2767 		LOG_ERR("err %d", err);
2768 		return BTP_STATUS_FAILED;
2769 	}
2770 
2771 	rp->status = status;
2772 	*rsp_len = sizeof(*rp);
2773 
2774 	return BTP_STATUS_SUCCESS;
2775 }
2776 
config_netkey_add(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2777 static uint8_t config_netkey_add(const void *cmd, uint16_t cmd_len,
2778 				 void *rsp, uint16_t *rsp_len)
2779 {
2780 	const struct btp_mesh_cfg_netkey_add_cmd *cp = cmd;
2781 	struct btp_mesh_cfg_netkey_add_rp *rp = rsp;
2782 	uint8_t status;
2783 	int err;
2784 
2785 	LOG_DBG("");
2786 
2787 	err = bt_mesh_cfg_cli_net_key_add(sys_le16_to_cpu(cp->net_idx),
2788 					  sys_le16_to_cpu(cp->address),
2789 					  sys_le16_to_cpu(cp->net_key_idx),
2790 					  cp->net_key, &status);
2791 
2792 	if (err) {
2793 		LOG_ERR("err %d", err);
2794 		return BTP_STATUS_FAILED;
2795 	}
2796 
2797 	rp->status = status;
2798 	*rsp_len = sizeof(*rp);
2799 
2800 	return BTP_STATUS_SUCCESS;
2801 }
2802 
config_netkey_update(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2803 static uint8_t config_netkey_update(const void *cmd, uint16_t cmd_len,
2804 				    void *rsp, uint16_t *rsp_len)
2805 {
2806 	const struct btp_mesh_cfg_netkey_update_cmd *cp = cmd;
2807 	struct btp_mesh_cfg_netkey_update_rp *rp = rsp;
2808 	uint8_t status;
2809 	int err;
2810 
2811 	LOG_DBG("");
2812 
2813 	err = bt_mesh_cfg_cli_net_key_update(sys_le16_to_cpu(cp->net_idx),
2814 					     sys_le16_to_cpu(cp->address),
2815 					     sys_le16_to_cpu(cp->net_key_idx),
2816 					     cp->net_key,
2817 					     &status);
2818 
2819 	if (err) {
2820 		LOG_ERR("err %d", err);
2821 		return BTP_STATUS_FAILED;
2822 	}
2823 
2824 	rp->status = status;
2825 	*rsp_len = sizeof(*rp);
2826 
2827 	return BTP_STATUS_SUCCESS;
2828 }
2829 
config_netkey_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2830 static uint8_t config_netkey_get(const void *cmd, uint16_t cmd_len,
2831 				 void *rsp, uint16_t *rsp_len)
2832 {
2833 	const struct btp_mesh_cfg_netkey_get_cmd *cp = cmd;
2834 	struct btp_mesh_cfg_netkey_get_rp *rp = rsp;
2835 	size_t key_cnt = 1;
2836 	uint16_t keys;
2837 	int err;
2838 
2839 	LOG_DBG("");
2840 
2841 	err = bt_mesh_cfg_cli_net_key_get(sys_le16_to_cpu(cp->net_idx),
2842 					  sys_le16_to_cpu(cp->address),
2843 					  &keys, &key_cnt);
2844 	if (err) {
2845 		LOG_ERR("err %d", err);
2846 		return BTP_STATUS_FAILED;
2847 	}
2848 
2849 	/* for historical reasons this command has status in response */
2850 	rp->status = 0;
2851 	*rsp_len = sizeof(*rp);
2852 
2853 	return BTP_STATUS_SUCCESS;
2854 }
2855 
config_netkey_del(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2856 static uint8_t config_netkey_del(const void *cmd, uint16_t cmd_len,
2857 				 void *rsp, uint16_t *rsp_len)
2858 {
2859 	const struct btp_mesh_cfg_netkey_del_cmd *cp = cmd;
2860 	struct btp_mesh_cfg_netkey_del_rp *rp = rsp;
2861 	uint8_t status;
2862 	int err;
2863 
2864 	LOG_DBG("");
2865 
2866 	err = bt_mesh_cfg_cli_net_key_del(sys_le16_to_cpu(cp->net_idx),
2867 					  sys_le16_to_cpu(cp->address),
2868 					  sys_le16_to_cpu(cp->net_key_idx),
2869 					  &status);
2870 
2871 	if (err) {
2872 		LOG_ERR("err %d", err);
2873 		return BTP_STATUS_FAILED;
2874 	}
2875 
2876 	rp->status = status;
2877 	*rsp_len = sizeof(*rp);
2878 
2879 	return BTP_STATUS_SUCCESS;
2880 }
2881 
config_appkey_add(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2882 static uint8_t config_appkey_add(const void *cmd, uint16_t cmd_len,
2883 				 void *rsp, uint16_t *rsp_len)
2884 {
2885 	const struct btp_mesh_cfg_appkey_add_cmd *cp = cmd;
2886 	struct btp_mesh_cfg_appkey_add_rp *rp = rsp;
2887 	uint8_t status;
2888 	int err;
2889 
2890 	LOG_DBG("");
2891 
2892 	err = bt_mesh_cfg_cli_app_key_add(sys_le16_to_cpu(cp->net_idx),
2893 					  sys_le16_to_cpu(cp->address),
2894 					  sys_le16_to_cpu(cp->net_key_idx),
2895 					  sys_le16_to_cpu(cp->app_key_idx),
2896 					  sys_le16_to_cpu(cp->app_key),
2897 					  &status);
2898 
2899 	if (err) {
2900 		LOG_ERR("err %d", err);
2901 		return BTP_STATUS_FAILED;
2902 	}
2903 
2904 	rp->status = status;
2905 	*rsp_len = sizeof(*rp);
2906 
2907 	return BTP_STATUS_SUCCESS;
2908 }
2909 
config_appkey_update(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2910 static uint8_t config_appkey_update(const void *cmd, uint16_t cmd_len,
2911 				    void *rsp, uint16_t *rsp_len)
2912 {
2913 	const struct btp_mesh_cfg_appkey_update_cmd *cp = cmd;
2914 	struct btp_mesh_cfg_appkey_update_rp *rp = rsp;
2915 	uint8_t status;
2916 	int err;
2917 
2918 	LOG_DBG("");
2919 
2920 	err = bt_mesh_cfg_cli_app_key_update(sys_le16_to_cpu(cp->net_idx),
2921 					     sys_le16_to_cpu(cp->address),
2922 					     sys_le16_to_cpu(cp->net_key_idx),
2923 					     sys_le16_to_cpu(cp->app_key_idx),
2924 					     sys_le16_to_cpu(cp->app_key),
2925 					     &status);
2926 
2927 	if (err) {
2928 		LOG_ERR("err %d", err);
2929 		return BTP_STATUS_FAILED;
2930 	}
2931 
2932 	rp->status = status;
2933 	*rsp_len = sizeof(*rp);
2934 
2935 	return BTP_STATUS_SUCCESS;
2936 }
2937 
config_appkey_del(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2938 static uint8_t config_appkey_del(const void *cmd, uint16_t cmd_len,
2939 				 void *rsp, uint16_t *rsp_len)
2940 {
2941 	const struct btp_mesh_cfg_appkey_del_cmd *cp = cmd;
2942 	struct btp_mesh_cfg_appkey_del_rp *rp = rsp;
2943 	uint8_t status;
2944 	int err;
2945 
2946 	LOG_DBG("");
2947 
2948 	err = bt_mesh_cfg_cli_app_key_del(sys_le16_to_cpu(cp->net_idx),
2949 					  sys_le16_to_cpu(cp->address),
2950 					  sys_le16_to_cpu(cp->net_key_idx),
2951 					  sys_le16_to_cpu(cp->app_key_idx),
2952 					  &status);
2953 
2954 	if (err) {
2955 		LOG_ERR("err %d", err);
2956 		return BTP_STATUS_FAILED;
2957 	}
2958 
2959 	rp->status = status;
2960 	*rsp_len = sizeof(*rp);
2961 
2962 	return BTP_STATUS_SUCCESS;
2963 }
2964 
config_appkey_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2965 static uint8_t config_appkey_get(const void *cmd, uint16_t cmd_len,
2966 				 void *rsp, uint16_t *rsp_len)
2967 {
2968 	const struct btp_mesh_cfg_appkey_get_cmd *cp = cmd;
2969 	struct btp_mesh_cfg_appkey_get_rp *rp = rsp;
2970 	uint8_t status;
2971 	uint16_t keys;
2972 	size_t key_cnt = 1;
2973 	int err;
2974 
2975 	LOG_DBG("");
2976 
2977 	err = bt_mesh_cfg_cli_app_key_get(sys_le16_to_cpu(cp->net_idx),
2978 					  sys_le16_to_cpu(cp->address),
2979 					  sys_le16_to_cpu(cp->net_key_idx),
2980 					  &status, &keys, &key_cnt);
2981 
2982 	if (err) {
2983 		LOG_ERR("err %d", err);
2984 		return BTP_STATUS_FAILED;
2985 	}
2986 
2987 	rp->status = status;
2988 	*rsp_len = sizeof(*rp);
2989 
2990 	return BTP_STATUS_SUCCESS;
2991 }
2992 
2993 
config_model_app_bind(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)2994 static uint8_t config_model_app_bind(const void *cmd, uint16_t cmd_len,
2995 				 void *rsp, uint16_t *rsp_len)
2996 {
2997 	const struct btp_mesh_cfg_model_app_bind_cmd *cp = cmd;
2998 	struct btp_mesh_cfg_model_app_bind_rp *rp = rsp;
2999 	uint8_t status;
3000 	int err;
3001 
3002 	LOG_DBG("");
3003 
3004 	bt_mesh_cfg_cli_timeout_set(5000);
3005 
3006 	err = bt_mesh_cfg_cli_mod_app_bind(sys_le16_to_cpu(cp->net_idx),
3007 					   sys_le16_to_cpu(cp->address),
3008 					   sys_le16_to_cpu(cp->elem_address),
3009 					   sys_le16_to_cpu(cp->app_key_idx),
3010 					   sys_le16_to_cpu(cp->mod_id),
3011 					   &status);
3012 
3013 	if (err) {
3014 		LOG_ERR("err %d", err);
3015 		return BTP_STATUS_FAILED;
3016 	}
3017 
3018 	rp->status = status;
3019 	*rsp_len = sizeof(*rp);
3020 
3021 	return BTP_STATUS_SUCCESS;
3022 }
3023 
config_model_app_bind_vnd(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3024 static uint8_t config_model_app_bind_vnd(const void *cmd, uint16_t cmd_len,
3025 					 void *rsp, uint16_t *rsp_len)
3026 {
3027 	const struct btp_mesh_cfg_model_app_bind_vnd_cmd *cp = cmd;
3028 	struct btp_mesh_cfg_model_app_bind_vnd_rp *rp = rsp;
3029 	uint8_t status;
3030 	int err;
3031 
3032 	LOG_DBG("");
3033 
3034 	err = bt_mesh_cfg_cli_mod_app_bind_vnd(sys_le16_to_cpu(cp->net_idx),
3035 					       sys_le16_to_cpu(cp->address),
3036 					       sys_le16_to_cpu(cp->elem_address),
3037 					       sys_le16_to_cpu(cp->app_key_idx),
3038 					       sys_le16_to_cpu(cp->mod_id),
3039 					       sys_le16_to_cpu(cp->cid),
3040 					       &status);
3041 
3042 	if (err) {
3043 		LOG_ERR("err %d", err);
3044 		return BTP_STATUS_FAILED;
3045 	}
3046 
3047 	rp->status = status;
3048 	*rsp_len = sizeof(*rp);
3049 
3050 	return BTP_STATUS_SUCCESS;
3051 }
3052 
config_model_app_unbind(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3053 static uint8_t config_model_app_unbind(const void *cmd, uint16_t cmd_len,
3054 				       void *rsp, uint16_t *rsp_len)
3055 {
3056 	const struct btp_mesh_cfg_model_app_unbind_cmd *cp = cmd;
3057 	struct btp_mesh_cfg_model_app_unbind_rp *rp = rsp;
3058 	uint8_t status;
3059 	int err;
3060 
3061 	LOG_DBG("");
3062 
3063 	err = bt_mesh_cfg_cli_mod_app_unbind(sys_le16_to_cpu(cp->net_idx),
3064 					     sys_le16_to_cpu(cp->address),
3065 					     sys_le16_to_cpu(cp->elem_address),
3066 					     sys_le16_to_cpu(cp->app_key_idx),
3067 					     sys_le16_to_cpu(cp->mod_id),
3068 					     &status);
3069 	if (err) {
3070 		LOG_ERR("err %d", err);
3071 		return BTP_STATUS_FAILED;
3072 	}
3073 
3074 	rp->status = status;
3075 	*rsp_len = sizeof(*rp);
3076 
3077 	return BTP_STATUS_SUCCESS;
3078 }
3079 
3080 
config_model_app_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3081 static uint8_t config_model_app_get(const void *cmd, uint16_t cmd_len,
3082 				    void *rsp, uint16_t *rsp_len)
3083 {
3084 	const struct btp_mesh_cfg_model_app_get_cmd *cp = cmd;
3085 	struct btp_mesh_cfg_model_app_get_rp *rp = rsp;
3086 	uint8_t status;
3087 	uint16_t apps;
3088 	size_t app_cnt;
3089 	int err;
3090 
3091 	LOG_DBG("");
3092 
3093 	err = bt_mesh_cfg_cli_mod_app_get(sys_le16_to_cpu(cp->net_idx),
3094 					  sys_le16_to_cpu(cp->address),
3095 					  sys_le16_to_cpu(cp->elem_address),
3096 					  sys_le16_to_cpu(cp->mod_id),
3097 					  &status, &apps, &app_cnt);
3098 
3099 	if (err) {
3100 		LOG_ERR("err %d", err);
3101 		return BTP_STATUS_FAILED;
3102 	}
3103 
3104 	rp->status = status;
3105 	*rsp_len = sizeof(*rp);
3106 
3107 	return BTP_STATUS_SUCCESS;
3108 }
3109 
config_model_app_vnd_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3110 static uint8_t config_model_app_vnd_get(const void *cmd, uint16_t cmd_len,
3111 				    void *rsp, uint16_t *rsp_len)
3112 {
3113 	const struct btp_mesh_cfg_model_app_vnd_get_cmd *cp = cmd;
3114 	struct btp_mesh_cfg_model_app_vnd_get_rp *rp = rsp;
3115 	uint8_t status;
3116 	uint16_t apps;
3117 	size_t app_cnt;
3118 	int err;
3119 
3120 	LOG_DBG("");
3121 
3122 	err = bt_mesh_cfg_cli_mod_app_get_vnd(sys_le16_to_cpu(cp->net_idx),
3123 					      sys_le16_to_cpu(cp->address),
3124 					      sys_le16_to_cpu(cp->elem_address),
3125 					      sys_le16_to_cpu(cp->mod_id),
3126 					      sys_le16_to_cpu(cp->cid),
3127 					      &status, &apps, &app_cnt);
3128 	if (err) {
3129 		LOG_ERR("err %d", err);
3130 		return BTP_STATUS_FAILED;
3131 	}
3132 
3133 	rp->status = status;
3134 	*rsp_len = sizeof(*rp);
3135 
3136 	return BTP_STATUS_SUCCESS;
3137 }
3138 
config_hb_pub_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3139 static uint8_t config_hb_pub_set(const void *cmd, uint16_t cmd_len,
3140 				 void *rsp, uint16_t *rsp_len)
3141 {
3142 	const struct btp_mesh_cfg_heartbeat_pub_set_cmd *cp = cmd;
3143 	struct btp_mesh_cfg_heartbeat_pub_set_rp *rp = rsp;
3144 	uint8_t status;
3145 	struct bt_mesh_cfg_cli_hb_pub pub;
3146 	int err;
3147 
3148 	LOG_DBG("");
3149 
3150 	pub.net_idx = sys_le16_to_cpu(cp->net_key_idx);
3151 	pub.dst = sys_le16_to_cpu(cp->destination);
3152 	pub.count = cp->count_log;
3153 	pub.period = cp->period_log;
3154 	pub.ttl = cp->ttl;
3155 	pub.feat = sys_le16_to_cpu(cp->features);
3156 
3157 	err = bt_mesh_cfg_cli_hb_pub_set(sys_le16_to_cpu(cp->net_idx),
3158 					 sys_le16_to_cpu(cp->address),
3159 					 &pub, &status);
3160 
3161 	if (err) {
3162 		LOG_ERR("err %d", err);
3163 		return BTP_STATUS_FAILED;
3164 	}
3165 
3166 	rp->status = status;
3167 	*rsp_len = sizeof(*rp);
3168 
3169 	return BTP_STATUS_SUCCESS;
3170 }
3171 
config_hb_pub_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3172 static uint8_t config_hb_pub_get(const void *cmd, uint16_t cmd_len,
3173 				 void *rsp, uint16_t *rsp_len)
3174 {
3175 	const struct btp_mesh_cfg_heartbeat_pub_get_cmd *cp = cmd;
3176 	struct btp_mesh_cfg_heartbeat_pub_get_rp *rp = rsp;
3177 	uint8_t status;
3178 	struct bt_mesh_cfg_cli_hb_pub pub;
3179 	int err;
3180 
3181 	LOG_DBG("");
3182 
3183 	err = bt_mesh_cfg_cli_hb_pub_get(sys_le16_to_cpu(cp->net_idx),
3184 					 sys_le16_to_cpu(cp->address),
3185 					 &pub, &status);
3186 
3187 	if (err) {
3188 		LOG_ERR("err %d", err);
3189 		return BTP_STATUS_FAILED;
3190 	}
3191 
3192 	rp->status = status;
3193 	*rsp_len = sizeof(*rp);
3194 
3195 	return BTP_STATUS_SUCCESS;
3196 }
3197 
config_hb_sub_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3198 static uint8_t config_hb_sub_set(const void *cmd, uint16_t cmd_len,
3199 				 void *rsp, uint16_t *rsp_len)
3200 {
3201 	const struct btp_mesh_cfg_heartbeat_sub_set_cmd *cp = cmd;
3202 	struct btp_mesh_cfg_heartbeat_sub_set_rp *rp = rsp;
3203 	uint8_t status;
3204 	struct bt_mesh_cfg_cli_hb_sub sub;
3205 	int err;
3206 
3207 	LOG_DBG("");
3208 
3209 	sub.src = sys_le16_to_cpu(cp->source);
3210 	sub.dst = sys_le16_to_cpu(cp->destination);
3211 	sub.period = cp->period_log;
3212 
3213 	err = bt_mesh_cfg_cli_hb_sub_set(sys_le16_to_cpu(cp->net_idx),
3214 					 sys_le16_to_cpu(cp->address),
3215 					 &sub, &status);
3216 
3217 	if (err) {
3218 		LOG_ERR("err %d", err);
3219 		return BTP_STATUS_FAILED;
3220 	}
3221 
3222 	rp->status = status;
3223 	*rsp_len = sizeof(*rp);
3224 
3225 	return BTP_STATUS_SUCCESS;
3226 }
3227 
config_hb_sub_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3228 static uint8_t config_hb_sub_get(const void *cmd, uint16_t cmd_len,
3229 				 void *rsp, uint16_t *rsp_len)
3230 {
3231 	const struct btp_mesh_cfg_heartbeat_sub_get_cmd *cp = cmd;
3232 	struct btp_mesh_cfg_heartbeat_sub_get_rp *rp = rsp;
3233 	uint8_t status;
3234 	struct bt_mesh_cfg_cli_hb_sub sub;
3235 	int err;
3236 
3237 	LOG_DBG("");
3238 
3239 	err = bt_mesh_cfg_cli_hb_sub_get(sys_le16_to_cpu(cp->net_idx),
3240 					 sys_le16_to_cpu(cp->address),
3241 					 &sub, &status);
3242 
3243 	if (err) {
3244 		LOG_ERR("err %d", err);
3245 		return BTP_STATUS_FAILED;
3246 	}
3247 
3248 	rp->status = status;
3249 	*rsp_len = sizeof(*rp);
3250 
3251 	return BTP_STATUS_SUCCESS;
3252 }
3253 
config_net_trans_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3254 static uint8_t config_net_trans_get(const void *cmd, uint16_t cmd_len,
3255 				    void *rsp, uint16_t *rsp_len)
3256 {
3257 	const struct btp_mesh_cfg_net_trans_get_cmd *cp = cmd;
3258 	struct btp_mesh_cfg_net_trans_get_rp *rp = rsp;
3259 	uint8_t transmit;
3260 	int err;
3261 
3262 	LOG_DBG("");
3263 
3264 	err = bt_mesh_cfg_cli_net_transmit_get(sys_le16_to_cpu(cp->net_idx),
3265 					       sys_le16_to_cpu(cp->address),
3266 					       &transmit);
3267 
3268 	if (err) {
3269 		LOG_ERR("err %d", err);
3270 		return BTP_STATUS_FAILED;
3271 	}
3272 
3273 	rp->transmit = transmit;
3274 	*rsp_len = sizeof(*rp);
3275 
3276 	return BTP_STATUS_SUCCESS;
3277 }
3278 
config_net_trans_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3279 static uint8_t config_net_trans_set(const void *cmd, uint16_t cmd_len,
3280 				    void *rsp, uint16_t *rsp_len)
3281 {
3282 	const struct btp_mesh_cfg_net_trans_set_cmd *cp = cmd;
3283 	struct btp_mesh_cfg_net_trans_set_rp *rp = rsp;
3284 	uint8_t transmit;
3285 	int err;
3286 
3287 	LOG_DBG("");
3288 
3289 	err = bt_mesh_cfg_cli_net_transmit_set(sys_le16_to_cpu(cp->net_idx),
3290 					       sys_le16_to_cpu(cp->address),
3291 					       cp->transmit, &transmit);
3292 
3293 	if (err) {
3294 		LOG_ERR("err %d", err);
3295 		return BTP_STATUS_FAILED;
3296 	}
3297 
3298 	rp->transmit = transmit;
3299 	*rsp_len = sizeof(*rp);
3300 
3301 	return BTP_STATUS_SUCCESS;
3302 }
3303 
config_node_identity_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3304 static uint8_t config_node_identity_set(const void *cmd, uint16_t cmd_len,
3305 				    void *rsp, uint16_t *rsp_len)
3306 {
3307 	const struct btp_mesh_cfg_node_idt_set_cmd *cp = cmd;
3308 	struct btp_mesh_cfg_node_idt_set_rp *rp = rsp;
3309 	uint8_t identity;
3310 	uint8_t status;
3311 	int err;
3312 
3313 	LOG_DBG("");
3314 
3315 	err = bt_mesh_cfg_cli_node_identity_set(sys_le16_to_cpu(cp->net_idx),
3316 						sys_le16_to_cpu(cp->address),
3317 						sys_le16_to_cpu(cp->net_key_idx),
3318 						cp->new_identity,
3319 						&status, &identity);
3320 
3321 	if (err) {
3322 		LOG_ERR("err %d", err);
3323 		return BTP_STATUS_FAILED;
3324 	}
3325 
3326 	rp->status = status;
3327 	rp->identity = identity;
3328 
3329 	*rsp_len = sizeof(*rp);
3330 	return BTP_STATUS_SUCCESS;
3331 }
3332 
config_node_identity_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3333 static uint8_t config_node_identity_get(const void *cmd, uint16_t cmd_len,
3334 				    void *rsp, uint16_t *rsp_len)
3335 {
3336 	const struct btp_mesh_cfg_node_idt_get_cmd *cp = cmd;
3337 	struct btp_mesh_cfg_node_idt_get_rp *rp = rsp;
3338 	uint8_t identity;
3339 	uint8_t status;
3340 	int err;
3341 
3342 	LOG_DBG("");
3343 
3344 	err = bt_mesh_cfg_cli_node_identity_get(sys_le16_to_cpu(cp->net_idx),
3345 						sys_le16_to_cpu(cp->address),
3346 						sys_le16_to_cpu(cp->net_key_idx),
3347 						&status, &identity);
3348 
3349 	if (err) {
3350 		LOG_ERR("err %d", err);
3351 		return BTP_STATUS_FAILED;
3352 	}
3353 
3354 	rp->status = status;
3355 	rp->identity = identity;
3356 
3357 	*rsp_len = sizeof(*rp);
3358 	return BTP_STATUS_SUCCESS;
3359 }
3360 
config_node_reset(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3361 static uint8_t config_node_reset(const void *cmd, uint16_t cmd_len,
3362 				 void *rsp, uint16_t *rsp_len)
3363 {
3364 	const struct btp_mesh_cfg_node_reset_cmd *cp = cmd;
3365 	struct btp_mesh_cfg_node_reset_rp *rp = rsp;
3366 	bool status;
3367 	int err;
3368 
3369 	LOG_DBG("");
3370 
3371 	err = bt_mesh_cfg_cli_node_reset(sys_le16_to_cpu(cp->net_idx),
3372 					 sys_le16_to_cpu(cp->address),
3373 					 &status);
3374 
3375 	if (err) {
3376 		LOG_ERR("err %d", err);
3377 		return BTP_STATUS_FAILED;
3378 	}
3379 
3380 	rp->status = status;
3381 
3382 	*rsp_len = sizeof(*rp);
3383 	return BTP_STATUS_SUCCESS;
3384 }
3385 
config_lpn_timeout_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3386 static uint8_t config_lpn_timeout_get(const void *cmd, uint16_t cmd_len,
3387 				      void *rsp, uint16_t *rsp_len)
3388 {
3389 	const struct btp_mesh_cfg_lpn_timeout_cmd *cp = cmd;
3390 	struct btp_mesh_cfg_lpn_timeout_rp *rp = rsp;
3391 	int32_t polltimeout;
3392 	int err;
3393 
3394 	LOG_DBG("");
3395 
3396 	err = bt_mesh_cfg_cli_lpn_timeout_get(sys_le16_to_cpu(cp->net_idx),
3397 					      sys_le16_to_cpu(cp->address),
3398 					      sys_le16_to_cpu(cp->unicast_addr),
3399 					      &polltimeout);
3400 
3401 	if (err) {
3402 		LOG_ERR("err %d", err);
3403 		return BTP_STATUS_FAILED;
3404 	}
3405 
3406 	rp->timeout = sys_cpu_to_le32(polltimeout);
3407 
3408 	*rsp_len = sizeof(*rp);
3409 	return BTP_STATUS_SUCCESS;
3410 }
3411 
health_fault_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3412 static uint8_t health_fault_get(const void *cmd, uint16_t cmd_len,
3413 				void *rsp, uint16_t *rsp_len)
3414 {
3415 	const struct btp_mesh_health_fault_get_cmd *cp = cmd;
3416 	struct bt_mesh_msg_ctx ctx = {
3417 		.net_idx = net.net_idx,
3418 		.addr = sys_le16_to_cpu(cp->address),
3419 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3420 	};
3421 	uint8_t test_id;
3422 	size_t fault_count = 16;
3423 	uint8_t faults[fault_count];
3424 	int err;
3425 
3426 	LOG_DBG("");
3427 
3428 	err = bt_mesh_health_cli_fault_get(&health_cli, &ctx,
3429 					   sys_le16_to_cpu(cp->cid), &test_id,
3430 					   faults, &fault_count);
3431 
3432 	if (err) {
3433 		LOG_ERR("err %d", err);
3434 		return BTP_STATUS_FAILED;
3435 	}
3436 
3437 	return BTP_STATUS_SUCCESS;
3438 }
3439 
health_fault_clear(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3440 static uint8_t health_fault_clear(const void *cmd, uint16_t cmd_len,
3441 				  void *rsp, uint16_t *rsp_len)
3442 {
3443 	const struct btp_mesh_health_fault_clear_cmd *cp = cmd;
3444 	struct bt_mesh_msg_ctx ctx = {
3445 		.net_idx = net.net_idx,
3446 		.addr = sys_le16_to_cpu(cp->address),
3447 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3448 	};
3449 	uint8_t test_id = 0;
3450 	size_t fault_count = 16;
3451 	uint8_t faults[fault_count];
3452 	int err;
3453 
3454 	LOG_DBG("");
3455 
3456 	if (cp->ack) {
3457 		err = bt_mesh_health_cli_fault_clear(&health_cli, &ctx,
3458 						     sys_le16_to_cpu(cp->cid),
3459 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3460 						     bt_mesh_op_agg_cli_seq_is_started() ?
3461 						     NULL :
3462 #endif
3463 						     &test_id,
3464 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3465 						     bt_mesh_op_agg_cli_seq_is_started() ?
3466 						     NULL :
3467 #endif
3468 						     faults,
3469 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3470 						     bt_mesh_op_agg_cli_seq_is_started() ?
3471 						     NULL :
3472 #endif
3473 						     &fault_count);
3474 	} else {
3475 		err = bt_mesh_health_cli_fault_clear_unack(&health_cli, &ctx,
3476 							   sys_le16_to_cpu(cp->cid));
3477 	}
3478 
3479 	if (err) {
3480 		LOG_ERR("err %d", err);
3481 		return BTP_STATUS_FAILED;
3482 	}
3483 
3484 	if (cp->ack) {
3485 		struct btp_mesh_health_fault_clear_rp *rp = rsp;
3486 
3487 		rp->test_id = test_id;
3488 		*rsp_len = sizeof(*rp);
3489 	}
3490 
3491 	return BTP_STATUS_SUCCESS;
3492 }
3493 
health_fault_test(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3494 static uint8_t health_fault_test(const void *cmd, uint16_t cmd_len,
3495 				 void *rsp, uint16_t *rsp_len)
3496 {
3497 	const struct btp_mesh_health_fault_test_cmd *cp = cmd;
3498 	struct bt_mesh_msg_ctx ctx = {
3499 		.net_idx = net.net_idx,
3500 		.addr = sys_le16_to_cpu(cp->address),
3501 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3502 	};
3503 	size_t fault_count = 16;
3504 	uint8_t faults[fault_count];
3505 	int err;
3506 
3507 	LOG_DBG("");
3508 
3509 	if (cp->ack) {
3510 		err = bt_mesh_health_cli_fault_test(&health_cli, &ctx,
3511 						    sys_le16_to_cpu(cp->cid),
3512 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3513 						    bt_mesh_op_agg_cli_seq_is_started() ?
3514 						    0 :
3515 #endif
3516 						    cp->test_id,
3517 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3518 						    bt_mesh_op_agg_cli_seq_is_started() ?
3519 						    NULL :
3520 #endif
3521 						    faults,
3522 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3523 						    bt_mesh_op_agg_cli_seq_is_started() ?
3524 						    NULL :
3525 #endif
3526 						    &fault_count);
3527 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3528 		if (bt_mesh_op_agg_cli_seq_is_started()) {
3529 			fault_count = 0;
3530 		}
3531 #endif
3532 	} else {
3533 		err = bt_mesh_health_cli_fault_test_unack(&health_cli, &ctx,
3534 							  sys_le16_to_cpu(cp->cid),
3535 							  cp->test_id);
3536 	}
3537 
3538 	if (err) {
3539 		LOG_ERR("err %d", err);
3540 		return BTP_STATUS_FAILED;
3541 	}
3542 
3543 	if (cp->ack) {
3544 		struct btp_mesh_health_fault_test_rp *rp = rsp;
3545 
3546 		rp->test_id = cp->test_id;
3547 		rp->cid = cp->cid;
3548 		(void)memcpy(rp->faults, faults, fault_count);
3549 
3550 		*rsp_len = sizeof(*rp) + fault_count;
3551 	}
3552 
3553 	return BTP_STATUS_SUCCESS;
3554 }
3555 
health_period_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3556 static uint8_t health_period_get(const void *cmd, uint16_t cmd_len,
3557 				 void *rsp, uint16_t *rsp_len)
3558 {
3559 	const struct btp_mesh_health_period_get_cmd *cp = cmd;
3560 	struct bt_mesh_msg_ctx ctx = {
3561 		.net_idx = net.net_idx,
3562 		.addr = sys_le16_to_cpu(cp->address),
3563 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3564 	};
3565 	uint8_t divisor;
3566 	int err;
3567 
3568 	LOG_DBG("");
3569 
3570 	err = bt_mesh_health_cli_period_get(&health_cli, &ctx, &divisor);
3571 
3572 	if (err) {
3573 		LOG_ERR("err %d", err);
3574 		return BTP_STATUS_FAILED;
3575 	}
3576 
3577 	return BTP_STATUS_SUCCESS;
3578 }
3579 
health_period_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3580 static uint8_t health_period_set(const void *cmd, uint16_t cmd_len,
3581 				 void *rsp, uint16_t *rsp_len)
3582 {
3583 	const struct btp_mesh_health_period_set_cmd *cp = cmd;
3584 	struct bt_mesh_msg_ctx ctx = {
3585 		.net_idx = net.net_idx,
3586 		.addr = sys_le16_to_cpu(cp->address),
3587 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3588 	};
3589 	uint8_t updated_divisor;
3590 	int err;
3591 
3592 	LOG_DBG("");
3593 
3594 	if (cp->ack) {
3595 		err = bt_mesh_health_cli_period_set(&health_cli, &ctx, cp->divisor,
3596 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3597 						    bt_mesh_op_agg_cli_seq_is_started() ?
3598 						    NULL :
3599 #endif
3600 						    &updated_divisor);
3601 	} else {
3602 		err = bt_mesh_health_cli_period_set_unack(&health_cli, &ctx, cp->divisor);
3603 	}
3604 
3605 	if (err) {
3606 		LOG_ERR("err %d", err);
3607 		return BTP_STATUS_FAILED;
3608 	}
3609 
3610 	if (cp->ack) {
3611 		struct btp_mesh_health_period_set_rp *rp = rsp;
3612 
3613 		rp->divisor = updated_divisor;
3614 
3615 		*rsp_len = sizeof(*rp);
3616 	}
3617 
3618 	return BTP_STATUS_SUCCESS;
3619 }
3620 
health_attention_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3621 static uint8_t health_attention_get(const void *cmd, uint16_t cmd_len,
3622 				    void *rsp, uint16_t *rsp_len)
3623 {
3624 	const struct btp_mesh_health_attention_get_cmd *cp = cmd;
3625 	struct bt_mesh_msg_ctx ctx = {
3626 		.net_idx = net.net_idx,
3627 		.addr = sys_le16_to_cpu(cp->address),
3628 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3629 	};
3630 	uint8_t attention;
3631 	int err;
3632 
3633 	LOG_DBG("");
3634 
3635 	err = bt_mesh_health_cli_attention_get(&health_cli, &ctx, &attention);
3636 
3637 	if (err) {
3638 		LOG_ERR("err %d", err);
3639 		return BTP_STATUS_FAILED;
3640 	}
3641 
3642 	return BTP_STATUS_SUCCESS;
3643 }
3644 
health_attention_set(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3645 static uint8_t health_attention_set(const void *cmd, uint16_t cmd_len,
3646 				    void *rsp, uint16_t *rsp_len)
3647 {
3648 	const struct btp_mesh_health_attention_set_cmd *cp = cmd;
3649 	struct bt_mesh_msg_ctx ctx = {
3650 		.net_idx = net.net_idx,
3651 		.addr = sys_le16_to_cpu(cp->address),
3652 		.app_idx = sys_le16_to_cpu(cp->app_idx),
3653 	};
3654 	uint8_t updated_attention;
3655 	int err;
3656 
3657 	LOG_DBG("");
3658 
3659 	if (cp->ack) {
3660 		err = bt_mesh_health_cli_attention_set(&health_cli, &ctx, cp->attention,
3661 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
3662 						       bt_mesh_op_agg_cli_seq_is_started() ?
3663 						       NULL :
3664 #endif
3665 						       &updated_attention);
3666 	} else {
3667 		err = bt_mesh_health_cli_attention_set_unack(&health_cli, &ctx, cp->attention);
3668 	}
3669 
3670 	if (err) {
3671 		LOG_ERR("err %d", err);
3672 		return BTP_STATUS_FAILED;
3673 	}
3674 
3675 	if (cp->ack) {
3676 		struct btp_mesh_health_attention_set_rp *rp = rsp;
3677 
3678 		rp->attention = updated_attention;
3679 
3680 		*rsp_len = sizeof(*rp);
3681 	}
3682 
3683 	return BTP_STATUS_SUCCESS;
3684 }
3685 
3686 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
opcodes_aggregator_init(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3687 static uint8_t opcodes_aggregator_init(const void *cmd, uint16_t cmd_len,
3688 				       void *rsp, uint16_t *rsp_len)
3689 {
3690 	const struct btp_mesh_opcodes_aggregator_init_cmd *cp = cmd;
3691 	int err;
3692 
3693 	LOG_DBG("");
3694 
3695 	err = bt_mesh_op_agg_cli_seq_start(cp->net_idx, cp->app_idx, cp->dst, cp->elem_addr);
3696 	if (err) {
3697 		LOG_ERR("Failed to init Opcodes Aggregator Context (err %d)", err);
3698 		return BTP_STATUS_FAILED;
3699 	}
3700 
3701 	return BTP_STATUS_SUCCESS;
3702 }
3703 
opcodes_aggregator_send(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3704 static uint8_t opcodes_aggregator_send(const void *cmd, uint16_t cmd_len,
3705 				       void *rsp, uint16_t *rsp_len)
3706 {
3707 	int err;
3708 
3709 	LOG_DBG("");
3710 
3711 	err = bt_mesh_op_agg_cli_seq_send();
3712 	if (err) {
3713 		LOG_ERR("Failed to send Opcodes Aggregator message (err %d)", err);
3714 		return BTP_STATUS_FAILED;
3715 	}
3716 
3717 	return BTP_STATUS_SUCCESS;
3718 }
3719 #endif
3720 
3721 #if defined(CONFIG_BT_MESH_RPR_CLI)
rpr_scan_start(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3722 static uint8_t rpr_scan_start(const void *cmd, uint16_t cmd_len,
3723 			      void *rsp, uint16_t *rsp_len)
3724 {
3725 	const struct btp_rpr_scan_start_cmd *cp = cmd;
3726 
3727 	struct bt_mesh_rpr_scan_status status;
3728 	const struct bt_mesh_rpr_node srv = {
3729 		.addr = cp->dst,
3730 		.net_idx = net.net_idx,
3731 		.ttl = BT_MESH_TTL_DEFAULT,
3732 	};
3733 	uint8_t uuid[16] = {0};
3734 	int err;
3735 
3736 	err = bt_mesh_rpr_scan_start(&rpr_cli, &srv,
3737 				     memcmp(uuid, cp->uuid, 16) ? cp->uuid : NULL,
3738 				     cp->timeout,
3739 				     BT_MESH_RPR_SCAN_MAX_DEVS_ANY, &status);
3740 
3741 	if (err) {
3742 		LOG_ERR("Scan start failed: %d", err);
3743 		return BTP_STATUS_FAILED;
3744 	}
3745 
3746 	return BTP_STATUS_SUCCESS;
3747 }
3748 
rpr_ext_scan_start(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3749 static uint8_t rpr_ext_scan_start(const void *cmd, uint16_t cmd_len,
3750 				  void *rsp, uint16_t *rsp_len)
3751 {
3752 	const struct btp_rpr_ext_scan_start_cmd *cp = cmd;
3753 	const struct bt_mesh_rpr_node srv = {
3754 		.addr = cp->dst,
3755 		.net_idx = net.net_idx,
3756 		.ttl = BT_MESH_TTL_DEFAULT,
3757 	};
3758 	int err;
3759 
3760 	err = bt_mesh_rpr_scan_start_ext(&rpr_cli, &srv, cp->uuid,
3761 					 cp->timeout, cp->ad_types,
3762 					 cp->ad_count);
3763 	if (err) {
3764 		LOG_ERR("Scan start failed: %d", err);
3765 		return BTP_STATUS_FAILED;
3766 	}
3767 
3768 	return BTP_STATUS_SUCCESS;
3769 }
3770 
rpr_scan_caps_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3771 static uint8_t rpr_scan_caps_get(const void *cmd, uint16_t cmd_len,
3772 				 void *rsp, uint16_t *rsp_len)
3773 {
3774 	const struct btp_rpr_scan_caps_get_cmd *cp = cmd;
3775 	struct bt_mesh_rpr_caps caps;
3776 	const struct bt_mesh_rpr_node srv = {
3777 		.addr = cp->dst,
3778 		.net_idx = net.net_idx,
3779 		.ttl = BT_MESH_TTL_DEFAULT,
3780 	};
3781 	int err;
3782 
3783 	err = bt_mesh_rpr_scan_caps_get(&rpr_cli, &srv, &caps);
3784 	if (err) {
3785 		LOG_ERR("Scan capabilities get failed: %d", err);
3786 		return BTP_STATUS_FAILED;
3787 	}
3788 
3789 	LOG_DBG("Remote Provisioning scan capabilities of 0x%04x:",
3790 		net.dst);
3791 	LOG_DBG("\tMax devices:     %u", caps.max_devs);
3792 	LOG_DBG("\tActive scanning: %s",
3793 		    caps.active_scan ? "true" : "false");
3794 
3795 	return BTP_STATUS_SUCCESS;
3796 }
3797 
rpr_scan_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3798 static uint8_t rpr_scan_get(const void *cmd, uint16_t cmd_len,
3799 			    void *rsp, uint16_t *rsp_len)
3800 {
3801 	const struct btp_rpr_scan_get_cmd *cp = cmd;
3802 	struct bt_mesh_rpr_scan_status status;
3803 	const struct bt_mesh_rpr_node srv = {
3804 		.addr = cp->dst,
3805 		.net_idx = net.net_idx,
3806 		.ttl = BT_MESH_TTL_DEFAULT,
3807 	};
3808 	int err;
3809 
3810 	err = bt_mesh_rpr_scan_get(&rpr_cli, &srv, &status);
3811 	if (err) {
3812 		LOG_ERR("Scan get failed: %d", err);
3813 		return BTP_STATUS_FAILED;
3814 	}
3815 
3816 	LOG_DBG("Remote Provisioning scan on 0x%04x:", cp->dst);
3817 	LOG_DBG("\tStatus:         %u", status.status);
3818 	LOG_DBG("\tScan type:      %u", status.scan);
3819 	LOG_DBG("\tMax devices:    %u", status.max_devs);
3820 	LOG_DBG("\tRemaining time: %u", status.timeout);
3821 
3822 	return BTP_STATUS_SUCCESS;
3823 }
3824 
rpr_scan_stop(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3825 static uint8_t rpr_scan_stop(const void *cmd, uint16_t cmd_len,
3826 			     void *rsp, uint16_t *rsp_len)
3827 {
3828 	const struct btp_rpr_scan_stop_cmd *cp = cmd;
3829 	struct bt_mesh_rpr_scan_status status;
3830 	const struct bt_mesh_rpr_node srv = {
3831 		.addr = cp->dst,
3832 		.net_idx = net.net_idx,
3833 		.ttl = BT_MESH_TTL_DEFAULT,
3834 	};
3835 	int err;
3836 
3837 	err = bt_mesh_rpr_scan_stop(&rpr_cli, &srv, &status);
3838 	if (err || status.status) {
3839 		LOG_DBG("Scan stop failed: %d %u", err, status.status);
3840 		return BTP_STATUS_FAILED;
3841 	}
3842 
3843 	LOG_DBG("Remote Provisioning scan on 0x%04x stopped.",
3844 		    net.dst);
3845 
3846 	return BTP_STATUS_SUCCESS;
3847 }
3848 
rpr_link_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3849 static uint8_t rpr_link_get(const void *cmd, uint16_t cmd_len,
3850 			    void *rsp, uint16_t *rsp_len)
3851 {
3852 	const struct btp_rpr_link_get_cmd *cp = cmd;
3853 	struct bt_mesh_rpr_link link;
3854 	const struct bt_mesh_rpr_node srv = {
3855 		.addr = cp->dst,
3856 		.net_idx = net.net_idx,
3857 		.ttl = BT_MESH_TTL_DEFAULT,
3858 	};
3859 	int err;
3860 
3861 	err = bt_mesh_rpr_link_get(&rpr_cli, &srv, &link);
3862 	if (err) {
3863 		LOG_ERR("Link get failed: %d %u", err, link.status);
3864 		return BTP_STATUS_FAILED;
3865 	}
3866 
3867 	LOG_DBG("Remote Provisioning Link on 0x%04x:", cp->dst);
3868 	LOG_DBG("\tStatus: %u", link.status);
3869 	LOG_DBG("\tState:  %u", link.state);
3870 
3871 	return BTP_STATUS_SUCCESS;
3872 }
3873 
rpr_link_close(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3874 static uint8_t rpr_link_close(const void *cmd, uint16_t cmd_len,
3875 			      void *rsp, uint16_t *rsp_len)
3876 {
3877 	const struct btp_rpr_link_close_cmd *cp = cmd;
3878 	struct bt_mesh_rpr_link link;
3879 	const struct bt_mesh_rpr_node srv = {
3880 		.addr = cp->dst,
3881 		.net_idx = net.net_idx,
3882 		.ttl = BT_MESH_TTL_DEFAULT,
3883 	};
3884 	int err;
3885 
3886 	err = bt_mesh_rpr_link_close(&rpr_cli, &srv, &link);
3887 	if (err) {
3888 		LOG_ERR("Link close failed: %d %u", err, link.status);
3889 		return BTP_STATUS_FAILED;
3890 	}
3891 
3892 	LOG_DBG("Remote Provisioning Link on 0x%04x:", cp->dst);
3893 	LOG_DBG("\tStatus: %u", link.status);
3894 	LOG_DBG("\tState:  %u", link.state);
3895 
3896 	return BTP_STATUS_SUCCESS;
3897 }
3898 
rpr_prov_remote(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3899 static uint8_t rpr_prov_remote(const void *cmd, uint16_t cmd_len,
3900 			       void *rsp, uint16_t *rsp_len)
3901 {
3902 	const struct btp_rpr_prov_remote_cmd *cp = cmd;
3903 	struct bt_mesh_rpr_node srv = {
3904 		.addr = cp->dst,
3905 		.net_idx = net.net_idx,
3906 		.ttl = BT_MESH_TTL_DEFAULT,
3907 	};
3908 	int err;
3909 
3910 	err = bt_mesh_provision_remote(&rpr_cli, &srv, cp->uuid,
3911 				       cp->net_idx, cp->addr);
3912 	if (err) {
3913 		LOG_ERR("Prov remote start failed: %d", err);
3914 		return BTP_STATUS_FAILED;
3915 	}
3916 
3917 	return BTP_STATUS_SUCCESS;
3918 }
3919 
rpr_reprov_remote(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)3920 static uint8_t rpr_reprov_remote(const void *cmd, uint16_t cmd_len,
3921 				 void *rsp, uint16_t *rsp_len)
3922 {
3923 	const struct btp_rpr_reprov_remote_cmd *cp = cmd;
3924 	struct bt_mesh_rpr_node srv = {
3925 		.addr = cp->dst,
3926 		.net_idx = net.net_idx,
3927 		.ttl = BT_MESH_TTL_DEFAULT,
3928 	};
3929 	int err;
3930 
3931 	if (!BT_MESH_ADDR_IS_UNICAST(cp->addr)) {
3932 		LOG_ERR("Must be a valid unicast address");
3933 		err = -EINVAL;
3934 		return BTP_STATUS_FAILED;
3935 	}
3936 
3937 	err = bt_mesh_reprovision_remote(&rpr_cli, &srv, cp->addr,
3938 					 cp->comp_change);
3939 	if (err) {
3940 		LOG_ERR("Reprovisioning failed: %d", err);
3941 		return BTP_STATUS_FAILED;
3942 	}
3943 
3944 	return BTP_STATUS_SUCCESS;
3945 }
3946 #endif
3947 
3948 #if defined(CONFIG_BT_MESH_DFD_SRV)
3949 static struct {
3950 	struct bt_mesh_dfu_target targets[32];
3951 	struct bt_mesh_blob_target_pull pull[32];
3952 	size_t target_cnt;
3953 	struct bt_mesh_blob_cli_inputs inputs;
3954 } dfu_tx;
3955 
dfu_tx_prepare(void)3956 static void dfu_tx_prepare(void)
3957 {
3958 	sys_slist_init(&dfu_tx.inputs.targets);
3959 
3960 	for (int i = 0; i < dfu_tx.target_cnt; i++) {
3961 		/* Reset target context. */
3962 		uint16_t addr = dfu_tx.targets[i].blob.addr;
3963 
3964 		memset(&dfu_tx.targets[i].blob, 0,
3965 		       sizeof(struct bt_mesh_blob_target));
3966 		memset(&dfu_tx.pull[i], 0,
3967 		       sizeof(struct bt_mesh_blob_target_pull));
3968 		dfu_tx.targets[i].blob.addr = addr;
3969 		dfu_tx.targets[i].blob.pull = &dfu_tx.pull[i];
3970 
3971 		sys_slist_append(&dfu_tx.inputs.targets,
3972 				 &dfu_tx.targets[i].blob.n);
3973 	}
3974 }
3975 
dfu_target(uint8_t img_idx,uint16_t addr)3976 static void dfu_target(uint8_t img_idx, uint16_t addr)
3977 {
3978 	if (dfu_tx.target_cnt == ARRAY_SIZE(dfu_tx.targets)) {
3979 		LOG_ERR("No room.");
3980 		return;
3981 	}
3982 
3983 	for (int i = 0; i < dfu_tx.target_cnt; i++) {
3984 		if (dfu_tx.targets[i].blob.addr == addr) {
3985 			LOG_ERR("Target 0x%04x already exists", addr);
3986 			return;
3987 		}
3988 	}
3989 
3990 	dfu_tx.targets[dfu_tx.target_cnt].blob.addr = addr;
3991 	dfu_tx.targets[dfu_tx.target_cnt].img_idx = img_idx;
3992 	sys_slist_append(&dfu_tx.inputs.targets,
3993 			 &dfu_tx.targets[dfu_tx.target_cnt].blob.n);
3994 	dfu_tx.target_cnt++;
3995 
3996 	LOG_DBG("Added target 0x%04x", addr);
3997 }
dfu_slot_add(size_t size,uint8_t * fwid,size_t fwid_len,uint8_t * metadata,size_t metadata_len)3998 static void dfu_slot_add(size_t size, uint8_t *fwid, size_t fwid_len,
3999 			 uint8_t *metadata, size_t metadata_len)
4000 {
4001 	struct bt_mesh_dfu_slot *slot;
4002 	int err;
4003 
4004 	slot = bt_mesh_dfu_slot_reserve();
4005 	err = bt_mesh_dfu_slot_info_set(slot, size, metadata, metadata_len);
4006 	if (err) {
4007 		LOG_ERR("Failed to set slot info: %d", err);
4008 		return;
4009 	}
4010 
4011 	err = bt_mesh_dfu_slot_fwid_set(slot, fwid, fwid_len);
4012 	if (err) {
4013 		LOG_ERR("Failed to set slot fwid: %d", err);
4014 		return;
4015 	}
4016 
4017 	err = bt_mesh_dfu_slot_commit(slot);
4018 	if (err) {
4019 		LOG_ERR("Failed to commit slot: %d", err);
4020 		return;
4021 	}
4022 
4023 	LOG_DBG("Slot added.");
4024 }
dfu_img_cb(struct bt_mesh_dfu_cli * cli,struct bt_mesh_msg_ctx * ctx,uint8_t idx,uint8_t total,const struct bt_mesh_dfu_img * img,void * cb_data)4025 static enum bt_mesh_dfu_iter dfu_img_cb(struct bt_mesh_dfu_cli *cli,
4026 					struct bt_mesh_msg_ctx *ctx,
4027 					uint8_t idx, uint8_t total,
4028 					const struct bt_mesh_dfu_img *img,
4029 					void *cb_data)
4030 {
4031 	char fwid[2 * CONFIG_BT_MESH_DFU_FWID_MAXLEN + 1];
4032 	size_t len;
4033 
4034 	idx = 0xff;
4035 
4036 	if (img->fwid_len <= sizeof(fwid)) {
4037 		len = bin2hex(img->fwid, img->fwid_len, fwid, sizeof(fwid));
4038 	} else {
4039 		LOG_ERR("FWID is too big");
4040 		return BT_MESH_DFU_ITER_STOP;
4041 	}
4042 
4043 	fwid[len] = '\0';
4044 
4045 	LOG_DBG("Image %u:", idx);
4046 	LOG_DBG("\tFWID: ");
4047 	if (img->uri) {
4048 		LOG_DBG("\tURI:  ");
4049 	}
4050 
4051 	return BT_MESH_DFU_ITER_CONTINUE;
4052 }
4053 
dfu_info_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4054 static uint8_t dfu_info_get(const void *cmd, uint16_t cmd_len,
4055 			    void *rsp, uint16_t *rsp_len)
4056 {
4057 	const struct btp_mmdl_dfu_info_get_cmd *cp = cmd;
4058 	struct model_data *model_bound;
4059 	struct bt_mesh_msg_ctx ctx = {
4060 		.net_idx = net.net_idx,
4061 		.send_ttl = BT_MESH_TTL_DEFAULT,
4062 	};
4063 	uint8_t max_count;
4064 	int err = 0;
4065 
4066 	LOG_DBG("");
4067 
4068 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4069 	if (!model_bound) {
4070 		LOG_ERR("Model not found");
4071 		return BTP_STATUS_FAILED;
4072 	}
4073 	ctx.addr = model_bound->addr;
4074 	ctx.app_idx = model_bound->appkey_idx;
4075 
4076 	max_count = cp->limit;
4077 
4078 	err = bt_mesh_dfu_cli_imgs_get(&dfd_srv.dfu, &ctx, dfu_img_cb, NULL,
4079 				       max_count);
4080 	if (err) {
4081 		return BTP_STATUS_FAILED;
4082 	}
4083 
4084 	return BTP_STATUS_SUCCESS;
4085 }
4086 
dfu_update_metadata_check(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4087 static uint8_t dfu_update_metadata_check(const void *cmd, uint16_t cmd_len,
4088 					 void *rsp, uint16_t *rsp_len)
4089 {
4090 	const struct btp_mmdl_dfu_metadata_check_cmd *cp = cmd;
4091 	struct btp_mmdl_dfu_metadata_check_rp *rp = rsp;
4092 	const struct bt_mesh_dfu_slot *slot;
4093 	struct model_data *model_bound;
4094 	struct bt_mesh_msg_ctx ctx = {
4095 		.net_idx = net.net_idx,
4096 		.send_ttl = BT_MESH_TTL_DEFAULT,
4097 	};
4098 	struct bt_mesh_dfu_metadata_status rsp_data;
4099 	uint8_t img_idx, slot_idx;
4100 	size_t size;
4101 	size_t fwid_len;
4102 	size_t metadata_len;
4103 	uint8_t fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN];
4104 	uint8_t metadata[CONFIG_BT_MESH_DFU_METADATA_MAXLEN];
4105 	int err;
4106 
4107 	LOG_DBG("");
4108 
4109 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4110 	if (!model_bound) {
4111 		LOG_ERR("Model not found");
4112 		return BTP_STATUS_FAILED;
4113 	}
4114 
4115 	ctx.addr = model_bound->addr;
4116 	ctx.app_idx = model_bound->appkey_idx;
4117 	img_idx = cp->index;
4118 	slot_idx = cp->slot_idx;
4119 	size = cp->slot_size;
4120 	fwid_len = cp->fwid_len;
4121 	metadata_len = cp->metadata_len;
4122 
4123 	if ((metadata_len > 0) &&
4124 		(metadata_len < CONFIG_BT_MESH_DFU_METADATA_MAXLEN)) {
4125 		memcpy(&metadata, cp->data, metadata_len);
4126 	}
4127 
4128 	dfu_slot_add(size, fwid, fwid_len, metadata, metadata_len);
4129 
4130 	slot = bt_mesh_dfu_slot_at(slot_idx);
4131 	if (!slot) {
4132 		LOG_ERR("No image in slot %u", slot_idx);
4133 		return BTP_STATUS_FAILED;
4134 	}
4135 
4136 	err = bt_mesh_dfu_cli_metadata_check(&dfd_srv.dfu, &ctx, img_idx, slot,
4137 					     &rsp_data);
4138 
4139 	if (err) {
4140 		LOG_ERR("ERR %d", err);
4141 		return BTP_STATUS_FAILED;
4142 	}
4143 
4144 	rp->idx = rsp_data.idx;
4145 	rp->status = rsp_data.status;
4146 	rp->effect = rsp_data.effect;
4147 
4148 	*rsp_len = sizeof(struct btp_mmdl_dfu_metadata_check_rp);
4149 
4150 	return BTP_STATUS_SUCCESS;
4151 }
4152 
dfu_firmware_update_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4153 static uint8_t dfu_firmware_update_get(const void *cmd, uint16_t cmd_len,
4154 				       void *rsp, uint16_t *rsp_len)
4155 {
4156 	struct model_data *model_bound;
4157 	struct bt_mesh_msg_ctx ctx = {
4158 		.net_idx = net.net_idx,
4159 		.send_ttl = BT_MESH_TTL_DEFAULT,
4160 	};
4161 	struct bt_mesh_dfu_target_status rsp_data;
4162 	struct btp_mmdl_dfu_firmware_update_rp *rp = rsp;
4163 	int err;
4164 
4165 	LOG_DBG("");
4166 
4167 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4168 	if (!model_bound) {
4169 		LOG_ERR("Model not found");
4170 		return BTP_STATUS_FAILED;
4171 	}
4172 
4173 	ctx.addr = model_bound->addr;
4174 	ctx.app_idx = model_bound->appkey_idx;
4175 
4176 	err = bt_mesh_dfu_cli_status_get(&dfd_srv.dfu, &ctx, &rsp_data);
4177 	if (err) {
4178 		LOG_ERR("err %d", err);
4179 		return BTP_STATUS_FAILED;
4180 	}
4181 
4182 	rp->status = rsp_data.status;
4183 	*rsp_len = sizeof(struct btp_mmdl_dfu_firmware_update_rp);
4184 	return BTP_STATUS_SUCCESS;
4185 }
4186 
dfu_firmware_update_cancel(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4187 static uint8_t dfu_firmware_update_cancel(const void *cmd, uint16_t cmd_len,
4188 					  void *rsp, uint16_t *rsp_len)
4189 {
4190 	struct model_data *model_bound;
4191 	struct bt_mesh_msg_ctx ctx = {
4192 		.net_idx = net.net_idx,
4193 		.send_ttl = BT_MESH_TTL_DEFAULT,
4194 	};
4195 	int err;
4196 
4197 	LOG_DBG("");
4198 
4199 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4200 	if (!model_bound) {
4201 		LOG_ERR("Model not found");
4202 		return BTP_STATUS_FAILED;
4203 	}
4204 
4205 	ctx.addr = model_bound->addr;
4206 	ctx.app_idx = model_bound->appkey_idx;
4207 
4208 	err = bt_mesh_dfu_cli_cancel(&dfd_srv.dfu, &ctx);
4209 	if (err) {
4210 		LOG_ERR("err %d", err);
4211 		return BTP_STATUS_FAILED;
4212 	}
4213 
4214 	return BTP_STATUS_SUCCESS;
4215 }
4216 
dfu_firmware_update_start(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4217 static uint8_t dfu_firmware_update_start(const void *cmd, uint16_t cmd_len,
4218 					 void *rsp, uint16_t *rsp_len)
4219 {
4220 	const struct btp_mmdl_dfu_firmware_update_cmd *cp = cmd;
4221 	struct model_data *model_bound;
4222 	struct bt_mesh_dfu_cli_xfer xfer;
4223 	uint8_t addr_cnt;
4224 	uint16_t addr = BT_MESH_ADDR_UNASSIGNED;
4225 	uint8_t slot_idx;
4226 	size_t size;
4227 	size_t fwid_len;
4228 	size_t metadata_len;
4229 	uint8_t fwid[CONFIG_BT_MESH_DFU_FWID_MAXLEN];
4230 	uint8_t metadata[CONFIG_BT_MESH_DFU_METADATA_MAXLEN];
4231 	int err = 0;
4232 	int i = 0;
4233 
4234 	LOG_DBG("");
4235 
4236 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4237 	if (!model_bound) {
4238 		LOG_ERR("Model not found");
4239 		return BTP_STATUS_FAILED;
4240 	}
4241 
4242 	struct bt_mesh_dfu_cli_xfer_blob_params blob = {
4243 		.block_size_log = cp->block_size,
4244 		.chunk_size = cp->chunk_size,
4245 	};
4246 
4247 	addr_cnt = cp->addr_cnt;
4248 	slot_idx = cp->slot_idx;
4249 	size = cp->slot_size;
4250 	fwid_len = cp->fwid_len;
4251 	metadata_len = cp->metadata_len;
4252 	xfer.mode = BT_MESH_BLOB_XFER_MODE_PUSH;
4253 	xfer.blob_params = &blob;
4254 
4255 	if ((metadata_len > 0) &&
4256 		(metadata_len < CONFIG_BT_MESH_DFU_METADATA_MAXLEN)) {
4257 		memcpy(&metadata, cp->data, metadata_len);
4258 	}
4259 
4260 	bt_mesh_dfu_slot_del_all();
4261 
4262 	dfu_slot_add(size, fwid, fwid_len, metadata, metadata_len);
4263 
4264 	xfer.slot = bt_mesh_dfu_slot_at(slot_idx);
4265 	if (!xfer.slot) {
4266 		LOG_ERR("No image in slot %u", slot_idx);
4267 		return BTP_STATUS_FAILED;
4268 	}
4269 
4270 	for (i = 0; i < addr_cnt; i++) {
4271 		addr = cp->data[metadata_len + 1 + i * sizeof(uint16_t)] |
4272 			(cp->data[metadata_len + i * sizeof(uint16_t)] << 8);
4273 		dfu_target(slot_idx, addr);
4274 	}
4275 
4276 	dfu_tx_prepare();
4277 
4278 	if (!dfu_tx.target_cnt) {
4279 		LOG_ERR("No targets.");
4280 		return BTP_STATUS_FAILED;
4281 	}
4282 
4283 	if (addr_cnt > 1) {
4284 		dfu_tx.inputs.group = BT_MESH_ADDR_UNASSIGNED;
4285 	} else {
4286 		dfu_tx.inputs.group = addr;
4287 	}
4288 
4289 	dfu_tx.inputs.app_idx = model_bound->appkey_idx;
4290 	dfu_tx.inputs.ttl = BT_MESH_TTL_DEFAULT;
4291 
4292 	err = bt_mesh_dfu_cli_send(&dfd_srv.dfu, &dfu_tx.inputs, &dummy_blob_io, &xfer);
4293 
4294 	if (err) {
4295 		LOG_ERR("err %d", err);
4296 		return BTP_STATUS_FAILED;
4297 	}
4298 
4299 	return BTP_STATUS_SUCCESS;
4300 }
4301 
dfu_firmware_update_apply(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4302 static uint8_t dfu_firmware_update_apply(const void *cmd, uint16_t cmd_len,
4303 					 void *rsp, uint16_t *rsp_len)
4304 {
4305 	struct model_data *model_bound;
4306 	int err;
4307 
4308 	LOG_DBG("");
4309 
4310 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_DFU_CLI);
4311 	if (!model_bound) {
4312 		LOG_ERR("Model not found");
4313 		return BTP_STATUS_FAILED;
4314 	}
4315 
4316 	err = bt_mesh_dfu_cli_apply(&dfd_srv.dfu);
4317 	if (err) {
4318 		LOG_ERR("err %d", err);
4319 		return BTP_STATUS_FAILED;
4320 	}
4321 
4322 	return BTP_STATUS_SUCCESS;
4323 }
4324 #endif
4325 
4326 #if defined(CONFIG_BT_MESH_BLOB_CLI) && !defined(CONFIG_BT_MESH_DFD_SRV)
blob_cli_inputs_prepare(uint16_t group,uint16_t app_idx)4327 static void blob_cli_inputs_prepare(uint16_t group, uint16_t app_idx)
4328 {
4329 	int i;
4330 
4331 	blob_cli_xfer.inputs.ttl = BT_MESH_TTL_DEFAULT;
4332 	blob_cli_xfer.inputs.group = group;
4333 	blob_cli_xfer.inputs.app_idx = app_idx;
4334 	sys_slist_init(&blob_cli_xfer.inputs.targets);
4335 
4336 	for (i = 0; i < blob_cli_xfer.target_count; ++i) {
4337 		/* Reset target context. */
4338 		uint16_t addr = blob_cli_xfer.targets[i].addr;
4339 
4340 		memset(&blob_cli_xfer.targets[i], 0,
4341 		       sizeof(struct bt_mesh_blob_target));
4342 		memset(&blob_cli_xfer.pull[i], 0,
4343 		       sizeof(struct bt_mesh_blob_target_pull));
4344 		blob_cli_xfer.targets[i].addr = addr;
4345 		blob_cli_xfer.targets[i].pull = &blob_cli_xfer.pull[i];
4346 
4347 		sys_slist_append(&blob_cli_xfer.inputs.targets,
4348 				 &blob_cli_xfer.targets[i].n);
4349 	}
4350 }
4351 
cmd_blob_target(uint16_t addr)4352 static int cmd_blob_target(uint16_t addr)
4353 {
4354 	struct bt_mesh_blob_target *t;
4355 
4356 	if (blob_cli_xfer.target_count == ARRAY_SIZE(blob_cli_xfer.targets)) {
4357 		LOG_ERR("No more room");
4358 		return 0;
4359 	}
4360 
4361 	t = &blob_cli_xfer.targets[blob_cli_xfer.target_count];
4362 
4363 	t->addr = addr;
4364 
4365 	LOG_DBG("Added target 0x%04x", t->addr);
4366 
4367 	blob_cli_xfer.target_count++;
4368 	return 0;
4369 }
4370 
blob_info_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4371 static uint8_t blob_info_get(const void *cmd, uint16_t cmd_len,
4372 			     void *rsp, uint16_t *rsp_len)
4373 {
4374 	const struct btp_mmdl_blob_info_get_cmd *cp = cmd;
4375 	struct model_data *model_bound;
4376 	uint16_t addr = BT_MESH_ADDR_UNASSIGNED;
4377 	uint16_t group = BT_MESH_ADDR_UNASSIGNED;
4378 	int err;
4379 
4380 	LOG_DBG("");
4381 
4382 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_BLOB_CLI);
4383 	if (!model_bound) {
4384 		LOG_ERR("Model not found");
4385 		return BTP_STATUS_FAILED;
4386 	}
4387 
4388 	for (int i = 0; i < cp->addr_cnt; i++) {
4389 		addr = cp->addr[1 + i * sizeof(uint16_t)] |
4390 			(cp->addr[i * sizeof(uint16_t)] << 8);
4391 		err = cmd_blob_target(addr);
4392 		if (err) {
4393 			LOG_ERR("err target %d", err);
4394 			return BTP_STATUS_FAILED;
4395 		}
4396 	}
4397 
4398 	if (cp->addr_cnt > 1) {
4399 		group = BT_MESH_ADDR_UNASSIGNED;
4400 	} else {
4401 		group = addr;
4402 	}
4403 
4404 	if (!blob_cli_xfer.target_count) {
4405 		LOG_ERR("Failed: No targets");
4406 		return BTP_STATUS_FAILED;
4407 	}
4408 
4409 	blob_cli_inputs_prepare(group, model_bound->appkey_idx);
4410 
4411 	err = bt_mesh_blob_cli_caps_get(&blob_cli, &blob_cli_xfer.inputs);
4412 
4413 	if (err) {
4414 		return BTP_STATUS_FAILED;
4415 	}
4416 
4417 	return BTP_STATUS_SUCCESS;
4418 }
4419 
blob_transfer_start(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4420 static uint8_t blob_transfer_start(const void *cmd, uint16_t cmd_len,
4421 				   void *rsp, uint16_t *rsp_len)
4422 {
4423 	const struct btp_mmdl_blob_transfer_start_cmd *cp = cmd;
4424 	struct model_data *model_bound;
4425 	int err = 0;
4426 
4427 	LOG_DBG("");
4428 
4429 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_BLOB_CLI);
4430 	if (!model_bound) {
4431 		LOG_ERR("Model not found");
4432 		return BTP_STATUS_FAILED;
4433 	}
4434 
4435 	if (!blob_cli_xfer.target_count) {
4436 		LOG_ERR("Failed: No targets");
4437 		return BTP_STATUS_FAILED;
4438 	}
4439 	blob_cli_xfer.xfer.id = cp->id;
4440 	blob_cli_xfer.xfer.size = cp->size;
4441 	blob_cli_xfer.xfer.block_size_log = cp->block_size;
4442 	blob_cli_xfer.xfer.chunk_size = cp->chunk_size;
4443 
4444 	if (blob_cli.caps.modes) {
4445 		blob_cli_xfer.xfer.mode = blob_cli.caps.modes;
4446 	} else {
4447 		blob_cli_xfer.xfer.mode = BT_MESH_BLOB_XFER_MODE_PUSH;
4448 	}
4449 
4450 	if (cp->timeout) {
4451 		blob_cli_xfer.inputs.timeout_base = cp->timeout;
4452 	}
4453 
4454 	if (cp->ttl) {
4455 		blob_cli_xfer.inputs.ttl = cp->ttl;
4456 	}
4457 
4458 	err = bt_mesh_blob_cli_send(&blob_cli, &blob_cli_xfer.inputs,
4459 				    &blob_cli_xfer.xfer, &dummy_blob_io);
4460 
4461 	if (err) {
4462 		return BTP_STATUS_FAILED;
4463 	}
4464 
4465 	return BTP_STATUS_SUCCESS;
4466 }
4467 
blob_transfer_cancel(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4468 static uint8_t blob_transfer_cancel(const void *cmd, uint16_t cmd_len,
4469 				    void *rsp, uint16_t *rsp_len)
4470 {
4471 	LOG_DBG("");
4472 
4473 	bt_mesh_blob_cli_cancel(&blob_cli);
4474 
4475 	return BTP_STATUS_SUCCESS;
4476 }
4477 
blob_transfer_get(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4478 static uint8_t blob_transfer_get(const void *cmd, uint16_t cmd_len,
4479 				 void *rsp, uint16_t *rsp_len)
4480 {
4481 	struct model_data *model_bound;
4482 	uint16_t group;
4483 	int err;
4484 
4485 	LOG_DBG("");
4486 
4487 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_BLOB_CLI);
4488 	if (!model_bound) {
4489 		LOG_ERR("Model not found");
4490 		return BTP_STATUS_FAILED;
4491 	}
4492 
4493 	group = model_bound->addr;
4494 
4495 	err = cmd_blob_target(group);
4496 	if (err) {
4497 		LOG_ERR("err target %d", err);
4498 		return BTP_STATUS_FAILED;
4499 	}
4500 
4501 	if (!blob_cli_xfer.target_count) {
4502 		LOG_ERR("Failed: No targets");
4503 		return BTP_STATUS_FAILED;
4504 	}
4505 
4506 	blob_cli_inputs_prepare(group, model_bound->appkey_idx);
4507 
4508 	err = bt_mesh_blob_cli_xfer_progress_get(&blob_cli, &blob_cli_xfer.inputs);
4509 
4510 	if (err) {
4511 		LOG_ERR("ERR %d", err);
4512 		return BTP_STATUS_FAILED;
4513 	}
4514 
4515 	return BTP_STATUS_SUCCESS;
4516 }
4517 #endif /* CONFIG_BT_MESH_BLOB_CLI */
4518 
4519 #if defined(CONFIG_BT_MESH_BLOB_SRV)
blob_srv_recv(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4520 static uint8_t blob_srv_recv(const void *cmd, uint16_t cmd_len,
4521 			     void *rsp, uint16_t *rsp_len)
4522 {
4523 	const struct btp_mmdl_blob_srv_recv_cmd *cp = cmd;
4524 	struct model_data *model_bound;
4525 	int err;
4526 
4527 #if defined(CONFIG_BT_MESH_DFD_SRV)
4528 	struct bt_mesh_blob_srv *srv = &dfd_srv.upload.blob;
4529 #elif defined(CONFIG_BT_MESH_DFU_SRV)
4530 	struct bt_mesh_blob_srv *srv = &dfu_srv.blob;
4531 #endif
4532 
4533 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_BLOB_SRV);
4534 	if (!model_bound) {
4535 		LOG_ERR("Model not found");
4536 		return BTP_STATUS_FAILED;
4537 	}
4538 
4539 	uint16_t timeout_base;
4540 	uint64_t id;
4541 	uint8_t ttl;
4542 
4543 	LOG_DBG("");
4544 
4545 	id = cp->id;
4546 	timeout_base = cp->timeout;
4547 	ttl = cp->ttl;
4548 
4549 	err = bt_mesh_blob_srv_recv(srv, id, &dummy_blob_io, ttl,
4550 				    timeout_base);
4551 
4552 	if (err) {
4553 		LOG_ERR("ERR %d", err);
4554 		return BTP_STATUS_FAILED;
4555 	}
4556 
4557 	return BTP_STATUS_SUCCESS;
4558 }
4559 
blob_srv_cancel(const void * cmd,uint16_t cmd_len,void * rsp,uint16_t * rsp_len)4560 static uint8_t blob_srv_cancel(const void *cmd, uint16_t cmd_len,
4561 			       void *rsp, uint16_t *rsp_len)
4562 {
4563 	struct model_data *model_bound;
4564 	int err;
4565 
4566 #if defined(CONFIG_BT_MESH_DFU_SRV)
4567 	struct bt_mesh_blob_srv *srv = &dfu_srv.blob;
4568 #elif defined(CONFIG_BT_MESH_DFD_SRV)
4569 	struct bt_mesh_blob_srv *srv = &dfd_srv.upload.blob;
4570 #endif
4571 
4572 	model_bound = lookup_model_bound(BT_MESH_MODEL_ID_BLOB_SRV);
4573 	if (!model_bound) {
4574 		LOG_ERR("Model not found");
4575 		return BTP_STATUS_FAILED;
4576 	}
4577 
4578 	LOG_DBG("");
4579 
4580 	err = bt_mesh_blob_srv_cancel(srv);
4581 
4582 	if (err) {
4583 		LOG_ERR("ERR %d", err);
4584 		return BTP_STATUS_FAILED;
4585 	}
4586 
4587 	return BTP_STATUS_SUCCESS;
4588 }
4589 #endif
4590 
4591 static const struct btp_handler handlers[] = {
4592 	{
4593 		.opcode = BTP_MESH_READ_SUPPORTED_COMMANDS,
4594 		.index = BTP_INDEX_NONE,
4595 		.expect_len = 0,
4596 		.func = supported_commands,
4597 	},
4598 	{
4599 		.opcode = BTP_MESH_CONFIG_PROVISIONING,
4600 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
4601 		.func = config_prov,
4602 	},
4603 	{
4604 		.opcode = BTP_MESH_PROVISION_NODE,
4605 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
4606 		.func = provision_node,
4607 	},
4608 	{
4609 		.opcode = BTP_MESH_INIT,
4610 		.expect_len = sizeof(struct btp_mesh_init_cmd),
4611 		.func = init,
4612 	},
4613 	{
4614 		.opcode = BTP_MESH_RESET,
4615 		.expect_len = 0,
4616 		.func = reset,
4617 	},
4618 	{
4619 		.opcode = BTP_MESH_INPUT_NUMBER,
4620 		.expect_len = sizeof(struct btp_mesh_input_number_cmd),
4621 		.func = input_number,
4622 	},
4623 	{
4624 		.opcode = BTP_MESH_INPUT_STRING,
4625 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
4626 		.func = input_string,
4627 	},
4628 	{
4629 		.opcode = BTP_MESH_IVU_TEST_MODE,
4630 		.expect_len = sizeof(struct btp_mesh_ivu_test_mode_cmd),
4631 		.func = ivu_test_mode,
4632 	},
4633 	{
4634 		.opcode = BTP_MESH_IVU_TOGGLE_STATE,
4635 		.expect_len = 0,
4636 		.func = ivu_toggle_state,
4637 	},
4638 #if defined(CONFIG_BT_MESH_LOW_POWER)
4639 	{
4640 		.opcode = BTP_MESH_LPN,
4641 		.expect_len = sizeof(struct btp_mesh_lpn_set_cmd),
4642 		.func = lpn,
4643 	},
4644 	{
4645 		.opcode = BTP_MESH_LPN_POLL,
4646 		.expect_len = 0,
4647 		.func = lpn_poll,
4648 	},
4649 #endif /* CONFIG_BT_MESH_LOW_POWER */
4650 	{
4651 		.opcode = BTP_MESH_NET_SEND,
4652 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
4653 		.func = net_send,
4654 	},
4655 	{
4656 		.opcode = BTP_MESH_HEALTH_GENERATE_FAULTS,
4657 		.expect_len = 0,
4658 		.func = health_generate_faults,
4659 	},
4660 	{
4661 		.opcode = BTP_MESH_HEALTH_CLEAR_FAULTS,
4662 		.expect_len = 0,
4663 		.func = health_clear_faults,
4664 	},
4665 	{
4666 		.opcode = BTP_MESH_MODEL_SEND,
4667 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
4668 		.func = model_send,
4669 	},
4670 	{
4671 		.opcode = BTP_MESH_COMP_DATA_GET,
4672 		.expect_len = sizeof(struct btp_mesh_comp_data_get_cmd),
4673 		.func = composition_data_get,
4674 	},
4675 	{
4676 		.opcode = BTP_MESH_CFG_BEACON_GET,
4677 		.expect_len = sizeof(struct btp_mesh_cfg_beacon_get_cmd),
4678 		.func = config_beacon_get,
4679 	},
4680 	{
4681 		.opcode = BTP_MESH_CFG_BEACON_SET,
4682 		.expect_len = sizeof(struct btp_mesh_cfg_beacon_set_cmd),
4683 		.func = config_beacon_set,
4684 	},
4685 	{
4686 		.opcode = BTP_MESH_CFG_DEFAULT_TTL_GET,
4687 		.expect_len = sizeof(struct btp_mesh_cfg_default_ttl_get_cmd),
4688 		.func = config_default_ttl_get,
4689 	},
4690 	{
4691 		.opcode = BTP_MESH_CFG_DEFAULT_TTL_SET,
4692 		.expect_len = sizeof(struct btp_mesh_cfg_default_ttl_set_cmd),
4693 		.func = config_default_ttl_set,
4694 	},
4695 	{
4696 		.opcode = BTP_MESH_CFG_GATT_PROXY_GET,
4697 		.expect_len = sizeof(struct btp_mesh_cfg_gatt_proxy_get_cmd),
4698 		.func = config_gatt_proxy_get,
4699 	},
4700 	{
4701 		.opcode = BTP_MESH_CFG_GATT_PROXY_SET,
4702 		.expect_len = sizeof(struct btp_mesh_cfg_gatt_proxy_set_cmd),
4703 		.func = config_gatt_proxy_set,
4704 	},
4705 	{
4706 		.opcode = BTP_MESH_CFG_FRIEND_GET,
4707 		.expect_len = sizeof(struct btp_mesh_cfg_friend_get_cmd),
4708 		.func = config_friend_get,
4709 	},
4710 	{
4711 		.opcode = BTP_MESH_CFG_FRIEND_SET,
4712 		.expect_len = sizeof(struct btp_mesh_cfg_friend_set_cmd),
4713 		.func = config_friend_set,
4714 	},
4715 	{
4716 		.opcode = BTP_MESH_CFG_RELAY_GET,
4717 		.expect_len = sizeof(struct btp_mesh_cfg_relay_get_cmd),
4718 		.func = config_relay_get,
4719 	},
4720 	{
4721 		.opcode = BTP_MESH_CFG_RELAY_SET,
4722 		.expect_len = sizeof(struct btp_mesh_cfg_relay_set_cmd),
4723 		.func = config_relay_set,
4724 	},
4725 	{
4726 		.opcode = BTP_MESH_CFG_MODEL_PUB_GET,
4727 		.expect_len = sizeof(struct btp_mesh_cfg_model_pub_get_cmd),
4728 		.func = config_mod_pub_get,
4729 	},
4730 	{
4731 		.opcode = BTP_MESH_CFG_MODEL_PUB_SET,
4732 		.expect_len = sizeof(struct btp_mesh_cfg_model_pub_set_cmd),
4733 		.func = config_mod_pub_set,
4734 	},
4735 	{
4736 		.opcode = BTP_MESH_CFG_MODEL_SUB_ADD,
4737 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_add_cmd),
4738 		.func = config_mod_sub_add,
4739 	},
4740 	{
4741 		.opcode = BTP_MESH_CFG_MODEL_SUB_DEL,
4742 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_del_cmd),
4743 		.func = config_mod_sub_del,
4744 	},
4745 	{
4746 		.opcode = BTP_MESH_CFG_MODEL_SUB_OVW,
4747 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_ovw_cmd),
4748 		.func = config_mod_sub_ovw,
4749 	},
4750 	{
4751 		.opcode = BTP_MESH_CFG_MODEL_SUB_DEL_ALL,
4752 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_del_all_cmd),
4753 		.func = config_mod_sub_del_all,
4754 	},
4755 	{
4756 		.opcode = BTP_MESH_CFG_MODEL_SUB_GET,
4757 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_get_cmd),
4758 		.func = config_mod_sub_get,
4759 	},
4760 	{
4761 		.opcode = BTP_MESH_CFG_MODEL_SUB_GET_VND,
4762 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_get_vnd_cmd),
4763 		.func = config_mod_sub_get_vnd,
4764 	},
4765 	{
4766 		.opcode = BTP_MESH_CFG_MODEL_SUB_VA_ADD,
4767 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_va_add_cmd),
4768 		.func = config_mod_sub_va_add,
4769 	},
4770 	{
4771 		.opcode = BTP_MESH_CFG_MODEL_SUB_VA_DEL,
4772 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_va_del_cmd),
4773 		.func = config_mod_sub_va_del,
4774 	},
4775 	{
4776 		.opcode = BTP_MESH_CFG_MODEL_SUB_VA_OVW,
4777 		.expect_len = sizeof(struct btp_mesh_cfg_model_sub_va_ovw_cmd),
4778 		.func = config_mod_sub_va_ovw,
4779 	},
4780 	{
4781 		.opcode = BTP_MESH_CFG_NETKEY_ADD,
4782 		.expect_len = sizeof(struct btp_mesh_cfg_netkey_add_cmd),
4783 		.func = config_netkey_add,
4784 	},
4785 	{
4786 		.opcode = BTP_MESH_CFG_NETKEY_GET,
4787 		.expect_len = sizeof(struct btp_mesh_cfg_netkey_get_cmd),
4788 		.func = config_netkey_get,
4789 	},
4790 	{
4791 		.opcode = BTP_MESH_CFG_NETKEY_DEL,
4792 		.expect_len = sizeof(struct btp_mesh_cfg_netkey_del_cmd),
4793 		.func = config_netkey_del,
4794 	},
4795 	{
4796 		.opcode = BTP_MESH_CFG_NETKEY_UPDATE,
4797 		.expect_len = sizeof(struct btp_mesh_cfg_netkey_update_cmd),
4798 		.func = config_netkey_update,
4799 	},
4800 	{
4801 		.opcode = BTP_MESH_CFG_APPKEY_ADD,
4802 		.expect_len = sizeof(struct btp_mesh_cfg_appkey_add_cmd),
4803 		.func = config_appkey_add,
4804 	},
4805 	{
4806 		.opcode = BTP_MESH_CFG_APPKEY_GET,
4807 		.expect_len = sizeof(struct btp_mesh_cfg_appkey_get_cmd),
4808 		.func = config_appkey_get,
4809 	},
4810 	{
4811 		.opcode = BTP_MESH_CFG_APPKEY_DEL,
4812 		.expect_len = sizeof(struct btp_mesh_cfg_appkey_del_cmd),
4813 		.func = config_appkey_del,
4814 	},
4815 	{
4816 		.opcode = BTP_MESH_CFG_APPKEY_UPDATE,
4817 		.expect_len = sizeof(struct btp_mesh_cfg_appkey_update_cmd),
4818 		.func = config_appkey_update,
4819 	},
4820 	{
4821 		.opcode = BTP_MESH_CFG_MODEL_APP_BIND,
4822 		.expect_len = sizeof(struct btp_mesh_cfg_model_app_bind_cmd),
4823 		.func = config_model_app_bind,
4824 	},
4825 	{
4826 		.opcode = BTP_MESH_CFG_MODEL_APP_UNBIND,
4827 		.expect_len = sizeof(struct btp_mesh_cfg_model_app_unbind_cmd),
4828 		.func = config_model_app_unbind,
4829 	},
4830 	{
4831 		.opcode = BTP_MESH_CFG_MODEL_APP_GET,
4832 		.expect_len = sizeof(struct btp_mesh_cfg_model_app_get_cmd),
4833 		.func = config_model_app_get,
4834 	},
4835 	{
4836 		.opcode = BTP_MESH_CFG_MODEL_APP_VND_GET,
4837 		.expect_len = sizeof(struct btp_mesh_cfg_model_app_vnd_get_cmd),
4838 		.func = config_model_app_vnd_get,
4839 	},
4840 	{
4841 		.opcode = BTP_MESH_CFG_HEARTBEAT_PUB_SET,
4842 		.expect_len = sizeof(struct btp_mesh_cfg_heartbeat_pub_set_cmd),
4843 		.func = config_hb_pub_set,
4844 	},
4845 	{
4846 		.opcode = BTP_MESH_CFG_HEARTBEAT_PUB_GET,
4847 		.expect_len = sizeof(struct btp_mesh_cfg_heartbeat_pub_get_cmd),
4848 		.func = config_hb_pub_get,
4849 	},
4850 	{
4851 		.opcode = BTP_MESH_CFG_HEARTBEAT_SUB_SET,
4852 		.expect_len = sizeof(struct btp_mesh_cfg_heartbeat_sub_set_cmd),
4853 		.func = config_hb_sub_set,
4854 	},
4855 	{
4856 		.opcode = BTP_MESH_CFG_HEARTBEAT_SUB_GET,
4857 		.expect_len = sizeof(struct btp_mesh_cfg_heartbeat_sub_get_cmd),
4858 		.func = config_hb_sub_get,
4859 	},
4860 	{
4861 		.opcode = BTP_MESH_CFG_NET_TRANS_GET,
4862 		.expect_len = sizeof(struct btp_mesh_cfg_net_trans_get_cmd),
4863 		.func = config_net_trans_get,
4864 	},
4865 	{
4866 		.opcode = BTP_MESH_CFG_NET_TRANS_SET,
4867 		.expect_len = sizeof(struct btp_mesh_cfg_net_trans_set_cmd),
4868 		.func = config_net_trans_set,
4869 	},
4870 	{
4871 		.opcode = BTP_MESH_CFG_NODE_IDT_SET,
4872 		.expect_len = sizeof(struct btp_mesh_cfg_node_idt_set_cmd),
4873 		.func = config_node_identity_set,
4874 	},
4875 	{
4876 		.opcode = BTP_MESH_CFG_NODE_IDT_GET,
4877 		.expect_len = sizeof(struct btp_mesh_cfg_node_idt_get_cmd),
4878 		.func = config_node_identity_get,
4879 	},
4880 	{
4881 		.opcode = BTP_MESH_CFG_NODE_RESET,
4882 		.expect_len = sizeof(struct btp_mesh_cfg_node_reset_cmd),
4883 		.func = config_node_reset,
4884 	},
4885 	{
4886 		.opcode = BTP_MESH_CFG_LPN_TIMEOUT_GET,
4887 		.expect_len = sizeof(struct btp_mesh_cfg_lpn_timeout_cmd),
4888 		.func = config_lpn_timeout_get,
4889 	},
4890 	{
4891 		.opcode = BTP_MESH_CFG_MODEL_PUB_VA_SET,
4892 		.expect_len = sizeof(struct btp_mesh_cfg_model_pub_va_set_cmd),
4893 		.func = config_mod_pub_va_set,
4894 	},
4895 	{
4896 		.opcode = BTP_MESH_CFG_MODEL_APP_BIND_VND,
4897 		.expect_len = sizeof(struct btp_mesh_cfg_model_app_bind_vnd_cmd),
4898 		.func = config_model_app_bind_vnd,
4899 	},
4900 	{
4901 		.opcode = BTP_MESH_HEALTH_FAULT_GET,
4902 		.expect_len = sizeof(struct btp_mesh_health_fault_get_cmd),
4903 		.func = health_fault_get,
4904 	},
4905 	{
4906 		.opcode = BTP_MESH_HEALTH_FAULT_CLEAR,
4907 		.expect_len = sizeof(struct btp_mesh_health_fault_clear_cmd),
4908 		.func = health_fault_clear,
4909 	},
4910 	{
4911 		.opcode = BTP_MESH_HEALTH_FAULT_TEST,
4912 		.expect_len = sizeof(struct btp_mesh_health_fault_test_cmd),
4913 		.func = health_fault_test,
4914 	},
4915 	{
4916 		.opcode = BTP_MESH_HEALTH_PERIOD_GET,
4917 		.expect_len = sizeof(struct btp_mesh_health_period_get_cmd),
4918 		.func = health_period_get,
4919 	},
4920 	{
4921 		.opcode = BTP_MESH_HEALTH_PERIOD_SET,
4922 		.expect_len = sizeof(struct btp_mesh_health_period_set_cmd),
4923 		.func = health_period_set,
4924 	},
4925 	{
4926 		.opcode = BTP_MESH_HEALTH_ATTENTION_GET,
4927 		.expect_len = sizeof(struct btp_mesh_health_attention_get_cmd),
4928 		.func = health_attention_get,
4929 	},
4930 	{
4931 		.opcode = BTP_MESH_HEALTH_ATTENTION_SET,
4932 		.expect_len = sizeof(struct btp_mesh_health_attention_set_cmd),
4933 		.func = health_attention_set,
4934 	},
4935 	{
4936 		.opcode = BTP_MESH_PROVISION_ADV,
4937 		.expect_len = sizeof(struct btp_mesh_provision_adv_cmd),
4938 		.func = provision_adv,
4939 	},
4940 	{
4941 		.opcode = BTP_MESH_CFG_KRP_GET,
4942 		.expect_len = sizeof(struct btp_mesh_cfg_krp_get_cmd),
4943 		.func = config_krp_get,
4944 	},
4945 	{
4946 		.opcode = BTP_MESH_CFG_KRP_SET,
4947 		.expect_len = sizeof(struct btp_mesh_cfg_krp_set_cmd),
4948 		.func = config_krp_set,
4949 	},
4950 	{
4951 		.opcode = BTP_MESH_VA_ADD,
4952 		.expect_len = sizeof(struct btp_mesh_va_add_cmd),
4953 		.func = va_add,
4954 	},
4955 	{
4956 		.opcode = BTP_MESH_VA_DEL,
4957 		.expect_len = sizeof(struct btp_mesh_va_del_cmd),
4958 		.func = va_del,
4959 	},
4960 #if defined(CONFIG_BT_TESTING)
4961 #if defined(CONFIG_BT_MESH_LOW_POWER)
4962 	{
4963 		.opcode = BTP_MESH_LPN_SUBSCRIBE,
4964 		.expect_len = sizeof(struct btp_mesh_lpn_subscribe_cmd),
4965 		.func = lpn_subscribe,
4966 	},
4967 	{
4968 		.opcode = BTP_MESH_LPN_UNSUBSCRIBE,
4969 		.expect_len = sizeof(struct btp_mesh_lpn_unsubscribe_cmd),
4970 		.func = lpn_unsubscribe,
4971 	},
4972 #endif /* CONFIG_BT_MESH_LOW_POWER */
4973 	{
4974 		.opcode = BTP_MESH_RPL_CLEAR,
4975 		.expect_len = 0,
4976 		.func = rpl_clear,
4977 	},
4978 #endif /* CONFIG_BT_TESTING */
4979 	{
4980 		.opcode = BTP_MESH_PROXY_IDENTITY,
4981 		.expect_len = 0,
4982 		.func = proxy_identity_enable,
4983 	},
4984 #if defined(CONFIG_BT_MESH_PROXY_CLIENT)
4985 	{
4986 		.opcode = BTP_MESH_PROXY_CONNECT,
4987 		.expect_len = sizeof(struct btp_proxy_connect_cmd),
4988 		.func = proxy_connect
4989 	},
4990 #endif
4991 #if defined(CONFIG_BT_MESH_SAR_CFG_CLI)
4992 	{
4993 		.opcode = BTP_MESH_SAR_TRANSMITTER_GET,
4994 		.expect_len = sizeof(struct btp_mesh_sar_transmitter_get_cmd),
4995 		.func = sar_transmitter_get
4996 	},
4997 	{
4998 		.opcode = BTP_MESH_SAR_TRANSMITTER_SET,
4999 		.expect_len = sizeof(struct btp_mesh_sar_transmitter_set_cmd),
5000 		.func = sar_transmitter_set
5001 	},
5002 	{
5003 		.opcode = BTP_MESH_SAR_RECEIVER_GET,
5004 		.expect_len = sizeof(struct btp_mesh_sar_receiver_get_cmd),
5005 		.func = sar_receiver_get
5006 	},
5007 	{
5008 		.opcode = BTP_MESH_SAR_RECEIVER_SET,
5009 		.expect_len = sizeof(struct btp_mesh_sar_receiver_set_cmd),
5010 		.func = sar_receiver_set
5011 	},
5012 #endif
5013 #if defined(CONFIG_BT_MESH_LARGE_COMP_DATA_CLI)
5014 	{
5015 		.opcode = BTP_MESH_LARGE_COMP_DATA_GET,
5016 		.expect_len = sizeof(struct btp_mesh_large_comp_data_get_cmd),
5017 		.func = large_comp_data_get
5018 	},
5019 	{
5020 		.opcode = BTP_MESH_MODELS_METADATA_GET,
5021 		.expect_len = sizeof(struct btp_mesh_models_metadata_get_cmd),
5022 		.func = models_metadata_get
5023 	},
5024 #endif
5025 #if defined(CONFIG_BT_MESH_OP_AGG_CLI)
5026 	{
5027 		.opcode = BTP_MESH_OPCODES_AGGREGATOR_INIT,
5028 		.expect_len = sizeof(struct btp_mesh_opcodes_aggregator_init_cmd),
5029 		.func = opcodes_aggregator_init
5030 	},
5031 	{
5032 		.opcode = BTP_MESH_OPCODES_AGGREGATOR_SEND,
5033 		.expect_len = 0,
5034 		.func = opcodes_aggregator_send
5035 	},
5036 #endif
5037 	{
5038 		.opcode = BTP_MESH_COMP_CHANGE_PREPARE,
5039 		.expect_len = 0,
5040 		.func = change_prepare
5041 	},
5042 #if defined(CONFIG_BT_MESH_RPR_CLI)
5043 	{
5044 		.opcode = BTP_MESH_RPR_SCAN_START,
5045 		.expect_len = sizeof(struct btp_rpr_scan_start_cmd),
5046 		.func = rpr_scan_start
5047 	},
5048 	{
5049 		.opcode = BTP_MESH_RPR_EXT_SCAN_START,
5050 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
5051 		.func = rpr_ext_scan_start
5052 	},
5053 	{
5054 		.opcode = BTP_MESH_RPR_SCAN_CAPS_GET,
5055 		.expect_len = sizeof(struct btp_rpr_scan_caps_get_cmd),
5056 		.func = rpr_scan_caps_get
5057 	},
5058 	{
5059 		.opcode = BTP_MESH_RPR_SCAN_GET,
5060 		.expect_len = sizeof(struct btp_rpr_scan_get_cmd),
5061 		.func = rpr_scan_get
5062 	},
5063 	{
5064 		.opcode = BTP_MESH_RPR_SCAN_STOP,
5065 		.expect_len = sizeof(struct btp_rpr_scan_stop_cmd),
5066 		.func = rpr_scan_stop
5067 	},
5068 	{
5069 		.opcode = BTP_MESH_RPR_LINK_GET,
5070 		.expect_len = sizeof(struct btp_rpr_link_get_cmd),
5071 		.func = rpr_link_get
5072 	},
5073 	{
5074 		.opcode = BTP_MESH_RPR_LINK_CLOSE,
5075 		.expect_len = sizeof(struct btp_rpr_link_close_cmd),
5076 		.func = rpr_link_close
5077 	},
5078 	{
5079 		.opcode = BTP_MESH_RPR_PROV_REMOTE,
5080 		.expect_len = sizeof(struct btp_rpr_prov_remote_cmd),
5081 		.func = rpr_prov_remote
5082 	},
5083 	{
5084 		.opcode = BTP_MESH_RPR_REPROV_REMOTE,
5085 		.expect_len = sizeof(struct btp_rpr_reprov_remote_cmd),
5086 		.func = rpr_reprov_remote
5087 	},
5088 #endif
5089 #if defined(CONFIG_BT_MESH_PRIV_BEACON_CLI)
5090 	{
5091 		.opcode = BTP_MESH_PRIV_BEACON_GET,
5092 		.expect_len = sizeof(struct btp_priv_beacon_get_cmd),
5093 		.func = priv_beacon_get
5094 	},
5095 	{
5096 		.opcode = BTP_MESH_PRIV_BEACON_SET,
5097 		.expect_len = sizeof(struct btp_priv_beacon_set_cmd),
5098 		.func = priv_beacon_set
5099 	},
5100 	{
5101 		.opcode = BTP_MESH_PRIV_GATT_PROXY_GET,
5102 		.expect_len = sizeof(struct btp_priv_gatt_proxy_get_cmd),
5103 		.func = priv_gatt_proxy_get
5104 	},
5105 	{
5106 		.opcode = BTP_MESH_PRIV_GATT_PROXY_SET,
5107 		.expect_len = sizeof(struct btp_priv_gatt_proxy_set_cmd),
5108 		.func = priv_gatt_proxy_set
5109 	},
5110 	{
5111 		.opcode = BTP_MESH_PRIV_NODE_ID_GET,
5112 		.expect_len = sizeof(struct btp_priv_node_id_get_cmd),
5113 		.func = priv_node_id_get
5114 	},
5115 	{
5116 		.opcode = BTP_MESH_PRIV_NODE_ID_SET,
5117 		.expect_len = sizeof(struct btp_priv_node_id_set_cmd),
5118 		.func = priv_node_id_set
5119 	},
5120 	{
5121 		.opcode = BTP_MESH_PROXY_PRIVATE_IDENTITY,
5122 		.expect_len = 0,
5123 		.func = proxy_private_identity_enable
5124 	},
5125 #endif
5126 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
5127 	{
5128 		.opcode = BTP_MESH_OD_PRIV_PROXY_GET,
5129 		.expect_len = sizeof(struct btp_od_priv_proxy_get_cmd),
5130 		.func = od_priv_proxy_get
5131 	},
5132 	{
5133 		.opcode = BTP_MESH_OD_PRIV_PROXY_SET,
5134 		.expect_len = sizeof(struct btp_od_priv_proxy_set_cmd),
5135 		.func = od_priv_proxy_set
5136 	},
5137 #endif
5138 #if defined(CONFIG_BT_MESH_SOL_PDU_RPL_CLI)
5139 	{
5140 		.opcode = BTP_MESH_SRPL_CLEAR,
5141 		.expect_len = sizeof(struct btp_srpl_clear_cmd),
5142 		.func = srpl_clear
5143 	},
5144 #endif
5145 #if defined(CONFIG_BT_MESH_PROXY_SOLICITATION)
5146 	{
5147 		.opcode = BTP_MESH_PROXY_SOLICIT,
5148 		.expect_len = sizeof(struct btp_proxy_solicit_cmd),
5149 		.func = proxy_solicit
5150 	},
5151 #endif
5152 	{
5153 		.opcode = BTP_MESH_START,
5154 		.expect_len = 0,
5155 		.func = start
5156 	},
5157 };
5158 
5159 
5160 static const struct btp_handler mdl_handlers[] = {
5161 #if defined(CONFIG_BT_MESH_DFD_SRV)
5162 	{
5163 		.opcode = BTP_MMDL_DFU_INFO_GET,
5164 		.expect_len = sizeof(struct btp_mmdl_dfu_info_get_cmd),
5165 		.func = dfu_info_get,
5166 	},
5167 	{
5168 		.opcode = BTP_MMDL_DFU_UPDATE_METADATA_CHECK,
5169 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
5170 		.func = dfu_update_metadata_check,
5171 	},
5172 	{
5173 		.opcode = BTP_MMDL_DFU_FIRMWARE_UPDATE_GET,
5174 		.expect_len = 0,
5175 		.func = dfu_firmware_update_get,
5176 	},
5177 	{
5178 		.opcode = BTP_MMDL_DFU_FIRMWARE_UPDATE_CANCEL,
5179 		.expect_len = 0,
5180 		.func = dfu_firmware_update_cancel,
5181 	},
5182 	{
5183 		.opcode = BTP_MMDL_DFU_FIRMWARE_UPDATE_START,
5184 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
5185 		.func = dfu_firmware_update_start,
5186 	},
5187 	{
5188 		.opcode = BTP_MMDL_DFU_FIRMWARE_UPDATE_APPLY,
5189 		.expect_len = 0,
5190 		.func = dfu_firmware_update_apply,
5191 	},
5192 #endif
5193 #if defined(CONFIG_BT_MESH_BLOB_CLI) && !defined(CONFIG_BT_MESH_DFD_SRV)
5194 	{
5195 		.opcode = BTP_MMDL_BLOB_INFO_GET,
5196 		.expect_len = BTP_HANDLER_LENGTH_VARIABLE,
5197 		.func = blob_info_get,
5198 	},
5199 	{
5200 		.opcode = BTP_MMDL_BLOB_TRANSFER_START,
5201 		.expect_len = sizeof(struct btp_mmdl_blob_transfer_start_cmd),
5202 		.func = blob_transfer_start,
5203 	},
5204 	{
5205 		.opcode = BTP_MMDL_BLOB_TRANSFER_CANCEL,
5206 		.expect_len = 0,
5207 		.func = blob_transfer_cancel,
5208 	},
5209 	{
5210 		.opcode = BTP_MMDL_BLOB_TRANSFER_GET,
5211 		.expect_len = 0,
5212 		.func = blob_transfer_get,
5213 	},
5214 #endif
5215 #if defined(CONFIG_BT_MESH_BLOB_SRV)
5216 	{
5217 		.opcode = BTP_MMDL_BLOB_SRV_RECV,
5218 		.expect_len = sizeof(struct btp_mmdl_blob_srv_recv_cmd),
5219 		.func = blob_srv_recv
5220 	},
5221 	{
5222 		.opcode = BTP_MMDL_BLOB_SRV_CANCEL,
5223 		.expect_len = 0,
5224 		.func = blob_srv_cancel
5225 	},
5226 #endif
5227 #if defined(CONFIG_BT_MESH_DFU_SRV)
5228 	{
5229 		.opcode = BTP_MMDL_DFU_SRV_APPLY,
5230 		.expect_len = 0,
5231 		.func = dfu_srv_apply
5232 	},
5233 #endif
5234 };
5235 
net_recv_ev(uint8_t ttl,uint8_t ctl,uint16_t src,uint16_t dst,const void * payload,size_t payload_len)5236 void net_recv_ev(uint8_t ttl, uint8_t ctl, uint16_t src, uint16_t dst, const void *payload,
5237 		 size_t payload_len)
5238 {
5239 	NET_BUF_SIMPLE_DEFINE(buf, UINT8_MAX);
5240 	struct btp_mesh_net_recv_ev *ev;
5241 
5242 	LOG_DBG("ttl 0x%02x ctl 0x%02x src 0x%04x dst 0x%04x payload_len %zu",
5243 		ttl, ctl, src, dst, payload_len);
5244 
5245 	if (payload_len > net_buf_simple_tailroom(&buf)) {
5246 		LOG_ERR("Payload size exceeds buffer size");
5247 		return;
5248 	}
5249 
5250 	ev = net_buf_simple_add(&buf, sizeof(*ev));
5251 	ev->ttl = ttl;
5252 	ev->ctl = ctl;
5253 	ev->src = sys_cpu_to_le16(src);
5254 	ev->dst = sys_cpu_to_le16(dst);
5255 	ev->payload_len = payload_len;
5256 	net_buf_simple_add_mem(&buf, payload, payload_len);
5257 
5258 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_NET_RECV, buf.data, buf.len);
5259 }
5260 
model_recv_ev(uint16_t src,uint16_t dst,const void * payload,size_t payload_len)5261 void model_recv_ev(uint16_t src, uint16_t dst, const void *payload,
5262 		   size_t payload_len)
5263 {
5264 	NET_BUF_SIMPLE_DEFINE(buf, UINT8_MAX);
5265 	struct btp_mesh_model_recv_ev *ev;
5266 
5267 	LOG_DBG("src 0x%04x dst 0x%04x payload_len %zu", src, dst, payload_len);
5268 
5269 	if (payload_len > net_buf_simple_tailroom(&buf)) {
5270 		LOG_ERR("Payload size exceeds buffer size");
5271 		return;
5272 	}
5273 
5274 	ev = net_buf_simple_add(&buf, sizeof(*ev));
5275 	ev->src = sys_cpu_to_le16(src);
5276 	ev->dst = sys_cpu_to_le16(dst);
5277 	ev->payload_len = payload_len;
5278 	net_buf_simple_add_mem(&buf, payload, payload_len);
5279 
5280 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_MODEL_RECV, buf.data, buf.len);
5281 }
5282 
model_bound_cb(uint16_t addr,const struct bt_mesh_model * model,uint16_t key_idx)5283 static void model_bound_cb(uint16_t addr, const struct bt_mesh_model *model,
5284 			   uint16_t key_idx)
5285 {
5286 	int i;
5287 
5288 	LOG_DBG("remote addr 0x%04x key_idx 0x%04x model %p",
5289 		addr, key_idx, model);
5290 
5291 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
5292 		if (!model_bound[i].model) {
5293 			model_bound[i].model = model;
5294 			model_bound[i].addr = addr;
5295 			model_bound[i].appkey_idx = key_idx;
5296 
5297 			return;
5298 		}
5299 	}
5300 
5301 	LOG_ERR("model_bound is full");
5302 }
5303 
model_unbound_cb(uint16_t addr,const struct bt_mesh_model * model,uint16_t key_idx)5304 static void model_unbound_cb(uint16_t addr, const struct bt_mesh_model *model,
5305 			     uint16_t key_idx)
5306 {
5307 	int i;
5308 
5309 	LOG_DBG("remote addr 0x%04x key_idx 0x%04x model %p",
5310 		addr, key_idx, model);
5311 
5312 	for (i = 0; i < ARRAY_SIZE(model_bound); i++) {
5313 		if (model_bound[i].model == model) {
5314 			model_bound[i].model = NULL;
5315 			model_bound[i].addr = 0x0000;
5316 			model_bound[i].appkey_idx = BT_MESH_KEY_UNUSED;
5317 
5318 			return;
5319 		}
5320 	}
5321 
5322 	LOG_INF("model not found");
5323 }
5324 
invalid_bearer_cb(uint8_t opcode)5325 static void invalid_bearer_cb(uint8_t opcode)
5326 {
5327 	struct btp_mesh_invalid_bearer_ev ev = {
5328 		.opcode = opcode,
5329 	};
5330 
5331 	LOG_DBG("opcode 0x%02x", opcode);
5332 
5333 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INVALID_BEARER, &ev, sizeof(ev));
5334 }
5335 
incomp_timer_exp_cb(void)5336 static void incomp_timer_exp_cb(void)
5337 {
5338 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_INCOMP_TIMER_EXP, NULL, 0);
5339 }
5340 
5341 static struct bt_test_cb bt_test_cb = {
5342 	.mesh_net_recv = net_recv_ev,
5343 	.mesh_model_recv = model_recv_ev,
5344 	.mesh_model_bound = model_bound_cb,
5345 	.mesh_model_unbound = model_unbound_cb,
5346 	.mesh_prov_invalid_bearer = invalid_bearer_cb,
5347 	.mesh_trans_incomp_timer_exp = incomp_timer_exp_cb,
5348 };
5349 
friend_established(uint16_t net_idx,uint16_t lpn_addr,uint8_t recv_delay,uint32_t polltimeout)5350 static void friend_established(uint16_t net_idx, uint16_t lpn_addr,
5351 			       uint8_t recv_delay, uint32_t polltimeout)
5352 {
5353 	struct btp_mesh_frnd_established_ev ev = { net_idx, lpn_addr, recv_delay,
5354 					       polltimeout };
5355 
5356 	LOG_DBG("Friendship (as Friend) established with "
5357 			"LPN 0x%04x Receive Delay %u Poll Timeout %u",
5358 			lpn_addr, recv_delay, polltimeout);
5359 
5360 
5361 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_FRND_ESTABLISHED, &ev, sizeof(ev));
5362 }
5363 
friend_terminated(uint16_t net_idx,uint16_t lpn_addr)5364 static void friend_terminated(uint16_t net_idx, uint16_t lpn_addr)
5365 {
5366 	struct btp_mesh_frnd_terminated_ev ev = { net_idx, lpn_addr };
5367 
5368 	LOG_DBG("Friendship (as Friend) lost with LPN "
5369 			"0x%04x", lpn_addr);
5370 
5371 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_FRND_TERMINATED, &ev, sizeof(ev));
5372 }
5373 
5374 BT_MESH_FRIEND_CB_DEFINE(friend_cb) = {
5375 	.established = friend_established,
5376 	.terminated = friend_terminated,
5377 };
5378 
lpn_established(uint16_t net_idx,uint16_t friend_addr,uint8_t queue_size,uint8_t recv_win)5379 static void lpn_established(uint16_t net_idx, uint16_t friend_addr,
5380 					uint8_t queue_size, uint8_t recv_win)
5381 {
5382 	struct btp_mesh_lpn_established_ev ev = { net_idx, friend_addr, queue_size,
5383 					      recv_win };
5384 
5385 	LOG_DBG("Friendship (as LPN) established with "
5386 			"Friend 0x%04x Queue Size %d Receive Window %d",
5387 			friend_addr, queue_size, recv_win);
5388 
5389 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_ESTABLISHED, &ev, sizeof(ev));
5390 }
5391 
lpn_terminated(uint16_t net_idx,uint16_t friend_addr)5392 static void lpn_terminated(uint16_t net_idx, uint16_t friend_addr)
5393 {
5394 	struct btp_mesh_lpn_polled_ev ev = { net_idx, friend_addr };
5395 
5396 	LOG_DBG("Friendship (as LPN) lost with Friend "
5397 			"0x%04x", friend_addr);
5398 
5399 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_TERMINATED, &ev, sizeof(ev));
5400 }
5401 
lpn_polled(uint16_t net_idx,uint16_t friend_addr,bool retry)5402 static void lpn_polled(uint16_t net_idx, uint16_t friend_addr, bool retry)
5403 {
5404 	struct btp_mesh_lpn_polled_ev ev = { net_idx, friend_addr, (uint8_t)retry };
5405 
5406 	LOG_DBG("LPN polled 0x%04x %s", friend_addr, retry ? "(retry)" : "");
5407 
5408 	tester_event(BTP_SERVICE_ID_MESH, BTP_MESH_EV_LPN_POLLED, &ev, sizeof(ev));
5409 }
5410 
5411 BT_MESH_LPN_CB_DEFINE(lpn_cb) = {
5412 	.established = lpn_established,
5413 	.terminated = lpn_terminated,
5414 	.polled = lpn_polled,
5415 };
5416 
tester_init_mesh(void)5417 uint8_t tester_init_mesh(void)
5418 {
5419 	if (IS_ENABLED(CONFIG_BT_TESTING)) {
5420 		bt_test_cb_register(&bt_test_cb);
5421 	}
5422 
5423 #if defined(CONFIG_BT_MESH_COMP_PAGE_2)
5424 	bt_mesh_comp2_register(&comp_p2);
5425 #endif
5426 
5427 	tester_register_command_handlers(BTP_SERVICE_ID_MESH, handlers,
5428 					 ARRAY_SIZE(handlers));
5429 
5430 	return BTP_STATUS_SUCCESS;
5431 }
5432 
tester_unregister_mesh(void)5433 uint8_t tester_unregister_mesh(void)
5434 {
5435 	return BTP_STATUS_SUCCESS;
5436 }
5437 
tester_init_mmdl(void)5438 uint8_t tester_init_mmdl(void)
5439 {
5440 	tester_register_command_handlers(BTP_SERVICE_ID_MESH_MDL, mdl_handlers,
5441 					 ARRAY_SIZE(mdl_handlers));
5442 	return BTP_STATUS_SUCCESS;
5443 }
5444 
tester_unregister_mmdl(void)5445 uint8_t tester_unregister_mmdl(void)
5446 {
5447 	return BTP_STATUS_SUCCESS;
5448 }
5449