From 0728b042e097e1e8a81f1d03d42db542389d47b2 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 16 Jul 2019 13:21:28 +0200 Subject: [PATCH] mesh: implement attention timer in mesh --- example/mesh_node_demo.c | 6 ++--- src/mesh/mesh.c | 47 ++++++++++++++++++++++++++++++++++++++-- src/mesh/mesh.h | 4 ++++ test/mesh/mesh_pts.c | 4 ++-- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/example/mesh_node_demo.c b/example/mesh_node_demo.c index b5065caf8..b16e27be8 100644 --- a/example/mesh_node_demo.c +++ b/example/mesh_node_demo.c @@ -104,12 +104,12 @@ static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t cha case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN: printf("Provisioner link opened"); break; + case MESH_SUBEVENT_ATTENTION_TIMER: + printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet)); + break; case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: printf("Provisioner link close"); break; - case MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER: - printf("Attention Timer: %u\n", mesh_subevent_pb_prov_attention_timer_get_attention_time(packet)); - break; case MESH_SUBEVENT_PB_PROV_COMPLETE: printf("Provisioning complete\n"); break; diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c index 9da0789a8..5375be097 100644 --- a/src/mesh/mesh.c +++ b/src/mesh/mesh.c @@ -147,6 +147,9 @@ static void * btstack_tlv_singleton_context; static uint32_t sequence_number_last_stored; static uint32_t sequence_number_storage_trigger; +// Attention Timer +static uint8_t attention_timer_timeout_s; +static btstack_timer_source_t attention_timer_timer; static void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){ @@ -181,12 +184,52 @@ static void mesh_access_setup_from_provisioning_data(const mesh_provisioning_dat #endif } +// Attention Timer state +static void mesh_emit_attention_timer_event(uint8_t timer_s){ + if (!provisioning_device_packet_handler) return; + uint8_t event[4] = { HCI_EVENT_MESH_META, 4, MESH_SUBEVENT_ATTENTION_TIMER}; + event[3] = timer_s; + provisioning_device_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); +} + +static void mesh_attention_timer_handler(btstack_timer_source_t * ts){ + UNUSED(ts); + attention_timer_timeout_s--; + mesh_emit_attention_timer_event(attention_timer_timeout_s); + if (attention_timer_timeout_s == 0) return; + + btstack_run_loop_set_timer(&attention_timer_timer, 1000); + btstack_run_loop_add_timer(&attention_timer_timer); +} + +void mesh_attention_timer_set(uint8_t timer_s){ + // stop old timer if running + if (attention_timer_timeout_s){ + btstack_run_loop_remove_timer(&attention_timer_timer); + } + + attention_timer_timeout_s = timer_s; + mesh_emit_attention_timer_event(attention_timer_timeout_s); + + if (attention_timer_timeout_s){ + btstack_run_loop_set_timer_handler(&attention_timer_timer, &mesh_attention_timer_handler); + btstack_run_loop_set_timer(&attention_timer_timer, 1000); + btstack_run_loop_add_timer(&attention_timer_timer); + } +} + +uint8_t mesh_attention_timer_get(void){ + return attention_timer_timeout_s; +} + static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ mesh_provisioning_data_t provisioning_data; - switch(packet[0]){ case HCI_EVENT_MESH_META: switch(packet[2]){ + case MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER: + mesh_attention_timer_set(mesh_subevent_pb_prov_attention_timer_get_attention_time(packet)); + break; case MESH_SUBEVENT_PB_PROV_COMPLETE: // get provisioning data provisioning_device_data_get(&provisioning_data); @@ -948,6 +991,7 @@ void mesh_init(void){ #endif provisioning_device_init(); + provisioning_device_register_packet_handler(&mesh_provisioning_message_handler); // Node Configuration mesh_node_init(); @@ -975,5 +1019,4 @@ void mesh_init(void){ */ void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t packet_handler){ provisioning_device_packet_handler = packet_handler; - provisioning_device_register_packet_handler(&mesh_provisioning_message_handler); } diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h index 1672ff91d..c7280c805 100644 --- a/src/mesh/mesh.h +++ b/src/mesh/mesh.h @@ -107,6 +107,10 @@ int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index) // Mesh Node Reset void mesh_node_reset(void); +// Attention Timer +void mesh_attention_timer_set(uint8_t timer_s); +uint8_t mesh_attention_timer_get(void); + // temp void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet); void mesh_access_netkey_finalize(mesh_network_key_t * network_key); diff --git a/test/mesh/mesh_pts.c b/test/mesh/mesh_pts.c index 66a44a5a7..86e35fc6b 100644 --- a/test/mesh/mesh_pts.c +++ b/test/mesh/mesh_pts.c @@ -148,8 +148,8 @@ static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t cha case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED: printf("Provisioner link close"); break; - case MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER: - printf("Attention Timer: %u\n", mesh_subevent_pb_prov_attention_timer_get_attention_time(packet)); + case MESH_SUBEVENT_ATTENTION_TIMER: + printf("Attention Timer: %u\n", mesh_subevent_attention_timer_get_attention_time(packet)); break; case MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST: printf("Enter passphrase: ");