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 #ifdef ENABLE_MESH_GATT_BEARER 66 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 67 UNUSED(channel); 68 UNUSED(size); 69 bd_addr_t addr; 70 switch (packet_type) { 71 case HCI_EVENT_PACKET: 72 switch (hci_event_packet_get_type(packet)) { 73 case BTSTACK_EVENT_STATE: 74 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 75 // setup gap name 76 gap_local_bd_addr(addr); 77 strcpy(gap_name_buffer, gap_name_prefix); 78 strcat(gap_name_buffer, bd_addr_to_str(addr)); 79 break; 80 default: 81 break; 82 } 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, 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 int btstack_main(void); 183 int btstack_main(void) 184 { 185 #ifdef HAVE_BTSTACK_STDIN 186 // console 187 btstack_stdin_setup(stdin_process); 188 #endif 189 190 // crypto 191 btstack_crypto_init(); 192 193 #ifdef ENABLE_MESH_GATT_BEARER 194 // l2cap 195 l2cap_init(); 196 197 // setup le device db 198 le_device_db_init(); 199 200 // setup ATT server 201 att_server_init(profile_data, &att_read_callback, NULL); 202 203 // 204 sm_init(); 205 #endif 206 207 #ifdef ENABLE_MESH_GATT_BEARER 208 // register for HCI events 209 hci_event_callback_registration.callback = &packet_handler; 210 hci_add_event_handler(&hci_event_callback_registration); 211 #endif 212 213 // mesh 214 mesh_init(); 215 216 #ifdef ENABLE_MESH_GATT_BEARER 217 // setup connectable advertisments 218 bd_addr_t null_addr; 219 memset(null_addr, 0, 6); 220 uint8_t adv_type = 0; // AFV_IND 221 uint16_t adv_int_min = 0x0030; 222 uint16_t adv_int_max = 0x0030; 223 adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 224 #endif 225 226 // Track Provisioning as device role 227 mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler); 228 229 // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors 230 mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103); 231 232 // Setup Generic On/Off model 233 mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER); 234 mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations(); 235 mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state; 236 mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler); 237 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model); 238 239 // Setup our custom model 240 mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER); 241 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model); 242 243 // Enable Output OOB 244 provisioning_device_set_output_oob_actions(0x08, 0x08); 245 246 // Enable PROXY 247 mesh_foundation_gatt_proxy_set(1); 248 249 #if defined(ENABLE_MESH_ADV_BEARER) 250 // setup scanning when supporting ADV Bearer 251 gap_set_scan_parameters(0, 0x300, 0x300); 252 gap_start_scan(); 253 #endif 254 255 // turn on! 256 hci_power_control(HCI_POWER_ON); 257 258 return 0; 259 } 260 /* EXAMPLE_END */ 261