xref: /nrf52832-nimble/packages/NimBLE-latest/nimble/host/mesh/src/mesh.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*  Bluetooth Mesh */
2 
3 /*
4  * Copyright (c) 2017 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stdbool.h>
10 #include <errno.h>
11 
12 #include "os/os_mbuf.h"
13 #include "mesh/mesh.h"
14 
15 #include "syscfg/syscfg.h"
16 #define BT_DBG_ENABLED (MYNEWT_VAL(BLE_MESH_DEBUG))
17 #include "host/ble_hs_log.h"
18 
19 #include "adv.h"
20 #include "prov.h"
21 #include "net.h"
22 #include "beacon.h"
23 #include "lpn.h"
24 #include "friend.h"
25 #include "transport.h"
26 #include "access.h"
27 #include "foundation.h"
28 #include "proxy.h"
29 #include "shell.h"
30 #include "mesh_priv.h"
31 #include "settings.h"
32 
33 u8_t g_mesh_addr_type;
34 
bt_mesh_provision(const u8_t net_key[16],u16_t net_idx,u8_t flags,u32_t iv_index,u16_t addr,const u8_t dev_key[16])35 int bt_mesh_provision(const u8_t net_key[16], u16_t net_idx,
36 		      u8_t flags, u32_t iv_index, u16_t addr,
37 		      const u8_t dev_key[16])
38 {
39 	int err;
40 
41 	BT_INFO("Primary Element: 0x%04x", addr);
42 	BT_DBG("net_idx 0x%04x flags 0x%02x iv_index 0x%04x",
43 	       net_idx, flags, iv_index);
44 
45 	if ((MYNEWT_VAL(BLE_MESH_PB_GATT))) {
46 		bt_mesh_proxy_prov_disable();
47 	}
48 
49 	err = bt_mesh_net_create(net_idx, flags, net_key, iv_index);
50 	if (err) {
51 		if ((MYNEWT_VAL(BLE_MESH_PB_GATT))) {
52 			bt_mesh_proxy_prov_enable();
53 		}
54 
55 		return err;
56 	}
57 
58 	bt_mesh.seq = 0;
59 
60 	bt_mesh_comp_provision(addr);
61 
62 	memcpy(bt_mesh.dev_key, dev_key, 16);
63 
64 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
65 		BT_DBG("Storing network information persistently");
66 		bt_mesh_store_net();
67 		bt_mesh_store_subnet(&bt_mesh.sub[0]);
68 		bt_mesh_store_iv(false);
69 	}
70 
71 	bt_mesh_net_start();
72 
73 	return 0;
74 }
75 
bt_mesh_reset(void)76 void bt_mesh_reset(void)
77 {
78 	if (!bt_mesh.valid) {
79 		return;
80 	}
81 
82 	bt_mesh.iv_index = 0;
83 	bt_mesh.seq = 0;
84 	bt_mesh.iv_update = 0;
85 	bt_mesh.pending_update = 0;
86 	bt_mesh.valid = 0;
87 	bt_mesh.ivu_duration = 0;
88 	bt_mesh.ivu_initiator = 0;
89 
90 	k_delayed_work_cancel(&bt_mesh.ivu_timer);
91 
92 	bt_mesh_cfg_reset();
93 
94 	bt_mesh_rx_reset();
95 	bt_mesh_tx_reset();
96 
97 	if ((MYNEWT_VAL(BLE_MESH_LOW_POWER))) {
98 		bt_mesh_lpn_disable(true);
99 	}
100 
101 	if ((MYNEWT_VAL(BLE_MESH_FRIEND))) {
102 		bt_mesh_friend_clear_net_idx(BT_MESH_KEY_ANY);
103 	}
104 
105 	if ((MYNEWT_VAL(BLE_MESH_GATT_PROXY))) {
106 		bt_mesh_proxy_gatt_disable();
107 	}
108 
109 	if ((MYNEWT_VAL(BLE_MESH_PB_GATT))) {
110 		bt_mesh_proxy_prov_enable();
111 	}
112 
113 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
114 		bt_mesh_clear_net();
115 	}
116 
117 	memset(bt_mesh.dev_key, 0, sizeof(bt_mesh.dev_key));
118 
119 	bt_mesh_scan_disable();
120 	bt_mesh_beacon_disable();
121 
122 	bt_mesh_comp_unprovision();
123 
124 	if (IS_ENABLED(CONFIG_BT_MESH_PROV)) {
125 		bt_mesh_prov_reset();
126 	}
127 }
128 
bt_mesh_is_provisioned(void)129 bool bt_mesh_is_provisioned(void)
130 {
131 	return bt_mesh.valid;
132 }
133 
bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)134 int bt_mesh_prov_enable(bt_mesh_prov_bearer_t bearers)
135 {
136 	if (bt_mesh_is_provisioned()) {
137 		return -EALREADY;
138 	}
139 
140 	if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
141 	    (bearers & BT_MESH_PROV_ADV)) {
142 		/* Make sure we're scanning for provisioning inviations */
143 		bt_mesh_scan_enable();
144 		/* Enable unprovisioned beacon sending */
145 		bt_mesh_beacon_enable();
146 	}
147 
148 	if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
149 	    (bearers & BT_MESH_PROV_GATT)) {
150 		bt_mesh_proxy_prov_enable();
151 		bt_mesh_adv_update();
152 	}
153 
154 	return 0;
155 }
156 
bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)157 int bt_mesh_prov_disable(bt_mesh_prov_bearer_t bearers)
158 {
159 	if (bt_mesh_is_provisioned()) {
160 		return -EALREADY;
161 	}
162 
163 	if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) &&
164 	    (bearers & BT_MESH_PROV_ADV)) {
165 		bt_mesh_beacon_disable();
166 		bt_mesh_scan_disable();
167 	}
168 
169 	if (IS_ENABLED(CONFIG_BT_MESH_PB_GATT) &&
170 	    (bearers & BT_MESH_PROV_GATT)) {
171 		bt_mesh_proxy_prov_disable();
172 		bt_mesh_adv_update();
173 	}
174 
175 	return 0;
176 }
177 
bt_mesh_gap_event(struct ble_gap_event * event,void * arg)178 static int bt_mesh_gap_event(struct ble_gap_event *event, void *arg)
179 {
180 	ble_adv_gap_mesh_cb(event, arg);
181 
182 #if (MYNEWT_VAL(BLE_MESH_PROXY))
183 	ble_mesh_proxy_gap_event(event, arg);
184 #endif
185 
186 	return 0;
187 }
188 
bt_mesh_init(uint8_t own_addr_type,const struct bt_mesh_prov * prov,const struct bt_mesh_comp * comp)189 int bt_mesh_init(uint8_t own_addr_type, const struct bt_mesh_prov *prov,
190 		 const struct bt_mesh_comp *comp)
191 {
192 	int err;
193 
194 	g_mesh_addr_type = own_addr_type;
195 
196 	/* initialize SM alg ECC subsystem (it is used directly from mesh code) */
197 	ble_sm_alg_ecc_init();
198 
199 	err = bt_mesh_comp_register(comp);
200 	if (err) {
201 		return err;
202 	}
203 
204 #if (MYNEWT_VAL(BLE_MESH_PROV))
205 	err = bt_mesh_prov_init(prov);
206 	if (err) {
207 		return err;
208 	}
209 #endif
210 
211 #if (MYNEWT_VAL(BLE_MESH_PROXY))
212 	bt_mesh_proxy_init();
213 #endif
214 
215 #if (MYNEWT_VAL(BLE_MESH_PROV))
216 	/* Need this to proper link.rx.buf allocation */
217 	bt_mesh_prov_reset_link();
218 #endif
219 
220 	bt_mesh_net_init();
221 	bt_mesh_trans_init();
222 	bt_mesh_beacon_init();
223 	bt_mesh_adv_init();
224 
225 #if (MYNEWT_VAL(BLE_MESH_PB_ADV))
226 	/* Make sure we're scanning for provisioning inviations */
227 	bt_mesh_scan_enable();
228 	/* Enable unprovisioned beacon sending */
229 
230 	bt_mesh_beacon_enable();
231 #endif
232 
233 #if (MYNEWT_VAL(BLE_MESH_PB_GATT))
234 	bt_mesh_proxy_prov_enable();
235 #endif
236 
237 	ble_gap_mesh_cb_register(bt_mesh_gap_event, NULL);
238 
239 #if (MYNEWT_VAL(BLE_MESH_SETTINGS))
240 	bt_mesh_settings_init();
241 #endif
242 
243 	return 0;
244 }
245