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