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