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