1 // Copyright 2017-2019 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 _BTC_BLE_MESH_PROV_H_
16 #define _BTC_BLE_MESH_PROV_H_
17 
18 #include "btc/btc_manage.h"
19 #include "mesh_byteorder.h"
20 #include "mesh_config.h"
21 #include "mesh_main.h"
22 #include "fast_prov.h"
23 #include "provisioner_prov.h"
24 #include "esp_ble_mesh_defs.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 typedef enum {
31     BTC_BLE_MESH_ACT_MESH_INIT = 0,
32     BTC_BLE_MESH_ACT_PROV_ENABLE,
33     BTC_BLE_MESH_ACT_PROV_DISABLE,
34     BTC_BLE_MESH_ACT_NODE_RESET,
35     BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY,
36     BTC_BLE_MESH_ACT_INPUT_NUMBER,
37     BTC_BLE_MESH_ACT_INPUT_STRING,
38     BTC_BLE_MESH_ACT_SET_DEVICE_NAME,
39     BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE,
40     BTC_BLE_MESH_ACT_PROXY_GATT_ENABLE,
41     BTC_BLE_MESH_ACT_PROXY_GATT_DISABLE,
42     BTC_BLE_MESH_ACT_NODE_ADD_LOCAL_NET_KEY,
43     BTC_BLE_MESH_ACT_NODE_ADD_LOCAL_APP_KEY,
44     BTC_BLE_MESH_ACT_NODE_BIND_APP_KEY_TO_MODEL,
45     BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY,
46     BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR,
47     BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM,
48     BTC_BLE_MESH_ACT_PROVISIONER_ENABLE,
49     BTC_BLE_MESH_ACT_PROVISIONER_DISABLE,
50     BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD,
51     BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR,
52     BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL,
53     BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH,
54     BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO,
55     BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL,
56     BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR,
57     BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME,
58     BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY,
59     BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY,
60     BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP,
61     BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY,
62     BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY,
63     BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA,
64     BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID,
65     BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR,
66     BTC_BLE_MESH_ACT_PROVISIONER_ENABLE_HEARTBEAT_RECV,
67     BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE,
68     BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_INFO,
69     BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS,
70     BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_INDEX,
71     BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_UID,
72     BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_INDEX,
73     BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_UID,
74     BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_INDEX,
75     BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_UID,
76     BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO,
77     BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION,
78     BTC_BLE_MESH_ACT_LPN_ENABLE,
79     BTC_BLE_MESH_ACT_LPN_DISABLE,
80     BTC_BLE_MESH_ACT_LPN_POLL,
81     BTC_BLE_MESH_ACT_PROXY_CLIENT_CONNECT,
82     BTC_BLE_MESH_ACT_PROXY_CLIENT_DISCONNECT,
83     BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE,
84     BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR,
85     BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR,
86     BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR,
87     BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR,
88     BTC_BLE_MESH_ACT_DEINIT_MESH,
89 } btc_ble_mesh_prov_act_t;
90 
91 typedef enum {
92     BTC_BLE_MESH_ACT_MODEL_PUBLISH,
93     BTC_BLE_MESH_ACT_SERVER_MODEL_SEND,
94     BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND,
95     BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE,
96 } btc_ble_mesh_model_act_t;
97 
98 typedef union {
99     struct ble_mesh_init_args {
100         esp_ble_mesh_prov_t *prov;
101         esp_ble_mesh_comp_t *comp;
102         SemaphoreHandle_t semaphore;
103     } mesh_init;
104     struct ble_mesh_node_prov_enable_args {
105         esp_ble_mesh_prov_bearer_t bearers;
106     } node_prov_enable;
107     struct ble_mesh_node_prov_disable_args {
108         esp_ble_mesh_prov_bearer_t bearers;
109     } node_prov_disable;
110     struct ble_mesh_set_oob_pub_key_args {
111         uint8_t pub_key_x[32];
112         uint8_t pub_key_y[32];
113         uint8_t private_key[32];
114     } set_oob_pub_key;
115     struct ble_mesh_node_input_num_args {
116         uint32_t number;
117     } input_number;
118     struct ble_mesh_node_input_str_args {
119         char string[8];
120     } input_string;
121     struct ble_mesh_set_device_name_args {
122         char name[ESP_BLE_MESH_DEVICE_NAME_MAX_LEN + 1];
123     } set_device_name;
124     struct ble_mesh_node_add_local_net_key_args {
125         uint8_t  net_key[16];
126         uint16_t net_idx;
127     } node_add_local_net_key;
128     struct ble_mesh_node_add_local_app_key_args {
129         uint8_t  app_key[16];
130         uint16_t net_idx;
131         uint16_t app_idx;
132     } node_add_local_app_key;
133     struct ble_mesh_node_bind_local_mod_app_args {
134         uint16_t element_addr;
135         uint16_t company_id;
136         uint16_t model_id;
137         uint16_t app_idx;
138     } node_local_mod_app_bind;
139     struct ble_mesh_provisioner_read_oob_pub_key_args {
140         uint8_t link_idx;
141         uint8_t pub_key_x[32];
142         uint8_t pub_key_y[32];
143     } provisioner_read_oob_pub_key;
144     struct ble_mesh_provisioner_input_str_args {
145         char string[8];
146         uint8_t link_idx;
147     } provisioner_input_str;
148     struct ble_mesh_provisioner_input_num_args {
149         uint32_t number;
150         uint8_t  link_idx;
151     } provisioner_input_num;
152     struct ble_mesh_provisioner_enable_args {
153         esp_ble_mesh_prov_bearer_t bearers;
154     } provisioner_enable;
155     struct ble_mesh_provisioner_disable_args {
156         esp_ble_mesh_prov_bearer_t bearers;
157     } provisioner_disable;
158     struct ble_mesh_provisioner_dev_add_args {
159         esp_ble_mesh_unprov_dev_add_t add_dev;
160         esp_ble_mesh_dev_add_flag_t   flags;
161     } provisioner_dev_add;
162     struct ble_mesh_provisioner_prov_dev_with_addr_args {
163         uint8_t uuid[16];
164         esp_ble_mesh_bd_addr_t addr;
165         esp_ble_mesh_addr_type_t addr_type;
166         esp_ble_mesh_prov_bearer_t bearer;
167         uint16_t oob_info;
168         uint16_t unicast_addr;
169     } provisioner_prov_dev_with_addr;
170     struct ble_mesh_provisioner_dev_del_args {
171         esp_ble_mesh_device_delete_t del_dev;
172     } provisioner_dev_del;
173     struct ble_mesh_provisioner_set_dev_uuid_match_args {
174         uint8_t offset;
175         uint8_t match_len;
176         uint8_t match_val[16];
177         bool prov_after_match;
178     } set_dev_uuid_match;
179     struct ble_mesh_provisioner_set_prov_net_idx_args {
180         esp_ble_mesh_prov_data_info_t prov_data;
181     } set_prov_data_info;
182     struct ble_mesh_provisioner_set_static_oob_val_args {
183         uint8_t value[16];
184         uint8_t length;
185     } set_static_oob_val;
186     struct ble_mesh_provisioner_set_primary_elem_addr_args {
187         uint16_t addr;
188     } set_primary_elem_addr;
189     struct ble_mesh_provisioner_set_node_name_args {
190         uint16_t index;
191         char name[ESP_BLE_MESH_NODE_NAME_MAX_LEN + 1];
192     } set_node_name;
193     struct ble_mesh_provisioner_add_local_app_key_args {
194         uint8_t app_key[16];
195         uint16_t net_idx;
196         uint16_t app_idx;
197     } add_local_app_key;
198     struct ble_mesh_provisioner_update_local_app_key_args {
199         uint8_t app_key[16];
200         uint16_t net_idx;
201         uint16_t app_idx;
202     } update_local_app_key;
203     struct ble_mesh_provisioner_bind_local_mod_app_args {
204         uint16_t elem_addr;
205         uint16_t model_id;
206         uint16_t cid;
207         uint16_t app_idx;
208     } local_mod_app_bind;
209     struct ble_mesh_provisioner_add_local_net_key_args {
210         uint8_t net_key[16];
211         uint16_t net_idx;
212     } add_local_net_key;
213     struct ble_mesh_provisioner_update_local_net_key_args {
214         uint8_t net_key[16];
215         uint16_t net_idx;
216     } update_local_net_key;
217     struct ble_mesh_provisioner_store_node_comp_data_args {
218         uint16_t unicast_addr;
219         uint16_t length;
220         uint8_t *data;
221     } store_node_comp_data;
222     struct ble_mesh_provisioner_delete_node_with_uuid_args {
223         uint8_t uuid[16];
224     } delete_node_with_uuid;
225     struct ble_mesh_provisioner_delete_node_with_addr_args {
226         uint16_t unicast_addr;
227     } delete_node_with_addr;
228     struct {
229         bool enable;
230     } enable_heartbeat_recv;
231     struct {
232         uint8_t type;
233     } set_heartbeat_filter_type;
234     struct {
235         uint8_t  op;
236         uint16_t hb_src;
237         uint16_t hb_dst;
238     } set_heartbeat_filter_info;
239     struct {
240         uint8_t index;
241     } open_settings_with_index;
242     struct {
243         char uid[ESP_BLE_MESH_SETTINGS_UID_SIZE + 1];
244     } open_settings_with_uid;
245     struct {
246         uint8_t index;
247         bool erase;
248     } close_settings_with_index;
249     struct {
250         char uid[ESP_BLE_MESH_SETTINGS_UID_SIZE + 1];
251         bool erase;
252     } close_settings_with_uid;
253     struct {
254         uint8_t index;
255     } delete_settings_with_index;
256     struct {
257         char uid[ESP_BLE_MESH_SETTINGS_UID_SIZE + 1];
258     } delete_settings_with_uid;
259     struct ble_mesh_set_fast_prov_info_args {
260         uint16_t unicast_min;
261         uint16_t unicast_max;
262         uint16_t net_idx;
263         uint8_t  flags;
264         uint32_t iv_index;
265         uint8_t  offset;
266         uint8_t  match_len;
267         uint8_t  match_val[16];
268     } set_fast_prov_info;
269     struct ble_mesh_set_fast_prov_action_args {
270         uint8_t action;
271     } set_fast_prov_action;
272     struct ble_mesh_lpn_enable_args {
273         /* RFU */
274     } lpn_enable;
275     struct ble_mesh_lpn_disable_args {
276         bool force;
277     } lpn_disable;
278     struct ble_mesh_lpn_poll_args {
279         /* RFU */
280     } lpn_poll;
281     struct ble_mesh_proxy_client_connect_args {
282         uint8_t  addr[6];
283         uint8_t  addr_type;
284         uint16_t net_idx;
285     } proxy_client_connect;
286     struct ble_mesh_proxy_client_disconnect_args {
287         uint8_t conn_handle;
288     } proxy_client_disconnect;
289     struct ble_mesh_proxy_client_set_filter_type_args {
290         uint8_t  conn_handle;
291         uint16_t net_idx;
292         uint8_t  filter_type;
293     } proxy_client_set_filter_type;
294     struct ble_mesh_proxy_client_add_filter_addr_args {
295         uint8_t   conn_handle;
296         uint16_t  net_idx;
297         uint16_t  addr_num;
298         uint16_t *addr;
299     } proxy_client_add_filter_addr;
300     struct ble_mesh_proxy_client_remove_filter_addr_args {
301         uint8_t   conn_handle;
302         uint16_t  net_idx;
303         uint16_t  addr_num;
304         uint16_t *addr;
305     } proxy_client_remove_filter_addr;
306     struct ble_mesh_model_sub_group_addr_args {
307         uint16_t element_addr;
308         uint16_t company_id;
309         uint16_t model_id;
310         uint16_t group_addr;
311     } model_sub_group_addr;
312     struct ble_mesh_model_unsub_group_addr_args {
313         uint16_t element_addr;
314         uint16_t company_id;
315         uint16_t model_id;
316         uint16_t group_addr;
317     } model_unsub_group_addr;
318     struct ble_mesh_deinit_args {
319         esp_ble_mesh_deinit_param_t param;
320     } mesh_deinit;
321 } btc_ble_mesh_prov_args_t;
322 
323 typedef union {
324     struct ble_mesh_model_publish_args {
325         esp_ble_mesh_model_t *model;
326         uint8_t device_role;
327     } model_publish;
328     struct ble_mesh_model_send_args {
329         esp_ble_mesh_model_t *model;
330         esp_ble_mesh_msg_ctx_t *ctx;
331         uint32_t opcode;
332         bool need_rsp;
333         uint16_t length;
334         uint8_t *data;
335         uint8_t device_role;
336         int32_t msg_timeout;
337     } model_send;
338     struct ble_mesh_server_model_update_state_args {
339         esp_ble_mesh_model_t *model;
340         esp_ble_mesh_server_state_type_t type;
341         esp_ble_mesh_server_state_value_t *value;
342     } model_update_state;
343 } btc_ble_mesh_model_args_t;
344 
345 void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
346 
347 void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
348 
349 const uint8_t *btc_ble_mesh_node_get_local_net_key(uint16_t net_idx);
350 
351 const uint8_t *btc_ble_mesh_node_get_local_app_key(uint16_t app_idx);
352 
353 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16]);
354 
355 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr);
356 
357 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_name(const char *name);
358 
359 uint16_t btc_ble_mesh_provisioner_get_prov_node_count(void);
360 
361 const esp_ble_mesh_node_t **btc_ble_mesh_provisioner_get_node_table_entry(void);
362 
363 int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model);
364 
365 int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model);
366 
367 int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod);
368 
369 uint16_t btc_ble_mesh_get_primary_addr(void);
370 
371 uint16_t *btc_ble_mesh_model_find_group(esp_ble_mesh_model_t *mod, uint16_t addr);
372 
373 esp_ble_mesh_elem_t *btc_ble_mesh_elem_find(uint16_t addr);
374 
375 uint8_t btc_ble_mesh_elem_count(void);
376 
377 esp_ble_mesh_model_t *btc_ble_mesh_model_find_vnd(const esp_ble_mesh_elem_t *elem,
378                                                   uint16_t company, uint16_t id);
379 
380 esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, uint16_t id);
381 
382 const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void);
383 
384 const char *btc_ble_mesh_provisioner_get_settings_uid(uint8_t index);
385 
386 uint8_t btc_ble_mesh_provisioner_get_settings_index(const char *uid);
387 
388 uint8_t btc_ble_mesh_provisioner_get_free_settings_count(void);
389 
390 void btc_ble_mesh_model_call_handler(btc_msg_t *msg);
391 void btc_ble_mesh_model_cb_handler(btc_msg_t *msg);
392 
393 void btc_ble_mesh_prov_call_handler(btc_msg_t *msg);
394 void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg);
395 
396 #ifdef __cplusplus
397 }
398 #endif
399 
400 #endif /* _BTC_BLE_MESH_PROV_H_ */
401