From fec314ce4b4e8862702d655ec09d31f74a8eb372 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 7 Nov 2018 15:40:15 +0100 Subject: [PATCH] mesh: implement mesh_transport_run() that processes incoming network pdus --- test/mesh/mesh.c | 59 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/test/mesh/mesh.c b/test/mesh/mesh.c index c59dc82de..3e0b88a6e 100644 --- a/test/mesh/mesh.c +++ b/test/mesh/mesh.c @@ -312,8 +312,14 @@ static const uint8_t * mesh_application_key_iterator_get_next(mesh_application_k // stub lower transport + +static void mesh_lower_transport_run(void); + static uint8_t application_nonce[13]; static btstack_crypto_ccm_t ccm; +static btstack_linked_list_t lower_transport_incoming; +static int mesh_transport_crypto_active; +static mesh_network_pdu_t * network_pdu_in_validation; static void transport_setup_application_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){ nonce[0] = 0x01; @@ -341,8 +347,8 @@ static void transport_received_message_b(void * arg){ uint8_t net_mic_len = ctl ? 8 : 4; - uint8_t * transport_pdu = &network_pdu->data[9]; - uint8_t transport_pdu_len = network_pdu->len - 9 - net_mic_len; + uint8_t * transport_pdu = &network_pdu_in_validation->data[9]; + uint8_t transport_pdu_len = network_pdu_in_validation->len - 9 - net_mic_len; printf("Decryted Transport network_pdu: "); printf_hexdump(&network_pdu->data[9], transport_pdu_len - trans_mic_len); @@ -353,21 +359,28 @@ static void transport_received_message_b(void * arg){ } printf("\n"); + // pass to upper layer + btstack_memory_mesh_network_pdu_free(network_pdu); + // done - mesh_network_message_processed_by_higher_layer(network_pdu); + mesh_transport_crypto_active = 0; + mesh_network_message_processed_by_higher_layer(network_pdu_in_validation); + mesh_lower_transport_run(); } static void transport_received_message(mesh_network_pdu_t * network_pdu){ - uint8_t ctl_ttl = network_pdu->data[1]; + uint8_t ctl_ttl = network_pdu_in_validation->data[1]; uint8_t ctl = ctl_ttl >> 7; uint8_t net_mic_len = ctl ? 8 : 4; + network_pdu->len = network_pdu_in_validation->len; + // - uint8_t * lower_transport_pdu = &network_pdu->data[9]; - uint8_t lower_transport_pdu_len = network_pdu->len - 9 - net_mic_len; + uint8_t * lower_transport_pdu = &network_pdu_in_validation->data[9]; + uint8_t lower_transport_pdu_len = network_pdu_in_validation->len - 9 - net_mic_len; printf("Lower Transport network_pdu: "); - printf_hexdump(&network_pdu->data[9], lower_transport_pdu_len); + printf_hexdump(&network_pdu_in_validation->data[9], lower_transport_pdu_len); // check type if (ctl){ @@ -385,8 +398,9 @@ static void transport_received_message(mesh_network_pdu_t * network_pdu){ // TODO: transmic hard coded to 4, could be 8 uint8_t trans_mic_len = 4; - uint8_t * upper_transport_pdu = &lower_transport_pdu[1]; - uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; + uint8_t * upper_transport_pdu_orig = &network_pdu_in_validation->data[10]; + uint8_t * upper_transport_pdu_dest = &network_pdu->data[10]; + uint8_t upper_transport_pdu_len = lower_transport_pdu_len - 1 - trans_mic_len; // application key nonce @@ -394,11 +408,13 @@ static void transport_received_message(mesh_network_pdu_t * network_pdu){ // TODO: lookup device or applicaton key + mesh_transport_crypto_active = 1; + const uint8_t * application_key = mesh_application_key_list_get(aid); - transport_setup_application_nonce(application_nonce, network_pdu); + transport_setup_application_nonce(application_nonce, network_pdu_in_validation); btstack_crypo_ccm_init(&ccm, application_key, application_nonce, upper_transport_pdu_len, 4); - btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu, upper_transport_pdu, &transport_received_message_b, network_pdu); + btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_orig, upper_transport_pdu_dest, &transport_received_message_b, network_pdu); return; } @@ -408,6 +424,25 @@ static void transport_received_message(mesh_network_pdu_t * network_pdu){ mesh_network_message_processed_by_higher_layer(network_pdu); } +static void mesh_lower_transport_run(void){ + if (mesh_transport_crypto_active) return; + + if (!btstack_linked_list_empty(&lower_transport_incoming)){ + mesh_network_pdu_t * decode_pdu = btstack_memory_mesh_network_pdu_get(); + if (!decode_pdu) return; + // get encoded network pdu and start processing + network_pdu_in_validation = (mesh_network_pdu_t *) btstack_linked_list_pop(&lower_transport_incoming); + printf("mesh_lower_transport_run: process %p\n", network_pdu_in_validation); + transport_received_message(decode_pdu); + return; + } +} + +static void mesh_transport_received_mesage(mesh_network_pdu_t * network_pdu){ + // add to list and go + btstack_linked_list_add_tail(&lower_transport_incoming, (btstack_linked_item_t *) network_pdu); + mesh_lower_transport_run(); +} // #define TEST_MESSAGE_1 // #define TEST_MESSAGE_24 @@ -673,7 +708,7 @@ int btstack_main(void) // Network layer mesh_network_init(); - mesh_network_set_higher_layer_handler(&transport_received_message); + mesh_network_set_higher_layer_handler(&mesh_transport_received_mesage); // PTS app key uint8_t application_key[16];