xref: /btstack/src/mesh/mesh_upper_transport.c (revision ecb8f2500c6c9d40e7faf718424bc435e83d3ede)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "mesh_upper_transport.c"
39 
40 #include "mesh/mesh_upper_transport.h"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack_util.h"
47 #include "btstack_memory.h"
48 #include "btstack_debug.h"
49 
50 #include "mesh/beacon.h"
51 #include "mesh/mesh_iv_index_seq_number.h"
52 #include "mesh/mesh_keys.h"
53 #include "mesh/mesh_lower_transport.h"
54 #include "mesh/mesh_peer.h"
55 #include "mesh/mesh_virtual_addresses.h"
56 
57 static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
58 
59 static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){
60     printf("%-20s ", name);
61     printf_hexdump(data, len);
62 }
63 // static void mesh_print_x(const char * name, uint32_t value){
64 //     printf("%20s: 0x%x", name, (int) value);
65 // }
66 
67 
68 // combined key x address iterator for upper transport decryption
69 
70 typedef struct {
71     // state
72     mesh_transport_key_iterator_t  key_it;
73     mesh_virtual_address_iterator_t address_it;
74     // elements
75     const mesh_transport_key_t *   key;
76     const mesh_virtual_address_t * address;
77     // address - might be virtual
78     uint16_t dst;
79     // key info
80 } mesh_transport_key_and_virtual_address_iterator_t;
81 
82 static void mesh_transport_key_and_virtual_address_iterator_init(mesh_transport_key_and_virtual_address_iterator_t *it,
83                                                                  uint16_t dst, uint16_t netkey_index, uint8_t akf,
84                                                                  uint8_t aid) {
85     printf("KEY_INIT: dst %04x, akf %x, aid %x\n", dst, akf, aid);
86     // config
87     it->dst   = dst;
88     // init elements
89     it->key     = NULL;
90     it->address = NULL;
91     // init element iterators
92     mesh_transport_key_aid_iterator_init(&it->key_it, netkey_index, akf, aid);
93     // init address iterator
94     if (mesh_network_address_virtual(it->dst)){
95         mesh_virtual_address_iterator_init(&it->address_it, dst);
96         // get first key
97         if (mesh_transport_key_aid_iterator_has_more(&it->key_it)) {
98             it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
99         }
100     }
101 }
102 
103 // cartesian product: keys x addressses
104 static int mesh_transport_key_and_virtual_address_iterator_has_more(mesh_transport_key_and_virtual_address_iterator_t * it){
105     if (mesh_network_address_virtual(it->dst)) {
106         // find next valid entry
107         while (1){
108             if (mesh_virtual_address_iterator_has_more(&it->address_it)) return 1;
109             if (!mesh_transport_key_aid_iterator_has_more(&it->key_it)) return 0;
110             // get next key
111             it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
112             mesh_virtual_address_iterator_init(&it->address_it, it->dst);
113         }
114     } else {
115         return mesh_transport_key_aid_iterator_has_more(&it->key_it);
116     }
117 }
118 
119 static void mesh_transport_key_and_virtual_address_iterator_next(mesh_transport_key_and_virtual_address_iterator_t * it){
120     if (mesh_network_address_virtual(it->dst)) {
121         it->address = mesh_virtual_address_iterator_get_next(&it->address_it);
122     } else {
123         it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
124     }
125 }
126 
127 // UPPER TRANSPORT
128 
129 // stub lower transport
130 
131 static void mesh_upper_transport_validate_unsegmented_message(void);
132 static void mesh_upper_transport_validate_segmented_message(void);
133 
134 static void mesh_upper_transport_run(void);
135 
136 static int crypto_active;
137 static mesh_network_pdu_t   * incoming_network_pdu_raw;
138 static mesh_network_pdu_t   * incoming_network_pdu_decoded;
139 static mesh_transport_pdu_t * incoming_transport_pdu_raw;
140 static mesh_transport_pdu_t * incoming_transport_pdu_decoded;
141 static uint8_t application_nonce[13];
142 static btstack_crypto_ccm_t ccm;
143 static mesh_transport_key_and_virtual_address_iterator_t mesh_transport_key_it;
144 
145 // upper transport callbacks - in access layer
146 static void (*mesh_access_message_handler)(mesh_pdu_t * pdu);
147 static void (*mesh_control_message_handler)(mesh_pdu_t * pdu);
148 
149 // incoming unsegmented (network) and segmented (transport) control and access messages
150 static btstack_linked_list_t upper_transport_incoming;
151 
152 // outgoing unsegmented (network) and segmented (uppert_transport_outgoing) control and access messages
153 static btstack_linked_list_t upper_transport_outgoing;
154 
155 static void mesh_upper_transport_dump_pdus(const char *name, btstack_linked_list_t *list){
156     printf("List: %s:\n", name);
157     btstack_linked_list_iterator_t it;
158     btstack_linked_list_iterator_init(&it, list);
159     while (btstack_linked_list_iterator_has_next(&it)){
160         mesh_pdu_t * pdu = (mesh_pdu_t*) btstack_linked_list_iterator_next(&it);
161         printf("- %p\n", pdu);
162         // printf_hexdump( mesh_pdu_data(pdu), mesh_pdu_len(pdu));
163     }
164 }
165 
166 static void mesh_upper_transport_reset_pdus(btstack_linked_list_t *list){
167     while (!btstack_linked_list_empty(list)){
168         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(list);
169         switch (pdu->pdu_type){
170             case MESH_PDU_TYPE_NETWORK:
171                 btstack_memory_mesh_network_pdu_free((mesh_network_pdu_t *) pdu);
172                 break;
173             case MESH_PDU_TYPE_TRANSPORT:
174                 btstack_memory_mesh_transport_pdu_free((mesh_transport_pdu_t *) pdu);
175                 break;
176             default:
177                 break;
178         }
179     }
180 }
181 
182 void mesh_upper_transport_dump(void){
183     printf("incoming_network_pdu_raw: %p\n", incoming_network_pdu_raw);
184     printf("incoming_network_pdu_decoded: %p\n", incoming_network_pdu_decoded);
185     mesh_upper_transport_dump_pdus("upper_transport_incoming", &upper_transport_incoming);
186 }
187 
188 void mesh_upper_transport_reset(void){
189     crypto_active = 0;
190     if (incoming_network_pdu_raw){
191         mesh_network_pdu_free(incoming_network_pdu_raw);
192         incoming_network_pdu_raw = NULL;
193     }
194     // if (incoming_network_pdu_decoded){
195     //     mesh_network_pdu_free(incoming_network_pdu_decoded);
196     //     incoming_network_pdu_decoded = NULL;
197     // }
198     mesh_upper_transport_reset_pdus(&upper_transport_incoming);
199 }
200 
201 static void mesh_upper_unsegmented_control_message_received(mesh_network_pdu_t * network_pdu){
202     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
203     uint8_t  opcode = lower_transport_pdu[0];
204     if (mesh_control_message_handler){
205         mesh_control_message_handler((mesh_pdu_t*) network_pdu);
206     } else {
207         printf("[!] Unhandled Control message with opcode %02x\n", opcode);
208         // done
209         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
210     }
211 }
212 
213 static void mesh_upper_transport_process_unsegmented_message_done(mesh_network_pdu_t *network_pdu){
214     crypto_active = 0;
215     if (mesh_network_control(network_pdu)) {
216         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
217     } else {
218         mesh_network_pdu_free(network_pdu);
219         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) incoming_network_pdu_raw);
220         incoming_network_pdu_raw = NULL;
221     }
222     mesh_upper_transport_run();
223 }
224 
225 static void mesh_upper_transport_process_segmented_message_done(mesh_transport_pdu_t *transport_pdu){
226     crypto_active = 0;
227     if (mesh_transport_ctl(transport_pdu)) {
228         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)transport_pdu);
229     } else {
230         mesh_transport_pdu_free(transport_pdu);
231         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)incoming_transport_pdu_raw);
232         incoming_transport_pdu_raw = NULL;
233     }
234     mesh_upper_transport_run();
235 }
236 
237 static uint32_t iv_index_for_ivi_nid(uint8_t ivi_nid){
238     // get IV Index and IVI
239     uint32_t iv_index = mesh_get_iv_index();
240     int ivi = ivi_nid >> 7;
241 
242     // if least significant bit differs, use previous IV Index
243     if ((iv_index & 1 ) ^ ivi){
244         iv_index--;
245     }
246     return iv_index;
247 }
248 
249 static void transport_unsegmented_setup_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
250     nonce[1] = 0x00;    // SZMIC if a Segmented Access message or 0 for all other message formats
251     memcpy(&nonce[2], &network_pdu->data[2], 7);
252     big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(network_pdu->data[0]));
253 }
254 
255 static void transport_segmented_setup_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
256     nonce[1] = transport_pdu->transmic_len == 8 ? 0x80 : 0x00;
257     memcpy(&nonce[2], &transport_pdu->network_header[2], 7);
258     big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(transport_pdu->network_header[0]));
259 }
260 
261 static void transport_unsegmented_setup_application_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
262     nonce[0] = 0x01;
263     transport_unsegmented_setup_nonce(nonce, network_pdu);
264     mesh_print_hex("AppNonce", nonce, 13);
265 }
266 
267 static void transport_unsegmented_setup_device_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
268     nonce[0] = 0x02;
269     transport_unsegmented_setup_nonce(nonce, network_pdu);
270     mesh_print_hex("DeviceNonce", nonce, 13);
271 }
272 
273 static void transport_segmented_setup_application_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
274     nonce[0] = 0x01;
275     transport_segmented_setup_nonce(nonce, transport_pdu);
276     mesh_print_hex("AppNonce", nonce, 13);
277 }
278 
279 static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
280     nonce[0] = 0x02;
281     transport_segmented_setup_nonce(nonce, transport_pdu);
282     mesh_print_hex("DeviceNonce", nonce, 13);
283 }
284 
285 static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){
286     UNUSED(arg);
287 
288     uint8_t * lower_transport_pdu = mesh_network_pdu_data(incoming_network_pdu_decoded);
289     uint8_t trans_mic_len = 4;
290 
291     // store TransMIC
292     uint8_t trans_mic[8];
293     btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic);
294     mesh_print_hex("TransMIC", trans_mic, trans_mic_len);
295 
296     uint8_t * upper_transport_pdu     = mesh_network_pdu_data(incoming_network_pdu_decoded) + 1;
297     uint8_t   upper_transport_pdu_len = mesh_network_pdu_len(incoming_network_pdu_decoded)  - 1;
298 
299     mesh_print_hex("Decryted PDU", upper_transport_pdu, upper_transport_pdu_len - trans_mic_len);
300 
301     if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len - trans_mic_len], trans_mic_len) == 0){
302         printf("TransMIC matches\n");
303 
304         // remove TransMIC from payload
305         incoming_network_pdu_decoded->len -= trans_mic_len;
306 
307         // if virtual address, update dst to pseudo_dst
308         if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){
309             big_endian_store_16(incoming_network_pdu_decoded->data, 7, mesh_transport_key_it.address->pseudo_dst);
310         }
311 
312         // pass to upper layer
313         if (mesh_access_message_handler){
314             mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_network_pdu_decoded;
315             incoming_network_pdu_decoded = NULL;
316             mesh_access_message_handler(pdu);
317         } else {
318             printf("[!] Unhandled Unsegmented Access message\n");
319             // done
320             mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
321         }
322 
323         printf("\n");
324     } else {
325         uint8_t afk = lower_transport_pdu[0] & 0x40;
326         if (afk){
327             printf("TransMIC does not match, try next key\n");
328             mesh_upper_transport_validate_unsegmented_message();
329         } else {
330             printf("TransMIC does not match device key, done\n");
331             // done
332             mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
333         }
334     }
335 }
336 
337 static void mesh_upper_transport_validate_segmented_message_ccm(void * arg){
338     UNUSED(arg);
339 
340     uint8_t * upper_transport_pdu     =  incoming_transport_pdu_decoded->data;
341     uint8_t   upper_transport_pdu_len =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
342 
343     mesh_print_hex("Decrypted PDU", upper_transport_pdu, upper_transport_pdu_len);
344 
345     // store TransMIC
346     uint8_t trans_mic[8];
347     btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic);
348     mesh_print_hex("TransMIC", trans_mic, incoming_transport_pdu_decoded->transmic_len);
349 
350     if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len], incoming_transport_pdu_decoded->transmic_len) == 0){
351         printf("TransMIC matches\n");
352 
353         // remove TransMIC from payload
354         incoming_transport_pdu_decoded->len -= incoming_transport_pdu_decoded->transmic_len;
355 
356         // if virtual address, update dst to pseudo_dst
357         if (mesh_network_address_virtual(mesh_transport_dst(incoming_transport_pdu_decoded))){
358             big_endian_store_16(incoming_transport_pdu_decoded->network_header, 7, mesh_transport_key_it.address->pseudo_dst);
359         }
360 
361         // pass to upper layer
362         if (mesh_access_message_handler){
363             mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_transport_pdu_decoded;
364             incoming_network_pdu_decoded = NULL;
365             mesh_access_message_handler(pdu);
366         } else {
367             printf("[!] Unhandled Segmented Access/Control message\n");
368             // done
369             mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
370         }
371 
372         printf("\n");
373 
374     } else {
375         uint8_t akf = incoming_transport_pdu_decoded->akf_aid_control & 0x40;
376         if (akf){
377             printf("TransMIC does not match, try next key\n");
378             mesh_upper_transport_validate_segmented_message();
379         } else {
380             printf("TransMIC does not match device key, done\n");
381             // done
382             mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
383         }
384     }
385 }
386 
387 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
388     crypto_active = 0;
389     switch (pdu->pdu_type){
390         case MESH_PDU_TYPE_NETWORK:
391             mesh_upper_transport_process_unsegmented_message_done((mesh_network_pdu_t *) pdu);
392             break;
393         case MESH_PDU_TYPE_TRANSPORT:
394             mesh_upper_transport_process_segmented_message_done((mesh_transport_pdu_t *) pdu);
395             break;
396         default:
397             break;
398     }
399 }
400 
401 static void mesh_upper_transport_validate_segmented_message_digest(void * arg){
402     UNUSED(arg);
403     uint8_t   upper_transport_pdu_len      = incoming_transport_pdu_raw->len - incoming_transport_pdu_raw->transmic_len;
404     uint8_t * upper_transport_pdu_data_in  = incoming_transport_pdu_raw->data;
405     uint8_t * upper_transport_pdu_data_out = incoming_transport_pdu_decoded->data;
406     btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_segmented_message_ccm, NULL);
407 }
408 
409 static void mesh_upper_transport_validate_unsegmented_message_digest(void * arg){
410     UNUSED(arg);
411     uint8_t   trans_mic_len = 4;
412     uint8_t   lower_transport_pdu_len      = incoming_network_pdu_raw->len - 9;
413     uint8_t * upper_transport_pdu_data_in  = &incoming_network_pdu_raw->data[10];
414     uint8_t * upper_transport_pdu_data_out = &incoming_network_pdu_decoded->data[10];
415     uint8_t   upper_transport_pdu_len      = lower_transport_pdu_len - 1 - trans_mic_len;
416     btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_unsegmented_message_ccm, NULL);
417 }
418 
419 static void mesh_upper_transport_validate_unsegmented_message(void){
420 
421     if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){
422         printf("No valid transport key found\n");
423         mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
424         return;
425     }
426     mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it);
427     const mesh_transport_key_t * message_key = mesh_transport_key_it.key;
428 
429     if (message_key->akf){
430         transport_unsegmented_setup_application_nonce(application_nonce, incoming_network_pdu_raw);
431     } else {
432         transport_unsegmented_setup_device_nonce(application_nonce, incoming_network_pdu_raw);
433     }
434 
435     // store application / device key index
436     mesh_print_hex("AppOrDevKey", message_key->key, 16);
437     incoming_network_pdu_decoded->appkey_index = message_key->appkey_index;
438 
439     // unsegmented message have TransMIC of 32 bit
440     uint8_t trans_mic_len = 4;
441     printf("Unsegmented Access message with TransMIC len 4\n");
442 
443     uint8_t   lower_transport_pdu_len = incoming_network_pdu_raw->len - 9;
444     uint8_t * upper_transport_pdu_data = &incoming_network_pdu_raw->data[10];
445     uint8_t   upper_transport_pdu_len  = lower_transport_pdu_len - 1 - trans_mic_len;
446 
447     mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len);
448 
449     // decrypt ccm
450     crypto_active = 1;
451     uint16_t aad_len  = 0;
452     if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){
453         aad_len  = 16;
454     }
455     btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, trans_mic_len);
456     if (aad_len){
457         btstack_crypto_ccm_digest(&ccm, (uint8_t*) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_unsegmented_message_digest, NULL);
458     } else {
459         mesh_upper_transport_validate_unsegmented_message_digest(incoming_network_pdu_decoded);
460     }
461 }
462 
463 static void mesh_upper_transport_validate_segmented_message(void){
464     uint8_t * upper_transport_pdu_data =  incoming_transport_pdu_decoded->data;
465     uint8_t   upper_transport_pdu_len  =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
466 
467     if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){
468         printf("No valid transport key found\n");
469         mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
470         return;
471     }
472     mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it);
473     const mesh_transport_key_t * message_key = mesh_transport_key_it.key;
474 
475     if (message_key->akf){
476         transport_segmented_setup_application_nonce(application_nonce, incoming_transport_pdu_raw);
477     } else {
478         transport_segmented_setup_device_nonce(application_nonce, incoming_transport_pdu_raw);
479     }
480 
481     // store application / device key index
482     mesh_print_hex("AppOrDevKey", message_key->key, 16);
483     incoming_transport_pdu_decoded->appkey_index = message_key->appkey_index;
484 
485     mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len);
486 
487     // decrypt ccm
488     crypto_active = 1;
489     uint16_t aad_len  = 0;
490     if (mesh_network_address_virtual(mesh_transport_dst(incoming_transport_pdu_decoded))){
491         aad_len  = 16;
492     }
493     btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, incoming_transport_pdu_decoded->transmic_len);
494 
495     if (aad_len){
496         btstack_crypto_ccm_digest(&ccm, (uint8_t *) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_segmented_message_digest, NULL);
497     } else {
498         mesh_upper_transport_validate_segmented_message_digest(NULL);
499     }
500 }
501 
502 static void mesh_upper_transport_process_unsegmented_access_message(void){
503     // copy original pdu
504     incoming_network_pdu_decoded->len = incoming_network_pdu_raw->len;
505     memcpy(incoming_network_pdu_decoded->data, incoming_network_pdu_raw->data, incoming_network_pdu_decoded->len);
506 
507     //
508     uint8_t * lower_transport_pdu     = &incoming_network_pdu_raw->data[9];
509     uint8_t   lower_transport_pdu_len = incoming_network_pdu_raw->len - 9;
510 
511     mesh_print_hex("Lower Transport network pdu", &incoming_network_pdu_raw->data[9], lower_transport_pdu_len);
512 
513     uint8_t aid =  lower_transport_pdu[0] & 0x3f;
514     uint8_t akf = (lower_transport_pdu[0] & 0x40) >> 6;
515     printf("AKF: %u\n",   akf);
516     printf("AID: %02x\n", aid);
517 
518     mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_network_dst(incoming_network_pdu_decoded),
519             incoming_network_pdu_decoded->netkey_index, akf, aid);
520     mesh_upper_transport_validate_unsegmented_message();
521 }
522 
523 static void mesh_upper_transport_process_message(void){
524     // copy original pdu
525     memcpy(incoming_transport_pdu_decoded, incoming_transport_pdu_raw, sizeof(mesh_transport_pdu_t));
526 
527     //
528     uint8_t * upper_transport_pdu     =  incoming_transport_pdu_decoded->data;
529     uint8_t   upper_transport_pdu_len =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
530     mesh_print_hex("Upper Transport pdu", upper_transport_pdu, upper_transport_pdu_len);
531 
532     uint8_t aid =  incoming_transport_pdu_decoded->akf_aid_control & 0x3f;
533     uint8_t akf = (incoming_transport_pdu_decoded->akf_aid_control & 0x40) >> 6;
534 
535     printf("AKF: %u\n",   akf);
536     printf("AID: %02x\n", aid);
537 
538     mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_transport_dst(incoming_transport_pdu_decoded),
539             incoming_transport_pdu_decoded->netkey_index, akf, aid);
540     mesh_upper_transport_validate_segmented_message();
541 }
542 
543 static void mesh_upper_transport_message_received(mesh_pdu_t * pdu){
544     btstack_linked_list_add_tail(&upper_transport_incoming, (btstack_linked_item_t*) pdu);
545     mesh_upper_transport_run();
546 }
547 
548 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){
549     mesh_network_pdu_t * network_pdu;
550     mesh_transport_pdu_t * transport_pdu;
551     switch (pdu->pdu_type) {
552         case MESH_PDU_TYPE_NETWORK:
553             network_pdu = (mesh_network_pdu_t *) pdu;
554             mesh_network_pdu_free(network_pdu);
555             break;
556         case MESH_PDU_TYPE_TRANSPORT:
557             transport_pdu = (mesh_transport_pdu_t *) pdu;
558             mesh_transport_pdu_free(transport_pdu);
559             break;
560         default:
561             break;
562     }
563 }
564 
565 static void mesh_upper_transport_pdu_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){
566     switch (callback_type){
567         case MESH_TRANSPORT_PDU_RECEIVED:
568             mesh_upper_transport_message_received(pdu);
569             break;
570         case MESH_TRANSPORT_PDU_SENT:
571             // notify upper layer (or just free pdu)
572             if (higher_layer_handler){
573                 higher_layer_handler(callback_type, status, pdu);
574             } else {
575                 mesh_upper_transport_pdu_free(pdu);
576             }
577             break;
578         default:
579             break;
580     }
581 }
582 static void mesh_upper_transport_send_unsegmented_access_pdu_ccm(void * arg){
583     crypto_active = 0;
584 
585     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
586     uint8_t * upper_transport_pdu     = mesh_network_pdu_data(network_pdu) + 1;
587     uint8_t   upper_transport_pdu_len = mesh_network_pdu_len(network_pdu)  - 1;
588     mesh_print_hex("EncAccessPayload", upper_transport_pdu, upper_transport_pdu_len);
589     // store TransMIC
590     btstack_crypto_ccm_get_authentication_value(&ccm, &upper_transport_pdu[upper_transport_pdu_len]);
591     mesh_print_hex("TransMIC", &upper_transport_pdu[upper_transport_pdu_len], 4);
592     network_pdu->len += 4;
593     mesh_print_hex("UpperTransportPDU", upper_transport_pdu, network_pdu->len);
594     // send network pdu
595     mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu);
596 }
597 
598 static void mesh_upper_transport_send_segmented_access_pdu_ccm(void * arg){
599     crypto_active = 0;
600 
601     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
602     mesh_print_hex("EncAccessPayload", transport_pdu->data, transport_pdu->len);
603     // store TransMIC
604     btstack_crypto_ccm_get_authentication_value(&ccm, &transport_pdu->data[transport_pdu->len]);
605     mesh_print_hex("TransMIC", &transport_pdu->data[transport_pdu->len], transport_pdu->transmic_len);
606     transport_pdu->len += transport_pdu->transmic_len;
607     mesh_print_hex("UpperTransportPDU", transport_pdu->data, transport_pdu->len);
608     mesh_lower_transport_send_pdu((mesh_pdu_t*) transport_pdu);
609 }
610 
611 static uint8_t mesh_upper_transport_setup_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode,
612                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
613 
614     printf("[+] Upper transport, setup unsegmented Control PDU %p (opcode %02x): ", network_pdu, opcode);
615     printf_hexdump(control_pdu_data, control_pdu_len);
616 
617     if (control_pdu_len > 11) return 1;
618 
619     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
620     if (!network_key) return 1;
621 
622     uint8_t transport_pdu_data[12];
623     transport_pdu_data[0] = opcode;
624     memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len);
625     uint16_t transport_pdu_len = control_pdu_len + 1;
626 
627     // setup network_pdu
628     mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, 0, src, dest, transport_pdu_data, transport_pdu_len);
629 
630     return 0;
631 }
632 
633 static uint8_t mesh_upper_transport_setup_segmented_control_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode,
634                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
635 
636     printf("[+] Upper transport, setup segmented Control PDU (opcode %02x): \n", opcode);
637     printf_hexdump(control_pdu_data, control_pdu_len);
638 
639     if (control_pdu_len > 256) return 1;
640 
641     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
642     if (!network_key) return 1;
643 
644     memcpy(transport_pdu->data, control_pdu_data, control_pdu_len);
645     transport_pdu->len = control_pdu_len;
646     transport_pdu->netkey_index = netkey_index;
647     transport_pdu->akf_aid_control = opcode;
648     transport_pdu->transmic_len = 0;    // no TransMIC for control
649     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid);
650     mesh_transport_set_src(transport_pdu, src);
651     mesh_transport_set_dest(transport_pdu, dest);
652     mesh_transport_set_ctl_ttl(transport_pdu, 0x80 | ttl);
653 
654     return 0;
655 }
656 
657 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index,
658                                                uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){
659     switch (pdu->pdu_type){
660         case MESH_PDU_TYPE_NETWORK:
661             return mesh_upper_transport_setup_unsegmented_control_pdu((mesh_network_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len);
662         case MESH_PDU_TYPE_TRANSPORT:
663             return mesh_upper_transport_setup_segmented_control_pdu((mesh_transport_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len);
664         default:
665             return 1;
666     }
667 }
668 
669 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index,
670         uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest){
671 
672     // get app or device key
673     const mesh_transport_key_t * appkey;
674     appkey = mesh_transport_key_get(appkey_index);
675     if (appkey == NULL){
676         printf("appkey_index %x unknown\n", appkey_index);
677         return 1;
678     }
679     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
680 
681     // lookup network by netkey_index
682     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
683     if (!network_key) return 1;
684 
685     // Nonce for Access Payload based on Network Sequence number: needs to be fixed now and lower layers need to send packet in right order
686     uint32_t seq = mesh_sequence_number_next();
687 
688     network_pdu->data[9] = akf_aid;
689     // setup network_pdu
690     mesh_network_setup_pdu_header(network_pdu, netkey_index, network_key->nid, 0, ttl, seq, src, dest);
691     network_pdu->appkey_index = appkey_index;
692     return 0;
693 }
694 
695 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest,
696                                                        const uint8_t * access_pdu_data, uint8_t access_pdu_len){
697 
698     int status = mesh_upper_transport_setup_unsegmented_access_pdu_header(network_pdu, netkey_index, appkey_index, ttl, src, dest);
699     if (status) return status;
700 
701     printf("[+] Upper transport, setup unsegmented Access PDU - seq %06x\n", mesh_network_seq(network_pdu));
702     mesh_print_hex("Access Payload", access_pdu_data, access_pdu_len);
703 
704     // store in transport pdu
705     memcpy(&network_pdu->data[10], access_pdu_data, access_pdu_len);
706     network_pdu->len = 10 + access_pdu_len;
707     return 0;
708 }
709 
710 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index,
711     uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
712 
713     // get app or device key
714     const mesh_transport_key_t *appkey;
715     appkey = mesh_transport_key_get(appkey_index);
716     if (appkey == NULL) {
717         printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index);
718         return 1;
719     }
720     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
721 
722     // lookup network by netkey_index
723     const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index);
724     if (!network_key) return 1;
725     if (network_key == NULL) {
726         printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index);
727         return 1;
728     }
729 
730     const uint8_t trans_mic_len = szmic ? 8 : 4;
731 
732     // lower transport will call next sequence number. Only peek here to use same seq for access payload encryption as well as for first network pdu (unit test)
733     uint32_t seq = mesh_sequence_number_peek();
734 
735     // store in transport pdu
736     transport_pdu->transmic_len = trans_mic_len;
737     transport_pdu->netkey_index = netkey_index;
738     transport_pdu->appkey_index = appkey_index;
739     transport_pdu->akf_aid_control = akf_aid;
740     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7));
741     mesh_transport_set_seq(transport_pdu, seq);
742     mesh_transport_set_src(transport_pdu, src);
743     mesh_transport_set_dest(transport_pdu, dest);
744     mesh_transport_set_ctl_ttl(transport_pdu, ttl);
745     return 0;
746 }
747 
748 
749 static uint8_t mesh_upper_transport_setup_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest,
750                           uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){
751     int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
752     if (status) return status;
753 
754     // store in transport pdu
755     memcpy(transport_pdu->data, access_pdu_data, access_pdu_len);
756     transport_pdu->len = access_pdu_len;
757 
758     printf("[+] Upper transport, setup segmented Access PDU - seq %06x, szmic %u, iv_index %08x\n", mesh_transport_seq(transport_pdu), szmic, mesh_get_iv_index_for_tx());
759     mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len);
760     return 0;
761 }
762 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
763                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
764     switch (pdu->pdu_type){
765         case MESH_PDU_TYPE_NETWORK:
766             return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest);
767         case MESH_PDU_TYPE_TRANSPORT:
768             return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
769         default:
770             return 1;
771     }
772 }
773 
774 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
775                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic,
776                                               const uint8_t * access_pdu_data, uint8_t access_pdu_len){
777     switch (pdu->pdu_type){
778         case MESH_PDU_TYPE_NETWORK:
779             return mesh_upper_transport_setup_unsegmented_access_pdu((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, access_pdu_data, access_pdu_len);
780         case MESH_PDU_TYPE_TRANSPORT:
781             return mesh_upper_transport_setup_segmented_access_pdu((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic, access_pdu_data, access_pdu_len);
782         default:
783             return 1;
784     }
785 }
786 
787 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){
788     mesh_lower_transport_send_pdu((mesh_pdu_t*) pdu);
789 }
790 
791 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){
792     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
793     uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1;
794     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
795     btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len, access_pdu_data, access_pdu_data, &mesh_upper_transport_send_unsegmented_access_pdu_ccm, network_pdu);
796 }
797 
798 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){
799     // Device Key is fixed
800     if (appkey_index == MESH_DEVICE_KEY_INDEX) {
801         return mesh_transport_key_get(appkey_index);
802     }
803 
804     // Get key refresh state from subnet
805     mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index);
806     if (subnet == NULL) return NULL;
807 
808     // identify old and new app keys for given appkey_index
809     mesh_transport_key_t * old_key = NULL;
810     mesh_transport_key_t * new_key = NULL;
811     mesh_transport_key_iterator_t it;
812     mesh_transport_key_iterator_init(&it, netkey_index);
813     while (mesh_transport_key_iterator_has_more(&it)){
814         mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it);
815         if (transport_key->appkey_index != appkey_index) continue;
816         if (transport_key->old_key == 0) {
817             new_key = transport_key;
818         } else {
819             old_key = transport_key;
820         }
821     }
822 
823     // if no key is marked as old, just use the current one
824     if (old_key == NULL) return new_key;
825 
826     // use new key if it exists in phase two
827     if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){
828         return new_key;
829     } else {
830         return old_key;
831     }
832 }
833 
834 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu){
835 
836     // if dst is virtual address, lookup label uuid and hash
837     uint16_t aad_len = 0;
838     mesh_virtual_address_t * virtual_address = NULL;
839     uint16_t dst = mesh_network_dst(network_pdu);
840     if (mesh_network_address_virtual(dst)){
841         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
842         if (!virtual_address){
843             printf("No virtual address register for pseudo dst %4x\n", dst);
844             btstack_memory_mesh_network_pdu_free(network_pdu);
845             return;
846         }
847         aad_len = 16;
848         big_endian_store_16(network_pdu->data, 7, virtual_address->hash);
849     }
850 
851     // setup nonce
852     uint16_t appkey_index = network_pdu->appkey_index;
853     if (appkey_index == MESH_DEVICE_KEY_INDEX){
854         transport_unsegmented_setup_device_nonce(application_nonce, network_pdu);
855     } else {
856         transport_unsegmented_setup_application_nonce(application_nonce, network_pdu);
857     }
858 
859     // get app or device key
860     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index);
861     mesh_print_hex("AppOrDevKey", appkey->key, 16);
862 
863     // encrypt ccm
864     uint8_t   trans_mic_len = 4;
865     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
866     crypto_active = 1;
867 
868     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len);
869     if (virtual_address){
870         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
871         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, network_pdu);
872     } else {
873         mesh_upper_transport_send_unsegmented_access_pdu_digest(network_pdu);
874     }
875 }
876 
877 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){
878     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
879     uint16_t  access_pdu_len  = transport_pdu->len;
880     uint8_t * access_pdu_data = transport_pdu->data;
881     btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len,access_pdu_data, access_pdu_data, &mesh_upper_transport_send_segmented_access_pdu_ccm, transport_pdu);
882 }
883 
884 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){
885 
886     // if dst is virtual address, lookup label uuid and hash
887     uint16_t aad_len = 0;
888     mesh_virtual_address_t * virtual_address = NULL;
889     uint16_t dst = mesh_transport_dst(transport_pdu);
890     if (mesh_network_address_virtual(dst)){
891         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
892         if (!virtual_address){
893             printf("No virtual address register for pseudo dst %4x\n", dst);
894             btstack_memory_mesh_transport_pdu_free(transport_pdu);
895             return;
896         }
897         // printf("Using hash %4x with LabelUUID: ", virtual_address->hash);
898         // printf_hexdump(virtual_address->label_uuid, 16);
899         aad_len = 16;
900         big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash);
901     }
902 
903     // setup nonce - uses dst, so after pseudo address translation
904     uint16_t appkey_index = transport_pdu->appkey_index;
905     if (appkey_index == MESH_DEVICE_KEY_INDEX){
906         transport_segmented_setup_device_nonce(application_nonce, transport_pdu);
907     } else {
908         transport_segmented_setup_application_nonce(application_nonce, transport_pdu);
909     }
910 
911     // get app or device key
912     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index);
913     mesh_print_hex("AppOrDevKey", appkey->key, 16);
914 
915     // encrypt ccm
916     uint8_t   transmic_len    = transport_pdu->transmic_len;
917     uint16_t  access_pdu_len  = transport_pdu->len;
918     crypto_active = 1;
919     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len);
920     if (virtual_address){
921         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
922         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu);
923     } else {
924         mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu);
925     }
926 }
927 
928 static void mesh_upper_transport_run(void){
929     while(!btstack_linked_list_empty(&upper_transport_incoming)){
930 
931         if (crypto_active) return;
932 
933         // peek at next message
934         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming);
935         mesh_transport_pdu_t * transport_pdu;
936         mesh_network_pdu_t   * network_pdu;
937         switch (pdu->pdu_type){
938             case MESH_PDU_TYPE_NETWORK:
939                 network_pdu = (mesh_network_pdu_t *) pdu;
940                 // control?
941                 if (mesh_network_control(network_pdu)) {
942                     (void) btstack_linked_list_pop(&upper_transport_incoming);
943                     mesh_upper_unsegmented_control_message_received(network_pdu);
944                 } else {
945                     incoming_network_pdu_decoded = mesh_network_pdu_get();
946                     if (!incoming_network_pdu_decoded) return;
947                     // get encoded network pdu and start processing
948                     incoming_network_pdu_raw = network_pdu;
949                     (void) btstack_linked_list_pop(&upper_transport_incoming);
950                     mesh_upper_transport_process_unsegmented_access_message();
951                 }
952                 break;
953             case MESH_PDU_TYPE_TRANSPORT:
954                 transport_pdu = (mesh_transport_pdu_t *) pdu;
955                 uint8_t ctl = mesh_transport_ctl(transport_pdu);
956                 if (ctl){
957                     printf("Ignoring Segmented Control Message\n");
958                     (void) btstack_linked_list_pop(&upper_transport_incoming);
959                     mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) transport_pdu);
960                 } else {
961                     incoming_transport_pdu_decoded = mesh_transport_pdu_get();
962                     if (!incoming_transport_pdu_decoded) return;
963                     // get encoded transport pdu and start processing
964                     incoming_transport_pdu_raw = transport_pdu;
965                     (void) btstack_linked_list_pop(&upper_transport_incoming);
966                     mesh_upper_transport_process_message();
967                 }
968                 break;
969             default:
970                 break;
971         }
972     }
973 
974     while (!btstack_linked_list_empty(&upper_transport_outgoing)){
975 
976         if (crypto_active) return;
977 
978         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_pop(&upper_transport_outgoing);
979         switch (pdu->pdu_type){
980             case MESH_PDU_TYPE_NETWORK:
981                 mesh_upper_transport_send_unsegmented_access_pdu((mesh_network_pdu_t *) pdu);
982                 break;
983             case MESH_PDU_TYPE_TRANSPORT:
984                 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu);
985                 break;
986             default:
987                 break;
988         }
989     }
990 }
991 
992 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){
993     mesh_access_message_handler = callback;
994 }
995 
996 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){
997     mesh_control_message_handler = callback;
998 }
999 
1000 void mesh_upper_transport_set_higher_layer_handler(void (*pdu_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)){
1001     higher_layer_handler = pdu_handler;
1002 }
1003 
1004 void mesh_upper_transport_init(){
1005     mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler);
1006 }
1007 
1008 void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){
1009     if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){
1010         btstack_assert(mesh_network_pdu_len((mesh_network_pdu_t *) pdu) >= 9);
1011     }
1012 
1013     btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu);
1014     mesh_upper_transport_run();
1015 }
1016