From d40f66434289ce27129e17d5f6c091f9997ba920 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:22:14 +0300 Subject: [PATCH] API for BLE Beacon app thanks to Willy-JL --- firmware/targets/f7/api_symbols.csv | 2 + firmware/targets/f7/ble_glue/gap.c | 52 ++++++++++++++----- firmware/targets/f7/ble_glue/gap.h | 2 + firmware/targets/f7/furi_hal/furi_hal_bt.c | 7 +++ .../targets/furi_hal_include/furi_hal_bt.h | 3 ++ 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/firmware/targets/f7/api_symbols.csv b/firmware/targets/f7/api_symbols.csv index 108605593..de973a440 100644 --- a/firmware/targets/f7/api_symbols.csv +++ b/firmware/targets/f7/api_symbols.csv @@ -1079,6 +1079,7 @@ Function,+,furi_hal_bt_serial_set_rpc_status,void,FuriHalBtSerialRpcStatus Function,+,furi_hal_bt_serial_start,void, Function,+,furi_hal_bt_serial_stop,void, Function,+,furi_hal_bt_serial_tx,_Bool,"uint8_t*, uint16_t" +Function,+,furi_hal_bt_set_custom_adv_data,void,"const uint8_t*, size_t" Function,+,furi_hal_bt_set_key_storage_change_callback,void,"BleGlueKeyStorageChangedCallback, void*" Function,+,furi_hal_bt_set_profile_adv_name,void,"FuriHalBtProfile, const char[( ( 1 + 8 + ( 8 + 1 ) ) + 1 )]" Function,+,furi_hal_bt_set_profile_mac_addr,void,"FuriHalBtProfile, const uint8_t[( 6 )]" @@ -1662,6 +1663,7 @@ Function,-,gammaf_r,float,"float, int*" Function,-,gap_get_remote_conn_rssi,uint32_t,int8_t* Function,-,gap_get_state,GapState, Function,-,gap_init,_Bool,"GapConfig*, GapEventCallback, void*" +Function,-,gap_set_custom_adv_data,void,"size_t, const uint8_t*" Function,-,gap_start_advertising,void, Function,-,gap_stop_advertising,void, Function,-,gap_thread_stop,void, diff --git a/firmware/targets/f7/ble_glue/gap.c b/firmware/targets/f7/ble_glue/gap.c index ab7b9d16c..6fedb54cf 100644 --- a/firmware/targets/f7/ble_glue/gap.c +++ b/firmware/targets/f7/ble_glue/gap.c @@ -37,6 +37,9 @@ typedef struct { FuriThread* thread; FuriMessageQueue* command_queue; bool enable_adv; + // API for BLE beacon plugin + size_t custom_adv_len; + const uint8_t* custom_adv_data; } Gap; typedef enum { @@ -430,18 +433,36 @@ static void gap_advertise_start(GapState new_state) { } } // Configure advertising - status = aci_gap_set_discoverable( - ADV_IND, - min_interval, - max_interval, - CFG_IDENTITY_ADDRESS, - 0, - strlen(gap->service.adv_name), - (uint8_t*)gap->service.adv_name, - gap->service.adv_svc_uuid_len, - gap->service.adv_svc_uuid, - 0, - 0); + // API For BLE beacon plugin + if(gap->custom_adv_data) { + // Custom adv logic from https://techryptic.github.io/2023/09/01/Annoying-Apple-Fans/ + static const uint16_t gap_appearance = 0x0000; //GAP_APPEARANCE_UNKNOWN + status = aci_gatt_update_char_value( + gap->service.gap_svc_handle, + gap->service.gap_svc_handle, + 0, + sizeof(gap_appearance), + (uint8_t*)&gap_appearance); + status = aci_gap_set_discoverable( + ADV_IND, min_interval, max_interval, CFG_IDENTITY_ADDRESS, 0, 0, NULL, 0, NULL, 0, 0); + status = aci_gap_delete_ad_type(AD_TYPE_FLAGS); + status = aci_gap_delete_ad_type(AD_TYPE_TX_POWER_LEVEL); + status = aci_gap_update_adv_data(gap->custom_adv_len, gap->custom_adv_data); + } else { + // Default adv logic + status = aci_gap_set_discoverable( + ADV_IND, + min_interval, + max_interval, + CFG_IDENTITY_ADDRESS, + 0, + strlen(gap->service.adv_name), + (uint8_t*)gap->service.adv_name, + gap->service.adv_svc_uuid_len, + gap->service.adv_svc_uuid, + 0, + 0); + } if(status) { FURI_LOG_E(TAG, "set_discoverable failed %d", status); } @@ -559,6 +580,13 @@ uint32_t gap_get_remote_conn_rssi(int8_t* rssi) { return 0; } +// API For BLE beacon plugin + +void gap_set_custom_adv_data(size_t adv_len, const uint8_t* adv_data) { + gap->custom_adv_len = adv_len; + gap->custom_adv_data = adv_data; +} + GapState gap_get_state() { GapState state; if(gap) { diff --git a/firmware/targets/f7/ble_glue/gap.h b/firmware/targets/f7/ble_glue/gap.h index 396d64e67..27bbc1cf0 100644 --- a/firmware/targets/f7/ble_glue/gap.h +++ b/firmware/targets/f7/ble_glue/gap.h @@ -83,6 +83,8 @@ void gap_thread_stop(); uint32_t gap_get_remote_conn_rssi(int8_t* rssi); +void gap_set_custom_adv_data(size_t adv_len, const uint8_t* adv_data); + #ifdef __cplusplus } #endif diff --git a/firmware/targets/f7/furi_hal/furi_hal_bt.c b/firmware/targets/f7/furi_hal/furi_hal_bt.c index 5a150d388..9fab0164a 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_bt.c +++ b/firmware/targets/f7/furi_hal/furi_hal_bt.c @@ -483,6 +483,13 @@ uint32_t furi_hal_bt_get_conn_rssi(uint8_t* rssi) { return since; } +// API for BLE beacon plugin +void furi_hal_bt_set_custom_adv_data(const uint8_t* adv_data, size_t adv_len) { + gap_set_custom_adv_data(adv_len, adv_data); + furi_hal_bt_stop_advertising(); + furi_hal_bt_start_advertising(); +} + void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) { uint8_t tmp; for(size_t i = 0; i < GAP_MAC_ADDR_SIZE / 2; i++) { diff --git a/firmware/targets/furi_hal_include/furi_hal_bt.h b/firmware/targets/furi_hal_include/furi_hal_bt.h index 7354d8770..9ef2c7646 100644 --- a/firmware/targets/furi_hal_include/furi_hal_bt.h +++ b/firmware/targets/furi_hal_include/furi_hal_bt.h @@ -252,6 +252,9 @@ const uint8_t* furi_hal_bt_get_profile_mac_addr(FuriHalBtProfile profile); uint32_t furi_hal_bt_get_conn_rssi(uint8_t* rssi); +// API for BLE Beacon plugin +void furi_hal_bt_set_custom_adv_data(const uint8_t* adv_data, size_t adv_len); + void furi_hal_bt_set_profile_pairing_method(FuriHalBtProfile profile, GapPairing pairing_method); GapPairing furi_hal_bt_get_profile_pairing_method(FuriHalBtProfile profile);