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