mirror of
https://github.com/flipperdevices/flipperzero-firmware.git
synced 2025-12-12 04:41:26 +04:00
BLE advertising improvements (#4151)
* Support longer advertised BLE UUID * BLE: support manufacturer data * Don't pair when GapPairingNone * Add PR feedback --------- Co-authored-by: hedger <hedger@users.noreply.github.com>
This commit is contained in:
@@ -380,7 +380,10 @@ bool ble_profile_hid_mouse_scroll(FuriHalBleProfileBase* profile, int8_t delta)
|
||||
#define CONNECTION_INTERVAL_MAX (0x24)
|
||||
|
||||
static GapConfig template_config = {
|
||||
.adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
|
||||
.adv_service = {
|
||||
.UUID_Type = UUID_TYPE_16,
|
||||
.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
|
||||
},
|
||||
.appearance_char = GAP_APPEARANCE_KEYBOARD,
|
||||
.bonding_mode = true,
|
||||
.pairing_method = GapPairingPinCodeVerifyYesNo,
|
||||
|
||||
@@ -23,6 +23,8 @@ typedef struct {
|
||||
uint16_t connection_handle;
|
||||
uint8_t adv_svc_uuid_len;
|
||||
uint8_t adv_svc_uuid[20];
|
||||
uint8_t mfg_data_len;
|
||||
uint8_t mfg_data[20];
|
||||
char* adv_name;
|
||||
} GapSvc;
|
||||
|
||||
@@ -198,8 +200,10 @@ BleEventFlowStatus ble_event_app_notification(void* pckt) {
|
||||
gap->service.connection_handle = event->Connection_Handle;
|
||||
|
||||
gap_verify_connection_parameters(gap);
|
||||
// Start pairing by sending security request
|
||||
aci_gap_slave_security_req(event->Connection_Handle);
|
||||
if(gap->config->pairing_method != GapPairingNone) {
|
||||
// Start pairing by sending security request
|
||||
aci_gap_slave_security_req(event->Connection_Handle);
|
||||
}
|
||||
} break;
|
||||
|
||||
default:
|
||||
@@ -321,6 +325,14 @@ static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
|
||||
gap->service.adv_svc_uuid_len += uid_len;
|
||||
}
|
||||
|
||||
static void set_manufacturer_data(uint8_t* mfg_data, uint8_t mfg_data_len) {
|
||||
furi_check(mfg_data_len < sizeof(gap->service.mfg_data) - 2);
|
||||
gap->service.mfg_data[0] = mfg_data_len + 1;
|
||||
gap->service.mfg_data[1] = AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
|
||||
memcpy(&gap->service.mfg_data[gap->service.mfg_data_len], mfg_data, mfg_data_len);
|
||||
gap->service.mfg_data_len += mfg_data_len;
|
||||
}
|
||||
|
||||
static void gap_init_svc(Gap* gap) {
|
||||
tBleStatus status;
|
||||
uint32_t srd_bd_addr[2];
|
||||
@@ -440,6 +452,11 @@ static void gap_advertise_start(GapState new_state) {
|
||||
FURI_LOG_D(TAG, "set_non_discoverable success");
|
||||
}
|
||||
}
|
||||
|
||||
if(gap->service.mfg_data_len > 0) {
|
||||
hci_le_set_scan_response_data(gap->service.mfg_data_len, gap->service.mfg_data);
|
||||
}
|
||||
|
||||
// Configure advertising
|
||||
status = aci_gap_set_discoverable(
|
||||
ADV_IND,
|
||||
@@ -550,11 +567,26 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
|
||||
gap->is_secure = false;
|
||||
gap->negotiation_round = 0;
|
||||
|
||||
uint8_t adv_service_uid[2];
|
||||
gap->service.adv_svc_uuid_len = 1;
|
||||
adv_service_uid[0] = gap->config->adv_service_uuid & 0xff;
|
||||
adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
|
||||
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
|
||||
if(gap->config->mfg_data_len > 0) {
|
||||
// Offset by 2 for length + AD_TYPE_MANUFACTURER_SPECIFIC_DATA
|
||||
gap->service.mfg_data_len = 2;
|
||||
set_manufacturer_data(gap->config->mfg_data, gap->config->mfg_data_len);
|
||||
}
|
||||
|
||||
if(gap->config->adv_service.UUID_Type == UUID_TYPE_16) {
|
||||
uint8_t adv_service_uid[2];
|
||||
gap->service.adv_svc_uuid_len = 1;
|
||||
adv_service_uid[0] = gap->config->adv_service.Service_UUID_16 & 0xff;
|
||||
adv_service_uid[1] = gap->config->adv_service.Service_UUID_16 >> 8;
|
||||
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
|
||||
} else if(gap->config->adv_service.UUID_Type == UUID_TYPE_128) {
|
||||
gap->service.adv_svc_uuid_len = 1;
|
||||
set_advertisment_service_uid(
|
||||
gap->config->adv_service.Service_UUID_128,
|
||||
sizeof(gap->config->adv_service.Service_UUID_128));
|
||||
} else {
|
||||
furi_crash("Invalid UUID type");
|
||||
}
|
||||
|
||||
// Set callback
|
||||
gap->on_event_cb = on_event_cb;
|
||||
|
||||
@@ -68,7 +68,13 @@ typedef struct {
|
||||
} GapConnectionParamsRequest;
|
||||
|
||||
typedef struct {
|
||||
uint16_t adv_service_uuid;
|
||||
struct {
|
||||
uint8_t UUID_Type;
|
||||
uint16_t Service_UUID_16;
|
||||
uint8_t Service_UUID_128[16];
|
||||
} adv_service;
|
||||
uint8_t mfg_data[20];
|
||||
uint8_t mfg_data_len;
|
||||
uint16_t appearance_char;
|
||||
bool bonding_mode;
|
||||
GapPairing pairing_method;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <services/battery_service.h>
|
||||
#include <services/serial_service.h>
|
||||
#include <furi.h>
|
||||
#include <ble/core/ble_defs.h>
|
||||
|
||||
typedef struct {
|
||||
FuriHalBleProfileBase base;
|
||||
@@ -47,7 +48,11 @@ static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
|
||||
#define CONNECTION_INTERVAL_MAX (0x24)
|
||||
|
||||
static const GapConfig serial_template_config = {
|
||||
.adv_service_uuid = 0x3080,
|
||||
.adv_service =
|
||||
{
|
||||
.UUID_Type = UUID_TYPE_16,
|
||||
.Service_UUID_16 = 0x3080,
|
||||
},
|
||||
.appearance_char = 0x8600,
|
||||
.bonding_mode = true,
|
||||
.pairing_method = GapPairingPinCodeShow,
|
||||
@@ -71,7 +76,8 @@ static void
|
||||
config->adv_name,
|
||||
furi_hal_version_get_ble_local_device_name_ptr(),
|
||||
FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
|
||||
config->adv_service_uuid |= furi_hal_version_get_hw_color();
|
||||
config->adv_service.UUID_Type = UUID_TYPE_16;
|
||||
config->adv_service.Service_UUID_16 |= furi_hal_version_get_hw_color();
|
||||
}
|
||||
|
||||
static const FuriHalBleProfileTemplate profile_callbacks = {
|
||||
|
||||
Reference in New Issue
Block a user