mesh: add mesh_pdu_type_t to mesh_network_pdu_t and mesh_transport_pdu_t, use mesh_network/transport_get/free to set pdu type on alloc

This commit is contained in:
Matthias Ringwald 2019-04-04 10:24:19 +02:00
parent 2a8034828b
commit f46b7bdedf
6 changed files with 73 additions and 27 deletions

View File

@ -598,7 +598,7 @@ static void mesh_network_run(void){
if (mesh_crypto_active) return;
if (!btstack_linked_list_empty(&network_pdus_received)){
mesh_network_pdu_t * decode_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * decode_pdu = mesh_network_pdu_get();
if (!decode_pdu) return;
// get encoded network pdu and start processing
mesh_crypto_active = 1;
@ -682,9 +682,8 @@ void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len){
if (pdu_len > 29) return;
// allocate network_pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
if (!network_pdu) return;
memset(network_pdu, 0, sizeof(mesh_network_pdu_t));
// store data
memcpy(network_pdu->data, pdu_data, pdu_len);
@ -702,9 +701,8 @@ void mesh_network_process_proxy_message(const uint8_t * pdu_data, uint8_t pdu_le
if (pdu_len > 29) return;
// allocate network_pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
if (!network_pdu) return;
memset(network_pdu, 0, sizeof(mesh_network_pdu_t));
// store data
memcpy(network_pdu->data, pdu_data, pdu_len);
@ -843,3 +841,17 @@ void mesh_network_reset(void){
mesh_network_reset_network_pdus(&network_pdus_queued);
mesh_network_reset_network_pdus(&network_pdus_outgoing);
}
// buffer pool
mesh_network_pdu_t * mesh_network_pdu_get(void){
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
if (network_pdu) {
memset(network_pdu, 0, sizeof(mesh_network_pdu_t));
network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_NETWORK;
}
return network_pdu;
}
void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu){
btstack_memory_mesh_network_pdu_free(network_pdu);
}

View File

@ -58,9 +58,21 @@ typedef enum {
MESH_NETWORK_PDU_SENT,
} mesh_network_callback_type_t;
typedef struct mesh_network_pdu {
typedef enum {
MESH_PDU_TYPE_NETWORK = 0,
MESH_PDU_TYPE_TRANSPORT,
} mesh_pdu_type_t;
typedef struct mesh_pdu {
// allow for linked lists
btstack_linked_item_t item;
// type
mesh_pdu_type_t pdu_type;
} mesh_pdu_t;
typedef struct mesh_network_pdu {
mesh_pdu_t pdu_header;
// callback
void (*callback)(struct mesh_network_pdu * network_pdu);
@ -77,8 +89,8 @@ typedef struct mesh_network_pdu {
} mesh_network_pdu_t;
typedef struct {
// allow for linked lists
btstack_linked_item_t item;
mesh_pdu_t pdu_header;
// rx/tx: acknowledgement timer / segment transmission timer
btstack_timer_source_t acknowledgement_timer;
// rx: incomplete timer / tx: resend timer
@ -216,6 +228,10 @@ int mesh_network_address_unicast(uint16_t addr);
*/
int mesh_network_address_virtual(uint16_t addr);
// buffer pool
mesh_network_pdu_t * mesh_network_pdu_get(void);
void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
// Mesh Network PDU Getter
uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu);
uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu);

View File

@ -155,7 +155,7 @@ static uint8_t mesh_network_send(uint16_t netkey_index, uint8_t ctl, uint8_t ttl
if (!network_key) return 0;
// allocate network_pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
if (!network_pdu) return 0;
// setup network_pdu
@ -404,7 +404,7 @@ static void send_pts_unsegmented_access_messsage(void){
uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
// send as unsegmented access pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
int status = mesh_upper_transport_setup_unsegmented_access_pdu(network_pdu, netkey_index, appkey_index, ttl, src, dest, access_pdu_data, access_pdu_len);
if (status) return;
mesh_upper_transport_send_unsegmented_access_pdu(network_pdu);
@ -425,7 +425,7 @@ static void send_pts_segmented_access_messsage_unicast(void){
uint16_t appkey_index = 0; // MESH_DEVICE_KEY_INDEX;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
int status = mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
if (status) return;
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
@ -446,7 +446,7 @@ static void send_pts_segmented_access_messsage_group(void){
uint16_t appkey_index = 0;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
int status = mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
if (status) return;
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
@ -467,7 +467,7 @@ static void send_pts_segmented_access_messsage_virtual(void){
uint16_t appkey_index = 0;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
int status = mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
if (status) return;
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
@ -627,7 +627,7 @@ static void config_composition_data_status(void){
pos += 2;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
}
@ -650,7 +650,7 @@ static void config_appkey_status(uint32_t netkey_and_appkey_index, uint8_t statu
pos += 3;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
}
@ -710,7 +710,7 @@ static void config_model_subscription_status(uint8_t status, uint16_t element_ad
pos += 2;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
}
@ -748,7 +748,7 @@ static void config_model_app_status(uint8_t status, uint16_t element_address, ui
pos += 2;
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, 0, access_pdu_data, access_pdu_len);
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
}

View File

@ -268,12 +268,12 @@ void test_send_access_message(uint16_t netkey_index, uint16_t appkey_index, uin
if (count == 1 ){
// send as unsegmented access pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
mesh_upper_transport_setup_unsegmented_access_pdu(network_pdu, netkey_index, appkey_index, ttl, src, dest, transport_pdu_data, transport_pdu_len);
mesh_upper_transport_send_unsegmented_access_pdu(network_pdu);
} else {
// send as segmented access pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_access_pdu(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic, transport_pdu_data, transport_pdu_len);
mesh_upper_transport_send_segmented_access_pdu(transport_pdu);
}
@ -314,12 +314,12 @@ void test_send_control_message(uint16_t netkey_index, uint8_t ttl, uint16_t src,
if (transport_pdu_len < 12){
// send as unsegmented control pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
mesh_upper_transport_setup_unsegmented_control_pdu(network_pdu, netkey_index, ttl, src, dest, opcode, transport_pdu_data+1, transport_pdu_len-1);
mesh_upper_transport_send_unsegmented_control_pdu(network_pdu);
} else {
// send as segmented control pdu
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * transport_pdu = mesh_transport_pdu_get();
mesh_upper_transport_setup_segmented_control_pdu(transport_pdu, netkey_index, ttl, src, dest, opcode, transport_pdu_data+1, transport_pdu_len-1);
mesh_upper_transport_send_segmented_control_pdu(transport_pdu);
}
@ -928,7 +928,7 @@ TEST(MessageTest, ProxyConfigSend){
uint8_t nid = 0x10;
mesh_set_iv_index(0x12345678);
load_network_key_nid_10();
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
uint8_t data[] = { 0 , 0 };
mesh_network_setup_pdu(network_pdu, netkey_index, nid, ctl, ttl, seq, src, dest, data, sizeof(data));
mesh_network_encrypt_proxy_message(network_pdu, &test_proxy_callback_handler);

View File

@ -133,7 +133,7 @@ static uint8_t mesh_network_send(uint16_t netkey_index, uint8_t ctl, uint8_t ttl
if (!network_key) return 0;
// allocate network_pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
if (!network_pdu) return 0;
// setup network_pdu
@ -339,7 +339,7 @@ static mesh_transport_pdu_t * mesh_lower_transport_pdu_for_segmented_message(mes
// no transport pdu active, check if seq zero is new
if (seq_auth > peer->seq_auth){
mesh_transport_pdu_t * pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * pdu = mesh_transport_pdu_get();
if (!pdu) return NULL;
// cache network pdu header
@ -568,7 +568,7 @@ static void mesh_lower_transport_send_segmented_pdu_once(mesh_transport_pdu_t *t
lower_transport_retry_count--;
// allocate network_pdu
mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
if (!network_pdu) return;
// setup
@ -1410,7 +1410,7 @@ static void mesh_transport_run(void){
mesh_network_message_processed_by_higher_layer(network_pdu);
} else {
// unsegmented access message (encrypted)
mesh_network_pdu_t * decode_pdu = btstack_memory_mesh_network_pdu_get();
mesh_network_pdu_t * decode_pdu = mesh_network_pdu_get();
if (!decode_pdu) return;
// get encoded network pdu and start processing
network_pdu_in_validation = network_pdu;
@ -1423,7 +1423,7 @@ static void mesh_transport_run(void){
if (!btstack_linked_list_empty(&upper_transport_access)){
// peek at next message
mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_access);
mesh_transport_pdu_t * decode_pdu = btstack_memory_mesh_transport_pdu_get();
mesh_transport_pdu_t * decode_pdu = mesh_transport_pdu_get();
if (!decode_pdu) return;
// get encoded transport pdu and start processing
transport_pdu_in_validation = transport_pdu;
@ -1434,3 +1434,17 @@ static void mesh_transport_run(void){
if (done) return;
}
}
// buffer pool
mesh_transport_pdu_t * mesh_transport_pdu_get(void){
mesh_transport_pdu_t * transport_pdu = btstack_memory_mesh_transport_pdu_get();
if (transport_pdu) {
memset(transport_pdu, 0, sizeof(mesh_transport_pdu_t));
transport_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_TRANSPORT;
}
return transport_pdu;
}
void mesh_transport_pdu_free(mesh_transport_pdu_t * transport_pdu){
btstack_memory_mesh_transport_pdu_free(transport_pdu);
}

View File

@ -113,6 +113,10 @@ void mesh_lower_transport_dump(void);
void mesh_lower_transport_reset(void);
void mesh_seq_auth_reset(void);
// allocator
mesh_transport_pdu_t * mesh_transport_pdu_get(void);
void mesh_transport_pdu_free(mesh_transport_pdu_t * transport_pdu);
#ifdef __cplusplus
} /* end of extern "C" */
#endif