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 // 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[] = "Mesh 00:00:00:00:00:00"; 61 62 static btstack_packet_callback_registration_t hci_event_callback_registration; 63 64 #ifdef ENABLE_MESH_GATT_BEARER 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 69 if (packet_type != HCI_EVENT_PACKET) return; 70 71 bd_addr_t addr; 72 73 switch (hci_event_packet_get_type(packet)) { 74 case BTSTACK_EVENT_STATE: 75 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 76 // setup gap name from local address 77 gap_local_bd_addr(addr); 78 btstack_replace_bd_addr_placeholder((uint8_t*)gap_name_buffer, sizeof(gap_name_buffer), addr); 79 break; 80 default: 81 break; 82 } 83 } 84 85 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){ 86 UNUSED(connection_handle); 87 if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){ 88 return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, (uint16_t) strlen(gap_name_buffer), offset, buffer, buffer_size); 89 } 90 return 0; 91 } 92 #endif 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_ATTENTION_TIMER: 108 printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet)); 109 break; 110 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: 111 printf("Provisioner link close"); 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 #ifdef HAVE_BTSTACK_STDIN 184 // console 185 btstack_stdin_setup(stdin_process); 186 #endif 187 188 // crypto 189 btstack_crypto_init(); 190 191 #ifdef ENABLE_MESH_GATT_BEARER 192 // l2cap 193 l2cap_init(); 194 195 // setup ATT server 196 att_server_init(profile_data, &att_read_callback, NULL); 197 198 // 199 sm_init(); 200 #endif 201 202 #ifdef ENABLE_MESH_GATT_BEARER 203 // register for HCI events 204 hci_event_callback_registration.callback = &packet_handler; 205 hci_add_event_handler(&hci_event_callback_registration); 206 #endif 207 208 // mesh 209 mesh_init(); 210 211 #ifdef ENABLE_MESH_GATT_BEARER 212 // setup connectable advertisments 213 bd_addr_t null_addr; 214 memset(null_addr, 0, 6); 215 uint8_t adv_type = 0; // AFV_IND 216 uint16_t adv_int_min = 0x0030; 217 uint16_t adv_int_max = 0x0030; 218 adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 219 #endif 220 221 // Track Provisioning as device role 222 mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler); 223 224 // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors 225 mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103); 226 227 // Setup Generic On/Off model 228 mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER); 229 mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations(); 230 mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state; 231 mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler); 232 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model); 233 234 // Setup our custom model 235 mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER); 236 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model); 237 238 // Enable Output OOB 239 provisioning_device_set_output_oob_actions(0x08, 0x08); 240 241 // Enable PROXY 242 mesh_foundation_gatt_proxy_set(1); 243 244 #if defined(ENABLE_MESH_ADV_BEARER) 245 // setup scanning when supporting ADV Bearer 246 gap_set_scan_parameters(0, 0x300, 0x300); 247 gap_start_scan(); 248 #endif 249 250 // turn on! 251 hci_power_control(HCI_POWER_ON); 252 253 return 0; 254 } 255 /* EXAMPLE_END */ 256