1 /*
2  * Copyright (c) 2017 Intel Corporation
3  * Copyright (c) 2021 Lingao Meng
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/iterable_sections.h>
11 #include <zephyr/net_buf.h>
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/bluetooth/conn.h>
14 #include <zephyr/bluetooth/gatt.h>
15 #include <zephyr/bluetooth/mesh.h>
16 #include <zephyr/sys/util.h>
17 
18 #include <zephyr/bluetooth/hci.h>
19 
20 #include "common/bt_str.h"
21 
22 #include "mesh.h"
23 #include "net.h"
24 #include "rpl.h"
25 #include "transport.h"
26 #include "prov.h"
27 #include "beacon.h"
28 #include "foundation.h"
29 #include "access.h"
30 #include "proxy.h"
31 #include "gatt.h"
32 #include "proxy_msg.h"
33 #include "crypto.h"
34 
35 #define LOG_LEVEL CONFIG_BT_MESH_PROXY_LOG_LEVEL
36 #include <zephyr/logging/log.h>
37 LOG_MODULE_REGISTER(bt_mesh_gatt);
38 
39 #define PROXY_SVC_INIT_TIMEOUT K_MSEC(10)
40 #define PROXY_SVC_REG_ATTEMPTS 5
41 
42 /* Interval to update random value in (10 minutes).
43  *
44  * Defined in the Bluetooth Mesh Specification v1.1, Section 7.2.2.2.4.
45  */
46 #define PROXY_RANDOM_UPDATE_INTERVAL (10 * 60 * MSEC_PER_SEC)
47 
48 #define ADV_OPT_ADDR(private) (IS_ENABLED(CONFIG_BT_MESH_DEBUG_USE_ID_ADDR) ?                      \
49 			       BT_LE_ADV_OPT_USE_IDENTITY : (private) ? BT_LE_ADV_OPT_USE_NRPA : 0)
50 
51 #define ADV_OPT_PROXY(private)                                                                     \
52 	(BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_SCANNABLE | ADV_OPT_ADDR(private))
53 
54 static void proxy_send_beacons(struct k_work *work);
55 static int proxy_send(struct bt_conn *conn,
56 		      const void *data, uint16_t len,
57 		      bt_gatt_complete_func_t end, void *user_data);
58 
59 static struct bt_mesh_proxy_client {
60 	struct bt_mesh_proxy_role *cli;
61 	uint16_t filter[CONFIG_BT_MESH_PROXY_FILTER_SIZE];
62 	enum __packed {
63 		NONE,
64 		ACCEPT,
65 		REJECT,
66 	} filter_type;
67 	struct k_work send_beacons;
68 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
69 	bool privacy;
70 #endif
71 } clients[CONFIG_BT_MAX_CONN] = {
72 	[0 ... (CONFIG_BT_MAX_CONN - 1)] = {
73 		.send_beacons = Z_WORK_INITIALIZER(proxy_send_beacons),
74 	},
75 };
76 
77 static bool service_registered;
78 
find_client(struct bt_conn * conn)79 static struct bt_mesh_proxy_client *find_client(struct bt_conn *conn)
80 {
81 	return &clients[bt_conn_index(conn)];
82 }
83 
gatt_recv(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)84 static ssize_t gatt_recv(struct bt_conn *conn,
85 			 const struct bt_gatt_attr *attr, const void *buf,
86 			 uint16_t len, uint16_t offset, uint8_t flags)
87 {
88 	const uint8_t *data = buf;
89 
90 	if (len < 1) {
91 		LOG_WRN("Too small Proxy PDU");
92 		return -EINVAL;
93 	}
94 
95 	if (PDU_TYPE(data) == BT_MESH_PROXY_PROV) {
96 		LOG_WRN("Proxy PDU type doesn't match GATT service");
97 		return -EINVAL;
98 	}
99 
100 	return bt_mesh_proxy_msg_recv(conn, buf, len);
101 }
102 
103 /* Next subnet in queue to be advertised */
104 static struct bt_mesh_subnet *beacon_sub;
105 
filter_set(struct bt_mesh_proxy_client * client,struct net_buf_simple * buf)106 static int filter_set(struct bt_mesh_proxy_client *client,
107 		      struct net_buf_simple *buf)
108 {
109 	uint8_t type;
110 
111 	if (buf->len < 1) {
112 		LOG_WRN("Too short Filter Set message");
113 		return -EINVAL;
114 	}
115 
116 	type = net_buf_simple_pull_u8(buf);
117 	LOG_DBG("type 0x%02x", type);
118 
119 	switch (type) {
120 	case 0x00:
121 		(void)memset(client->filter, 0, sizeof(client->filter));
122 		client->filter_type = ACCEPT;
123 		break;
124 	case 0x01:
125 		(void)memset(client->filter, 0, sizeof(client->filter));
126 		client->filter_type = REJECT;
127 		break;
128 	default:
129 		LOG_WRN("Prohibited Filter Type 0x%02x", type);
130 		return -EINVAL;
131 	}
132 
133 	return 0;
134 }
135 
filter_add(struct bt_mesh_proxy_client * client,uint16_t addr)136 static void filter_add(struct bt_mesh_proxy_client *client, uint16_t addr)
137 {
138 	int i;
139 
140 	LOG_DBG("addr 0x%04x", addr);
141 
142 	if (addr == BT_MESH_ADDR_UNASSIGNED) {
143 		return;
144 	}
145 
146 	for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
147 		if (client->filter[i] == addr) {
148 			return;
149 		}
150 	}
151 
152 	for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
153 		if (client->filter[i] == BT_MESH_ADDR_UNASSIGNED) {
154 			client->filter[i] = addr;
155 			return;
156 		}
157 	}
158 }
159 
filter_remove(struct bt_mesh_proxy_client * client,uint16_t addr)160 static void filter_remove(struct bt_mesh_proxy_client *client, uint16_t addr)
161 {
162 	int i;
163 
164 	LOG_DBG("addr 0x%04x", addr);
165 
166 	if (addr == BT_MESH_ADDR_UNASSIGNED) {
167 		return;
168 	}
169 
170 	for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
171 		if (client->filter[i] == addr) {
172 			client->filter[i] = BT_MESH_ADDR_UNASSIGNED;
173 			return;
174 		}
175 	}
176 }
177 
send_filter_status(struct bt_mesh_proxy_client * client,struct bt_mesh_net_rx * rx,struct net_buf_simple * buf)178 static void send_filter_status(struct bt_mesh_proxy_client *client,
179 			       struct bt_mesh_net_rx *rx,
180 			       struct net_buf_simple *buf)
181 {
182 	struct bt_mesh_net_tx tx = {
183 		.sub = rx->sub,
184 		.ctx = &rx->ctx,
185 		.src = bt_mesh_primary_addr(),
186 	};
187 	uint16_t filter_size;
188 	int i, err;
189 
190 	/* Configuration messages always have dst unassigned */
191 	tx.ctx->addr = BT_MESH_ADDR_UNASSIGNED;
192 
193 	net_buf_simple_reset(buf);
194 	net_buf_simple_reserve(buf, 10);
195 
196 	net_buf_simple_add_u8(buf, CFG_FILTER_STATUS);
197 
198 	if (client->filter_type == ACCEPT) {
199 		net_buf_simple_add_u8(buf, 0x00);
200 	} else {
201 		net_buf_simple_add_u8(buf, 0x01);
202 	}
203 
204 	for (filter_size = 0U, i = 0; i < ARRAY_SIZE(client->filter); i++) {
205 		if (client->filter[i] != BT_MESH_ADDR_UNASSIGNED) {
206 			filter_size++;
207 		}
208 	}
209 
210 	net_buf_simple_add_be16(buf, filter_size);
211 
212 	LOG_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
213 
214 	err = bt_mesh_net_encode(&tx, buf, BT_MESH_NONCE_PROXY);
215 	if (err) {
216 		LOG_ERR("Encoding Proxy cfg message failed (err %d)", err);
217 		return;
218 	}
219 
220 	err = bt_mesh_proxy_msg_send(client->cli->conn, BT_MESH_PROXY_CONFIG,
221 				     buf, NULL, NULL);
222 	if (err) {
223 		LOG_ERR("Failed to send proxy cfg message (err %d)", err);
224 	}
225 }
226 
proxy_filter_recv(struct bt_conn * conn,struct bt_mesh_net_rx * rx,struct net_buf_simple * buf)227 static void proxy_filter_recv(struct bt_conn *conn,
228 			      struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
229 {
230 	struct bt_mesh_proxy_client *client;
231 	uint8_t opcode;
232 
233 	client = find_client(conn);
234 
235 	opcode = net_buf_simple_pull_u8(buf);
236 	switch (opcode) {
237 	case CFG_FILTER_SET:
238 		filter_set(client, buf);
239 		send_filter_status(client, rx, buf);
240 		break;
241 	case CFG_FILTER_ADD:
242 		while (buf->len >= 2) {
243 			uint16_t addr;
244 
245 			addr = net_buf_simple_pull_be16(buf);
246 			filter_add(client, addr);
247 		}
248 		send_filter_status(client, rx, buf);
249 		break;
250 	case CFG_FILTER_REMOVE:
251 		while (buf->len >= 2) {
252 			uint16_t addr;
253 
254 			addr = net_buf_simple_pull_be16(buf);
255 			filter_remove(client, addr);
256 		}
257 		send_filter_status(client, rx, buf);
258 		break;
259 	default:
260 		LOG_WRN("Unhandled configuration OpCode 0x%02x", opcode);
261 		break;
262 	}
263 }
264 
proxy_cfg(struct bt_mesh_proxy_role * role)265 static void proxy_cfg(struct bt_mesh_proxy_role *role)
266 {
267 	NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_NET_MAX_PDU_LEN);
268 	struct bt_mesh_net_rx rx;
269 	int err;
270 
271 	err = bt_mesh_net_decode(&role->buf, BT_MESH_NET_IF_PROXY_CFG,
272 				 &rx, &buf);
273 	if (err) {
274 		LOG_ERR("Failed to decode Proxy Configuration (err %d)", err);
275 		return;
276 	}
277 
278 	rx.local_match = 1U;
279 
280 	if (bt_mesh_rpl_check(&rx, NULL, false)) {
281 		LOG_WRN("Replay: src 0x%04x dst 0x%04x seq 0x%06x", rx.ctx.addr, rx.ctx.recv_dst,
282 			rx.seq);
283 		return;
284 	}
285 
286 	/* Remove network headers */
287 	net_buf_simple_pull(&buf, BT_MESH_NET_HDR_LEN);
288 
289 	LOG_DBG("%u bytes: %s", buf.len, bt_hex(buf.data, buf.len));
290 
291 	if (buf.len < 1) {
292 		LOG_WRN("Too short proxy configuration PDU");
293 		return;
294 	}
295 
296 	proxy_filter_recv(role->conn, &rx, &buf);
297 }
298 
proxy_msg_recv(struct bt_mesh_proxy_role * role)299 static void proxy_msg_recv(struct bt_mesh_proxy_role *role)
300 {
301 	switch (role->msg_type) {
302 	case BT_MESH_PROXY_NET_PDU:
303 		LOG_DBG("Mesh Network PDU");
304 		bt_mesh_net_recv(&role->buf, 0, BT_MESH_NET_IF_PROXY);
305 		break;
306 	case BT_MESH_PROXY_BEACON:
307 		LOG_DBG("Mesh Beacon PDU");
308 		bt_mesh_beacon_recv(&role->buf);
309 		break;
310 	case BT_MESH_PROXY_CONFIG:
311 		LOG_DBG("Mesh Configuration PDU");
312 		proxy_cfg(role);
313 		break;
314 	default:
315 		LOG_WRN("Unhandled Message Type 0x%02x", role->msg_type);
316 		break;
317 	}
318 }
319 
beacon_send(struct bt_mesh_proxy_client * client,struct bt_mesh_subnet * sub)320 static int beacon_send(struct bt_mesh_proxy_client *client,
321 		       struct bt_mesh_subnet *sub)
322 {
323 	int err;
324 
325 	NET_BUF_SIMPLE_DEFINE(buf, 28);
326 
327 	net_buf_simple_reserve(&buf, 1);
328 
329 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
330 	err = bt_mesh_beacon_create(sub, &buf, client->privacy);
331 #else
332 	err = bt_mesh_beacon_create(sub, &buf, false);
333 #endif
334 	if (err) {
335 		return err;
336 	}
337 
338 	return bt_mesh_proxy_msg_send(client->cli->conn, BT_MESH_PROXY_BEACON,
339 				      &buf, NULL, NULL);
340 }
341 
send_beacon_cb(struct bt_mesh_subnet * sub,void * cb_data)342 static bool send_beacon_cb(struct bt_mesh_subnet *sub, void *cb_data)
343 {
344 	struct bt_mesh_proxy_client *client = cb_data;
345 
346 	return beacon_send(client, sub) != 0;
347 }
348 
proxy_send_beacons(struct k_work * work)349 static void proxy_send_beacons(struct k_work *work)
350 {
351 	struct bt_mesh_proxy_client *client;
352 
353 	client = CONTAINER_OF(work, struct bt_mesh_proxy_client, send_beacons);
354 
355 	(void)bt_mesh_subnet_find(send_beacon_cb, client);
356 }
357 
bt_mesh_proxy_beacon_send(struct bt_mesh_subnet * sub)358 void bt_mesh_proxy_beacon_send(struct bt_mesh_subnet *sub)
359 {
360 	int i;
361 
362 	if (!sub) {
363 		/* NULL means we send on all subnets */
364 		bt_mesh_subnet_foreach(bt_mesh_proxy_beacon_send);
365 		return;
366 	}
367 
368 	for (i = 0; i < ARRAY_SIZE(clients); i++) {
369 		if (clients[i].cli) {
370 			beacon_send(&clients[i], sub);
371 		}
372 	}
373 }
374 
identity_enabled(struct bt_mesh_subnet * sub)375 static void identity_enabled(struct bt_mesh_subnet *sub)
376 {
377 	sub->node_id = BT_MESH_NODE_IDENTITY_RUNNING;
378 	sub->node_id_start = k_uptime_get_32();
379 
380 	STRUCT_SECTION_FOREACH(bt_mesh_proxy_cb, cb) {
381 		if (cb->identity_enabled) {
382 			cb->identity_enabled(sub->net_idx);
383 		}
384 	}
385 }
386 
node_id_start(struct bt_mesh_subnet * sub)387 static void node_id_start(struct bt_mesh_subnet *sub)
388 {
389 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
390 	sub->priv_beacon_ctx.node_id = false;
391 #endif
392 
393 	identity_enabled(sub);
394 }
395 
private_node_id_start(struct bt_mesh_subnet * sub)396 static void private_node_id_start(struct bt_mesh_subnet *sub)
397 {
398 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
399 	sub->priv_beacon_ctx.node_id = true;
400 #endif
401 
402 	identity_enabled(sub);
403 }
404 
bt_mesh_proxy_identity_start(struct bt_mesh_subnet * sub,bool private)405 void bt_mesh_proxy_identity_start(struct bt_mesh_subnet *sub, bool private)
406 {
407 	if (private) {
408 		private_node_id_start(sub);
409 	} else {
410 		node_id_start(sub);
411 	}
412 
413 	/* Prioritize the recently enabled subnet */
414 	beacon_sub = sub;
415 }
416 
bt_mesh_proxy_identity_stop(struct bt_mesh_subnet * sub)417 void bt_mesh_proxy_identity_stop(struct bt_mesh_subnet *sub)
418 {
419 	sub->node_id = BT_MESH_NODE_IDENTITY_STOPPED;
420 	sub->node_id_start = 0U;
421 
422 	STRUCT_SECTION_FOREACH(bt_mesh_proxy_cb, cb) {
423 		if (cb->identity_disabled) {
424 			cb->identity_disabled(sub->net_idx);
425 		}
426 	}
427 }
428 
bt_mesh_proxy_identity_enable(void)429 int bt_mesh_proxy_identity_enable(void)
430 {
431 	LOG_DBG("");
432 
433 	if (!bt_mesh_is_provisioned()) {
434 		return -EAGAIN;
435 	}
436 
437 	if (bt_mesh_subnet_foreach(node_id_start)) {
438 		bt_mesh_adv_gatt_update();
439 	}
440 
441 	return 0;
442 }
443 
bt_mesh_proxy_private_identity_enable(void)444 int bt_mesh_proxy_private_identity_enable(void)
445 {
446 	LOG_DBG("");
447 
448 	if (!IS_ENABLED(CONFIG_BT_MESH_PRIV_BEACONS)) {
449 		return -ENOTSUP;
450 	}
451 
452 	if (!bt_mesh_is_provisioned()) {
453 		return -EAGAIN;
454 	}
455 
456 	if (bt_mesh_subnet_foreach(private_node_id_start)) {
457 		bt_mesh_adv_gatt_update();
458 	}
459 
460 	return 0;
461 }
462 
463 #define ENC_ID_LEN  19
464 #define NET_ID_LEN   11
465 
466 #define NODE_ID_TIMEOUT (CONFIG_BT_MESH_NODE_ID_TIMEOUT * MSEC_PER_SEC)
467 
468 static uint8_t proxy_svc_data[ENC_ID_LEN] = {
469 	BT_UUID_16_ENCODE(BT_UUID_MESH_PROXY_VAL),
470 };
471 
472 static const struct bt_data enc_id_ad[] = {
473 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
474 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
475 		      BT_UUID_16_ENCODE(BT_UUID_MESH_PROXY_VAL)),
476 	BT_DATA(BT_DATA_SVC_DATA16, proxy_svc_data, ENC_ID_LEN),
477 };
478 
479 static const struct bt_data net_id_ad[] = {
480 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
481 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
482 		      BT_UUID_16_ENCODE(BT_UUID_MESH_PROXY_VAL)),
483 	BT_DATA(BT_DATA_SVC_DATA16, proxy_svc_data, NET_ID_LEN),
484 };
485 
randomize_bt_addr(void)486 static int randomize_bt_addr(void)
487 {
488 	/* TODO: There appears to be no way to force an RPA/NRPA refresh. */
489 	return 0;
490 }
491 
enc_id_adv(struct bt_mesh_subnet * sub,uint8_t type,uint8_t hash[16],int32_t duration)492 static int enc_id_adv(struct bt_mesh_subnet *sub, uint8_t type,
493 		      uint8_t hash[16], int32_t duration)
494 {
495 	struct bt_le_adv_param slow_adv_param = {
496 		.id = BT_ID_DEFAULT,
497 		.options = ADV_OPT_PROXY(type == BT_MESH_ID_TYPE_PRIV_NET ||
498 					 type == BT_MESH_ID_TYPE_PRIV_NODE),
499 		ADV_SLOW_INT,
500 	};
501 	struct bt_le_adv_param fast_adv_param = {
502 		.id = BT_ID_DEFAULT,
503 		.options = ADV_OPT_PROXY(type == BT_MESH_ID_TYPE_PRIV_NET ||
504 					 type == BT_MESH_ID_TYPE_PRIV_NODE),
505 		ADV_FAST_INT,
506 	};
507 	struct bt_data sd[1];
508 	int err;
509 
510 	err = bt_mesh_encrypt(&sub->keys[SUBNET_KEY_TX_IDX(sub)].identity, hash, hash);
511 	if (err) {
512 		return err;
513 	}
514 
515 	/* MshPRTv1.1: 7.2.2.2.4: The AdvA field shall be regenerated whenever the Random field is
516 	 * regenerated.
517 	 */
518 	err = randomize_bt_addr();
519 	if (err) {
520 		LOG_ERR("AdvA refresh failed: %d", err);
521 		return err;
522 	}
523 
524 	proxy_svc_data[2] = type;
525 	memcpy(&proxy_svc_data[3], &hash[8], 8);
526 
527 	if (IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME)) {
528 		sd[0].type = BT_DATA_NAME_COMPLETE;
529 		sd[0].data_len = BT_DEVICE_NAME_LEN;
530 		sd[0].data = BT_DEVICE_NAME;
531 	}
532 
533 	err = bt_mesh_adv_gatt_start(
534 		type == BT_MESH_ID_TYPE_PRIV_NET ? &slow_adv_param : &fast_adv_param,
535 		duration, enc_id_ad, ARRAY_SIZE(enc_id_ad),
536 		IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME) ? sd : NULL,
537 		IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME) ? ARRAY_SIZE(sd) : 0);
538 	if (err) {
539 		LOG_WRN("Failed to advertise using type 0x%02x (err %d)", type, err);
540 		return err;
541 	}
542 
543 	return 0;
544 }
545 
node_id_adv(struct bt_mesh_subnet * sub,int32_t duration)546 static int node_id_adv(struct bt_mesh_subnet *sub, int32_t duration)
547 {
548 	uint8_t *random = &proxy_svc_data[11];
549 	uint8_t tmp[16];
550 	int err;
551 
552 	LOG_DBG("0x%03x", sub->net_idx);
553 
554 	err = bt_rand(random, 8);
555 	if (err) {
556 		return err;
557 	}
558 
559 	memset(&tmp[0], 0x00, 6);
560 	memcpy(&tmp[6], random, 8);
561 	sys_put_be16(bt_mesh_primary_addr(), &tmp[14]);
562 
563 	return enc_id_adv(sub, BT_MESH_ID_TYPE_NODE, tmp, duration);
564 }
565 
priv_node_id_adv(struct bt_mesh_subnet * sub,int32_t duration)566 static int priv_node_id_adv(struct bt_mesh_subnet *sub, int32_t duration)
567 {
568 	uint8_t *random = &proxy_svc_data[11];
569 	uint8_t tmp[16];
570 	int err;
571 
572 	LOG_DBG("0x%03x", sub->net_idx);
573 
574 	err = bt_rand(random, 8);
575 	if (err) {
576 		return err;
577 	}
578 
579 	memset(&tmp[0], 0x00, 5);
580 	tmp[5] = 0x03;
581 	memcpy(&tmp[6], random, 8);
582 	sys_put_be16(bt_mesh_primary_addr(), &tmp[14]);
583 
584 	return enc_id_adv(sub, BT_MESH_ID_TYPE_PRIV_NODE, tmp, duration);
585 }
586 
priv_net_id_adv(struct bt_mesh_subnet * sub,int32_t duration)587 static int priv_net_id_adv(struct bt_mesh_subnet *sub, int32_t duration)
588 {
589 	uint8_t *random = &proxy_svc_data[11];
590 	uint8_t tmp[16];
591 	int err;
592 
593 	LOG_DBG("0x%03x", sub->net_idx);
594 
595 	err = bt_rand(random, 8);
596 	if (err) {
597 		return err;
598 	}
599 
600 	memcpy(&tmp[0], sub->keys[SUBNET_KEY_TX_IDX(sub)].net_id, 8);
601 	memcpy(&tmp[8], random, 8);
602 
603 	return enc_id_adv(sub, BT_MESH_ID_TYPE_PRIV_NET, tmp, duration);
604 }
605 
net_id_adv(struct bt_mesh_subnet * sub,int32_t duration)606 static int net_id_adv(struct bt_mesh_subnet *sub, int32_t duration)
607 {
608 	struct bt_le_adv_param slow_adv_param = {
609 		.id = BT_ID_DEFAULT,
610 		.options = ADV_OPT_PROXY(false),
611 		ADV_SLOW_INT,
612 	};
613 	struct bt_data sd[1];
614 	int err;
615 
616 	proxy_svc_data[2] = BT_MESH_ID_TYPE_NET;
617 
618 	LOG_DBG("Advertising with NetId %s", bt_hex(sub->keys[SUBNET_KEY_TX_IDX(sub)].net_id, 8));
619 
620 	memcpy(proxy_svc_data + 3, sub->keys[SUBNET_KEY_TX_IDX(sub)].net_id, 8);
621 
622 	if (IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME)) {
623 		sd[0].type = BT_DATA_NAME_COMPLETE;
624 		sd[0].data_len = BT_DEVICE_NAME_LEN;
625 		sd[0].data = BT_DEVICE_NAME;
626 	}
627 
628 	err = bt_mesh_adv_gatt_start(&slow_adv_param, duration, net_id_ad,
629 				     ARRAY_SIZE(net_id_ad),
630 				     IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME) ? sd : NULL,
631 				     IS_ENABLED(CONFIG_BT_MESH_PROXY_USE_DEVICE_NAME) ?
632 					ARRAY_SIZE(sd) : 0);
633 	if (err) {
634 		LOG_WRN("Failed to advertise using Network ID (err %d)", err);
635 		return err;
636 	}
637 
638 	return 0;
639 }
640 
is_sub_proxy_active(struct bt_mesh_subnet * sub)641 static bool is_sub_proxy_active(struct bt_mesh_subnet *sub)
642 {
643 	if (sub->net_idx == BT_MESH_KEY_UNUSED) {
644 		return false;
645 	}
646 
647 	return (sub->node_id == BT_MESH_NODE_IDENTITY_RUNNING ||
648 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
649 		(bt_mesh_od_priv_proxy_get() > 0 && sub->solicited) ||
650 #endif
651 		bt_mesh_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED ||
652 		bt_mesh_priv_gatt_proxy_get() == BT_MESH_GATT_PROXY_ENABLED);
653 }
654 
active_proxy_sub_cnt_cb(struct bt_mesh_subnet * sub,void * cb_data)655 static bool active_proxy_sub_cnt_cb(struct bt_mesh_subnet *sub, void *cb_data)
656 {
657 	int *cnt = cb_data;
658 
659 	if (is_sub_proxy_active(sub)) {
660 		(*cnt)++;
661 	}
662 
663 	/* Don't stop until we've visited all subnets.
664 	 * We're only using the "find" variant of the subnet iteration to get a context parameter.
665 	 */
666 	return false;
667 }
668 
active_proxy_sub_cnt_get(void)669 static int active_proxy_sub_cnt_get(void)
670 {
671 	int cnt = 0;
672 
673 	(void)bt_mesh_subnet_find(active_proxy_sub_cnt_cb, &cnt);
674 
675 	return cnt;
676 }
677 
proxy_adv_timeout_eval(struct bt_mesh_subnet * sub)678 static void proxy_adv_timeout_eval(struct bt_mesh_subnet *sub)
679 {
680 	int32_t time_passed;
681 
682 	if (sub->node_id == BT_MESH_NODE_IDENTITY_RUNNING) {
683 		time_passed = k_uptime_get_32() - sub->node_id_start;
684 		if (time_passed > (NODE_ID_TIMEOUT - MSEC_PER_SEC)) {
685 			bt_mesh_proxy_identity_stop(sub);
686 			LOG_DBG("Node ID stopped for subnet %d after %dms", sub->net_idx,
687 				time_passed);
688 		}
689 	}
690 
691 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
692 	if (bt_mesh_od_priv_proxy_get() > 0 && sub->solicited && sub->priv_net_id_sent) {
693 		time_passed = k_uptime_get_32() - sub->priv_net_id_sent;
694 		if (time_passed > ((MSEC_PER_SEC * bt_mesh_od_priv_proxy_get()) - MSEC_PER_SEC)) {
695 			sub->priv_net_id_sent = 0;
696 			sub->solicited = false;
697 			LOG_DBG("Private Network ID stopped for subnet %d after %dms on "
698 				"solicitation",
699 				sub->net_idx, time_passed);
700 		}
701 	}
702 #endif
703 }
704 
705 enum proxy_adv_evt {
706 	NET_ID,
707 	PRIV_NET_ID,
708 	NODE_ID,
709 	PRIV_NODE_ID,
710 	OD_PRIV_NET_ID,
711 };
712 
713 struct proxy_adv_request {
714 	int32_t duration;
715 	enum proxy_adv_evt evt;
716 };
717 
proxy_adv_request_get(struct bt_mesh_subnet * sub,struct proxy_adv_request * request)718 static bool proxy_adv_request_get(struct bt_mesh_subnet *sub, struct proxy_adv_request *request)
719 {
720 	if (!sub) {
721 		return false;
722 	}
723 
724 	if (sub->net_idx == BT_MESH_KEY_UNUSED) {
725 		return false;
726 	}
727 
728 	/** The priority for proxy adv is first solicitation, then Node Identity,
729 	 *  and lastly Network ID. Network ID is prioritized last since, in many
730 	 *  cases, another device can fulfill the same demand. Solicitation is
731 	 *  prioritized first since legacy devices are dependent on this to
732 	 *  connect to the network.
733 	 */
734 
735 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
736 	if (bt_mesh_od_priv_proxy_get() > 0 && sub->solicited) {
737 		int32_t timeout = MSEC_PER_SEC * (int32_t)bt_mesh_od_priv_proxy_get();
738 
739 		request->evt = OD_PRIV_NET_ID;
740 		request->duration = !sub->priv_net_id_sent
741 					    ? timeout
742 					    : timeout - (k_uptime_get_32() - sub->priv_net_id_sent);
743 		return true;
744 	}
745 #endif
746 
747 	if (sub->node_id == BT_MESH_NODE_IDENTITY_RUNNING) {
748 		request->duration = NODE_ID_TIMEOUT - (k_uptime_get_32() - sub->node_id_start);
749 		request->evt =
750 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
751 			sub->priv_beacon_ctx.node_id ? PRIV_NODE_ID :
752 #endif
753 				NODE_ID;
754 
755 		return true;
756 	}
757 
758 	if (bt_mesh_priv_gatt_proxy_get() == BT_MESH_FEATURE_ENABLED) {
759 		request->evt = PRIV_NET_ID;
760 		request->duration = PROXY_RANDOM_UPDATE_INTERVAL;
761 		return true;
762 	}
763 
764 	if (bt_mesh_gatt_proxy_get() == BT_MESH_FEATURE_ENABLED) {
765 		request->evt = NET_ID;
766 		request->duration = SYS_FOREVER_MS;
767 		return true;
768 	}
769 
770 	return false;
771 }
772 
adv_sub_get_next(struct bt_mesh_subnet * sub_start,struct proxy_adv_request * request)773 static struct bt_mesh_subnet *adv_sub_get_next(struct bt_mesh_subnet *sub_start,
774 					       struct proxy_adv_request *request)
775 {
776 	struct bt_mesh_subnet *sub_temp = bt_mesh_subnet_next(sub_start);
777 
778 	do {
779 		if (proxy_adv_request_get(sub_temp, request)) {
780 			return sub_temp;
781 		}
782 
783 		sub_temp = bt_mesh_subnet_next(sub_temp);
784 	} while (sub_temp != sub_start);
785 
786 	return NULL;
787 }
788 
789 static struct {
790 	int32_t start;
791 	struct bt_mesh_subnet *sub;
792 	struct proxy_adv_request request;
793 } sub_adv;
794 
gatt_proxy_advertise(void)795 static int gatt_proxy_advertise(void)
796 {
797 	int err;
798 
799 	int32_t max_adv_duration = 0;
800 	int cnt;
801 	struct bt_mesh_subnet *sub;
802 	struct proxy_adv_request request;
803 
804 	LOG_DBG("");
805 
806 	/* Close proxy activity that has timed out on all subnets */
807 	bt_mesh_subnet_foreach(proxy_adv_timeout_eval);
808 
809 	if (!bt_mesh_proxy_has_avail_conn()) {
810 		LOG_DBG("Connectable advertising deferred (max connections)");
811 		return -ENOMEM;
812 	}
813 
814 	cnt = active_proxy_sub_cnt_get();
815 	if (!cnt) {
816 		LOG_DBG("No subnets to advertise proxy on");
817 		return -ENOENT;
818 	} else if (cnt > 1) {
819 		/** There is more than one subnet that requires proxy adv,
820 		 *  and the adv resources must be shared.
821 		 */
822 
823 		/* We use NODE_ID_TIMEOUT as a starting point since it may
824 		 * be less than 60 seconds. Divide this period into at least
825 		 * 6 slices, but make sure that a slice is more than one
826 		 * second long (to avoid excessive rotation).
827 		 */
828 		max_adv_duration = NODE_ID_TIMEOUT / MAX(cnt, 6);
829 		max_adv_duration = MAX(max_adv_duration, MSEC_PER_SEC + 20);
830 
831 		/* Check if the previous subnet finished its allocated timeslot */
832 		if ((sub_adv.request.duration != SYS_FOREVER_MS) &&
833 		    proxy_adv_request_get(sub_adv.sub, &request) &&
834 		    (sub_adv.request.evt == request.evt)) {
835 			int32_t time_passed = k_uptime_get_32() - sub_adv.start;
836 
837 			if (time_passed < sub_adv.request.duration &&
838 			    ((sub_adv.request.duration - time_passed) >= MSEC_PER_SEC)) {
839 				sub = sub_adv.sub;
840 				request.duration = sub_adv.request.duration - time_passed;
841 				goto end;
842 			}
843 		}
844 	}
845 
846 	sub = adv_sub_get_next(sub_adv.sub, &request);
847 	if (!sub) {
848 		LOG_ERR("Could not find subnet to advertise");
849 		return -ENOENT;
850 	}
851 end:
852 	if (cnt > 1) {
853 		request.duration = (request.duration == SYS_FOREVER_MS)
854 					   ? max_adv_duration
855 					   : MIN(request.duration, max_adv_duration);
856 	}
857 
858 	/* Save current state for next iteration */
859 	sub_adv.start = k_uptime_get_32();
860 	sub_adv.sub = sub;
861 	sub_adv.request = request;
862 
863 	switch (request.evt) {
864 	case NET_ID:
865 		err = net_id_adv(sub, request.duration);
866 		break;
867 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
868 	case OD_PRIV_NET_ID:
869 		if (!sub->priv_net_id_sent) {
870 			sub->priv_net_id_sent = k_uptime_get();
871 		}
872 		/* Fall through */
873 #endif
874 	case PRIV_NET_ID:
875 		err = priv_net_id_adv(sub, request.duration);
876 		break;
877 	case NODE_ID:
878 		err = node_id_adv(sub, request.duration);
879 		break;
880 	case PRIV_NODE_ID:
881 		err = priv_node_id_adv(sub, request.duration);
882 		break;
883 	default:
884 		LOG_ERR("Unexpected proxy adv evt: %d", request.evt);
885 		return -ENODEV;
886 	}
887 
888 	if (err) {
889 		LOG_ERR("Advertising proxy failed (err: %d)", err);
890 		return err;
891 	}
892 
893 	LOG_DBG("Advertising %d ms for net_idx 0x%04x", request.duration, sub->net_idx);
894 	return err;
895 }
896 
subnet_evt(struct bt_mesh_subnet * sub,enum bt_mesh_key_evt evt)897 static void subnet_evt(struct bt_mesh_subnet *sub, enum bt_mesh_key_evt evt)
898 {
899 	if (evt == BT_MESH_KEY_DELETED) {
900 		if (sub == beacon_sub) {
901 			beacon_sub = NULL;
902 		}
903 	} else {
904 		bt_mesh_proxy_beacon_send(sub);
905 		bt_mesh_adv_gatt_update();
906 	}
907 }
908 
909 BT_MESH_SUBNET_CB_DEFINE(gatt_services) = {
910 	.evt_handler = subnet_evt,
911 };
912 
proxy_ccc_changed(const struct bt_gatt_attr * attr,uint16_t value)913 static void proxy_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
914 {
915 	LOG_DBG("value 0x%04x", value);
916 }
917 
proxy_ccc_write(struct bt_conn * conn,const struct bt_gatt_attr * attr,uint16_t value)918 static ssize_t proxy_ccc_write(struct bt_conn *conn,
919 			       const struct bt_gatt_attr *attr, uint16_t value)
920 {
921 	struct bt_mesh_proxy_client *client;
922 
923 	LOG_DBG("value: 0x%04x", value);
924 
925 	if (value != BT_GATT_CCC_NOTIFY) {
926 		LOG_WRN("Client wrote 0x%04x instead enabling notify", value);
927 		return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
928 	}
929 
930 	client = find_client(conn);
931 	if (client->filter_type == NONE) {
932 		client->filter_type = ACCEPT;
933 		bt_mesh_wq_submit(&client->send_beacons);
934 	}
935 
936 	return sizeof(value);
937 }
938 
939 /* Mesh Proxy Service Declaration */
940 static struct _bt_gatt_ccc proxy_ccc =
941 	BT_GATT_CCC_INITIALIZER(proxy_ccc_changed, proxy_ccc_write, NULL);
942 
943 static struct bt_gatt_attr proxy_attrs[] = {
944 	BT_GATT_PRIMARY_SERVICE(BT_UUID_MESH_PROXY),
945 
946 	BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROXY_DATA_IN,
947 			       BT_GATT_CHRC_WRITE_WITHOUT_RESP,
948 			       BT_GATT_PERM_WRITE,
949 			       NULL, gatt_recv, NULL),
950 
951 	BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROXY_DATA_OUT,
952 			       BT_GATT_CHRC_NOTIFY,
953 			       BT_GATT_PERM_NONE,
954 			       NULL, NULL, NULL),
955 	BT_GATT_CCC_MANAGED(&proxy_ccc,
956 			    BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
957 };
958 
959 static struct bt_gatt_service proxy_svc = BT_GATT_SERVICE(proxy_attrs);
960 static void svc_reg_work_handler(struct k_work *work);
961 static struct k_work_delayable svc_reg_work = Z_WORK_DELAYABLE_INITIALIZER(svc_reg_work_handler);
962 static uint32_t svc_reg_attempts;
963 
svc_reg_work_handler(struct k_work * work)964 static void svc_reg_work_handler(struct k_work *work)
965 {
966 	int err;
967 
968 	err = bt_gatt_service_register(&proxy_svc);
969 	if ((err == -EINVAL) && ((--svc_reg_attempts) > 0)) {
970 		/* settings_load() didn't finish yet. Try again. */
971 		(void)k_work_schedule(&svc_reg_work, PROXY_SVC_INIT_TIMEOUT);
972 		return;
973 	} else if (err) {
974 		LOG_ERR("Unable to register Mesh Proxy Service (err %d)", err);
975 		return;
976 	}
977 
978 	service_registered = true;
979 
980 	for (int i = 0; i < ARRAY_SIZE(clients); i++) {
981 		if (clients[i].cli) {
982 			clients[i].filter_type = ACCEPT;
983 		}
984 	}
985 
986 	bt_mesh_adv_gatt_update();
987 }
988 
bt_mesh_proxy_gatt_enable(void)989 int bt_mesh_proxy_gatt_enable(void)
990 {
991 	int err;
992 
993 	LOG_DBG("");
994 
995 	if (!bt_mesh_is_provisioned()) {
996 		return -ENOTSUP;
997 	}
998 
999 	if (service_registered) {
1000 		return -EBUSY;
1001 	}
1002 
1003 	svc_reg_attempts = PROXY_SVC_REG_ATTEMPTS;
1004 	err = k_work_schedule(&svc_reg_work, PROXY_SVC_INIT_TIMEOUT);
1005 	if (err < 0) {
1006 		LOG_ERR("Enabling GATT proxy failed (err %d)", err);
1007 		return err;
1008 	}
1009 
1010 	return 0;
1011 }
1012 
bt_mesh_proxy_gatt_disconnect(void)1013 void bt_mesh_proxy_gatt_disconnect(void)
1014 {
1015 	int i;
1016 
1017 	LOG_DBG("");
1018 
1019 	for (i = 0; i < ARRAY_SIZE(clients); i++) {
1020 		struct bt_mesh_proxy_client *client = &clients[i];
1021 
1022 		if (client->cli && (client->filter_type == ACCEPT ||
1023 				     client->filter_type == REJECT)) {
1024 			client->filter_type = NONE;
1025 			bt_conn_disconnect(client->cli->conn,
1026 					   BT_HCI_ERR_REMOTE_USER_TERM_CONN);
1027 		}
1028 	}
1029 }
1030 
bt_mesh_proxy_gatt_disable(void)1031 int bt_mesh_proxy_gatt_disable(void)
1032 {
1033 	LOG_DBG("");
1034 
1035 	if (!service_registered) {
1036 		return -EALREADY;
1037 	}
1038 
1039 	bt_mesh_proxy_gatt_disconnect();
1040 
1041 	bt_gatt_service_unregister(&proxy_svc);
1042 	service_registered = false;
1043 
1044 	return 0;
1045 }
1046 
bt_mesh_proxy_addr_add(struct net_buf_simple * buf,uint16_t addr)1047 void bt_mesh_proxy_addr_add(struct net_buf_simple *buf, uint16_t addr)
1048 {
1049 	struct bt_mesh_proxy_client *client;
1050 	struct bt_mesh_proxy_role *cli =
1051 		CONTAINER_OF(buf, struct bt_mesh_proxy_role, buf);
1052 
1053 	client = find_client(cli->conn);
1054 
1055 	LOG_DBG("filter_type %u addr 0x%04x", client->filter_type, addr);
1056 
1057 	if (client->filter_type == ACCEPT) {
1058 		filter_add(client, addr);
1059 	} else if (client->filter_type == REJECT) {
1060 		filter_remove(client, addr);
1061 	}
1062 }
1063 
client_filter_match(struct bt_mesh_proxy_client * client,uint16_t addr)1064 static bool client_filter_match(struct bt_mesh_proxy_client *client,
1065 				uint16_t addr)
1066 {
1067 	int i;
1068 
1069 	LOG_DBG("filter_type %u addr 0x%04x", client->filter_type, addr);
1070 
1071 	if (client->filter_type == REJECT) {
1072 		for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
1073 			if (client->filter[i] == addr) {
1074 				return false;
1075 			}
1076 		}
1077 
1078 		return true;
1079 	}
1080 
1081 	if (addr == BT_MESH_ADDR_ALL_NODES) {
1082 		return true;
1083 	}
1084 
1085 	if (client->filter_type == ACCEPT) {
1086 		for (i = 0; i < ARRAY_SIZE(client->filter); i++) {
1087 			if (client->filter[i] == addr) {
1088 				return true;
1089 			}
1090 		}
1091 	}
1092 
1093 	return false;
1094 }
1095 
bt_mesh_proxy_relay(struct bt_mesh_adv * adv,uint16_t dst)1096 bool bt_mesh_proxy_relay(struct bt_mesh_adv *adv, uint16_t dst)
1097 {
1098 	bool relayed = false;
1099 	int i;
1100 
1101 	LOG_DBG("%u bytes to dst 0x%04x", adv->b.len, dst);
1102 
1103 	for (i = 0; i < ARRAY_SIZE(clients); i++) {
1104 		struct bt_mesh_proxy_client *client = &clients[i];
1105 
1106 		if (!client->cli) {
1107 			continue;
1108 		}
1109 
1110 		if (!client_filter_match(client, dst)) {
1111 			continue;
1112 		}
1113 
1114 		if (bt_mesh_proxy_relay_send(client->cli->conn, adv)) {
1115 			continue;
1116 		}
1117 
1118 		relayed = true;
1119 	}
1120 
1121 	return relayed;
1122 }
1123 
solicitation_reset(struct bt_mesh_subnet * sub)1124 static void solicitation_reset(struct bt_mesh_subnet *sub)
1125 {
1126 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
1127 	sub->solicited = false;
1128 	sub->priv_net_id_sent = 0;
1129 #endif
1130 }
1131 
gatt_connected(struct bt_conn * conn,uint8_t conn_err)1132 static void gatt_connected(struct bt_conn *conn, uint8_t conn_err)
1133 {
1134 	struct bt_mesh_proxy_client *client;
1135 	struct bt_conn_info info;
1136 	int err;
1137 
1138 	err = bt_conn_get_info(conn, &info);
1139 	if (err || info.role != BT_CONN_ROLE_PERIPHERAL || !service_registered ||
1140 	    info.id != BT_ID_DEFAULT) {
1141 		return;
1142 	}
1143 
1144 	LOG_DBG("conn %p err 0x%02x", (void *)conn, conn_err);
1145 
1146 	client = find_client(conn);
1147 
1148 	client->filter_type = NONE;
1149 	(void)memset(client->filter, 0, sizeof(client->filter));
1150 	client->cli = bt_mesh_proxy_role_setup(conn, proxy_send,
1151 					       proxy_msg_recv);
1152 
1153 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
1154 	/* Binding from MshPRTv1.1: 7.2.2.2.6. */
1155 	enum bt_mesh_subnets_node_id_state cur_node_id = bt_mesh_subnets_node_id_state_get();
1156 
1157 	if (bt_mesh_gatt_proxy_get() == BT_MESH_FEATURE_ENABLED ||
1158 	    cur_node_id == BT_MESH_SUBNETS_NODE_ID_STATE_ENABLED) {
1159 		client->privacy = false;
1160 	} else {
1161 		client->privacy = (bt_mesh_priv_gatt_proxy_get() == BT_MESH_FEATURE_ENABLED) ||
1162 				  (cur_node_id == BT_MESH_SUBNETS_NODE_ID_STATE_ENABLED_PRIVATE);
1163 	}
1164 
1165 	LOG_DBG("privacy: %d", client->privacy);
1166 #endif
1167 
1168 	/* If connection was formed after Proxy Solicitation we need to stop future
1169 	 * Private Network ID advertisements
1170 	 */
1171 	bt_mesh_subnet_foreach(solicitation_reset);
1172 
1173 	/* Try to re-enable advertising in case it's possible */
1174 	if (bt_mesh_proxy_has_avail_conn()) {
1175 		bt_mesh_adv_gatt_update();
1176 	}
1177 }
1178 
gatt_disconnected(struct bt_conn * conn,uint8_t reason)1179 static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
1180 {
1181 	struct bt_conn_info info;
1182 	struct bt_mesh_proxy_client *client;
1183 	int err;
1184 
1185 	err = bt_conn_get_info(conn, &info);
1186 	if (err || info.role != BT_CONN_ROLE_PERIPHERAL || info.id != BT_ID_DEFAULT) {
1187 		return;
1188 	}
1189 
1190 	if (!service_registered && bt_mesh_is_provisioned()) {
1191 		(void)bt_mesh_proxy_gatt_enable();
1192 		return;
1193 	}
1194 
1195 	client = find_client(conn);
1196 	if (client->cli) {
1197 		bt_mesh_proxy_role_cleanup(client->cli);
1198 		client->cli = NULL;
1199 	}
1200 }
1201 
proxy_send(struct bt_conn * conn,const void * data,uint16_t len,bt_gatt_complete_func_t end,void * user_data)1202 static int proxy_send(struct bt_conn *conn,
1203 		      const void *data, uint16_t len,
1204 		      bt_gatt_complete_func_t end, void *user_data)
1205 {
1206 	LOG_DBG("%u bytes: %s", len, bt_hex(data, len));
1207 
1208 	struct bt_gatt_notify_params params = {
1209 		.data = data,
1210 		.len = len,
1211 		.attr = &proxy_attrs[3],
1212 		.user_data = user_data,
1213 		.func = end,
1214 	};
1215 
1216 	return bt_gatt_notify_cb(conn, &params);
1217 }
1218 
bt_mesh_proxy_adv_start(void)1219 int bt_mesh_proxy_adv_start(void)
1220 {
1221 	LOG_DBG("");
1222 
1223 	if (!service_registered || !bt_mesh_is_provisioned()) {
1224 		return -ENOTSUP;
1225 	}
1226 
1227 	return gatt_proxy_advertise();
1228 }
1229 
1230 BT_CONN_CB_DEFINE(conn_callbacks) = {
1231 	.connected = gatt_connected,
1232 	.disconnected = gatt_disconnected,
1233 };
1234 
bt_mesh_proxy_srv_connected_cnt(void)1235 uint8_t bt_mesh_proxy_srv_connected_cnt(void)
1236 {
1237 	uint8_t cnt = 0;
1238 
1239 	for (int i = 0; i < ARRAY_SIZE(clients); i++) {
1240 		if (clients[i].cli) {
1241 			cnt++;
1242 		}
1243 	}
1244 
1245 	return cnt;
1246 }
1247