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_MAIN_H_
16 #define _PROVISIONER_MAIN_H_
17 
18 #define MESH_NAME_SIZE  31
19 
20 #ifdef CONFIG_BT_MESH_PROVISIONER
21 
22 enum {
23     MESH_NODE_FLAG_STORE = 0x01,
24     MESH_NODE_FLAG_CLEAR = 0x02,
25 };
26 
27 /* Each node information stored by provisioner */
28 struct bt_mesh_node_t {
29     char  node_name[MESH_NAME_SIZE];    /* Node name */
30     u8_t  dev_uuid[16];                 /* Device UUID pointer, stored in provisioner_prov.c */
31     u16_t oob_info;                     /* Node OOB information */
32     u16_t unicast_addr;                 /* Node unicast address */
33     u8_t  element_num;                  /* Node element number */
34     u16_t net_idx;                      /* Node provision net_idx */
35     u8_t  flags;                        /* Node key refresh flag and iv update flag */
36     bt_u32_t iv_index;                     /* Node IV Index */
37     u8_t  dev_key[16];                  /* Node device key */
38     bool  node_active;
39     u8_t  addr_val[6];
40     u8_t  addr_type:4;
41     u8_t  flag:4;                       /* If node is stored */
42 } __packed;
43 
44 /* The following APIs are for key init, node provision & node reset. */
45 
46 int provisioner_node_provision(int node_index, const u8_t uuid[16], u16_t oob_info,
47                                u16_t unicast_addr, u8_t element_num, u16_t net_idx,
48                                u8_t flags, bt_u32_t iv_index, const u8_t dev_key[16], u8_t *dev_addr);
49 
50 int provisioner_node_reset(int node_index);
51 
52 int provisioner_upper_reset_all_nodes(void);
53 
54 int provisioner_upper_init(void);
55 
56 /* The following APIs are for provisioner upper layers internal usage. */
57 
58 const u8_t *provisioner_net_key_get(u16_t net_idx);
59 
60 struct bt_mesh_subnet *provisioner_subnet_get(u16_t net_idx);
61 
62 bool provisioner_check_msg_dst_addr(u16_t dst_addr);
63 
64 const u8_t *provisioner_get_device_key(u16_t dst_addr);
65 
66 struct bt_mesh_app_key *provisioner_app_key_find(u16_t app_idx);
67 
68 bt_u32_t provisioner_get_prov_node_count(void);
69 
70 /* The following APIs are for provisioner application use. */
71 
72 int bt_mesh_provisioner_store_node_info(struct bt_mesh_node_t *node_info);
73 
74 int bt_mesh_provisioner_get_all_node_unicast_addr(struct net_buf_simple *buf);
75 
76 int bt_mesh_provisioner_set_node_name(int node_index, const char *name);
77 
78 const char *bt_mesh_provisioner_get_node_name(int node_index);
79 
80 int bt_mesh_provisioner_get_node_index(const char *name);
81 
82 struct bt_mesh_node_t *bt_mesh_provisioner_get_node_info(u16_t unicast_addr);
83 
84 bt_u32_t bt_mesh_provisioner_get_net_key_count(void);
85 
86 bt_u32_t bt_mesh_provisioner_get_app_key_count(void);
87 
88 int bt_mesh_provisioner_local_app_key_add(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx);
89 
90 const u8_t *bt_mesh_provisioner_local_app_key_get(u16_t net_idx, u16_t app_idx);
91 
92 int bt_mesh_provisioner_local_app_key_delete(u16_t net_idx, u16_t app_idx);
93 
94 int bt_mesh_provisioner_local_net_key_add(const u8_t net_key[16], u16_t *net_idx);
95 
96 const u8_t *bt_mesh_provisioner_local_net_key_get(u16_t net_idx);
97 
98 int bt_mesh_provisioner_local_net_key_delete(u16_t net_idx);
99 
100 int bt_mesh_provisioner_get_own_unicast_addr(u16_t *addr, u8_t *elem_num);
101 
102 /* Provisioner bind local client model with proper appkey index */
103 int bt_mesh_provisioner_bind_local_model_app_idx(u16_t elem_addr, u16_t mod_id,
104         u16_t cid, u16_t app_idx);
105 
106 /* Provisioner unbind local client model with proper appkey index */
107 int bt_mesh_provisioner_unbind_local_model_app_idx(u16_t elem_addr, u16_t mod_id,
108         u16_t cid, u16_t app_idx);
109 
110 
111 /* This API can be used to change the net_idx binded with the app_idx. */
112 int bt_mesh_provisioner_bind_local_app_net_idx(u16_t net_idx, u16_t app_idx);
113 
114 /* Provisioner print own element information */
115 int bt_mesh_provisioner_print_local_element_info(void);
116 int bt_mesh_provisioner_print_node_info(void);
117 
118 /* The following APIs are for temporary provisioner use */
119 
120 /* Set the net_idx to be assigned for the added netkey */
121 int bt_mesh_temp_prov_net_idx_set(const u8_t net_key[16], u16_t *net_idx, u8_t *status);
122 
123 /* Set the app_idx to be assigned for the added appkey */
124 int bt_mesh_temp_prov_app_idx_set(const u8_t app_key[16], u16_t net_idx, u16_t *app_idx, u8_t *status);
125 
126 bool provisioner_is_node_provisioned(const u8_t *dev_addr);
127 
128 bool bt_mesh_is_provisioner_en(void);
129 struct bt_mesh_app_key  *bt_mesh_provisioner_p_app_key_alloc();
130 struct bt_mesh_node_t *bt_mesh_provisioner_get_node_info_by_id(int node_index);
131 
132 #endif /* CONFIG_BT_MESH_PROVISIONER */
133 
134 #endif /* _PROVISIONER_MAIN_H_ */
135