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 // Mesh Access Message Builder 587 588 // message builder 589 590 static int mesh_access_setup_opcode(uint8_t * buffer, uint32_t opcode){ 591 if (opcode < 0x100){ 592 buffer[0] = opcode; 593 return 1; 594 } 595 if (opcode < 0x10000){ 596 big_endian_store_16(buffer, 0, opcode); 597 return 2; 598 } 599 buffer[0] = opcode >> 16; 600 little_endian_store_16(buffer, 1, opcode & 0xffff); 601 return 3; 602 } 603 604 mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode){ 605 mesh_transport_pdu_t * pdu = mesh_transport_pdu_get(); 606 if (!pdu) return NULL; 607 608 pdu->len = mesh_access_setup_opcode(pdu->data, opcode); 609 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 610 return pdu; 611 } 612 613 void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value){ 614 pdu->data[pdu->len++] = value; 615 } 616 617 void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value){ 618 little_endian_store_16(pdu->data, pdu->len, value); 619 pdu->len += 2; 620 } 621 622 void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value){ 623 little_endian_store_24(pdu->data, pdu->len, value); 624 pdu->len += 3; 625 } 626 627 void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value){ 628 little_endian_store_32(pdu->data, pdu->len, value); 629 pdu->len += 4; 630 } 631 632 void mesh_access_transport_add_label_uuid(mesh_transport_pdu_t * pdu, uint8_t * value){ 633 (void)memcpy(value, pdu->data, 16); 634 pdu->len += 16; 635 } 636 637 void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier){ 638 if (!mesh_model_is_bluetooth_sig(model_identifier)){ 639 mesh_access_transport_add_uint16( pdu, mesh_model_get_vendor_id(model_identifier) ); 640 } 641 mesh_access_transport_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 642 } 643 644 mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode){ 645 mesh_network_pdu_t * pdu = mesh_network_pdu_get(); 646 if (!pdu) return NULL; 647 648 pdu->len = mesh_access_setup_opcode(&pdu->data[10], opcode) + 10; 649 pdu->pdu_header.ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 650 return pdu; 651 } 652 653 void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value){ 654 pdu->data[pdu->len++] = value; 655 } 656 657 void mesh_access_network_add_uint16(mesh_network_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_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value){ 663 little_endian_store_24(pdu->data, pdu->len, value); 664 pdu->len += 3; 665 } 666 667 void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value){ 668 little_endian_store_32(pdu->data, pdu->len, value); 669 pdu->len += 4; 670 } 671 672 void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier){ 673 if (mesh_model_is_bluetooth_sig(model_identifier)){ 674 mesh_access_network_add_uint16( pdu, mesh_model_get_model_id(model_identifier) ); 675 } else { 676 mesh_access_network_add_uint32( pdu, model_identifier ); 677 } 678 } 679 680 // access message template 681 682 mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *message_template, ...){ 683 mesh_network_pdu_t * network_pdu = mesh_access_network_init(message_template->opcode); 684 if (!network_pdu) return NULL; 685 686 va_list argptr; 687 va_start(argptr, message_template); 688 689 // add params 690 const char * format = message_template->format; 691 uint16_t word; 692 uint32_t longword; 693 while (*format){ 694 switch (*format){ 695 case '1': 696 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 697 mesh_access_network_add_uint8( network_pdu, word); 698 break; 699 case '2': 700 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 701 mesh_access_network_add_uint16( network_pdu, word); 702 break; 703 case '3': 704 longword = va_arg(argptr, uint32_t); 705 mesh_access_network_add_uint24( network_pdu, longword); 706 break; 707 case '4': 708 longword = va_arg(argptr, uint32_t); 709 mesh_access_network_add_uint32( network_pdu, longword); 710 break; 711 case 'm': 712 longword = va_arg(argptr, uint32_t); 713 mesh_access_network_add_model_identifier( network_pdu, longword); 714 break; 715 default: 716 log_error("Unsupported mesh message format specifier '%c", *format); 717 break; 718 } 719 format++; 720 } 721 722 va_end(argptr); 723 724 return network_pdu; 725 } 726 727 mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *message_template, ...){ 728 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(message_template->opcode); 729 if (!transport_pdu) return NULL; 730 731 va_list argptr; 732 va_start(argptr, message_template); 733 734 // add params 735 const char * format = message_template->format; 736 uint16_t word; 737 uint32_t longword; 738 uint8_t * ptr; 739 while (*format){ 740 switch (*format++){ 741 case '1': 742 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 743 mesh_access_transport_add_uint8( transport_pdu, word); 744 break; 745 case '2': 746 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 747 mesh_access_transport_add_uint16( transport_pdu, word); 748 break; 749 case '3': 750 longword = va_arg(argptr, uint32_t); 751 mesh_access_transport_add_uint24( transport_pdu, longword); 752 break; 753 case '4': 754 longword = va_arg(argptr, uint32_t); 755 mesh_access_transport_add_uint32( transport_pdu, longword); 756 break; 757 case 'P': // 16 byte, eg LabelUUID, in network endianess 758 ptr = va_arg(argptr, uint8_t *); 759 mesh_access_transport_add_label_uuid( transport_pdu, ptr); 760 break; 761 case 'm': 762 longword = va_arg(argptr, uint32_t); 763 mesh_access_transport_add_model_identifier( transport_pdu, longword); 764 break; 765 default: 766 break; 767 } 768 } 769 770 va_end(argptr); 771 772 return transport_pdu; 773 } 774 775 static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode){ 776 // find opcode in table 777 const mesh_operation_t * operation = model->operations; 778 if (operation == NULL) return NULL; 779 for ( ; operation->handler != NULL ; operation++){ 780 if (operation->opcode != opcode) continue; 781 return operation; 782 } 783 return NULL; 784 } 785 786 static const mesh_operation_t * mesh_model_lookup_operation(mesh_model_t * model, mesh_pdu_t * pdu){ 787 788 uint32_t opcode = 0; 789 uint16_t opcode_size = 0; 790 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 791 if (!ok) return NULL; 792 793 uint16_t len = mesh_pdu_len(pdu); 794 795 // find opcode in table 796 const mesh_operation_t * operation = model->operations; 797 if (operation == NULL) return NULL; 798 for ( ; operation->handler != NULL ; operation++){ 799 if (operation->opcode != opcode) continue; 800 if ((opcode_size + operation->minimum_length) > len) continue; 801 return operation; 802 } 803 return NULL; 804 } 805 806 static int mesh_access_validate_appkey_index(mesh_model_t * model, uint16_t appkey_index){ 807 // DeviceKey is valid for all models 808 if (appkey_index == MESH_DEVICE_KEY_INDEX) return 1; 809 // check if AppKey that is bound to this particular model 810 return mesh_model_contains_appkey(model, appkey_index); 811 } 812 813 // decrease use count and report as free if done 814 void mesh_access_message_processed(mesh_pdu_t * pdu){ 815 if (mesh_access_received_pdu_refcount > 0){ 816 mesh_access_received_pdu_refcount--; 817 } 818 if (mesh_access_received_pdu_refcount == 0){ 819 mesh_upper_transport_message_processed_by_higher_layer(pdu); 820 } 821 } 822 823 static void mesh_access_message_process_handler(mesh_pdu_t * pdu){ 824 825 // init use count 826 mesh_access_received_pdu_refcount = 1; 827 828 // get opcode and size 829 uint32_t opcode = 0; 830 uint16_t opcode_size = 0; 831 832 int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size); 833 if (!ok) { 834 mesh_access_message_processed(pdu); 835 return; 836 } 837 838 uint16_t len = mesh_pdu_len(pdu); 839 printf("MESH Access Message, Opcode = %x: ", opcode); 840 printf_hexdump(mesh_pdu_data(pdu), len); 841 842 uint16_t src = mesh_pdu_src(pdu); 843 uint16_t dst = mesh_pdu_dst(pdu); 844 uint16_t appkey_index = mesh_pdu_appkey_index(pdu); 845 if (mesh_network_address_unicast(dst)){ 846 // loookup element by unicast address 847 mesh_element_t * element = mesh_node_element_for_unicast_address(dst); 848 if (element != NULL){ 849 // iterate over models, look for operation 850 mesh_model_iterator_t model_it; 851 mesh_model_iterator_init(&model_it, element); 852 while (mesh_model_iterator_has_next(&model_it)){ 853 mesh_model_t * model = mesh_model_iterator_next(&model_it); 854 // find opcode in table 855 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 856 if (operation == NULL) continue; 857 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 858 mesh_access_acknowledged_received(src, opcode); 859 mesh_access_received_pdu_refcount++; 860 operation->handler(model, pdu); 861 } 862 } 863 } 864 else if (mesh_network_address_group(dst)){ 865 866 // handle fixed group address 867 if (dst >= 0xff00){ 868 int deliver_to_primary_element = 1; 869 switch (dst){ 870 case MESH_ADDRESS_ALL_PROXIES: 871 if (mesh_foundation_gatt_proxy_get() == 1){ 872 deliver_to_primary_element = 1; 873 } 874 break; 875 case MESH_ADDRESS_ALL_FRIENDS: 876 // TODO: not implemented 877 break; 878 case MESH_ADDRESS_ALL_RELAYS: 879 if (mesh_foundation_relay_get() == 1){ 880 deliver_to_primary_element = 1; 881 } 882 break; 883 case MESH_ADDRESS_ALL_NODES: 884 deliver_to_primary_element = 1; 885 break; 886 default: 887 break; 888 } 889 if (deliver_to_primary_element){ 890 mesh_model_iterator_t model_it; 891 mesh_model_iterator_init(&model_it, mesh_node_get_primary_element()); 892 while (mesh_model_iterator_has_next(&model_it)){ 893 mesh_model_t * model = mesh_model_iterator_next(&model_it); 894 // find opcode in table 895 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 896 if (operation == NULL) continue; 897 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 898 mesh_access_acknowledged_received(src, opcode); 899 mesh_access_received_pdu_refcount++; 900 operation->handler(model, pdu); 901 } 902 } 903 } 904 else { 905 // iterate over all elements / models, check subscription list 906 mesh_element_iterator_t it; 907 mesh_element_iterator_init(&it); 908 while (mesh_element_iterator_has_next(&it)){ 909 mesh_element_t * element = (mesh_element_t *) mesh_element_iterator_next(&it); 910 mesh_model_iterator_t model_it; 911 mesh_model_iterator_init(&model_it, element); 912 while (mesh_model_iterator_has_next(&model_it)){ 913 mesh_model_t * model = mesh_model_iterator_next(&model_it); 914 if (mesh_model_contains_subscription(model, dst)){ 915 // find opcode in table 916 const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu); 917 if (operation == NULL) continue; 918 if (mesh_access_validate_appkey_index(model, appkey_index) == 0) continue; 919 mesh_access_acknowledged_received(src, opcode); 920 mesh_access_received_pdu_refcount++; 921 operation->handler(model, pdu); 922 } 923 } 924 } 925 } 926 } 927 928 // we're done 929 mesh_access_message_processed(pdu); 930 } 931 932 // Mesh Model Publication 933 static btstack_timer_source_t mesh_access_publication_timer; 934 935 static uint32_t mesh_model_publication_retransmit_count(uint8_t retransmit){ 936 return retransmit & 0x07u; 937 } 938 939 static uint32_t mesh_model_publication_retransmission_period_ms(uint8_t retransmit){ 940 return ((uint32_t)((retransmit >> 3) + 1)) * 50; 941 } 942 943 static void mesh_model_publication_setup_publication(mesh_publication_model_t * publication_model, uint32_t now){ 944 945 // set retransmit counter 946 publication_model->retransmit_count = mesh_model_publication_retransmit_count(publication_model->retransmit); 947 948 // schedule next publication or retransmission 949 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 950 951 // set next publication 952 if (publication_period_ms != 0){ 953 publication_model->next_publication_ms = now + publication_period_ms; 954 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 955 } else { 956 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 957 } 958 } 959 960 // assumes retransmit_count is valid 961 static void mesh_model_publication_setup_retransmission(mesh_publication_model_t * publication_model, uint32_t now){ 962 uint32_t publication_period_ms = mesh_access_time_gdtt2ms(publication_model->period) >> publication_model->period_divisor; 963 964 // retransmission done 965 if (publication_model->retransmit_count == 0) { 966 // wait for next main event if periodic and retransmission complete 967 if (publication_period_ms != 0){ 968 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS; 969 } else { 970 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 971 } 972 return; 973 } 974 975 // calc next retransmit time 976 uint32_t retransmission_period_ms = mesh_model_publication_retransmission_period_ms(publication_model->retransmit) >> publication_model->period_divisor; 977 uint32_t retransmission_ms = now + retransmission_period_ms; 978 979 // check next publication timeout is before next retransmission 980 if (publication_period_ms != 0){ 981 if (btstack_time_delta(retransmission_ms, publication_model->next_publication_ms) > 0) return; 982 } 983 984 // schedule next retransmission 985 publication_model->next_retransmit_ms = retransmission_ms; 986 publication_model->state = MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS; 987 } 988 989 static void mesh_model_publication_publish_now_model(mesh_model_t * mesh_model){ 990 mesh_publication_model_t * publication_model = mesh_model->publication_model; 991 if (publication_model == NULL) return; 992 if (publication_model->publish_state_fn == NULL) return; 993 uint16_t dest = publication_model->address; 994 if (dest == MESH_ADDRESS_UNSASSIGNED) return; 995 uint16_t appkey_index = publication_model->appkey_index; 996 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 997 if (app_key == NULL) return; 998 999 // compose message 1000 mesh_pdu_t * pdu = (*publication_model->publish_state_fn)(mesh_model); 1001 if (pdu == NULL) return; 1002 1003 // handle ttl = default 1004 uint8_t ttl = publication_model->ttl; 1005 if (ttl == 0xff){ 1006 ttl = mesh_foundation_default_ttl_get(); 1007 } 1008 1009 mesh_upper_transport_setup_access_pdu_header(pdu, app_key->netkey_index, appkey_index, ttl, mesh_access_get_element_address(mesh_model), dest, 0); 1010 mesh_access_send_unacknowledged_pdu(pdu); 1011 } 1012 1013 static void mesh_model_publication_run(btstack_timer_source_t * ts){ 1014 1015 uint32_t now = btstack_run_loop_get_time_ms(); 1016 1017 // iterate over elements and models and handle time-based transitions 1018 mesh_element_iterator_t element_it; 1019 mesh_element_iterator_init(&element_it); 1020 while (mesh_element_iterator_has_next(&element_it)){ 1021 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1022 mesh_model_iterator_t model_it; 1023 mesh_model_iterator_init(&model_it, element); 1024 while (mesh_model_iterator_has_next(&model_it)){ 1025 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1026 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1027 if (publication_model == NULL) continue; 1028 1029 // check if either timer fired 1030 switch (publication_model->state){ 1031 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1032 if (btstack_time_delta(publication_model->next_publication_ms, now) > 0) break; 1033 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1034 break; 1035 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1036 if (btstack_time_delta(publication_model->next_retransmit_ms, now) > 0) break; 1037 publication_model->state = MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY; 1038 break; 1039 default: 1040 break; 1041 } 1042 1043 switch (publication_model->state){ 1044 case MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY: 1045 // schedule next publication and retransmission 1046 mesh_model_publication_setup_publication(publication_model, now); 1047 mesh_model_publication_setup_retransmission(publication_model, now); 1048 mesh_model_publication_publish_now_model(mesh_model); 1049 break; 1050 case MESH_MODEL_PUBLICATION_STATE_RETRANSMIT_READY: 1051 // schedule next retransmission 1052 publication_model->retransmit_count--; 1053 mesh_model_publication_setup_retransmission(publication_model, now); 1054 mesh_model_publication_publish_now_model(mesh_model); 1055 break; 1056 default: 1057 break; 1058 } 1059 } 1060 } 1061 1062 int32_t next_timeout_ms = 0; 1063 mesh_element_iterator_init(&element_it); 1064 while (mesh_element_iterator_has_next(&element_it)){ 1065 mesh_element_t * element = mesh_element_iterator_next(&element_it); 1066 mesh_model_iterator_t model_it; 1067 mesh_model_iterator_init(&model_it, element); 1068 while (mesh_model_iterator_has_next(&model_it)){ 1069 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 1070 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1071 if (publication_model == NULL) continue; 1072 1073 // schedule next 1074 int32_t timeout_delta_ms; 1075 switch (publication_model->state){ 1076 case MESH_MODEL_PUBLICATION_STATE_W4_PUBLICATION_MS: 1077 timeout_delta_ms = btstack_time_delta(publication_model->next_publication_ms, now); 1078 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1079 next_timeout_ms = timeout_delta_ms; 1080 } 1081 break; 1082 case MESH_MODEL_PUBLICATION_STATE_W4_RETRANSMIT_MS: 1083 timeout_delta_ms = btstack_time_delta(publication_model->next_retransmit_ms, now); 1084 if (next_timeout_ms == 0 || timeout_delta_ms < next_timeout_ms){ 1085 next_timeout_ms = timeout_delta_ms; 1086 } 1087 break; 1088 default: 1089 break; 1090 } 1091 } 1092 } 1093 1094 // remove current timer if active 1095 if (ts == NULL){ 1096 btstack_run_loop_remove_timer(&mesh_access_publication_timer); 1097 1098 } 1099 1100 // new timeout? 1101 if (next_timeout_ms == 0) return; 1102 1103 // set timer 1104 btstack_run_loop_set_timer(&mesh_access_publication_timer, next_timeout_ms); 1105 btstack_run_loop_set_timer_handler(&mesh_access_publication_timer, mesh_model_publication_run); 1106 btstack_run_loop_add_timer(&mesh_access_publication_timer); 1107 } 1108 1109 void mesh_model_publication_start(mesh_model_t * mesh_model){ 1110 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1111 if (publication_model == NULL) return; 1112 1113 // publish right away 1114 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1115 mesh_model_publication_run(NULL); 1116 } 1117 1118 void mesh_model_publication_stop(mesh_model_t * mesh_model){ 1119 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1120 if (publication_model == NULL) return; 1121 1122 // reset state 1123 publication_model->state = MESH_MODEL_PUBLICATION_STATE_IDLE; 1124 } 1125 1126 void mesh_access_state_changed(mesh_model_t * mesh_model){ 1127 mesh_publication_model_t * publication_model = mesh_model->publication_model; 1128 if (publication_model == NULL) return; 1129 publication_model->state = MESH_MODEL_PUBLICATION_STATE_PUBLICATION_READY; 1130 mesh_model_publication_run(NULL); 1131 } 1132 1133