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 
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 
17 #include "common/bt_str.h"
18 
19 #include "mesh.h"
20 #include "net.h"
21 #include "rpl.h"
22 #include "transport.h"
23 #include "prov.h"
24 #include "pb_gatt.h"
25 #include "beacon.h"
26 #include "foundation.h"
27 #include "access.h"
28 #include "proxy.h"
29 #include "proxy_msg.h"
30 #include "pb_gatt_srv.h"
31 
32 #define LOG_LEVEL CONFIG_BT_MESH_PROV_LOG_LEVEL
33 #include <zephyr/logging/log.h>
34 LOG_MODULE_REGISTER(bt_mesh_pb_gatt_srv);
35 
36 #define ADV_OPT_PROV                                                           \
37 	(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_SCANNABLE |                 \
38 	 BT_LE_ADV_OPT_ONE_TIME | ADV_OPT_USE_IDENTITY)
39 
40 #define FAST_ADV_TIME (60LL * MSEC_PER_SEC)
41 
42 static int64_t fast_adv_timestamp;
43 
44 static int gatt_send(struct bt_conn *conn,
45 		     const void *data, uint16_t len,
46 		     bt_gatt_complete_func_t end, void *user_data);
47 
48 static struct bt_mesh_proxy_role *cli;
49 static bool service_registered;
50 
proxy_msg_recv(struct bt_mesh_proxy_role * role)51 static void proxy_msg_recv(struct bt_mesh_proxy_role *role)
52 {
53 	switch (role->msg_type) {
54 	case BT_MESH_PROXY_PROV:
55 		LOG_DBG("Mesh Provisioning PDU");
56 		bt_mesh_pb_gatt_recv(role->conn, &role->buf);
57 		break;
58 
59 	default:
60 		LOG_WRN("Unhandled Message Type 0x%02x", role->msg_type);
61 		break;
62 	}
63 }
64 
gatt_recv(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)65 static ssize_t gatt_recv(struct bt_conn *conn,
66 			 const struct bt_gatt_attr *attr, const void *buf,
67 			 uint16_t len, uint16_t offset, uint8_t flags)
68 {
69 	const uint8_t *data = buf;
70 
71 	if (cli->conn != conn) {
72 		LOG_ERR("No PB-GATT Client found");
73 		return -ENOTCONN;
74 	}
75 
76 	if (len < 1) {
77 		LOG_WRN("Too small Proxy PDU");
78 		return -EINVAL;
79 	}
80 
81 	if (PDU_TYPE(data) != BT_MESH_PROXY_PROV) {
82 		LOG_WRN("Proxy PDU type doesn't match GATT service");
83 		return -EINVAL;
84 	}
85 
86 	return bt_mesh_proxy_msg_recv(conn, buf, len);
87 }
88 
gatt_connected(struct bt_conn * conn,uint8_t conn_err)89 static void gatt_connected(struct bt_conn *conn, uint8_t conn_err)
90 {
91 	struct bt_conn_info info;
92 	int err;
93 
94 	err = bt_conn_get_info(conn, &info);
95 	if (err || info.role != BT_CONN_ROLE_PERIPHERAL || !service_registered ||
96 	    bt_mesh_is_provisioned() || info.id != BT_ID_DEFAULT || cli)  {
97 		return;
98 	}
99 
100 	cli = bt_mesh_proxy_role_setup(conn, gatt_send, proxy_msg_recv);
101 
102 	LOG_DBG("conn %p err 0x%02x", (void *)conn, conn_err);
103 }
104 
gatt_disconnected(struct bt_conn * conn,uint8_t reason)105 static void gatt_disconnected(struct bt_conn *conn, uint8_t reason)
106 {
107 	struct bt_conn_info info;
108 	int err;
109 
110 	err = bt_conn_get_info(conn, &info);
111 	if (err || info.role != BT_CONN_ROLE_PERIPHERAL || !service_registered ||
112 	    info.id != BT_ID_DEFAULT || !cli || cli->conn != conn) {
113 		return;
114 	}
115 
116 	bt_mesh_proxy_role_cleanup(cli);
117 	cli = NULL;
118 
119 	bt_mesh_pb_gatt_close(conn);
120 
121 	if (bt_mesh_is_provisioned()) {
122 		(void)bt_mesh_pb_gatt_srv_disable();
123 	}
124 }
125 
prov_ccc_changed(const struct bt_gatt_attr * attr,uint16_t value)126 static void prov_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value)
127 {
128 	LOG_DBG("value 0x%04x", value);
129 }
130 
prov_ccc_write(struct bt_conn * conn,const struct bt_gatt_attr * attr,uint16_t value)131 static ssize_t prov_ccc_write(struct bt_conn *conn,
132 			      const struct bt_gatt_attr *attr, uint16_t value)
133 {
134 	if (cli->conn != conn) {
135 		LOG_ERR("No PB-GATT Client found");
136 		return -ENOTCONN;
137 	}
138 
139 	LOG_DBG("value 0x%04x", value);
140 
141 	if (value != BT_GATT_CCC_NOTIFY) {
142 		LOG_WRN("Client wrote 0x%04x instead enabling notify", value);
143 		return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
144 	}
145 
146 	bt_mesh_pb_gatt_start(conn);
147 
148 	return sizeof(value);
149 }
150 
151 /* Mesh Provisioning Service Declaration */
152 static struct _bt_gatt_ccc prov_ccc =
153 	BT_GATT_CCC_INITIALIZER(prov_ccc_changed, prov_ccc_write, NULL);
154 
155 static struct bt_gatt_attr prov_attrs[] = {
156 	BT_GATT_PRIMARY_SERVICE(BT_UUID_MESH_PROV),
157 
158 	BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROV_DATA_IN,
159 			       BT_GATT_CHRC_WRITE_WITHOUT_RESP,
160 			       BT_GATT_PERM_WRITE, NULL, gatt_recv,
161 			       NULL),
162 
163 	BT_GATT_CHARACTERISTIC(BT_UUID_MESH_PROV_DATA_OUT,
164 			       BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE,
165 			       NULL, NULL, NULL),
166 	BT_GATT_CCC_MANAGED(&prov_ccc,
167 			    BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
168 };
169 
170 static struct bt_gatt_service prov_svc = BT_GATT_SERVICE(prov_attrs);
171 
bt_mesh_pb_gatt_srv_enable(void)172 int bt_mesh_pb_gatt_srv_enable(void)
173 {
174 	LOG_DBG("");
175 
176 	if (bt_mesh_is_provisioned()) {
177 		return -ENOTSUP;
178 	}
179 
180 	if (service_registered) {
181 		return -EBUSY;
182 	}
183 
184 	(void)bt_gatt_service_register(&prov_svc);
185 	service_registered = true;
186 	fast_adv_timestamp = k_uptime_get();
187 
188 	return 0;
189 }
190 
bt_mesh_pb_gatt_srv_disable(void)191 int bt_mesh_pb_gatt_srv_disable(void)
192 {
193 	LOG_DBG("");
194 
195 	if (!service_registered) {
196 		return -EALREADY;
197 	}
198 
199 	bt_gatt_service_unregister(&prov_svc);
200 	service_registered = false;
201 
202 	bt_mesh_adv_gatt_update();
203 
204 	return 0;
205 }
206 
207 static uint8_t prov_svc_data[20] = {
208 	BT_UUID_16_ENCODE(BT_UUID_MESH_PROV_VAL),
209 };
210 
211 static const struct bt_data prov_ad[] = {
212 	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
213 	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
214 		      BT_UUID_16_ENCODE(BT_UUID_MESH_PROV_VAL)),
215 	BT_DATA(BT_DATA_SVC_DATA16, prov_svc_data, sizeof(prov_svc_data)),
216 };
217 
gatt_prov_adv_create(struct bt_data prov_sd[2])218 static size_t gatt_prov_adv_create(struct bt_data prov_sd[2])
219 {
220 	size_t prov_sd_len = 0;
221 
222 	const struct bt_mesh_prov *prov = bt_mesh_prov_get();
223 	size_t uri_len;
224 
225 	memcpy(prov_svc_data + 2, prov->uuid, 16);
226 	sys_put_be16(prov->oob_info, prov_svc_data + 18);
227 
228 	if (!prov->uri) {
229 		goto dev_name;
230 	}
231 
232 	uri_len = strlen(prov->uri);
233 	if (uri_len > 29) {
234 		/* There's no way to shorten an URI */
235 		LOG_WRN("Too long URI to fit advertising packet");
236 		goto dev_name;
237 	}
238 
239 	prov_sd[prov_sd_len].type = BT_DATA_URI;
240 	prov_sd[prov_sd_len].data_len = uri_len;
241 	prov_sd[prov_sd_len].data = (const uint8_t *)prov->uri;
242 
243 	prov_sd_len += 1;
244 
245 dev_name:
246 #if defined(CONFIG_BT_MESH_PB_GATT_USE_DEVICE_NAME)
247 	prov_sd[prov_sd_len].type = BT_DATA_NAME_COMPLETE;
248 	prov_sd[prov_sd_len].data_len = sizeof(CONFIG_BT_DEVICE_NAME) - 1;
249 	prov_sd[prov_sd_len].data = CONFIG_BT_DEVICE_NAME;
250 
251 	prov_sd_len += 1;
252 #endif
253 
254 	return prov_sd_len;
255 }
256 
gatt_send(struct bt_conn * conn,const void * data,uint16_t len,bt_gatt_complete_func_t end,void * user_data)257 static int gatt_send(struct bt_conn *conn,
258 		     const void *data, uint16_t len,
259 		     bt_gatt_complete_func_t end, void *user_data)
260 {
261 	LOG_DBG("%u bytes: %s", len, bt_hex(data, len));
262 
263 	struct bt_gatt_notify_params params = {
264 		.data = data,
265 		.len = len,
266 		.attr = &prov_attrs[3],
267 		.user_data = user_data,
268 		.func = end,
269 	};
270 
271 	return bt_gatt_notify_cb(conn, &params);
272 }
273 
bt_mesh_pb_gatt_srv_adv_start(void)274 int bt_mesh_pb_gatt_srv_adv_start(void)
275 {
276 	LOG_DBG("");
277 
278 	if (!service_registered || bt_mesh_is_provisioned() ||
279 	    !bt_mesh_proxy_has_avail_conn()) {
280 		return -ENOTSUP;
281 	}
282 
283 	struct bt_le_adv_param fast_adv_param = {
284 		.id = BT_ID_DEFAULT,
285 		.options = ADV_OPT_PROV,
286 		ADV_FAST_INT,
287 	};
288 	struct bt_data prov_sd[2];
289 	size_t prov_sd_len;
290 	int64_t timestamp = fast_adv_timestamp;
291 	int64_t elapsed_time = k_uptime_delta(&timestamp);
292 
293 	prov_sd_len = gatt_prov_adv_create(prov_sd);
294 
295 	if (elapsed_time > FAST_ADV_TIME) {
296 		struct bt_le_adv_param slow_adv_param = {
297 			.id = BT_ID_DEFAULT,
298 			.options = ADV_OPT_PROV,
299 			ADV_SLOW_INT,
300 		};
301 
302 		return bt_mesh_adv_gatt_start(&slow_adv_param, SYS_FOREVER_MS, prov_ad,
303 					      ARRAY_SIZE(prov_ad), prov_sd, prov_sd_len);
304 	}
305 
306 	LOG_DBG("remaining fast adv time (%lld ms)", (FAST_ADV_TIME - elapsed_time));
307 	/* Advertise 60 seconds using fast interval */
308 	return bt_mesh_adv_gatt_start(&fast_adv_param, (FAST_ADV_TIME - elapsed_time),
309 				      prov_ad, ARRAY_SIZE(prov_ad),
310 				      prov_sd, prov_sd_len);
311 
312 }
313 
314 BT_CONN_CB_DEFINE(conn_callbacks) = {
315 	.connected = gatt_connected,
316 	.disconnected = gatt_disconnected,
317 };
318