Skip to content

Commit 5786066

Browse files
bettsehedger
andauthored
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 <[email protected]>
1 parent bd1e395 commit 5786066

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

lib/ble_profile/extra_profiles/hid_profile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ bool ble_profile_hid_mouse_scroll(FuriHalBleProfileBase* profile, int8_t delta)
380380
#define CONNECTION_INTERVAL_MAX (0x24)
381381

382382
static GapConfig template_config = {
383-
.adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
383+
.adv_service = {
384+
.UUID_Type = UUID_TYPE_16,
385+
.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
386+
},
384387
.appearance_char = GAP_APPEARANCE_KEYBOARD,
385388
.bonding_mode = true,
386389
.pairing_method = GapPairingPinCodeVerifyYesNo,

targets/f7/ble_glue/gap.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ typedef struct {
2323
uint16_t connection_handle;
2424
uint8_t adv_svc_uuid_len;
2525
uint8_t adv_svc_uuid[20];
26+
uint8_t mfg_data_len;
27+
uint8_t mfg_data[20];
2628
char* adv_name;
2729
} GapSvc;
2830

@@ -198,8 +200,10 @@ BleEventFlowStatus ble_event_app_notification(void* pckt) {
198200
gap->service.connection_handle = event->Connection_Handle;
199201

200202
gap_verify_connection_parameters(gap);
201-
// Start pairing by sending security request
202-
aci_gap_slave_security_req(event->Connection_Handle);
203+
if(gap->config->pairing_method != GapPairingNone) {
204+
// Start pairing by sending security request
205+
aci_gap_slave_security_req(event->Connection_Handle);
206+
}
203207
} break;
204208

205209
default:
@@ -321,6 +325,14 @@ static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
321325
gap->service.adv_svc_uuid_len += uid_len;
322326
}
323327

328+
static void set_manufacturer_data(uint8_t* mfg_data, uint8_t mfg_data_len) {
329+
furi_check(mfg_data_len < sizeof(gap->service.mfg_data) - 2);
330+
gap->service.mfg_data[0] = mfg_data_len + 1;
331+
gap->service.mfg_data[1] = AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
332+
memcpy(&gap->service.mfg_data[gap->service.mfg_data_len], mfg_data, mfg_data_len);
333+
gap->service.mfg_data_len += mfg_data_len;
334+
}
335+
324336
static void gap_init_svc(Gap* gap) {
325337
tBleStatus status;
326338
uint32_t srd_bd_addr[2];
@@ -440,6 +452,11 @@ static void gap_advertise_start(GapState new_state) {
440452
FURI_LOG_D(TAG, "set_non_discoverable success");
441453
}
442454
}
455+
456+
if(gap->service.mfg_data_len > 0) {
457+
hci_le_set_scan_response_data(gap->service.mfg_data_len, gap->service.mfg_data);
458+
}
459+
443460
// Configure advertising
444461
status = aci_gap_set_discoverable(
445462
ADV_IND,
@@ -550,11 +567,26 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
550567
gap->is_secure = false;
551568
gap->negotiation_round = 0;
552569

553-
uint8_t adv_service_uid[2];
554-
gap->service.adv_svc_uuid_len = 1;
555-
adv_service_uid[0] = gap->config->adv_service_uuid & 0xff;
556-
adv_service_uid[1] = gap->config->adv_service_uuid >> 8;
557-
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
570+
if(gap->config->mfg_data_len > 0) {
571+
// Offset by 2 for length + AD_TYPE_MANUFACTURER_SPECIFIC_DATA
572+
gap->service.mfg_data_len = 2;
573+
set_manufacturer_data(gap->config->mfg_data, gap->config->mfg_data_len);
574+
}
575+
576+
if(gap->config->adv_service.UUID_Type == UUID_TYPE_16) {
577+
uint8_t adv_service_uid[2];
578+
gap->service.adv_svc_uuid_len = 1;
579+
adv_service_uid[0] = gap->config->adv_service.Service_UUID_16 & 0xff;
580+
adv_service_uid[1] = gap->config->adv_service.Service_UUID_16 >> 8;
581+
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
582+
} else if(gap->config->adv_service.UUID_Type == UUID_TYPE_128) {
583+
gap->service.adv_svc_uuid_len = 1;
584+
set_advertisment_service_uid(
585+
gap->config->adv_service.Service_UUID_128,
586+
sizeof(gap->config->adv_service.Service_UUID_128));
587+
} else {
588+
furi_crash("Invalid UUID type");
589+
}
558590

559591
// Set callback
560592
gap->on_event_cb = on_event_cb;

targets/f7/ble_glue/gap.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ typedef struct {
6868
} GapConnectionParamsRequest;
6969

7070
typedef struct {
71-
uint16_t adv_service_uuid;
71+
struct {
72+
uint8_t UUID_Type;
73+
uint16_t Service_UUID_16;
74+
uint8_t Service_UUID_128[16];
75+
} adv_service;
76+
uint8_t mfg_data[20];
77+
uint8_t mfg_data_len;
7278
uint16_t appearance_char;
7379
bool bonding_mode;
7480
GapPairing pairing_method;

targets/f7/ble_glue/profiles/serial_profile.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <services/battery_service.h>
77
#include <services/serial_service.h>
88
#include <furi.h>
9+
#include <ble/core/ble_defs.h>
910

1011
typedef struct {
1112
FuriHalBleProfileBase base;
@@ -47,7 +48,11 @@ static void ble_profile_serial_stop(FuriHalBleProfileBase* profile) {
4748
#define CONNECTION_INTERVAL_MAX (0x24)
4849

4950
static const GapConfig serial_template_config = {
50-
.adv_service_uuid = 0x3080,
51+
.adv_service =
52+
{
53+
.UUID_Type = UUID_TYPE_16,
54+
.Service_UUID_16 = 0x3080,
55+
},
5156
.appearance_char = 0x8600,
5257
.bonding_mode = true,
5358
.pairing_method = GapPairingPinCodeShow,
@@ -71,7 +76,8 @@ static void
7176
config->adv_name,
7277
furi_hal_version_get_ble_local_device_name_ptr(),
7378
FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
74-
config->adv_service_uuid |= furi_hal_version_get_hw_color();
79+
config->adv_service.UUID_Type = UUID_TYPE_16;
80+
config->adv_service.Service_UUID_16 |= furi_hal_version_get_hw_color();
7581
}
7682

7783
static const FuriHalBleProfileTemplate profile_callbacks = {

0 commit comments

Comments
 (0)