Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FL-3920] WIP: fix lost BadBLE keystrokes #3993

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/ble_profile/extra_services/hid_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#define TAG "BleHid"

#define BLE_SVC_HID_REPORT_MAP_MAX_LEN (255)
#define BLE_SVC_HID_REPORT_MAX_LEN (255)
#define BLE_SVC_HID_REPORT_REF_LEN (2)
#define BLE_SVC_HID_INFO_LEN (4)
#define BLE_SVC_HID_CONTROL_POINT_LEN (1)
#define BLE_SVC_HID_REPORT_MAX_LEN (255)
#define BLE_SVC_HID_REPORT_REF_LEN (2)
#define BLE_SVC_HID_INFO_LEN (4)
#define BLE_SVC_HID_CONTROL_POINT_LEN (1)

#define BLE_SVC_HID_INPUT_REPORT_COUNT (3)
#define BLE_SVC_HID_OUTPUT_REPORT_COUNT (0)
#define BLE_SVC_HID_INPUT_REPORT_COUNT (3)
#define BLE_SVC_HID_OUTPUT_REPORT_COUNT (0)
#define BLE_SVC_HID_FEATURE_REPORT_COUNT (0)
#define BLE_SVC_HID_REPORT_COUNT \
(BLE_SVC_HID_INPUT_REPORT_COUNT + BLE_SVC_HID_OUTPUT_REPORT_COUNT + \
Expand Down Expand Up @@ -157,6 +157,7 @@ static BleEventAckStatus ble_svc_hid_event_handler(void* event, void* context) {
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
// aci_gatt_attribute_modified_event_rp0* attribute_modified;

if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
// Process modification events
Expand Down Expand Up @@ -274,6 +275,7 @@ bool ble_svc_hid_update_input_report(
.data_ptr = data,
.data_len = len,
};

return ble_gatt_characteristic_update(
hid_svc->svc_handle, &hid_svc->input_report_chars[input_report_num], &report_data);
}
Expand Down
3 changes: 2 additions & 1 deletion targets/f18/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,78.1,,
Version,+,78.2,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
Header,+,applications/services/cli/cli.h,,
Expand Down Expand Up @@ -1711,6 +1711,7 @@ Function,-,gap_init,_Bool,"GapConfig*, GapEventCallback, void*"
Function,-,gap_start_advertising,void,
Function,-,gap_stop_advertising,void,
Function,-,gap_thread_stop,void,
Function,-,gap_wait_for_tx_pool_acailable,void,FuriWait
Function,-,getc,int,FILE*
Function,-,getc_unlocked,int,FILE*
Function,-,getchar,int,
Expand Down
3 changes: 2 additions & 1 deletion targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,78.1,,
Version,+,78.2,,
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
Expand Down Expand Up @@ -1930,6 +1930,7 @@ Function,-,gap_init,_Bool,"GapConfig*, GapEventCallback, void*"
Function,-,gap_start_advertising,void,
Function,-,gap_stop_advertising,void,
Function,-,gap_thread_stop,void,
Function,-,gap_wait_for_tx_pool_acailable,void,FuriWait
Function,-,getc,int,FILE*
Function,-,getc_unlocked,int,FILE*
Function,-,getchar,int,
Expand Down
20 changes: 19 additions & 1 deletion targets/f7/ble_glue/furi_ble/gatt.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "gatt.h"
#include "gap.h"
#include <ble/ble.h>

#include <furi.h>
Expand Down Expand Up @@ -42,6 +43,7 @@ void ble_gatt_characteristic_init(
&char_instance->handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add %s char: %d", char_descriptor->name, status);
furi_assert(false, "Failed to add characteristic");
}

char_instance->descriptor_handle = 0;
Expand All @@ -68,6 +70,7 @@ void ble_gatt_characteristic_init(
&char_instance->descriptor_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add %s char descriptor: %d", char_descriptor->name, status);
furi_assert(false, "Failed to add characteristic descriptor");
}
if(release_data) {
free((void*)char_data);
Expand All @@ -82,6 +85,7 @@ void ble_gatt_characteristic_delete(
if(status) {
FURI_LOG_E(
TAG, "Failed to delete %s char: %d", char_instance->characteristic->name, status);
furi_assert(false, "Failed to delete characteristic");
}
free((void*)char_instance->characteristic);
}
Expand Down Expand Up @@ -114,11 +118,23 @@ bool ble_gatt_characteristic_update(
tBleStatus result = aci_gatt_update_char_value(
svc_handle, char_instance->handle, 0, char_data_size, char_data);
if(result) {
FURI_LOG_E(TAG, "Failed updating %s characteristic: %d", char_descriptor->name, result);
if(result == BLE_STATUS_INSUFFICIENT_RESOURCES) {
FURI_LOG_E(TAG, "Insufficient resources for %s characteristic", char_descriptor->name);
gap_wait_for_tx_pool_acailable(1000); // 1 second timeout
result = aci_gatt_update_char_value(
svc_handle, char_instance->handle, 0, char_data_size, char_data);
}
}

if(release_data) {
free((void*)char_data);
}

if(result != BLE_STATUS_SUCCESS) {
FURI_LOG_E(TAG, "Failed updating %s characteristic: %d", char_descriptor->name, result);
furi_assert(false, "Failed to update characteristic");
}

return result != BLE_STATUS_SUCCESS;
}

Expand All @@ -132,6 +148,7 @@ bool ble_gatt_service_add(
Service_UUID_Type, Service_UUID, Service_Type, Max_Attribute_Records, Service_Handle);
if(result) {
FURI_LOG_E(TAG, "Failed to add service: %x", result);
furi_assert(false, "Failed to add service");
}

return result == BLE_STATUS_SUCCESS;
Expand All @@ -141,6 +158,7 @@ bool ble_gatt_service_delete(uint16_t svc_handle) {
tBleStatus result = aci_gatt_del_service(svc_handle);
if(result) {
FURI_LOG_E(TAG, "Failed to delete service: %x", result);
furi_assert(false, "Failed to delete service");
}

return result == BLE_STATUS_SUCCESS;
Expand Down
14 changes: 14 additions & 0 deletions targets/f7/ble_glue/gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct {
bool enable_adv;
bool is_secure;
uint8_t negotiation_round;
FuriSemaphore* tx_pool_busy;
} Gap;

typedef enum {
Expand Down Expand Up @@ -299,6 +300,11 @@ BleEventFlowStatus ble_event_app_notification(void* pckt) {
}
break;
}

case ACI_GATT_TX_POOL_AVAILABLE_VSEVT_CODE:
FURI_LOG_D(TAG, "TX pool available event");
furi_semaphore_release(gap->tx_pool_busy);
break;
}
default:
break;
Expand All @@ -309,6 +315,11 @@ BleEventFlowStatus ble_event_app_notification(void* pckt) {
return BleEventFlowEnable;
}

void gap_wait_for_tx_pool_acailable(FuriWait wait) {
furi_check(gap);
furi_check(furi_semaphore_acquire(gap->tx_pool_busy, wait) == FuriStatusOk);
}

static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
if(uid_len == 2) {
gap->service.adv_svc_uuid[0] = AD_TYPE_16_BIT_SERV_UUID;
Expand Down Expand Up @@ -527,6 +538,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {

gap = malloc(sizeof(Gap));
gap->config = config;
gap->tx_pool_busy = furi_semaphore_alloc(1, 0);
// Create advertising timer
gap->advertise_timer = furi_timer_alloc(gap_advetise_timer_callback, FuriTimerTypeOnce, NULL);
// Initialization of GATT & GAP layer
Expand Down Expand Up @@ -592,6 +604,8 @@ void gap_thread_stop(void) {
gap->command_queue = NULL;
furi_timer_free(gap->advertise_timer);
gap->advertise_timer = NULL;
furi_semaphore_free(gap->tx_pool_busy);
gap->tx_pool_busy = NULL;

ble_event_dispatcher_reset();
free(gap);
Expand Down
3 changes: 3 additions & 0 deletions targets/f7/ble_glue/gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdint.h>
#include <stdbool.h>
#include <furi/core/base.h>

#include <furi_hal_version.h>

Expand Down Expand Up @@ -89,6 +90,8 @@ void gap_thread_stop(void);

void gap_emit_ble_beacon_status_event(bool active);

void gap_wait_for_tx_pool_acailable(FuriWait wait);

#ifdef __cplusplus
}
#endif
Loading