mesh: use mesh_unsegmnted_incoming_pdu_t for unsegmented control messages

This commit is contained in:
Matthias Ringwald 2020-03-14 21:10:41 +01:00
parent cae44b3ba8
commit 3d9c3b8e63
3 changed files with 44 additions and 52 deletions

View File

@ -236,19 +236,12 @@ static void mesh_lower_transport_deliver_to_higher_layer(void){
if (mesh_lower_transport_higher_layer_pdu == NULL && !btstack_linked_list_empty(&mesh_lower_transport_queued_for_higher_layer)){
mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&mesh_lower_transport_queued_for_higher_layer);
mesh_network_pdu_t * network_pdu;
switch (pdu->pdu_type){
case MESH_MSG_TYPE_NETWORK_PDU:
network_pdu = (mesh_network_pdu_t *) pdu;
if (mesh_network_control(network_pdu) != 0){
// unsegmented control pdu
mesh_lower_transport_higher_layer_pdu = pdu;
} else {
// unsegmented access pdu
mesh_lower_transport_higher_layer_pdu = (mesh_pdu_t *) &lower_transport_access_incoming_singleton;
lower_transport_access_incoming_singleton.pdu_header.pdu_type = MESH_PDU_TYPE_UNSEGMENTED_INCOMING;
btstack_linked_list_add(&lower_transport_access_incoming_singleton.segments, (btstack_linked_item_t*) network_pdu);
}
// unsegmented pdu
mesh_lower_transport_higher_layer_pdu = (mesh_pdu_t *) &lower_transport_access_incoming_singleton;
lower_transport_access_incoming_singleton.pdu_header.pdu_type = MESH_PDU_TYPE_UNSEGMENTED_INCOMING;
btstack_linked_list_add(&lower_transport_access_incoming_singleton.segments, (btstack_linked_item_t*) pdu);
break;
default:
// segmented control or access pdu
@ -641,10 +634,8 @@ static void mesh_lower_transport_process_network_pdu(mesh_network_pdu_t *network
void mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
btstack_assert(pdu == mesh_lower_transport_higher_layer_pdu);
mesh_lower_transport_higher_layer_pdu = NULL;
mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu;
switch (pdu->pdu_type){
case MESH_PDU_TYPE_NETWORK:
mesh_network_message_processed_by_higher_layer((mesh_network_pdu_t *) pdu);
break;
case MESH_PDU_TYPE_TRANSPORT:
mesh_transport_pdu_free((mesh_transport_pdu_t *) pdu);
break;
@ -654,9 +645,10 @@ void mesh_lower_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
mesh_message_pdu_free((mesh_message_pdu_t *) pdu);
break;
case MESH_PDU_TYPE_UNSEGMENTED_INCOMING:
mesh_network_message_processed_by_higher_layer((mesh_network_pdu_t *) btstack_linked_list_pop(&lower_transport_access_incoming_singleton.segments));
mesh_network_message_processed_by_higher_layer((mesh_network_pdu_t *) btstack_linked_list_pop(&unsegmented_incoming_pdu->segments));
break;
default:
btstack_assert(0);
break;
}
mesh_lower_transport_deliver_to_higher_layer();

View File

@ -257,15 +257,16 @@ static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_t
mesh_print_hex("DeviceNonce", nonce, 13);
}
static void mesh_upper_unsegmented_control_message_received(mesh_network_pdu_t * network_pdu){
uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu);
uint8_t opcode = lower_transport_pdu[0];
static void mesh_upper_unsegmented_control_message_received(mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu){
if (mesh_control_message_handler){
mesh_control_message_handler((mesh_pdu_t*) network_pdu);
mesh_control_message_handler((mesh_pdu_t*) unsegmented_incoming_pdu);
} else {
mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&unsegmented_incoming_pdu->segments);
uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu);
uint8_t opcode = lower_transport_pdu[0];
printf("[!] Unhandled Control message with opcode %02x\n", opcode);
// done
mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t*) unsegmented_incoming_pdu);
}
}
@ -285,18 +286,21 @@ static void mesh_upper_transport_process_message_done(mesh_message_pdu_t *messag
mesh_upper_transport_run();
}
static void mesh_upper_transport_process_unsegmented_message_done(mesh_network_pdu_t *network_pdu){
static void mesh_upper_transport_process_unsegmented_message_done(mesh_pdu_t * pdu){
btstack_assert(pdu != NULL);
btstack_assert(pdu->pdu_type == MESH_PDU_TYPE_UNSEGMENTED_INCOMING);
mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu;
btstack_assert(unsegmented_incoming_pdu == incoming_unsegmented_pdu_raw);
crypto_active = 0;
if (mesh_network_control(network_pdu)) {
mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
} else {
btstack_assert(network_pdu != NULL);
incoming_unsegmented_pdu_raw = NULL;
mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&unsegmented_incoming_pdu->segments);
if (!mesh_network_control(network_pdu)) {
mesh_network_pdu_free(network_pdu);
mesh_pdu_t * pdu = (mesh_pdu_t *) incoming_unsegmented_pdu_raw;
incoming_unsegmented_pdu_raw = NULL;
incoming_unsegmented_pdu_raw = NULL;
mesh_lower_transport_message_processed_by_higher_layer(pdu);
}
mesh_lower_transport_message_processed_by_higher_layer(pdu);
mesh_upper_transport_run();
}
@ -346,7 +350,7 @@ static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){
} else {
printf("[!] Unhandled Unsegmented Access message\n");
// done
mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw);
}
printf("\n");
@ -358,7 +362,7 @@ static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){
} else {
printf("TransMIC does not match device key, done\n");
// done
mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw);
}
}
}
@ -436,7 +440,7 @@ static void mesh_upper_transport_validate_unsegmented_message(void){
if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){
printf("No valid transport key found\n");
mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
mesh_upper_transport_process_unsegmented_message_done((mesh_pdu_t*) incoming_unsegmented_pdu_raw);
return;
}
mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it);
@ -999,32 +1003,21 @@ static void mesh_upper_transport_run(void){
mesh_message_pdu_t * message_pdu;
mesh_unsegmented_incoming_pdu_t * unsegmented_pdu;
switch (pdu->pdu_type){
case MESH_PDU_TYPE_NETWORK:
network_pdu = (mesh_network_pdu_t *) pdu;
// control?
if (mesh_network_control(network_pdu)) {
(void) btstack_linked_list_pop(&upper_transport_incoming);
mesh_upper_unsegmented_control_message_received(network_pdu);
} else {
btstack_assert(0);
break;
}
break;
case MESH_PDU_TYPE_UNSEGMENTED_INCOMING:
unsegmented_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu;
network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&unsegmented_pdu->segments);
btstack_assert(network_pdu != NULL);
incoming_unsegmented_pdu_raw = unsegmented_pdu;
// control?
if (mesh_network_control(network_pdu)) {
// TODO: handle unsegmented control message
btstack_assert(0);
(void) btstack_linked_list_pop(&upper_transport_incoming);
mesh_upper_unsegmented_control_message_received(unsegmented_pdu);
break;
} else {
incoming_network_pdu_decoded = mesh_network_pdu_get();
if (!incoming_network_pdu_decoded) return;
(void) btstack_linked_list_pop(&upper_transport_incoming);
incoming_unsegmented_pdu_raw = unsegmented_pdu;
// use pre-allocated message pdu
incoming_message_pdu_singleton.pdu_header.pdu_type = MESH_PDU_TYPE_MESSAGE;
@ -1081,6 +1074,7 @@ static void mesh_upper_transport_run(void){
}
break;
default:
btstack_assert(0);
break;
}
}
@ -1150,16 +1144,17 @@ void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){
void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
crypto_active = 0;
switch (pdu->pdu_type){
case MESH_PDU_TYPE_NETWORK:
mesh_upper_transport_process_unsegmented_message_done((mesh_network_pdu_t *) pdu);
break;
case MESH_PDU_TYPE_TRANSPORT:
mesh_upper_transport_process_segmented_message_done((mesh_transport_pdu_t *) pdu);
break;
case MESH_PDU_TYPE_MESSAGE:
mesh_upper_transport_process_message_done((mesh_message_pdu_t *) pdu);
break;
case MESH_PDU_TYPE_UNSEGMENTED_INCOMING:
mesh_upper_transport_process_unsegmented_message_done(pdu);
break;
default:
btstack_assert(0);
break;
}
}

View File

@ -227,7 +227,7 @@ static void load_provisioning_data_test_message(void){
static void test_lower_transport_callback_handler(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu){
switch (callback_type){
case MESH_NETWORK_PDU_RECEIVED:
printf("test MESH_NETWORK_PDU_RECEIVED\n");
printf("test MESH_NETWORK_PDU_RECEIVED\n");
received_network_pdu = network_pdu;
break;
case MESH_NETWORK_PDU_SENT:
@ -289,6 +289,7 @@ static void test_upper_transport_access_message_handler(mesh_pdu_t * pdu){
static void test_upper_transport_control_message_handler(mesh_pdu_t * pdu){
mesh_transport_pdu_t * transport_pdu;
mesh_network_pdu_t * network_pdu;
mesh_unsegmented_incoming_pdu_t * unsegmented_incoming_pdu;
switch(pdu->pdu_type){
case MESH_PDU_TYPE_TRANSPORT:
transport_pdu = (mesh_transport_pdu_t *) pdu;
@ -298,8 +299,12 @@ static void test_upper_transport_control_message_handler(mesh_pdu_t * pdu){
mesh_upper_transport_message_processed_by_higher_layer(pdu);
break;
case MESH_PDU_TYPE_NETWORK:
network_pdu = (mesh_network_pdu_t *) pdu;
printf("test MESH_CONTROL_NETWORK_PDU_RECEIVED\n");
btstack_assert(0);
break;
case MESH_PDU_TYPE_UNSEGMENTED_INCOMING:
unsegmented_incoming_pdu = (mesh_unsegmented_incoming_pdu_t *) pdu;
network_pdu = unsegmented_incoming_pdu->segment;
printf("test MESH_PDU_TYPE_UNSEGMENTED_INCOMING\n");
recv_upper_transport_pdu_len = mesh_network_pdu_len(network_pdu);
memcpy(recv_upper_transport_pdu_data, mesh_network_pdu_data(network_pdu), recv_upper_transport_pdu_len);
mesh_upper_transport_message_processed_by_higher_layer(pdu);