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