1 /* 2 * Copyright (C) 2014 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 #define __BTSTACK_FILE__ "mesh_pts.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "btstack.h" 46 #include "mesh_pts.h" 47 48 // general 49 static void show_usage(void); 50 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 51 static void mesh_pts_dump_mesh_options(void); 52 53 #define MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER 0x0000u 54 55 static btstack_packet_callback_registration_t hci_event_callback_registration; 56 static int provisioned; 57 58 static mesh_model_t mesh_vendor_model; 59 60 static mesh_model_t mesh_generic_on_off_server_model; 61 static mesh_generic_on_off_state_t mesh_generic_on_off_state; 62 63 static char gap_name_buffer[30]; 64 static char gap_name_prefix[] = "Mesh "; 65 66 // pts add-on 67 #define PTS_DEFAULT_TTL 10 68 69 static int pts_type; 70 71 static mesh_virtual_address_t * pts_virtual_addresss; 72 73 const char * pts_device_uuid_string = "001BDC0810210B0E0A0C000B0E0A0C00"; 74 75 static uint8_t prov_static_oob_data[16]; 76 static const char * prov_static_oob_string = "00000000000000000102030405060708"; 77 78 static uint8_t prov_public_key_data[64]; 79 static const char * prov_public_key_string = "F465E43FF23D3F1B9DC7DFC04DA8758184DBC966204796ECCF0D6CF5E16500CC0201D048BCBBD899EEEFC424164E33C201C2B010CA6B4D43A8A155CAD8ECB279"; 80 static uint8_t prov_private_key_data[32]; 81 static const char * prov_private_key_string = "529AA0670D72CD6497502ED473502B037E8803B5C60829A5A3CAA219505530BA"; 82 83 static mesh_transport_key_t pts_application_key; 84 85 // pin entry (pts) 86 static int ui_chars_for_pin; 87 static uint8_t ui_pin[17]; 88 static int ui_pin_offset; 89 90 static mesh_publication_model_t generic_on_off_server_publication; 91 92 static mesh_health_fault_t health_fault; 93 94 static void mesh_provisioning_dump(const mesh_provisioning_data_t * data){ 95 mesh_network_key_t * key = data->network_key; 96 printf("UnicastAddr: 0x%02x\n", data->unicast_address); 97 printf("DevKey: "); printf_hexdump(data->device_key, 16); 98 printf("IV Index: 0x%08x\n", data->iv_index); 99 printf("Flags: 0x%02x\n", data->flags); 100 printf("NetKey: "); printf_hexdump(key->net_key, 16); 101 printf("-- Derived from NetKey --\n"); 102 printf("NID: 0x%02x\n", key->nid); 103 printf("NetworkID: "); printf_hexdump(key->network_id, 8); 104 printf("BeaconKey: "); printf_hexdump(key->beacon_key, 16); 105 printf("EncryptionKey: "); printf_hexdump(key->encryption_key, 16); 106 printf("PrivacyKey: "); printf_hexdump(key->privacy_key, 16); 107 printf("IdentityKey: "); printf_hexdump(key->identity_key, 16); 108 } 109 110 111 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 112 UNUSED(channel); 113 UNUSED(size); 114 bd_addr_t addr; 115 int i; 116 117 switch (packet_type) { 118 case HCI_EVENT_PACKET: 119 switch (hci_event_packet_get_type(packet)) { 120 case BTSTACK_EVENT_STATE: 121 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 122 // dump bd_addr in pts format 123 gap_local_bd_addr(addr); 124 printf("Local addr: %s - ", bd_addr_to_str(addr)); 125 for (i=0;i<6;i++) { 126 printf("%02x", addr[i]); 127 } 128 printf("\n"); 129 130 // setup gap name 131 strcpy(gap_name_buffer, gap_name_prefix); 132 strcat(gap_name_buffer, bd_addr_to_str(addr)); 133 134 // dump PTS MeshOptions.ini 135 mesh_pts_dump_mesh_options(); 136 break; 137 default: 138 break; 139 } 140 break; 141 } 142 } 143 144 static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 145 mesh_provisioning_data_t provisioning_data; 146 147 switch(packet[0]){ 148 case HCI_EVENT_MESH_META: 149 switch(packet[2]){ 150 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN: 151 printf("Provisioner link opened"); 152 break; 153 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: 154 printf("Provisioner link close"); 155 break; 156 case MESH_SUBEVENT_ATTENTION_TIMER: 157 printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet)); 158 break; 159 case MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST: 160 printf("Enter passphrase: "); 161 fflush(stdout); 162 ui_chars_for_pin = 1; 163 ui_pin_offset = 0; 164 break; 165 case MESH_SUBEVENT_PB_PROV_COMPLETE: 166 printf("Provisioning complete\n"); 167 // dump provisioning data 168 provisioning_device_data_get(&provisioning_data); 169 mesh_provisioning_dump(&provisioning_data); 170 provisioned = 1; 171 break; 172 default: 173 break; 174 } 175 break; 176 default: 177 break; 178 } 179 } 180 181 static void mesh_state_update_message_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 182 if (packet_type != HCI_EVENT_PACKET) return; 183 184 switch(packet[0]){ 185 case HCI_EVENT_MESH_META: 186 switch(packet[2]){ 187 case MESH_SUBEVENT_STATE_UPDATE_BOOL: 188 printf("State update: model identifier 0x%08x, state identifier 0x%08x, reason %u, state %u\n", 189 mesh_subevent_state_update_bool_get_model_identifier(packet), 190 mesh_subevent_state_update_bool_get_state_identifier(packet), 191 mesh_subevent_state_update_bool_get_reason(packet), 192 mesh_subevent_state_update_bool_get_value(packet)); 193 break; 194 default: 195 break; 196 } 197 break; 198 default: 199 break; 200 } 201 } 202 203 // PTS 204 205 // helper network layer, temp 206 static void mesh_pts_received_network_message(mesh_network_callback_type_t callback_type, mesh_network_pdu_t *network_pdu){ 207 switch (callback_type){ 208 case MESH_NETWORK_PDU_RECEIVED: 209 printf("Received network message. SRC %04x, DST %04x, SEQ %04x\n", 210 mesh_network_src(network_pdu), mesh_network_dst(network_pdu), mesh_network_seq(network_pdu)); 211 printf_hexdump(mesh_network_pdu_data(network_pdu), mesh_network_pdu_len(network_pdu)); 212 mesh_network_message_processed_by_higher_layer(network_pdu); 213 break; 214 default: 215 break; 216 } 217 } 218 219 static uint8_t mesh_network_send(uint8_t ttl, uint16_t dest, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len){ 220 221 uint16_t netkey_index = 0; 222 uint8_t ctl = 0; 223 uint16_t src = mesh_node_get_primary_element_address(); 224 uint32_t seq = mesh_sequence_number_next(); 225 226 // "3.4.5.2: The output filter of the interface connected to advertising or GATT bearers shall drop all messages with TTL value set to 1." 227 // if (ttl <= 1) return 0; 228 229 // TODO: check transport_pdu_len depending on ctl 230 231 // lookup network by netkey_index 232 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 233 if (!network_key) return 0; 234 235 // allocate network_pdu 236 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 237 if (!network_pdu) return 0; 238 239 // setup network_pdu 240 mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, ctl, ttl, seq, src, dest, transport_pdu_data, transport_pdu_len); 241 242 // send network_pdu 243 mesh_lower_transport_send_pdu((mesh_pdu_t *) network_pdu); 244 return 0; 245 } 246 247 static void printf_hex(const uint8_t * data, uint16_t len){ 248 while (len){ 249 printf("%02x", *data); 250 data++; 251 len--; 252 } 253 printf("\n"); 254 } 255 256 static void mesh_pts_dump_mesh_options(void){ 257 printf("\nMeshOptions.ini - into 'My Decoders' of Bluetooth Protocol Viewer\n"); 258 259 printf("[mesh]\n"); 260 261 printf("{IVindex}\n"); 262 printf("%08x\n", mesh_get_iv_index()); 263 264 mesh_network_key_t * network_key = mesh_network_key_list_get(0); 265 if (network_key){ 266 printf("{NetKey}\n"); 267 printf_hex(network_key->net_key, 16); 268 } 269 270 mesh_transport_key_t * transport_key = mesh_transport_key_get(0); 271 if (transport_key){ 272 printf("{AppKey}\n"); 273 printf_hex(transport_key->key, 16); 274 } 275 276 mesh_transport_key_t * device_key = mesh_transport_key_get(MESH_DEVICE_KEY_INDEX); 277 if (device_key){ 278 printf("{DevKey}\n"); 279 printf_hex(device_key->key, 16); 280 } 281 printf("\n"); 282 } 283 284 static void mesh_unprovisioned_beacon_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 285 if (packet_type != MESH_BEACON_PACKET) return; 286 uint8_t device_uuid[16]; 287 uint16_t oob; 288 memcpy(device_uuid, &packet[1], 16); 289 oob = big_endian_read_16(packet, 17); 290 printf("received unprovisioned device beacon, oob data %x, device uuid: ", oob); 291 printf_hexdump(device_uuid, 16); 292 pb_adv_create_link(device_uuid); 293 } 294 295 static int scan_hex_byte(const char * byte_string){ 296 int upper_nibble = nibble_for_char(*byte_string++); 297 if (upper_nibble < 0) return -1; 298 int lower_nibble = nibble_for_char(*byte_string); 299 if (lower_nibble < 0) return -1; 300 return (upper_nibble << 4) | lower_nibble; 301 } 302 303 static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){ 304 int i; 305 for (i = 0; i < len; i++) { 306 int single_byte = scan_hex_byte(string); 307 if (single_byte < 0) return 0; 308 string += 2; 309 buffer[i] = (uint8_t)single_byte; 310 // don't check seperator after last byte 311 if (i == len - 1) { 312 return 1; 313 } 314 // optional seperator 315 char separator = *string; 316 if (separator == ':' && separator == '-' && separator == ' ') { 317 string++; 318 } 319 } 320 return 1; 321 } 322 323 static void btstack_print_hex(const uint8_t * data, uint16_t len, char separator){ 324 int i; 325 for (i=0;i<len;i++){ 326 printf("%02x", data[i]); 327 if (separator){ 328 printf("%c", separator); 329 } 330 } 331 printf("\n"); 332 } 333 334 static void load_pts_app_key(void){ 335 // PTS app key 336 btstack_parse_hex("3216D1509884B533248541792B877F98", 16, pts_application_key.key); 337 pts_application_key.aid = 0x38; 338 pts_application_key.internal_index = mesh_transport_key_get_free_index(); 339 mesh_transport_key_add(&pts_application_key); 340 printf("PTS Application Key (AID %02x): ", 0x38); 341 printf_hexdump(pts_application_key.key, 16); 342 } 343 static void send_pts_network_messsage(const char * dst_type, uint16_t dst_addr, int ttl_type){ 344 uint8_t ttl; 345 switch (ttl_type){ 346 case 0: 347 ttl = 0; 348 break; 349 case 1: 350 ttl = PTS_DEFAULT_TTL; 351 break; 352 default: 353 ttl = 0x7f; 354 break; 355 } 356 printf("%s dst %04x, ttl %u\n", dst_type, dst_addr, ttl); 357 int lower_transport_pdu_len = 16; 358 uint8_t lower_transport_pdu_data[16]; 359 memset(lower_transport_pdu_data, 0x55, lower_transport_pdu_len); 360 mesh_network_send(ttl, dst_addr, lower_transport_pdu_data, lower_transport_pdu_len); 361 } 362 363 static void send_pts_unsegmented_access_messsage(void){ 364 uint8_t access_pdu_data[16]; 365 366 load_pts_app_key(); 367 368 uint16_t src = mesh_node_get_primary_element_address(); 369 uint16_t dest = 0x0001; 370 uint8_t ttl = PTS_DEFAULT_TTL; 371 372 int access_pdu_len = 1; 373 memset(access_pdu_data, 0x55, access_pdu_len); 374 uint16_t netkey_index = 0; 375 uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX; 376 377 // send as unsegmented access pdu 378 mesh_pdu_t * pdu = (mesh_pdu_t*) mesh_network_pdu_get(); 379 int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len); 380 if (status) return; 381 mesh_access_send_unacknowledged_pdu(pdu); 382 } 383 384 static void send_pts_segmented_access_messsage_unicast(void){ 385 uint8_t access_pdu_data[20]; 386 387 load_pts_app_key(); 388 389 uint16_t src = mesh_node_get_primary_element_address(); 390 uint16_t dest = 0x0001; 391 uint8_t ttl = PTS_DEFAULT_TTL; 392 393 int access_pdu_len = 20; 394 memset(access_pdu_data, 0x55, access_pdu_len); 395 uint16_t netkey_index = 0; 396 uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX; 397 398 // send as segmented access pdu 399 mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get(); 400 int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len); 401 if (status) return; 402 mesh_access_send_unacknowledged_pdu(pdu); 403 } 404 405 static void send_pts_segmented_access_messsage_group(void){ 406 uint8_t access_pdu_data[20]; 407 408 load_pts_app_key(); 409 410 uint16_t src = mesh_node_get_primary_element_address(); 411 uint16_t dest = 0xd000; 412 uint8_t ttl = PTS_DEFAULT_TTL; 413 414 int access_pdu_len = 20; 415 memset(access_pdu_data, 0x55, access_pdu_len); 416 uint16_t netkey_index = 0; 417 uint16_t appkey_index = 0; 418 419 // send as segmented access pdu 420 mesh_pdu_t * pdu = (mesh_pdu_t *) mesh_transport_pdu_get(); 421 int status = mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len); 422 if (status) return; 423 mesh_access_send_unacknowledged_pdu(pdu); 424 } 425 426 static void send_pts_segmented_access_messsage_virtual(void){ 427 uint8_t access_pdu_data[20]; 428 429 load_pts_app_key(); 430 431 uint16_t src = mesh_node_get_primary_element_address(); 432 uint16_t dest = pts_virtual_addresss->pseudo_dst; 433 uint8_t ttl = PTS_DEFAULT_TTL; 434 435 int access_pdu_len = 20; 436 memset(access_pdu_data, 0x55, access_pdu_len); 437 uint16_t netkey_index = 0; 438 uint16_t appkey_index = 0; 439 440 // send as segmented access pdu 441 mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get(); 442 int status = mesh_upper_transport_setup_access_pdu((mesh_pdu_t*) transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len); 443 if (status) return; 444 mesh_access_send_unacknowledged_pdu((mesh_pdu_t*) transport_pdu); 445 } 446 447 static void show_usage(void){ 448 bd_addr_t iut_address; 449 gap_local_bd_addr(iut_address); 450 printf("\n--- Bluetooth Mesh Console at %s ---\n", bd_addr_to_str(iut_address)); 451 printf("0 - Send Network Message Unicast\n"); 452 printf("1 - Send Network Message Virtual 9779\n"); 453 printf("2 - Send Network Message Group D000\n"); 454 printf("3 - Send Network Message All Proxies\n"); 455 printf("4 - Send Network Message All Friends\n"); 456 printf("5 - Send Network Message All Relays\n"); 457 printf("6 - Send Network Message Nodes\n"); 458 printf("7 - Dump Network Messages\n"); 459 printf("? - Send Unsegmented Access Message\n"); 460 printf("? - Send Segmented Access Message - Unicast\n"); 461 printf("? - Send Segmented Access Message - Group D000\n"); 462 printf("? - Send Segmented Access Message - Virtual 9779\n"); 463 printf("? - Clear Replay Protection List\n"); 464 printf("? - Load PTS App key\n"); 465 printf("8 - Delete provisioning data\n"); 466 printf("p - Enable Public Key OOB \n"); 467 printf("o - Enable Output OOB \n"); 468 printf("i - Input Output OOB \n"); 469 printf("s - Static Output OOB \n"); 470 printf("b - Set Secure Network Beacon %s\n", mesh_foundation_beacon_get() ? "Off" : "On"); 471 #ifdef ENABLE_MESH_PROXY_SERVER 472 printf("n - Start Advertising with Node Identity\n"); 473 printf("N - Stop Advertising with Node Identity\n"); 474 #endif 475 printf("g - Generic ON/OFF Server Toggle Value\n"); 476 printf("f - Register Battery Low warning\n"); 477 printf("\n"); 478 } 479 480 static void stdin_process(char cmd){ 481 if (ui_chars_for_pin){ 482 printf("%c", cmd); 483 fflush(stdout); 484 if (cmd == '\n'){ 485 printf("\nSending Pin '%s'\n", ui_pin); 486 provisioning_device_input_oob_complete_alphanumeric(1, ui_pin, ui_pin_offset); 487 ui_chars_for_pin = 0; 488 } else { 489 ui_pin[ui_pin_offset++] = cmd; 490 } 491 return; 492 } 493 switch (cmd){ 494 case '0': 495 send_pts_network_messsage("Unicast", 0x0001, pts_type++); 496 break; 497 case '1': 498 send_pts_network_messsage("Virtual", pts_virtual_addresss->hash, pts_type++); 499 break; 500 case '2': 501 send_pts_network_messsage("Group", 0xd000, pts_type++); 502 break; 503 case '3': 504 send_pts_network_messsage("All Proxies", MESH_ADDRESS_ALL_PROXIES, pts_type++); 505 break; 506 case '4': 507 send_pts_network_messsage("All Friends", MESH_ADDRESS_ALL_FRIENDS, pts_type++); 508 break; 509 case '5': 510 send_pts_network_messsage("All Relays", MESH_ADDRESS_ALL_RELAYS, pts_type++); 511 break; 512 case '6': 513 send_pts_network_messsage("All Nodes", MESH_ADDRESS_ALL_NODES, pts_type++); 514 break; 515 case '7': 516 printf("Dump Network packets\n"); 517 mesh_network_set_higher_layer_handler(&mesh_pts_received_network_message); 518 break; 519 case '8': 520 mesh_node_reset(); 521 printf("Mesh Node Reset!\n"); 522 #ifdef ENABLE_MESH_PROXY_SERVER 523 mesh_proxy_start_advertising_unprovisioned_device(); 524 #endif 525 break; 526 case 'p': 527 printf("+ Public Key OOB Enabled\n"); 528 btstack_parse_hex(prov_public_key_string, 64, prov_public_key_data); 529 btstack_parse_hex(prov_private_key_string, 32, prov_private_key_data); 530 provisioning_device_set_public_key_oob(prov_public_key_data, prov_private_key_data); 531 break; 532 case 'o': 533 printf("+ Output OOB Enabled\n"); 534 provisioning_device_set_output_oob_actions(0x08, 0x08); 535 break; 536 case 'i': 537 printf("+ Input OOB Enabled\n"); 538 provisioning_device_set_input_oob_actions(0x08, 0x08); 539 break; 540 case 's': 541 printf("+ Static OOB Enabled\n"); 542 btstack_parse_hex(prov_static_oob_string, 16, prov_static_oob_data); 543 provisioning_device_set_static_oob(16, prov_static_oob_data); 544 break; 545 case 'g': 546 printf("Generic ON/OFF Server Toggle Value\n"); 547 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); 548 break; 549 case 'b': 550 printf("Turn Secure Network Beacon %s\n", mesh_foundation_beacon_get() ? "Off" : "On"); 551 mesh_foundation_beacon_set(1 - mesh_foundation_beacon_get()); 552 mesh_foundation_state_store(); 553 break; 554 #ifdef ENABLE_MESH_PROXY_SERVER 555 case 'n': 556 printf("Start Advertising with Node ID\n"); 557 mesh_proxy_start_advertising_with_node_id(); 558 break; 559 case 'N': 560 printf("Stop Advertising with Node ID\n"); 561 mesh_proxy_start_advertising_with_node_id(); 562 break; 563 #endif 564 case 'f': 565 // 0x01 = Battery Low 566 mesh_health_server_set_fault(mesh_node_get_health_server(), BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 1); 567 case ' ': 568 show_usage(); 569 break; 570 default: 571 printf("Command: '%c' not implemented\n", cmd); 572 show_usage(); 573 break; 574 } 575 } 576 577 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){ 578 UNUSED(connection_handle); 579 if (att_handle == ATT_CHARACTERISTIC_GAP_DEVICE_NAME_01_VALUE_HANDLE){ 580 return att_read_callback_handle_blob((const uint8_t *)gap_name_buffer, strlen(gap_name_buffer), offset, buffer, buffer_size); 581 } 582 return 0; 583 } 584 585 int btstack_main(void); 586 int btstack_main(void) 587 { 588 // console 589 btstack_stdin_setup(stdin_process); 590 591 // crypto 592 btstack_crypto_init(); 593 594 // l2cap 595 l2cap_init(); 596 597 // setup le device db 598 le_device_db_init(); 599 600 // setup ATT server 601 att_server_init(profile_data, &att_read_callback, NULL); 602 603 // 604 sm_init(); 605 606 607 // mesh 608 mesh_init(); 609 610 // setup connectable advertisments 611 bd_addr_t null_addr; 612 memset(null_addr, 0, 6); 613 uint8_t adv_type = 0; // AFV_IND 614 uint16_t adv_int_min = 0x0030; 615 uint16_t adv_int_max = 0x0030; 616 adv_bearer_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00); 617 618 // Provisioning in device role 619 mesh_register_provisioning_device_packet_handler(&mesh_provisioning_message_handler); 620 621 // Loc - bottom - https://www.bluetooth.com/specifications/assigned-numbers/gatt-namespace-descriptors 622 mesh_node_set_element_location(mesh_node_get_primary_element(), 0x103); 623 624 // Setup node info 625 mesh_node_set_info(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 0, 0); 626 627 // setup health server 628 mesh_health_server_add_fault_state(mesh_node_get_health_server(), BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, &health_fault); 629 630 // Setup Generic On/Off model 631 mesh_generic_on_off_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_GENERIC_ON_OFF_SERVER); 632 mesh_generic_on_off_server_model.operations = mesh_generic_on_off_server_get_operations(); 633 mesh_generic_on_off_server_model.model_data = (void *) &mesh_generic_on_off_state; 634 mesh_generic_on_off_server_register_packet_handler(&mesh_generic_on_off_server_model, &mesh_state_update_message_handler); 635 mesh_generic_on_off_server_set_publication_model(&mesh_generic_on_off_server_model, &generic_on_off_server_publication); 636 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_generic_on_off_server_model); 637 638 // Setup our custom model 639 mesh_vendor_model.model_identifier = mesh_model_get_model_identifier(BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, MESH_BLUEKITCHEN_MODEL_ID_TEST_SERVER); 640 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_vendor_model); 641 642 // Enable PROXY 643 mesh_foundation_gatt_proxy_set(1); 644 645 // register for HCI events 646 hci_event_callback_registration.callback = &packet_handler; 647 hci_add_event_handler(&hci_event_callback_registration); 648 649 #if defined(ENABLE_MESH_ADV_BEARER) 650 // setup scanning when supporting ADV Bearer 651 gap_set_scan_parameters(0, 0x300, 0x300); 652 gap_start_scan(); 653 #endif 654 655 // PTS add-on 656 657 #ifdef ENABLE_MESH_ADV_BEARER 658 // Register for Unprovisioned Device Beacons provisioner 659 beacon_register_for_unprovisioned_device_beacons(&mesh_unprovisioned_beacon_handler); 660 #endif 661 662 // Set PTS Device UUID 663 uint8_t pts_device_uuid[16]; 664 btstack_parse_hex(pts_device_uuid_string, 16, pts_device_uuid); 665 mesh_node_set_device_uuid(pts_device_uuid); 666 btstack_print_hex(pts_device_uuid, 16, 0); 667 668 // PTS Virtual Address Label UUID - without Config Model, PTS uses our device uuid 669 uint8_t label_uuid[16]; 670 btstack_parse_hex("001BDC0810210B0E0A0C000B0E0A0C00", 16, label_uuid); 671 pts_virtual_addresss = mesh_virtual_address_register(label_uuid, 0x9779); 672 673 printf("TSPX_iut_model_id 0x1000 (Generic OnOff Server)\n"); 674 printf("TSPX_vendor_model_id 0x%08x\n", mesh_vendor_model.model_identifier); 675 // turn on! 676 hci_power_control(HCI_POWER_ON); 677 678 return 0; 679 } 680 /* EXAMPLE_END */ 681