177ba3d3fSMatthias Ringwald /* 277ba3d3fSMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH 377ba3d3fSMatthias Ringwald * 477ba3d3fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 577ba3d3fSMatthias Ringwald * modification, are permitted provided that the following conditions 677ba3d3fSMatthias Ringwald * are met: 777ba3d3fSMatthias Ringwald * 877ba3d3fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 977ba3d3fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1077ba3d3fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1177ba3d3fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1277ba3d3fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 1377ba3d3fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1477ba3d3fSMatthias Ringwald * contributors may be used to endorse or promote products derived 1577ba3d3fSMatthias Ringwald * from this software without specific prior written permission. 1677ba3d3fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1777ba3d3fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 1877ba3d3fSMatthias Ringwald * monetary gain. 1977ba3d3fSMatthias Ringwald * 2077ba3d3fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2177ba3d3fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2277ba3d3fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2377ba3d3fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2477ba3d3fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2577ba3d3fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2677ba3d3fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2777ba3d3fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2877ba3d3fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2977ba3d3fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3077ba3d3fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3177ba3d3fSMatthias Ringwald * SUCH DAMAGE. 3277ba3d3fSMatthias Ringwald * 3377ba3d3fSMatthias Ringwald * Please inquire about commercial licensing options at 3477ba3d3fSMatthias Ringwald * [email protected] 3577ba3d3fSMatthias Ringwald * 3677ba3d3fSMatthias Ringwald */ 3777ba3d3fSMatthias Ringwald 38b3a527ecSMatthias Ringwald #define __BTSTACK_FILE__ "beacon.c" 3977ba3d3fSMatthias Ringwald 4077ba3d3fSMatthias Ringwald #include <string.h> 4177ba3d3fSMatthias Ringwald 4277ba3d3fSMatthias Ringwald #include "mesh/beacon.h" 4377ba3d3fSMatthias Ringwald #include "mesh/adv_bearer.h" 4477ba3d3fSMatthias Ringwald #include "mesh/gatt_bearer.h" 4577ba3d3fSMatthias Ringwald #include "ble/core.h" 4677ba3d3fSMatthias Ringwald #include "bluetooth.h" 4777ba3d3fSMatthias Ringwald #include "bluetooth_data_types.h" 4877ba3d3fSMatthias Ringwald #include "btstack_debug.h" 4977ba3d3fSMatthias Ringwald #include "btstack_util.h" 5077ba3d3fSMatthias Ringwald #include "btstack_run_loop.h" 5177ba3d3fSMatthias Ringwald #include "btstack_event.h" 5277ba3d3fSMatthias Ringwald #include "gap.h" 5377ba3d3fSMatthias Ringwald #include "mesh_keys.h" 5477ba3d3fSMatthias Ringwald 5577ba3d3fSMatthias Ringwald #define BEACON_TYPE_UNPROVISIONED_DEVICE 0 5677ba3d3fSMatthias Ringwald #define BEACON_TYPE_SECURE_NETWORK 1 5777ba3d3fSMatthias Ringwald 5877ba3d3fSMatthias Ringwald #define UNPROVISIONED_BEACON_INTERVAL_MS 5000 5977ba3d3fSMatthias Ringwald #define UNPROVISIONED_BEACON_LEN 23 6077ba3d3fSMatthias Ringwald 6177ba3d3fSMatthias Ringwald #define SECURE_NETWORK_BEACON_INTERVAL_MIN_MS 10000 6277ba3d3fSMatthias Ringwald #define SECURE_NETWORK_BEACON_INTERVAL_MAX_MS 600000 6377ba3d3fSMatthias Ringwald #define SECURE_NETWORK_BEACON_LEN 22 6477ba3d3fSMatthias Ringwald 6577ba3d3fSMatthias Ringwald // prototypes 6677ba3d3fSMatthias Ringwald static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts); 6777ba3d3fSMatthias Ringwald 68b3a527ecSMatthias Ringwald // bearers 69b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER 70b3a527ecSMatthias Ringwald static hci_con_handle_t gatt_bearer_con_handle; 71b3a527ecSMatthias Ringwald #endif 72b3a527ecSMatthias Ringwald 7377ba3d3fSMatthias Ringwald // beacon 7477ba3d3fSMatthias Ringwald static uint8_t mesh_beacon_data[29]; 7577ba3d3fSMatthias Ringwald static uint8_t mesh_beacon_len; 7677ba3d3fSMatthias Ringwald static btstack_timer_source_t beacon_timer; 7777ba3d3fSMatthias Ringwald 7877ba3d3fSMatthias Ringwald // unprovisioned device beacon 79b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 8077ba3d3fSMatthias Ringwald static const uint8_t * beacon_device_uuid; 8177ba3d3fSMatthias Ringwald static uint16_t beacon_oob_information; 8277ba3d3fSMatthias Ringwald static uint32_t beacon_uri_hash; 8377ba3d3fSMatthias Ringwald static int beacon_send_device_beacon; 84b3a527ecSMatthias Ringwald #endif 8577ba3d3fSMatthias Ringwald 8677ba3d3fSMatthias Ringwald static btstack_packet_handler_t unprovisioned_device_beacon_handler; 8777ba3d3fSMatthias Ringwald 8877ba3d3fSMatthias Ringwald // secure network beacon 8977ba3d3fSMatthias Ringwald static btstack_crypto_aes128_cmac_t mesh_secure_network_beacon_cmac_request; 9077ba3d3fSMatthias Ringwald static uint8_t mesh_secure_network_beacon_auth_value[16]; 9177ba3d3fSMatthias Ringwald static btstack_packet_handler_t mesh_secure_network_beacon_handler; 9277ba3d3fSMatthias Ringwald static int mesh_secure_network_beacon_active; 9377ba3d3fSMatthias Ringwald static uint8_t mesh_secure_network_beacon_validate_buffer[SECURE_NETWORK_BEACON_LEN]; 9477ba3d3fSMatthias Ringwald 95b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 9677ba3d3fSMatthias Ringwald static void beacon_timer_handler(btstack_timer_source_t * ts){ 9777ba3d3fSMatthias Ringwald // restart timer 9877ba3d3fSMatthias Ringwald btstack_run_loop_set_timer(ts, UNPROVISIONED_BEACON_INTERVAL_MS); 9977ba3d3fSMatthias Ringwald btstack_run_loop_add_timer(ts); 10077ba3d3fSMatthias Ringwald 10177ba3d3fSMatthias Ringwald // setup beacon 10277ba3d3fSMatthias Ringwald mesh_beacon_len = UNPROVISIONED_BEACON_LEN; 10377ba3d3fSMatthias Ringwald mesh_beacon_data[0] = BEACON_TYPE_UNPROVISIONED_DEVICE; 10477ba3d3fSMatthias Ringwald memcpy(&mesh_beacon_data[1], beacon_device_uuid, 16); 10577ba3d3fSMatthias Ringwald big_endian_store_16(mesh_beacon_data, 17, beacon_oob_information); 10677ba3d3fSMatthias Ringwald big_endian_store_32(mesh_beacon_data, 19, beacon_uri_hash); 10777ba3d3fSMatthias Ringwald 10877ba3d3fSMatthias Ringwald // request to send 10977ba3d3fSMatthias Ringwald beacon_send_device_beacon = 1; 11077ba3d3fSMatthias Ringwald adv_bearer_request_can_send_now_for_mesh_beacon(); 11177ba3d3fSMatthias Ringwald } 112b3a527ecSMatthias Ringwald #endif 11377ba3d3fSMatthias Ringwald 11477ba3d3fSMatthias Ringwald static void mesh_secure_network_beacon_auth_value_calculated(void * arg){ 11577ba3d3fSMatthias Ringwald mesh_network_key_t * mesh_network_key = (mesh_network_key_t *) arg; 11677ba3d3fSMatthias Ringwald 11777ba3d3fSMatthias Ringwald memcpy(&mesh_beacon_data[14], mesh_secure_network_beacon_auth_value, 8); 11877ba3d3fSMatthias Ringwald mesh_beacon_len = SECURE_NETWORK_BEACON_LEN; 11977ba3d3fSMatthias Ringwald 12077ba3d3fSMatthias Ringwald printf("Secure Network Beacon\n"); 12177ba3d3fSMatthias Ringwald printf("- "); 12277ba3d3fSMatthias Ringwald printf_hexdump(mesh_beacon_data, mesh_beacon_len); 12377ba3d3fSMatthias Ringwald 12477ba3d3fSMatthias Ringwald mesh_network_key->beacon_state = MESH_SECURE_NETWORK_BEACON_AUTH_VALUE; 12577ba3d3fSMatthias Ringwald 12677ba3d3fSMatthias Ringwald mesh_secure_network_beacon_run(NULL); 12777ba3d3fSMatthias Ringwald } 12877ba3d3fSMatthias Ringwald 12977ba3d3fSMatthias Ringwald static uint8_t mesh_secure_network_beacon_get_flags(mesh_network_key_t * mesh_network_key){ 13077ba3d3fSMatthias Ringwald uint8_t mesh_flags = 0; 13177ba3d3fSMatthias Ringwald if (mesh_network_key->key_refresh != MESH_KEY_REFRESH_NOT_ACTIVE){ 13277ba3d3fSMatthias Ringwald mesh_flags |= 1; 13377ba3d3fSMatthias Ringwald } 13477ba3d3fSMatthias Ringwald 13577ba3d3fSMatthias Ringwald // TODO: set bit 1 if IV Update is active 13677ba3d3fSMatthias Ringwald return mesh_flags; 13777ba3d3fSMatthias Ringwald } 13877ba3d3fSMatthias Ringwald 13977ba3d3fSMatthias Ringwald static void mesh_secure_network_beacon_setup(mesh_network_key_t * mesh_network_key){ 14077ba3d3fSMatthias Ringwald mesh_beacon_data[0] = BEACON_TYPE_SECURE_NETWORK; 14177ba3d3fSMatthias Ringwald mesh_beacon_data[1] = mesh_secure_network_beacon_get_flags(mesh_network_key); 14277ba3d3fSMatthias Ringwald memcpy(&mesh_beacon_data[2], mesh_network_key->network_id, 8); 14377ba3d3fSMatthias Ringwald big_endian_store_32(mesh_beacon_data, 10, mesh_get_iv_index()); 14477ba3d3fSMatthias Ringwald btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, mesh_network_key->beacon_key, 13, 14577ba3d3fSMatthias Ringwald &mesh_beacon_data[1], mesh_secure_network_beacon_auth_value, &mesh_secure_network_beacon_auth_value_calculated, mesh_network_key); 14677ba3d3fSMatthias Ringwald } 14777ba3d3fSMatthias Ringwald 14877ba3d3fSMatthias Ringwald static void mesh_secure_network_beacon_update_interval(mesh_network_key_t * subnet){ 14977ba3d3fSMatthias Ringwald uint32_t min_observation_period_ms = 2 * subnet->beacon_interval_ms; 15077ba3d3fSMatthias Ringwald uint32_t actual_observation_period = btstack_time_delta(btstack_run_loop_get_time_ms(), subnet->beacon_observation_start_ms); 15177ba3d3fSMatthias Ringwald 15277ba3d3fSMatthias Ringwald // The Observation Period in seconds should typically be double the typical Beacon Interval. 15377ba3d3fSMatthias Ringwald if (actual_observation_period < min_observation_period_ms) return; 15477ba3d3fSMatthias Ringwald 15577ba3d3fSMatthias Ringwald // Expected Number of Beacons (1 beacon per 10 seconds) 15677ba3d3fSMatthias Ringwald uint16_t expected_number_of_beacons = actual_observation_period / SECURE_NETWORK_BEACON_INTERVAL_MIN_MS; 15777ba3d3fSMatthias Ringwald 15877ba3d3fSMatthias Ringwald // Beacon Interval = Observation Period * (Observed Number of Beacons + 1) / Expected Number of Beacons 15977ba3d3fSMatthias Ringwald uint32_t new_beacon_interval = actual_observation_period * (subnet->beacon_observation_counter + 1) / expected_number_of_beacons; 16077ba3d3fSMatthias Ringwald 16177ba3d3fSMatthias Ringwald if (new_beacon_interval > SECURE_NETWORK_BEACON_INTERVAL_MAX_MS){ 16277ba3d3fSMatthias Ringwald new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS; 16377ba3d3fSMatthias Ringwald } 16477ba3d3fSMatthias Ringwald else if (new_beacon_interval < SECURE_NETWORK_BEACON_INTERVAL_MIN_MS){ 16577ba3d3fSMatthias Ringwald new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS; 16677ba3d3fSMatthias Ringwald } 16777ba3d3fSMatthias Ringwald subnet->beacon_interval_ms = new_beacon_interval; 16877ba3d3fSMatthias Ringwald log_info("New beacon interval %u seconds", (int) (subnet->beacon_interval_ms / 1000)); 16977ba3d3fSMatthias Ringwald } 17077ba3d3fSMatthias Ringwald 17177ba3d3fSMatthias Ringwald static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts){ 17277ba3d3fSMatthias Ringwald UNUSED(ts); 17377ba3d3fSMatthias Ringwald 17477ba3d3fSMatthias Ringwald uint32_t next_timeout_ms = 0; 17577ba3d3fSMatthias Ringwald 17677ba3d3fSMatthias Ringwald // iterate over all networks 17777ba3d3fSMatthias Ringwald mesh_network_key_iterator_t it; 17877ba3d3fSMatthias Ringwald mesh_network_key_iterator_init(&it); 17977ba3d3fSMatthias Ringwald while (mesh_network_key_iterator_has_more(&it)){ 18077ba3d3fSMatthias Ringwald mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it); 18177ba3d3fSMatthias Ringwald switch (subnet->beacon_state){ 18277ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_W4_INTERVAL: 18377ba3d3fSMatthias Ringwald // update beacon interval 18477ba3d3fSMatthias Ringwald mesh_secure_network_beacon_update_interval(subnet); 18577ba3d3fSMatthias Ringwald 18677ba3d3fSMatthias Ringwald // send new beacon 18777ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE; 18877ba3d3fSMatthias Ringwald 18977ba3d3fSMatthias Ringwald /** Explict Fall-through */ 19077ba3d3fSMatthias Ringwald 19177ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE: 19277ba3d3fSMatthias Ringwald if (mesh_secure_network_beacon_active){ 19377ba3d3fSMatthias Ringwald // just try again in 10 ms 19477ba3d3fSMatthias Ringwald next_timeout_ms = 10; 19577ba3d3fSMatthias Ringwald break; 19677ba3d3fSMatthias Ringwald } 197*21ed246fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE; 19877ba3d3fSMatthias Ringwald mesh_secure_network_beacon_active = 1; 19977ba3d3fSMatthias Ringwald mesh_secure_network_beacon_setup(subnet); 20077ba3d3fSMatthias Ringwald break; 20177ba3d3fSMatthias Ringwald 20277ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_AUTH_VALUE: 20377ba3d3fSMatthias Ringwald 20477ba3d3fSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 20577ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV; 20677ba3d3fSMatthias Ringwald adv_bearer_request_can_send_now_for_mesh_beacon(); 20777ba3d3fSMatthias Ringwald break; 20877ba3d3fSMatthias Ringwald #endif 20977ba3d3fSMatthias Ringwald 21077ba3d3fSMatthias Ringwald /** Explict Fall-through */ 21177ba3d3fSMatthias Ringwald 21277ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_ADV_SENT: 21377ba3d3fSMatthias Ringwald 21477ba3d3fSMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER 2152931ab39SMatthias Ringwald if (gatt_bearer_con_handle != HCI_CON_HANDLE_INVALID){ 21677ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT; 21777ba3d3fSMatthias Ringwald gatt_bearer_request_can_send_now_for_mesh_beacon(); 2182931ab39SMatthias Ringwald break; 2192931ab39SMatthias Ringwald } 22077ba3d3fSMatthias Ringwald #endif 22177ba3d3fSMatthias Ringwald 22277ba3d3fSMatthias Ringwald /** Explict Fall-through */ 22377ba3d3fSMatthias Ringwald 22477ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_GATT_SENT: 22577ba3d3fSMatthias Ringwald // now, start listening for beacons 22677ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W4_INTERVAL; 22777ba3d3fSMatthias Ringwald // and request timeout 22877ba3d3fSMatthias Ringwald if (next_timeout_ms == 0 || next_timeout_ms > subnet->beacon_interval_ms){ 22977ba3d3fSMatthias Ringwald next_timeout_ms = subnet->beacon_interval_ms; 23077ba3d3fSMatthias Ringwald } 23177ba3d3fSMatthias Ringwald break; 2322931ab39SMatthias Ringwald 23377ba3d3fSMatthias Ringwald default: 23477ba3d3fSMatthias Ringwald break; 23577ba3d3fSMatthias Ringwald } 23677ba3d3fSMatthias Ringwald } 23777ba3d3fSMatthias Ringwald 23877ba3d3fSMatthias Ringwald // setup next run 23977ba3d3fSMatthias Ringwald if (next_timeout_ms == 0) return; 24077ba3d3fSMatthias Ringwald 24177ba3d3fSMatthias Ringwald btstack_run_loop_set_timer(&beacon_timer, 10); 24277ba3d3fSMatthias Ringwald btstack_run_loop_set_timer_handler(&beacon_timer, mesh_secure_network_beacon_run); 24377ba3d3fSMatthias Ringwald btstack_run_loop_add_timer(&beacon_timer); 24477ba3d3fSMatthias Ringwald } 24577ba3d3fSMatthias Ringwald 24677ba3d3fSMatthias Ringwald static void beacon_handle_secure_beacon_auth_value_calculated(void * arg){ 24777ba3d3fSMatthias Ringwald UNUSED(arg); 24877ba3d3fSMatthias Ringwald 24977ba3d3fSMatthias Ringwald // pass on, if auth value checks out 25077ba3d3fSMatthias Ringwald if (memcmp(&mesh_secure_network_beacon_validate_buffer[14], mesh_secure_network_beacon_auth_value, 8) == 0) { 25177ba3d3fSMatthias Ringwald if (mesh_secure_network_beacon_handler){ 25277ba3d3fSMatthias Ringwald (*mesh_secure_network_beacon_handler)(MESH_BEACON_PACKET, 0, mesh_secure_network_beacon_validate_buffer, SECURE_NETWORK_BEACON_LEN); 25377ba3d3fSMatthias Ringwald } 25477ba3d3fSMatthias Ringwald } 25577ba3d3fSMatthias Ringwald 25677ba3d3fSMatthias Ringwald // done 25777ba3d3fSMatthias Ringwald mesh_secure_network_beacon_active = 0; 25877ba3d3fSMatthias Ringwald mesh_secure_network_beacon_run(NULL); 25977ba3d3fSMatthias Ringwald } 26077ba3d3fSMatthias Ringwald 26177ba3d3fSMatthias Ringwald static void beacon_handle_secure_beacon(uint8_t * packet, uint16_t size){ 26277ba3d3fSMatthias Ringwald if (size != SECURE_NETWORK_BEACON_LEN) return; 26377ba3d3fSMatthias Ringwald 26477ba3d3fSMatthias Ringwald // lookup subnet by network id 26577ba3d3fSMatthias Ringwald uint8_t * beacon_network_id = &packet[2]; 26677ba3d3fSMatthias Ringwald mesh_network_key_iterator_t it; 26777ba3d3fSMatthias Ringwald mesh_network_key_iterator_init(&it); 26877ba3d3fSMatthias Ringwald mesh_network_key_t * subnet = NULL; 26977ba3d3fSMatthias Ringwald while (mesh_network_key_iterator_has_more(&it)){ 27077ba3d3fSMatthias Ringwald mesh_network_key_t * item = mesh_network_key_iterator_get_next(&it); 27177ba3d3fSMatthias Ringwald if (memcmp(item->network_id, beacon_network_id, 8) != 0 ) continue; 27277ba3d3fSMatthias Ringwald subnet = item; 27377ba3d3fSMatthias Ringwald break; 27477ba3d3fSMatthias Ringwald } 27577ba3d3fSMatthias Ringwald if (subnet == NULL) return; 27677ba3d3fSMatthias Ringwald 27777ba3d3fSMatthias Ringwald // count beacon 27877ba3d3fSMatthias Ringwald subnet->beacon_observation_counter++; 27977ba3d3fSMatthias Ringwald 28077ba3d3fSMatthias Ringwald // check if new flags are set 28177ba3d3fSMatthias Ringwald uint8_t current_flags = mesh_secure_network_beacon_get_flags(subnet); 28277ba3d3fSMatthias Ringwald uint8_t new_flags = packet[1] & (~current_flags); 28377ba3d3fSMatthias Ringwald 28477ba3d3fSMatthias Ringwald if (new_flags == 0) return; 28577ba3d3fSMatthias Ringwald 28677ba3d3fSMatthias Ringwald // validate beacon - if crytpo ready 28777ba3d3fSMatthias Ringwald if (mesh_secure_network_beacon_active) return; 28877ba3d3fSMatthias Ringwald 28977ba3d3fSMatthias Ringwald mesh_secure_network_beacon_active = 1; 29077ba3d3fSMatthias Ringwald memcpy(mesh_secure_network_beacon_validate_buffer, &packet[0], SECURE_NETWORK_BEACON_LEN); 29177ba3d3fSMatthias Ringwald 29277ba3d3fSMatthias Ringwald btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, subnet->beacon_key, 13, 29377ba3d3fSMatthias Ringwald &mesh_secure_network_beacon_validate_buffer[1], mesh_secure_network_beacon_auth_value, &beacon_handle_secure_beacon_auth_value_calculated, subnet); 29477ba3d3fSMatthias Ringwald } 29577ba3d3fSMatthias Ringwald 29677ba3d3fSMatthias Ringwald static void beacon_handle_beacon_packet(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 29777ba3d3fSMatthias Ringwald log_info("beacon type %u", packet[0]); 29877ba3d3fSMatthias Ringwald switch (packet[0]){ 29977ba3d3fSMatthias Ringwald case BEACON_TYPE_UNPROVISIONED_DEVICE: 30077ba3d3fSMatthias Ringwald if (unprovisioned_device_beacon_handler){ 30177ba3d3fSMatthias Ringwald (*unprovisioned_device_beacon_handler)(packet_type, channel, packet, size); 30277ba3d3fSMatthias Ringwald } 30377ba3d3fSMatthias Ringwald break; 30477ba3d3fSMatthias Ringwald case BEACON_TYPE_SECURE_NETWORK: 30577ba3d3fSMatthias Ringwald beacon_handle_secure_beacon(packet, size); 30677ba3d3fSMatthias Ringwald break; 30777ba3d3fSMatthias Ringwald default: 30877ba3d3fSMatthias Ringwald break; 30977ba3d3fSMatthias Ringwald } 31077ba3d3fSMatthias Ringwald } 31177ba3d3fSMatthias Ringwald 312b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 31377ba3d3fSMatthias Ringwald static void beacon_adv_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 31477ba3d3fSMatthias Ringwald mesh_network_key_iterator_t it; 31577ba3d3fSMatthias Ringwald switch (packet_type){ 31677ba3d3fSMatthias Ringwald case HCI_EVENT_PACKET: 31777ba3d3fSMatthias Ringwald switch(packet[0]){ 31877ba3d3fSMatthias Ringwald case HCI_EVENT_MESH_META: 31977ba3d3fSMatthias Ringwald switch(packet[2]){ 32077ba3d3fSMatthias Ringwald case MESH_SUBEVENT_CAN_SEND_NOW: 32177ba3d3fSMatthias Ringwald if (beacon_send_device_beacon){ 32277ba3d3fSMatthias Ringwald beacon_send_device_beacon = 0; 32377ba3d3fSMatthias Ringwald adv_bearer_send_mesh_beacon(mesh_beacon_data, mesh_beacon_len); 32477ba3d3fSMatthias Ringwald break; 32577ba3d3fSMatthias Ringwald } 32677ba3d3fSMatthias Ringwald // secure beacon state machine 32777ba3d3fSMatthias Ringwald mesh_network_key_iterator_init(&it); 32877ba3d3fSMatthias Ringwald while (mesh_network_key_iterator_has_more(&it)){ 32977ba3d3fSMatthias Ringwald mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it); 33077ba3d3fSMatthias Ringwald switch (subnet->beacon_state){ 33177ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV: 33277ba3d3fSMatthias Ringwald adv_bearer_send_mesh_beacon(mesh_beacon_data, mesh_beacon_len); 33377ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_ADV_SENT; 33477ba3d3fSMatthias Ringwald mesh_secure_network_beacon_run(NULL); 33577ba3d3fSMatthias Ringwald break; 33677ba3d3fSMatthias Ringwald default: 33777ba3d3fSMatthias Ringwald break; 33877ba3d3fSMatthias Ringwald } 33977ba3d3fSMatthias Ringwald } 34077ba3d3fSMatthias Ringwald break; 34177ba3d3fSMatthias Ringwald default: 34277ba3d3fSMatthias Ringwald break; 34377ba3d3fSMatthias Ringwald } 34477ba3d3fSMatthias Ringwald break; 34577ba3d3fSMatthias Ringwald default: 34677ba3d3fSMatthias Ringwald break; 34777ba3d3fSMatthias Ringwald } 34877ba3d3fSMatthias Ringwald break; 34977ba3d3fSMatthias Ringwald case MESH_BEACON_PACKET: 35077ba3d3fSMatthias Ringwald beacon_handle_beacon_packet(packet_type, channel, packet, size); 35177ba3d3fSMatthias Ringwald break; 35277ba3d3fSMatthias Ringwald default: 35377ba3d3fSMatthias Ringwald break; 35477ba3d3fSMatthias Ringwald } 35577ba3d3fSMatthias Ringwald } 356b3a527ecSMatthias Ringwald #endif 35777ba3d3fSMatthias Ringwald 35877ba3d3fSMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER 359*21ed246fSMatthias Ringwald // handle MESH_SUBEVENT_PROXY_DISCONNECTED and MESH_SUBEVENT_CAN_SEND_NOW 360*21ed246fSMatthias Ringwald static void beacon_gatt_handle_mesh_event(uint8_t mesh_subevent){ 36177ba3d3fSMatthias Ringwald mesh_network_key_iterator_t it; 36277ba3d3fSMatthias Ringwald mesh_network_key_iterator_init(&it); 36377ba3d3fSMatthias Ringwald while (mesh_network_key_iterator_has_more(&it)){ 36477ba3d3fSMatthias Ringwald mesh_network_key_t * subnet = mesh_network_key_iterator_get_next(&it); 36577ba3d3fSMatthias Ringwald switch (subnet->beacon_state){ 36677ba3d3fSMatthias Ringwald case MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT: 367*21ed246fSMatthias Ringwald // skip send on MESH_SUBEVENT_PROXY_DISCONNECTED 368*21ed246fSMatthias Ringwald if (mesh_subevent == MESH_SUBEVENT_CAN_SEND_NOW){ 36977ba3d3fSMatthias Ringwald gatt_bearer_send_mesh_beacon(mesh_beacon_data, mesh_beacon_len); 370*21ed246fSMatthias Ringwald } 37177ba3d3fSMatthias Ringwald subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_GATT_SENT; 37277ba3d3fSMatthias Ringwald mesh_secure_network_beacon_run(NULL); 37377ba3d3fSMatthias Ringwald break; 37477ba3d3fSMatthias Ringwald default: 37577ba3d3fSMatthias Ringwald break; 37677ba3d3fSMatthias Ringwald } 37777ba3d3fSMatthias Ringwald } 378*21ed246fSMatthias Ringwald 379*21ed246fSMatthias Ringwald } 380*21ed246fSMatthias Ringwald 381*21ed246fSMatthias Ringwald static void beacon_gatt_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 382*21ed246fSMatthias Ringwald uint8_t mesh_subevent; 383*21ed246fSMatthias Ringwald switch (packet_type){ 384*21ed246fSMatthias Ringwald case HCI_EVENT_PACKET: 385*21ed246fSMatthias Ringwald switch(packet[0]){ 386*21ed246fSMatthias Ringwald case HCI_EVENT_MESH_META: 387*21ed246fSMatthias Ringwald mesh_subevent = packet[2]; 388*21ed246fSMatthias Ringwald switch(mesh_subevent){ 389*21ed246fSMatthias Ringwald case MESH_SUBEVENT_PROXY_CONNECTED: 390*21ed246fSMatthias Ringwald gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet); 391*21ed246fSMatthias Ringwald break; 392*21ed246fSMatthias Ringwald case MESH_SUBEVENT_PROXY_DISCONNECTED: 393*21ed246fSMatthias Ringwald gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 394*21ed246fSMatthias Ringwald beacon_gatt_handle_mesh_event(mesh_subevent); 395*21ed246fSMatthias Ringwald break; 396*21ed246fSMatthias Ringwald case MESH_SUBEVENT_CAN_SEND_NOW: 397*21ed246fSMatthias Ringwald beacon_gatt_handle_mesh_event(mesh_subevent); 39877ba3d3fSMatthias Ringwald break; 39977ba3d3fSMatthias Ringwald default: 40077ba3d3fSMatthias Ringwald break; 40177ba3d3fSMatthias Ringwald } 40277ba3d3fSMatthias Ringwald break; 40377ba3d3fSMatthias Ringwald default: 40477ba3d3fSMatthias Ringwald break; 40577ba3d3fSMatthias Ringwald } 40677ba3d3fSMatthias Ringwald break; 40777ba3d3fSMatthias Ringwald case MESH_BEACON_PACKET: 40877ba3d3fSMatthias Ringwald beacon_handle_beacon_packet(packet_type, channel, packet, size); 40977ba3d3fSMatthias Ringwald break; 41077ba3d3fSMatthias Ringwald default: 41177ba3d3fSMatthias Ringwald break; 41277ba3d3fSMatthias Ringwald } 41377ba3d3fSMatthias Ringwald } 41477ba3d3fSMatthias Ringwald #endif 41577ba3d3fSMatthias Ringwald 41677ba3d3fSMatthias Ringwald void beacon_init(void){ 417b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 41877ba3d3fSMatthias Ringwald adv_bearer_register_for_mesh_beacon(&beacon_adv_packet_handler); 419b3a527ecSMatthias Ringwald #endif 42077ba3d3fSMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER 421b3a527ecSMatthias Ringwald gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 42277ba3d3fSMatthias Ringwald gatt_bearer_register_for_mesh_beacon(&beacon_gatt_packet_handler); 42377ba3d3fSMatthias Ringwald #endif 42477ba3d3fSMatthias Ringwald } 42577ba3d3fSMatthias Ringwald 42677ba3d3fSMatthias Ringwald /** 42777ba3d3fSMatthias Ringwald * Start Unprovisioned Device Beacon 42877ba3d3fSMatthias Ringwald */ 42977ba3d3fSMatthias Ringwald void beacon_unprovisioned_device_start(const uint8_t * device_uuid, uint16_t oob_information){ 430b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 43177ba3d3fSMatthias Ringwald beacon_oob_information = oob_information; 43277ba3d3fSMatthias Ringwald if (device_uuid){ 43377ba3d3fSMatthias Ringwald beacon_device_uuid = device_uuid; 43477ba3d3fSMatthias Ringwald beacon_timer.process = &beacon_timer_handler; 43577ba3d3fSMatthias Ringwald beacon_timer_handler(&beacon_timer); 43677ba3d3fSMatthias Ringwald } 437b3a527ecSMatthias Ringwald #endif 43877ba3d3fSMatthias Ringwald } 43977ba3d3fSMatthias Ringwald 44077ba3d3fSMatthias Ringwald /** 44177ba3d3fSMatthias Ringwald * Stop Unprovisioned Device Beacon 44277ba3d3fSMatthias Ringwald */ 44377ba3d3fSMatthias Ringwald void beacon_unprovisioned_device_stop(void){ 444b3a527ecSMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER 44577ba3d3fSMatthias Ringwald btstack_run_loop_remove_timer(&beacon_timer); 446b3a527ecSMatthias Ringwald #endif 44777ba3d3fSMatthias Ringwald } 44877ba3d3fSMatthias Ringwald 44977ba3d3fSMatthias Ringwald // secure network beacons 45077ba3d3fSMatthias Ringwald 45177ba3d3fSMatthias Ringwald void beacon_secure_network_start(mesh_network_key_t * mesh_network_key){ 45277ba3d3fSMatthias Ringwald // default interval 45377ba3d3fSMatthias Ringwald mesh_network_key->beacon_interval_ms = SECURE_NETWORK_BEACON_INTERVAL_MIN_MS; 45477ba3d3fSMatthias Ringwald mesh_network_key->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE; 45577ba3d3fSMatthias Ringwald mesh_network_key->beacon_observation_start_ms = btstack_run_loop_get_time_ms(); 45677ba3d3fSMatthias Ringwald mesh_network_key->beacon_observation_counter = 0; 45777ba3d3fSMatthias Ringwald 45877ba3d3fSMatthias Ringwald // start sending 45977ba3d3fSMatthias Ringwald mesh_secure_network_beacon_run(NULL); 46077ba3d3fSMatthias Ringwald } 46177ba3d3fSMatthias Ringwald 46277ba3d3fSMatthias Ringwald // register handler 46377ba3d3fSMatthias Ringwald void beacon_register_for_unprovisioned_device_beacons(btstack_packet_handler_t packet_handler){ 46477ba3d3fSMatthias Ringwald unprovisioned_device_beacon_handler = packet_handler; 46577ba3d3fSMatthias Ringwald } 46677ba3d3fSMatthias Ringwald 46777ba3d3fSMatthias Ringwald void beacon_register_for_secure_network_beacons(btstack_packet_handler_t packet_handler){ 46877ba3d3fSMatthias Ringwald mesh_secure_network_beacon_handler = packet_handler; 46977ba3d3fSMatthias Ringwald } 470