mesh: add flags to mesh_transport, use MESH_TRANSPORT_FLAG_RESERVED for segmented access messages

This commit is contained in:
Matthias Ringwald 2019-10-10 21:51:46 +02:00
parent 59064b8059
commit b16fda24f6
3 changed files with 26 additions and 9 deletions

View File

@ -554,7 +554,14 @@ static void mesh_lower_transport_setup_segment(mesh_transport_pdu_t *transport_p
int ctl = mesh_transport_ctl(transport_pdu);
uint16_t max_segment_len = ctl ? 8 : 12; // control 8 bytes (64 bit NetMic), access 12 bytes (32 bit NetMIC)
uint32_t seq = mesh_sequence_number_next();
// 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)
uint32_t seq;
if ((transport_pdu->flags & MESH_TRANSPORT_FLAG_SEQ_RESERVED) != 0){
transport_pdu->flags &= ~(MESH_TRANSPORT_FLAG_SEQ_RESERVED);
seq = mesh_transport_seq(transport_pdu);
} else {
seq = mesh_sequence_number_next();
}
uint16_t seq_zero = mesh_transport_seq(transport_pdu) & 0x01fff;
uint8_t seg_n = (transport_pdu->len - 1) / max_segment_len;
uint8_t szmic = ((!ctl) && (transport_pdu->transmic_len == 8)) ? 1 : 0; // only 1 for access messages with 64 bit TransMIC
@ -760,7 +767,10 @@ static void mesh_lower_transport_run(void){
switch (pdu->pdu_type) {
case MESH_PDU_TYPE_NETWORK:
network_pdu = (mesh_network_pdu_t *) pdu;
mesh_network_pdu_set_seq(network_pdu, mesh_sequence_number_next());
if (mesh_network_control(network_pdu)){
// unsegmented access messages set seq before encyption
mesh_network_pdu_set_seq(network_pdu, mesh_sequence_number_next());
}
mesh_network_send_pdu(network_pdu);
break;
case MESH_PDU_TYPE_TRANSPORT:

View File

@ -98,7 +98,7 @@ typedef struct mesh_network_pdu {
uint16_t netkey_index;
// meta data transport layer
uint16_t appkey_index;
// flags: bit 0 indicates Proxy PDU
// MESH_NETWORK_PDU_FLAGS
uint16_t flags;
// pdu
@ -106,6 +106,8 @@ typedef struct mesh_network_pdu {
uint8_t data[MESH_NETWORK_PAYLOAD_MAX];
} mesh_network_pdu_t;
#define MESH_TRANSPORT_FLAG_SEQ_RESERVED 1
typedef struct {
mesh_pdu_t pdu_header;
@ -125,6 +127,8 @@ typedef struct {
uint8_t akf_aid_control;
// network pdu header
uint8_t network_header[9];
// MESH_TRANSPORT_FLAG
uint16_t flags;
// acknowledgement timer active
uint8_t acknowledgement_timer_active;
// incomplete timer active

View File

@ -589,8 +589,9 @@ static void mesh_upper_transport_send_unsegmented_access_pdu_ccm(void * arg){
// store TransMIC
btstack_crypto_ccm_get_authentication_value(&ccm, &upper_transport_pdu[upper_transport_pdu_len]);
mesh_print_hex("TransMIC", &upper_transport_pdu[upper_transport_pdu_len], 4);
network_pdu->len += 4;
mesh_print_hex("UpperTransportPDU", upper_transport_pdu, network_pdu->len);
network_pdu->len += 4;
upper_transport_pdu_len += 4;
mesh_print_hex("UpperTransportPDU", upper_transport_pdu, upper_transport_pdu_len);
// send network pdu
mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu);
}
@ -729,9 +730,10 @@ static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_trans
const uint8_t trans_mic_len = szmic ? 8 : 4;
// 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)
uint32_t seq = mesh_sequence_number_peek();
// reserve one sequence number, which is also used to encrypt access payload
uint32_t seq = mesh_sequence_number_next();
transport_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED;
// store in transport pdu
transport_pdu->transmic_len = trans_mic_len;
transport_pdu->netkey_index = netkey_index;
@ -1007,7 +1009,8 @@ void mesh_upper_transport_init(){
void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){
if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){
btstack_assert(mesh_network_pdu_len((mesh_network_pdu_t *) pdu) >= 9);
mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) pdu;
btstack_assert(network_pdu->len >= 9);
}
btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu);