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