177ba3d3fSMatthias Ringwald /* 277ba3d3fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 377ba3d3fSMatthias Ringwald * 477ba3d3fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 577ba3d3fSMatthias Ringwald * modification, are permitted provided that the following conditions 677ba3d3fSMatthias Ringwald * are met: 777ba3d3fSMatthias Ringwald * 877ba3d3fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 977ba3d3fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1077ba3d3fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1177ba3d3fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1277ba3d3fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 1377ba3d3fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1477ba3d3fSMatthias Ringwald * contributors may be used to endorse or promote products derived 1577ba3d3fSMatthias Ringwald * from this software without specific prior written permission. 1677ba3d3fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1777ba3d3fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 1877ba3d3fSMatthias Ringwald * monetary gain. 1977ba3d3fSMatthias Ringwald * 2077ba3d3fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2177ba3d3fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2277ba3d3fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2377ba3d3fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2477ba3d3fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2577ba3d3fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2677ba3d3fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2777ba3d3fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2877ba3d3fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2977ba3d3fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3077ba3d3fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3177ba3d3fSMatthias Ringwald * SUCH DAMAGE. 3277ba3d3fSMatthias Ringwald * 3377ba3d3fSMatthias Ringwald * Please inquire about commercial licensing options at 3477ba3d3fSMatthias Ringwald * [email protected] 3577ba3d3fSMatthias Ringwald * 3677ba3d3fSMatthias Ringwald */ 3777ba3d3fSMatthias Ringwald 382d4000d1SMatthias Ringwald #define BTSTACK_FILE__ "mesh_lower_transport.c" 3977ba3d3fSMatthias Ringwald 4077ba3d3fSMatthias Ringwald #include <stdio.h> 4177ba3d3fSMatthias Ringwald #include <stdlib.h> 4277ba3d3fSMatthias Ringwald #include <string.h> 43fc5e2620SMatthias Ringwald #include <btstack.h> 44f4854a5eSMatthias Ringwald 4577ba3d3fSMatthias Ringwald #include "btstack_memory.h" 46f4854a5eSMatthias Ringwald #include "btstack_util.h" 4779eb95b0SMatthias Ringwald #include "btstack_bool.h" 48f4854a5eSMatthias Ringwald 49683cf298SMatthias Ringwald #include "mesh/beacon.h" 50f4854a5eSMatthias Ringwald #include "mesh/mesh_iv_index_seq_number.h" 51f4854a5eSMatthias Ringwald #include "mesh/mesh_lower_transport.h" 52683cf298SMatthias Ringwald #include "mesh/mesh_node.h" 53f4854a5eSMatthias Ringwald #include "mesh/mesh_peer.h" 5477ba3d3fSMatthias Ringwald 55b66a7a84SMatthias Ringwald #define LOG_LOWER_TRANSPORT 56b66a7a84SMatthias Ringwald 57*27e8a9aaSMatthias Ringwald // prototypes 5879eb95b0SMatthias Ringwald static void mesh_lower_transport_run(void); 5979eb95b0SMatthias Ringwald static void mesh_lower_transport_outgoing_complete(void); 60*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts); 6179eb95b0SMatthias Ringwald 6279eb95b0SMatthias Ringwald 6379eb95b0SMatthias Ringwald // lower transport outgoing state 6479eb95b0SMatthias Ringwald 65b4977f35SMatthias Ringwald // queued mesh_segmented_pdu_t or mesh_network_pdu_t 6679eb95b0SMatthias Ringwald static btstack_linked_list_t lower_transport_outgoing_ready; 6779eb95b0SMatthias Ringwald 68b4977f35SMatthias Ringwald // mesh_segmented_pdu_t to unicast address, segment transmission timer is active 69b4977f35SMatthias Ringwald static btstack_linked_list_t lower_transport_outgoing_waiting; 70b4977f35SMatthias Ringwald 7179eb95b0SMatthias Ringwald // active outgoing segmented message 7279eb95b0SMatthias Ringwald static mesh_segmented_pdu_t * lower_transport_outgoing_message; 7379eb95b0SMatthias Ringwald static uint16_t lower_transport_outgoing_seg_o; 7479eb95b0SMatthias Ringwald static mesh_network_pdu_t * lower_transport_outgoing_segment; 7579eb95b0SMatthias Ringwald 7679eb95b0SMatthias Ringwald // segment at network layer 7779eb95b0SMatthias Ringwald static bool lower_transport_outgoing_segment_queued; 7879eb95b0SMatthias Ringwald // transmission timeout occurred (while outgoing segment queued at network layer) 7979eb95b0SMatthias Ringwald static bool lower_transport_outgoing_transmission_timeout; 8079eb95b0SMatthias Ringwald // transmission completed either fully ack'ed or remote aborted (while outgoing segment queued at network layer) 8179eb95b0SMatthias Ringwald static bool lower_transport_outgoing_transmission_complete; 8279eb95b0SMatthias Ringwald 8379eb95b0SMatthias Ringwald // active outgoing unsegmented message 8479eb95b0SMatthias Ringwald static mesh_network_pdu_t * lower_transport_outgoing_network_pdu; 8579eb95b0SMatthias Ringwald 8679eb95b0SMatthias Ringwald // deliver to higher layer 8777ba3d3fSMatthias Ringwald static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 8879eb95b0SMatthias Ringwald static mesh_pdu_t * mesh_lower_transport_higher_layer_pdu; 8979eb95b0SMatthias Ringwald static btstack_linked_list_t mesh_lower_transport_queued_for_higher_layer; 9077ba3d3fSMatthias Ringwald 9177ba3d3fSMatthias Ringwald static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){ 9277ba3d3fSMatthias Ringwald printf("%-20s ", name); 9377ba3d3fSMatthias Ringwald printf_hexdump(data, len); 9477ba3d3fSMatthias Ringwald } 9577ba3d3fSMatthias Ringwald 9677ba3d3fSMatthias Ringwald // utility 9777ba3d3fSMatthias Ringwald 9879eb95b0SMatthias Ringwald mesh_segmented_pdu_t * mesh_segmented_pdu_get(void){ 99a4bbc09dSMatthias Ringwald mesh_segmented_pdu_t * message_pdu = btstack_memory_mesh_segmented_pdu_get(); 100926e8875SMatthias Ringwald if (message_pdu){ 101a4bbc09dSMatthias Ringwald message_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED; 102926e8875SMatthias Ringwald } 103926e8875SMatthias Ringwald return message_pdu; 104926e8875SMatthias Ringwald } 105926e8875SMatthias Ringwald 10679eb95b0SMatthias Ringwald void mesh_segmented_pdu_free(mesh_segmented_pdu_t * message_pdu){ 107cdcfd2c1SMatthias Ringwald while (message_pdu->segments){ 108cdcfd2c1SMatthias Ringwald mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments); 109cdcfd2c1SMatthias Ringwald mesh_network_pdu_free(segment); 110cdcfd2c1SMatthias Ringwald } 111a4bbc09dSMatthias Ringwald btstack_memory_mesh_segmented_pdu_free(message_pdu); 112926e8875SMatthias Ringwald } 113926e8875SMatthias Ringwald 114*27e8a9aaSMatthias Ringwald // INCOMING // 115*27e8a9aaSMatthias Ringwald 116*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_report_segments_as_processed(mesh_segmented_pdu_t * message_pdu) { 11735699c48SMatthias Ringwald while (message_pdu->segments){ 11835699c48SMatthias Ringwald mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments); 11935699c48SMatthias Ringwald mesh_network_message_processed_by_higher_layer(segment); 12035699c48SMatthias Ringwald } 12135699c48SMatthias Ringwald } 12235699c48SMatthias Ringwald 123*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_deliver_to_higher_layer(void){ 124fc5e2620SMatthias Ringwald if (mesh_lower_transport_higher_layer_pdu == NULL && !btstack_linked_list_empty(&mesh_lower_transport_queued_for_higher_layer)){ 1252ae11e2cSMatthias Ringwald mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&mesh_lower_transport_queued_for_higher_layer); 1262ae11e2cSMatthias Ringwald 1272ae11e2cSMatthias Ringwald switch (pdu->pdu_type){ 1285236fe42SMatthias Ringwald case MESH_PDU_TYPE_NETWORK: 1293d9c3b8eSMatthias Ringwald // unsegmented pdu 130e4121a34SMatthias Ringwald mesh_lower_transport_higher_layer_pdu = (mesh_pdu_t *) pdu; 131e4121a34SMatthias Ringwald pdu->pdu_type = MESH_PDU_TYPE_UNSEGMENTED; 1322ae11e2cSMatthias Ringwald break; 1335236fe42SMatthias Ringwald case MESH_PDU_TYPE_SEGMENTED: 1342ae11e2cSMatthias Ringwald // segmented control or access pdu 1352ae11e2cSMatthias Ringwald mesh_lower_transport_higher_layer_pdu = pdu; 1362ae11e2cSMatthias Ringwald break; 1375236fe42SMatthias Ringwald default: 1385236fe42SMatthias Ringwald btstack_assert(false); 1395236fe42SMatthias Ringwald break; 1402ae11e2cSMatthias Ringwald } 141fc5e2620SMatthias Ringwald higher_layer_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, mesh_lower_transport_higher_layer_pdu); 142fc5e2620SMatthias Ringwald } 143fc5e2620SMatthias Ringwald } 144fc5e2620SMatthias Ringwald 145*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_queue_for_higher_layer(mesh_pdu_t * pdu){ 146fc5e2620SMatthias Ringwald btstack_linked_list_add_tail(&mesh_lower_transport_queued_for_higher_layer, (btstack_linked_item_t *) pdu); 147*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_deliver_to_higher_layer(); 1489a0bb1c9SMatthias Ringwald } 14968d3bb6cSMatthias Ringwald 150*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_setup_acknowledge_message(uint8_t * data, uint8_t obo, uint16_t seq_zero, uint32_t block_ack){ 15177ba3d3fSMatthias Ringwald // printf("ACK Upper Transport, seq_zero %x\n", seq_zero); 15277ba3d3fSMatthias Ringwald data[0] = 0; // SEG = 0, Opcode = 0 15377ba3d3fSMatthias Ringwald big_endian_store_16( data, 1, (obo << 15) | (seq_zero << 2) | 0); // OBO, SeqZero, RFU 15477ba3d3fSMatthias Ringwald big_endian_store_32( data, 3, block_ack); 155b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 15677ba3d3fSMatthias Ringwald mesh_print_hex("ACK Upper Transport", data, 7); 157b66a7a84SMatthias Ringwald #endif 15877ba3d3fSMatthias Ringwald } 15977ba3d3fSMatthias Ringwald 160*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_send_ack(uint16_t netkey_index, uint8_t ttl, uint16_t dest, uint16_t seq_zero, uint32_t block_ack){ 16177ba3d3fSMatthias Ringwald // setup ack message 16277ba3d3fSMatthias Ringwald uint8_t ack_msg[7]; 163*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_setup_acknowledge_message(ack_msg, 0, seq_zero, block_ack); 16477ba3d3fSMatthias Ringwald // 16577ba3d3fSMatthias Ringwald // "3.4.5.2: The output filter of the interface connected to advertising or GATT bearers shall drop all messages with TTL value set to 1." 16677ba3d3fSMatthias Ringwald // if (ttl <= 1) return 0; 16777ba3d3fSMatthias Ringwald 16877ba3d3fSMatthias Ringwald // TODO: check transport_pdu_len depending on ctl 16977ba3d3fSMatthias Ringwald 17077ba3d3fSMatthias Ringwald // lookup network by netkey_index 17177ba3d3fSMatthias Ringwald const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 17277ba3d3fSMatthias Ringwald if (!network_key) return; 17377ba3d3fSMatthias Ringwald 17477ba3d3fSMatthias Ringwald // allocate network_pdu 17577ba3d3fSMatthias Ringwald mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 17677ba3d3fSMatthias Ringwald if (!network_pdu) return; 17777ba3d3fSMatthias Ringwald 17877ba3d3fSMatthias Ringwald // setup network_pdu 179001c65e0SMatthias Ringwald mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, mesh_sequence_number_next(), mesh_node_get_primary_element_address(), dest, ack_msg, sizeof(ack_msg)); 18077ba3d3fSMatthias Ringwald 18177ba3d3fSMatthias Ringwald // send network_pdu 18277ba3d3fSMatthias Ringwald mesh_network_send_pdu(network_pdu); 18377ba3d3fSMatthias Ringwald } 18477ba3d3fSMatthias Ringwald 185*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_send_ack_for_segmented_pdu(mesh_segmented_pdu_t * segmented_pdu){ 186*27e8a9aaSMatthias Ringwald uint16_t seq_zero = segmented_pdu->seq_zero; 187*27e8a9aaSMatthias Ringwald uint8_t ttl = segmented_pdu->ctl_ttl & 0x7f; 188*27e8a9aaSMatthias Ringwald uint16_t dest = segmented_pdu->src; 189*27e8a9aaSMatthias Ringwald uint16_t netkey_index = segmented_pdu->netkey_index; 190b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 19177ba3d3fSMatthias Ringwald printf("mesh_transport_send_ack_for_transport_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n", 192*27e8a9aaSMatthias Ringwald segmented_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest); 193b66a7a84SMatthias Ringwald #endif 194*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, segmented_pdu->block_ack); 19577ba3d3fSMatthias Ringwald } 19677ba3d3fSMatthias Ringwald 197*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_send_ack_for_network_pdu(mesh_network_pdu_t *network_pdu, uint16_t seq_zero, uint32_t block_ack) { 19877ba3d3fSMatthias Ringwald uint8_t ttl = mesh_network_ttl(network_pdu); 19977ba3d3fSMatthias Ringwald uint16_t dest = mesh_network_src(network_pdu); 20077ba3d3fSMatthias Ringwald uint16_t netkey_index = network_pdu->netkey_index; 201b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 20277ba3d3fSMatthias Ringwald printf("mesh_transport_send_ack_for_network_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n", 203001c65e0SMatthias Ringwald network_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest); 204b66a7a84SMatthias Ringwald #endif 205*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, block_ack); 20677ba3d3fSMatthias Ringwald } 20777ba3d3fSMatthias Ringwald 208*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){ 209*27e8a9aaSMatthias Ringwald if (!segmented_pdu->acknowledgement_timer_active) return; 210*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 0; 211*27e8a9aaSMatthias Ringwald btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer); 212926e8875SMatthias Ringwald } 213926e8875SMatthias Ringwald 214*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_incomplete_timer(mesh_segmented_pdu_t *segmented_pdu){ 215*27e8a9aaSMatthias Ringwald if (!segmented_pdu->incomplete_timer_active) return; 216*27e8a9aaSMatthias Ringwald segmented_pdu->incomplete_timer_active = 0; 217*27e8a9aaSMatthias Ringwald btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer); 218926e8875SMatthias Ringwald } 219926e8875SMatthias Ringwald 220*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_segmented_message_complete(mesh_segmented_pdu_t * segmented_pdu){ 22177ba3d3fSMatthias Ringwald // set flag 222*27e8a9aaSMatthias Ringwald segmented_pdu->message_complete = 1; 22377ba3d3fSMatthias Ringwald // stop timers 224*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_stop_acknowledgment_timer(segmented_pdu); 225*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_stop_incomplete_timer(segmented_pdu); 22677ba3d3fSMatthias Ringwald // stop reassembly 227*27e8a9aaSMatthias Ringwald mesh_peer_t * peer = mesh_peer_for_addr(segmented_pdu->src); 22877ba3d3fSMatthias Ringwald if (peer){ 229926e8875SMatthias Ringwald peer->message_pdu = NULL; 230926e8875SMatthias Ringwald } 23177ba3d3fSMatthias Ringwald } 23277ba3d3fSMatthias Ringwald 233*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_ack_timeout(btstack_timer_source_t *ts){ 234*27e8a9aaSMatthias Ringwald mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts); 235b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 236*27e8a9aaSMatthias Ringwald printf("ACK: acknowledgement timer fired for %p, send ACK\n", segmented_pdu); 237b66a7a84SMatthias Ringwald #endif 238*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 0; 239*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_send_ack_for_segmented_pdu(segmented_pdu); 24077ba3d3fSMatthias Ringwald } 24177ba3d3fSMatthias Ringwald 242*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_incomplete_timeout(btstack_timer_source_t *ts){ 243*27e8a9aaSMatthias Ringwald mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts); 244b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 245*27e8a9aaSMatthias Ringwald printf("mesh_lower_transport_incoming_incomplete_timeout for %p - give up\n", segmented_pdu); 246b66a7a84SMatthias Ringwald #endif 247*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_segmented_message_complete(segmented_pdu); 2488a504b30SMatthias Ringwald // free segments 249*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_report_segments_as_processed(segmented_pdu); 25077ba3d3fSMatthias Ringwald // free message 251*27e8a9aaSMatthias Ringwald btstack_memory_mesh_segmented_pdu_free(segmented_pdu); 25277ba3d3fSMatthias Ringwald } 25377ba3d3fSMatthias Ringwald 254*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_start_acknowledgment_timer(mesh_segmented_pdu_t * segmented_pdu, uint32_t timeout){ 255b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 256*27e8a9aaSMatthias Ringwald printf("ACK: start rx ack timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout); 257b66a7a84SMatthias Ringwald #endif 258*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout); 259*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_incoming_ack_timeout); 260*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, segmented_pdu); 261*27e8a9aaSMatthias Ringwald btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer); 262*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 1; 26377ba3d3fSMatthias Ringwald } 26477ba3d3fSMatthias Ringwald 265*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_restart_incomplete_timer(mesh_segmented_pdu_t * segmented_pdu, uint32_t timeout, 26677ba3d3fSMatthias Ringwald void (*callback)(btstack_timer_source_t *ts)){ 267b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 268*27e8a9aaSMatthias Ringwald printf("RX-(re)start incomplete timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout); 269b66a7a84SMatthias Ringwald #endif 270*27e8a9aaSMatthias Ringwald if (segmented_pdu->incomplete_timer_active){ 271*27e8a9aaSMatthias Ringwald btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer); 27277ba3d3fSMatthias Ringwald } 273*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer(&segmented_pdu->incomplete_timer, timeout); 274*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_handler(&segmented_pdu->incomplete_timer, callback); 275*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_context(&segmented_pdu->incomplete_timer, segmented_pdu); 276*27e8a9aaSMatthias Ringwald btstack_run_loop_add_timer(&segmented_pdu->incomplete_timer); 277*27e8a9aaSMatthias Ringwald segmented_pdu->incomplete_timer_active = 1; 27877ba3d3fSMatthias Ringwald } 27977ba3d3fSMatthias Ringwald 280*27e8a9aaSMatthias Ringwald static mesh_segmented_pdu_t * mesh_lower_transport_incoming_pdu_for_segmented_message(mesh_network_pdu_t *network_pdu){ 28177ba3d3fSMatthias Ringwald uint16_t src = mesh_network_src(network_pdu); 28277ba3d3fSMatthias Ringwald uint16_t seq_zero = ( big_endian_read_16(mesh_network_pdu_data(network_pdu), 1) >> 2) & 0x1fff; 283b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 28477ba3d3fSMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: seq_zero %x\n", seq_zero); 285b66a7a84SMatthias Ringwald #endif 28677ba3d3fSMatthias Ringwald mesh_peer_t * peer = mesh_peer_for_addr(src); 28777ba3d3fSMatthias Ringwald if (!peer) { 28877ba3d3fSMatthias Ringwald return NULL; 28977ba3d3fSMatthias Ringwald } 290b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 29177ba3d3fSMatthias Ringwald printf("mesh_seq_zero_validate(%x, %x) -- last (%x, %x)\n", src, seq_zero, peer->address, peer->seq_zero); 292b66a7a84SMatthias Ringwald #endif 29377ba3d3fSMatthias Ringwald 29477ba3d3fSMatthias Ringwald // reception of transport message ongoing 295926e8875SMatthias Ringwald if (peer->message_pdu){ 29677ba3d3fSMatthias Ringwald // check if segment for same seq zero 2973945bdb6SMatthias Ringwald uint16_t active_seq_zero = peer->message_pdu->seq_zero; 29877ba3d3fSMatthias Ringwald if (active_seq_zero == seq_zero) { 299b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 30077ba3d3fSMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: segment for current transport pdu with SeqZero %x\n", active_seq_zero); 301b66a7a84SMatthias Ringwald #endif 302926e8875SMatthias Ringwald return peer->message_pdu; 30377ba3d3fSMatthias Ringwald } else { 30477ba3d3fSMatthias Ringwald // seq zero differs from current transport pdu, but current pdu is not complete 305b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 30677ba3d3fSMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: drop segment. current transport pdu SeqZero %x, now %x\n", active_seq_zero, seq_zero); 307b66a7a84SMatthias Ringwald #endif 30877ba3d3fSMatthias Ringwald return NULL; 30977ba3d3fSMatthias Ringwald } 31077ba3d3fSMatthias Ringwald } 31177ba3d3fSMatthias Ringwald 31277ba3d3fSMatthias Ringwald // send ACK if segment for previously completed transport pdu (no ongoing reception, block ack is cleared) 31377ba3d3fSMatthias Ringwald if ((seq_zero == peer->seq_zero) && (peer->block_ack != 0)){ 314b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 31577ba3d3fSMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: segment for last completed message. send ack\n"); 316b66a7a84SMatthias Ringwald #endif 317*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_send_ack_for_network_pdu(network_pdu, seq_zero, peer->block_ack); 31877ba3d3fSMatthias Ringwald return NULL; 31977ba3d3fSMatthias Ringwald } 32077ba3d3fSMatthias Ringwald 32177ba3d3fSMatthias Ringwald // reconstruct lowest 24 bit of SeqAuth 32277ba3d3fSMatthias Ringwald uint32_t seq = mesh_network_seq(network_pdu); 32377ba3d3fSMatthias Ringwald uint32_t seq_auth = (seq & 0xffe000) | seq_zero; 32477ba3d3fSMatthias Ringwald if (seq_auth > seq){ 32577ba3d3fSMatthias Ringwald seq_auth -= 0x2000; 32677ba3d3fSMatthias Ringwald } 32777ba3d3fSMatthias Ringwald 32877ba3d3fSMatthias Ringwald // no transport pdu active, check new message: seq auth is greater OR seq auth is same but no segments 32977ba3d3fSMatthias Ringwald if (seq_auth > peer->seq_auth || (seq_auth == peer->seq_auth && peer->block_ack == 0)){ 33079eb95b0SMatthias Ringwald mesh_segmented_pdu_t * pdu = mesh_segmented_pdu_get(); 33177ba3d3fSMatthias Ringwald if (!pdu) return NULL; 33277ba3d3fSMatthias Ringwald 33377ba3d3fSMatthias Ringwald // cache network pdu header 3347cb45abfSMatthias Ringwald pdu->ivi_nid = network_pdu->data[0]; 3357cb45abfSMatthias Ringwald pdu->ctl_ttl = network_pdu->data[1]; 3367cb45abfSMatthias Ringwald pdu->src = big_endian_read_16(network_pdu->data, 5); 3377cb45abfSMatthias Ringwald pdu->dst = big_endian_read_16(network_pdu->data, 7); 33877ba3d3fSMatthias Ringwald // store lower 24 bit of SeqAuth for App / Device Nonce 3397cb45abfSMatthias Ringwald pdu->seq = seq_auth; 34077ba3d3fSMatthias Ringwald 34177ba3d3fSMatthias Ringwald // store meta data in new pdu 34277ba3d3fSMatthias Ringwald pdu->netkey_index = network_pdu->netkey_index; 34377ba3d3fSMatthias Ringwald pdu->block_ack = 0; 34477ba3d3fSMatthias Ringwald pdu->acknowledgement_timer_active = 0; 34577ba3d3fSMatthias Ringwald pdu->message_complete = 0; 34677ba3d3fSMatthias Ringwald pdu->seq_zero = seq_zero; 34777ba3d3fSMatthias Ringwald 34877ba3d3fSMatthias Ringwald // update peer info 349926e8875SMatthias Ringwald peer->message_pdu = pdu; 35077ba3d3fSMatthias Ringwald peer->seq_zero = seq_zero; 35177ba3d3fSMatthias Ringwald peer->seq_auth = seq_auth; 35277ba3d3fSMatthias Ringwald peer->block_ack = 0; 35377ba3d3fSMatthias Ringwald 354b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 3553945bdb6SMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: setup transport pdu %p for src %x, seq %06x, seq_zero %x\n", pdu, src, 3563945bdb6SMatthias Ringwald pdu->seq, seq_zero); 357b66a7a84SMatthias Ringwald #endif 358926e8875SMatthias Ringwald return peer->message_pdu; 35977ba3d3fSMatthias Ringwald } else { 36077ba3d3fSMatthias Ringwald // seq zero differs from current transport pdu 361b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 36277ba3d3fSMatthias Ringwald printf("mesh_transport_pdu_for_segmented_message: drop segment for old seq %x\n", seq_zero); 363b66a7a84SMatthias Ringwald #endif 36477ba3d3fSMatthias Ringwald return NULL; 36577ba3d3fSMatthias Ringwald } 36677ba3d3fSMatthias Ringwald } 36777ba3d3fSMatthias Ringwald 368*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_process_segment(mesh_segmented_pdu_t * message_pdu, mesh_network_pdu_t * network_pdu){ 36977ba3d3fSMatthias Ringwald 37077ba3d3fSMatthias Ringwald uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 37177ba3d3fSMatthias Ringwald uint8_t lower_transport_pdu_len = mesh_network_pdu_len(network_pdu); 37277ba3d3fSMatthias Ringwald 37377ba3d3fSMatthias Ringwald // get akf_aid & transmic 374926e8875SMatthias Ringwald message_pdu->akf_aid_control = lower_transport_pdu[0] & 0x7f; 375926e8875SMatthias Ringwald message_pdu->transmic_len = lower_transport_pdu[1] & 0x80 ? 8 : 4; 37677ba3d3fSMatthias Ringwald 37777ba3d3fSMatthias Ringwald // get seq_zero 37877ba3d3fSMatthias Ringwald uint16_t seq_zero = ( big_endian_read_16(lower_transport_pdu, 1) >> 2) & 0x1fff; 37977ba3d3fSMatthias Ringwald 38077ba3d3fSMatthias Ringwald // get seg fields 38177ba3d3fSMatthias Ringwald uint8_t seg_o = ( big_endian_read_16(lower_transport_pdu, 2) >> 5) & 0x001f; 38277ba3d3fSMatthias Ringwald uint8_t seg_n = lower_transport_pdu[3] & 0x1f; 38377ba3d3fSMatthias Ringwald uint8_t segment_len = lower_transport_pdu_len - 4; 38477ba3d3fSMatthias Ringwald uint8_t * segment_data = &lower_transport_pdu[4]; 38577ba3d3fSMatthias Ringwald 386b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 387*27e8a9aaSMatthias Ringwald printf("mesh_lower_transport_incoming_process_segment: seq zero %04x, seg_o %02x, seg_n %02x, transmic len: %u\n", seq_zero, seg_o, seg_n, message_pdu->transmic_len * 8); 38877ba3d3fSMatthias Ringwald mesh_print_hex("Segment", segment_data, segment_len); 389b66a7a84SMatthias Ringwald #endif 39077ba3d3fSMatthias Ringwald 391926e8875SMatthias Ringwald // drop if already stored 392926e8875SMatthias Ringwald if ((message_pdu->block_ack & (1<<seg_o)) != 0){ 393926e8875SMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 394926e8875SMatthias Ringwald return; 395926e8875SMatthias Ringwald } 396926e8875SMatthias Ringwald 39777ba3d3fSMatthias Ringwald // mark as received 398926e8875SMatthias Ringwald message_pdu->block_ack |= (1<<seg_o); 399926e8875SMatthias Ringwald 40016fd08f9SMatthias Ringwald // store segment 40116fd08f9SMatthias Ringwald uint8_t max_segment_len = mesh_network_control(network_pdu) ? 8 : 12; 40216fd08f9SMatthias Ringwald mesh_network_pdu_t * latest_segment = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments); 40316fd08f9SMatthias Ringwald if ((latest_segment != NULL) && ((MESH_NETWORK_PAYLOAD_MAX - latest_segment->len) > (max_segment_len + 1))){ 40416fd08f9SMatthias Ringwald // store in last added segment if there is enough space available 40516fd08f9SMatthias Ringwald latest_segment->data[latest_segment->len++] = seg_o; 40616fd08f9SMatthias Ringwald (void) memcpy(&latest_segment->data[latest_segment->len], &lower_transport_pdu[4], segment_len); 40716fd08f9SMatthias Ringwald latest_segment->len += segment_len; 40816fd08f9SMatthias Ringwald // free buffer 40916fd08f9SMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 41016fd08f9SMatthias Ringwald } else { 41116fd08f9SMatthias Ringwald // move to beginning 41216fd08f9SMatthias Ringwald network_pdu->data[0] = seg_o; 41316fd08f9SMatthias Ringwald uint8_t i; 41416fd08f9SMatthias Ringwald for (i=0;i<segment_len;i++){ 41516fd08f9SMatthias Ringwald network_pdu->data[1+i] = network_pdu->data[13+i]; 41616fd08f9SMatthias Ringwald } 41716fd08f9SMatthias Ringwald network_pdu->len = 1 + segment_len; 41816fd08f9SMatthias Ringwald // add this buffer 419926e8875SMatthias Ringwald btstack_linked_list_add(&message_pdu->segments, (btstack_linked_item_t *) network_pdu); 42016fd08f9SMatthias Ringwald } 421926e8875SMatthias Ringwald 42277ba3d3fSMatthias Ringwald // last segment -> store len 42377ba3d3fSMatthias Ringwald if (seg_o == seg_n){ 4248a129166SMatthias Ringwald message_pdu->len = (seg_n * max_segment_len) + segment_len; 425b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 426926e8875SMatthias Ringwald printf("Assembled payload len %u\n", message_pdu->len); 427b66a7a84SMatthias Ringwald #endif 42877ba3d3fSMatthias Ringwald } 42977ba3d3fSMatthias Ringwald 43077ba3d3fSMatthias Ringwald // check for complete 43177ba3d3fSMatthias Ringwald int i; 43277ba3d3fSMatthias Ringwald for (i=0;i<=seg_n;i++){ 433926e8875SMatthias Ringwald if ( (message_pdu->block_ack & (1<<i)) == 0) return; 434926e8875SMatthias Ringwald } 435926e8875SMatthias Ringwald 43677ba3d3fSMatthias Ringwald // store block ack in peer info 4373945bdb6SMatthias Ringwald mesh_peer_t * peer = mesh_peer_for_addr(message_pdu->src); 43877ba3d3fSMatthias Ringwald // TODO: check if NULL check can be removed 43977ba3d3fSMatthias Ringwald if (peer){ 440926e8875SMatthias Ringwald peer->block_ack = message_pdu->block_ack; 44177ba3d3fSMatthias Ringwald } 44277ba3d3fSMatthias Ringwald 44377ba3d3fSMatthias Ringwald // send ack 444*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_send_ack_for_segmented_pdu(message_pdu); 44577ba3d3fSMatthias Ringwald 44677ba3d3fSMatthias Ringwald // forward to upper transport 447*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) message_pdu); 4482a974a69SMatthias Ringwald 4492a974a69SMatthias Ringwald // mark as done 450*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_segmented_message_complete(message_pdu); 45158bb2379SMatthias Ringwald } 45258bb2379SMatthias Ringwald 45377ba3d3fSMatthias Ringwald void mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){ 454fc5e2620SMatthias Ringwald btstack_assert(pdu == mesh_lower_transport_higher_layer_pdu); 455fc5e2620SMatthias Ringwald mesh_lower_transport_higher_layer_pdu = NULL; 45684011563SMatthias Ringwald mesh_network_pdu_t * network_pdu; 45777ba3d3fSMatthias Ringwald switch (pdu->pdu_type){ 458a4bbc09dSMatthias Ringwald case MESH_PDU_TYPE_SEGMENTED: 4598a504b30SMatthias Ringwald // free segments 460*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_report_segments_as_processed((mesh_segmented_pdu_t *) pdu); 46179eb95b0SMatthias Ringwald mesh_segmented_pdu_free((mesh_segmented_pdu_t *) pdu); 46235699c48SMatthias Ringwald break; 4638facb3eaSMatthias Ringwald case MESH_PDU_TYPE_UNSEGMENTED: 464e4121a34SMatthias Ringwald network_pdu = (mesh_network_pdu_t *) pdu; 46584011563SMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 4662ae11e2cSMatthias Ringwald break; 46777ba3d3fSMatthias Ringwald default: 4683d9c3b8eSMatthias Ringwald btstack_assert(0); 46977ba3d3fSMatthias Ringwald break; 47077ba3d3fSMatthias Ringwald } 471*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_deliver_to_higher_layer(); 47277ba3d3fSMatthias Ringwald } 47377ba3d3fSMatthias Ringwald 474*27e8a9aaSMatthias Ringwald // OUTGOING // 475*27e8a9aaSMatthias Ringwald 476*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_setup_block_ack(mesh_segmented_pdu_t *message_pdu){ 477*27e8a9aaSMatthias Ringwald // setup block ack - set bit for segment to send, will be cleared on ack 478*27e8a9aaSMatthias Ringwald int ctl = message_pdu->ctl_ttl >> 7; 479*27e8a9aaSMatthias Ringwald uint16_t max_segment_len = ctl ? 8 : 12; // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC) 480*27e8a9aaSMatthias Ringwald uint8_t seg_n = (message_pdu->len - 1) / max_segment_len; 481*27e8a9aaSMatthias Ringwald if (seg_n < 31){ 482*27e8a9aaSMatthias Ringwald message_pdu->block_ack = (1 << (seg_n+1)) - 1; 48377ba3d3fSMatthias Ringwald } else { 484*27e8a9aaSMatthias Ringwald message_pdu->block_ack = 0xffffffff; 48577ba3d3fSMatthias Ringwald } 48677ba3d3fSMatthias Ringwald } 48777ba3d3fSMatthias Ringwald 488*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_process_segment_acknowledgement_message(mesh_network_pdu_t *network_pdu){ 489*27e8a9aaSMatthias Ringwald if (lower_transport_outgoing_message == NULL) return; 490*27e8a9aaSMatthias Ringwald 491*27e8a9aaSMatthias Ringwald uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 492*27e8a9aaSMatthias Ringwald uint16_t seq_zero_pdu = big_endian_read_16(lower_transport_pdu, 1) >> 2; 493*27e8a9aaSMatthias Ringwald uint16_t seq_zero_out = lower_transport_outgoing_message->seq & 0x1fff; 494*27e8a9aaSMatthias Ringwald uint32_t block_ack = big_endian_read_32(lower_transport_pdu, 3); 495*27e8a9aaSMatthias Ringwald 496*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 497*27e8a9aaSMatthias Ringwald printf("[+] Segment Acknowledgment message with seq_zero %06x, block_ack %08x - outgoing seq %06x, block_ack %08x\n", 498*27e8a9aaSMatthias Ringwald seq_zero_pdu, block_ack, seq_zero_out, lower_transport_outgoing_message->block_ack); 499*27e8a9aaSMatthias Ringwald #endif 500*27e8a9aaSMatthias Ringwald 501*27e8a9aaSMatthias Ringwald if (block_ack == 0){ 502*27e8a9aaSMatthias Ringwald // If a Segment Acknowledgment message with the BlockAck field set to 0x00000000 is received, 503*27e8a9aaSMatthias Ringwald // then the Upper Transport PDU shall be immediately cancelled and the higher layers shall be notified that 504*27e8a9aaSMatthias Ringwald // the Upper Transport PDU has been cancelled. 505*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 506*27e8a9aaSMatthias Ringwald printf("[+] Block Ack == 0 => Abort\n"); 507*27e8a9aaSMatthias Ringwald #endif 508*27e8a9aaSMatthias Ringwald if (lower_transport_outgoing_segment_queued){ 509*27e8a9aaSMatthias Ringwald lower_transport_outgoing_transmission_complete = true; 510*27e8a9aaSMatthias Ringwald } else { 511*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_complete(); 512*27e8a9aaSMatthias Ringwald } 513*27e8a9aaSMatthias Ringwald return; 514*27e8a9aaSMatthias Ringwald } 515*27e8a9aaSMatthias Ringwald if (seq_zero_pdu != seq_zero_out){ 516*27e8a9aaSMatthias Ringwald 517*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 518*27e8a9aaSMatthias Ringwald printf("[!] Seq Zero doesn't match\n"); 519*27e8a9aaSMatthias Ringwald #endif 520*27e8a9aaSMatthias Ringwald return; 521*27e8a9aaSMatthias Ringwald } 522*27e8a9aaSMatthias Ringwald 523*27e8a9aaSMatthias Ringwald lower_transport_outgoing_message->block_ack &= ~block_ack; 524*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 525*27e8a9aaSMatthias Ringwald printf("[+] Updated block_ack %08x\n", lower_transport_outgoing_message->block_ack); 526*27e8a9aaSMatthias Ringwald #endif 527*27e8a9aaSMatthias Ringwald 528*27e8a9aaSMatthias Ringwald if (lower_transport_outgoing_message->block_ack == 0){ 529*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 530*27e8a9aaSMatthias Ringwald printf("[+] Sent complete\n"); 531*27e8a9aaSMatthias Ringwald #endif 532*27e8a9aaSMatthias Ringwald 533*27e8a9aaSMatthias Ringwald if (lower_transport_outgoing_segment_queued){ 534*27e8a9aaSMatthias Ringwald lower_transport_outgoing_transmission_complete = true; 535*27e8a9aaSMatthias Ringwald } else { 536*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_complete(); 537*27e8a9aaSMatthias Ringwald } 538*27e8a9aaSMatthias Ringwald } 539*27e8a9aaSMatthias Ringwald } 540*27e8a9aaSMatthias Ringwald 541*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){ 542*27e8a9aaSMatthias Ringwald if (!segmented_pdu->acknowledgement_timer_active) return; 543*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 0; 544*27e8a9aaSMatthias Ringwald btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer); 545*27e8a9aaSMatthias Ringwald } 546*27e8a9aaSMatthias Ringwald 547*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_restart_segment_transmission_timer(mesh_segmented_pdu_t *segmented_pdu){ 548*27e8a9aaSMatthias Ringwald // restart segment transmission timer for unicast dst 549*27e8a9aaSMatthias Ringwald // - "This timer shall be set to a minimum of 200 + 50 * TTL milliseconds." 550*27e8a9aaSMatthias Ringwald uint32_t timeout = 200 + 50 * (segmented_pdu->ctl_ttl & 0x7f); 551*27e8a9aaSMatthias Ringwald if (segmented_pdu->acknowledgement_timer_active){ 552*27e8a9aaSMatthias Ringwald btstack_run_loop_remove_timer(&lower_transport_outgoing_message->acknowledgement_timer); 553*27e8a9aaSMatthias Ringwald } 554*27e8a9aaSMatthias Ringwald 555*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 556*27e8a9aaSMatthias Ringwald printf("[+] Lower transport, segmented pdu %p, seq %06x: setup transmission timeout %u ms\n", segmented_pdu, 557*27e8a9aaSMatthias Ringwald segmented_pdu->seq, (int) timeout); 558*27e8a9aaSMatthias Ringwald #endif 559*27e8a9aaSMatthias Ringwald 560*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout); 561*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_outgoing_segment_transmission_timeout); 562*27e8a9aaSMatthias Ringwald btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, lower_transport_outgoing_message); 563*27e8a9aaSMatthias Ringwald btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer); 564*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 1; 565*27e8a9aaSMatthias Ringwald } 566*27e8a9aaSMatthias Ringwald 567*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_complete(void){ 568*27e8a9aaSMatthias Ringwald btstack_assert(lower_transport_outgoing_message != NULL); 569*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 570*27e8a9aaSMatthias Ringwald printf("mesh_lower_transport_outgoing_complete %p, ack timer active %u, incomplete active %u\n", lower_transport_outgoing_message, 571*27e8a9aaSMatthias Ringwald lower_transport_outgoing_message->acknowledgement_timer_active, lower_transport_outgoing_message->incomplete_timer_active); 572*27e8a9aaSMatthias Ringwald #endif 573*27e8a9aaSMatthias Ringwald // stop timers 574*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_stop_acknowledgment_timer(lower_transport_outgoing_message); 575*27e8a9aaSMatthias Ringwald 576*27e8a9aaSMatthias Ringwald lower_transport_outgoing_message = NULL; 577*27e8a9aaSMatthias Ringwald 578*27e8a9aaSMatthias Ringwald // notify upper transport 579*27e8a9aaSMatthias Ringwald mesh_segmented_pdu_t * pdu = lower_transport_outgoing_message; 580*27e8a9aaSMatthias Ringwald lower_transport_outgoing_message = NULL; 581*27e8a9aaSMatthias Ringwald higher_layer_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SEND_ABORT_BY_REMOTE, (mesh_pdu_t *) pdu); 582*27e8a9aaSMatthias Ringwald } 583*27e8a9aaSMatthias Ringwald 584*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_setup_segment(mesh_segmented_pdu_t *message_pdu, uint8_t seg_o, mesh_network_pdu_t *network_pdu){ 58577ba3d3fSMatthias Ringwald 5863945bdb6SMatthias Ringwald int ctl = message_pdu->ctl_ttl >> 7; 58777ba3d3fSMatthias Ringwald uint16_t max_segment_len = ctl ? 8 : 12; // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC) 58877ba3d3fSMatthias Ringwald 589b16fda24SMatthias Ringwald // use seq number from transport pdu once if MESH_TRANSPORT_FLAG_SEQ_RESERVED (to allow reserving seq number in upper transport while using all seq numbers) 590b16fda24SMatthias Ringwald uint32_t seq; 591a7f44e8dSMatthias Ringwald if ((message_pdu->flags & MESH_TRANSPORT_FLAG_SEQ_RESERVED) != 0){ 592a7f44e8dSMatthias Ringwald message_pdu->flags &= ~(MESH_TRANSPORT_FLAG_SEQ_RESERVED); 5933945bdb6SMatthias Ringwald seq = message_pdu->seq; 594b16fda24SMatthias Ringwald } else { 595b16fda24SMatthias Ringwald seq = mesh_sequence_number_next(); 596b16fda24SMatthias Ringwald } 5973945bdb6SMatthias Ringwald uint16_t seq_zero = message_pdu->seq & 0x01fff; 598a7f44e8dSMatthias Ringwald uint8_t seg_n = (message_pdu->len - 1) / max_segment_len; 599a7f44e8dSMatthias Ringwald uint8_t szmic = ((!ctl) && (message_pdu->transmic_len == 8)) ? 1 : 0; // only 1 for access messages with 64 bit TransMIC 6003945bdb6SMatthias Ringwald uint8_t nid = message_pdu->ivi_nid & 0x7f; 6013945bdb6SMatthias Ringwald uint8_t ttl = message_pdu->ctl_ttl & 0x7f; 6023945bdb6SMatthias Ringwald uint16_t src = message_pdu->src; 6033945bdb6SMatthias Ringwald uint16_t dest = message_pdu->dst; 60477ba3d3fSMatthias Ringwald 60577ba3d3fSMatthias Ringwald // current segment. 60677ba3d3fSMatthias Ringwald uint16_t seg_offset = seg_o * max_segment_len; 60777ba3d3fSMatthias Ringwald 60877ba3d3fSMatthias Ringwald uint8_t lower_transport_pdu_data[16]; 609a7f44e8dSMatthias Ringwald lower_transport_pdu_data[0] = 0x80 | message_pdu->akf_aid_control; 61077ba3d3fSMatthias Ringwald big_endian_store_24(lower_transport_pdu_data, 1, (szmic << 23) | (seq_zero << 10) | (seg_o << 5) | seg_n); 611a7f44e8dSMatthias Ringwald uint16_t segment_len = btstack_min(message_pdu->len - seg_offset, max_segment_len); 612a7f44e8dSMatthias Ringwald 61377ba3d3fSMatthias Ringwald uint16_t lower_transport_pdu_len = 4 + segment_len; 61477ba3d3fSMatthias Ringwald 615a7f44e8dSMatthias Ringwald // find network-pdu with chunk for seg_offset 616a7f44e8dSMatthias Ringwald mesh_network_pdu_t * chunk = (mesh_network_pdu_t *) lower_transport_outgoing_message->segments; 617a7f44e8dSMatthias Ringwald uint16_t chunk_start = 0; 618a7f44e8dSMatthias Ringwald while ((chunk_start + MESH_NETWORK_PAYLOAD_MAX) <= seg_offset){ 619a7f44e8dSMatthias Ringwald chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next; 620a7f44e8dSMatthias Ringwald chunk_start += MESH_NETWORK_PAYLOAD_MAX; 621a7f44e8dSMatthias Ringwald } 622a7f44e8dSMatthias Ringwald // first part 623a7f44e8dSMatthias Ringwald uint16_t chunk_offset = seg_offset - chunk_start; 624a7f44e8dSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(MESH_NETWORK_PAYLOAD_MAX - chunk_offset, segment_len); 625a7f44e8dSMatthias Ringwald (void)memcpy(&lower_transport_pdu_data[4], 626a7f44e8dSMatthias Ringwald &chunk->data[chunk_offset], bytes_to_copy); 627a7f44e8dSMatthias Ringwald segment_len -= bytes_to_copy; 628a7f44e8dSMatthias Ringwald // second part 629a7f44e8dSMatthias Ringwald if (segment_len > 0){ 630a7f44e8dSMatthias Ringwald chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next; 631a7f44e8dSMatthias Ringwald (void)memcpy(&lower_transport_pdu_data[4+bytes_to_copy], 632a7f44e8dSMatthias Ringwald &chunk->data[0], segment_len); 633a7f44e8dSMatthias Ringwald } 634a7f44e8dSMatthias Ringwald 635a7f44e8dSMatthias Ringwald mesh_network_setup_pdu(network_pdu, message_pdu->netkey_index, nid, 0, ttl, seq, src, dest, lower_transport_pdu_data, lower_transport_pdu_len); 63677ba3d3fSMatthias Ringwald } 63777ba3d3fSMatthias Ringwald 638*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_send_next_segment(void){ 63979eb95b0SMatthias Ringwald btstack_assert(lower_transport_outgoing_message != NULL); 64077ba3d3fSMatthias Ringwald 6411b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 6423945bdb6SMatthias Ringwald printf("[+] Lower Transport, segmented pdu %p, seq %06x: send next segment\n", lower_transport_outgoing_message, 6433945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq); 6441b639008SMatthias Ringwald #endif 6451b639008SMatthias Ringwald 6463945bdb6SMatthias Ringwald int ctl = lower_transport_outgoing_message->ctl_ttl >> 7; 64777ba3d3fSMatthias Ringwald uint16_t max_segment_len = ctl ? 8 : 12; // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC) 648a7f44e8dSMatthias Ringwald uint8_t seg_n = (lower_transport_outgoing_message->len - 1) / max_segment_len; 64977ba3d3fSMatthias Ringwald 650764c8f0dSMatthias Ringwald // find next unacknowledged segment 651a7f44e8dSMatthias Ringwald while ((lower_transport_outgoing_seg_o <= seg_n) && ((lower_transport_outgoing_message->block_ack & (1 << lower_transport_outgoing_seg_o)) == 0)){ 65277ba3d3fSMatthias Ringwald lower_transport_outgoing_seg_o++; 65377ba3d3fSMatthias Ringwald } 65477ba3d3fSMatthias Ringwald 65577ba3d3fSMatthias Ringwald if (lower_transport_outgoing_seg_o > seg_n){ 656b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 6573945bdb6SMatthias Ringwald printf("[+] Lower Transport, segmented pdu %p, seq %06x: send complete (dst %x)\n", lower_transport_outgoing_message, 6583945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq, 6593945bdb6SMatthias Ringwald lower_transport_outgoing_message->dst); 660b66a7a84SMatthias Ringwald #endif 66177ba3d3fSMatthias Ringwald lower_transport_outgoing_seg_o = 0; 66277ba3d3fSMatthias Ringwald 66377ba3d3fSMatthias Ringwald // done for unicast, ack timer already set, too 664b4977f35SMatthias Ringwald if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)) { 665b4977f35SMatthias Ringwald // btstack_linked_list_add(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) lower_transport_outgoing_message); 666b4977f35SMatthias Ringwald // lower_transport_outgoing_message = NULL; 667b4977f35SMatthias Ringwald return; 668b4977f35SMatthias Ringwald } 66977ba3d3fSMatthias Ringwald 670b4977f35SMatthias Ringwald // done for group/virtual, no more retries? 671764c8f0dSMatthias Ringwald if (lower_transport_outgoing_message->retry_count == 0){ 672b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 67377ba3d3fSMatthias Ringwald printf("[+] Lower Transport, message unacknowledged -> free\n"); 674b66a7a84SMatthias Ringwald #endif 675b4977f35SMatthias Ringwald // notify upper transport, sets lower_transport_outgoing_message = NULL 67677ba3d3fSMatthias Ringwald mesh_lower_transport_outgoing_complete(); 67777ba3d3fSMatthias Ringwald return; 67877ba3d3fSMatthias Ringwald } 67977ba3d3fSMatthias Ringwald 680b4977f35SMatthias Ringwald // re-queue mssage; 681b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 682764c8f0dSMatthias Ringwald printf("[+] Lower Transport, message unacknowledged retry count %u\n", lower_transport_outgoing_message->retry_count); 683b66a7a84SMatthias Ringwald #endif 684764c8f0dSMatthias Ringwald lower_transport_outgoing_message->retry_count--; 685b4977f35SMatthias Ringwald // btstack_linked_list_add(&lower_transport_outgoing_ready, (btstack_linked_item_t *) lower_transport_outgoing_message); 686b4977f35SMatthias Ringwald // lower_transport_outgoing_message = NULL; 687b4977f35SMatthias Ringwald // mesh_lower_transport_run(); 688b4977f35SMatthias Ringwald // return; 68977ba3d3fSMatthias Ringwald } 69077ba3d3fSMatthias Ringwald 691eb3826d8SMatthias Ringwald // restart segment transmission timer for unicast dst 6923945bdb6SMatthias Ringwald if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)){ 693*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_restart_segment_transmission_timer(lower_transport_outgoing_message); 69477ba3d3fSMatthias Ringwald } 69577ba3d3fSMatthias Ringwald 696*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_setup_segment(lower_transport_outgoing_message, lower_transport_outgoing_seg_o, 69777ba3d3fSMatthias Ringwald lower_transport_outgoing_segment); 69877ba3d3fSMatthias Ringwald 699b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 7003945bdb6SMatthias Ringwald printf("[+] Lower Transport, segmented pdu %p, seq %06x: send seg_o %x, seg_n %x\n", lower_transport_outgoing_message, 7013945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq, lower_transport_outgoing_seg_o, seg_n); 702b0d2aa0bSMatthias Ringwald mesh_print_hex("LowerTransportPDU", &lower_transport_outgoing_segment->data[9], lower_transport_outgoing_segment->len-9); 703b66a7a84SMatthias Ringwald #endif 70477ba3d3fSMatthias Ringwald 70577ba3d3fSMatthias Ringwald // next segment 70677ba3d3fSMatthias Ringwald lower_transport_outgoing_seg_o++; 70777ba3d3fSMatthias Ringwald 70877ba3d3fSMatthias Ringwald // send network pdu 70979eb95b0SMatthias Ringwald lower_transport_outgoing_segment_queued = true; 71077ba3d3fSMatthias Ringwald mesh_network_send_pdu(lower_transport_outgoing_segment); 71177ba3d3fSMatthias Ringwald } 71277ba3d3fSMatthias Ringwald 713*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_setup_sending_segmented_pdus(void){ 7143945bdb6SMatthias Ringwald printf("[+] Lower Transport, segmented pdu %p, seq %06x: send retry count %u\n", lower_transport_outgoing_message, 715764c8f0dSMatthias Ringwald lower_transport_outgoing_message->seq, lower_transport_outgoing_message->retry_count); 716764c8f0dSMatthias Ringwald lower_transport_outgoing_message->retry_count--; 717b489fec0SMatthias Ringwald lower_transport_outgoing_seg_o = 0; 718b489fec0SMatthias Ringwald } 719b489fec0SMatthias Ringwald 720*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_fired(void){ 721b489fec0SMatthias Ringwald // once more? 722764c8f0dSMatthias Ringwald if (lower_transport_outgoing_message->retry_count == 0){ 7233945bdb6SMatthias Ringwald printf("[!] Lower transport, segmented pdu %p, seq %06x: send failed, retries exhausted\n", lower_transport_outgoing_message, 7243945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq); 725b489fec0SMatthias Ringwald mesh_lower_transport_outgoing_complete(); 726b489fec0SMatthias Ringwald return; 727b489fec0SMatthias Ringwald } 728b489fec0SMatthias Ringwald 7291b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 7303945bdb6SMatthias Ringwald printf("[+] Lower transport, segmented pdu %p, seq %06x: transmission fired\n", lower_transport_outgoing_message, 7313945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq); 7321b639008SMatthias Ringwald #endif 7331b639008SMatthias Ringwald 734b489fec0SMatthias Ringwald // send remaining segments again 735*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_setup_sending_segmented_pdus(); 7365f62b711SMatthias Ringwald // send next segment 737*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_send_next_segment(); 7381b639008SMatthias Ringwald } 739b489fec0SMatthias Ringwald 740*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts){ 741*27e8a9aaSMatthias Ringwald mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts); 742*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 743*27e8a9aaSMatthias Ringwald printf("[+] Lower transport, segmented pdu %p, seq %06x: transmission timer fired\n", segmented_pdu, 744*27e8a9aaSMatthias Ringwald segmented_pdu->seq); 745*27e8a9aaSMatthias Ringwald #endif 746*27e8a9aaSMatthias Ringwald segmented_pdu->acknowledgement_timer_active = 0; 747*27e8a9aaSMatthias Ringwald 748*27e8a9aaSMatthias Ringwald if (lower_transport_outgoing_segment_queued){ 749*27e8a9aaSMatthias Ringwald lower_transport_outgoing_transmission_timeout = true; 750*27e8a9aaSMatthias Ringwald } else { 751*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_segment_transmission_fired(); 752*27e8a9aaSMatthias Ringwald } 753*27e8a9aaSMatthias Ringwald } 754*27e8a9aaSMatthias Ringwald 755*27e8a9aaSMatthias Ringwald // GENERAL // 756*27e8a9aaSMatthias Ringwald 75777ba3d3fSMatthias Ringwald static void mesh_lower_transport_network_pdu_sent(mesh_network_pdu_t *network_pdu){ 75877ba3d3fSMatthias Ringwald // figure out what pdu was sent 75977ba3d3fSMatthias Ringwald 7601e6c9225SMatthias Ringwald // single segment? 76177ba3d3fSMatthias Ringwald if (lower_transport_outgoing_segment == network_pdu){ 762a7f44e8dSMatthias Ringwald btstack_assert(lower_transport_outgoing_message != NULL); 763b02ff6bdSMatthias Ringwald 7641e6c9225SMatthias Ringwald // of segmented message 765b02ff6bdSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 7663945bdb6SMatthias Ringwald printf("[+] Lower transport, segmented pdu %p, seq %06x: network pdu %p sent\n", lower_transport_outgoing_message, 7673945bdb6SMatthias Ringwald lower_transport_outgoing_message->seq, network_pdu); 768b02ff6bdSMatthias Ringwald #endif 769b02ff6bdSMatthias Ringwald 77079eb95b0SMatthias Ringwald lower_transport_outgoing_segment_queued = false; 771764c8f0dSMatthias Ringwald if (lower_transport_outgoing_transmission_complete){ 7721b639008SMatthias Ringwald // handle complete 77379eb95b0SMatthias Ringwald lower_transport_outgoing_transmission_complete = false; 77479eb95b0SMatthias Ringwald lower_transport_outgoing_transmission_timeout = false; 7751b639008SMatthias Ringwald mesh_lower_transport_outgoing_complete(); 7761b639008SMatthias Ringwald return; 7771b639008SMatthias Ringwald } 778b489fec0SMatthias Ringwald if (lower_transport_outgoing_transmission_timeout){ 779b489fec0SMatthias Ringwald // handle timeout 78079eb95b0SMatthias Ringwald lower_transport_outgoing_transmission_timeout = false; 781*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_segment_transmission_fired(); 7821b639008SMatthias Ringwald return; 783b489fec0SMatthias Ringwald } 784b02ff6bdSMatthias Ringwald 785b489fec0SMatthias Ringwald // send next segment 786*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_send_next_segment(); 78777ba3d3fSMatthias Ringwald return; 78877ba3d3fSMatthias Ringwald } 78977ba3d3fSMatthias Ringwald 79077ba3d3fSMatthias Ringwald // Segment Acknowledgment message sent by us? 79177ba3d3fSMatthias Ringwald if (mesh_network_control(network_pdu) && network_pdu->data[0] == 0){ 79277ba3d3fSMatthias Ringwald btstack_memory_mesh_network_pdu_free(network_pdu); 79377ba3d3fSMatthias Ringwald return; 79477ba3d3fSMatthias Ringwald } 79577ba3d3fSMatthias Ringwald 79677ba3d3fSMatthias Ringwald // other 79777ba3d3fSMatthias Ringwald higher_layer_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SUCCESS, (mesh_pdu_t *) network_pdu); 79877ba3d3fSMatthias Ringwald } 79977ba3d3fSMatthias Ringwald 800*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_unsegmented_control_message(mesh_network_pdu_t *network_pdu){ 801*27e8a9aaSMatthias Ringwald uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 802*27e8a9aaSMatthias Ringwald uint8_t opcode = lower_transport_pdu[0]; 803*27e8a9aaSMatthias Ringwald 804*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 805*27e8a9aaSMatthias Ringwald printf("Unsegmented Control message, outgoing message %p, opcode %x\n", lower_transport_outgoing_message, opcode); 806*27e8a9aaSMatthias Ringwald #endif 807*27e8a9aaSMatthias Ringwald 808*27e8a9aaSMatthias Ringwald switch (opcode){ 809*27e8a9aaSMatthias Ringwald case 0: 810*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_process_segment_acknowledgement_message(network_pdu); 811*27e8a9aaSMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 812*27e8a9aaSMatthias Ringwald break; 813*27e8a9aaSMatthias Ringwald default: 814*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu); 815*27e8a9aaSMatthias Ringwald break; 816*27e8a9aaSMatthias Ringwald } 817*27e8a9aaSMatthias Ringwald } 818*27e8a9aaSMatthias Ringwald 819*27e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_network_pdu(mesh_network_pdu_t *network_pdu) {// segmented? 820*27e8a9aaSMatthias Ringwald if (mesh_network_segmented(network_pdu)){ 821*27e8a9aaSMatthias Ringwald mesh_segmented_pdu_t * message_pdu = mesh_lower_transport_incoming_pdu_for_segmented_message(network_pdu); 822*27e8a9aaSMatthias Ringwald if (message_pdu) { 823*27e8a9aaSMatthias Ringwald // start acknowledgment timer if inactive 824*27e8a9aaSMatthias Ringwald if (message_pdu->acknowledgement_timer_active == 0){ 825*27e8a9aaSMatthias Ringwald // - "The acknowledgment timer shall be set to a minimum of 150 + 50 * TTL milliseconds" 826*27e8a9aaSMatthias Ringwald uint32_t timeout = 150 + 50 * mesh_network_ttl(network_pdu); 827*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_start_acknowledgment_timer(message_pdu, timeout); 828*27e8a9aaSMatthias Ringwald } 829*27e8a9aaSMatthias Ringwald // restart incomplete timer 830*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_restart_incomplete_timer(message_pdu, 10000, 831*27e8a9aaSMatthias Ringwald &mesh_lower_transport_incoming_incomplete_timeout); 832*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_process_segment(message_pdu, network_pdu); 833166cce4dSMatthias Ringwald } else { 834*27e8a9aaSMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 835*27e8a9aaSMatthias Ringwald } 836*27e8a9aaSMatthias Ringwald } else { 837*27e8a9aaSMatthias Ringwald // control? 838*27e8a9aaSMatthias Ringwald if (mesh_network_control(network_pdu)){ 839*27e8a9aaSMatthias Ringwald // unsegmented control message (not encrypted) 840*27e8a9aaSMatthias Ringwald mesh_lower_transport_process_unsegmented_control_message(network_pdu); 841*27e8a9aaSMatthias Ringwald } else { 842*27e8a9aaSMatthias Ringwald // unsegmented access message (encrypted) 843*27e8a9aaSMatthias Ringwald mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu); 844*27e8a9aaSMatthias Ringwald } 845*27e8a9aaSMatthias Ringwald } 846*27e8a9aaSMatthias Ringwald } 847*27e8a9aaSMatthias Ringwald 848*27e8a9aaSMatthias Ringwald void mesh_lower_transport_received_message(mesh_network_callback_type_t callback_type, mesh_network_pdu_t *network_pdu){ 849*27e8a9aaSMatthias Ringwald mesh_peer_t * peer; 850*27e8a9aaSMatthias Ringwald uint16_t src; 851*27e8a9aaSMatthias Ringwald uint16_t seq; 852*27e8a9aaSMatthias Ringwald switch (callback_type){ 853*27e8a9aaSMatthias Ringwald case MESH_NETWORK_PDU_RECEIVED: 854*27e8a9aaSMatthias Ringwald src = mesh_network_src(network_pdu); 855*27e8a9aaSMatthias Ringwald seq = mesh_network_seq(network_pdu); 856*27e8a9aaSMatthias Ringwald peer = mesh_peer_for_addr(src); 857*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 858*27e8a9aaSMatthias Ringwald printf("Transport: received message. SRC %x, SEQ %x\n", src, seq); 859*27e8a9aaSMatthias Ringwald #endif 860*27e8a9aaSMatthias Ringwald // validate seq 861*27e8a9aaSMatthias Ringwald if (peer && seq > peer->seq){ 862*27e8a9aaSMatthias Ringwald // track seq 863*27e8a9aaSMatthias Ringwald peer->seq = seq; 864*27e8a9aaSMatthias Ringwald // process 865*27e8a9aaSMatthias Ringwald mesh_lower_transport_process_network_pdu(network_pdu); 866*27e8a9aaSMatthias Ringwald mesh_lower_transport_run(); 867*27e8a9aaSMatthias Ringwald } else { 868*27e8a9aaSMatthias Ringwald // drop packet 869*27e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT 870*27e8a9aaSMatthias Ringwald printf("Transport: drop packet - src/seq auth failed\n"); 871*27e8a9aaSMatthias Ringwald #endif 872*27e8a9aaSMatthias Ringwald mesh_network_message_processed_by_higher_layer(network_pdu); 873*27e8a9aaSMatthias Ringwald } 874*27e8a9aaSMatthias Ringwald break; 875*27e8a9aaSMatthias Ringwald case MESH_NETWORK_PDU_SENT: 876*27e8a9aaSMatthias Ringwald mesh_lower_transport_network_pdu_sent(network_pdu); 877*27e8a9aaSMatthias Ringwald break; 878*27e8a9aaSMatthias Ringwald default: 879*27e8a9aaSMatthias Ringwald break; 880166cce4dSMatthias Ringwald } 881166cce4dSMatthias Ringwald } 882166cce4dSMatthias Ringwald 88377ba3d3fSMatthias Ringwald void mesh_lower_transport_send_pdu(mesh_pdu_t *pdu){ 884a01ba0fdSMatthias Ringwald mesh_network_pdu_t * network_pdu; 885b4977f35SMatthias Ringwald mesh_segmented_pdu_t * segmented_pdu; 886a01ba0fdSMatthias Ringwald switch (pdu->pdu_type){ 887e9c16304SMatthias Ringwald case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 888e9c16304SMatthias Ringwald case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 889e9c16304SMatthias Ringwald network_pdu = (mesh_network_pdu_t *) pdu; 890a7f44e8dSMatthias Ringwald btstack_assert(network_pdu->len >= 9); 891a01ba0fdSMatthias Ringwald break; 892a4bbc09dSMatthias Ringwald case MESH_PDU_TYPE_SEGMENTED: 893b4977f35SMatthias Ringwald segmented_pdu = (mesh_segmented_pdu_t *) pdu; 894b4977f35SMatthias Ringwald segmented_pdu->retry_count = 3; 895a01ba0fdSMatthias Ringwald break; 896a01ba0fdSMatthias Ringwald default: 897a01ba0fdSMatthias Ringwald btstack_assert(false); 898d38a7664SMatthias Ringwald break; 89948321319SMatthias Ringwald } 90079eb95b0SMatthias Ringwald btstack_linked_list_add_tail(&lower_transport_outgoing_ready, (btstack_linked_item_t*) pdu); 90177ba3d3fSMatthias Ringwald mesh_lower_transport_run(); 90277ba3d3fSMatthias Ringwald } 90377ba3d3fSMatthias Ringwald 90477ba3d3fSMatthias Ringwald static void mesh_lower_transport_run(void){ 90577ba3d3fSMatthias Ringwald 90677ba3d3fSMatthias Ringwald // check if outgoing segmented pdu is active 9071e6c9225SMatthias Ringwald if (lower_transport_outgoing_message) return; 90877ba3d3fSMatthias Ringwald 90979eb95b0SMatthias Ringwald while(!btstack_linked_list_empty(&lower_transport_outgoing_ready)) { 91077ba3d3fSMatthias Ringwald // get next message 91177ba3d3fSMatthias Ringwald mesh_network_pdu_t * network_pdu; 912a4bbc09dSMatthias Ringwald mesh_segmented_pdu_t * message_pdu; 91379eb95b0SMatthias Ringwald mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_ready); 91477ba3d3fSMatthias Ringwald switch (pdu->pdu_type) { 915e9c16304SMatthias Ringwald case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 916e9c16304SMatthias Ringwald case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 917e9c16304SMatthias Ringwald // lower_transport_outgoing_unsegmented_pdu = (mesh_unsegmented_pdu_t *) pdu; 918e9c16304SMatthias Ringwald // network_pdu = lower_transport_outgoing_unsegmented_pdu->segment; 919e9c16304SMatthias Ringwald lower_transport_outgoing_network_pdu = (mesh_network_pdu_t *) pdu; 920e9c16304SMatthias Ringwald mesh_network_send_pdu(lower_transport_outgoing_network_pdu); 921b837963cSMatthias Ringwald break; 922a4bbc09dSMatthias Ringwald case MESH_PDU_TYPE_SEGMENTED: 923a4bbc09dSMatthias Ringwald message_pdu = (mesh_segmented_pdu_t *) pdu; 924a7f44e8dSMatthias Ringwald // 9253945bdb6SMatthias Ringwald printf("[+] Lower transport, segmented pdu %p, seq %06x: run start sending now\n", message_pdu, 9263945bdb6SMatthias Ringwald message_pdu->seq); 92777ba3d3fSMatthias Ringwald // start sending segmented pdu 928a7f44e8dSMatthias Ringwald lower_transport_outgoing_message = message_pdu; 92979eb95b0SMatthias Ringwald lower_transport_outgoing_transmission_timeout = false; 93079eb95b0SMatthias Ringwald lower_transport_outgoing_transmission_complete = false; 931*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_setup_block_ack(message_pdu); 932*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_setup_sending_segmented_pdus(); 933*27e8a9aaSMatthias Ringwald mesh_lower_transport_outgoing_send_next_segment(); 9348fa6125aSMatthias Ringwald break; 93577ba3d3fSMatthias Ringwald default: 9368fa6125aSMatthias Ringwald btstack_assert(false); 93777ba3d3fSMatthias Ringwald break; 93877ba3d3fSMatthias Ringwald } 93977ba3d3fSMatthias Ringwald } 94077ba3d3fSMatthias Ringwald } 94177ba3d3fSMatthias Ringwald 94277ba3d3fSMatthias Ringwald static void mesh_lower_transport_dump_network_pdus(const char *name, btstack_linked_list_t *list){ 94377ba3d3fSMatthias Ringwald printf("List: %s:\n", name); 94477ba3d3fSMatthias Ringwald btstack_linked_list_iterator_t it; 94577ba3d3fSMatthias Ringwald btstack_linked_list_iterator_init(&it, list); 94677ba3d3fSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 94777ba3d3fSMatthias Ringwald mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t*) btstack_linked_list_iterator_next(&it); 94877ba3d3fSMatthias Ringwald printf("- %p: ", network_pdu); printf_hexdump(network_pdu->data, network_pdu->len); 94977ba3d3fSMatthias Ringwald } 95077ba3d3fSMatthias Ringwald } 95177ba3d3fSMatthias Ringwald static void mesh_lower_transport_reset_network_pdus(btstack_linked_list_t *list){ 95277ba3d3fSMatthias Ringwald while (!btstack_linked_list_empty(list)){ 95377ba3d3fSMatthias Ringwald mesh_network_pdu_t * pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(list); 95477ba3d3fSMatthias Ringwald btstack_memory_mesh_network_pdu_free(pdu); 95577ba3d3fSMatthias Ringwald } 95677ba3d3fSMatthias Ringwald } 95777ba3d3fSMatthias Ringwald 95833439c2eSMilanka Ringwald bool mesh_lower_transport_can_send_to_dest(uint16_t dest){ 95933439c2eSMilanka Ringwald UNUSED(dest); 96079eb95b0SMatthias Ringwald return (lower_transport_outgoing_message == NULL) && btstack_linked_list_empty(&lower_transport_outgoing_ready); 961cd16791dSMatthias Ringwald } 962cd16791dSMatthias Ringwald 963cd16791dSMatthias Ringwald void mesh_lower_transport_reserve_slot(void){ 964cd16791dSMatthias Ringwald } 965cd16791dSMatthias Ringwald 96677ba3d3fSMatthias Ringwald void mesh_lower_transport_dump(void){ 96777ba3d3fSMatthias Ringwald } 96877ba3d3fSMatthias Ringwald 96977ba3d3fSMatthias Ringwald void mesh_lower_transport_reset(void){ 970a7f44e8dSMatthias Ringwald if (lower_transport_outgoing_message){ 971a7f44e8dSMatthias Ringwald while (!btstack_linked_list_empty(&lower_transport_outgoing_message->segments)){ 972a7f44e8dSMatthias Ringwald mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_message->segments); 973a7f44e8dSMatthias Ringwald mesh_network_pdu_free(network_pdu); 974a7f44e8dSMatthias Ringwald } 975a7f44e8dSMatthias Ringwald lower_transport_outgoing_message = NULL; 976a7f44e8dSMatthias Ringwald } 9771e6cebb8SMatthias Ringwald mesh_network_pdu_free(lower_transport_outgoing_segment); 97879eb95b0SMatthias Ringwald lower_transport_outgoing_segment_queued = false; 9791e6cebb8SMatthias Ringwald lower_transport_outgoing_segment = NULL; 98077ba3d3fSMatthias Ringwald } 98177ba3d3fSMatthias Ringwald 98277ba3d3fSMatthias Ringwald void mesh_lower_transport_init(){ 98377ba3d3fSMatthias Ringwald // register with network layer 98477ba3d3fSMatthias Ringwald mesh_network_set_higher_layer_handler(&mesh_lower_transport_received_message); 98577ba3d3fSMatthias Ringwald // allocate network_pdu for segmentation 98679eb95b0SMatthias Ringwald lower_transport_outgoing_segment_queued = false; 98777ba3d3fSMatthias Ringwald lower_transport_outgoing_segment = mesh_network_pdu_get(); 98877ba3d3fSMatthias Ringwald } 98977ba3d3fSMatthias Ringwald 99077ba3d3fSMatthias Ringwald void mesh_lower_transport_set_higher_layer_handler(void (*pdu_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)){ 99177ba3d3fSMatthias Ringwald higher_layer_handler = pdu_handler; 99277ba3d3fSMatthias Ringwald } 993