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