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 > 0x3E) 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 } 382 383 void mesh_access_transitions_add(mesh_transition_t * transition){ 384 if (transition->step_duration_ms == 0) return; 385 386 if (btstack_linked_list_empty(&transitions) || transition->step_duration_ms < transition_step_min_ms){ 387 transition_step_min_ms = transition->step_duration_ms; 388 } 389 mesh_access_transitions_timer_start(); 390 btstack_linked_list_add(&transitions, (btstack_linked_item_t *) transition); 391 (transition->transition_callback)(transition, TRANSITION_START, btstack_run_loop_get_time_ms()); 392 } 393 394 void mesh_access_transitions_remove(mesh_transition_t * transition){ 395 mesh_access_transitions_setup(transition, NULL, 0, 0, NULL); 396 btstack_linked_list_remove(&transitions, (btstack_linked_item_t *) transition); 397 398 if (btstack_linked_list_empty(&transitions)){ 399 mesh_access_transitions_timer_stop(); 400 } else { 401 transition_step_min_ms = mesh_access_transitions_get_step_min_ms(); 402 } 403 } 404 405 uint8_t mesh_access_transactions_get_next_transaction_id(void){ 406 mesh_transaction_id_counter++; 407 if (mesh_transaction_id_counter == 0){ 408 mesh_transaction_id_counter = 1; 409 } 410 return mesh_transaction_id_counter; 411 } 412 413 uint16_t mesh_pdu_ctl(mesh_pdu_t * pdu){ 414 switch (pdu->pdu_type){ 415 case MESH_PDU_TYPE_TRANSPORT: 416 return mesh_transport_ctl((mesh_transport_pdu_t*) pdu); 417 case MESH_PDU_TYPE_NETWORK: 418 return mesh_network_control((mesh_network_pdu_t *) pdu); 419 default: 420 return 0; 421 } 422 } 423 424 uint16_t mesh_pdu_ttl(mesh_pdu_t * pdu){ 425 switch (pdu->pdu_type){ 426 case MESH_PDU_TYPE_TRANSPORT: 427 return mesh_transport_ttl((mesh_transport_pdu_t*) pdu); 428 case MESH_PDU_TYPE_NETWORK: 429 return mesh_network_ttl((mesh_network_pdu_t *) pdu); 430 default: 431 return 0; 432 } 433 } 434 435 uint16_t mesh_pdu_src(mesh_pdu_t * pdu){ 436 switch (pdu->pdu_type){ 437 case MESH_PDU_TYPE_TRANSPORT: 438 return mesh_transport_src((mesh_transport_pdu_t*) pdu); 439 case MESH_PDU_TYPE_NETWORK: 440 return mesh_network_src((mesh_network_pdu_t *) pdu); 441 default: 442 return MESH_ADDRESS_UNSASSIGNED; 443 } 444 } 445 446 uint16_t mesh_pdu_dst(mesh_pdu_t * pdu){ 447 switch (pdu->pdu_type){ 448 case MESH_PDU_TYPE_TRANSPORT: 449 return mesh_transport_dst((mesh_transport_pdu_t*) pdu); 450 case MESH_PDU_TYPE_NETWORK: 451 return mesh_network_dst((mesh_network_pdu_t *) pdu); 452 default: 453 return MESH_ADDRESS_UNSASSIGNED; 454 } 455 } 456 457 uint16_t mesh_pdu_netkey_index(mesh_pdu_t * pdu){ 458 switch (pdu->pdu_type){ 459 case MESH_PDU_TYPE_TRANSPORT: 460 return ((mesh_transport_pdu_t*) pdu)->netkey_index; 461 case MESH_PDU_TYPE_NETWORK: 462 return ((mesh_network_pdu_t *) pdu)->netkey_index; 463 default: 464 return 0; 465 } 466 } 467 468 uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu){ 469 switch (pdu->pdu_type){ 470 case MESH_PDU_TYPE_TRANSPORT: 471 return ((mesh_transport_pdu_t*) pdu)->appkey_index; 472 case MESH_PDU_TYPE_NETWORK: 473 return ((mesh_network_pdu_t *) pdu)->appkey_index; 474 default: 475 return 0; 476 } 477 } 478 479 uint16_t mesh_pdu_len(mesh_pdu_t * pdu){ 480 switch (pdu->pdu_type){ 481 case MESH_PDU_TYPE_TRANSPORT: 482 return ((mesh_transport_pdu_t*) pdu)->len; 483 case MESH_PDU_TYPE_NETWORK: 484 return ((mesh_network_pdu_t *) pdu)->len - 10; 485 default: 486 return 0; 487 } 488 } 489 490 uint8_t * mesh_pdu_data(mesh_pdu_t * pdu){ 491 switch (pdu->pdu_type){ 492 case MESH_PDU_TYPE_TRANSPORT: 493 return ((mesh_transport_pdu_t*) pdu)->data; 494 case MESH_PDU_TYPE_NETWORK: 495 return &((mesh_network_pdu_t *) pdu)->data[10]; 496 default: 497 return NULL; 498 } 499 } 500 501 uint8_t mesh_pdu_control_opcode(mesh_pdu_t * pdu){ 502 switch (pdu->pdu_type){ 503 case MESH_PDU_TYPE_TRANSPORT: 504 return mesh_transport_control_opcode((mesh_transport_pdu_t*) pdu); 505 case MESH_PDU_TYPE_NETWORK: 506 return mesh_network_control_opcode((mesh_network_pdu_t *) pdu); 507 default: 508 return 0xff; 509 } 510 } 511 512 // message parser 513 514 static int mesh_access_get_opcode(uint8_t * buffer, uint16_t buffer_size, uint32_t * opcode, uint16_t * opcode_size){ 515 switch (buffer[0] >> 6){ 516 case 0: 517 case 1: 518 if (buffer[0] == 0x7f) return 0; 519 *opcode = buffer[0]; 520 *opcode_size = 1; 521 return 1; 522 case 2: 523 if (buffer_size < 2) return 0; 524 *opcode = big_endian_read_16(buffer, 0); 525 *opcode_size = 2; 526 return 1; 527 case 3: 528 if (buffer_size < 3) return 0; 529 *opcode = (buffer[0] << 16) | little_endian_read_16(buffer, 1); 530 *opcode_size = 3; 531 return 1; 532 default: 533 return 0; 534 } 535 } 536 537 static int mesh_access_transport_get_opcode(mesh_transport_pdu_t * transport_pdu, uint32_t * opcode, uint16_t * opcode_size){ 538 return mesh_access_get_opcode(transport_pdu->data, transport_pdu->len, opcode, opcode_size); 539 } 540 541 static int mesh_access_network_get_opcode(mesh_network_pdu_t * network_pdu, uint32_t * opcode, uint16_t * opcode_size){ 542 // TransMIC already removed by mesh_upper_transport_validate_unsegmented_message_ccm 543 return mesh_access_get_opcode(&network_pdu->data[10], network_pdu->len - 10, opcode, opcode_size); 544 } 545 546 int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size){ 547 switch (pdu->pdu_type){ 548 case MESH_PDU_TYPE_TRANSPORT: 549 return mesh_access_transport_get_opcode((mesh_transport_pdu_t*) pdu, opcode, opcode_size); 550 case MESH_PDU_TYPE_NETWORK: 551 return mesh_access_network_get_opcode((mesh_network_pdu_t *) pdu, opcode, opcode_size); 552 default: 553 return 0; 554 } 555 } 556 557 void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){ 558 state->data += bytes_to_skip; 559 state->len -= bytes_to_skip; 560 } 561 562 int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){ 563 state->data = mesh_pdu_data(pdu); 564 state->len = mesh_pdu_len(pdu); 565 566 uint16_t opcode_size = 0; 567 int ok = mesh_access_get_opcode(state->data, state->len, &state->opcode, &opcode_size); 568 if (ok){ 569 mesh_access_parser_skip(state, opcode_size); 570 } 571 return ok; 572 } 573 574 uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){ 575 return state->len; 576 } 577 578 uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){ 579 uint8_t value = *state->data; 580 mesh_access_parser_skip(state, 1); 581 return value; 582 } 583 584 uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){ 585 uint16_t value = little_endian_read_16(state->data, 0); 586 mesh_access_parser_skip(state, 2); 587 return value; 588 } 589 590 uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){ 591 uint32_t value = little_endian_read_24(state->data, 0); 592 mesh_access_parser_skip(state, 3); 593 return value; 594 } 595 596 uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){ 597 uint32_t value = little_endian_read_32(state->data, 0); 598 mesh_access_parser_skip(state, 4); 599 return value; 600 } 601 602 void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){ 603 reverse_128( state->data, dest); 604 mesh_access_parser_skip(state, 16); 605 } 606 607 void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){ 608 memcpy( dest, state->data, 16); 609 mesh_access_parser_skip(state, 16); 610 } 611 612 void mesh_access_parser_get_key(mesh_access_parser_state_t * state, uint8_t * dest){ 613 memcpy( dest, state->data, 16); 614 mesh_access_parser_skip(state, 16); 615 } 616 617 uint32_t mesh_access_parser_get_model_identifier(mesh_access_parser_state_t * parser){ 618 uint16_t vendor_id = BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC; 619 if (mesh_access_parser_available(parser) == 4){ 620 vendor_id = mesh_access_parser_get_u16(parser); 621 } 622 uint16_t model_id = mesh_access_parser_get_u16(parser); 623 return mesh_model_get_model_identifier(vendor_id, model_id); 624 } 625 626 // Mesh Access Message Builder 627 628 // message builder 629 630 static int mesh_access_setup_opcode(uint8_t * buffer, uint32_t opcode){ 631 if (opcode < 0x100){ 632 buffer[0] = opcode; 633 return 1; 634 } 635 if (opcode < 0x10000){ 636 big_endian_store_16(buffer, 0, opcode); 637 return 2; 638 } 639 buffer[0] = opcode >> 16; 640 little_endian_store_16(buffer, 1, opcode & 0xffff); 641 return 3; 642 } 643 644 mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode){ 645 mesh_transport_pdu_t * pdu = mesh_transport_pdu_get(); 646 if (!pdu) return NULL; 647 648 pdu->len = mesh_access_setup_opcode(pdu->data, opcode); 649 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 650 return pdu; 651 } 652 653 void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value){ 654 pdu->data[pdu->len++] = value; 655 } 656 657 void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value){ 658 little_endian_store_16(pdu->data, pdu->len, value); 659 pdu->len += 2; 660 } 661 662 void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value){ 663 little_endian_store_24(pdu->data, pdu->len, value); 664 pdu->len += 3; 665 } 666 667 void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value){ 668 little_endian_store_32(pdu->data, pdu->len, value); 669 pdu->len += 4; 670 } 671 void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier){ 672 if (!mesh_model_is_bluetooth_sig(model_identifier)){ 673 mesh_access_transport_add_uint16( pdu, mesh_model_get_vendor_id(model_identifier) ); 674 } 675 mesh_access_transport_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 676 } 677 678 mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode){ 679 mesh_network_pdu_t * pdu = mesh_network_pdu_get(); 680 if (!pdu) return NULL; 681 682 pdu->len = mesh_access_setup_opcode(&pdu->data[10], opcode) + 10; 683 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 684 return pdu; 685 } 686 687 void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value){ 688 pdu->data[pdu->len++] = value; 689 } 690 691 void mesh_access_network_add_uint16(mesh_network_pdu_t * pdu, uint16_t value){ 692 little_endian_store_16(pdu->data, pdu->len, value); 693 pdu->len += 2; 694 } 695 696 void mesh_access_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value){ 697 little_endian_store_24(pdu->data, pdu->len, value); 698 pdu->len += 3; 699 } 700 701 void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value){ 702 little_endian_store_32(pdu->data, pdu->len, value); 703 pdu->len += 4; 704 } 705 706 void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier){ 707 if (mesh_model_is_bluetooth_sig(model_identifier)){ 708 mesh_access_network_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 709 } else { 710 mesh_access_network_add_uint32( pdu, model_identifier ); 711 } 712 } 713 714 // access message template 715 716 mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *message_template, ...){ 717 mesh_network_pdu_t * network_pdu = mesh_access_network_init(message_template->opcode); 718 if (!network_pdu) return NULL; 719 720 va_list argptr; 721 va_start(argptr, message_template); 722 723 // add params 724 const char * format = message_template->format; 725 uint16_t word; 726 uint32_t longword; 727 while (*format){ 728 switch (*format){ 729 case '1': 730 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 731 mesh_access_network_add_uint8( network_pdu, word); 732 break; 733 case '2': 734 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 735 mesh_access_network_add_uint16( network_pdu, word); 736 break; 737 case '3': 738 longword = va_arg(argptr, uint32_t); 739 mesh_access_network_add_uint24( network_pdu, longword); 740 break; 741 case '4': 742 longword = va_arg(argptr, uint32_t); 743 mesh_access_network_add_uint32( network_pdu, longword); 744 break; 745 case 'm': 746 longword = va_arg(argptr, uint32_t); 747 mesh_access_network_add_model_identifier( network_pdu, longword); 748 break; 749 default: 750 log_error("Unsupported mesh message format specifier '%c", *format); 751 break; 752 } 753 format++; 754 } 755 756 va_end(argptr); 757 758 return network_pdu; 759 } 760 761 mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *message_template, ...){ 762 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(message_template->opcode); 763 if (!transport_pdu) return NULL; 764 765 va_list argptr; 766 va_start(argptr, message_template); 767 768 // add params 769 const char * format = message_template->format; 770 uint16_t word; 771 uint32_t longword; 772 while (*format){ 773 switch (*format++){ 774 case '1': 775 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 776 mesh_access_transport_add_uint8( transport_pdu, word); 777 break; 778 case '2': 779 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 780 mesh_access_transport_add_uint16( transport_pdu, word); 781 break; 782 case '3': 783 longword = va_arg(argptr, uint32_t); 784 mesh_access_transport_add_uint24( transport_pdu, longword); 785 break; 786 case '4': 787 longword = va_arg(argptr, uint32_t); 788 mesh_access_transport_add_uint32( transport_pdu, longword); 789 break; 790 case 'm': 791 longword = va_arg(argptr, uint32_t); 792 mesh_access_transport_add_model_identifier( transport_pdu, longword); 793 break; 794 default: 795 break; 796 } 797 } 798 799 va_end(argptr); 800 801 return transport_pdu; 802 } 803 804 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode){ 805 // find opcode in table 806 const mesh_operation_t * operation = model->operations; 807 if (operation == NULL) return NULL; 808 for ( ; operation->handler != NULL ; operation++){ 809 if (operation->opcode != opcode) continue; 810 return operation; 811 } 812 return NULL; 813 } 814 815 static const mesh_operation_t * mesh_model_lookup_operation(mesh_model_t * model, mesh_pdu_t * pdu){ 816 817 uint32_t opcode = 0; 818 uint16_t opcode_size = 0; 819 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 820 if (!ok) return NULL; 821 822 uint16_t len = mesh_pdu_len(pdu); 823 824 // find opcode in table 825 const mesh_operation_t * operation = model->operations; 826 if (operation == NULL) return NULL; 827 for ( ; operation->handler != NULL ; operation++){ 828 if (operation->opcode != opcode) continue; 829 if ((opcode_size + operation->minimum_length) > len) continue; 830 return operation; 831 } 832 return NULL; 833 } 834 835 static int mesh_access_validate_appkey_index(mesh_model_t * model, uint16_t appkey_index){ 836 // DeviceKey is valid for all models 837 if (appkey_index == MESH_DEVICE_KEY_INDEX) return 1; 838 // check if AppKey that is bound to this particular model 839 return mesh_model_contains_appkey(model, appkey_index); 840 } 841 842 // decrease use count and report as free if done 843 void mesh_access_message_processed(mesh_pdu_t * pdu){ 844 if (mesh_access_received_pdu_refcount > 0){ 845 mesh_access_received_pdu_refcount--; 846 } 847 if (mesh_access_received_pdu_refcount == 0){ 848 mesh_upper_transport_message_processed_by_higher_layer(pdu); 849 } 850 } 851 852 static void mesh_access_message_process_handler(mesh_pdu_t * pdu){ 853 854 // init use count 855 mesh_access_received_pdu_refcount = 1; 856 857 // get opcode and size 858 uint32_t opcode = 0; 859 uint16_t opcode_size = 0; 860 861 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 862 if (!ok) { 863 mesh_access_message_processed(pdu); 864 return; 865 } 866 867 uint16_t len = mesh_pdu_len(pdu); 868 printf("MESH Access Message, Opcode = %x: ", opcode); 869 printf_hexdump(mesh_pdu_data(pdu), len); 870 871 uint16_t src = mesh_pdu_src(pdu); 872 uint16_t dst = mesh_pdu_dst(pdu); 873 uint16_t appkey_index = mesh_pdu_appkey_index(pdu); 874 if (mesh_network_address_unicast(dst)){ 875 // loookup element by unicast address 876 mesh_element_t * element = mesh_node_element_for_unicast_address(dst); 877 if (element != NULL){ 878 // iterate over models, look for operation 879 mesh_model_iterator_t model_it; 880 mesh_model_iterator_init(&model_it, element); 881 while (mesh_model_iterator_has_next(&model_it)){ 882 mesh_model_t * model = mesh_model_iterator_next(&model_it); 883 // find opcode in table 884 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 885 if (operation == NULL) continue; 886 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 887 mesh_access_acknowledged_received(src, opcode); 888 mesh_access_received_pdu_refcount++; 889 operation->handler(model, pdu); 890 } 891 } 892 } 893 else if (mesh_network_address_group(dst)){ 894 895 // handle fixed group address 896 if (dst >= 0xff00){ 897 int deliver_to_primary_element = 1; 898 switch (dst){ 899 case MESH_ADDRESS_ALL_PROXIES: 900 if (mesh_foundation_gatt_proxy_get() == 1){ 901 deliver_to_primary_element = 1; 902 } 903 break; 904 case MESH_ADDRESS_ALL_FRIENDS: 905 // TODO: not implemented 906 break; 907 case MESH_ADDRESS_ALL_RELAYS: 908 if (mesh_foundation_relay_get() == 1){ 909 deliver_to_primary_element = 1; 910 } 911 break; 912 case MESH_ADDRESS_ALL_NODES: 913 deliver_to_primary_element = 1; 914 break; 915 default: 916 break; 917 } 918 if (deliver_to_primary_element){ 919 mesh_model_iterator_t model_it; 920 mesh_model_iterator_init(&model_it, mesh_node_get_primary_element()); 921 while (mesh_model_iterator_has_next(&model_it)){ 922 mesh_model_t * model = mesh_model_iterator_next(&model_it); 923 // find opcode in table 924 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 925 if (operation == NULL) continue; 926 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 927 mesh_access_acknowledged_received(src, opcode); 928 mesh_access_received_pdu_refcount++; 929 operation->handler(model, pdu); 930 } 931 } 932 } 933 else { 934 // iterate over all elements / models, check subscription list 935 mesh_element_iterator_t it; 936 mesh_element_iterator_init(&it); 937 while (mesh_element_iterator_has_next(&it)){ 938 mesh_element_t * element = (mesh_element_t *) mesh_element_iterator_next(&it); 939 mesh_model_iterator_t model_it; 940 mesh_model_iterator_init(&model_it, element); 941 while (mesh_model_iterator_has_next(&model_it)){ 942 mesh_model_t * model = mesh_model_iterator_next(&model_it); 943 if (mesh_model_contains_subscription(model, dst)){ 944 // find opcode in table 945 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 946 if (operation == NULL) continue; 947 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 948 mesh_access_acknowledged_received(src, opcode); 949 mesh_access_received_pdu_refcount++; 950 operation->handler(model, pdu); 951 } 952 } 953 } 954 } 955 } 956 957 // we're done 958 mesh_access_message_processed(pdu); 959 } 960 961 // Mesh Model Publication 962 static btstack_timer_source_t mesh_access_publication_timer; 963 964 static uint32_t mesh_model_publication_retransmit_count(uint8_t retransmit){ 965 return retransmit & 0x07u; 966 } 967 968 static uint32_t mesh_model_publication_retransmission_period_ms(uint8_t retransmit){ 969 return ((uint32_t)((retransmit >> 3) + 1)) * 50; 970 } 971 972 static void mesh_model_publication_setup_publication(mesh_publication_model_t * publication_model, uint32_t now){ 973 974 // set retransmit counter 975 publication_model->retransmit_count = mesh_model_publication_retransmit_count(publication_model->retransmit); 976 977 // schedule next publication or retransmission 978 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 979 980 // set next publication 981 if (publication_period_ms != 0){ 982 publication_model->next_publication_ms = now + publication_period_ms; 983 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 984 } else { 985 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 986 } 987 } 988 989 // assumes retransmit_count is valid 990 static void mesh_model_publication_setup_retransmission(mesh_publication_model_t * publication_model, uint32_t now){ 991 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 992 993 // retransmission done 994 if (publication_model->retransmit_count == 0) { 995 // wait for next main event if periodic and retransmission complete 996 if (publication_period_ms != 0){ 997 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 998 } else { 999 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 1000 } 1001 return; 1002 } 1003 1004 // calc next retransmit time 1005 uint32_t retransmission_period_ms = mesh_model_publication_retransmission_period_ms(publication_model->retransmit) >> publication_model->period_divisor; 1006 uint32_t retransmission_ms = now + retransmission_period_ms; 1007 1008 // check next publication timeout is before next retransmission 1009 if (publication_period_ms != 0){ 1010 if (btstack_time_delta(retransmission_ms, publication_model->next_publication_ms) > 0) return; 1011 } 1012 1013 // schedule next retransmission 1014 publication_model->next_retransmit_ms = retransmission_ms; 1015 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS; 1016 } 1017 1018 static void mesh_model_publication_publish_now_model(mesh_model_t * mesh_model){ 1019 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1020 if (publication_model == NULL) return; 1021 if (publication_model->publish_state_fn == NULL) return; 1022 uint16_t dest = publication_model->address; 1023 if (dest == MESH_ADDRESS_UNSASSIGNED) return; 1024 uint16_t appkey_index = publication_model->appkey_index; 1025 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 1026 if (app_key == NULL) return; 1027 1028 // compose message 1029 mesh_pdu_t * pdu = (*publication_model->publish_state_fn)(mesh_model); 1030 if (pdu == NULL) return; 1031 1032 // handle ttl = default 1033 uint8_t ttl = publication_model->ttl; 1034 if (ttl == 0xff){ 1035 ttl = mesh_foundation_default_ttl_get(); 1036 } 1037 1038 mesh_upper_transport_setup_access_pdu_header(pdu, app_key->netkey_index, appkey_index, ttl, mesh_access_get_element_address(mesh_model), dest, 0); 1039 mesh_access_send_unacknowledged_pdu(pdu); 1040 } 1041 1042 static void mesh_model_publication_run(btstack_timer_source_t * ts){ 1043 1044 uint32_t now = btstack_run_loop_get_time_ms(); 1045 1046 // iterate over elements and models and handle time-based transitions 1047 mesh_element_iterator_t element_it; 1048 mesh_element_iterator_init(&element_it); 1049 while (mesh_element_iterator_has_next(&element_it)){ 1050 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1051 mesh_model_iterator_t model_it; 1052 mesh_model_iterator_init(&model_it, element); 1053 while (mesh_model_iterator_has_next(&model_it)){ 1054 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1055 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1056 if (publication_model == NULL) continue; 1057 1058 // check if either timer fired 1059 switch (publication_model->state){ 1060 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1061 if (btstack_time_delta(publication_model->next_publication_ms, now) > 0) break; 1062 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1063 break; 1064 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1065 if (btstack_time_delta(publication_model->next_retransmit_ms, now) > 0) break; 1066 publication_model->state = MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY; 1067 break; 1068 default: 1069 break; 1070 } 1071 1072 switch (publication_model->state){ 1073 case MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY: 1074 // schedule next publication and retransmission 1075 mesh_model_publication_setup_publication(publication_model, now); 1076 mesh_model_publication_setup_retransmission(publication_model, now); 1077 mesh_model_publication_publish_now_model(mesh_model); 1078 break; 1079 case MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY: 1080 // schedule next retransmission 1081 publication_model->retransmit_count--; 1082 mesh_model_publication_setup_retransmission(publication_model, now); 1083 mesh_model_publication_publish_now_model(mesh_model); 1084 break; 1085 default: 1086 break; 1087 } 1088 } 1089 } 1090 1091 int32_t next_timeout_ms = 0; 1092 mesh_element_iterator_init(&element_it); 1093 while (mesh_element_iterator_has_next(&element_it)){ 1094 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1095 mesh_model_iterator_t model_it; 1096 mesh_model_iterator_init(&model_it, element); 1097 while (mesh_model_iterator_has_next(&model_it)){ 1098 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1099 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1100 if (publication_model == NULL) continue; 1101 1102 // schedule next 1103 int32_t timeout_delta_ms; 1104 switch (publication_model->state){ 1105 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1106 timeout_delta_ms = btstack_time_delta(publication_model->next_publication_ms, now); 1107 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1108 next_timeout_ms = timeout_delta_ms; 1109 } 1110 break; 1111 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1112 timeout_delta_ms = btstack_time_delta(publication_model->next_retransmit_ms, now); 1113 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1114 next_timeout_ms = timeout_delta_ms; 1115 } 1116 break; 1117 default: 1118 break; 1119 } 1120 } 1121 } 1122 1123 // remove current timer if active 1124 if (ts == NULL){ 1125 btstack_run_loop_remove_timer(&mesh_access_publication_timer); 1126 1127 } 1128 1129 // new timeout? 1130 if (next_timeout_ms == 0) return; 1131 1132 // set timer 1133 btstack_run_loop_set_timer(&mesh_access_publication_timer, next_timeout_ms); 1134 btstack_run_loop_set_timer_handler(&mesh_access_publication_timer, mesh_model_publication_run); 1135 btstack_run_loop_add_timer(&mesh_access_publication_timer); 1136 } 1137 1138 void mesh_model_publication_start(mesh_model_t * mesh_model){ 1139 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1140 if (publication_model == NULL) return; 1141 1142 // publish right away 1143 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1144 mesh_model_publication_run(NULL); 1145 } 1146 1147 void mesh_model_publication_stop(mesh_model_t * mesh_model){ 1148 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1149 if (publication_model == NULL) return; 1150 1151 // reset state 1152 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 1153 } 1154 1155 void mesh_access_state_changed(mesh_model_t * mesh_model){ 1156 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1157 if (publication_model == NULL) return; 1158 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1159 mesh_model_publication_run(NULL); 1160 } 1161 1162