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 #define BTSTACK_FILE__ "mesh_access.c" 39 40 #include <string.h> 41 #include <stdio.h> 42 #include <stdarg.h> 43 44 #include "mesh/mesh_access.h" 45 46 #include "btstack_debug.h" 47 #include "btstack_memory.h" 48 #include "btstack_tlv.h" 49 50 #include "mesh/beacon.h" 51 #include "mesh/mesh_foundation.h" 52 #include "mesh/mesh_iv_index_seq_number.h" 53 #include "mesh/mesh_node.h" 54 #include "mesh/mesh_proxy.h" 55 #include "mesh/mesh_upper_transport.h" 56 #include "mesh/mesh.h" 57 58 #define MEST_TRANSACTION_TIMEOUT_MS 6000 59 60 static void mesh_access_message_process_handler(mesh_pdu_t * pdu); 61 static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 62 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode); 63 64 // receive 65 static uint16_t mesh_access_received_pdu_refcount; 66 67 // acknowledged messages 68 static btstack_linked_list_t mesh_access_acknowledged_messages; 69 static btstack_timer_source_t mesh_access_acknowledged_timer; 70 static int mesh_access_acknowledged_timer_active; 71 72 // Transitions 73 static btstack_linked_list_t transitions; 74 static btstack_timer_source_t transitions_timer; 75 static uint32_t transition_step_min_ms; 76 static uint8_t mesh_transaction_id_counter = 0; 77 78 void mesh_access_init(void){ 79 // register with upper transport 80 mesh_upper_transport_register_access_message_handler(&mesh_access_message_process_handler); 81 mesh_upper_transport_set_higher_layer_handler(&mesh_access_upper_transport_handler); 82 } 83 84 void mesh_access_emit_state_update_bool(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier, 85 model_state_id_t state_identifier, model_state_update_reason_t reason, uint8_t value){ 86 if (event_handler == NULL) return; 87 uint8_t event[14] = {HCI_EVENT_MESH_META, 12, MESH_SUBEVENT_STATE_UPDATE_BOOL}; 88 int pos = 3; 89 event[pos++] = element_index; 90 little_endian_store_32(event, pos, model_identifier); 91 pos += 4; 92 little_endian_store_32(event, pos, (uint32_t)state_identifier); 93 pos += 4; 94 event[pos++] = (uint8_t)reason; 95 event[pos++] = value; 96 (*event_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 97 } 98 99 void mesh_access_emit_state_update_int16(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier, 100 model_state_id_t state_identifier, model_state_update_reason_t reason, int16_t value){ 101 if (event_handler == NULL) return; 102 uint8_t event[15] = {HCI_EVENT_MESH_META, 13, MESH_SUBEVENT_STATE_UPDATE_BOOL}; 103 int pos = 3; 104 event[pos++] = element_index; 105 little_endian_store_32(event, pos, model_identifier); 106 pos += 4; 107 little_endian_store_32(event, pos, (uint32_t)state_identifier); 108 pos += 4; 109 event[pos++] = (uint8_t)reason; 110 little_endian_store_16(event, pos, (uint16_t) value); 111 pos += 2; 112 (*event_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 113 } 114 115 uint8_t mesh_access_acknowledged_message_retransmissions(void){ 116 return 3; 117 } 118 119 uint32_t mesh_access_acknowledged_message_timeout_ms(void){ 120 return 30000; 121 } 122 123 #define MESH_ACCESS_OPCODE_INVALID 0xFFFFFFFFu 124 #define MESH_ACCESS_OPCODE_NOT_SET 0xFFFFFFFEu 125 126 void mesh_access_send_unacknowledged_pdu(mesh_pdu_t * pdu){ 127 pdu->ack_opcode = MESH_ACCESS_OPCODE_INVALID; 128 mesh_upper_transport_send_access_pdu(pdu); 129 } 130 131 void mesh_access_send_acknowledged_pdu(mesh_pdu_t * pdu, uint8_t retransmissions, uint32_t ack_opcode){ 132 pdu->retransmit_count = retransmissions; 133 pdu->ack_opcode = ack_opcode; 134 135 mesh_upper_transport_send_access_pdu(pdu); 136 } 137 138 #define MESH_SUBEVENT_MESSAGE_NOT_ACKNOWLEDGED 0x30 139 140 static void mesh_access_acknowledged_run(btstack_timer_source_t * ts){ 141 UNUSED(ts); 142 143 uint32_t now = btstack_run_loop_get_time_ms(); 144 145 // handle timeouts 146 btstack_linked_list_iterator_t ack_it; 147 btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages); 148 while (btstack_linked_list_iterator_has_next(&ack_it)){ 149 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it); 150 if (btstack_time_delta(now, pdu->retransmit_timeout_ms) >= 0) { 151 // remove from list 152 btstack_linked_list_remove(&mesh_access_acknowledged_messages, (btstack_linked_item_t*) pdu); 153 // retransmit or report failure 154 if (pdu->retransmit_count){ 155 pdu->retransmit_count--; 156 mesh_upper_transport_send_access_pdu(pdu); 157 } else { 158 // find correct model and emit error 159 uint16_t src = mesh_pdu_src(pdu); 160 uint16_t dst = mesh_pdu_dst(pdu); 161 mesh_element_t * element = mesh_node_element_for_unicast_address(src); 162 if (element){ 163 // find 164 mesh_model_iterator_t model_it; 165 mesh_model_iterator_init(&model_it, element); 166 while (mesh_model_iterator_has_next(&model_it)){ 167 mesh_model_t * model = mesh_model_iterator_next(&model_it); 168 // find opcode in table 169 const mesh_operation_t * operation = mesh_model_lookup_operation_by_opcode(model, pdu->ack_opcode); 170 if (operation == NULL) continue; 171 if (model->model_packet_handler == NULL) continue; 172 // emit event 173 uint8_t event[13]; 174 event[0] = HCI_EVENT_MESH_META; 175 event[1] = sizeof(event) - 2; 176 event[2] = element->element_index; 177 little_endian_store_32(event, 3, model->model_identifier); 178 little_endian_store_32(event, 7, pdu->ack_opcode); 179 little_endian_store_16(event, 11, dst); 180 (*model->model_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 181 } 182 } 183 184 // free 185 mesh_upper_transport_pdu_free(pdu); 186 } 187 } 188 } 189 190 if (mesh_access_acknowledged_timer_active) return; 191 192 // find earliest timeout and set timer 193 btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages); 194 int32_t next_timeout_ms = 0; 195 while (btstack_linked_list_iterator_has_next(&ack_it)){ 196 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it); 197 int32_t timeout_delta_ms = btstack_time_delta(pdu->retransmit_timeout_ms, now); 198 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 199 next_timeout_ms = timeout_delta_ms; 200 } 201 } 202 203 // set timer 204 if (next_timeout_ms == 0) return; 205 206 btstack_run_loop_set_timer(&mesh_access_acknowledged_timer, next_timeout_ms); 207 btstack_run_loop_set_timer_handler(&mesh_access_acknowledged_timer, mesh_access_acknowledged_run); 208 btstack_run_loop_add_timer(&mesh_access_acknowledged_timer); 209 mesh_access_acknowledged_timer_active = 1; 210 } 211 212 static void mesh_access_acknowledged_received(uint16_t rx_src, uint32_t opcode){ 213 // check if received src matches our dest 214 // free acknowledged messages if we were waiting for this message 215 216 btstack_linked_list_iterator_t ack_it; 217 btstack_linked_list_iterator_init(&ack_it, &mesh_access_acknowledged_messages); 218 while (btstack_linked_list_iterator_has_next(&ack_it)){ 219 mesh_pdu_t * tx_pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&ack_it); 220 uint16_t tx_dest = mesh_pdu_dst(tx_pdu); 221 if (tx_dest != rx_src) continue; 222 if (tx_pdu->ack_opcode != opcode) continue; 223 // got expected response from dest, remove from outgoing messages 224 mesh_upper_transport_pdu_free(tx_pdu); 225 return; 226 } 227 } 228 229 static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){ 230 UNUSED(status); 231 switch (callback_type){ 232 case MESH_TRANSPORT_PDU_SENT: 233 // unacknowledged -> free 234 if (pdu->ack_opcode == MESH_ACCESS_OPCODE_INVALID){ 235 mesh_upper_transport_pdu_free(pdu); 236 break; 237 } 238 // setup timeout 239 pdu->retransmit_timeout_ms = btstack_run_loop_get_time_ms() + mesh_access_acknowledged_message_timeout_ms(); 240 // add to mesh_access_acknowledged_messages 241 btstack_linked_list_add(&mesh_access_acknowledged_messages, (btstack_linked_item_t *) pdu); 242 // update timer 243 mesh_access_acknowledged_run(NULL); 244 break; 245 default: 246 break; 247 } 248 } 249 250 // Mesh Model Transitions 251 252 void mesh_access_transitions_setup_transaction(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address){ 253 transition->transaction_timestamp_ms = btstack_run_loop_get_time_ms(); 254 transition->transaction_identifier = transaction_identifier; 255 transition->src_address = src_address; 256 transition->dst_address = dst_address; 257 } 258 259 void mesh_access_transitions_abort_transaction(mesh_transition_t * transition){ 260 mesh_access_transitions_remove(transition); 261 } 262 263 264 static int mesh_access_transitions_transaction_is_expired(mesh_transition_t * transition){ 265 return (btstack_run_loop_get_time_ms() - transition->transaction_timestamp_ms) > MEST_TRANSACTION_TIMEOUT_MS; 266 } 267 268 mesh_transaction_status_t mesh_access_transitions_transaction_status(mesh_transition_t * transition, uint8_t transaction_identifier, uint16_t src_address, uint16_t dst_address){ 269 if (transition->src_address != src_address || transition->dst_address != dst_address) return MESH_TRANSACTION_STATUS_DIFFERENT_DST_OR_SRC; 270 271 if (transition->transaction_identifier == transaction_identifier && !mesh_access_transitions_transaction_is_expired(transition)){ 272 return MESH_TRANSACTION_STATUS_RETRANSMISSION; 273 } 274 return MESH_TRANSACTION_STATUS_NEW; 275 } 276 277 uint8_t mesh_access_transitions_num_steps_from_gdtt(uint8_t time_gdtt){ 278 return time_gdtt & 0x3fu; 279 } 280 281 static uint32_t mesh_access_transitions_step_ms_from_gdtt(uint8_t time_gdtt){ 282 mesh_default_transition_step_resolution_t step_resolution = (mesh_default_transition_step_resolution_t) (time_gdtt >> 6); 283 switch (step_resolution){ 284 case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_100ms: 285 return 100; 286 case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_1s: 287 return 1000; 288 case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10s: 289 return 10000; 290 case MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10min: 291 return 600000; 292 default: 293 return 0; 294 } 295 } 296 297 uint32_t mesh_access_time_gdtt2ms(uint8_t time_gdtt){ 298 uint8_t num_steps = mesh_access_transitions_num_steps_from_gdtt(time_gdtt); 299 if (num_steps > 0x3E) return 0; 300 301 return mesh_access_transitions_step_ms_from_gdtt(time_gdtt) * num_steps; 302 } 303 304 uint8_t mesh_access_time_as_gdtt(uint32_t step_resolution_ms, uint32_t time_ms){ 305 uint8_t resolution; 306 switch (step_resolution_ms){ 307 case 100: 308 resolution = MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_100ms; 309 break; 310 case 1000: 311 resolution = MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_1s; 312 break; 313 case 10000: 314 resolution = MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10s; 315 break; 316 case 600000: 317 resolution = MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_10min; 318 break; 319 default: 320 resolution = MESH_DEFAULT_TRANSITION_STEP_RESOLUTION_100ms; 321 break; 322 } 323 uint8_t num_steps = time_ms / step_resolution_ms; 324 return (resolution << 6) | num_steps; 325 } 326 327 static void mesh_access_transitions_timeout_handler(btstack_timer_source_t * timer){ 328 btstack_linked_list_iterator_t it; 329 btstack_linked_list_iterator_init(&it, &transitions); 330 while (btstack_linked_list_iterator_has_next(&it)){ 331 mesh_transition_t * transition = (mesh_transition_t *)btstack_linked_list_iterator_next(&it); 332 (transition->transition_callback)(transition, TRANSITION_UPDATE, btstack_run_loop_get_time_ms()); 333 } 334 if (btstack_linked_list_empty(&transitions)) return; 335 336 btstack_run_loop_set_timer(timer, transition_step_min_ms); 337 btstack_run_loop_add_timer(timer); 338 } 339 340 static void mesh_access_transitions_timer_start(void){ 341 btstack_run_loop_remove_timer(&transitions_timer); 342 btstack_run_loop_set_timer_handler(&transitions_timer, mesh_access_transitions_timeout_handler); 343 btstack_run_loop_set_timer(&transitions_timer, transition_step_min_ms); 344 btstack_run_loop_add_timer(&transitions_timer); 345 } 346 347 static void mesh_access_transitions_timer_stop(void){ 348 btstack_run_loop_remove_timer(&transitions_timer); 349 } 350 351 static uint32_t mesh_access_transitions_get_step_min_ms(void){ 352 uint32_t min_timeout_ms = 0; 353 354 btstack_linked_list_iterator_t it; 355 btstack_linked_list_iterator_init(&it, &transitions); 356 while (btstack_linked_list_iterator_has_next(&it)){ 357 mesh_transition_t * transition = (mesh_transition_t *)btstack_linked_list_iterator_next(&it); 358 if (min_timeout_ms == 0 || transition->step_duration_ms < min_timeout_ms){ 359 min_timeout_ms = transition->step_duration_ms; 360 } 361 } 362 return min_timeout_ms; 363 } 364 365 void mesh_access_transitions_setup(mesh_transition_t * transition, mesh_model_t * mesh_model, 366 uint8_t transition_time_gdtt, uint8_t delay_gdtt, 367 void (* transition_callback)(struct mesh_transition * transition, transition_event_t event, uint32_t current_timestamp)){ 368 369 // Only values of 0x00 through 0x3E shall be used to specify the value of the Transition Number of Steps field 370 uint8_t num_steps = mesh_access_transitions_num_steps_from_gdtt(transition_time_gdtt); 371 if (num_steps >= MESH_TRANSITION_NUM_STEPS_INFINITE) return; 372 373 transition->state = MESH_TRANSITION_STATE_IDLE; 374 transition->phase_start_ms = 0; 375 376 transition->mesh_model = mesh_model; 377 transition->transition_callback = transition_callback; 378 transition->step_duration_ms = mesh_access_transitions_step_ms_from_gdtt(transition_time_gdtt); 379 transition->remaining_delay_time_ms = delay_gdtt * 5; 380 transition->remaining_transition_time_ms = num_steps * transition->step_duration_ms; 381 transition->num_steps = num_steps; 382 } 383 384 void mesh_access_transitions_add(mesh_transition_t * transition){ 385 if (transition->step_duration_ms == 0) return; 386 387 if (btstack_linked_list_empty(&transitions) || transition->step_duration_ms < transition_step_min_ms){ 388 transition_step_min_ms = transition->step_duration_ms; 389 } 390 mesh_access_transitions_timer_start(); 391 btstack_linked_list_add(&transitions, (btstack_linked_item_t *) transition); 392 (transition->transition_callback)(transition, TRANSITION_START, btstack_run_loop_get_time_ms()); 393 } 394 395 void mesh_access_transitions_remove(mesh_transition_t * transition){ 396 mesh_access_transitions_setup(transition, NULL, 0, 0, NULL); 397 btstack_linked_list_remove(&transitions, (btstack_linked_item_t *) transition); 398 399 if (btstack_linked_list_empty(&transitions)){ 400 mesh_access_transitions_timer_stop(); 401 } else { 402 transition_step_min_ms = mesh_access_transitions_get_step_min_ms(); 403 } 404 } 405 406 uint8_t mesh_access_transactions_get_next_transaction_id(void){ 407 mesh_transaction_id_counter++; 408 if (mesh_transaction_id_counter == 0){ 409 mesh_transaction_id_counter = 1; 410 } 411 return mesh_transaction_id_counter; 412 } 413 414 uint16_t mesh_pdu_ctl(mesh_pdu_t * pdu){ 415 switch (pdu->pdu_type){ 416 case MESH_PDU_TYPE_TRANSPORT: 417 return mesh_transport_ctl((mesh_transport_pdu_t*) pdu); 418 case MESH_PDU_TYPE_NETWORK: 419 return mesh_network_control((mesh_network_pdu_t *) pdu); 420 default: 421 return 0; 422 } 423 } 424 425 uint16_t mesh_pdu_ttl(mesh_pdu_t * pdu){ 426 switch (pdu->pdu_type){ 427 case MESH_PDU_TYPE_TRANSPORT: 428 return mesh_transport_ttl((mesh_transport_pdu_t*) pdu); 429 case MESH_PDU_TYPE_NETWORK: 430 return mesh_network_ttl((mesh_network_pdu_t *) pdu); 431 default: 432 return 0; 433 } 434 } 435 436 uint16_t mesh_pdu_src(mesh_pdu_t * pdu){ 437 switch (pdu->pdu_type){ 438 case MESH_PDU_TYPE_TRANSPORT: 439 return mesh_transport_src((mesh_transport_pdu_t*) pdu); 440 case MESH_PDU_TYPE_NETWORK: 441 return mesh_network_src((mesh_network_pdu_t *) pdu); 442 default: 443 return MESH_ADDRESS_UNSASSIGNED; 444 } 445 } 446 447 uint16_t mesh_pdu_dst(mesh_pdu_t * pdu){ 448 switch (pdu->pdu_type){ 449 case MESH_PDU_TYPE_TRANSPORT: 450 return mesh_transport_dst((mesh_transport_pdu_t*) pdu); 451 case MESH_PDU_TYPE_NETWORK: 452 return mesh_network_dst((mesh_network_pdu_t *) pdu); 453 default: 454 return MESH_ADDRESS_UNSASSIGNED; 455 } 456 } 457 458 uint16_t mesh_pdu_netkey_index(mesh_pdu_t * pdu){ 459 switch (pdu->pdu_type){ 460 case MESH_PDU_TYPE_TRANSPORT: 461 return ((mesh_transport_pdu_t*) pdu)->netkey_index; 462 case MESH_PDU_TYPE_NETWORK: 463 return ((mesh_network_pdu_t *) pdu)->netkey_index; 464 default: 465 return 0; 466 } 467 } 468 469 uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu){ 470 switch (pdu->pdu_type){ 471 case MESH_PDU_TYPE_TRANSPORT: 472 return ((mesh_transport_pdu_t*) pdu)->appkey_index; 473 case MESH_PDU_TYPE_NETWORK: 474 return ((mesh_network_pdu_t *) pdu)->appkey_index; 475 default: 476 return 0; 477 } 478 } 479 480 uint16_t mesh_pdu_len(mesh_pdu_t * pdu){ 481 switch (pdu->pdu_type){ 482 case MESH_PDU_TYPE_TRANSPORT: 483 return ((mesh_transport_pdu_t*) pdu)->len; 484 case MESH_PDU_TYPE_NETWORK: 485 return ((mesh_network_pdu_t *) pdu)->len - 10; 486 default: 487 return 0; 488 } 489 } 490 491 uint8_t * mesh_pdu_data(mesh_pdu_t * pdu){ 492 switch (pdu->pdu_type){ 493 case MESH_PDU_TYPE_TRANSPORT: 494 return ((mesh_transport_pdu_t*) pdu)->data; 495 case MESH_PDU_TYPE_NETWORK: 496 return &((mesh_network_pdu_t *) pdu)->data[10]; 497 default: 498 return NULL; 499 } 500 } 501 502 uint8_t mesh_pdu_control_opcode(mesh_pdu_t * pdu){ 503 switch (pdu->pdu_type){ 504 case MESH_PDU_TYPE_TRANSPORT: 505 return mesh_transport_control_opcode((mesh_transport_pdu_t*) pdu); 506 case MESH_PDU_TYPE_NETWORK: 507 return mesh_network_control_opcode((mesh_network_pdu_t *) pdu); 508 default: 509 return 0xff; 510 } 511 } 512 513 // message parser 514 515 static int mesh_access_get_opcode(uint8_t * buffer, uint16_t buffer_size, uint32_t * opcode, uint16_t * opcode_size){ 516 switch (buffer[0] >> 6){ 517 case 0: 518 case 1: 519 if (buffer[0] == 0x7f) return 0; 520 *opcode = buffer[0]; 521 *opcode_size = 1; 522 return 1; 523 case 2: 524 if (buffer_size < 2) return 0; 525 *opcode = big_endian_read_16(buffer, 0); 526 *opcode_size = 2; 527 return 1; 528 case 3: 529 if (buffer_size < 3) return 0; 530 *opcode = (buffer[0] << 16) | little_endian_read_16(buffer, 1); 531 *opcode_size = 3; 532 return 1; 533 default: 534 return 0; 535 } 536 } 537 538 static int mesh_access_transport_get_opcode(mesh_transport_pdu_t * transport_pdu, uint32_t * opcode, uint16_t * opcode_size){ 539 return mesh_access_get_opcode(transport_pdu->data, transport_pdu->len, opcode, opcode_size); 540 } 541 542 static int mesh_access_network_get_opcode(mesh_network_pdu_t * network_pdu, uint32_t * opcode, uint16_t * opcode_size){ 543 // TransMIC already removed by mesh_upper_transport_validate_unsegmented_message_ccm 544 return mesh_access_get_opcode(&network_pdu->data[10], network_pdu->len - 10, opcode, opcode_size); 545 } 546 547 int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size){ 548 switch (pdu->pdu_type){ 549 case MESH_PDU_TYPE_TRANSPORT: 550 return mesh_access_transport_get_opcode((mesh_transport_pdu_t*) pdu, opcode, opcode_size); 551 case MESH_PDU_TYPE_NETWORK: 552 return mesh_access_network_get_opcode((mesh_network_pdu_t *) pdu, opcode, opcode_size); 553 default: 554 return 0; 555 } 556 } 557 558 void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){ 559 state->data += bytes_to_skip; 560 state->len -= bytes_to_skip; 561 } 562 563 int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){ 564 state->data = mesh_pdu_data(pdu); 565 state->len = mesh_pdu_len(pdu); 566 567 uint16_t opcode_size = 0; 568 int ok = mesh_access_get_opcode(state->data, state->len, &state->opcode, &opcode_size); 569 if (ok){ 570 mesh_access_parser_skip(state, opcode_size); 571 } 572 return ok; 573 } 574 575 uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){ 576 return state->len; 577 } 578 579 uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){ 580 uint8_t value = *state->data; 581 mesh_access_parser_skip(state, 1); 582 return value; 583 } 584 585 uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){ 586 uint16_t value = little_endian_read_16(state->data, 0); 587 mesh_access_parser_skip(state, 2); 588 return value; 589 } 590 591 uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){ 592 uint32_t value = little_endian_read_24(state->data, 0); 593 mesh_access_parser_skip(state, 3); 594 return value; 595 } 596 597 uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){ 598 uint32_t value = little_endian_read_32(state->data, 0); 599 mesh_access_parser_skip(state, 4); 600 return value; 601 } 602 603 void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){ 604 reverse_128( state->data, dest); 605 mesh_access_parser_skip(state, 16); 606 } 607 608 void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){ 609 memcpy( dest, state->data, 16); 610 mesh_access_parser_skip(state, 16); 611 } 612 613 void mesh_access_parser_get_key(mesh_access_parser_state_t * state, uint8_t * dest){ 614 memcpy( dest, state->data, 16); 615 mesh_access_parser_skip(state, 16); 616 } 617 618 uint32_t mesh_access_parser_get_model_identifier(mesh_access_parser_state_t * parser){ 619 uint16_t vendor_id = BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC; 620 if (mesh_access_parser_available(parser) == 4){ 621 vendor_id = mesh_access_parser_get_u16(parser); 622 } 623 uint16_t model_id = mesh_access_parser_get_u16(parser); 624 return mesh_model_get_model_identifier(vendor_id, model_id); 625 } 626 627 // Mesh Access Message Builder 628 629 // message builder 630 631 static int mesh_access_setup_opcode(uint8_t * buffer, uint32_t opcode){ 632 if (opcode < 0x100){ 633 buffer[0] = opcode; 634 return 1; 635 } 636 if (opcode < 0x10000){ 637 big_endian_store_16(buffer, 0, opcode); 638 return 2; 639 } 640 buffer[0] = opcode >> 16; 641 little_endian_store_16(buffer, 1, opcode & 0xffff); 642 return 3; 643 } 644 645 mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode){ 646 mesh_transport_pdu_t * pdu = mesh_transport_pdu_get(); 647 if (!pdu) return NULL; 648 649 pdu->len = mesh_access_setup_opcode(pdu->data, opcode); 650 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 651 return pdu; 652 } 653 654 void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value){ 655 pdu->data[pdu->len++] = value; 656 } 657 658 void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value){ 659 little_endian_store_16(pdu->data, pdu->len, value); 660 pdu->len += 2; 661 } 662 663 void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value){ 664 little_endian_store_24(pdu->data, pdu->len, value); 665 pdu->len += 3; 666 } 667 668 void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value){ 669 little_endian_store_32(pdu->data, pdu->len, value); 670 pdu->len += 4; 671 } 672 void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier){ 673 if (!mesh_model_is_bluetooth_sig(model_identifier)){ 674 mesh_access_transport_add_uint16( pdu, mesh_model_get_vendor_id(model_identifier) ); 675 } 676 mesh_access_transport_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 677 } 678 679 mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode){ 680 mesh_network_pdu_t * pdu = mesh_network_pdu_get(); 681 if (!pdu) return NULL; 682 683 pdu->len = mesh_access_setup_opcode(&pdu->data[10], opcode) + 10; 684 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 685 return pdu; 686 } 687 688 void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value){ 689 pdu->data[pdu->len++] = value; 690 } 691 692 void mesh_access_network_add_uint16(mesh_network_pdu_t * pdu, uint16_t value){ 693 little_endian_store_16(pdu->data, pdu->len, value); 694 pdu->len += 2; 695 } 696 697 void mesh_access_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value){ 698 little_endian_store_24(pdu->data, pdu->len, value); 699 pdu->len += 3; 700 } 701 702 void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value){ 703 little_endian_store_32(pdu->data, pdu->len, value); 704 pdu->len += 4; 705 } 706 707 void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier){ 708 if (mesh_model_is_bluetooth_sig(model_identifier)){ 709 mesh_access_network_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 710 } else { 711 mesh_access_network_add_uint32( pdu, model_identifier ); 712 } 713 } 714 715 // access message template 716 717 mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *message_template, ...){ 718 mesh_network_pdu_t * network_pdu = mesh_access_network_init(message_template->opcode); 719 if (!network_pdu) return NULL; 720 721 va_list argptr; 722 va_start(argptr, message_template); 723 724 // add params 725 const char * format = message_template->format; 726 uint16_t word; 727 uint32_t longword; 728 while (*format){ 729 switch (*format){ 730 case '1': 731 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 732 mesh_access_network_add_uint8( network_pdu, word); 733 break; 734 case '2': 735 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 736 mesh_access_network_add_uint16( network_pdu, word); 737 break; 738 case '3': 739 longword = va_arg(argptr, uint32_t); 740 mesh_access_network_add_uint24( network_pdu, longword); 741 break; 742 case '4': 743 longword = va_arg(argptr, uint32_t); 744 mesh_access_network_add_uint32( network_pdu, longword); 745 break; 746 case 'm': 747 longword = va_arg(argptr, uint32_t); 748 mesh_access_network_add_model_identifier( network_pdu, longword); 749 break; 750 default: 751 log_error("Unsupported mesh message format specifier '%c", *format); 752 break; 753 } 754 format++; 755 } 756 757 va_end(argptr); 758 759 return network_pdu; 760 } 761 762 mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *message_template, ...){ 763 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(message_template->opcode); 764 if (!transport_pdu) return NULL; 765 766 va_list argptr; 767 va_start(argptr, message_template); 768 769 // add params 770 const char * format = message_template->format; 771 uint16_t word; 772 uint32_t longword; 773 while (*format){ 774 switch (*format++){ 775 case '1': 776 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 777 mesh_access_transport_add_uint8( transport_pdu, word); 778 break; 779 case '2': 780 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 781 mesh_access_transport_add_uint16( transport_pdu, word); 782 break; 783 case '3': 784 longword = va_arg(argptr, uint32_t); 785 mesh_access_transport_add_uint24( transport_pdu, longword); 786 break; 787 case '4': 788 longword = va_arg(argptr, uint32_t); 789 mesh_access_transport_add_uint32( transport_pdu, longword); 790 break; 791 case 'm': 792 longword = va_arg(argptr, uint32_t); 793 mesh_access_transport_add_model_identifier( transport_pdu, longword); 794 break; 795 default: 796 break; 797 } 798 } 799 800 va_end(argptr); 801 802 return transport_pdu; 803 } 804 805 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode){ 806 // find opcode in table 807 const mesh_operation_t * operation = model->operations; 808 if (operation == NULL) return NULL; 809 for ( ; operation->handler != NULL ; operation++){ 810 if (operation->opcode != opcode) continue; 811 return operation; 812 } 813 return NULL; 814 } 815 816 static const mesh_operation_t * mesh_model_lookup_operation(mesh_model_t * model, mesh_pdu_t * pdu){ 817 818 uint32_t opcode = 0; 819 uint16_t opcode_size = 0; 820 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 821 if (!ok) return NULL; 822 823 uint16_t len = mesh_pdu_len(pdu); 824 825 // find opcode in table 826 const mesh_operation_t * operation = model->operations; 827 if (operation == NULL) return NULL; 828 for ( ; operation->handler != NULL ; operation++){ 829 if (operation->opcode != opcode) continue; 830 if ((opcode_size + operation->minimum_length) > len) continue; 831 return operation; 832 } 833 return NULL; 834 } 835 836 static int mesh_access_validate_appkey_index(mesh_model_t * model, uint16_t appkey_index){ 837 // DeviceKey is valid for all models 838 if (appkey_index == MESH_DEVICE_KEY_INDEX) return 1; 839 // check if AppKey that is bound to this particular model 840 return mesh_model_contains_appkey(model, appkey_index); 841 } 842 843 // decrease use count and report as free if done 844 void mesh_access_message_processed(mesh_pdu_t * pdu){ 845 if (mesh_access_received_pdu_refcount > 0){ 846 mesh_access_received_pdu_refcount--; 847 } 848 if (mesh_access_received_pdu_refcount == 0){ 849 mesh_upper_transport_message_processed_by_higher_layer(pdu); 850 } 851 } 852 853 static void mesh_access_message_process_handler(mesh_pdu_t * pdu){ 854 855 // init use count 856 mesh_access_received_pdu_refcount = 1; 857 858 // get opcode and size 859 uint32_t opcode = 0; 860 uint16_t opcode_size = 0; 861 862 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 863 if (!ok) { 864 mesh_access_message_processed(pdu); 865 return; 866 } 867 868 uint16_t len = mesh_pdu_len(pdu); 869 printf("MESH Access Message, Opcode = %x: ", opcode); 870 printf_hexdump(mesh_pdu_data(pdu), len); 871 872 uint16_t src = mesh_pdu_src(pdu); 873 uint16_t dst = mesh_pdu_dst(pdu); 874 uint16_t appkey_index = mesh_pdu_appkey_index(pdu); 875 if (mesh_network_address_unicast(dst)){ 876 // loookup element by unicast address 877 mesh_element_t * element = mesh_node_element_for_unicast_address(dst); 878 if (element != NULL){ 879 // iterate over models, look for operation 880 mesh_model_iterator_t model_it; 881 mesh_model_iterator_init(&model_it, element); 882 while (mesh_model_iterator_has_next(&model_it)){ 883 mesh_model_t * model = mesh_model_iterator_next(&model_it); 884 // find opcode in table 885 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 886 if (operation == NULL) continue; 887 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 888 mesh_access_acknowledged_received(src, opcode); 889 mesh_access_received_pdu_refcount++; 890 operation->handler(model, pdu); 891 } 892 } 893 } 894 else if (mesh_network_address_group(dst)){ 895 896 // handle fixed group address 897 if (dst >= 0xff00){ 898 int deliver_to_primary_element = 1; 899 switch (dst){ 900 case MESH_ADDRESS_ALL_PROXIES: 901 if (mesh_foundation_gatt_proxy_get() == 1){ 902 deliver_to_primary_element = 1; 903 } 904 break; 905 case MESH_ADDRESS_ALL_FRIENDS: 906 // TODO: not implemented 907 break; 908 case MESH_ADDRESS_ALL_RELAYS: 909 if (mesh_foundation_relay_get() == 1){ 910 deliver_to_primary_element = 1; 911 } 912 break; 913 case MESH_ADDRESS_ALL_NODES: 914 deliver_to_primary_element = 1; 915 break; 916 default: 917 break; 918 } 919 if (deliver_to_primary_element){ 920 mesh_model_iterator_t model_it; 921 mesh_model_iterator_init(&model_it, mesh_node_get_primary_element()); 922 while (mesh_model_iterator_has_next(&model_it)){ 923 mesh_model_t * model = mesh_model_iterator_next(&model_it); 924 // find opcode in table 925 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 926 if (operation == NULL) continue; 927 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 928 mesh_access_acknowledged_received(src, opcode); 929 mesh_access_received_pdu_refcount++; 930 operation->handler(model, pdu); 931 } 932 } 933 } 934 else { 935 // iterate over all elements / models, check subscription list 936 mesh_element_iterator_t it; 937 mesh_element_iterator_init(&it); 938 while (mesh_element_iterator_has_next(&it)){ 939 mesh_element_t * element = (mesh_element_t *) mesh_element_iterator_next(&it); 940 mesh_model_iterator_t model_it; 941 mesh_model_iterator_init(&model_it, element); 942 while (mesh_model_iterator_has_next(&model_it)){ 943 mesh_model_t * model = mesh_model_iterator_next(&model_it); 944 if (mesh_model_contains_subscription(model, dst)){ 945 // find opcode in table 946 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 947 if (operation == NULL) continue; 948 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 949 mesh_access_acknowledged_received(src, opcode); 950 mesh_access_received_pdu_refcount++; 951 operation->handler(model, pdu); 952 } 953 } 954 } 955 } 956 } 957 958 // we're done 959 mesh_access_message_processed(pdu); 960 } 961 962 // Mesh Model Publication 963 static btstack_timer_source_t mesh_access_publication_timer; 964 965 static uint32_t mesh_model_publication_retransmit_count(uint8_t retransmit){ 966 return retransmit & 0x07u; 967 } 968 969 static uint32_t mesh_model_publication_retransmission_period_ms(uint8_t retransmit){ 970 return ((uint32_t)((retransmit >> 3) + 1)) * 50; 971 } 972 973 static void mesh_model_publication_setup_publication(mesh_publication_model_t * publication_model, uint32_t now){ 974 975 // set retransmit counter 976 publication_model->retransmit_count = mesh_model_publication_retransmit_count(publication_model->retransmit); 977 978 // schedule next publication or retransmission 979 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 980 981 // set next publication 982 if (publication_period_ms != 0){ 983 publication_model->next_publication_ms = now + publication_period_ms; 984 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 985 } else { 986 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 987 } 988 } 989 990 // assumes retransmit_count is valid 991 static void mesh_model_publication_setup_retransmission(mesh_publication_model_t * publication_model, uint32_t now){ 992 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 993 994 // retransmission done 995 if (publication_model->retransmit_count == 0) { 996 // wait for next main event if periodic and retransmission complete 997 if (publication_period_ms != 0){ 998 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 999 } else { 1000 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 1001 } 1002 return; 1003 } 1004 1005 // calc next retransmit time 1006 uint32_t retransmission_period_ms = mesh_model_publication_retransmission_period_ms(publication_model->retransmit) >> publication_model->period_divisor; 1007 uint32_t retransmission_ms = now + retransmission_period_ms; 1008 1009 // check next publication timeout is before next retransmission 1010 if (publication_period_ms != 0){ 1011 if (btstack_time_delta(retransmission_ms, publication_model->next_publication_ms) > 0) return; 1012 } 1013 1014 // schedule next retransmission 1015 publication_model->next_retransmit_ms = retransmission_ms; 1016 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS; 1017 } 1018 1019 static void mesh_model_publication_publish_now_model(mesh_model_t * mesh_model){ 1020 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1021 if (publication_model == NULL) return; 1022 if (publication_model->publish_state_fn == NULL) return; 1023 uint16_t dest = publication_model->address; 1024 if (dest == MESH_ADDRESS_UNSASSIGNED) return; 1025 uint16_t appkey_index = publication_model->appkey_index; 1026 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 1027 if (app_key == NULL) return; 1028 1029 // compose message 1030 mesh_pdu_t * pdu = (*publication_model->publish_state_fn)(mesh_model); 1031 if (pdu == NULL) return; 1032 1033 // handle ttl = default 1034 uint8_t ttl = publication_model->ttl; 1035 if (ttl == 0xff){ 1036 ttl = mesh_foundation_default_ttl_get(); 1037 } 1038 1039 mesh_upper_transport_setup_access_pdu_header(pdu, app_key->netkey_index, appkey_index, ttl, mesh_access_get_element_address(mesh_model), dest, 0); 1040 mesh_access_send_unacknowledged_pdu(pdu); 1041 } 1042 1043 static void mesh_model_publication_run(btstack_timer_source_t * ts){ 1044 1045 uint32_t now = btstack_run_loop_get_time_ms(); 1046 1047 // iterate over elements and models and handle time-based transitions 1048 mesh_element_iterator_t element_it; 1049 mesh_element_iterator_init(&element_it); 1050 while (mesh_element_iterator_has_next(&element_it)){ 1051 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1052 mesh_model_iterator_t model_it; 1053 mesh_model_iterator_init(&model_it, element); 1054 while (mesh_model_iterator_has_next(&model_it)){ 1055 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1056 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1057 if (publication_model == NULL) continue; 1058 1059 // check if either timer fired 1060 switch (publication_model->state){ 1061 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1062 if (btstack_time_delta(publication_model->next_publication_ms, now) > 0) break; 1063 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1064 break; 1065 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1066 if (btstack_time_delta(publication_model->next_retransmit_ms, now) > 0) break; 1067 publication_model->state = MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY; 1068 break; 1069 default: 1070 break; 1071 } 1072 1073 switch (publication_model->state){ 1074 case MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY: 1075 // schedule next publication and retransmission 1076 mesh_model_publication_setup_publication(publication_model, now); 1077 mesh_model_publication_setup_retransmission(publication_model, now); 1078 mesh_model_publication_publish_now_model(mesh_model); 1079 break; 1080 case MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY: 1081 // schedule next retransmission 1082 publication_model->retransmit_count--; 1083 mesh_model_publication_setup_retransmission(publication_model, now); 1084 mesh_model_publication_publish_now_model(mesh_model); 1085 break; 1086 default: 1087 break; 1088 } 1089 } 1090 } 1091 1092 int32_t next_timeout_ms = 0; 1093 mesh_element_iterator_init(&element_it); 1094 while (mesh_element_iterator_has_next(&element_it)){ 1095 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1096 mesh_model_iterator_t model_it; 1097 mesh_model_iterator_init(&model_it, element); 1098 while (mesh_model_iterator_has_next(&model_it)){ 1099 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1100 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1101 if (publication_model == NULL) continue; 1102 1103 // schedule next 1104 int32_t timeout_delta_ms; 1105 switch (publication_model->state){ 1106 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1107 timeout_delta_ms = btstack_time_delta(publication_model->next_publication_ms, now); 1108 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1109 next_timeout_ms = timeout_delta_ms; 1110 } 1111 break; 1112 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1113 timeout_delta_ms = btstack_time_delta(publication_model->next_retransmit_ms, now); 1114 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1115 next_timeout_ms = timeout_delta_ms; 1116 } 1117 break; 1118 default: 1119 break; 1120 } 1121 } 1122 } 1123 1124 // remove current timer if active 1125 if (ts == NULL){ 1126 btstack_run_loop_remove_timer(&mesh_access_publication_timer); 1127 1128 } 1129 1130 // new timeout? 1131 if (next_timeout_ms == 0) return; 1132 1133 // set timer 1134 btstack_run_loop_set_timer(&mesh_access_publication_timer, next_timeout_ms); 1135 btstack_run_loop_set_timer_handler(&mesh_access_publication_timer, mesh_model_publication_run); 1136 btstack_run_loop_add_timer(&mesh_access_publication_timer); 1137 } 1138 1139 void mesh_model_publication_start(mesh_model_t * mesh_model){ 1140 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1141 if (publication_model == NULL) return; 1142 1143 // publish right away 1144 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1145 mesh_model_publication_run(NULL); 1146 } 1147 1148 void mesh_model_publication_stop(mesh_model_t * mesh_model){ 1149 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1150 if (publication_model == NULL) return; 1151 1152 // reset state 1153 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 1154 } 1155 1156 void mesh_access_state_changed(mesh_model_t * mesh_model){ 1157 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1158 if (publication_model == NULL) return; 1159 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1160 mesh_model_publication_run(NULL); 1161 } 1162 1163