1 // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _PROVISIONER_PROV_H_
16 #define _PROVISIONER_PROV_H_
17 
18 #include "bluetooth/hci.h"
19 #include "bluetooth/conn.h"
20 
21 #include "api/mesh/main.h"
22 
23 #ifndef CONFIG_BT_MESH_PROVISIONER
24 
25 #define CONFIG_BT_MESH_PBA_SAME_TIME 0
26 #define CONFIG_BT_MESH_PBG_SAME_TIME 0
27 
28 #else
29 
30 #if !defined(CONFIG_BT_MESH_PB_ADV)
31 #define CONFIG_BT_MESH_PBA_SAME_TIME 0
32 #else
33 #define CONFIG_BT_MESH_PBA_SAME_TIME 1
34 #endif /* !CONFIG_BT_MESH_PB_ADV */
35 
36 #if !defined(CONFIG_BT_MESH_PB_GATT)
37 #define CONFIG_BT_MESH_PBG_SAME_TIME 0
38 #else
39 #define CONFIG_BT_MESH_PBG_SAME_TIME 1
40 #endif /* !CONFIG_BT_MESH_PB_GATT */
41 
42 #define BT_DATA_FLAGS 0x01
43 #define BT_DATA_SERVICE_UUID 0x03
44 #define BT_DATA_SERVICE_DATA 0X16
45 
46 #define RM_AFTER_PROV BIT(0)
47 #define START_PROV_NOW BIT(1)
48 #define FLUSHABLE_DEV BIT(2)
49 
50 #define BT_UUID_MESH_PROV_VAL (0x1827)
51 #define BT_UUID_MESH_PROXY_VAL (0x1828)
52 
53 typedef struct _auto_appkey_config {
54     u8_t auto_add_appkey;
55     u16_t auto_appkey_idx;
56 } auto_appkey_config;
57 
58 struct bt_mesh_unprov_dev_add {
59     u8_t addr[6];
60     u8_t addr_type;
61     u8_t uuid[16];
62     u8_t bearer;
63     u16_t oob_info;
64     uint8_t auto_add_appkey;
65 };
66 
67 struct bt_mesh_device_delete {
68     u8_t addr[6];
69     u8_t addr_type;
70     u8_t uuid[16];
71 };
72 
73 #define NET_IDX_FLAG BIT(0)
74 #define FLAGS_FLAG BIT(1)
75 #define IV_INDEX_FLAG BIT(2)
76 
77 struct bt_mesh_prov_data_info {
78     union {
79         u16_t net_idx;
80         u8_t flags;
81         bt_u32_t iv_index;
82     };
83     u8_t flag;
84 };
85 
86 struct bt_conn;
87 struct bt_le_conn_param;
88 
89 struct node_info {
90     bool provisioned; /* device provisioned flag */
91     bt_addr_le_t addr; /* device address */
92     u8_t uuid[16]; /* node uuid */
93     u16_t oob_info; /* oob info contained in adv pkt */
94     u8_t element_num; /* element contained in this node */
95     u16_t unicast_addr; /* primary unicast address of this node */
96     u16_t net_idx; /* Netkey index got during provisioning */
97     u8_t flags; /* Key refresh flag and iv update flag */
98     bt_u32_t iv_index; /* IV Index */
99     bt_u32_t provisioned_time; /* provison time */
100     uint8_t auto_add_appkey;
101     ; /*node auto add appkey flag*/
102 };
103 
104 #ifdef CONFIG_BT_MESH_PROVISIONER
105 struct bt_prov_conn_cb {
106     void (*connected)(struct bt_conn *conn, int id);
107 
108     void (*disconnected)(struct bt_conn *conn, u8_t reason);
109 
110     size_t (*prov_write_descr)(struct bt_conn *conn, u8_t *addr);
111 
112     size_t (*prov_notify)(struct bt_conn *conn, u8_t *data, u16_t len);
113 
114     size_t (*proxy_write_descr)(struct bt_conn *conn);
115 
116     size_t (*proxy_notify)(struct bt_conn *conn, u8_t *data, u16_t len);
117 
118     bool (*le_param_req)(struct bt_conn *conn, struct bt_le_conn_param *param);
119 
120     void (*le_param_updated)(struct bt_conn *conn, u16_t interval, u16_t latency, u16_t timeout);
121 
122 #if defined(CONFIG_BT_SMP)
123     void (*identity_resolved)(struct bt_conn *conn, const bt_addr_le_t *rpa, const bt_addr_le_t *identity);
124 #endif /* CONFIG_BT_SMP */
125 
126 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
127     void (*security_changed)(struct bt_conn *conn, bt_security_t level);
128 #endif /* defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR) */
129 
130     struct bt_conn_cb *_next;
131 };
132 #endif /* CONFIG_BT_MESH_PROVISIONER */
133 
134 /* The following APIs are for primary provisioner internal use */
135 
136 /**
137  * @brief This function is called to decrement the current PB-GATT count.
138  *
139  * @return None
140  */
141 void provisioner_pbg_count_dec(void);
142 
143 /**
144  * @brief This function is called to increment the current PB-GATT count.
145  *
146  * @return None
147  */
148 void provisioner_pbg_count_inc(void);
149 
150 /**
151  * @brief This function is called to clear part of the link
152  *        info of the proper device.
153  *
154  * @param[in] index: Index of link within the array.
155  *
156  * @return None
157  */
158 void provisioner_clear_connecting(int index);
159 
160 /**
161  * @brief This function is called to deal with the received PB-ADV pdus.
162  *
163  * @param[in] buf: Pointer to the buffer containing generic provisioning pdus
164  *
165  * @return Zero-success, other-fail
166  */
167 void provisioner_pb_adv_recv(struct net_buf_simple *buf);
168 
169 /**
170  * @brief This function is called to send provisioning invite to start
171  *        provisioning this unprovisioned device.
172  *
173  * @param[in] conn:  Pointer to the bt_conn structure
174  * @param[in] index: The 'index' link whose conn will be set
175  *
176  * @return Zero-success, other-fail
177  */
178 int provisioner_set_prov_conn(struct bt_conn *conn, int index);
179 
180 /**
181  * @brief This function is called to send provisioning invite to start
182  *        provisioning this unprovisioned device.
183  *
184  * @param[in] conn: Pointer to the bt_conn structure
185  * @param[in] addr: Address of the connected device
186  *
187  * @return Zero-success, other-fail
188  */
189 int provisioner_pb_gatt_open(struct bt_conn *conn, u8_t *addr);
190 
191 /**
192  * @brief This function is called to reset the used information when
193  *        related connection is terminated.
194  *
195  * @param[in] conn:   Pointer to the bt_conn structure
196  * @param[in] reason: Connection terminated reason
197  *
198  * @return Zero-success, other-fail
199  */
200 int provisioner_pb_gatt_close(struct bt_conn *conn, u8_t reason);
201 
202 /**
203  * @brief This function is called to deal with the received PB-GATT provision
204  *        pdus.
205  *
206  * @param[in] conn: Pointer to the bt_conn structure
207  * @param[in] buf:  Pointer to the buffer containing provision pdus
208  *
209  * @return Zero-success, other-fail
210  */
211 int provisioner_pb_gatt_recv(struct bt_conn *conn, struct net_buf_simple *buf);
212 
213 /**
214  * @brief This function is called to initialize provisioner's PB-GATT and PB-ADV
215  *        related informations.
216  *
217  * @param[in] prov_info: Pointer to the application-initialized provisioner info.
218  *
219  * @return Zero-success, other-fail
220  */
221 int provisioner_prov_init(const struct bt_mesh_provisioner *provisioner_info);
222 
223 /**
224  * @brief This function is called to parse the received unprovisioned device
225  *        beacon adv pkts, and if checked, start to provision this device
226  *        using PB-ADV bearer.
227  *
228  * @param[in] buf: Pointer to the buffer containing unprovisioned device beacon
229  *
230  * @return None
231  */
232 void provisioner_unprov_beacon_recv(struct net_buf_simple *buf);
233 
234 /**
235  * @brief This function is called to parse the flags part of the
236  *        received connectable mesh provisioning adv pkts.
237  *
238  * @param[in] buf: Pointer to the buffer containing adv flags part
239  *
240  * @return True-success, False-fail
241  */
242 bool provisioner_flags_match(struct net_buf_simple *buf);
243 
244 /**
245  * @brief This function is called to parse the service uuid part of the
246  *        received connectable mesh provisioning adv pkts.
247  *
248  * @param[in] buf: Pointer to the buffer containing service uuid part
249  *
250  * @return Zero-fail, other-Service UUID(0x1827 or 0x1828)
251  */
252 u16_t provisioner_srv_uuid_recv(struct net_buf_simple *buf);
253 
254 /**
255  * @brief This function is called to parse the service data part of the
256  *        received connectable mesh provisioning adv pkts.
257  *
258  * @param[in] buf:  Pointer to the buffer containing remianing service data part
259  * @param[in] addr: Pointer to the received device address
260  * @param[in] uuid: Service UUID contained in the service uuid part
261  *
262  * @return None
263  */
264 void provisioner_srv_data_recv(struct net_buf_simple *buf, const bt_addr_le_t *addr, u16_t uuid);
265 
266 /**
267  * @brief This function is called to get bt_mesh_prov pointer.
268  *
269  * @return bt_mesh_prov pointer(prov)
270  */
271 const struct bt_mesh_provisioner *provisioner_get_prov_info(void);
272 
273 /**
274  * @brief This function is called to reset all nodes information in provisioner_prov.c.
275  *
276  * @return Zero
277  */
278 int provisioner_prov_reset_all_nodes(void);
279 
280 /* The following APIs are for primary provisioner application use */
281 
282 /** @brief Add unprovisioned device info to unprov_dev queue
283  *
284  *  @param[in] add_dev: Pointer to the structure containing the device information
285  *  @param[in] flags:   Flags indicate several oprations of the device information
286  *                       - Remove device information from queue after been provisioned (BIT0)
287  *                       - Start provisioning at once when device is added to queue (BIT1)
288  *                       - Device can be flushed when device queue is full (BIT2)
289  *
290  *  @return Zero on success or (negative) error code otherwise.
291  *
292  *  @Note: 1. Currently address type only supports public address and static random address.
293  *         2. If device UUID and/or device address and address type already exist in the
294  *            device queue, but the bearer is different with the existing one, add operation
295  *            will also be successful and it will update the provision bearer supported by
296  *            the device.
297  */
298 int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, u8_t flags);
299 
300 /** @brief Delete device from queue, reset current provisioning link and reset the node
301  *
302  *  @param[in] del_dev: Pointer to the structure containing the device information
303  *
304  *  @return Zero on success or (negative) error code otherwise.
305  */
306 int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev);
307 
308 /** @brief Delete unprov device from queue, reset current provisioning link and reset the node
309  *
310  *  @param[in] del_dev: Pointer to the structure containing the device information
311  *
312  *  @return Zero on success or (negative) error code otherwise.
313  */
314 
315 int bt_mesh_provisioner_delete_unprov_device(struct bt_mesh_device_delete *del_dev);
316 
317 /**
318  * @brief This function is called to set part of the device uuid to be compared before
319  *        starting to provision the device.
320  *
321  * @param[in] offset: offset of the device uuid to be compared
322  * @param[in] length: length of the device uuid to be compared
323  * @param[in] match:  value to be compared
324  * @param[in] prov_flag: flags indicate when received uuid_match adv pkts, the device
325  *                       will be provisioned at once or report to application layer
326  *
327  * @return 0 - success, others - fail
328  */
329 int bt_mesh_provisioner_set_dev_uuid_match(u8_t offset, u8_t length, const u8_t *match, bool prov_flag);
330 
331 /** @brief Callback for provisioner received adv pkt from unprovisioned devices which are
332  *  not in the unprovisioned device queue.
333  *
334  *  Notify the unprovisioned device beacon and mesh provisioning service adv data to application
335  *
336  *  @param addr      Unprovisioned device address pointer
337  *  @param addr_type Unprovisioned device address type
338  *  @param dev_uuid  Unprovisioned device device UUID pointer
339  *  @param bearer    Adv packet received from PB-GATT or PB-ADV bearer
340  *  @param adv_type  Adv packet type, currently this is not used and we can use bearer to device
341  *                   the adv_type(ADV_IND or ADV_NONCONN_IND). Later when we support scan response
342  *                   data, this parameter will be used.
343  *
344  */
345 typedef void (*prov_adv_pkt_cb)(const u8_t addr[6], const u8_t addr_type, const u8_t adv_type, const u8_t dev_uuid[16],
346                                 u16_t oob_info, bt_mesh_prov_bearer_t bearer);
347 
348 /**
349  * @brief This function is called to register the callback which notifies the
350  *        application layer of the received mesh provisioning or unprovisioned
351  *        device beacon adv pkts(from devices not in the unprov device queue).
352  *
353  * @param[in] cb: Callback of the notifying adv pkts function
354  *
355  * @return Zero-success, other-fail
356  */
357 int bt_mesh_prov_adv_pkt_cb_register(prov_adv_pkt_cb cb);
358 
359 /**
360  * @brief This function is called to change net_idx or flags or iv_index used in provisioning data.
361  *
362  * @param[in] info: Pointer of structure containing net_idx or flags or iv_index
363  *
364  * @return 0 - success, others - fail
365  */
366 int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info);
367 
368 /* The following APIs are for temporary provisioner internal/application(bt_mesh_xxx) use */
369 
370 /**
371  * @brief This function is called to set temp_prov_flag.
372  *
373  * @param[in] flag: Flag set to temp_prov_flag
374  *
375  * @return None
376  */
377 void provisioner_temp_prov_flag_set(bool flag);
378 
379 /**
380  * @brief This function is called to set unicast address range used for temp prov.
381  *
382  * @param[in] min: Minimum unicast address
383  * @param[in] max: Maximum unicast address
384  *
385  * @return status for set unicast address range msg
386  */
387 u8_t bt_mesh_temp_prov_set_unicast_addr(u16_t min, u16_t max);
388 
389 /**
390  * @brief This function is called to set flags & iv_index used for temp prov.
391  *
392  * @param[in] flags:    Key refresh flag and iv update flag
393  * @param[in] iv_index: IV index
394  *
395  * @return 0 - success
396  */
397 int bt_mesh_temp_prov_set_flags_iv_index(u8_t flags, bt_u32_t iv_index);
398 
399 /**
400  * @brief This function is called to set netkey index used for temp prov.
401  *
402  * @param[in] net_key: Netkey pointer
403  * @param[in] net_idx: Netkey index
404  *
405  * @return status for set netkey index msg
406  */
407 u8_t provisioner_temp_prov_set_net_idx(const u8_t *net_key, u16_t net_idx);
408 
409 int bt_mesh_prov_input_data(u8_t *num, u8_t size, bool num_flag);
410 int bt_mesh_prov_output_data(u8_t *num, u8_t size, bool num_flag);
411 int provisioner_clear_prov_conn(struct bt_conn *conn);
412 int provisioner_prov_restore_nodes_info(bt_addr_le_t *addr, u8_t uuid[16], u16_t oob_info, u8_t element_num,
413                                         u16_t unicast_addr, u16_t net_idx, u8_t flags, bt_u32_t iv_index,
414                                         u8_t dev_key[16], bt_u32_t provisioned_time);
415 auto_appkey_config *get_node_autoconfig_info(u16_t unicast_addr);
416 
417 int bt_mesh_provisioner_local_provision();
418 
419 int bt_mesh_provisioner_add_node(struct node_info *node_info, uint8_t dev_key[16]);
420 
421 #endif /* CONFIG_BT_MESH_PROVISIONER */
422 
423 #endif /* #ifndef _PROVISIONER_PROV_H_ */
424