diff --git a/src/ble/mesh/mesh_network.c b/src/ble/mesh/mesh_network.c index f7e073516..514d85e1b 100644 --- a/src/ble/mesh/mesh_network.c +++ b/src/ble/mesh/mesh_network.c @@ -64,6 +64,7 @@ typedef struct { static uint32_t global_iv_index; static uint16_t mesh_network_primary_address; static uint16_t mesh_network_num_elements; +static void (*mesh_network_higher_layer_handler)(mesh_network_pdu_t * network_pdu); // shared send/receive crypto static int mesh_crypto_active; @@ -112,7 +113,6 @@ static int mesh_network_cache_index; static void mesh_network_run(void); static void process_network_pdu_validate(mesh_network_pdu_t * network_pdu); -static void mesh_network_message_processed_by_upper_layer(mesh_network_pdu_t * network_pdu); // network caching static uint32_t mesh_network_cache_hash(mesh_network_pdu_t * network_pdu){ @@ -192,21 +192,7 @@ static const mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_networ return &mesh_network_primary_key; } -// stub lower transport -static void transport_received_message(mesh_network_pdu_t * network_pdu){ - uint8_t ctl_ttl = network_pdu->data[1]; - uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4; - - // - printf("Lower Transport network_pdu: "); - printf_hexdump(&network_pdu->data[9], network_pdu->len - 9 - net_mic_len); - - // done - mesh_network_message_processed_by_upper_layer(network_pdu); -} - // common helper - int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst){ printf("CTL: %u\n", ctl); printf("SRC: %04x\n", src); @@ -371,7 +357,7 @@ static void process_network_pdu_validate_d(void * arg){ mesh_network_cache_add(hash); // forward to lower transport layer. message is freed by call to mesh_network_message_processed_by_upper_layer - transport_received_message(network_pdu); + (*mesh_network_higher_layer_handler)(network_pdu); } else { @@ -386,7 +372,7 @@ static void process_network_pdu_validate_d(void * arg){ } } -static void mesh_network_message_processed_by_upper_layer(mesh_network_pdu_t * network_pdu){ +void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu){ #ifdef ENABLE_MESH_RELAY uint8_t ctl_ttl = network_pdu->data[1]; uint8_t ctl = ctl_ttl >> 7; @@ -559,6 +545,11 @@ void mesh_network_init(void){ adv_bearer_register_for_mesh_message(&mesh_message_handler); } +void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_pdu_t * network_pdu)){ + mesh_network_higher_layer_handler = packet_handler; +} + + void mesh_network_set_primary_element_address(uint16_t addr){ mesh_network_primary_address = addr; mesh_network_num_elements = 1; diff --git a/src/ble/mesh/mesh_network.h b/src/ble/mesh/mesh_network.h index 96a9c9d40..b96d0aa67 100644 --- a/src/ble/mesh/mesh_network.h +++ b/src/ble/mesh/mesh_network.h @@ -81,6 +81,18 @@ typedef struct { */ void mesh_network_init(void); +/** + * @brief Set higher layer Network PDU handler + * @param packet_handler + */ +void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_pdu_t * network_pdu)); + +/** + * @brief Mark packet as processed + * @param newtork_pdu received via call packet_handler + */ +void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); + /** * @brief Configure address filter */ diff --git a/test/mesh/mesh.c b/test/mesh/mesh.c index d3151b869..54ee675cb 100644 --- a/test/mesh/mesh.c +++ b/test/mesh/mesh.c @@ -398,6 +398,19 @@ static void mesh_secure_network_beacon_auth_value_calculated(void * arg){ adv_bearer_send_mesh_beacon(mesh_secure_network_beacon, sizeof(mesh_secure_network_beacon)); } +// stub lower transport +static void transport_received_message(mesh_network_pdu_t * network_pdu){ + uint8_t ctl_ttl = network_pdu->data[1]; + uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4; + + // + printf("Lower Transport network_pdu: "); + printf_hexdump(&network_pdu->data[9], network_pdu->len - 9 - net_mic_len); + + // done + mesh_network_message_processed_by_higher_layer(network_pdu); +} + static int pts_type; static void stdin_process(char cmd){ @@ -508,6 +521,7 @@ int btstack_main(void) // Network layer mesh_network_init(); + mesh_network_set_higher_layer_handler(&transport_received_message); // btstack_parse_hex(pts_device_uuid_string, 16, pts_device_uuid);