From 94d617e4bd5430dd3b7666e70d35ae97cfa050a0 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 24 Apr 2020 16:15:43 +0200 Subject: [PATCH] mesh: support separate packet handlers for provisioner and device roles --- src/mesh/pb_adv.c | 21 +++++++++++++++++---- src/mesh/pb_adv.h | 11 ++++++++--- src/mesh/provisioning_device.c | 2 +- src/mesh/provisioning_provisioner.c | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/mesh/pb_adv.c b/src/mesh/pb_adv.c index bcc74d7f6..05fc42981 100644 --- a/src/mesh/pb_adv.c +++ b/src/mesh/pb_adv.c @@ -97,7 +97,7 @@ static uint8_t pb_adv_provisioner_open_countdown; static uint8_t pb_adv_msg_in_buffer[MESH_PB_ADV_MAX_PDU_SIZE]; // TODO: how large are prov messages? -// single adv link +// single adv link, roles: provisioner = 1, device = 0 static uint16_t pb_adv_cid = 1; static uint8_t pb_adv_provisioner_role; @@ -137,7 +137,8 @@ static uint8_t pb_adv_msg_out_seg; static uint32_t pb_adv_msg_out_start; static const uint8_t * pb_adv_msg_out_buffer; -static btstack_packet_handler_t pb_adv_packet_handler; +static btstack_packet_handler_t pb_adv_device_packet_handler; +static btstack_packet_handler_t pb_adv_provisioner_packet_handler; // poor man's random number generator static uint32_t pb_adv_random(void){ @@ -145,6 +146,14 @@ static uint32_t pb_adv_random(void){ return pb_adv_lfsr; } +static void pb_adv_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){ + if (pb_adv_provisioner_role == 0){ + (*pb_adv_device_packet_handler)(packet_type, channel, packet, size); + } else { + (*pb_adv_provisioner_packet_handler)(packet_type, channel, packet, size); + } +} + static void pb_adv_emit_pdu_sent(uint8_t status){ uint8_t event[] = { HCI_EVENT_MESH_META, 2, MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT, status}; pb_adv_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); @@ -614,8 +623,12 @@ void pb_adv_init(void){ pb_adv_random(); } -void pb_adv_register_packet_handler(btstack_packet_handler_t packet_handler){ - pb_adv_packet_handler = packet_handler; +void pb_adv_register_device_packet_handler(btstack_packet_handler_t packet_handler){ + pb_adv_device_packet_handler = packet_handler; +} + +void pb_adv_register_provisioner_packet_handler(btstack_packet_handler_t packet_handler){ + pb_adv_provisioner_packet_handler = packet_handler; } void pb_adv_send_pdu(uint16_t pb_transport_cid, const uint8_t * pdu, uint16_t size){ diff --git a/src/mesh/pb_adv.h b/src/mesh/pb_adv.h index 8a9293dc6..bd210b519 100644 --- a/src/mesh/pb_adv.h +++ b/src/mesh/pb_adv.h @@ -54,11 +54,16 @@ extern "C" { void pb_adv_init(void); /** - * Register listener for Provisioning PDUs and MESH_PBV_ADV_SEND_COMPLETE + * Register provisioning device listener for Provisioning PDUs and MESH_PBV_ADV_SEND_COMPLETE */ -void pb_adv_register_packet_handler(btstack_packet_handler_t packet_handler); +void pb_adv_register_device_packet_handler(btstack_packet_handler_t packet_handler); -/** +/** + * Register provisioning provisioner listener for Provisioning PDUs and MESH_PBV_ADV_SEND_COMPLETE + */ +void pb_adv_register_provisioner_packet_handler(btstack_packet_handler_t packet_handler); + +/** * Send Provisioning PDU * @param pb_adv_cid * @param pdu diff --git a/src/mesh/provisioning_device.c b/src/mesh/provisioning_device.c index 771d6b6c7..f78403fa6 100644 --- a/src/mesh/provisioning_device.c +++ b/src/mesh/provisioning_device.c @@ -843,7 +843,7 @@ void provisioning_device_init(void){ #ifdef ENABLE_MESH_ADV_BEARER // setup PB ADV pb_adv_init(); - pb_adv_register_packet_handler(&provisioning_handle_pdu); + pb_adv_register_device_packet_handler(&provisioning_handle_pdu); #endif #ifdef ENABLE_MESH_GATT_BEARER // setup PB GATT diff --git a/src/mesh/provisioning_provisioner.c b/src/mesh/provisioning_provisioner.c index 76445a822..39a4e2914 100644 --- a/src/mesh/provisioning_provisioner.c +++ b/src/mesh/provisioning_provisioner.c @@ -734,7 +734,7 @@ static void prov_key_generated(void * arg){ void provisioning_provisioner_init(void){ pb_adv_cid = MESH_PB_TRANSPORT_INVALID_CID; pb_adv_init(); - pb_adv_register_packet_handler(&provisioning_handle_pdu); + pb_adv_register_provisioner_packet_handler(&provisioning_handle_pdu); } void provisioning_provisioner_register_packet_handler(btstack_packet_handler_t packet_handler){