xref: /btstack/example/mesh_node_demo.c (revision d778e7488c62d361fc176e8ae40c0d6bc8b00089)
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[30];
63 static char gap_name_prefix[] = "Mesh ";
64 
65 static btstack_packet_callback_registration_t hci_event_callback_registration;
66 
67 #ifdef ENABLE_MESH_GATT_BEARER
68 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
69     UNUSED(channel);
70     UNUSED(size);
71 
72     if (packet_type != HCI_EVENT_PACKET) return;
73 
74     bd_addr_t addr;
75 
76     switch (hci_event_packet_get_type(packet)) {
77         case BTSTACK_EVENT_STATE:
78             if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
79             // setup gap name
80             gap_local_bd_addr(addr);
81             strcpy(gap_name_buffer, gap_name_prefix);
82             strcat(gap_name_buffer, bd_addr_to_str(addr));
83             break;
84         default:
85             break;
86     }
87 }
88 
89 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){
90     UNUSED(connection_handle);
91     if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){
92         return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, strlen(gap_name_buffer), offset, buffer, buffer_size);
93     }
94     return 0;
95 }
96 #endif
97 
98 static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
99     UNUSED(packet_type);
100     UNUSED(channel);
101     UNUSED(size);
102 
103     if (packet_type != HCI_EVENT_PACKET) return;
104 
105     switch(packet[0]){
106         case HCI_EVENT_MESH_META:
107             switch(packet[2]){
108                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
109                     printf("Provisioner link opened");
110                     break;
111                 case MESH_SUBEVENT_ATTENTION_TIMER:
112                     printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet));
113                     break;
114                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
115                     printf("Provisioner link close");
116                     break;
117                 case MESH_SUBEVENT_PB_PROV_COMPLETE:
118                     printf("Provisioning complete\n");
119                     break;
120                 default:
121                     break;
122             }
123             break;
124         default:
125             break;
126     }
127 }
128 
129 static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
130     UNUSED(channel);
131     UNUSED(size);
132 
133     if (packet_type != HCI_EVENT_PACKET) return;
134 
135     switch(packet[0]){
136         case HCI_EVENT_MESH_META:
137             switch(packet[2]){
138                 case MESH_SUBEVENT_STATE_UPDATE_BOOL:
139                     printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n",
140                         mesh_subevent_state_update_bool_get_model_identifier(packet),
141                         mesh_subevent_state_update_bool_get_state_identifier(packet),
142                         mesh_subevent_state_update_bool_get_reason(packet),
143                         mesh_subevent_state_update_bool_get_value(packet));
144                     break;
145                 default:
146                     break;
147             }
148             break;
149         default:
150             break;
151     }
152 }
153 
154 static void show_usage(void){
155     bd_addr_t      iut_address;
156     gap_local_bd_addr(iut_address);
157     printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address));
158     printf("8      - Delete provisioning data\n");
159     printf("g      - Generic ON/OFF Server Toggle Value\n");
160     printf("\n");
161 }
162 
163 static void stdin_process(char cmd){
164     switch (cmd){
165         case '8':
166             mesh_node_reset();
167             printf("Mesh Node Reset!\n");
168             mesh_proxy_start_advertising_unprovisioned_device();
169             break;
170         case 'g':
171             printf("Generic ON/OFF Server Toggle Value\n");
172             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);
173             break;
174         case ' ':
175             show_usage();
176             break;
177         default:
178             printf("Command: '%c' not implemented\n", cmd);
179             show_usage();
180             break;
181     }
182 }
183 
184 static int scan_hex_byte(const char * byte_string){
185     int upper_nibble = nibble_for_char(*byte_string++);
186     if (upper_nibble < 0) return -1;
187     int lower_nibble = nibble_for_char(*byte_string);
188     if (lower_nibble < 0) return -1;
189     return (upper_nibble << 4) | lower_nibble;
190 }
191 
192 static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
193     int i;
194     for (i = 0; i < len; i++) {
195         int single_byte = scan_hex_byte(string);
196         if (single_byte < 0) return 0;
197         string += 2;
198         buffer[i] = (uint8_t)single_byte;
199         // don't check seperator after last byte
200         if (i == len - 1) {
201             return 1;
202         }
203         // optional seperator
204         char separator = *string;
205         if (separator == ':' && separator == '-' && separator == ' ') {
206             string++;
207         }
208     }
209     return 1;
210 }
211 
212 int btstack_main(void);
213 int btstack_main(void)
214 {
215 #ifdef HAVE_BTSTACK_STDIN
216     // console
217     btstack_stdin_setup(stdin_process);
218 #endif
219 
220     // crypto
221     btstack_crypto_init();
222 
223 #ifdef ENABLE_MESH_GATT_BEARER
224     // l2cap
225     l2cap_init();
226 
227     // setup ATT server
228     att_server_init(profile_data, &att_read_callback, NULL);
229 
230     //
231     sm_init();
232 #endif
233 
234 #ifdef ENABLE_MESH_GATT_BEARER
235     // register for HCI events
236     hci_event_callback_registration.callback = &packet_handler;
237     hci_add_event_handler(&hci_event_callback_registration);
238 #endif
239 
240     // mesh
241     mesh_init();
242 
243 #ifdef ENABLE_MESH_GATT_BEARER
244     // setup connectable advertisments
245     bd_addr_t null_addr;
246     memset(null_addr, 0, 6);
247     uint8_t adv_type = 0;   // AFV_IND
248     uint16_t adv_int_min = 0x0030;
249     uint16_t adv_int_max = 0x0030;
250     adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
251 #endif
252 
253     // Track Provisioning as device role
254     mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler);
255 
256     // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors
257     mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103);
258 
259     // Setup Generic On/Off model
260     mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER);
261     mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations();
262     mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state;
263     mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler);
264     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model);
265 
266     // Setup our custom model
267     mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER);
268     mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model);
269 
270     // Enable Output OOB
271     provisioning_device_set_output_oob_actions(0x08, 0x08);
272 
273     // Enable PROXY
274     mesh_foundation_gatt_proxy_set(1);
275 
276 #if defined(ENABLE_MESH_ADV_BEARER)
277     // setup scanning when supporting ADV Bearer
278     gap_set_scan_parameters(0, 0x300, 0x300);
279     gap_start_scan();
280 #endif
281 
282     uint8_t device_uuid[16];
283     btstack_parse_hex(device_uuid_string, 16, device_uuid);
284     mesh_node_set_device_uuid(device_uuid);
285 
286     // turn on!
287 	hci_power_control(HCI_POWER_ON);
288 
289     return 0;
290 }
291 /* EXAMPLE_END */
292