mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-30 16:20:24 +00:00
mesh: add callback to mesh_network_pdu_t, add mesh_network_encrypt_proxy_message, add proxy encryption test
This commit is contained in:
parent
fac8099173
commit
5a90c404e7
@ -249,6 +249,18 @@ static void mesh_proxy_create_nonce(uint8_t * nonce, const mesh_network_pdu_t *
|
||||
|
||||
// NID/IVI | obfuscated (CTL/TTL, SEQ (24), SRC (16) ), encrypted ( DST(16), TransportPDU), MIC(32 or 64)
|
||||
|
||||
static void mesh_network_send_d(mesh_network_pdu_t * network_pdu){
|
||||
|
||||
// add to queue
|
||||
btstack_linked_list_add_tail(&network_pdus_outgoing, (btstack_linked_item_t *) network_pdu);
|
||||
|
||||
// request to send
|
||||
adv_bearer_request_can_send_now_for_mesh_message();
|
||||
|
||||
// go
|
||||
mesh_network_run();
|
||||
}
|
||||
|
||||
// new
|
||||
static void mesh_network_send_c(void *arg){
|
||||
mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
|
||||
@ -265,15 +277,8 @@ static void mesh_network_send_c(void *arg){
|
||||
// crypto done
|
||||
mesh_crypto_active = 0;
|
||||
|
||||
|
||||
// add to queue
|
||||
btstack_linked_list_add_tail(&network_pdus_outgoing, (btstack_linked_item_t *) network_pdu);
|
||||
|
||||
// request to send
|
||||
adv_bearer_request_can_send_now_for_mesh_message();
|
||||
|
||||
// go
|
||||
mesh_network_run();
|
||||
// done
|
||||
(network_pdu->callback)(network_pdu);
|
||||
}
|
||||
|
||||
static void mesh_network_send_b(void *arg){
|
||||
@ -301,6 +306,9 @@ static void mesh_network_send_b(void *arg){
|
||||
}
|
||||
|
||||
static void mesh_network_send_a(mesh_network_pdu_t * network_pdu){
|
||||
|
||||
mesh_crypto_active = 1;
|
||||
|
||||
// lookup network by netkey_index
|
||||
current_network_key = mesh_network_key_list_get(network_pdu->netkey_index);
|
||||
if (!current_network_key) {
|
||||
@ -313,9 +321,16 @@ static void mesh_network_send_a(mesh_network_pdu_t * network_pdu){
|
||||
}
|
||||
|
||||
// get network nonce
|
||||
mesh_network_create_nonce(network_nonce, network_pdu, global_iv_index);
|
||||
printf("TX-NetworkNonce: ");
|
||||
printf_hexdump(network_nonce, 13);
|
||||
if (network_pdu->flags & 1){
|
||||
mesh_proxy_create_nonce(network_nonce, network_pdu, global_iv_index);
|
||||
printf("TX-ProxyNonce: ");
|
||||
printf_hexdump(network_nonce, 13);
|
||||
} else {
|
||||
mesh_network_create_nonce(network_nonce, network_pdu, global_iv_index);
|
||||
printf("TX-NetworkNonce: ");
|
||||
printf_hexdump(network_nonce, 13);
|
||||
}
|
||||
|
||||
printf("TX-EncryptionKey: ");
|
||||
printf_hexdump(current_network_key->encryption_key, 16);
|
||||
|
||||
@ -341,6 +356,7 @@ void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network
|
||||
network_pdu->data[1] = (ctl << 7) | (ttl - 1);
|
||||
|
||||
// queue up
|
||||
network_pdu->callback = &mesh_network_send_d;
|
||||
btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
|
||||
|
||||
// go
|
||||
@ -517,6 +533,10 @@ static void process_network_pdu(mesh_network_pdu_t * network_pdu){
|
||||
process_network_pdu_validate(network_pdu);
|
||||
}
|
||||
|
||||
// static void mesh_network_encrypt_and_obfuscate(mesh_network_pdu_t * network_pdu, void (*callback)(mesh_network_pdu_t * network_pdu)){
|
||||
// network_pdu->callback = callback;
|
||||
// }
|
||||
|
||||
static void mesh_network_run(void){
|
||||
if (mesh_crypto_active) return;
|
||||
|
||||
@ -532,7 +552,6 @@ static void mesh_network_run(void){
|
||||
|
||||
if (!btstack_linked_list_empty(&network_pdus_queued)){
|
||||
// get queued network pdu and start processing
|
||||
mesh_crypto_active = 1;
|
||||
mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_queued);
|
||||
mesh_network_send_a(network_pdu);
|
||||
return;
|
||||
@ -635,6 +654,25 @@ void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu){
|
||||
printf("NetworkPDU(unencrypted): ");
|
||||
printf_hexdump(network_pdu->data, network_pdu->len);
|
||||
|
||||
// setup callback
|
||||
network_pdu->callback = &mesh_network_send_d;
|
||||
network_pdu->flags = 0;
|
||||
|
||||
// queue up
|
||||
btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
|
||||
|
||||
// go
|
||||
mesh_network_run();
|
||||
}
|
||||
|
||||
void mesh_network_encrypt_proxy_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback)){
|
||||
printf("ProxyPDU(unencrypted): ");
|
||||
printf_hexdump(network_pdu->data, network_pdu->len);
|
||||
|
||||
// setup callback
|
||||
network_pdu->callback = callback;
|
||||
network_pdu->flags = 1;
|
||||
|
||||
// queue up
|
||||
btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
|
||||
|
||||
|
@ -56,9 +56,12 @@ typedef enum {
|
||||
MESH_NETWORK_PDU_SENT,
|
||||
} mesh_network_callback_type_t;
|
||||
|
||||
typedef struct {
|
||||
typedef struct mesh_network_pdu {
|
||||
// allow for linked lists
|
||||
btstack_linked_item_t item;
|
||||
// callback
|
||||
void (*callback)(struct mesh_network_pdu * network_pdu);
|
||||
|
||||
// meta data network layer
|
||||
uint16_t netkey_index;
|
||||
// meta data transport layer
|
||||
@ -208,6 +211,7 @@ uint32_t mesh_get_iv_index(void);
|
||||
// Testing only
|
||||
void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len);
|
||||
void mesh_network_process_proxy_message(const uint8_t * pdu_data, uint8_t pdu_len);
|
||||
void mesh_network_encrypt_proxy_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
|
||||
void mesh_network_dump(void);
|
||||
void mesh_network_reset(void);
|
||||
|
||||
|
@ -718,8 +718,6 @@ TEST(MessageTest, Message19Send){
|
||||
test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message19_upper_transport_pdu, 1, message19_lower_transport_pdus, message19_network_pdus);
|
||||
}
|
||||
|
||||
??
|
||||
|
||||
// Message 20
|
||||
char * message20_network_pdus[] = {
|
||||
(char *) "e85cca51e2e8998c3dc87344a16c787f6b08cc897c941a5368",
|
||||
@ -771,6 +769,7 @@ TEST(MessageTest, Message21Send){
|
||||
mesh_upper_transport_set_seq(seq);
|
||||
test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message21_upper_transport_pdu, 1, message21_lower_transport_pdus, message21_network_pdus);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
// Message 22
|
||||
@ -809,7 +808,6 @@ TEST(MessageTest, Message22Send){
|
||||
mesh_upper_transport_set_seq(seq);
|
||||
test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message22_upper_transport_pdu, 1, message22_lower_transport_pdus, message22_network_pdus);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
// Message 23
|
||||
@ -919,21 +917,40 @@ TEST(MessageTest, ProxyConfigReceive){
|
||||
received_proxy_pdu = NULL;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void test_proxy_callback_handler(mesh_network_pdu_t * network_pdu){
|
||||
received_proxy_pdu = network_pdu;
|
||||
}
|
||||
|
||||
TEST(MessageTest, ProxyConfigSend){
|
||||
uint16_t netkey_index = 0;
|
||||
uint16_t appkey_index = 0;
|
||||
uint8_t ttl = 3;
|
||||
uint16_t src = 0x1234;
|
||||
uint16_t dest = 0x9736;
|
||||
uint32_t seq = 0x07080d;
|
||||
uint8_t szmic = 1;
|
||||
|
||||
uint8_t ctl = 1;
|
||||
uint8_t ttl = 0;
|
||||
uint16_t src = 1;
|
||||
uint16_t dest = 0;
|
||||
uint32_t seq = 1;
|
||||
uint8_t nid = 0x10;
|
||||
mesh_set_iv_index(0x12345678);
|
||||
mesh_upper_transport_set_seq(seq);
|
||||
test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message24_upper_transport_pdu, 2, message24_lower_transport_pdus, message24_network_pdus);
|
||||
load_network_key_nid_10();
|
||||
mesh_network_pdu_t * network_pdu = btstack_memory_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);
|
||||
while (received_proxy_pdu == NULL) {
|
||||
mock_process_hci_cmd();
|
||||
}
|
||||
uint8_t * proxy_pdu_data = received_proxy_pdu->data;
|
||||
uint8_t proxy_pdu_len = received_proxy_pdu->len;
|
||||
|
||||
int i = 0;
|
||||
char ** network_pdus = proxy_config_pdus;
|
||||
transport_pdu_len = strlen(network_pdus[i]) / 2;
|
||||
btstack_parse_hex(network_pdus[i], transport_pdu_len, transport_pdu_data);
|
||||
|
||||
CHECK_EQUAL( transport_pdu_len-1, proxy_pdu_len);
|
||||
CHECK_EQUAL_ARRAY(transport_pdu_data+1, proxy_pdu_data, transport_pdu_len-1);
|
||||
|
||||
received_proxy_pdu = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main (int argc, const char * argv[]){
|
||||
return CommandLineTestRunner::RunAllTests(argc, argv);
|
||||
|
Loading…
x
Reference in New Issue
Block a user