xref: /btstack/src/mesh/mesh_lower_transport.c (revision c0005391c59579766b9ca84b4be1c4b3cda06a1f)
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 
5727e8a9aaSMatthias Ringwald // prototypes
5879eb95b0SMatthias Ringwald static void mesh_lower_transport_run(void);
5979eb95b0SMatthias Ringwald static void mesh_lower_transport_outgoing_complete(void);
6027e8a9aaSMatthias 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;
73e3f8b4e7SMatthias Ringwald // index of outgoing segment
7479eb95b0SMatthias Ringwald static uint16_t               lower_transport_outgoing_seg_o;
75e3f8b4e7SMatthias Ringwald // network pdu with outgoing segment
7679eb95b0SMatthias Ringwald static mesh_network_pdu_t   * lower_transport_outgoing_segment;
77e3f8b4e7SMatthias Ringwald // segment currently queued at network layer (only valid for lower_transport_outgoing_message)
78e3f8b4e7SMatthias Ringwald static bool                    lower_transport_outgoing_segment_at_network_layer;
7979eb95b0SMatthias Ringwald // transmission timeout occurred (while outgoing segment queued at network layer)
8079eb95b0SMatthias Ringwald static bool                    lower_transport_outgoing_transmission_timeout;
8179eb95b0SMatthias Ringwald // transmission completed either fully ack'ed or remote aborted (while outgoing segment queued at network layer)
8279eb95b0SMatthias Ringwald static bool                    lower_transport_outgoing_transmission_complete;
8379eb95b0SMatthias Ringwald 
8479eb95b0SMatthias Ringwald // active outgoing unsegmented message
8579eb95b0SMatthias Ringwald static mesh_network_pdu_t *   lower_transport_outgoing_network_pdu;
8679eb95b0SMatthias Ringwald 
8779eb95b0SMatthias Ringwald // deliver to higher layer
8877ba3d3fSMatthias Ringwald static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
8979eb95b0SMatthias Ringwald static mesh_pdu_t * mesh_lower_transport_higher_layer_pdu;
9079eb95b0SMatthias Ringwald static btstack_linked_list_t mesh_lower_transport_queued_for_higher_layer;
9177ba3d3fSMatthias Ringwald 
9277ba3d3fSMatthias Ringwald static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){
9377ba3d3fSMatthias Ringwald     printf("%-20s ", name);
9477ba3d3fSMatthias Ringwald     printf_hexdump(data, len);
9577ba3d3fSMatthias Ringwald }
9677ba3d3fSMatthias Ringwald 
9777ba3d3fSMatthias Ringwald // utility
9877ba3d3fSMatthias Ringwald 
9979eb95b0SMatthias Ringwald mesh_segmented_pdu_t * mesh_segmented_pdu_get(void){
100a4bbc09dSMatthias Ringwald     mesh_segmented_pdu_t * message_pdu = btstack_memory_mesh_segmented_pdu_get();
101926e8875SMatthias Ringwald     if (message_pdu){
102a4bbc09dSMatthias Ringwald         message_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED;
103926e8875SMatthias Ringwald     }
104926e8875SMatthias Ringwald     return message_pdu;
105926e8875SMatthias Ringwald }
106926e8875SMatthias Ringwald 
10779eb95b0SMatthias Ringwald void mesh_segmented_pdu_free(mesh_segmented_pdu_t * message_pdu){
108cdcfd2c1SMatthias Ringwald     while (message_pdu->segments){
109cdcfd2c1SMatthias Ringwald         mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments);
110cdcfd2c1SMatthias Ringwald         mesh_network_pdu_free(segment);
111cdcfd2c1SMatthias Ringwald     }
112a4bbc09dSMatthias Ringwald     btstack_memory_mesh_segmented_pdu_free(message_pdu);
113926e8875SMatthias Ringwald }
114926e8875SMatthias Ringwald 
11527e8a9aaSMatthias Ringwald // INCOMING //
11627e8a9aaSMatthias Ringwald 
11727e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_report_segments_as_processed(mesh_segmented_pdu_t * message_pdu) {
11835699c48SMatthias Ringwald     while (message_pdu->segments){
11935699c48SMatthias Ringwald         mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments);
12035699c48SMatthias Ringwald         mesh_network_message_processed_by_higher_layer(segment);
12135699c48SMatthias Ringwald     }
12235699c48SMatthias Ringwald }
12335699c48SMatthias Ringwald 
12427e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_deliver_to_higher_layer(void){
125fc5e2620SMatthias Ringwald     if (mesh_lower_transport_higher_layer_pdu == NULL && !btstack_linked_list_empty(&mesh_lower_transport_queued_for_higher_layer)){
1262ae11e2cSMatthias Ringwald         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&mesh_lower_transport_queued_for_higher_layer);
1272ae11e2cSMatthias Ringwald 
1282ae11e2cSMatthias Ringwald         switch (pdu->pdu_type){
1295236fe42SMatthias Ringwald             case MESH_PDU_TYPE_NETWORK:
1303d9c3b8eSMatthias Ringwald                 // unsegmented pdu
131e4121a34SMatthias Ringwald                 mesh_lower_transport_higher_layer_pdu = (mesh_pdu_t *) pdu;
132e4121a34SMatthias Ringwald                 pdu->pdu_type = MESH_PDU_TYPE_UNSEGMENTED;
1332ae11e2cSMatthias Ringwald                 break;
1345236fe42SMatthias Ringwald             case MESH_PDU_TYPE_SEGMENTED:
1352ae11e2cSMatthias Ringwald                 // segmented control or access pdu
1362ae11e2cSMatthias Ringwald                 mesh_lower_transport_higher_layer_pdu = pdu;
1372ae11e2cSMatthias Ringwald                 break;
1385236fe42SMatthias Ringwald             default:
1395236fe42SMatthias Ringwald                 btstack_assert(false);
1405236fe42SMatthias Ringwald                 break;
1412ae11e2cSMatthias Ringwald         }
142fc5e2620SMatthias Ringwald         higher_layer_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, mesh_lower_transport_higher_layer_pdu);
143fc5e2620SMatthias Ringwald     }
144fc5e2620SMatthias Ringwald }
145fc5e2620SMatthias Ringwald 
14627e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_queue_for_higher_layer(mesh_pdu_t * pdu){
147fc5e2620SMatthias Ringwald     btstack_linked_list_add_tail(&mesh_lower_transport_queued_for_higher_layer, (btstack_linked_item_t *) pdu);
14827e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_deliver_to_higher_layer();
1499a0bb1c9SMatthias Ringwald }
15068d3bb6cSMatthias Ringwald 
15127e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_setup_acknowledge_message(uint8_t * data, uint8_t obo, uint16_t seq_zero, uint32_t block_ack){
15277ba3d3fSMatthias Ringwald     // printf("ACK Upper Transport, seq_zero %x\n", seq_zero);
15377ba3d3fSMatthias Ringwald     data[0] = 0;    // SEG = 0, Opcode = 0
15477ba3d3fSMatthias Ringwald     big_endian_store_16( data, 1, (obo << 15) | (seq_zero << 2) | 0);    // OBO, SeqZero, RFU
15577ba3d3fSMatthias Ringwald     big_endian_store_32( data, 3, block_ack);
156b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
15777ba3d3fSMatthias Ringwald     mesh_print_hex("ACK Upper Transport", data, 7);
158b66a7a84SMatthias Ringwald #endif
15977ba3d3fSMatthias Ringwald }
16077ba3d3fSMatthias Ringwald 
16127e8a9aaSMatthias 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){
16277ba3d3fSMatthias Ringwald     // setup ack message
16377ba3d3fSMatthias Ringwald     uint8_t  ack_msg[7];
16427e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_setup_acknowledge_message(ack_msg, 0, seq_zero, block_ack);
16577ba3d3fSMatthias Ringwald     //
16677ba3d3fSMatthias 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."
16777ba3d3fSMatthias Ringwald     // if (ttl <= 1) return 0;
16877ba3d3fSMatthias Ringwald 
16977ba3d3fSMatthias Ringwald     // TODO: check transport_pdu_len depending on ctl
17077ba3d3fSMatthias Ringwald 
17177ba3d3fSMatthias Ringwald     // lookup network by netkey_index
17277ba3d3fSMatthias Ringwald     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
17377ba3d3fSMatthias Ringwald     if (!network_key) return;
17477ba3d3fSMatthias Ringwald 
17577ba3d3fSMatthias Ringwald     // allocate network_pdu
17677ba3d3fSMatthias Ringwald     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
17777ba3d3fSMatthias Ringwald     if (!network_pdu) return;
17877ba3d3fSMatthias Ringwald 
17977ba3d3fSMatthias Ringwald     // setup network_pdu
180*c0005391SMatthias Ringwald     network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENT_ACKNOWLEDGMENT;
181001c65e0SMatthias 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));
18277ba3d3fSMatthias Ringwald 
18377ba3d3fSMatthias Ringwald     // send network_pdu
18477ba3d3fSMatthias Ringwald     mesh_network_send_pdu(network_pdu);
18577ba3d3fSMatthias Ringwald }
18677ba3d3fSMatthias Ringwald 
18727e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_send_ack_for_segmented_pdu(mesh_segmented_pdu_t * segmented_pdu){
188bd915455SMatthias Ringwald     uint16_t seq_zero = segmented_pdu->seq & 0x1fff;
18927e8a9aaSMatthias Ringwald     uint8_t  ttl = segmented_pdu->ctl_ttl & 0x7f;
19027e8a9aaSMatthias Ringwald     uint16_t dest = segmented_pdu->src;
19127e8a9aaSMatthias Ringwald     uint16_t netkey_index = segmented_pdu->netkey_index;
192b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
19377ba3d3fSMatthias Ringwald     printf("mesh_transport_send_ack_for_transport_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n",
19427e8a9aaSMatthias Ringwald            segmented_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest);
195b66a7a84SMatthias Ringwald #endif
19627e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, segmented_pdu->block_ack);
19777ba3d3fSMatthias Ringwald }
19877ba3d3fSMatthias Ringwald 
19927e8a9aaSMatthias 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) {
20077ba3d3fSMatthias Ringwald     uint8_t ttl = mesh_network_ttl(network_pdu);
20177ba3d3fSMatthias Ringwald     uint16_t dest = mesh_network_src(network_pdu);
20277ba3d3fSMatthias Ringwald     uint16_t netkey_index = network_pdu->netkey_index;
203b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
20477ba3d3fSMatthias Ringwald     printf("mesh_transport_send_ack_for_network_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n",
205001c65e0SMatthias Ringwald            network_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest);
206b66a7a84SMatthias Ringwald #endif
20727e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, block_ack);
20877ba3d3fSMatthias Ringwald }
20977ba3d3fSMatthias Ringwald 
21027e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){
211354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0) return;
212354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
21327e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer);
214926e8875SMatthias Ringwald }
215926e8875SMatthias Ringwald 
21627e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_incomplete_timer(mesh_segmented_pdu_t *segmented_pdu){
217354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) == 0) return;
218354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER;
21927e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer);
220926e8875SMatthias Ringwald }
221926e8875SMatthias Ringwald 
22227e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_segmented_message_complete(mesh_segmented_pdu_t * segmented_pdu){
22377ba3d3fSMatthias Ringwald     // stop timers
22427e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_stop_acknowledgment_timer(segmented_pdu);
22527e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_stop_incomplete_timer(segmented_pdu);
22677ba3d3fSMatthias Ringwald     // stop reassembly
22727e8a9aaSMatthias 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 
23327e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_ack_timeout(btstack_timer_source_t *ts){
23427e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
235b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
23627e8a9aaSMatthias Ringwald     printf("ACK: acknowledgement timer fired for %p, send ACK\n", segmented_pdu);
237b66a7a84SMatthias Ringwald #endif
238354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
23927e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack_for_segmented_pdu(segmented_pdu);
24077ba3d3fSMatthias Ringwald }
24177ba3d3fSMatthias Ringwald 
24227e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_incomplete_timeout(btstack_timer_source_t *ts){
24327e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
244b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
24527e8a9aaSMatthias Ringwald     printf("mesh_lower_transport_incoming_incomplete_timeout for %p - give up\n", segmented_pdu);
246b66a7a84SMatthias Ringwald #endif
24727e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_segmented_message_complete(segmented_pdu);
2488a504b30SMatthias Ringwald     // free segments
24927e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_report_segments_as_processed(segmented_pdu);
25077ba3d3fSMatthias Ringwald     // free message
25127e8a9aaSMatthias Ringwald     btstack_memory_mesh_segmented_pdu_free(segmented_pdu);
25277ba3d3fSMatthias Ringwald }
25377ba3d3fSMatthias Ringwald 
25427e8a9aaSMatthias 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
25627e8a9aaSMatthias Ringwald     printf("ACK: start rx ack timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout);
257b66a7a84SMatthias Ringwald #endif
25827e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout);
25927e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_incoming_ack_timeout);
26027e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, segmented_pdu);
26127e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer);
262354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_ACK_TIMER;
26377ba3d3fSMatthias Ringwald }
26477ba3d3fSMatthias Ringwald 
26527e8a9aaSMatthias 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
26827e8a9aaSMatthias Ringwald     printf("RX-(re)start incomplete timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout);
269b66a7a84SMatthias Ringwald #endif
270354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) != 0){
27127e8a9aaSMatthias Ringwald         btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer);
27277ba3d3fSMatthias Ringwald     }
27327e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->incomplete_timer, timeout);
27427e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->incomplete_timer, callback);
27527e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->incomplete_timer, segmented_pdu);
27627e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->incomplete_timer);
277354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER;
27877ba3d3fSMatthias Ringwald }
27977ba3d3fSMatthias Ringwald 
28027e8a9aaSMatthias 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
297bd915455SMatthias Ringwald         uint16_t active_seq_zero = peer->message_pdu->seq & 0x1fff;
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
31727e8a9aaSMatthias 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 
341e5828668SMatthias Ringwald         // get akf_aid & transmic
342b30b4f6eSMatthias Ringwald         pdu->akf_aid_control = network_pdu->data[9] & 0x7f;
343b30b4f6eSMatthias Ringwald         if ((network_pdu->data[10] & 0x80) != 0){
344b30b4f6eSMatthias Ringwald             pdu->flags |= MESH_TRANSPORT_FLAG_TRANSMIC_64;
345b30b4f6eSMatthias Ringwald         }
346e5828668SMatthias Ringwald 
34777ba3d3fSMatthias Ringwald         // store meta data in new pdu
34877ba3d3fSMatthias Ringwald         pdu->netkey_index = network_pdu->netkey_index;
34977ba3d3fSMatthias Ringwald         pdu->block_ack = 0;
350354884c7SMatthias Ringwald         pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
35177ba3d3fSMatthias Ringwald 
35277ba3d3fSMatthias Ringwald         // update peer info
353926e8875SMatthias Ringwald         peer->message_pdu   = pdu;
35477ba3d3fSMatthias Ringwald         peer->seq_zero      = seq_zero;
35577ba3d3fSMatthias Ringwald         peer->seq_auth      = seq_auth;
35677ba3d3fSMatthias Ringwald         peer->block_ack     = 0;
35777ba3d3fSMatthias Ringwald 
358b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
3593945bdb6SMatthias Ringwald         printf("mesh_transport_pdu_for_segmented_message: setup transport pdu %p for src %x, seq %06x, seq_zero %x\n", pdu, src,
3603945bdb6SMatthias Ringwald                pdu->seq, seq_zero);
361b66a7a84SMatthias Ringwald #endif
362926e8875SMatthias Ringwald         return peer->message_pdu;
36377ba3d3fSMatthias Ringwald     }  else {
36477ba3d3fSMatthias Ringwald         // seq zero differs from current transport pdu
365b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
36677ba3d3fSMatthias Ringwald         printf("mesh_transport_pdu_for_segmented_message: drop segment for old seq %x\n", seq_zero);
367b66a7a84SMatthias Ringwald #endif
36877ba3d3fSMatthias Ringwald         return NULL;
36977ba3d3fSMatthias Ringwald     }
37077ba3d3fSMatthias Ringwald }
37177ba3d3fSMatthias Ringwald 
37227e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_process_segment(mesh_segmented_pdu_t * message_pdu, mesh_network_pdu_t * network_pdu){
37377ba3d3fSMatthias Ringwald 
37477ba3d3fSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
37577ba3d3fSMatthias Ringwald     uint8_t   lower_transport_pdu_len = mesh_network_pdu_len(network_pdu);
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
387b30b4f6eSMatthias Ringwald     uint8_t transmic_len = ((message_pdu->flags & MESH_TRANSPORT_FLAG_TRANSMIC_64) != 0) ? 64 : 32;
388b30b4f6eSMatthias Ringwald     printf("mesh_lower_transport_incoming_process_segment: seq zero %04x, seg_o %02x, seg_n %02x, transmic len: %u bit\n", seq_zero, seg_o, seg_n, transmic_len);
38977ba3d3fSMatthias Ringwald     mesh_print_hex("Segment", segment_data, segment_len);
390b66a7a84SMatthias Ringwald #endif
39177ba3d3fSMatthias Ringwald 
392926e8875SMatthias Ringwald     // drop if already stored
393926e8875SMatthias Ringwald     if ((message_pdu->block_ack & (1<<seg_o)) != 0){
394926e8875SMatthias Ringwald         mesh_network_message_processed_by_higher_layer(network_pdu);
395926e8875SMatthias Ringwald         return;
396926e8875SMatthias Ringwald     }
397926e8875SMatthias Ringwald 
39877ba3d3fSMatthias Ringwald     // mark as received
399926e8875SMatthias Ringwald     message_pdu->block_ack |= (1<<seg_o);
400926e8875SMatthias Ringwald 
40116fd08f9SMatthias Ringwald     // store segment
40216fd08f9SMatthias Ringwald     uint8_t max_segment_len = mesh_network_control(network_pdu) ? 8 : 12;
40316fd08f9SMatthias Ringwald     mesh_network_pdu_t * latest_segment = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments);
40416fd08f9SMatthias Ringwald     if ((latest_segment != NULL) && ((MESH_NETWORK_PAYLOAD_MAX - latest_segment->len) > (max_segment_len  + 1))){
40516fd08f9SMatthias Ringwald         // store in last added segment if there is enough space available
40616fd08f9SMatthias Ringwald         latest_segment->data[latest_segment->len++] = seg_o;
40716fd08f9SMatthias Ringwald         (void) memcpy(&latest_segment->data[latest_segment->len], &lower_transport_pdu[4], segment_len);
40816fd08f9SMatthias Ringwald         latest_segment->len += segment_len;
40916fd08f9SMatthias Ringwald         // free buffer
41016fd08f9SMatthias Ringwald         mesh_network_message_processed_by_higher_layer(network_pdu);
41116fd08f9SMatthias Ringwald     } else {
41216fd08f9SMatthias Ringwald         // move to beginning
41316fd08f9SMatthias Ringwald         network_pdu->data[0] = seg_o;
41416fd08f9SMatthias Ringwald         uint8_t i;
41516fd08f9SMatthias Ringwald         for (i=0;i<segment_len;i++){
41616fd08f9SMatthias Ringwald             network_pdu->data[1+i] = network_pdu->data[13+i];
41716fd08f9SMatthias Ringwald         }
41816fd08f9SMatthias Ringwald         network_pdu->len = 1 + segment_len;
41916fd08f9SMatthias Ringwald         // add this buffer
420926e8875SMatthias Ringwald         btstack_linked_list_add(&message_pdu->segments, (btstack_linked_item_t *) network_pdu);
42116fd08f9SMatthias Ringwald     }
422926e8875SMatthias Ringwald 
42377ba3d3fSMatthias Ringwald     // last segment -> store len
42477ba3d3fSMatthias Ringwald     if (seg_o == seg_n){
4258a129166SMatthias Ringwald         message_pdu->len = (seg_n * max_segment_len) + segment_len;
426b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
427926e8875SMatthias Ringwald         printf("Assembled payload len %u\n", message_pdu->len);
428b66a7a84SMatthias Ringwald #endif
42977ba3d3fSMatthias Ringwald     }
43077ba3d3fSMatthias Ringwald 
43177ba3d3fSMatthias Ringwald     // check for complete
43277ba3d3fSMatthias Ringwald     int i;
43377ba3d3fSMatthias Ringwald     for (i=0;i<=seg_n;i++){
434926e8875SMatthias Ringwald         if ( (message_pdu->block_ack & (1<<i)) == 0) return;
435926e8875SMatthias Ringwald     }
436926e8875SMatthias Ringwald 
43777ba3d3fSMatthias Ringwald     // store block ack in peer info
4383945bdb6SMatthias Ringwald     mesh_peer_t * peer = mesh_peer_for_addr(message_pdu->src);
43977ba3d3fSMatthias Ringwald     // TODO: check if NULL check can be removed
44077ba3d3fSMatthias Ringwald     if (peer){
441926e8875SMatthias Ringwald         peer->block_ack = message_pdu->block_ack;
44277ba3d3fSMatthias Ringwald     }
44377ba3d3fSMatthias Ringwald 
44477ba3d3fSMatthias Ringwald     // send ack
44527e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack_for_segmented_pdu(message_pdu);
44677ba3d3fSMatthias Ringwald 
44777ba3d3fSMatthias Ringwald     // forward to upper transport
44827e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) message_pdu);
4492a974a69SMatthias Ringwald 
4502a974a69SMatthias Ringwald     // mark as done
45127e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_segmented_message_complete(message_pdu);
45258bb2379SMatthias Ringwald }
45358bb2379SMatthias Ringwald 
45477ba3d3fSMatthias Ringwald void mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
455fc5e2620SMatthias Ringwald     btstack_assert(pdu == mesh_lower_transport_higher_layer_pdu);
456fc5e2620SMatthias Ringwald     mesh_lower_transport_higher_layer_pdu = NULL;
45784011563SMatthias Ringwald     mesh_network_pdu_t * network_pdu;
45877ba3d3fSMatthias Ringwald     switch (pdu->pdu_type){
459a4bbc09dSMatthias Ringwald         case MESH_PDU_TYPE_SEGMENTED:
4608a504b30SMatthias Ringwald             // free segments
46127e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_report_segments_as_processed((mesh_segmented_pdu_t *) pdu);
46279eb95b0SMatthias Ringwald             mesh_segmented_pdu_free((mesh_segmented_pdu_t *) pdu);
46335699c48SMatthias Ringwald             break;
4648facb3eaSMatthias Ringwald         case MESH_PDU_TYPE_UNSEGMENTED:
465e4121a34SMatthias Ringwald             network_pdu = (mesh_network_pdu_t *) pdu;
46684011563SMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
4672ae11e2cSMatthias Ringwald             break;
46877ba3d3fSMatthias Ringwald         default:
4693d9c3b8eSMatthias Ringwald             btstack_assert(0);
47077ba3d3fSMatthias Ringwald             break;
47177ba3d3fSMatthias Ringwald     }
47227e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_deliver_to_higher_layer();
47377ba3d3fSMatthias Ringwald }
47477ba3d3fSMatthias Ringwald 
47527e8a9aaSMatthias Ringwald // OUTGOING //
47627e8a9aaSMatthias Ringwald 
47727e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_setup_block_ack(mesh_segmented_pdu_t *message_pdu){
47827e8a9aaSMatthias Ringwald     // setup block ack - set bit for segment to send, will be cleared on ack
47927e8a9aaSMatthias Ringwald     int      ctl = message_pdu->ctl_ttl >> 7;
48027e8a9aaSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
48127e8a9aaSMatthias Ringwald     uint8_t  seg_n = (message_pdu->len - 1) / max_segment_len;
48227e8a9aaSMatthias Ringwald     if (seg_n < 31){
48327e8a9aaSMatthias Ringwald         message_pdu->block_ack = (1 << (seg_n+1)) - 1;
48477ba3d3fSMatthias Ringwald     } else {
48527e8a9aaSMatthias Ringwald         message_pdu->block_ack = 0xffffffff;
48677ba3d3fSMatthias Ringwald     }
48777ba3d3fSMatthias Ringwald }
48877ba3d3fSMatthias Ringwald 
489e3f8b4e7SMatthias Ringwald static mesh_segmented_pdu_t * mesh_lower_transport_outgoing_message_for_dst(uint16_t dst){
490e3f8b4e7SMatthias Ringwald     if (lower_transport_outgoing_message != NULL && lower_transport_outgoing_message->dst == dst){
491e3f8b4e7SMatthias Ringwald         return lower_transport_outgoing_message;
492e3f8b4e7SMatthias Ringwald     }
493e3f8b4e7SMatthias Ringwald     return NULL;
494e3f8b4e7SMatthias Ringwald }
495e3f8b4e7SMatthias Ringwald 
49627e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_process_segment_acknowledgement_message(mesh_network_pdu_t *network_pdu){
497e3f8b4e7SMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = mesh_lower_transport_outgoing_message_for_dst( mesh_network_src(network_pdu));
498e3f8b4e7SMatthias Ringwald     if (segmented_pdu == NULL) return;
49927e8a9aaSMatthias Ringwald 
50027e8a9aaSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
50127e8a9aaSMatthias Ringwald     uint16_t seq_zero_pdu = big_endian_read_16(lower_transport_pdu, 1) >> 2;
50227e8a9aaSMatthias Ringwald     uint16_t seq_zero_out = lower_transport_outgoing_message->seq & 0x1fff;
50327e8a9aaSMatthias Ringwald     uint32_t block_ack = big_endian_read_32(lower_transport_pdu, 3);
50427e8a9aaSMatthias Ringwald 
50527e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
50627e8a9aaSMatthias Ringwald     printf("[+] Segment Acknowledgment message with seq_zero %06x, block_ack %08x - outgoing seq %06x, block_ack %08x\n",
507e3f8b4e7SMatthias Ringwald            seq_zero_pdu, block_ack, seq_zero_out, segmented_pdu->block_ack);
50827e8a9aaSMatthias Ringwald #endif
50927e8a9aaSMatthias Ringwald 
51027e8a9aaSMatthias Ringwald     if (block_ack == 0){
51127e8a9aaSMatthias Ringwald         // If a Segment Acknowledgment message with the BlockAck field set to 0x00000000 is received,
51227e8a9aaSMatthias Ringwald         // then the Upper Transport PDU shall be immediately cancelled and the higher layers shall be notified that
51327e8a9aaSMatthias Ringwald         // the Upper Transport PDU has been cancelled.
51427e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
51527e8a9aaSMatthias Ringwald         printf("[+] Block Ack == 0 => Abort\n");
51627e8a9aaSMatthias Ringwald #endif
517e3f8b4e7SMatthias Ringwald         // current?
518e3f8b4e7SMatthias Ringwald         if ((lower_transport_outgoing_message == segmented_pdu) && lower_transport_outgoing_segment_at_network_layer){
51927e8a9aaSMatthias Ringwald             lower_transport_outgoing_transmission_complete = true;
52027e8a9aaSMatthias Ringwald         } else {
52127e8a9aaSMatthias Ringwald             mesh_lower_transport_outgoing_complete();
52227e8a9aaSMatthias Ringwald         }
52327e8a9aaSMatthias Ringwald         return;
52427e8a9aaSMatthias Ringwald     }
52527e8a9aaSMatthias Ringwald     if (seq_zero_pdu != seq_zero_out){
52627e8a9aaSMatthias Ringwald 
52727e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
52827e8a9aaSMatthias Ringwald         printf("[!] Seq Zero doesn't match\n");
52927e8a9aaSMatthias Ringwald #endif
53027e8a9aaSMatthias Ringwald         return;
53127e8a9aaSMatthias Ringwald     }
53227e8a9aaSMatthias Ringwald 
533e3f8b4e7SMatthias Ringwald     segmented_pdu->block_ack &= ~block_ack;
53427e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
535e3f8b4e7SMatthias Ringwald     printf("[+] Updated block_ack %08x\n", segmented_pdu->block_ack);
53627e8a9aaSMatthias Ringwald #endif
53727e8a9aaSMatthias Ringwald 
538e3f8b4e7SMatthias Ringwald     if (segmented_pdu->block_ack == 0){
53927e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
54027e8a9aaSMatthias Ringwald         printf("[+] Sent complete\n");
54127e8a9aaSMatthias Ringwald #endif
54227e8a9aaSMatthias Ringwald 
543e3f8b4e7SMatthias Ringwald         if ((lower_transport_outgoing_message == segmented_pdu) && lower_transport_outgoing_segment_at_network_layer){
54427e8a9aaSMatthias Ringwald             lower_transport_outgoing_transmission_complete = true;
54527e8a9aaSMatthias Ringwald         } else {
54627e8a9aaSMatthias Ringwald             mesh_lower_transport_outgoing_complete();
54727e8a9aaSMatthias Ringwald         }
54827e8a9aaSMatthias Ringwald     }
54927e8a9aaSMatthias Ringwald }
55027e8a9aaSMatthias Ringwald 
55127e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){
552354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0) return;
553354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
55427e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer);
55527e8a9aaSMatthias Ringwald }
55627e8a9aaSMatthias Ringwald 
55727e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_restart_segment_transmission_timer(mesh_segmented_pdu_t *segmented_pdu){
55827e8a9aaSMatthias Ringwald     // restart segment transmission timer for unicast dst
55927e8a9aaSMatthias Ringwald     // - "This timer shall be set to a minimum of 200 + 50 * TTL milliseconds."
56027e8a9aaSMatthias Ringwald     uint32_t timeout = 200 + 50 * (segmented_pdu->ctl_ttl & 0x7f);
561354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) != 0){
56227e8a9aaSMatthias Ringwald         btstack_run_loop_remove_timer(&lower_transport_outgoing_message->acknowledgement_timer);
56327e8a9aaSMatthias Ringwald     }
56427e8a9aaSMatthias Ringwald 
56527e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
56627e8a9aaSMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06x: setup transmission timeout %u ms\n", segmented_pdu,
56727e8a9aaSMatthias Ringwald            segmented_pdu->seq, (int) timeout);
56827e8a9aaSMatthias Ringwald #endif
56927e8a9aaSMatthias Ringwald 
57027e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout);
57127e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_outgoing_segment_transmission_timeout);
57227e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, lower_transport_outgoing_message);
57327e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer);
574354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_ACK_TIMER;
57527e8a9aaSMatthias Ringwald }
57627e8a9aaSMatthias Ringwald 
57727e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_complete(void){
57827e8a9aaSMatthias Ringwald     btstack_assert(lower_transport_outgoing_message != NULL);
57927e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
58027e8a9aaSMatthias Ringwald     printf("mesh_lower_transport_outgoing_complete %p, ack timer active %u, incomplete active %u\n", lower_transport_outgoing_message,
581354884c7SMatthias Ringwald            ((lower_transport_outgoing_message->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) != 0), ((lower_transport_outgoing_message->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) != 0));
58227e8a9aaSMatthias Ringwald #endif
58327e8a9aaSMatthias Ringwald     // stop timers
58427e8a9aaSMatthias Ringwald     mesh_lower_transport_outgoing_stop_acknowledgment_timer(lower_transport_outgoing_message);
58527e8a9aaSMatthias Ringwald 
58627e8a9aaSMatthias Ringwald     // notify upper transport
58727e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * pdu = lower_transport_outgoing_message;
58827e8a9aaSMatthias Ringwald     lower_transport_outgoing_message = NULL;
58927e8a9aaSMatthias Ringwald     higher_layer_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SEND_ABORT_BY_REMOTE, (mesh_pdu_t *) pdu);
59027e8a9aaSMatthias Ringwald }
59127e8a9aaSMatthias Ringwald 
59227e8a9aaSMatthias 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){
59377ba3d3fSMatthias Ringwald 
5943945bdb6SMatthias Ringwald     int ctl = message_pdu->ctl_ttl >> 7;
59577ba3d3fSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
59677ba3d3fSMatthias Ringwald 
597b16fda24SMatthias 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)
598b16fda24SMatthias Ringwald     uint32_t seq;
599a7f44e8dSMatthias Ringwald     if ((message_pdu->flags & MESH_TRANSPORT_FLAG_SEQ_RESERVED) != 0){
600a7f44e8dSMatthias Ringwald         message_pdu->flags &= ~(MESH_TRANSPORT_FLAG_SEQ_RESERVED);
6013945bdb6SMatthias Ringwald         seq = message_pdu->seq;
602b16fda24SMatthias Ringwald     } else {
603b16fda24SMatthias Ringwald         seq = mesh_sequence_number_next();
604b16fda24SMatthias Ringwald     }
6053945bdb6SMatthias Ringwald     uint16_t seq_zero = message_pdu->seq & 0x01fff;
606a7f44e8dSMatthias Ringwald     uint8_t  seg_n    = (message_pdu->len - 1) / max_segment_len;
607b30b4f6eSMatthias Ringwald     uint8_t  szmic    = ((message_pdu->flags & MESH_TRANSPORT_FLAG_TRANSMIC_64) != 0) ? 1 : 0;
6083945bdb6SMatthias Ringwald     uint8_t  nid      = message_pdu->ivi_nid & 0x7f;
6093945bdb6SMatthias Ringwald     uint8_t  ttl      = message_pdu->ctl_ttl & 0x7f;
6103945bdb6SMatthias Ringwald     uint16_t src      = message_pdu->src;
6113945bdb6SMatthias Ringwald     uint16_t dest     = message_pdu->dst;
61277ba3d3fSMatthias Ringwald 
613b30b4f6eSMatthias Ringwald     // only 1 for access messages with 64 bit TransMIC
614b30b4f6eSMatthias Ringwald     btstack_assert((szmic == 0) || !ctl);
615b30b4f6eSMatthias Ringwald 
61677ba3d3fSMatthias Ringwald     // current segment.
61777ba3d3fSMatthias Ringwald     uint16_t seg_offset = seg_o * max_segment_len;
61877ba3d3fSMatthias Ringwald 
61977ba3d3fSMatthias Ringwald     uint8_t lower_transport_pdu_data[16];
620a7f44e8dSMatthias Ringwald     lower_transport_pdu_data[0] = 0x80 | message_pdu->akf_aid_control;
62177ba3d3fSMatthias Ringwald     big_endian_store_24(lower_transport_pdu_data, 1, (szmic << 23) | (seq_zero << 10) | (seg_o << 5) | seg_n);
622a7f44e8dSMatthias Ringwald     uint16_t segment_len = btstack_min(message_pdu->len - seg_offset, max_segment_len);
623a7f44e8dSMatthias Ringwald 
62477ba3d3fSMatthias Ringwald     uint16_t lower_transport_pdu_len = 4 + segment_len;
62577ba3d3fSMatthias Ringwald 
626a7f44e8dSMatthias Ringwald     // find network-pdu with chunk for seg_offset
627a7f44e8dSMatthias Ringwald     mesh_network_pdu_t * chunk = (mesh_network_pdu_t *) lower_transport_outgoing_message->segments;
628a7f44e8dSMatthias Ringwald     uint16_t chunk_start = 0;
629a7f44e8dSMatthias Ringwald     while ((chunk_start + MESH_NETWORK_PAYLOAD_MAX) <= seg_offset){
630a7f44e8dSMatthias Ringwald         chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next;
631a7f44e8dSMatthias Ringwald         chunk_start += MESH_NETWORK_PAYLOAD_MAX;
632a7f44e8dSMatthias Ringwald     }
633a7f44e8dSMatthias Ringwald     // first part
634a7f44e8dSMatthias Ringwald     uint16_t chunk_offset = seg_offset - chunk_start;
635a7f44e8dSMatthias Ringwald     uint16_t bytes_to_copy = btstack_min(MESH_NETWORK_PAYLOAD_MAX - chunk_offset, segment_len);
636a7f44e8dSMatthias Ringwald     (void)memcpy(&lower_transport_pdu_data[4],
637a7f44e8dSMatthias Ringwald                  &chunk->data[chunk_offset], bytes_to_copy);
638a7f44e8dSMatthias Ringwald     segment_len -= bytes_to_copy;
639a7f44e8dSMatthias Ringwald     // second part
640a7f44e8dSMatthias Ringwald     if (segment_len > 0){
641a7f44e8dSMatthias Ringwald         chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next;
642a7f44e8dSMatthias Ringwald         (void)memcpy(&lower_transport_pdu_data[4+bytes_to_copy],
643a7f44e8dSMatthias Ringwald                      &chunk->data[0], segment_len);
644a7f44e8dSMatthias Ringwald     }
645a7f44e8dSMatthias Ringwald 
646a7f44e8dSMatthias 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);
64777ba3d3fSMatthias Ringwald }
64877ba3d3fSMatthias Ringwald 
64927e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_send_next_segment(void){
65079eb95b0SMatthias Ringwald     btstack_assert(lower_transport_outgoing_message != NULL);
65177ba3d3fSMatthias Ringwald 
6521b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
6533945bdb6SMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06x: send next segment\n", lower_transport_outgoing_message,
6543945bdb6SMatthias Ringwald            lower_transport_outgoing_message->seq);
6551b639008SMatthias Ringwald #endif
6561b639008SMatthias Ringwald 
6573945bdb6SMatthias Ringwald     int ctl = lower_transport_outgoing_message->ctl_ttl >> 7;
65877ba3d3fSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
659a7f44e8dSMatthias Ringwald     uint8_t  seg_n = (lower_transport_outgoing_message->len - 1) / max_segment_len;
66077ba3d3fSMatthias Ringwald 
661764c8f0dSMatthias Ringwald     // find next unacknowledged segment
662a7f44e8dSMatthias Ringwald     while ((lower_transport_outgoing_seg_o <= seg_n) && ((lower_transport_outgoing_message->block_ack & (1 << lower_transport_outgoing_seg_o)) == 0)){
66377ba3d3fSMatthias Ringwald         lower_transport_outgoing_seg_o++;
66477ba3d3fSMatthias Ringwald     }
66577ba3d3fSMatthias Ringwald 
66677ba3d3fSMatthias Ringwald     if (lower_transport_outgoing_seg_o > seg_n){
667b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
6683945bdb6SMatthias Ringwald         printf("[+] Lower Transport, segmented pdu %p, seq %06x: send complete (dst %x)\n", lower_transport_outgoing_message,
6693945bdb6SMatthias Ringwald                lower_transport_outgoing_message->seq,
6703945bdb6SMatthias Ringwald                lower_transport_outgoing_message->dst);
671b66a7a84SMatthias Ringwald #endif
67277ba3d3fSMatthias Ringwald         lower_transport_outgoing_seg_o   = 0;
67377ba3d3fSMatthias Ringwald 
67477ba3d3fSMatthias Ringwald         // done for unicast, ack timer already set, too
675b4977f35SMatthias Ringwald         if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)) {
676b4977f35SMatthias Ringwald //             btstack_linked_list_add(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) lower_transport_outgoing_message);
677b4977f35SMatthias Ringwald //             lower_transport_outgoing_message = NULL;
678b4977f35SMatthias Ringwald             return;
679b4977f35SMatthias Ringwald         }
68077ba3d3fSMatthias Ringwald 
681b4977f35SMatthias Ringwald         // done for group/virtual, no more retries?
682764c8f0dSMatthias Ringwald         if (lower_transport_outgoing_message->retry_count == 0){
683b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
68477ba3d3fSMatthias Ringwald             printf("[+] Lower Transport, message unacknowledged -> free\n");
685b66a7a84SMatthias Ringwald #endif
686b4977f35SMatthias Ringwald             // notify upper transport, sets lower_transport_outgoing_message = NULL
68777ba3d3fSMatthias Ringwald             mesh_lower_transport_outgoing_complete();
68877ba3d3fSMatthias Ringwald             return;
68977ba3d3fSMatthias Ringwald         }
69077ba3d3fSMatthias Ringwald 
691f874399cSMatthias Ringwald         // re-queue mssage
692b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
693764c8f0dSMatthias Ringwald         printf("[+] Lower Transport, message unacknowledged retry count %u\n", lower_transport_outgoing_message->retry_count);
694b66a7a84SMatthias Ringwald #endif
695764c8f0dSMatthias Ringwald         lower_transport_outgoing_message->retry_count--;
696f874399cSMatthias Ringwald         btstack_linked_list_add(&lower_transport_outgoing_ready, (btstack_linked_item_t *) lower_transport_outgoing_message);
697f874399cSMatthias Ringwald         lower_transport_outgoing_message = NULL;
698f874399cSMatthias Ringwald         mesh_lower_transport_run();
699f874399cSMatthias Ringwald         return;
70077ba3d3fSMatthias Ringwald     }
70177ba3d3fSMatthias Ringwald 
702eb3826d8SMatthias Ringwald     // restart segment transmission timer for unicast dst
7033945bdb6SMatthias Ringwald     if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)){
70427e8a9aaSMatthias Ringwald         mesh_lower_transport_outgoing_restart_segment_transmission_timer(lower_transport_outgoing_message);
70577ba3d3fSMatthias Ringwald     }
70677ba3d3fSMatthias Ringwald 
70727e8a9aaSMatthias Ringwald     mesh_lower_transport_outgoing_setup_segment(lower_transport_outgoing_message, lower_transport_outgoing_seg_o,
70877ba3d3fSMatthias Ringwald                                                 lower_transport_outgoing_segment);
70977ba3d3fSMatthias Ringwald 
710b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
7113945bdb6SMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06x: send seg_o %x, seg_n %x\n", lower_transport_outgoing_message,
7123945bdb6SMatthias Ringwald            lower_transport_outgoing_message->seq, lower_transport_outgoing_seg_o, seg_n);
713b0d2aa0bSMatthias Ringwald     mesh_print_hex("LowerTransportPDU", &lower_transport_outgoing_segment->data[9], lower_transport_outgoing_segment->len-9);
714b66a7a84SMatthias Ringwald #endif
71577ba3d3fSMatthias Ringwald 
71677ba3d3fSMatthias Ringwald     // next segment
71777ba3d3fSMatthias Ringwald     lower_transport_outgoing_seg_o++;
71877ba3d3fSMatthias Ringwald 
71977ba3d3fSMatthias Ringwald     // send network pdu
720e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = true;
72177ba3d3fSMatthias Ringwald     mesh_network_send_pdu(lower_transport_outgoing_segment);
72277ba3d3fSMatthias Ringwald }
72377ba3d3fSMatthias Ringwald 
7246d212510SMatthias Ringwald static void mesh_lower_transport_outgoing_setup_sending_segmented_pdus(mesh_segmented_pdu_t *segmented_pdu) {
725a4331339SMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06x: send retry count %u\n", segmented_pdu, segmented_pdu->seq, segmented_pdu->retry_count);
726a4331339SMatthias Ringwald 
7276d212510SMatthias Ringwald     segmented_pdu->retry_count--;
728b489fec0SMatthias Ringwald     lower_transport_outgoing_seg_o   = 0;
729a4331339SMatthias Ringwald     lower_transport_outgoing_transmission_timeout  = false;
730a4331339SMatthias Ringwald     lower_transport_outgoing_transmission_complete = false;
731a4331339SMatthias Ringwald 
732a4331339SMatthias Ringwald     lower_transport_outgoing_message = segmented_pdu;
733b489fec0SMatthias Ringwald }
734b489fec0SMatthias Ringwald 
7356d212510SMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_fired(mesh_segmented_pdu_t *segmented_pdu) {
736b489fec0SMatthias Ringwald     // once more?
7376d212510SMatthias Ringwald     if (segmented_pdu->retry_count == 0){
7383945bdb6SMatthias Ringwald         printf("[!] Lower transport, segmented pdu %p, seq %06x: send failed, retries exhausted\n", lower_transport_outgoing_message,
7393945bdb6SMatthias Ringwald                lower_transport_outgoing_message->seq);
740b489fec0SMatthias Ringwald         mesh_lower_transport_outgoing_complete();
741b489fec0SMatthias Ringwald         return;
742b489fec0SMatthias Ringwald     }
743b489fec0SMatthias Ringwald 
7441b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
745a4331339SMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06x: transmission fired\n", segmented_pdu, segmented_pdu->seq);
7461b639008SMatthias Ringwald #endif
7471b639008SMatthias Ringwald 
748a4331339SMatthias Ringwald     // re-queue message for sending remaining segments
749a4331339SMatthias Ringwald     btstack_linked_list_remove(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) segmented_pdu);
750a4331339SMatthias Ringwald     btstack_linked_list_add_tail(&lower_transport_outgoing_ready, (btstack_linked_item_t *) segmented_pdu);
7511b639008SMatthias Ringwald }
752b489fec0SMatthias Ringwald 
75327e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts){
75427e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
75527e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
75627e8a9aaSMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06x: transmission timer fired\n", segmented_pdu,
75727e8a9aaSMatthias Ringwald            segmented_pdu->seq);
75827e8a9aaSMatthias Ringwald #endif
759354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
76027e8a9aaSMatthias Ringwald 
761e3f8b4e7SMatthias Ringwald     if (lower_transport_outgoing_segment_at_network_layer){
76227e8a9aaSMatthias Ringwald         lower_transport_outgoing_transmission_timeout = true;
76327e8a9aaSMatthias Ringwald     } else {
7646d212510SMatthias Ringwald         mesh_lower_transport_outgoing_segment_transmission_fired(segmented_pdu);
76527e8a9aaSMatthias Ringwald     }
76627e8a9aaSMatthias Ringwald }
76727e8a9aaSMatthias Ringwald 
76827e8a9aaSMatthias Ringwald // GENERAL //
76927e8a9aaSMatthias Ringwald 
77077ba3d3fSMatthias Ringwald static void mesh_lower_transport_network_pdu_sent(mesh_network_pdu_t *network_pdu){
77177ba3d3fSMatthias Ringwald     // figure out what pdu was sent
77277ba3d3fSMatthias Ringwald 
773*c0005391SMatthias Ringwald     // Segment Acknowledgment message sent by us?
774*c0005391SMatthias Ringwald     if (network_pdu->pdu_header.pdu_type == MESH_PDU_TYPE_SEGMENT_ACKNOWLEDGMENT){
775*c0005391SMatthias Ringwald         btstack_memory_mesh_network_pdu_free(network_pdu);
776*c0005391SMatthias Ringwald         return;
777*c0005391SMatthias Ringwald     }
778*c0005391SMatthias Ringwald 
7791e6c9225SMatthias Ringwald     // single segment?
78077ba3d3fSMatthias Ringwald     if (lower_transport_outgoing_segment == network_pdu){
781a7f44e8dSMatthias Ringwald         btstack_assert(lower_transport_outgoing_message != NULL);
782b02ff6bdSMatthias Ringwald 
7831e6c9225SMatthias Ringwald         // of segmented message
784b02ff6bdSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
7853945bdb6SMatthias Ringwald         printf("[+] Lower transport, segmented pdu %p, seq %06x: network pdu %p sent\n", lower_transport_outgoing_message,
7863945bdb6SMatthias Ringwald                lower_transport_outgoing_message->seq, network_pdu);
787b02ff6bdSMatthias Ringwald #endif
788b02ff6bdSMatthias Ringwald 
789e3f8b4e7SMatthias Ringwald         lower_transport_outgoing_segment_at_network_layer = false;
790764c8f0dSMatthias Ringwald         if (lower_transport_outgoing_transmission_complete){
7911b639008SMatthias Ringwald             // handle complete
79279eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_complete = false;
79379eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_timeout  = false;
7941b639008SMatthias Ringwald             mesh_lower_transport_outgoing_complete();
7951b639008SMatthias Ringwald             return;
7961b639008SMatthias Ringwald         }
797b489fec0SMatthias Ringwald         if (lower_transport_outgoing_transmission_timeout){
798b489fec0SMatthias Ringwald             // handle timeout
79979eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_timeout = false;
8006d212510SMatthias Ringwald             mesh_lower_transport_outgoing_segment_transmission_fired(lower_transport_outgoing_message);
8011b639008SMatthias Ringwald             return;
802b489fec0SMatthias Ringwald         }
803b02ff6bdSMatthias Ringwald 
804b489fec0SMatthias Ringwald         // send next segment
80527e8a9aaSMatthias Ringwald         mesh_lower_transport_outgoing_send_next_segment();
80677ba3d3fSMatthias Ringwald         return;
80777ba3d3fSMatthias Ringwald     }
80877ba3d3fSMatthias Ringwald 
80977ba3d3fSMatthias Ringwald     // other
81077ba3d3fSMatthias Ringwald     higher_layer_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SUCCESS, (mesh_pdu_t *) network_pdu);
81177ba3d3fSMatthias Ringwald }
81277ba3d3fSMatthias Ringwald 
81327e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_unsegmented_control_message(mesh_network_pdu_t *network_pdu){
81427e8a9aaSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
81527e8a9aaSMatthias Ringwald     uint8_t  opcode = lower_transport_pdu[0];
81627e8a9aaSMatthias Ringwald 
81727e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
81827e8a9aaSMatthias Ringwald     printf("Unsegmented Control message, outgoing message %p, opcode %x\n", lower_transport_outgoing_message, opcode);
81927e8a9aaSMatthias Ringwald #endif
82027e8a9aaSMatthias Ringwald 
82127e8a9aaSMatthias Ringwald     switch (opcode){
82227e8a9aaSMatthias Ringwald         case 0:
82327e8a9aaSMatthias Ringwald             mesh_lower_transport_outgoing_process_segment_acknowledgement_message(network_pdu);
82427e8a9aaSMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
82527e8a9aaSMatthias Ringwald             break;
82627e8a9aaSMatthias Ringwald         default:
82727e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu);
82827e8a9aaSMatthias Ringwald             break;
82927e8a9aaSMatthias Ringwald     }
83027e8a9aaSMatthias Ringwald }
83127e8a9aaSMatthias Ringwald 
83227e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_network_pdu(mesh_network_pdu_t *network_pdu) {// segmented?
83327e8a9aaSMatthias Ringwald     if (mesh_network_segmented(network_pdu)){
83427e8a9aaSMatthias Ringwald         mesh_segmented_pdu_t * message_pdu = mesh_lower_transport_incoming_pdu_for_segmented_message(network_pdu);
83527e8a9aaSMatthias Ringwald         if (message_pdu) {
83627e8a9aaSMatthias Ringwald             // start acknowledgment timer if inactive
837354884c7SMatthias Ringwald             if ((message_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0){
83827e8a9aaSMatthias Ringwald                 // - "The acknowledgment timer shall be set to a minimum of 150 + 50 * TTL milliseconds"
83927e8a9aaSMatthias Ringwald                 uint32_t timeout = 150 + 50 * mesh_network_ttl(network_pdu);
84027e8a9aaSMatthias Ringwald                 mesh_lower_transport_incoming_start_acknowledgment_timer(message_pdu, timeout);
84127e8a9aaSMatthias Ringwald             }
84227e8a9aaSMatthias Ringwald             // restart incomplete timer
84327e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_restart_incomplete_timer(message_pdu, 10000,
84427e8a9aaSMatthias Ringwald                                                                    &mesh_lower_transport_incoming_incomplete_timeout);
84527e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_process_segment(message_pdu, network_pdu);
846166cce4dSMatthias Ringwald         } else {
84727e8a9aaSMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
84827e8a9aaSMatthias Ringwald         }
84927e8a9aaSMatthias Ringwald     } else {
85027e8a9aaSMatthias Ringwald         // control?
85127e8a9aaSMatthias Ringwald         if (mesh_network_control(network_pdu)){
85227e8a9aaSMatthias Ringwald             // unsegmented control message (not encrypted)
85327e8a9aaSMatthias Ringwald             mesh_lower_transport_process_unsegmented_control_message(network_pdu);
85427e8a9aaSMatthias Ringwald         } else {
85527e8a9aaSMatthias Ringwald             // unsegmented access message (encrypted)
85627e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu);
85727e8a9aaSMatthias Ringwald         }
85827e8a9aaSMatthias Ringwald     }
85927e8a9aaSMatthias Ringwald }
86027e8a9aaSMatthias Ringwald 
86127e8a9aaSMatthias Ringwald void mesh_lower_transport_received_message(mesh_network_callback_type_t callback_type, mesh_network_pdu_t *network_pdu){
86227e8a9aaSMatthias Ringwald     mesh_peer_t * peer;
86327e8a9aaSMatthias Ringwald     uint16_t src;
86427e8a9aaSMatthias Ringwald     uint16_t seq;
86527e8a9aaSMatthias Ringwald     switch (callback_type){
86627e8a9aaSMatthias Ringwald         case MESH_NETWORK_PDU_RECEIVED:
86727e8a9aaSMatthias Ringwald             src = mesh_network_src(network_pdu);
86827e8a9aaSMatthias Ringwald             seq = mesh_network_seq(network_pdu);
86927e8a9aaSMatthias Ringwald             peer = mesh_peer_for_addr(src);
87027e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
87127e8a9aaSMatthias Ringwald             printf("Transport: received message. SRC %x, SEQ %x\n", src, seq);
87227e8a9aaSMatthias Ringwald #endif
87327e8a9aaSMatthias Ringwald             // validate seq
87427e8a9aaSMatthias Ringwald             if (peer && seq > peer->seq){
87527e8a9aaSMatthias Ringwald                 // track seq
87627e8a9aaSMatthias Ringwald                 peer->seq = seq;
87727e8a9aaSMatthias Ringwald                 // process
87827e8a9aaSMatthias Ringwald                 mesh_lower_transport_process_network_pdu(network_pdu);
87927e8a9aaSMatthias Ringwald                 mesh_lower_transport_run();
88027e8a9aaSMatthias Ringwald             } else {
88127e8a9aaSMatthias Ringwald                 // drop packet
88227e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
88327e8a9aaSMatthias Ringwald                 printf("Transport: drop packet - src/seq auth failed\n");
88427e8a9aaSMatthias Ringwald #endif
88527e8a9aaSMatthias Ringwald                 mesh_network_message_processed_by_higher_layer(network_pdu);
88627e8a9aaSMatthias Ringwald             }
88727e8a9aaSMatthias Ringwald             break;
88827e8a9aaSMatthias Ringwald         case MESH_NETWORK_PDU_SENT:
88927e8a9aaSMatthias Ringwald             mesh_lower_transport_network_pdu_sent(network_pdu);
89027e8a9aaSMatthias Ringwald             break;
89127e8a9aaSMatthias Ringwald         default:
89227e8a9aaSMatthias Ringwald             break;
893166cce4dSMatthias Ringwald     }
894166cce4dSMatthias Ringwald }
895166cce4dSMatthias Ringwald 
89677ba3d3fSMatthias Ringwald static void mesh_lower_transport_run(void){
89777ba3d3fSMatthias Ringwald 
89877ba3d3fSMatthias Ringwald     // check if outgoing segmented pdu is active
8991e6c9225SMatthias Ringwald     if (lower_transport_outgoing_message) return;
90077ba3d3fSMatthias Ringwald 
90179eb95b0SMatthias Ringwald     while(!btstack_linked_list_empty(&lower_transport_outgoing_ready)) {
90277ba3d3fSMatthias Ringwald         // get next message
90377ba3d3fSMatthias Ringwald         mesh_network_pdu_t   * network_pdu;
904a4bbc09dSMatthias Ringwald         mesh_segmented_pdu_t   * message_pdu;
90579eb95b0SMatthias Ringwald         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_ready);
90677ba3d3fSMatthias Ringwald         switch (pdu->pdu_type) {
907e9c16304SMatthias Ringwald             case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS:
908e9c16304SMatthias Ringwald             case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL:
909e9c16304SMatthias Ringwald                 // lower_transport_outgoing_unsegmented_pdu = (mesh_unsegmented_pdu_t *) pdu;
910e9c16304SMatthias Ringwald                 // network_pdu = lower_transport_outgoing_unsegmented_pdu->segment;
911e9c16304SMatthias Ringwald                 lower_transport_outgoing_network_pdu = (mesh_network_pdu_t *) pdu;
912e9c16304SMatthias Ringwald                 mesh_network_send_pdu(lower_transport_outgoing_network_pdu);
913b837963cSMatthias Ringwald                 break;
914a4bbc09dSMatthias Ringwald             case MESH_PDU_TYPE_SEGMENTED:
915a4bbc09dSMatthias Ringwald                 message_pdu = (mesh_segmented_pdu_t *) pdu;
916a7f44e8dSMatthias Ringwald                 //
9173945bdb6SMatthias Ringwald                 printf("[+] Lower transport, segmented pdu %p, seq %06x: run start sending now\n", message_pdu,
9183945bdb6SMatthias Ringwald                        message_pdu->seq);
91977ba3d3fSMatthias Ringwald                 // start sending segmented pdu
9206d212510SMatthias Ringwald                 mesh_lower_transport_outgoing_setup_sending_segmented_pdus(message_pdu);
92127e8a9aaSMatthias Ringwald                 mesh_lower_transport_outgoing_send_next_segment();
9228fa6125aSMatthias Ringwald                 break;
92377ba3d3fSMatthias Ringwald             default:
9248fa6125aSMatthias Ringwald                 btstack_assert(false);
92577ba3d3fSMatthias Ringwald                 break;
92677ba3d3fSMatthias Ringwald         }
92777ba3d3fSMatthias Ringwald     }
92877ba3d3fSMatthias Ringwald }
92977ba3d3fSMatthias Ringwald 
930a4331339SMatthias Ringwald void mesh_lower_transport_send_pdu(mesh_pdu_t *pdu){
931a4331339SMatthias Ringwald     mesh_network_pdu_t * network_pdu;
932a4331339SMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu;
933a4331339SMatthias Ringwald     switch (pdu->pdu_type){
934a4331339SMatthias Ringwald         case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS:
935a4331339SMatthias Ringwald         case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL:
936a4331339SMatthias Ringwald             network_pdu = (mesh_network_pdu_t *) pdu;
937a4331339SMatthias Ringwald             btstack_assert(network_pdu->len >= 9);
938a4331339SMatthias Ringwald             break;
939a4331339SMatthias Ringwald         case MESH_PDU_TYPE_SEGMENTED:
940a4331339SMatthias Ringwald             // set num retries, set of segments to send
941a4331339SMatthias Ringwald             segmented_pdu = (mesh_segmented_pdu_t *) pdu;
942a4331339SMatthias Ringwald             segmented_pdu->retry_count = 3;
943a4331339SMatthias Ringwald             mesh_lower_transport_outgoing_setup_block_ack(segmented_pdu);
944a4331339SMatthias Ringwald             break;
945a4331339SMatthias Ringwald         default:
946a4331339SMatthias Ringwald             btstack_assert(false);
947a4331339SMatthias Ringwald             break;
948a4331339SMatthias Ringwald     }
949a4331339SMatthias Ringwald     btstack_linked_list_add_tail(&lower_transport_outgoing_ready, (btstack_linked_item_t*) pdu);
950a4331339SMatthias Ringwald     mesh_lower_transport_run();
951a4331339SMatthias Ringwald }
952a4331339SMatthias Ringwald 
95377ba3d3fSMatthias Ringwald static void mesh_lower_transport_dump_network_pdus(const char *name, btstack_linked_list_t *list){
95477ba3d3fSMatthias Ringwald     printf("List: %s:\n", name);
95577ba3d3fSMatthias Ringwald     btstack_linked_list_iterator_t it;
95677ba3d3fSMatthias Ringwald     btstack_linked_list_iterator_init(&it, list);
95777ba3d3fSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
95877ba3d3fSMatthias Ringwald         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t*) btstack_linked_list_iterator_next(&it);
95977ba3d3fSMatthias Ringwald         printf("- %p: ", network_pdu); printf_hexdump(network_pdu->data, network_pdu->len);
96077ba3d3fSMatthias Ringwald     }
96177ba3d3fSMatthias Ringwald }
96277ba3d3fSMatthias Ringwald static void mesh_lower_transport_reset_network_pdus(btstack_linked_list_t *list){
96377ba3d3fSMatthias Ringwald     while (!btstack_linked_list_empty(list)){
96477ba3d3fSMatthias Ringwald         mesh_network_pdu_t * pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(list);
96577ba3d3fSMatthias Ringwald         btstack_memory_mesh_network_pdu_free(pdu);
96677ba3d3fSMatthias Ringwald     }
96777ba3d3fSMatthias Ringwald }
96877ba3d3fSMatthias Ringwald 
96933439c2eSMilanka Ringwald bool mesh_lower_transport_can_send_to_dest(uint16_t dest){
97033439c2eSMilanka Ringwald     UNUSED(dest);
971a4331339SMatthias Ringwald     // check current
972a4331339SMatthias Ringwald     uint16_t num_messages = 0;
973a4331339SMatthias Ringwald     if (lower_transport_outgoing_message != NULL) {
974a4331339SMatthias Ringwald         if (lower_transport_outgoing_message->dst == dest) {
975a4331339SMatthias Ringwald             return false;
976a4331339SMatthias Ringwald         }
977a4331339SMatthias Ringwald         num_messages++;
978a4331339SMatthias Ringwald     }
979a4331339SMatthias Ringwald     // check waiting
980a4331339SMatthias Ringwald     btstack_linked_list_iterator_t it;
981a4331339SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &lower_transport_outgoing_waiting);
982a4331339SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
983a4331339SMatthias Ringwald         mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_linked_list_iterator_next(&it);
984a4331339SMatthias Ringwald         num_messages++;
985cd15e003SMatthias Ringwald         if (segmented_pdu->dst == dest){
986cd15e003SMatthias Ringwald             return false;
987a4331339SMatthias Ringwald         }
988cd15e003SMatthias Ringwald     }
989cd15e003SMatthias Ringwald #ifdef MAX_NR_MESH_OUTGOING_SEGMENTED_MESSAGES
990cd15e003SMatthias Ringwald     // limit number of parallel outgoing messages if configured
991cd15e003SMatthias Ringwald     if (num_messages >= MAX_NR_MESH_OUTGOING_SEGMENTED_MESSAGES) return false;
992cd15e003SMatthias Ringwald #endif
993a4331339SMatthias Ringwald     return true;
994cd16791dSMatthias Ringwald }
995cd16791dSMatthias Ringwald 
996cd16791dSMatthias Ringwald void mesh_lower_transport_reserve_slot(void){
997cd16791dSMatthias Ringwald }
998cd16791dSMatthias Ringwald 
99977ba3d3fSMatthias Ringwald void mesh_lower_transport_dump(void){
100077ba3d3fSMatthias Ringwald }
100177ba3d3fSMatthias Ringwald 
100277ba3d3fSMatthias Ringwald void mesh_lower_transport_reset(void){
1003a7f44e8dSMatthias Ringwald     if (lower_transport_outgoing_message){
1004a7f44e8dSMatthias Ringwald         while (!btstack_linked_list_empty(&lower_transport_outgoing_message->segments)){
1005a7f44e8dSMatthias Ringwald             mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_message->segments);
1006a7f44e8dSMatthias Ringwald             mesh_network_pdu_free(network_pdu);
1007a7f44e8dSMatthias Ringwald         }
1008a7f44e8dSMatthias Ringwald         lower_transport_outgoing_message = NULL;
1009a7f44e8dSMatthias Ringwald     }
10101e6cebb8SMatthias Ringwald     mesh_network_pdu_free(lower_transport_outgoing_segment);
1011e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = false;
10121e6cebb8SMatthias Ringwald     lower_transport_outgoing_segment = NULL;
101377ba3d3fSMatthias Ringwald }
101477ba3d3fSMatthias Ringwald 
101577ba3d3fSMatthias Ringwald void mesh_lower_transport_init(){
101677ba3d3fSMatthias Ringwald     // register with network layer
101777ba3d3fSMatthias Ringwald     mesh_network_set_higher_layer_handler(&mesh_lower_transport_received_message);
101877ba3d3fSMatthias Ringwald     // allocate network_pdu for segmentation
1019e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = false;
102077ba3d3fSMatthias Ringwald     lower_transport_outgoing_segment = mesh_network_pdu_get();
102177ba3d3fSMatthias Ringwald }
102277ba3d3fSMatthias Ringwald 
102377ba3d3fSMatthias 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)){
102477ba3d3fSMatthias Ringwald     higher_layer_handler = pdu_handler;
102577ba3d3fSMatthias Ringwald }
1026