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