xref: /btstack/example/mesh_node_demo.c (revision 4a9eead824c50b40e12b6f72611a74a3f57a47f6)
1 /*
2  * Copyright (C) 2019 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * Basic Mesh Node demo
40  */
41 
42 #define __BTSTACK_FILE__ "mesh_node_demo.c"
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "btstack.h"
50 #include "mesh_node_demo.h"
51 
52 const char * device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00";
53 
54 // general
55 #define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER   0x0000u
56 
57 static mesh_model_t                 mesh_vendor_model;
58 
59 static mesh_model_t                 mesh_generic_on_off_server_model;
60 static mesh_generic_on_off_state_t  mesh_generic_on_off_state;
61 
62 static char gap_name_buffer[] = "Mesh 00:00:00:00:00:00";
63 
64 static btstack_packet_callback_registration_t hci_event_callback_registration;
65 
66 #ifdef ENABLE_MESH_GATT_BEARER
67 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
68     UNUSED(channel);
69     UNUSED(size);
70 
71     if (packet_type != HCI_EVENT_PACKET) return;
72 
73     bd_addr_t addr;
74 
75     switch (hci_event_packet_get_type(packet)) {
76         case BTSTACK_EVENT_STATE:
77             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
78             // setup gap name from local address
79             gap_local_bd_addr(addr);
80             btstack_replace_bd_addr_placeholder((uint8_t*)gap_name_buffer, sizeof(gap_name_buffer), addr);
81             break;
82         default:
83             break;
84     }
85 }
86 
87 static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
88     UNUSED(connection_handle);
89     if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){
90         return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, (uint16_t) strlen(gap_name_buffer), offset, buffer, buffer_size);
91     }
92     return 0;
93 }
94 #endif
95 
96 static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
97     UNUSED(packet_type);
98     UNUSED(channel);
99     UNUSED(size);
100 
101     if (packet_type != HCI_EVENT_PACKET) return;
102 
103     switch(packet[0]){
104         case HCI_EVENT_MESH_META:
105             switch(packet[2]){
106                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
107                     printf("Provisioner link opened");
108                     break;
109                 case MESH_SUBEVENT_ATTENTION_TIMER:
110                     printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet));
111                     break;
112                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
113                     printf("Provisioner link close");
114                     break;
115                 case MESH_SUBEVENT_PB_PROV_COMPLETE:
116                     printf("Provisioning complete\n");
117                     break;
118                 default:
119                     break;
120             }
121             break;
122         default:
123             break;
124     }
125 }
126 
127 static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
128     UNUSED(channel);
129     UNUSED(size);
130 
131     if (packet_type != HCI_EVENT_PACKET) return;
132 
133     switch(packet[0]){
134         case HCI_EVENT_MESH_META:
135             switch(packet[2]){
136                 case MESH_SUBEVENT_STATE_UPDATE_BOOL:
137                     printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
138                         mesh_subevent_state_update_bool_get_model_identifier(packet),
139                         mesh_subevent_state_update_bool_get_state_identifier(packet),
140                         mesh_subevent_state_update_bool_get_reason(packet),
141                         mesh_subevent_state_update_bool_get_value(packet));
142                     break;
143                 default:
144                     break;
145             }
146             break;
147         default:
148             break;
149     }
150 }
151 
152 static void show_usage(void){
153     bd_addr_t      iut_address;
154     gap_local_bd_addr(iut_address);
155     printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address));
156     printf("8      - Delete provisioning data\n");
157     printf("g      - Generic ON/OFF Server Toggle Value\n");
158     printf("\n");
159 }
160 
161 static void stdin_process(char cmd){
162     switch (cmd){
163         case '8':
164             mesh_node_reset();
165             printf("Mesh Node Reset!\n");
166             mesh_proxy_start_advertising_unprovisioned_device();
167             break;
168         case 'g':
169             printf("Generic ON/OFF Server Toggle Value\n");
170             mesh_generic_on_off_server_set(&mesh_generic_on_off_server_model, 1-mesh_generic_on_off_server_get(&mesh_generic_on_off_server_model), 0, 0);
171             break;
172         case ' ':
173             show_usage();
174             break;
175         default:
176             printf("Command: '%c' not implemented\n", cmd);
177             show_usage();
178             break;
179     }
180 }
181 
182 static int scan_hex_byte(const char * byte_string){
183     int upper_nibble = nibble_for_char(*byte_string++);
184     if (upper_nibble < 0) return -1;
185     int lower_nibble = nibble_for_char(*byte_string);
186     if (lower_nibble < 0) return -1;
187     return (upper_nibble << 4) | lower_nibble;
188 }
189 
190 static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
191     int i;
192     for (i = 0; i < len; i++) {
193         int single_byte = scan_hex_byte(string);
194         if (single_byte < 0) return 0;
195         string += 2;
196         buffer[i] = (uint8_t)single_byte;
197         // don't check seperator after last byte
198         if (i == len - 1) {
199             return 1;
200         }
201         // optional seperator
202         char separator = *string;
203         if (separator == ':' && separator == '-' && separator == ' ') {
204             string++;
205         }
206     }
207     return 1;
208 }
209 
210 int btstack_main(void);
211 int btstack_main(void)
212 {
213 #ifdef HAVE_BTSTACK_STDIN
214     // console
215     btstack_stdin_setup(stdin_process);
216 #endif
217 
218     // crypto
219     btstack_crypto_init();
220 
221 #ifdef ENABLE_MESH_GATT_BEARER
222     // l2cap
223     l2cap_init();
224 
225     // setup ATT server
226     att_server_init(profile_data, &att_read_callback, NULL);
227 
228     //
229     sm_init();
230 #endif
231 
232 #ifdef ENABLE_MESH_GATT_BEARER
233     // register for HCI events
234     hci_event_callback_registration.callback = &packet_handler;
235     hci_add_event_handler(&hci_event_callback_registration);
236 #endif
237 
238     // mesh
239     mesh_init();
240 
241 #ifdef ENABLE_MESH_GATT_BEARER
242     // setup connectable advertisments
243     bd_addr_t null_addr;
244     memset(null_addr, 0, 6);
245     uint8_t adv_type = 0;   // AFV_IND
246     uint16_t adv_int_min = 0x0030;
247     uint16_t adv_int_max = 0x0030;
248     adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
249 #endif
250 
251     // Track Provisioning as device role
252     mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler);
253 
254     // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
255     mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103);
256 
257     // Setup Generic On/Off model
258     mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
259     mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
260     mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
261     mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
262     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model);
263 
264     // Setup our custom model
265     mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
266     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model);
267 
268     // Enable Output OOB
269     provisioning_device_set_output_oob_actions(0x08, 0x08);
270 
271     // Enable PROXY
272     mesh_foundation_gatt_proxy_set(1);
273 
274 #if defined(ENABLE_MESH_ADV_BEARER)
275     // setup scanning when supporting ADV Bearer
276     gap_set_scan_parameters(0, 0x300, 0x300);
277     gap_start_scan();
278 #endif
279 
280     uint8_t device_uuid[16];
281     btstack_parse_hex(device_uuid_string, 16, device_uuid);
282     mesh_node_set_device_uuid(device_uuid);
283 
284     // turn on!
285 	hci_power_control(HCI_POWER_ON);
286 
287     return 0;
288 }
289 /* EXAMPLE_END */
290