xref: /nrf52832-nimble/packages/NimBLE-latest/apps/blemesh_models_example_2/src/ble_mesh.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /* Bluetooth: Mesh Generic OnOff, Generic Level, Lighting & Vendor Models
2  *
3  * Copyright (c) 2018 Vikrant More
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "console/console.h"
9 
10 #include "ble_mesh.h"
11 #include "device_composition.h"
12 
13 #define OOB_AUTH_ENABLE 1
14 
15 #ifdef OOB_AUTH_ENABLE
16 
output_number(bt_mesh_output_action_t action,u32_t number)17 static int output_number(bt_mesh_output_action_t action, u32_t number)
18 {
19 	printk("OOB Number: %lu\n", number);
20 	return 0;
21 }
22 
output_string(const char * str)23 static int output_string(const char *str)
24 {
25 	printk("OOB String: %s\n", str);
26 	return 0;
27 }
28 
29 #endif
30 
prov_complete(u16_t net_idx,u16_t addr)31 static void prov_complete(u16_t net_idx, u16_t addr)
32 {
33 }
34 
prov_reset(void)35 static void prov_reset(void)
36 {
37 	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
38 }
39 
40 static u8_t dev_uuid[16] = MYNEWT_VAL(BLE_MESH_DEV_UUID);
41 
42 static const struct bt_mesh_prov prov = {
43 	.uuid = dev_uuid,
44 
45 #ifdef OOB_AUTH_ENABLE
46 
47 	.output_size = 6,
48 	.output_actions = BT_MESH_DISPLAY_NUMBER | BT_MESH_DISPLAY_STRING,
49 	.output_number = output_number,
50 	.output_string = output_string,
51 
52 #endif
53 
54 	.complete = prov_complete,
55 	.reset = prov_reset,
56 };
57 
blemesh_on_reset(int reason)58 void blemesh_on_reset(int reason)
59 {
60 	BLE_HS_LOG(ERROR, "Resetting state; reason=%d\n", reason);
61 }
62 
blemesh_on_sync(void)63 void blemesh_on_sync(void)
64 {
65 	int err;
66 	ble_addr_t addr;
67 
68 	console_printf("Bluetooth initialized\n");
69 
70 	/* Use NRPA */
71 	err = ble_hs_id_gen_rnd(1, &addr);
72 	assert(err == 0);
73 	err = ble_hs_id_set_rnd(addr.val);
74 	assert(err == 0);
75 
76 	err = bt_mesh_init(addr.type, &prov, &comp);
77 	if (err) {
78 		console_printf("Initializing mesh failed (err %d)\n", err);
79 		return;
80 	}
81 
82 	if (IS_ENABLED(CONFIG_SETTINGS)) {
83 		settings_load();
84 	}
85 
86 	if (bt_mesh_is_provisioned()) {
87 		console_printf("Mesh network restored from flash\n");
88 	}
89 
90 	bt_mesh_prov_enable(BT_MESH_PROV_GATT | BT_MESH_PROV_ADV);
91 
92 	console_printf("Mesh initialized\n");
93 }
94