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__ "pb_adv.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include "mesh/adv_bearer.h" 45 #include "mesh/beacon.h" 46 #include "mesh/mesh_node.h" 47 #include "provisioning.h" 48 #include "btstack_util.h" 49 #include "btstack_debug.h" 50 #include "btstack_event.h" 51 52 #define PB_ADV_LINK_OPEN_RETRANSMIT_MS 1000 53 54 static void pb_adv_run(void); 55 56 /* taps: 32 31 29 1; characteristic polynomial: x^32 + x^31 + x^29 + x + 1 */ 57 #define LFSR(a) ((a >> 1) ^ (uint32_t)((0 - (a & 1u)) & 0xd0000001u)) 58 59 // PB-ADV - Provisioning Bearer using Advertisement Bearer 60 61 #define MESH_GENERIC_PROVISIONING_LINK_OPEN 0x00 62 #define MESH_GENERIC_PROVISIONING_LINK_ACK 0x01 63 #define MESH_GENERIC_PROVISIONING_LINK_CLOSE 0x02 64 65 #define MESH_GENERIC_PROVISIONING_TRANSACTION_TIMEOUT_MS 30000 66 67 #define MESH_PB_ADV_MAX_PDU_SIZE 100 68 #define MESH_PB_ADV_MAX_SEGMENTS 8 69 #define MESH_PB_ADV_START_PAYLOAD 20 70 #define MESH_PB_ADV_CONT_PAYLOAD 23 71 72 typedef enum mesh_gpcf_format { 73 MESH_GPCF_TRANSACTION_START = 0, 74 MESH_GPCF_TRANSACTION_ACK, 75 MESH_GPCF_TRANSACTION_CONT, 76 MESH_GPCF_PROV_BEARER_CONTROL, 77 } mesh_gpcf_format_t; 78 79 typedef enum { 80 LINK_STATE_W4_OPEN, 81 LINK_STATE_W2_SEND_ACK, 82 LINK_STATE_W4_ACK, 83 LINK_STATE_OPEN, 84 LINK_STATE_CLOSING, 85 } link_state_t; 86 static link_state_t link_state; 87 88 #ifdef ENABLE_MESH_PROVISIONER 89 static const uint8_t * pb_adv_peer_device_uuid; 90 #endif 91 92 static uint8_t pb_adv_msg_in_buffer[MESH_PB_ADV_MAX_PDU_SIZE]; // TODO: how large are prov messages? 93 94 // single adv link 95 static uint16_t pb_adv_cid = 1; 96 static uint8_t pb_adv_provisioner_role; 97 98 // link state 99 static uint32_t pb_adv_link_id; 100 static uint8_t pb_adv_link_close_reason; 101 static uint8_t pb_adv_link_close_countdown; 102 103 // random delay for outgoing packets 104 static uint32_t pb_adv_lfsr; 105 static uint8_t pb_adv_random_delay_active; 106 static btstack_timer_source_t pb_adv_random_delay_timer; 107 108 // incoming message 109 static uint8_t pb_adv_msg_in_transaction_nr_prev; 110 static uint16_t pb_adv_msg_in_len; // 111 static uint8_t pb_adv_msg_in_fcs; 112 static uint8_t pb_adv_msg_in_last_segment; 113 static uint8_t pb_adv_msg_in_segments_missing; // bitfield for segmentes 1-n 114 static uint8_t pb_adv_msg_in_transaction_nr; 115 static uint8_t pb_adv_msg_in_send_ack; 116 117 // oputgoing message 118 static uint8_t pb_adv_msg_out_active; 119 static uint8_t pb_adv_msg_out_transaction_nr; 120 static uint8_t pb_adv_msg_out_completed_transaction_nr; 121 static uint16_t pb_adv_msg_out_len; 122 static uint16_t pb_adv_msg_out_pos; 123 static uint8_t pb_adv_msg_out_seg; 124 static uint32_t pb_adv_msg_out_start; 125 static const uint8_t * pb_adv_msg_out_buffer; 126 127 static btstack_packet_handler_t pb_adv_packet_handler; 128 129 // poor man's random number generator 130 static uint32_t pb_adv_random(void){ 131 pb_adv_lfsr = LFSR(pb_adv_lfsr); 132 return pb_adv_lfsr; 133 } 134 135 static void pb_adv_emit_pdu_sent(uint8_t status){ 136 uint8_t event[] = { HCI_EVENT_MESH_META, 2, MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT, status}; 137 pb_adv_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 138 } 139 140 static void pb_adv_emit_link_open(uint8_t status, uint16_t pb_adv_cid){ 141 uint8_t event[7] = { HCI_EVENT_MESH_META, 5, MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN, status}; 142 little_endian_store_16(event, 4, pb_adv_cid); 143 event[6] = PB_TYPE_ADV; 144 pb_adv_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 145 } 146 147 static void pb_adv_emit_link_close(uint16_t pb_adv_cid, uint8_t reason){ 148 uint8_t event[5] = { HCI_EVENT_MESH_META, 3, MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED}; 149 little_endian_store_16(event, 4, pb_adv_cid); 150 pb_adv_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 151 } 152 153 static void pb_adv_handle_bearer_control(uint32_t link_id, uint8_t transaction_nr, const uint8_t * pdu, uint16_t size){ 154 uint8_t bearer_opcode = pdu[0] >> 2; 155 uint8_t reason; 156 const uint8_t * own_device_uuid; 157 switch (bearer_opcode){ 158 case MESH_GENERIC_PROVISIONING_LINK_OPEN: // Open a session on a bearer with a device 159 // does it match our device_uuid? 160 own_device_uuid = mesh_node_get_device_uuid(); 161 if (!own_device_uuid) break; 162 if (memcmp(&pdu[1], own_device_uuid, 16) != 0) break; 163 switch(link_state){ 164 case LINK_STATE_W4_OPEN: 165 pb_adv_link_id = link_id; 166 pb_adv_provisioner_role = 0; 167 pb_adv_msg_in_transaction_nr = 0xff; // first transaction nr will be 0x00 168 pb_adv_msg_in_transaction_nr_prev = 0xff; 169 log_info("link open, id %08x", pb_adv_link_id); 170 printf("PB-ADV: Link Open %08x\n", pb_adv_link_id); 171 link_state = LINK_STATE_W2_SEND_ACK; 172 adv_bearer_request_can_send_now_for_provisioning_pdu(); 173 pb_adv_emit_link_open(0, pb_adv_cid); 174 break; 175 case LINK_STATE_OPEN: 176 if (pb_adv_link_id != link_id) break; 177 log_info("link open, resend ACK"); 178 link_state = LINK_STATE_W2_SEND_ACK; 179 adv_bearer_request_can_send_now_for_provisioning_pdu(); 180 break; 181 default: 182 break; 183 } 184 break; 185 #ifdef ENABLE_MESH_PROVISIONER 186 case MESH_GENERIC_PROVISIONING_LINK_ACK: // Acknowledge a session on a bearer 187 if (link_state != LINK_STATE_W4_ACK) break; 188 link_state = LINK_STATE_OPEN; 189 pb_adv_msg_out_transaction_nr = 0; 190 pb_adv_msg_in_transaction_nr = 0x7f; // first transaction nr will be 0x80 191 pb_adv_msg_in_transaction_nr_prev = 0x7f; 192 btstack_run_loop_remove_timer(&pb_adv_random_delay_timer); 193 log_info("link open, id %08x", pb_adv_link_id); 194 printf("PB-ADV: Link Open %08x\n", pb_adv_link_id); 195 pb_adv_emit_link_open(0, pb_adv_cid); 196 break; 197 #endif 198 case MESH_GENERIC_PROVISIONING_LINK_CLOSE: // Close a session on a bearer 199 // does it match link id 200 if (link_id != pb_adv_link_id) break; 201 reason = pdu[1]; 202 link_state = LINK_STATE_W4_OPEN; 203 log_info("link close, reason %x", reason); 204 pb_adv_emit_link_close(pb_adv_cid, reason); 205 break; 206 default: 207 log_info("BearerOpcode %x reserved for future use\n", bearer_opcode); 208 break; 209 } 210 } 211 212 static void pb_adv_pdu_complete(void){ 213 214 // Verify FCS 215 uint8_t pdu_crc = btstack_crc8_calc((uint8_t*)pb_adv_msg_in_buffer, pb_adv_msg_in_len); 216 if (pdu_crc != pb_adv_msg_in_fcs){ 217 printf("Incoming PDU: fcs %02x, calculated %02x -> drop packet\n", pb_adv_msg_in_fcs, btstack_crc8_calc(pb_adv_msg_in_buffer, pb_adv_msg_in_len)); 218 return; 219 } 220 221 printf("PB-ADV: %02x complete\n", pb_adv_msg_in_transaction_nr); 222 223 // transaction complete 224 pb_adv_msg_in_transaction_nr_prev = pb_adv_msg_in_transaction_nr; 225 if (pb_adv_provisioner_role){ 226 pb_adv_msg_in_transaction_nr = 0x7f; // invalid 227 } else { 228 pb_adv_msg_in_transaction_nr = 0xff; // invalid 229 } 230 231 // Ack Transaction 232 pb_adv_msg_in_send_ack = 1; 233 pb_adv_run(); 234 235 // Forward to Provisioning 236 pb_adv_packet_handler(PROVISIONING_DATA_PACKET, 0, pb_adv_msg_in_buffer, pb_adv_msg_in_len); 237 } 238 239 static void pb_adv_handle_transaction_start(uint8_t transaction_nr, const uint8_t * pdu, uint16_t size){ 240 241 // resend ack if packet from previous transaction received 242 if (transaction_nr != 0xff && transaction_nr == pb_adv_msg_in_transaction_nr_prev){ 243 printf("PB_ADV: %02x transaction complete, resending ack \n", transaction_nr); 244 pb_adv_msg_in_send_ack = 1; 245 return; 246 } 247 248 // new transaction? 249 if (transaction_nr != pb_adv_msg_in_transaction_nr){ 250 251 // check len 252 uint16_t msg_len = big_endian_read_16(pdu, 1); 253 if (msg_len > MESH_PB_ADV_MAX_PDU_SIZE){ 254 // abort transaction 255 return; 256 } 257 258 // check num segments 259 uint8_t last_segment = pdu[0] >> 2; 260 if (last_segment >= MESH_PB_ADV_MAX_SEGMENTS){ 261 // abort transaction 262 return; 263 } 264 265 printf("PB-ADV: %02x started\n", transaction_nr); 266 267 pb_adv_msg_in_transaction_nr = transaction_nr; 268 pb_adv_msg_in_len = msg_len; 269 pb_adv_msg_in_fcs = pdu[3]; 270 pb_adv_msg_in_last_segment = last_segment; 271 272 // set bits for segments 1..n (segment 0 already received in this message) 273 pb_adv_msg_in_segments_missing = (1 << last_segment) - 1; 274 275 // store payload 276 uint16_t payload_len = size - 4; 277 memcpy(pb_adv_msg_in_buffer, &pdu[4], payload_len); 278 279 // complete? 280 if (pb_adv_msg_in_segments_missing == 0){ 281 pb_adv_pdu_complete(); 282 } 283 } 284 } 285 286 static void pb_adv_handle_transaction_cont(uint8_t transaction_nr, const uint8_t * pdu, uint16_t size){ 287 288 // check transaction nr 289 if (transaction_nr != 0xff && transaction_nr == pb_adv_msg_in_transaction_nr_prev){ 290 printf("PB_ADV: %02x transaction complete, resending resending ack\n", transaction_nr); 291 pb_adv_msg_in_send_ack = 1; 292 return; 293 } 294 295 if (transaction_nr != pb_adv_msg_in_transaction_nr){ 296 printf("PB-ADV: %02x received msg for transaction nr %x\n", pb_adv_msg_in_transaction_nr, transaction_nr); 297 return; 298 } 299 300 // validate seg nr 301 uint8_t seg = pdu[0] >> 2; 302 if (seg >= MESH_PB_ADV_MAX_SEGMENTS || seg == 0){ 303 return; 304 } 305 306 // check if segment already received 307 uint8_t seg_mask = 1 << (seg-1); 308 if ((pb_adv_msg_in_segments_missing & seg_mask) == 0){ 309 printf("PB-ADV: %02x, segment %u already received\n", transaction_nr, seg); 310 return; 311 } 312 printf("PB-ADV: %02x, segment %u stored\n", transaction_nr, seg); 313 314 // calculate offset and fragment size 315 uint16_t msg_pos = MESH_PB_ADV_START_PAYLOAD + (seg-1) * MESH_PB_ADV_CONT_PAYLOAD; 316 uint16_t fragment_size = size - 1; 317 318 // check size if last segment 319 if (seg == pb_adv_msg_in_last_segment && (msg_pos + fragment_size) != pb_adv_msg_in_len){ 320 // last segment has invalid size 321 return; 322 } 323 324 // store segment and mark as received 325 memcpy(&pb_adv_msg_in_buffer[msg_pos], &pdu[1], fragment_size); 326 pb_adv_msg_in_segments_missing &= ~seg_mask; 327 328 // last segment 329 if (pb_adv_msg_in_segments_missing == 0){ 330 pb_adv_pdu_complete(); 331 } 332 } 333 334 static void pb_adv_outgoing_transation_complete(uint8_t status){ 335 // stop sending 336 pb_adv_msg_out_active = 0; 337 // emit done 338 pb_adv_emit_pdu_sent(status); 339 // keep track of ack'ed transactions 340 pb_adv_msg_out_completed_transaction_nr = pb_adv_msg_out_transaction_nr; 341 // increment outgoing transaction nr 342 pb_adv_msg_out_transaction_nr++; 343 if (pb_adv_msg_out_transaction_nr == 0x00){ 344 // Device role 345 pb_adv_msg_out_transaction_nr = 0x80; 346 } 347 if (pb_adv_msg_out_transaction_nr == 0x80){ 348 // Provisioner role 349 pb_adv_msg_out_transaction_nr = 0x00; 350 } 351 } 352 353 static void pb_adv_handle_transaction_ack(uint8_t transaction_nr, const uint8_t * pdu, uint16_t size){ 354 if (transaction_nr == pb_adv_msg_out_transaction_nr){ 355 printf("PB-ADV: %02x ACK received\n", transaction_nr); 356 pb_adv_outgoing_transation_complete(ERROR_CODE_SUCCESS); 357 } else if (transaction_nr == pb_adv_msg_out_completed_transaction_nr){ 358 // Transaction ack received again 359 } else { 360 printf("PB-ADV: %02x unexpected Transaction ACK %x recevied\n", pb_adv_msg_out_transaction_nr, transaction_nr); 361 } 362 } 363 364 static int pb_adv_packet_to_send(void){ 365 return pb_adv_msg_in_send_ack || pb_adv_msg_out_active || (link_state == LINK_STATE_W4_ACK); 366 } 367 368 static void pb_adv_timer_handler(btstack_timer_source_t * ts){ 369 pb_adv_random_delay_active = 0; 370 if (!pb_adv_packet_to_send()) return; 371 adv_bearer_request_can_send_now_for_provisioning_pdu(); 372 } 373 374 static void pb_adv_run(void){ 375 if (!pb_adv_packet_to_send()) return; 376 if (pb_adv_random_delay_active) return; 377 378 // spec recommends 20-50 ms, we use 20-51 ms 379 pb_adv_random_delay_active = 1; 380 uint16_t random_delay_ms = 20 + (pb_adv_random() & 0x1f); 381 log_info("random delay %u ms", random_delay_ms); 382 btstack_run_loop_set_timer_handler(&pb_adv_random_delay_timer, &pb_adv_timer_handler); 383 btstack_run_loop_set_timer(&pb_adv_random_delay_timer, random_delay_ms); 384 btstack_run_loop_add_timer(&pb_adv_random_delay_timer); 385 } 386 387 static void pb_adv_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 388 if (packet_type != HCI_EVENT_PACKET) return; 389 const uint8_t * data; 390 uint8_t length; 391 uint32_t link_id; 392 uint8_t transaction_nr; 393 uint8_t generic_provisioning_control; 394 switch(packet[0]){ 395 case GAP_EVENT_ADVERTISING_REPORT: 396 397 data = gap_event_advertising_report_get_data(packet); 398 // PDB ADV PDU 399 length = data[0]; 400 link_id = big_endian_read_32(data, 2); 401 transaction_nr = data[6]; 402 // generic provision PDU 403 generic_provisioning_control = data[7]; 404 mesh_gpcf_format_t generic_provisioning_control_format = (mesh_gpcf_format_t) generic_provisioning_control & 3; 405 406 // unless, we're waiting for LINK_OPEN, check link_id 407 if (link_state != LINK_STATE_W4_OPEN){ 408 if (link_id != pb_adv_link_id) break; 409 } 410 411 if (generic_provisioning_control_format == MESH_GPCF_PROV_BEARER_CONTROL){ 412 pb_adv_handle_bearer_control(link_id, transaction_nr, &data[7], length-6); 413 break; 414 } 415 416 // verify link id and link state 417 if (link_state != LINK_STATE_OPEN) break; 418 419 switch (generic_provisioning_control_format){ 420 case MESH_GPCF_TRANSACTION_START: 421 pb_adv_handle_transaction_start(transaction_nr, &data[7], length-6); 422 break; 423 case MESH_GPCF_TRANSACTION_CONT: 424 pb_adv_handle_transaction_cont(transaction_nr, &data[7], length-6); 425 break; 426 case MESH_GPCF_TRANSACTION_ACK: 427 pb_adv_handle_transaction_ack(transaction_nr, &data[7], length-6); 428 break; 429 default: 430 break; 431 } 432 pb_adv_run(); 433 break; 434 case HCI_EVENT_MESH_META: 435 switch(packet[2]){ 436 case MESH_SUBEVENT_CAN_SEND_NOW: 437 if (link_state == LINK_STATE_W4_ACK){ 438 // build packet 439 uint8_t buffer[22]; 440 big_endian_store_32(buffer, 0, pb_adv_link_id); 441 buffer[4] = 0; // Transaction ID = 0 442 buffer[5] = (0 << 2) | 3; // Link Open | Provisioning Bearer Control 443 memcpy(&buffer[6], pb_adv_peer_device_uuid, 16); 444 adv_bearer_send_provisioning_pdu(buffer, sizeof(buffer)); 445 log_info("link open %08x", pb_adv_link_id); 446 printf("PB-ADV: Sending Link Open for device uuid: "); 447 printf_hexdump(pb_adv_peer_device_uuid, 16); 448 btstack_run_loop_set_timer_handler(&pb_adv_random_delay_timer, &pb_adv_timer_handler); 449 btstack_run_loop_set_timer(&pb_adv_random_delay_timer, PB_ADV_LINK_OPEN_RETRANSMIT_MS); 450 btstack_run_loop_add_timer(&pb_adv_random_delay_timer); 451 break; 452 } 453 if (link_state == LINK_STATE_CLOSING){ 454 log_info("link close %08x", pb_adv_link_id); 455 printf("PB-ADV: Sending Link Close\n"); 456 // build packet 457 uint8_t buffer[7]; 458 big_endian_store_32(buffer, 0, pb_adv_link_id); 459 buffer[4] = 0; // Transaction ID = 0 460 buffer[5] = (2 << 2) | 3; // Link Close | Provisioning Bearer Control 461 buffer[6] = pb_adv_link_close_reason; 462 adv_bearer_send_provisioning_pdu(buffer, sizeof(buffer)); 463 pb_adv_link_close_countdown--; 464 if (pb_adv_link_close_countdown) { 465 adv_bearer_request_can_send_now_for_provisioning_pdu(); 466 } else { 467 link_state = LINK_STATE_W4_OPEN; 468 } 469 break; 470 } 471 if (link_state == LINK_STATE_W2_SEND_ACK){ 472 link_state = LINK_STATE_OPEN; 473 pb_adv_msg_out_transaction_nr = 0x80; 474 // build packet 475 uint8_t buffer[6]; 476 big_endian_store_32(buffer, 0, pb_adv_link_id); 477 buffer[4] = 0; 478 buffer[5] = (1 << 2) | 3; // Link Ack | Provisioning Bearer Control 479 adv_bearer_send_provisioning_pdu(buffer, sizeof(buffer)); 480 log_info("link ack %08x", pb_adv_link_id); 481 printf("PB-ADV: Sending Link Open Ack\n"); 482 break; 483 } 484 if (pb_adv_msg_in_send_ack){ 485 pb_adv_msg_in_send_ack = 0; 486 uint8_t buffer[6]; 487 big_endian_store_32(buffer, 0, pb_adv_link_id); 488 buffer[4] = pb_adv_msg_in_transaction_nr_prev; 489 buffer[5] = MESH_GPCF_TRANSACTION_ACK; 490 adv_bearer_send_provisioning_pdu(buffer, sizeof(buffer)); 491 log_info("transaction ack %08x", pb_adv_link_id); 492 printf("PB-ADV: %02x sending ACK\n", pb_adv_msg_in_transaction_nr_prev); 493 pb_adv_run(); 494 break; 495 } 496 if (pb_adv_msg_out_active){ 497 498 // check timeout for outgoing message 499 // since uint32_t is used and time now must be greater than pb_adv_msg_out_start, 500 // this claculation is correct even when the run loop time overruns 501 uint32_t transaction_time_ms = btstack_run_loop_get_time_ms() - pb_adv_msg_out_start; 502 if (transaction_time_ms >= MESH_GENERIC_PROVISIONING_TRANSACTION_TIMEOUT_MS){ 503 pb_adv_outgoing_transation_complete(ERROR_CODE_CONNECTION_TIMEOUT); 504 return; 505 } 506 507 uint8_t buffer[29]; // ADV MTU 508 big_endian_store_32(buffer, 0, pb_adv_link_id); 509 buffer[4] = pb_adv_msg_out_transaction_nr; 510 uint16_t bytes_left; 511 uint16_t pos; 512 if (pb_adv_msg_out_pos == 0){ 513 // Transaction start 514 int seg_n = pb_adv_msg_out_len / 24; 515 pb_adv_msg_out_seg = 0; 516 buffer[5] = seg_n << 2 | MESH_GPCF_TRANSACTION_START; 517 big_endian_store_16(buffer, 6, pb_adv_msg_out_len); 518 buffer[8] = btstack_crc8_calc((uint8_t*)pb_adv_msg_out_buffer, pb_adv_msg_out_len); 519 pos = 9; 520 bytes_left = 24 - 4; 521 printf("PB-ADV: %02x Sending Start: ", pb_adv_msg_out_transaction_nr); 522 } else { 523 // Transaction continue 524 buffer[5] = pb_adv_msg_out_seg << 2 | MESH_GPCF_TRANSACTION_CONT; 525 pos = 6; 526 bytes_left = 24 - 1; 527 printf("PB-ADV: %02x Sending Cont: ", pb_adv_msg_out_transaction_nr); 528 } 529 pb_adv_msg_out_seg++; 530 uint16_t bytes_to_copy = btstack_min(bytes_left, pb_adv_msg_out_len - pb_adv_msg_out_pos); 531 memcpy(&buffer[pos], &pb_adv_msg_out_buffer[pb_adv_msg_out_pos], bytes_to_copy); 532 pos += bytes_to_copy; 533 printf("bytes %02u, pos %02u, len %02u: ", bytes_to_copy, pb_adv_msg_out_pos, pb_adv_msg_out_len); 534 printf_hexdump(buffer, pos); 535 pb_adv_msg_out_pos += bytes_to_copy; 536 537 if (pb_adv_msg_out_pos == pb_adv_msg_out_len){ 538 // done 539 pb_adv_msg_out_pos = 0; 540 } 541 adv_bearer_send_provisioning_pdu(buffer, pos); 542 pb_adv_run(); 543 break; 544 } 545 break; 546 default: 547 break; 548 } 549 default: 550 break; 551 } 552 } 553 554 void pb_adv_init(void){ 555 adv_bearer_register_for_provisioning_pdu(&pb_adv_handler); 556 pb_adv_lfsr = 0x12345678; 557 pb_adv_random(); 558 } 559 560 void pb_adv_register_packet_handler(btstack_packet_handler_t packet_handler){ 561 pb_adv_packet_handler = packet_handler; 562 } 563 564 void pb_adv_send_pdu(uint16_t pb_adv_cid, const uint8_t * pdu, uint16_t size){ 565 UNUSED(pb_adv_cid); 566 printf("PB-ADV: Send packet "); 567 printf_hexdump(pdu, size); 568 pb_adv_msg_out_buffer = pdu; 569 pb_adv_msg_out_len = size; 570 pb_adv_msg_out_pos = 0; 571 pb_adv_msg_out_start = btstack_run_loop_get_time_ms(); 572 pb_adv_msg_out_active = 1; 573 pb_adv_run(); 574 } 575 576 /** 577 * Close Link 578 * @param pb_adv_cid 579 */ 580 void pb_adv_close_link(uint16_t pb_adv_cid, uint8_t reason){ 581 switch (link_state){ 582 case LINK_STATE_W4_ACK: 583 case LINK_STATE_OPEN: 584 case LINK_STATE_W2_SEND_ACK: 585 pb_adv_emit_link_close(pb_adv_cid, 0); 586 link_state = LINK_STATE_CLOSING; 587 pb_adv_link_close_countdown = 3; 588 pb_adv_link_close_reason = reason; 589 adv_bearer_request_can_send_now_for_provisioning_pdu(); 590 break; 591 case LINK_STATE_W4_OPEN: 592 case LINK_STATE_CLOSING: 593 // nothing to do 594 break; 595 } 596 } 597 598 #ifdef ENABLE_MESH_PROVISIONER 599 uint16_t pb_adv_create_link(const uint8_t * device_uuid){ 600 if (link_state != LINK_STATE_W4_OPEN) return 0; 601 602 pb_adv_peer_device_uuid = device_uuid; 603 pb_adv_provisioner_role = 1; 604 605 // create new 32-bit link id 606 pb_adv_link_id = pb_adv_random(); 607 608 // after sending OPEN, we wait for an ACK 609 link_state = LINK_STATE_W4_ACK; 610 611 // request outgoing 612 adv_bearer_request_can_send_now_for_provisioning_pdu(); 613 614 // dummy pb_adv_cid 615 return pb_adv_cid; 616 } 617 #endif 618 619