xref: /btstack/src/mesh/mesh_lower_transport.c (revision 212d7460b51305c7cacec5e89401f03abaee2092)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
40cb7b3e6fSMatthias Ringwald #include <inttypes.h>
4177ba3d3fSMatthias Ringwald #include <stdio.h>
4277ba3d3fSMatthias Ringwald #include <stdlib.h>
4377ba3d3fSMatthias Ringwald #include <string.h>
44f4854a5eSMatthias Ringwald 
4577ba3d3fSMatthias Ringwald #include "btstack_memory.h"
46f4854a5eSMatthias Ringwald #include "btstack_util.h"
4779eb95b0SMatthias Ringwald #include "btstack_bool.h"
487c76cd61SMatthias Ringwald #include "btstack_debug.h"
49f4854a5eSMatthias Ringwald 
50683cf298SMatthias Ringwald #include "mesh/beacon.h"
51f4854a5eSMatthias Ringwald #include "mesh/mesh_iv_index_seq_number.h"
52f4854a5eSMatthias Ringwald #include "mesh/mesh_lower_transport.h"
53683cf298SMatthias Ringwald #include "mesh/mesh_node.h"
54f4854a5eSMatthias Ringwald #include "mesh/mesh_peer.h"
5577ba3d3fSMatthias Ringwald 
56b66a7a84SMatthias Ringwald #define LOG_LOWER_TRANSPORT
57b66a7a84SMatthias Ringwald 
5827e8a9aaSMatthias Ringwald // prototypes
5979eb95b0SMatthias Ringwald static void mesh_lower_transport_run(void);
6012835760SMatthias Ringwald static void mesh_lower_transport_outgoing_complete(mesh_segmented_pdu_t * segmented_pdu, mesh_transport_status_t status);
6127e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts);
6279eb95b0SMatthias Ringwald 
6379eb95b0SMatthias Ringwald 
6479eb95b0SMatthias Ringwald // lower transport outgoing state
6579eb95b0SMatthias Ringwald 
66b4977f35SMatthias Ringwald // queued mesh_segmented_pdu_t or mesh_network_pdu_t
6779eb95b0SMatthias Ringwald static btstack_linked_list_t lower_transport_outgoing_ready;
6879eb95b0SMatthias Ringwald 
69b4977f35SMatthias Ringwald // mesh_segmented_pdu_t to unicast address, segment transmission timer is active
70b4977f35SMatthias Ringwald static btstack_linked_list_t lower_transport_outgoing_waiting;
71b4977f35SMatthias Ringwald 
7279eb95b0SMatthias Ringwald // active outgoing segmented message
7379eb95b0SMatthias Ringwald static mesh_segmented_pdu_t * lower_transport_outgoing_message;
74e3f8b4e7SMatthias Ringwald // index of outgoing segment
7579eb95b0SMatthias Ringwald static uint16_t               lower_transport_outgoing_seg_o;
76e3f8b4e7SMatthias Ringwald // network pdu with outgoing segment
7779eb95b0SMatthias Ringwald static mesh_network_pdu_t   * lower_transport_outgoing_segment;
78e3f8b4e7SMatthias Ringwald // segment currently queued at network layer (only valid for lower_transport_outgoing_message)
79e3f8b4e7SMatthias Ringwald static bool                    lower_transport_outgoing_segment_at_network_layer;
8079eb95b0SMatthias Ringwald // transmission timeout occurred (while outgoing segment queued at network layer)
8179eb95b0SMatthias Ringwald static bool                    lower_transport_outgoing_transmission_timeout;
8212835760SMatthias Ringwald // transmission completed fully ack'ed (while outgoing segment queued at network layer)
8379eb95b0SMatthias Ringwald static bool                    lower_transport_outgoing_transmission_complete;
8412835760SMatthias Ringwald // transmission aborted by remote (while outgoing segment queued at network layer)
8512835760SMatthias Ringwald static bool                    lower_transport_outgoing_transmission_aborted;
8679eb95b0SMatthias Ringwald 
8779eb95b0SMatthias Ringwald // active outgoing unsegmented message
8879eb95b0SMatthias Ringwald static mesh_network_pdu_t *   lower_transport_outgoing_network_pdu;
8979eb95b0SMatthias Ringwald 
9079eb95b0SMatthias Ringwald // deliver to higher layer
9177ba3d3fSMatthias Ringwald static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
9279eb95b0SMatthias Ringwald static mesh_pdu_t * mesh_lower_transport_higher_layer_pdu;
9379eb95b0SMatthias Ringwald static btstack_linked_list_t mesh_lower_transport_queued_for_higher_layer;
9477ba3d3fSMatthias Ringwald 
mesh_print_hex(const char * name,const uint8_t * data,uint16_t len)9577ba3d3fSMatthias Ringwald static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){
9677ba3d3fSMatthias Ringwald     printf("%-20s ", name);
9777ba3d3fSMatthias Ringwald     printf_hexdump(data, len);
9877ba3d3fSMatthias Ringwald }
9977ba3d3fSMatthias Ringwald 
10077ba3d3fSMatthias Ringwald // utility
10177ba3d3fSMatthias Ringwald 
mesh_segmented_pdu_get(void)10279eb95b0SMatthias Ringwald mesh_segmented_pdu_t * mesh_segmented_pdu_get(void){
103a4bbc09dSMatthias Ringwald     mesh_segmented_pdu_t * message_pdu = btstack_memory_mesh_segmented_pdu_get();
104926e8875SMatthias Ringwald     if (message_pdu){
105a4bbc09dSMatthias Ringwald         message_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED;
106926e8875SMatthias Ringwald     }
107926e8875SMatthias Ringwald     return message_pdu;
108926e8875SMatthias Ringwald }
109926e8875SMatthias Ringwald 
mesh_segmented_pdu_free(mesh_segmented_pdu_t * message_pdu)11079eb95b0SMatthias Ringwald void mesh_segmented_pdu_free(mesh_segmented_pdu_t * message_pdu){
111cdcfd2c1SMatthias Ringwald     while (message_pdu->segments){
112cdcfd2c1SMatthias Ringwald         mesh_network_pdu_t * segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&message_pdu->segments);
113cdcfd2c1SMatthias Ringwald         mesh_network_pdu_free(segment);
114cdcfd2c1SMatthias Ringwald     }
115a4bbc09dSMatthias Ringwald     btstack_memory_mesh_segmented_pdu_free(message_pdu);
116926e8875SMatthias Ringwald }
117926e8875SMatthias Ringwald 
11827e8a9aaSMatthias Ringwald // INCOMING //
11927e8a9aaSMatthias Ringwald 
mesh_lower_transport_incoming_deliver_to_higher_layer(void)12027e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_deliver_to_higher_layer(void){
121fc5e2620SMatthias Ringwald     if (mesh_lower_transport_higher_layer_pdu == NULL && !btstack_linked_list_empty(&mesh_lower_transport_queued_for_higher_layer)){
1222ae11e2cSMatthias Ringwald         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&mesh_lower_transport_queued_for_higher_layer);
1232ae11e2cSMatthias Ringwald 
1242ae11e2cSMatthias Ringwald         switch (pdu->pdu_type){
1255236fe42SMatthias Ringwald             case MESH_PDU_TYPE_NETWORK:
1263d9c3b8eSMatthias Ringwald                 // unsegmented pdu
127e4121a34SMatthias Ringwald                 mesh_lower_transport_higher_layer_pdu = (mesh_pdu_t *) pdu;
128e4121a34SMatthias Ringwald                 pdu->pdu_type = MESH_PDU_TYPE_UNSEGMENTED;
1292ae11e2cSMatthias Ringwald                 break;
1305236fe42SMatthias Ringwald             case MESH_PDU_TYPE_SEGMENTED:
1312ae11e2cSMatthias Ringwald                 // segmented control or access pdu
1322ae11e2cSMatthias Ringwald                 mesh_lower_transport_higher_layer_pdu = pdu;
1332ae11e2cSMatthias Ringwald                 break;
1345236fe42SMatthias Ringwald             default:
1355236fe42SMatthias Ringwald                 btstack_assert(false);
1365236fe42SMatthias Ringwald                 break;
1372ae11e2cSMatthias Ringwald         }
138fc5e2620SMatthias Ringwald         higher_layer_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, mesh_lower_transport_higher_layer_pdu);
139fc5e2620SMatthias Ringwald     }
140fc5e2620SMatthias Ringwald }
141fc5e2620SMatthias Ringwald 
mesh_lower_transport_incoming_queue_for_higher_layer(mesh_pdu_t * pdu)14227e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_queue_for_higher_layer(mesh_pdu_t * pdu){
143fc5e2620SMatthias Ringwald     btstack_linked_list_add_tail(&mesh_lower_transport_queued_for_higher_layer, (btstack_linked_item_t *) pdu);
14427e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_deliver_to_higher_layer();
1459a0bb1c9SMatthias Ringwald }
14668d3bb6cSMatthias Ringwald 
mesh_lower_transport_incoming_setup_acknowledge_message(uint8_t * data,uint8_t obo,uint16_t seq_zero,uint32_t block_ack)14727e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_setup_acknowledge_message(uint8_t * data, uint8_t obo, uint16_t seq_zero, uint32_t block_ack){
14877ba3d3fSMatthias Ringwald     // printf("ACK Upper Transport, seq_zero %x\n", seq_zero);
14977ba3d3fSMatthias Ringwald     data[0] = 0;    // SEG = 0, Opcode = 0
15077ba3d3fSMatthias Ringwald     big_endian_store_16( data, 1, (obo << 15) | (seq_zero << 2) | 0);    // OBO, SeqZero, RFU
15177ba3d3fSMatthias Ringwald     big_endian_store_32( data, 3, block_ack);
152b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
15377ba3d3fSMatthias Ringwald     mesh_print_hex("ACK Upper Transport", data, 7);
154b66a7a84SMatthias Ringwald #endif
15577ba3d3fSMatthias Ringwald }
15677ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_send_ack(uint16_t netkey_index,uint8_t ttl,uint16_t dest,uint16_t seq_zero,uint32_t block_ack)15727e8a9aaSMatthias 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){
15877ba3d3fSMatthias Ringwald     // setup ack message
15977ba3d3fSMatthias Ringwald     uint8_t  ack_msg[7];
16027e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_setup_acknowledge_message(ack_msg, 0, seq_zero, block_ack);
16177ba3d3fSMatthias Ringwald     //
16277ba3d3fSMatthias 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."
16377ba3d3fSMatthias Ringwald     // if (ttl <= 1) return 0;
16477ba3d3fSMatthias Ringwald 
16577ba3d3fSMatthias Ringwald     // TODO: check transport_pdu_len depending on ctl
16677ba3d3fSMatthias Ringwald 
16777ba3d3fSMatthias Ringwald     // lookup network by netkey_index
16877ba3d3fSMatthias Ringwald     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
16977ba3d3fSMatthias Ringwald     if (!network_key) return;
17077ba3d3fSMatthias Ringwald 
17177ba3d3fSMatthias Ringwald     // allocate network_pdu
17277ba3d3fSMatthias Ringwald     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
17377ba3d3fSMatthias Ringwald     if (!network_pdu) return;
17477ba3d3fSMatthias Ringwald 
17577ba3d3fSMatthias Ringwald     // setup network_pdu
176c0005391SMatthias Ringwald     network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENT_ACKNOWLEDGMENT;
177001c65e0SMatthias 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));
17877ba3d3fSMatthias Ringwald 
17977ba3d3fSMatthias Ringwald     // send network_pdu
18077ba3d3fSMatthias Ringwald     mesh_network_send_pdu(network_pdu);
18177ba3d3fSMatthias Ringwald }
18277ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_send_ack_for_segmented_pdu(mesh_segmented_pdu_t * segmented_pdu)18327e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_send_ack_for_segmented_pdu(mesh_segmented_pdu_t * segmented_pdu){
184bd915455SMatthias Ringwald     uint16_t seq_zero = segmented_pdu->seq & 0x1fff;
18527e8a9aaSMatthias Ringwald     uint8_t  ttl = segmented_pdu->ctl_ttl & 0x7f;
18627e8a9aaSMatthias Ringwald     uint16_t dest = segmented_pdu->src;
18727e8a9aaSMatthias Ringwald     uint16_t netkey_index = segmented_pdu->netkey_index;
188b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
18977ba3d3fSMatthias Ringwald     printf("mesh_transport_send_ack_for_transport_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n",
19027e8a9aaSMatthias Ringwald            segmented_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest);
191b66a7a84SMatthias Ringwald #endif
19227e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, segmented_pdu->block_ack);
19377ba3d3fSMatthias Ringwald }
19477ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_send_ack_for_network_pdu(mesh_network_pdu_t * network_pdu,uint16_t seq_zero,uint32_t block_ack)19527e8a9aaSMatthias 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) {
19677ba3d3fSMatthias Ringwald     uint8_t ttl = mesh_network_ttl(network_pdu);
19777ba3d3fSMatthias Ringwald     uint16_t dest = mesh_network_src(network_pdu);
19877ba3d3fSMatthias Ringwald     uint16_t netkey_index = network_pdu->netkey_index;
199b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
20077ba3d3fSMatthias Ringwald     printf("mesh_transport_send_ack_for_network_pdu %p with netkey_index %x, TTL = %u, SeqZero = %x, SRC = %x, DST = %x\n",
201001c65e0SMatthias Ringwald            network_pdu, netkey_index, ttl, seq_zero, mesh_node_get_primary_element_address(), dest);
202b66a7a84SMatthias Ringwald #endif
20327e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack(netkey_index, ttl, dest, seq_zero, block_ack);
20477ba3d3fSMatthias Ringwald }
20577ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_stop_acknowledgment_timer(mesh_segmented_pdu_t * segmented_pdu)20627e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){
207354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0) return;
208354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
20927e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer);
210926e8875SMatthias Ringwald }
211926e8875SMatthias Ringwald 
mesh_lower_transport_incoming_stop_incomplete_timer(mesh_segmented_pdu_t * segmented_pdu)21227e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_stop_incomplete_timer(mesh_segmented_pdu_t *segmented_pdu){
213354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) == 0) return;
214354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER;
21527e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer);
216926e8875SMatthias Ringwald }
217926e8875SMatthias Ringwald 
mesh_lower_transport_incoming_segmented_message_complete(mesh_segmented_pdu_t * segmented_pdu)21827e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_segmented_message_complete(mesh_segmented_pdu_t * segmented_pdu){
21977ba3d3fSMatthias Ringwald     // stop timers
22027e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_stop_acknowledgment_timer(segmented_pdu);
22127e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_stop_incomplete_timer(segmented_pdu);
22277ba3d3fSMatthias Ringwald     // stop reassembly
22327e8a9aaSMatthias Ringwald     mesh_peer_t * peer = mesh_peer_for_addr(segmented_pdu->src);
22477ba3d3fSMatthias Ringwald     if (peer){
225926e8875SMatthias Ringwald         peer->message_pdu = NULL;
226926e8875SMatthias Ringwald     }
22777ba3d3fSMatthias Ringwald }
22877ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_ack_timeout(btstack_timer_source_t * ts)22927e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_ack_timeout(btstack_timer_source_t *ts){
23027e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
231b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
23227e8a9aaSMatthias Ringwald     printf("ACK: acknowledgement timer fired for %p, send ACK\n", segmented_pdu);
233b66a7a84SMatthias Ringwald #endif
234354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
23527e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack_for_segmented_pdu(segmented_pdu);
23677ba3d3fSMatthias Ringwald }
23777ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_incomplete_timeout(btstack_timer_source_t * ts)23827e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_incomplete_timeout(btstack_timer_source_t *ts){
23927e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
240b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
24127e8a9aaSMatthias Ringwald     printf("mesh_lower_transport_incoming_incomplete_timeout for %p - give up\n", segmented_pdu);
242b66a7a84SMatthias Ringwald #endif
24327e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_segmented_message_complete(segmented_pdu);
24477ba3d3fSMatthias Ringwald     // free message
2456dac9d64SMatthias Ringwald     mesh_segmented_pdu_free(segmented_pdu);
24677ba3d3fSMatthias Ringwald }
24777ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_start_acknowledgment_timer(mesh_segmented_pdu_t * segmented_pdu,uint32_t timeout)24827e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_start_acknowledgment_timer(mesh_segmented_pdu_t * segmented_pdu, uint32_t timeout){
249b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
25027e8a9aaSMatthias Ringwald     printf("ACK: start rx ack timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout);
251b66a7a84SMatthias Ringwald #endif
25227e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout);
25327e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_incoming_ack_timeout);
25427e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, segmented_pdu);
25527e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer);
256354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_ACK_TIMER;
25777ba3d3fSMatthias Ringwald }
25877ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_restart_incomplete_timer(mesh_segmented_pdu_t * segmented_pdu,uint32_t timeout,void (* callback)(btstack_timer_source_t * ts))25927e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_restart_incomplete_timer(mesh_segmented_pdu_t * segmented_pdu, uint32_t timeout,
26077ba3d3fSMatthias Ringwald                                                                    void (*callback)(btstack_timer_source_t *ts)){
261b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
26227e8a9aaSMatthias Ringwald     printf("RX-(re)start incomplete timer for %p, timeout %u ms\n", segmented_pdu, (int) timeout);
263b66a7a84SMatthias Ringwald #endif
264354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) != 0){
26527e8a9aaSMatthias Ringwald         btstack_run_loop_remove_timer(&segmented_pdu->incomplete_timer);
26677ba3d3fSMatthias Ringwald     }
26727e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->incomplete_timer, timeout);
26827e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->incomplete_timer, callback);
26927e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->incomplete_timer, segmented_pdu);
27027e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->incomplete_timer);
271354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER;
27277ba3d3fSMatthias Ringwald }
27377ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_pdu_for_segmented_message(mesh_network_pdu_t * network_pdu)27427e8a9aaSMatthias Ringwald static mesh_segmented_pdu_t * mesh_lower_transport_incoming_pdu_for_segmented_message(mesh_network_pdu_t *network_pdu){
27577ba3d3fSMatthias Ringwald     uint16_t src = mesh_network_src(network_pdu);
27677ba3d3fSMatthias Ringwald     uint16_t seq_zero = ( big_endian_read_16(mesh_network_pdu_data(network_pdu), 1) >> 2) & 0x1fff;
277b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
27877ba3d3fSMatthias Ringwald     printf("mesh_transport_pdu_for_segmented_message: seq_zero %x\n", seq_zero);
279b66a7a84SMatthias Ringwald #endif
28077ba3d3fSMatthias Ringwald     mesh_peer_t * peer = mesh_peer_for_addr(src);
28177ba3d3fSMatthias Ringwald     if (!peer) {
28277ba3d3fSMatthias Ringwald         return NULL;
28377ba3d3fSMatthias Ringwald     }
284b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
28577ba3d3fSMatthias Ringwald     printf("mesh_seq_zero_validate(%x, %x) -- last (%x, %x)\n", src, seq_zero, peer->address, peer->seq_zero);
286b66a7a84SMatthias Ringwald #endif
28777ba3d3fSMatthias Ringwald 
28877ba3d3fSMatthias Ringwald     // reception of transport message ongoing
289926e8875SMatthias Ringwald     if (peer->message_pdu){
29077ba3d3fSMatthias Ringwald         // check if segment for same seq zero
291bd915455SMatthias Ringwald         uint16_t active_seq_zero = peer->message_pdu->seq & 0x1fff;
29277ba3d3fSMatthias Ringwald         if (active_seq_zero == seq_zero) {
293b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
29477ba3d3fSMatthias Ringwald             printf("mesh_transport_pdu_for_segmented_message: segment for current transport pdu with SeqZero %x\n", active_seq_zero);
295b66a7a84SMatthias Ringwald #endif
296926e8875SMatthias Ringwald             return peer->message_pdu;
29777ba3d3fSMatthias Ringwald         } else {
29877ba3d3fSMatthias Ringwald             // seq zero differs from current transport pdu, but current pdu is not complete
299b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
30077ba3d3fSMatthias Ringwald             printf("mesh_transport_pdu_for_segmented_message: drop segment. current transport pdu SeqZero %x, now %x\n", active_seq_zero, seq_zero);
301b66a7a84SMatthias Ringwald #endif
30277ba3d3fSMatthias Ringwald             return NULL;
30377ba3d3fSMatthias Ringwald         }
30477ba3d3fSMatthias Ringwald     }
30577ba3d3fSMatthias Ringwald 
30677ba3d3fSMatthias Ringwald     // send ACK if segment for previously completed transport pdu (no ongoing reception, block ack is cleared)
30777ba3d3fSMatthias Ringwald     if ((seq_zero == peer->seq_zero) && (peer->block_ack != 0)){
308b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
30977ba3d3fSMatthias Ringwald         printf("mesh_transport_pdu_for_segmented_message: segment for last completed message. send ack\n");
310b66a7a84SMatthias Ringwald #endif
31127e8a9aaSMatthias Ringwald         mesh_lower_transport_incoming_send_ack_for_network_pdu(network_pdu, seq_zero, peer->block_ack);
31277ba3d3fSMatthias Ringwald         return NULL;
31377ba3d3fSMatthias Ringwald     }
31477ba3d3fSMatthias Ringwald 
31577ba3d3fSMatthias Ringwald     // reconstruct lowest 24 bit of SeqAuth
31677ba3d3fSMatthias Ringwald     uint32_t seq = mesh_network_seq(network_pdu);
31777ba3d3fSMatthias Ringwald     uint32_t seq_auth = (seq & 0xffe000) | seq_zero;
31877ba3d3fSMatthias Ringwald     if (seq_auth > seq){
31977ba3d3fSMatthias Ringwald         seq_auth -= 0x2000;
32077ba3d3fSMatthias Ringwald     }
32177ba3d3fSMatthias Ringwald 
32277ba3d3fSMatthias Ringwald     // no transport pdu active, check new message: seq auth is greater OR seq auth is same but no segments
32377ba3d3fSMatthias Ringwald     if (seq_auth > peer->seq_auth || (seq_auth == peer->seq_auth && peer->block_ack == 0)){
32479eb95b0SMatthias Ringwald         mesh_segmented_pdu_t * pdu = mesh_segmented_pdu_get();
32577ba3d3fSMatthias Ringwald         if (!pdu) return NULL;
32677ba3d3fSMatthias Ringwald 
32777ba3d3fSMatthias Ringwald         // cache network pdu header
3287cb45abfSMatthias Ringwald         pdu->ivi_nid = network_pdu->data[0];
3297cb45abfSMatthias Ringwald         pdu->ctl_ttl = network_pdu->data[1];
3307cb45abfSMatthias Ringwald         pdu->src = big_endian_read_16(network_pdu->data, 5);
3317cb45abfSMatthias Ringwald         pdu->dst = big_endian_read_16(network_pdu->data, 7);
33277ba3d3fSMatthias Ringwald         // store lower 24 bit of SeqAuth for App / Device Nonce
3337cb45abfSMatthias Ringwald         pdu->seq = seq_auth;
33477ba3d3fSMatthias Ringwald 
335e5828668SMatthias Ringwald         // get akf_aid & transmic
336b30b4f6eSMatthias Ringwald         pdu->akf_aid_control = network_pdu->data[9] & 0x7f;
337b30b4f6eSMatthias Ringwald         if ((network_pdu->data[10] & 0x80) != 0){
338b30b4f6eSMatthias Ringwald             pdu->flags |= MESH_TRANSPORT_FLAG_TRANSMIC_64;
339b30b4f6eSMatthias Ringwald         }
340e5828668SMatthias Ringwald 
34177ba3d3fSMatthias Ringwald         // store meta data in new pdu
34277ba3d3fSMatthias Ringwald         pdu->netkey_index = network_pdu->netkey_index;
34377ba3d3fSMatthias Ringwald         pdu->block_ack = 0;
344354884c7SMatthias Ringwald         pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
34577ba3d3fSMatthias Ringwald 
34677ba3d3fSMatthias Ringwald         // update peer info
347926e8875SMatthias Ringwald         peer->message_pdu   = pdu;
34877ba3d3fSMatthias Ringwald         peer->seq_zero      = seq_zero;
34977ba3d3fSMatthias Ringwald         peer->seq_auth      = seq_auth;
35077ba3d3fSMatthias Ringwald         peer->block_ack     = 0;
35177ba3d3fSMatthias Ringwald 
352b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
353cb7b3e6fSMatthias Ringwald         printf("mesh_transport_pdu_for_segmented_message: setup transport pdu %p for src %x, seq %06" PRIx32 ", seq_zero %x\n", pdu, src,
3543945bdb6SMatthias Ringwald                pdu->seq, seq_zero);
355b66a7a84SMatthias Ringwald #endif
356926e8875SMatthias Ringwald         return peer->message_pdu;
35777ba3d3fSMatthias Ringwald     }  else {
35877ba3d3fSMatthias Ringwald         // seq zero differs from current transport pdu
359b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
36077ba3d3fSMatthias Ringwald         printf("mesh_transport_pdu_for_segmented_message: drop segment for old seq %x\n", seq_zero);
361b66a7a84SMatthias Ringwald #endif
36277ba3d3fSMatthias Ringwald         return NULL;
36377ba3d3fSMatthias Ringwald     }
36477ba3d3fSMatthias Ringwald }
36577ba3d3fSMatthias Ringwald 
mesh_lower_transport_incoming_process_segment(mesh_segmented_pdu_t * message_pdu,mesh_network_pdu_t * network_pdu)36627e8a9aaSMatthias Ringwald static void mesh_lower_transport_incoming_process_segment(mesh_segmented_pdu_t * message_pdu, mesh_network_pdu_t * network_pdu){
36777ba3d3fSMatthias Ringwald 
36877ba3d3fSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
36977ba3d3fSMatthias Ringwald     uint8_t   lower_transport_pdu_len = mesh_network_pdu_len(network_pdu);
37077ba3d3fSMatthias Ringwald 
37177ba3d3fSMatthias Ringwald     // get seq_zero
37277ba3d3fSMatthias Ringwald     uint16_t seq_zero =  ( big_endian_read_16(lower_transport_pdu, 1) >> 2) & 0x1fff;
37377ba3d3fSMatthias Ringwald 
37477ba3d3fSMatthias Ringwald     // get seg fields
37577ba3d3fSMatthias Ringwald     uint8_t  seg_o    =  ( big_endian_read_16(lower_transport_pdu, 2) >> 5) & 0x001f;
37677ba3d3fSMatthias Ringwald     uint8_t  seg_n    =  lower_transport_pdu[3] & 0x1f;
37777ba3d3fSMatthias Ringwald     uint8_t   segment_len  =  lower_transport_pdu_len - 4;
37877ba3d3fSMatthias Ringwald     uint8_t * segment_data = &lower_transport_pdu[4];
37977ba3d3fSMatthias Ringwald 
380b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
381b30b4f6eSMatthias Ringwald     uint8_t transmic_len = ((message_pdu->flags & MESH_TRANSPORT_FLAG_TRANSMIC_64) != 0) ? 64 : 32;
382b30b4f6eSMatthias 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);
38377ba3d3fSMatthias Ringwald     mesh_print_hex("Segment", segment_data, segment_len);
384b66a7a84SMatthias Ringwald #endif
38577ba3d3fSMatthias Ringwald 
386926e8875SMatthias Ringwald     // drop if already stored
387926e8875SMatthias Ringwald     if ((message_pdu->block_ack & (1<<seg_o)) != 0){
388926e8875SMatthias Ringwald         mesh_network_message_processed_by_higher_layer(network_pdu);
389926e8875SMatthias Ringwald         return;
390926e8875SMatthias Ringwald     }
391926e8875SMatthias Ringwald 
39277ba3d3fSMatthias Ringwald     // mark as received
393926e8875SMatthias Ringwald     message_pdu->block_ack |= (1<<seg_o);
394926e8875SMatthias Ringwald 
39516fd08f9SMatthias Ringwald     // store segment
39616fd08f9SMatthias Ringwald     uint8_t max_segment_len = mesh_network_control(network_pdu) ? 8 : 12;
39716fd08f9SMatthias Ringwald     mesh_network_pdu_t * latest_segment = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments);
39816fd08f9SMatthias Ringwald     if ((latest_segment != NULL) && ((MESH_NETWORK_PAYLOAD_MAX - latest_segment->len) > (max_segment_len  + 1))){
39916fd08f9SMatthias Ringwald         // store in last added segment if there is enough space available
40016fd08f9SMatthias Ringwald         latest_segment->data[latest_segment->len++] = seg_o;
40116fd08f9SMatthias Ringwald         (void) memcpy(&latest_segment->data[latest_segment->len], &lower_transport_pdu[4], segment_len);
40216fd08f9SMatthias Ringwald         latest_segment->len += segment_len;
40316fd08f9SMatthias Ringwald         // free buffer
40416fd08f9SMatthias Ringwald         mesh_network_message_processed_by_higher_layer(network_pdu);
40516fd08f9SMatthias Ringwald     } else {
40616fd08f9SMatthias Ringwald         // move to beginning
40716fd08f9SMatthias Ringwald         network_pdu->data[0] = seg_o;
40816fd08f9SMatthias Ringwald         uint8_t i;
40916fd08f9SMatthias Ringwald         for (i=0;i<segment_len;i++){
41016fd08f9SMatthias Ringwald             network_pdu->data[1+i] = network_pdu->data[13+i];
41116fd08f9SMatthias Ringwald         }
41216fd08f9SMatthias Ringwald         network_pdu->len = 1 + segment_len;
41316fd08f9SMatthias Ringwald         // add this buffer
414926e8875SMatthias Ringwald         btstack_linked_list_add(&message_pdu->segments, (btstack_linked_item_t *) network_pdu);
41516fd08f9SMatthias Ringwald     }
416926e8875SMatthias Ringwald 
41777ba3d3fSMatthias Ringwald     // last segment -> store len
41877ba3d3fSMatthias Ringwald     if (seg_o == seg_n){
4198a129166SMatthias Ringwald         message_pdu->len = (seg_n * max_segment_len) + segment_len;
420b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
421926e8875SMatthias Ringwald         printf("Assembled payload len %u\n", message_pdu->len);
422b66a7a84SMatthias Ringwald #endif
42377ba3d3fSMatthias Ringwald     }
42477ba3d3fSMatthias Ringwald 
42577ba3d3fSMatthias Ringwald     // check for complete
42677ba3d3fSMatthias Ringwald     int i;
42777ba3d3fSMatthias Ringwald     for (i=0;i<=seg_n;i++){
428926e8875SMatthias Ringwald         if ( (message_pdu->block_ack & (1<<i)) == 0) return;
429926e8875SMatthias Ringwald     }
430926e8875SMatthias Ringwald 
43177ba3d3fSMatthias Ringwald     // store block ack in peer info
4323945bdb6SMatthias Ringwald     mesh_peer_t * peer = mesh_peer_for_addr(message_pdu->src);
43377ba3d3fSMatthias Ringwald     // TODO: check if NULL check can be removed
43477ba3d3fSMatthias Ringwald     if (peer){
435926e8875SMatthias Ringwald         peer->block_ack = message_pdu->block_ack;
43677ba3d3fSMatthias Ringwald     }
43777ba3d3fSMatthias Ringwald 
43877ba3d3fSMatthias Ringwald     // send ack
43927e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_send_ack_for_segmented_pdu(message_pdu);
44077ba3d3fSMatthias Ringwald 
44177ba3d3fSMatthias Ringwald     // forward to upper transport
44227e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) message_pdu);
4432a974a69SMatthias Ringwald 
4442a974a69SMatthias Ringwald     // mark as done
44527e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_segmented_message_complete(message_pdu);
44658bb2379SMatthias Ringwald }
44758bb2379SMatthias Ringwald 
mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu)44877ba3d3fSMatthias Ringwald void mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
449fc5e2620SMatthias Ringwald     btstack_assert(pdu == mesh_lower_transport_higher_layer_pdu);
450fc5e2620SMatthias Ringwald     mesh_lower_transport_higher_layer_pdu = NULL;
45184011563SMatthias Ringwald     mesh_network_pdu_t * network_pdu;
45277ba3d3fSMatthias Ringwald     switch (pdu->pdu_type){
453a4bbc09dSMatthias Ringwald         case MESH_PDU_TYPE_SEGMENTED:
4548a504b30SMatthias Ringwald             // free segments
45579eb95b0SMatthias Ringwald             mesh_segmented_pdu_free((mesh_segmented_pdu_t *) pdu);
45635699c48SMatthias Ringwald             break;
4578facb3eaSMatthias Ringwald         case MESH_PDU_TYPE_UNSEGMENTED:
458e4121a34SMatthias Ringwald             network_pdu = (mesh_network_pdu_t *) pdu;
45984011563SMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
4602ae11e2cSMatthias Ringwald             break;
46177ba3d3fSMatthias Ringwald         default:
4623d9c3b8eSMatthias Ringwald             btstack_assert(0);
46377ba3d3fSMatthias Ringwald             break;
46477ba3d3fSMatthias Ringwald     }
46527e8a9aaSMatthias Ringwald     mesh_lower_transport_incoming_deliver_to_higher_layer();
46677ba3d3fSMatthias Ringwald }
46777ba3d3fSMatthias Ringwald 
46827e8a9aaSMatthias Ringwald // OUTGOING //
46927e8a9aaSMatthias Ringwald 
mesh_lower_transport_outgoing_setup_block_ack(mesh_segmented_pdu_t * message_pdu)47027e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_setup_block_ack(mesh_segmented_pdu_t *message_pdu){
47127e8a9aaSMatthias Ringwald     // setup block ack - set bit for segment to send, will be cleared on ack
47227e8a9aaSMatthias Ringwald     int      ctl = message_pdu->ctl_ttl >> 7;
47327e8a9aaSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
47427e8a9aaSMatthias Ringwald     uint8_t  seg_n = (message_pdu->len - 1) / max_segment_len;
47527e8a9aaSMatthias Ringwald     if (seg_n < 31){
47627e8a9aaSMatthias Ringwald         message_pdu->block_ack = (1 << (seg_n+1)) - 1;
47777ba3d3fSMatthias Ringwald     } else {
47827e8a9aaSMatthias Ringwald         message_pdu->block_ack = 0xffffffff;
47977ba3d3fSMatthias Ringwald     }
48077ba3d3fSMatthias Ringwald }
48177ba3d3fSMatthias Ringwald 
mesh_lower_transport_outgoing_message_for_dst(uint16_t dst)482e3f8b4e7SMatthias Ringwald static mesh_segmented_pdu_t * mesh_lower_transport_outgoing_message_for_dst(uint16_t dst){
483e3f8b4e7SMatthias Ringwald     if (lower_transport_outgoing_message != NULL && lower_transport_outgoing_message->dst == dst){
484e3f8b4e7SMatthias Ringwald         return lower_transport_outgoing_message;
485e3f8b4e7SMatthias Ringwald     }
486e3f8b4e7SMatthias Ringwald     return NULL;
487e3f8b4e7SMatthias Ringwald }
488e3f8b4e7SMatthias Ringwald 
mesh_lower_transport_outgoing_process_segment_acknowledgement_message(mesh_network_pdu_t * network_pdu)48927e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_process_segment_acknowledgement_message(mesh_network_pdu_t *network_pdu){
490e3f8b4e7SMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = mesh_lower_transport_outgoing_message_for_dst( mesh_network_src(network_pdu));
491e3f8b4e7SMatthias Ringwald     if (segmented_pdu == NULL) return;
49227e8a9aaSMatthias Ringwald 
49327e8a9aaSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
49427e8a9aaSMatthias Ringwald     uint16_t seq_zero_pdu = big_endian_read_16(lower_transport_pdu, 1) >> 2;
49527e8a9aaSMatthias Ringwald     uint16_t seq_zero_out = lower_transport_outgoing_message->seq & 0x1fff;
49627e8a9aaSMatthias Ringwald     uint32_t block_ack = big_endian_read_32(lower_transport_pdu, 3);
49727e8a9aaSMatthias Ringwald 
49827e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
499cb7b3e6fSMatthias Ringwald     printf("[+] Segment Acknowledgment message with seq_zero %06x, block_ack %08" PRIx32 " - outgoing seq %06x, block_ack %08" PRIx32 "\n",
500e3f8b4e7SMatthias Ringwald            seq_zero_pdu, block_ack, seq_zero_out, segmented_pdu->block_ack);
50127e8a9aaSMatthias Ringwald #endif
50227e8a9aaSMatthias Ringwald 
50327e8a9aaSMatthias Ringwald     if (block_ack == 0){
50427e8a9aaSMatthias Ringwald         // If a Segment Acknowledgment message with the BlockAck field set to 0x00000000 is received,
50527e8a9aaSMatthias Ringwald         // then the Upper Transport PDU shall be immediately cancelled and the higher layers shall be notified that
50627e8a9aaSMatthias Ringwald         // the Upper Transport PDU has been cancelled.
50727e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
50827e8a9aaSMatthias Ringwald         printf("[+] Block Ack == 0 => Abort\n");
50927e8a9aaSMatthias Ringwald #endif
510e3f8b4e7SMatthias Ringwald         // current?
511e3f8b4e7SMatthias Ringwald         if ((lower_transport_outgoing_message == segmented_pdu) && lower_transport_outgoing_segment_at_network_layer){
51212835760SMatthias Ringwald             lower_transport_outgoing_transmission_aborted = true;
51327e8a9aaSMatthias Ringwald         } else {
51412835760SMatthias Ringwald             mesh_lower_transport_outgoing_complete(segmented_pdu, MESH_TRANSPORT_STATUS_SEND_ABORT_BY_REMOTE);
51527e8a9aaSMatthias Ringwald         }
51627e8a9aaSMatthias Ringwald         return;
51727e8a9aaSMatthias Ringwald     }
51827e8a9aaSMatthias Ringwald     if (seq_zero_pdu != seq_zero_out){
51927e8a9aaSMatthias Ringwald 
52027e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
52127e8a9aaSMatthias Ringwald         printf("[!] Seq Zero doesn't match\n");
52227e8a9aaSMatthias Ringwald #endif
52327e8a9aaSMatthias Ringwald         return;
52427e8a9aaSMatthias Ringwald     }
52527e8a9aaSMatthias Ringwald 
526e3f8b4e7SMatthias Ringwald     segmented_pdu->block_ack &= ~block_ack;
52727e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
528cb7b3e6fSMatthias Ringwald     printf("[+] Updated block_ack %08" PRIx32 "\n", segmented_pdu->block_ack);
52927e8a9aaSMatthias Ringwald #endif
53027e8a9aaSMatthias Ringwald 
531e3f8b4e7SMatthias Ringwald     if (segmented_pdu->block_ack == 0){
53227e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
53327e8a9aaSMatthias Ringwald         printf("[+] Sent complete\n");
53427e8a9aaSMatthias Ringwald #endif
53527e8a9aaSMatthias Ringwald 
536e3f8b4e7SMatthias Ringwald         if ((lower_transport_outgoing_message == segmented_pdu) && lower_transport_outgoing_segment_at_network_layer){
53727e8a9aaSMatthias Ringwald             lower_transport_outgoing_transmission_complete = true;
53827e8a9aaSMatthias Ringwald         } else {
53912835760SMatthias Ringwald             mesh_lower_transport_outgoing_complete(segmented_pdu, MESH_TRANSPORT_STATUS_SUCCESS);
54027e8a9aaSMatthias Ringwald         }
54127e8a9aaSMatthias Ringwald     }
54227e8a9aaSMatthias Ringwald }
54327e8a9aaSMatthias Ringwald 
mesh_lower_transport_outgoing_stop_acknowledgment_timer(mesh_segmented_pdu_t * segmented_pdu)54427e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_stop_acknowledgment_timer(mesh_segmented_pdu_t *segmented_pdu){
545354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0) return;
546354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
54727e8a9aaSMatthias Ringwald     btstack_run_loop_remove_timer(&segmented_pdu->acknowledgement_timer);
54827e8a9aaSMatthias Ringwald }
54927e8a9aaSMatthias Ringwald 
mesh_lower_transport_outgoing_restart_segment_transmission_timer(mesh_segmented_pdu_t * segmented_pdu)55027e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_restart_segment_transmission_timer(mesh_segmented_pdu_t *segmented_pdu){
55127e8a9aaSMatthias Ringwald     // restart segment transmission timer for unicast dst
55227e8a9aaSMatthias Ringwald     // - "This timer shall be set to a minimum of 200 + 50 * TTL milliseconds."
55327e8a9aaSMatthias Ringwald     uint32_t timeout = 200 + 50 * (segmented_pdu->ctl_ttl & 0x7f);
554354884c7SMatthias Ringwald     if ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) != 0){
55527e8a9aaSMatthias Ringwald         btstack_run_loop_remove_timer(&lower_transport_outgoing_message->acknowledgement_timer);
55627e8a9aaSMatthias Ringwald     }
55727e8a9aaSMatthias Ringwald 
55827e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
559cb7b3e6fSMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06" PRIx32 ": setup transmission timeout %u ms\n", segmented_pdu,
56027e8a9aaSMatthias Ringwald            segmented_pdu->seq, (int) timeout);
56127e8a9aaSMatthias Ringwald #endif
56227e8a9aaSMatthias Ringwald 
56327e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer(&segmented_pdu->acknowledgement_timer, timeout);
56427e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_handler(&segmented_pdu->acknowledgement_timer, &mesh_lower_transport_outgoing_segment_transmission_timeout);
56527e8a9aaSMatthias Ringwald     btstack_run_loop_set_timer_context(&segmented_pdu->acknowledgement_timer, lower_transport_outgoing_message);
56627e8a9aaSMatthias Ringwald     btstack_run_loop_add_timer(&segmented_pdu->acknowledgement_timer);
567354884c7SMatthias Ringwald     segmented_pdu->flags |= MESH_TRANSPORT_FLAG_ACK_TIMER;
56827e8a9aaSMatthias Ringwald }
56927e8a9aaSMatthias Ringwald 
mesh_lower_transport_outgoing_complete(mesh_segmented_pdu_t * segmented_pdu,mesh_transport_status_t status)57012835760SMatthias Ringwald static void mesh_lower_transport_outgoing_complete(mesh_segmented_pdu_t * segmented_pdu, mesh_transport_status_t status){
57169061c1cSMatthias Ringwald     btstack_assert(segmented_pdu != NULL);
57227e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
57369061c1cSMatthias Ringwald     printf("[+] outgoing_complete %p, ack timer active %u, incomplete active %u\n", segmented_pdu,
57469061c1cSMatthias Ringwald            ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) != 0), ((segmented_pdu->flags & MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER) != 0));
57527e8a9aaSMatthias Ringwald #endif
57627e8a9aaSMatthias Ringwald     // stop timers
57769061c1cSMatthias Ringwald     mesh_lower_transport_outgoing_stop_acknowledgment_timer(segmented_pdu);
57869061c1cSMatthias Ringwald 
57969061c1cSMatthias Ringwald     // remove from lists
58069061c1cSMatthias Ringwald     if (lower_transport_outgoing_message == segmented_pdu){
58169061c1cSMatthias Ringwald         lower_transport_outgoing_message = NULL;
58269061c1cSMatthias Ringwald     } else {
58369061c1cSMatthias Ringwald         btstack_linked_list_remove(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) segmented_pdu);
58469061c1cSMatthias Ringwald         btstack_linked_list_remove(&lower_transport_outgoing_ready, (btstack_linked_item_t *) segmented_pdu);
58569061c1cSMatthias Ringwald     }
58627e8a9aaSMatthias Ringwald 
58727e8a9aaSMatthias Ringwald     // notify upper transport
58812835760SMatthias Ringwald     higher_layer_handler(MESH_TRANSPORT_PDU_SENT, status, (mesh_pdu_t *) segmented_pdu);
58927e8a9aaSMatthias Ringwald }
59027e8a9aaSMatthias Ringwald 
mesh_lower_transport_outgoing_setup_segment(mesh_segmented_pdu_t * message_pdu,uint8_t seg_o,mesh_network_pdu_t * network_pdu)59127e8a9aaSMatthias 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){
59277ba3d3fSMatthias Ringwald 
5933945bdb6SMatthias Ringwald     int ctl = message_pdu->ctl_ttl >> 7;
59477ba3d3fSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
59577ba3d3fSMatthias Ringwald 
596b16fda24SMatthias 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)
597b16fda24SMatthias Ringwald     uint32_t seq;
598a7f44e8dSMatthias Ringwald     if ((message_pdu->flags & MESH_TRANSPORT_FLAG_SEQ_RESERVED) != 0){
599a7f44e8dSMatthias Ringwald         message_pdu->flags &= ~(MESH_TRANSPORT_FLAG_SEQ_RESERVED);
6003945bdb6SMatthias Ringwald         seq = message_pdu->seq;
601b16fda24SMatthias Ringwald     } else {
602b16fda24SMatthias Ringwald         seq = mesh_sequence_number_next();
603b16fda24SMatthias Ringwald     }
6043945bdb6SMatthias Ringwald     uint16_t seq_zero = message_pdu->seq & 0x01fff;
605a7f44e8dSMatthias Ringwald     uint8_t  seg_n    = (message_pdu->len - 1) / max_segment_len;
606b30b4f6eSMatthias Ringwald     uint8_t  szmic    = ((message_pdu->flags & MESH_TRANSPORT_FLAG_TRANSMIC_64) != 0) ? 1 : 0;
6073945bdb6SMatthias Ringwald     uint8_t  nid      = message_pdu->ivi_nid & 0x7f;
6083945bdb6SMatthias Ringwald     uint8_t  ttl      = message_pdu->ctl_ttl & 0x7f;
6093945bdb6SMatthias Ringwald     uint16_t src      = message_pdu->src;
6103945bdb6SMatthias Ringwald     uint16_t dest     = message_pdu->dst;
61177ba3d3fSMatthias Ringwald 
612b30b4f6eSMatthias Ringwald     // only 1 for access messages with 64 bit TransMIC
613b30b4f6eSMatthias Ringwald     btstack_assert((szmic == 0) || !ctl);
614b30b4f6eSMatthias Ringwald 
61577ba3d3fSMatthias Ringwald     // current segment.
61677ba3d3fSMatthias Ringwald     uint16_t seg_offset = seg_o * max_segment_len;
61777ba3d3fSMatthias Ringwald 
61877ba3d3fSMatthias Ringwald     uint8_t lower_transport_pdu_data[16];
619a7f44e8dSMatthias Ringwald     lower_transport_pdu_data[0] = 0x80 | message_pdu->akf_aid_control;
62077ba3d3fSMatthias Ringwald     big_endian_store_24(lower_transport_pdu_data, 1, (szmic << 23) | (seq_zero << 10) | (seg_o << 5) | seg_n);
621a7f44e8dSMatthias Ringwald     uint16_t segment_len = btstack_min(message_pdu->len - seg_offset, max_segment_len);
622a7f44e8dSMatthias Ringwald 
62377ba3d3fSMatthias Ringwald     uint16_t lower_transport_pdu_len = 4 + segment_len;
62477ba3d3fSMatthias Ringwald 
625a7f44e8dSMatthias Ringwald     // find network-pdu with chunk for seg_offset
626a7f44e8dSMatthias Ringwald     mesh_network_pdu_t * chunk = (mesh_network_pdu_t *) lower_transport_outgoing_message->segments;
627a7f44e8dSMatthias Ringwald     uint16_t chunk_start = 0;
628a7f44e8dSMatthias Ringwald     while ((chunk_start + MESH_NETWORK_PAYLOAD_MAX) <= seg_offset){
629a7f44e8dSMatthias Ringwald         chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next;
630a7f44e8dSMatthias Ringwald         chunk_start += MESH_NETWORK_PAYLOAD_MAX;
631a7f44e8dSMatthias Ringwald     }
632a7f44e8dSMatthias Ringwald     // first part
633a7f44e8dSMatthias Ringwald     uint16_t chunk_offset = seg_offset - chunk_start;
634a7f44e8dSMatthias Ringwald     uint16_t bytes_to_copy = btstack_min(MESH_NETWORK_PAYLOAD_MAX - chunk_offset, segment_len);
635a7f44e8dSMatthias Ringwald     (void)memcpy(&lower_transport_pdu_data[4],
636a7f44e8dSMatthias Ringwald                  &chunk->data[chunk_offset], bytes_to_copy);
637a7f44e8dSMatthias Ringwald     segment_len -= bytes_to_copy;
638a7f44e8dSMatthias Ringwald     // second part
639a7f44e8dSMatthias Ringwald     if (segment_len > 0){
640a7f44e8dSMatthias Ringwald         chunk = (mesh_network_pdu_t *) chunk->pdu_header.item.next;
641a7f44e8dSMatthias Ringwald         (void)memcpy(&lower_transport_pdu_data[4+bytes_to_copy],
642a7f44e8dSMatthias Ringwald                      &chunk->data[0], segment_len);
643a7f44e8dSMatthias Ringwald     }
644a7f44e8dSMatthias Ringwald 
645a7f44e8dSMatthias 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);
64677ba3d3fSMatthias Ringwald }
64777ba3d3fSMatthias Ringwald 
mesh_lower_transport_outgoing_send_next_segment(void)64827e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_send_next_segment(void){
64979eb95b0SMatthias Ringwald     btstack_assert(lower_transport_outgoing_message != NULL);
65077ba3d3fSMatthias Ringwald 
6511b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
652cb7b3e6fSMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06" PRIx32 ": send next segment\n", lower_transport_outgoing_message,
6533945bdb6SMatthias Ringwald            lower_transport_outgoing_message->seq);
6541b639008SMatthias Ringwald #endif
6551b639008SMatthias Ringwald 
6563945bdb6SMatthias Ringwald     int ctl = lower_transport_outgoing_message->ctl_ttl >> 7;
65777ba3d3fSMatthias Ringwald     uint16_t max_segment_len = ctl ? 8 : 12;    // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
658a7f44e8dSMatthias Ringwald     uint8_t  seg_n = (lower_transport_outgoing_message->len - 1) / max_segment_len;
65977ba3d3fSMatthias Ringwald 
660764c8f0dSMatthias Ringwald     // find next unacknowledged segment
661a7f44e8dSMatthias Ringwald     while ((lower_transport_outgoing_seg_o <= seg_n) && ((lower_transport_outgoing_message->block_ack & (1 << lower_transport_outgoing_seg_o)) == 0)){
66277ba3d3fSMatthias Ringwald         lower_transport_outgoing_seg_o++;
66377ba3d3fSMatthias Ringwald     }
66477ba3d3fSMatthias Ringwald 
66577ba3d3fSMatthias Ringwald     if (lower_transport_outgoing_seg_o > seg_n){
666b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
667cb7b3e6fSMatthias Ringwald         printf("[+] Lower Transport, segmented pdu %p, seq %06" PRIx32 ": send complete (dst %x)\n", lower_transport_outgoing_message,
6683945bdb6SMatthias Ringwald                lower_transport_outgoing_message->seq,
6693945bdb6SMatthias Ringwald                lower_transport_outgoing_message->dst);
670b66a7a84SMatthias Ringwald #endif
67177ba3d3fSMatthias Ringwald         lower_transport_outgoing_seg_o   = 0;
67277ba3d3fSMatthias Ringwald 
67377ba3d3fSMatthias Ringwald         // done for unicast, ack timer already set, too
674b4977f35SMatthias Ringwald         if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)) {
67569061c1cSMatthias Ringwald             btstack_linked_list_add(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) lower_transport_outgoing_message);
67669061c1cSMatthias Ringwald             lower_transport_outgoing_message = NULL;
677b4977f35SMatthias Ringwald             return;
678b4977f35SMatthias Ringwald         }
67977ba3d3fSMatthias Ringwald 
680b4977f35SMatthias Ringwald         // done for group/virtual, no more retries?
681764c8f0dSMatthias Ringwald         if (lower_transport_outgoing_message->retry_count == 0){
682b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
68377ba3d3fSMatthias Ringwald             printf("[+] Lower Transport, message unacknowledged -> free\n");
684b66a7a84SMatthias Ringwald #endif
68569061c1cSMatthias Ringwald             // notify upper transport
68612835760SMatthias Ringwald             mesh_lower_transport_outgoing_complete(lower_transport_outgoing_message, MESH_TRANSPORT_STATUS_SUCCESS);
68777ba3d3fSMatthias Ringwald             return;
68877ba3d3fSMatthias Ringwald         }
68977ba3d3fSMatthias Ringwald 
690f874399cSMatthias Ringwald         // re-queue mssage
691b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
692764c8f0dSMatthias Ringwald         printf("[+] Lower Transport, message unacknowledged retry count %u\n", lower_transport_outgoing_message->retry_count);
693b66a7a84SMatthias Ringwald #endif
694764c8f0dSMatthias Ringwald         lower_transport_outgoing_message->retry_count--;
695f874399cSMatthias Ringwald         btstack_linked_list_add(&lower_transport_outgoing_ready, (btstack_linked_item_t *) lower_transport_outgoing_message);
696f874399cSMatthias Ringwald         lower_transport_outgoing_message = NULL;
697f874399cSMatthias Ringwald         mesh_lower_transport_run();
698f874399cSMatthias Ringwald         return;
69977ba3d3fSMatthias Ringwald     }
70077ba3d3fSMatthias Ringwald 
701eb3826d8SMatthias Ringwald     // restart segment transmission timer for unicast dst
7023945bdb6SMatthias Ringwald     if (mesh_network_address_unicast(lower_transport_outgoing_message->dst)){
70327e8a9aaSMatthias Ringwald         mesh_lower_transport_outgoing_restart_segment_transmission_timer(lower_transport_outgoing_message);
70477ba3d3fSMatthias Ringwald     }
70577ba3d3fSMatthias Ringwald 
70627e8a9aaSMatthias Ringwald     mesh_lower_transport_outgoing_setup_segment(lower_transport_outgoing_message, lower_transport_outgoing_seg_o,
70777ba3d3fSMatthias Ringwald                                                 lower_transport_outgoing_segment);
70877ba3d3fSMatthias Ringwald 
709b66a7a84SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
710cb7b3e6fSMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06" PRIx32 ": send seg_o %x, seg_n %x\n", lower_transport_outgoing_message,
7113945bdb6SMatthias Ringwald            lower_transport_outgoing_message->seq, lower_transport_outgoing_seg_o, seg_n);
712b0d2aa0bSMatthias Ringwald     mesh_print_hex("LowerTransportPDU", &lower_transport_outgoing_segment->data[9], lower_transport_outgoing_segment->len-9);
713b66a7a84SMatthias Ringwald #endif
71477ba3d3fSMatthias Ringwald 
71577ba3d3fSMatthias Ringwald     // next segment
71677ba3d3fSMatthias Ringwald     lower_transport_outgoing_seg_o++;
71777ba3d3fSMatthias Ringwald 
71877ba3d3fSMatthias Ringwald     // send network pdu
719e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = true;
72077ba3d3fSMatthias Ringwald     mesh_network_send_pdu(lower_transport_outgoing_segment);
72177ba3d3fSMatthias Ringwald }
72277ba3d3fSMatthias Ringwald 
mesh_lower_transport_outgoing_setup_sending_segmented_pdus(mesh_segmented_pdu_t * segmented_pdu)7236d212510SMatthias Ringwald static void mesh_lower_transport_outgoing_setup_sending_segmented_pdus(mesh_segmented_pdu_t *segmented_pdu) {
724cb7b3e6fSMatthias Ringwald     printf("[+] Lower Transport, segmented pdu %p, seq %06" PRIx32 ": send retry count %u\n", segmented_pdu, segmented_pdu->seq, segmented_pdu->retry_count);
725a4331339SMatthias Ringwald 
7266d212510SMatthias Ringwald     segmented_pdu->retry_count--;
727b489fec0SMatthias Ringwald     lower_transport_outgoing_seg_o   = 0;
728a4331339SMatthias Ringwald     lower_transport_outgoing_transmission_timeout  = false;
729a4331339SMatthias Ringwald     lower_transport_outgoing_transmission_complete = false;
73012835760SMatthias Ringwald     lower_transport_outgoing_transmission_aborted  = false;
731a4331339SMatthias Ringwald 
732a4331339SMatthias Ringwald     lower_transport_outgoing_message = segmented_pdu;
733b489fec0SMatthias Ringwald }
734b489fec0SMatthias Ringwald 
mesh_lower_transport_outgoing_segment_transmission_fired(mesh_segmented_pdu_t * segmented_pdu)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){
738cb7b3e6fSMatthias Ringwald         printf("[!] Lower transport, segmented pdu %p, seq %06" PRIx32 ": send failed, retries exhausted\n", segmented_pdu,
73969061c1cSMatthias Ringwald                segmented_pdu->seq);
74012835760SMatthias Ringwald         mesh_lower_transport_outgoing_complete(segmented_pdu, MESH_TRANSPORT_STATUS_SEND_FAILED);
741b489fec0SMatthias Ringwald         return;
742b489fec0SMatthias Ringwald     }
743b489fec0SMatthias Ringwald 
7441b639008SMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
745cb7b3e6fSMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06" PRIx32": transmission fired\n", segmented_pdu, segmented_pdu->seq);
7461b639008SMatthias Ringwald #endif
7471b639008SMatthias Ringwald 
748a4331339SMatthias Ringwald     // re-queue message for sending remaining segments
74969061c1cSMatthias Ringwald     if (lower_transport_outgoing_message == segmented_pdu){
75069061c1cSMatthias Ringwald         lower_transport_outgoing_message = NULL;
75169061c1cSMatthias Ringwald     } else {
752a4331339SMatthias Ringwald         btstack_linked_list_remove(&lower_transport_outgoing_waiting, (btstack_linked_item_t *) segmented_pdu);
75369061c1cSMatthias Ringwald     }
754a4331339SMatthias Ringwald     btstack_linked_list_add_tail(&lower_transport_outgoing_ready, (btstack_linked_item_t *) segmented_pdu);
7556dac9d64SMatthias Ringwald 
7566dac9d64SMatthias Ringwald     // continue
7576dac9d64SMatthias Ringwald     mesh_lower_transport_run();
7581b639008SMatthias Ringwald }
759b489fec0SMatthias Ringwald 
mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts)76027e8a9aaSMatthias Ringwald static void mesh_lower_transport_outgoing_segment_transmission_timeout(btstack_timer_source_t * ts){
76127e8a9aaSMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_run_loop_get_timer_context(ts);
76227e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
763cb7b3e6fSMatthias Ringwald     printf("[+] Lower transport, segmented pdu %p, seq %06" PRIx32 ": transmission timer fired\n", segmented_pdu,
76427e8a9aaSMatthias Ringwald            segmented_pdu->seq);
76527e8a9aaSMatthias Ringwald #endif
766354884c7SMatthias Ringwald     segmented_pdu->flags &= ~MESH_TRANSPORT_FLAG_ACK_TIMER;
76727e8a9aaSMatthias Ringwald 
76869061c1cSMatthias Ringwald     if ((segmented_pdu == lower_transport_outgoing_message) && lower_transport_outgoing_segment_at_network_layer){
76927e8a9aaSMatthias Ringwald         lower_transport_outgoing_transmission_timeout = true;
77027e8a9aaSMatthias Ringwald     } else {
7716d212510SMatthias Ringwald         mesh_lower_transport_outgoing_segment_transmission_fired(segmented_pdu);
77227e8a9aaSMatthias Ringwald     }
77327e8a9aaSMatthias Ringwald }
77427e8a9aaSMatthias Ringwald 
77527e8a9aaSMatthias Ringwald // GENERAL //
77627e8a9aaSMatthias Ringwald 
mesh_lower_transport_network_pdu_sent(mesh_network_pdu_t * network_pdu)77777ba3d3fSMatthias Ringwald static void mesh_lower_transport_network_pdu_sent(mesh_network_pdu_t *network_pdu){
77877ba3d3fSMatthias Ringwald     // figure out what pdu was sent
77977ba3d3fSMatthias Ringwald 
780c0005391SMatthias Ringwald     // Segment Acknowledgment message sent by us?
781c0005391SMatthias Ringwald     if (network_pdu->pdu_header.pdu_type == MESH_PDU_TYPE_SEGMENT_ACKNOWLEDGMENT){
782c0005391SMatthias Ringwald         btstack_memory_mesh_network_pdu_free(network_pdu);
783c0005391SMatthias Ringwald         return;
784c0005391SMatthias Ringwald     }
785c0005391SMatthias Ringwald 
7861e6c9225SMatthias Ringwald     // single segment?
78777ba3d3fSMatthias Ringwald     if (lower_transport_outgoing_segment == network_pdu){
788a7f44e8dSMatthias Ringwald         btstack_assert(lower_transport_outgoing_message != NULL);
789b02ff6bdSMatthias Ringwald 
7901e6c9225SMatthias Ringwald         // of segmented message
791b02ff6bdSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
792cb7b3e6fSMatthias Ringwald         printf("[+] Lower transport, segmented pdu %p, seq %06" PRIx32 ": network pdu %p sent\n", lower_transport_outgoing_message,
7933945bdb6SMatthias Ringwald                lower_transport_outgoing_message->seq, network_pdu);
794b02ff6bdSMatthias Ringwald #endif
795b02ff6bdSMatthias Ringwald 
796e3f8b4e7SMatthias Ringwald         lower_transport_outgoing_segment_at_network_layer = false;
797764c8f0dSMatthias Ringwald         if (lower_transport_outgoing_transmission_complete){
79812835760SMatthias Ringwald             // handle success
79979eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_complete = false;
80079eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_timeout  = false;
80112835760SMatthias Ringwald             mesh_lower_transport_outgoing_complete(lower_transport_outgoing_message, MESH_TRANSPORT_STATUS_SUCCESS);
80212835760SMatthias Ringwald             return;
80312835760SMatthias Ringwald         }
80412835760SMatthias Ringwald         if (lower_transport_outgoing_transmission_aborted){
80512835760SMatthias Ringwald             // handle abort
80612835760SMatthias Ringwald             lower_transport_outgoing_transmission_complete = false;
80712835760SMatthias Ringwald             lower_transport_outgoing_transmission_timeout  = false;
80812835760SMatthias Ringwald             mesh_lower_transport_outgoing_complete(lower_transport_outgoing_message, MESH_TRANSPORT_STATUS_SEND_ABORT_BY_REMOTE);
8091b639008SMatthias Ringwald             return;
8101b639008SMatthias Ringwald         }
811b489fec0SMatthias Ringwald         if (lower_transport_outgoing_transmission_timeout){
812b489fec0SMatthias Ringwald             // handle timeout
81379eb95b0SMatthias Ringwald             lower_transport_outgoing_transmission_timeout = false;
8146d212510SMatthias Ringwald             mesh_lower_transport_outgoing_segment_transmission_fired(lower_transport_outgoing_message);
8151b639008SMatthias Ringwald             return;
816b489fec0SMatthias Ringwald         }
817b02ff6bdSMatthias Ringwald 
818b489fec0SMatthias Ringwald         // send next segment
81927e8a9aaSMatthias Ringwald         mesh_lower_transport_outgoing_send_next_segment();
82077ba3d3fSMatthias Ringwald         return;
82177ba3d3fSMatthias Ringwald     }
82277ba3d3fSMatthias Ringwald 
82377ba3d3fSMatthias Ringwald     // other
82477ba3d3fSMatthias Ringwald     higher_layer_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SUCCESS, (mesh_pdu_t *) network_pdu);
82577ba3d3fSMatthias Ringwald }
82677ba3d3fSMatthias Ringwald 
mesh_lower_transport_process_unsegmented_control_message(mesh_network_pdu_t * network_pdu)82727e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_unsegmented_control_message(mesh_network_pdu_t *network_pdu){
82827e8a9aaSMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
82927e8a9aaSMatthias Ringwald     uint8_t  opcode = lower_transport_pdu[0];
83027e8a9aaSMatthias Ringwald 
83127e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
83227e8a9aaSMatthias Ringwald     printf("Unsegmented Control message, outgoing message %p, opcode %x\n", lower_transport_outgoing_message, opcode);
83327e8a9aaSMatthias Ringwald #endif
83427e8a9aaSMatthias Ringwald 
83527e8a9aaSMatthias Ringwald     switch (opcode){
83627e8a9aaSMatthias Ringwald         case 0:
83727e8a9aaSMatthias Ringwald             mesh_lower_transport_outgoing_process_segment_acknowledgement_message(network_pdu);
83827e8a9aaSMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
83927e8a9aaSMatthias Ringwald             break;
84027e8a9aaSMatthias Ringwald         default:
84127e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu);
84227e8a9aaSMatthias Ringwald             break;
84327e8a9aaSMatthias Ringwald     }
84427e8a9aaSMatthias Ringwald }
84527e8a9aaSMatthias Ringwald 
mesh_lower_transport_process_network_pdu(mesh_network_pdu_t * network_pdu)84627e8a9aaSMatthias Ringwald static void mesh_lower_transport_process_network_pdu(mesh_network_pdu_t *network_pdu) {// segmented?
84727e8a9aaSMatthias Ringwald     if (mesh_network_segmented(network_pdu)){
84827e8a9aaSMatthias Ringwald         mesh_segmented_pdu_t * message_pdu = mesh_lower_transport_incoming_pdu_for_segmented_message(network_pdu);
84927e8a9aaSMatthias Ringwald         if (message_pdu) {
85027e8a9aaSMatthias Ringwald             // start acknowledgment timer if inactive
851354884c7SMatthias Ringwald             if ((message_pdu->flags & MESH_TRANSPORT_FLAG_ACK_TIMER) == 0){
85227e8a9aaSMatthias Ringwald                 // - "The acknowledgment timer shall be set to a minimum of 150 + 50 * TTL milliseconds"
85327e8a9aaSMatthias Ringwald                 uint32_t timeout = 150 + 50 * mesh_network_ttl(network_pdu);
85427e8a9aaSMatthias Ringwald                 mesh_lower_transport_incoming_start_acknowledgment_timer(message_pdu, timeout);
85527e8a9aaSMatthias Ringwald             }
85627e8a9aaSMatthias Ringwald             // restart incomplete timer
85727e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_restart_incomplete_timer(message_pdu, 10000,
85827e8a9aaSMatthias Ringwald                                                                    &mesh_lower_transport_incoming_incomplete_timeout);
85927e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_process_segment(message_pdu, network_pdu);
860166cce4dSMatthias Ringwald         } else {
86127e8a9aaSMatthias Ringwald             mesh_network_message_processed_by_higher_layer(network_pdu);
86227e8a9aaSMatthias Ringwald         }
86327e8a9aaSMatthias Ringwald     } else {
86427e8a9aaSMatthias Ringwald         // control?
86527e8a9aaSMatthias Ringwald         if (mesh_network_control(network_pdu)){
86627e8a9aaSMatthias Ringwald             // unsegmented control message (not encrypted)
86727e8a9aaSMatthias Ringwald             mesh_lower_transport_process_unsegmented_control_message(network_pdu);
86827e8a9aaSMatthias Ringwald         } else {
86927e8a9aaSMatthias Ringwald             // unsegmented access message (encrypted)
87027e8a9aaSMatthias Ringwald             mesh_lower_transport_incoming_queue_for_higher_layer((mesh_pdu_t *) network_pdu);
87127e8a9aaSMatthias Ringwald         }
87227e8a9aaSMatthias Ringwald     }
87327e8a9aaSMatthias Ringwald }
87427e8a9aaSMatthias Ringwald 
mesh_lower_transport_received_message(mesh_network_callback_type_t callback_type,mesh_network_pdu_t * network_pdu)87527e8a9aaSMatthias Ringwald void mesh_lower_transport_received_message(mesh_network_callback_type_t callback_type, mesh_network_pdu_t *network_pdu){
87627e8a9aaSMatthias Ringwald     mesh_peer_t * peer;
87727e8a9aaSMatthias Ringwald     uint16_t src;
87827e8a9aaSMatthias Ringwald     uint16_t seq;
87927e8a9aaSMatthias Ringwald     switch (callback_type){
88027e8a9aaSMatthias Ringwald         case MESH_NETWORK_PDU_RECEIVED:
88127e8a9aaSMatthias Ringwald             src = mesh_network_src(network_pdu);
88227e8a9aaSMatthias Ringwald             seq = mesh_network_seq(network_pdu);
88327e8a9aaSMatthias Ringwald             peer = mesh_peer_for_addr(src);
88427e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
88527e8a9aaSMatthias Ringwald             printf("Transport: received message. SRC %x, SEQ %x\n", src, seq);
88627e8a9aaSMatthias Ringwald #endif
88727e8a9aaSMatthias Ringwald             // validate seq
88827e8a9aaSMatthias Ringwald             if (peer && seq > peer->seq){
88927e8a9aaSMatthias Ringwald                 // track seq
89027e8a9aaSMatthias Ringwald                 peer->seq = seq;
89127e8a9aaSMatthias Ringwald                 // process
89227e8a9aaSMatthias Ringwald                 mesh_lower_transport_process_network_pdu(network_pdu);
89327e8a9aaSMatthias Ringwald                 mesh_lower_transport_run();
89427e8a9aaSMatthias Ringwald             } else {
89527e8a9aaSMatthias Ringwald                 // drop packet
89627e8a9aaSMatthias Ringwald #ifdef LOG_LOWER_TRANSPORT
89727e8a9aaSMatthias Ringwald                 printf("Transport: drop packet - src/seq auth failed\n");
89827e8a9aaSMatthias Ringwald #endif
89927e8a9aaSMatthias Ringwald                 mesh_network_message_processed_by_higher_layer(network_pdu);
90027e8a9aaSMatthias Ringwald             }
90127e8a9aaSMatthias Ringwald             break;
90227e8a9aaSMatthias Ringwald         case MESH_NETWORK_PDU_SENT:
90327e8a9aaSMatthias Ringwald             mesh_lower_transport_network_pdu_sent(network_pdu);
90427e8a9aaSMatthias Ringwald             break;
90527e8a9aaSMatthias Ringwald         default:
90627e8a9aaSMatthias Ringwald             break;
907166cce4dSMatthias Ringwald     }
908166cce4dSMatthias Ringwald }
909166cce4dSMatthias Ringwald 
mesh_lower_transport_run(void)91077ba3d3fSMatthias Ringwald static void mesh_lower_transport_run(void){
91177ba3d3fSMatthias Ringwald 
91277ba3d3fSMatthias Ringwald     // check if outgoing segmented pdu is active
9131e6c9225SMatthias Ringwald     if (lower_transport_outgoing_message) return;
91477ba3d3fSMatthias Ringwald 
91579eb95b0SMatthias Ringwald     while(!btstack_linked_list_empty(&lower_transport_outgoing_ready)) {
91677ba3d3fSMatthias Ringwald         // get next message
917a4bbc09dSMatthias Ringwald         mesh_segmented_pdu_t   * message_pdu;
91879eb95b0SMatthias Ringwald         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_ready);
91977ba3d3fSMatthias Ringwald         switch (pdu->pdu_type) {
920e9c16304SMatthias Ringwald             case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS:
921e9c16304SMatthias Ringwald             case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL:
92269061c1cSMatthias Ringwald                 printf("[+] Lower transport, unsegmented pdu, sending now %p\n", pdu);
923e9c16304SMatthias Ringwald                 lower_transport_outgoing_network_pdu = (mesh_network_pdu_t *) pdu;
924e9c16304SMatthias Ringwald                 mesh_network_send_pdu(lower_transport_outgoing_network_pdu);
925b837963cSMatthias Ringwald                 break;
926a4bbc09dSMatthias Ringwald             case MESH_PDU_TYPE_SEGMENTED:
927a4bbc09dSMatthias Ringwald                 message_pdu = (mesh_segmented_pdu_t *) pdu;
928a7f44e8dSMatthias Ringwald                 //
929cb7b3e6fSMatthias Ringwald                 printf("[+] Lower transport, segmented pdu %p, seq %06" PRIx32 ": run start sending now\n", message_pdu,
9303945bdb6SMatthias Ringwald                        message_pdu->seq);
93177ba3d3fSMatthias Ringwald                 // start sending segmented pdu
9326d212510SMatthias Ringwald                 mesh_lower_transport_outgoing_setup_sending_segmented_pdus(message_pdu);
93327e8a9aaSMatthias 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 
mesh_lower_transport_send_pdu(mesh_pdu_t * pdu)942a4331339SMatthias Ringwald void mesh_lower_transport_send_pdu(mesh_pdu_t *pdu){
943a4331339SMatthias Ringwald     mesh_segmented_pdu_t * segmented_pdu;
944a4331339SMatthias Ringwald     switch (pdu->pdu_type){
945a4331339SMatthias Ringwald         case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS:
946a4331339SMatthias Ringwald         case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL:
94705002aecSMatthias Ringwald             btstack_assert(((mesh_network_pdu_t *) pdu)->len >= 9);
948a4331339SMatthias Ringwald             break;
949a4331339SMatthias Ringwald         case MESH_PDU_TYPE_SEGMENTED:
950a4331339SMatthias Ringwald             // set num retries, set of segments to send
951a4331339SMatthias Ringwald             segmented_pdu = (mesh_segmented_pdu_t *) pdu;
952a4331339SMatthias Ringwald             segmented_pdu->retry_count = 3;
953a4331339SMatthias Ringwald             mesh_lower_transport_outgoing_setup_block_ack(segmented_pdu);
954a4331339SMatthias Ringwald             break;
955a4331339SMatthias Ringwald         default:
956a4331339SMatthias Ringwald             btstack_assert(false);
957a4331339SMatthias Ringwald             break;
958a4331339SMatthias Ringwald     }
959a4331339SMatthias Ringwald     btstack_linked_list_add_tail(&lower_transport_outgoing_ready, (btstack_linked_item_t*) pdu);
960a4331339SMatthias Ringwald     mesh_lower_transport_run();
961a4331339SMatthias Ringwald }
962a4331339SMatthias Ringwald 
mesh_lower_transport_dump_network_pdus(const char * name,btstack_linked_list_t * list)963df71a9a4SMatthias Ringwald void mesh_lower_transport_dump_network_pdus(const char *name, btstack_linked_list_t *list){
96477ba3d3fSMatthias Ringwald     printf("List: %s:\n", name);
96577ba3d3fSMatthias Ringwald     btstack_linked_list_iterator_t it;
96677ba3d3fSMatthias Ringwald     btstack_linked_list_iterator_init(&it, list);
96777ba3d3fSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
96877ba3d3fSMatthias Ringwald         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t*) btstack_linked_list_iterator_next(&it);
96977ba3d3fSMatthias Ringwald         printf("- %p: ", network_pdu); printf_hexdump(network_pdu->data, network_pdu->len);
97077ba3d3fSMatthias Ringwald     }
97177ba3d3fSMatthias Ringwald }
972df71a9a4SMatthias Ringwald 
mesh_lower_transport_reset_network_pdus(btstack_linked_list_t * list)973df71a9a4SMatthias Ringwald void mesh_lower_transport_reset_network_pdus(btstack_linked_list_t *list){
97477ba3d3fSMatthias Ringwald     while (!btstack_linked_list_empty(list)){
97577ba3d3fSMatthias Ringwald         mesh_network_pdu_t * pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(list);
97677ba3d3fSMatthias Ringwald         btstack_memory_mesh_network_pdu_free(pdu);
97777ba3d3fSMatthias Ringwald     }
97877ba3d3fSMatthias Ringwald }
97977ba3d3fSMatthias Ringwald 
mesh_lower_transport_can_send_to_dest(uint16_t dest)98033439c2eSMilanka Ringwald bool mesh_lower_transport_can_send_to_dest(uint16_t dest){
98133439c2eSMilanka Ringwald     UNUSED(dest);
982a4331339SMatthias Ringwald     // check current
983a4331339SMatthias Ringwald     uint16_t num_messages = 0;
984a4331339SMatthias Ringwald     if (lower_transport_outgoing_message != NULL) {
985a4331339SMatthias Ringwald         if (lower_transport_outgoing_message->dst == dest) {
986a4331339SMatthias Ringwald             return false;
987a4331339SMatthias Ringwald         }
988a4331339SMatthias Ringwald         num_messages++;
989a4331339SMatthias Ringwald     }
990a4331339SMatthias Ringwald     // check waiting
991a4331339SMatthias Ringwald     btstack_linked_list_iterator_t it;
992a4331339SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &lower_transport_outgoing_waiting);
993a4331339SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
994a4331339SMatthias Ringwald         mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_linked_list_iterator_next(&it);
995a4331339SMatthias Ringwald         num_messages++;
996cd15e003SMatthias Ringwald         if (segmented_pdu->dst == dest){
997cd15e003SMatthias Ringwald             return false;
998a4331339SMatthias Ringwald         }
999cd15e003SMatthias Ringwald     }
1000cd15e003SMatthias Ringwald #ifdef MAX_NR_MESH_OUTGOING_SEGMENTED_MESSAGES
1001cd15e003SMatthias Ringwald     // limit number of parallel outgoing messages if configured
1002cd15e003SMatthias Ringwald     if (num_messages >= MAX_NR_MESH_OUTGOING_SEGMENTED_MESSAGES) return false;
1003*212d7460SMatthias Ringwald #else
1004*212d7460SMatthias Ringwald     UNUSED(num_messages);
1005cd15e003SMatthias Ringwald #endif
1006a4331339SMatthias Ringwald     return true;
1007cd16791dSMatthias Ringwald }
1008cd16791dSMatthias Ringwald 
mesh_lower_transport_reserve_slot(void)1009cd16791dSMatthias Ringwald void mesh_lower_transport_reserve_slot(void){
1010cd16791dSMatthias Ringwald }
1011cd16791dSMatthias Ringwald 
mesh_lower_transport_reset(void)101277ba3d3fSMatthias Ringwald void mesh_lower_transport_reset(void){
1013a7f44e8dSMatthias Ringwald     if (lower_transport_outgoing_message){
1014a7f44e8dSMatthias Ringwald         while (!btstack_linked_list_empty(&lower_transport_outgoing_message->segments)){
1015a7f44e8dSMatthias Ringwald             mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_message->segments);
1016a7f44e8dSMatthias Ringwald             mesh_network_pdu_free(network_pdu);
1017a7f44e8dSMatthias Ringwald         }
1018a7f44e8dSMatthias Ringwald         lower_transport_outgoing_message = NULL;
1019a7f44e8dSMatthias Ringwald     }
102084e4acf2SMatthias Ringwald     while (!btstack_linked_list_empty(&lower_transport_outgoing_waiting)){
102184e4acf2SMatthias Ringwald         mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) btstack_linked_list_pop(&lower_transport_outgoing_waiting);
102284e4acf2SMatthias Ringwald         btstack_memory_mesh_segmented_pdu_free(segmented_pdu);
102384e4acf2SMatthias Ringwald     }
10241e6cebb8SMatthias Ringwald     mesh_network_pdu_free(lower_transport_outgoing_segment);
1025e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = false;
10261e6cebb8SMatthias Ringwald     lower_transport_outgoing_segment = NULL;
102777ba3d3fSMatthias Ringwald }
102877ba3d3fSMatthias Ringwald 
mesh_lower_transport_init()102977ba3d3fSMatthias Ringwald void mesh_lower_transport_init(){
103077ba3d3fSMatthias Ringwald     // register with network layer
103177ba3d3fSMatthias Ringwald     mesh_network_set_higher_layer_handler(&mesh_lower_transport_received_message);
103277ba3d3fSMatthias Ringwald     // allocate network_pdu for segmentation
1033e3f8b4e7SMatthias Ringwald     lower_transport_outgoing_segment_at_network_layer = false;
103477ba3d3fSMatthias Ringwald     lower_transport_outgoing_segment = mesh_network_pdu_get();
103577ba3d3fSMatthias Ringwald }
103677ba3d3fSMatthias Ringwald 
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))103777ba3d3fSMatthias 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)){
103877ba3d3fSMatthias Ringwald     higher_layer_handler = pdu_handler;
103977ba3d3fSMatthias Ringwald }
1040