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