mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-23 05:24:46 -07:00
ble: profile rework (#3272)
* ble: profile rework, initial * apps: hid: fix for pairing cleanup * app: hid: select transport based on #define * fixing PVS warnings * ble: serial service: fixed uid naming * bt service: on-demand dialog init; ble profiles: docs; battery svc: proper update * Added shci_cmd_resp_wait/shci_cmd_resp_release impl with semaphore * app: hid: separated transport code * ble: fixed service init order for serial svc; moved hardfault check to ble_glue * cli: ps: added thread prio to output, fixed heap display * ble_glue: naming changes; separate thread for event processing; * furi: added runtime stats; cli: added cpu% to `ps` * cli: fixed thread time calculation * furi: added getter for thread priority * fixing pvs warnings * hid profile: fixed naming * more naming fixes * hal: ble init small cleanup * cleanup & draft beacon api * f18: api sync * apps: moved example_custom_font from debug to examples * BLE extra beacon demo app * naming fix * UI fixes for demo app (wip) * desktop, ble svc: added statusbar icon for beacon * minor cleanup * Minor cleanup & naming fixes * api sync * Removed stale header * hal: added FURI_BLE_EXTRA_LOG for extra logging; comments & code cleanup * naming & macro fixes * quick fixes from review * Eliminated stock svc_ctl * cli: ps: removed runtime stats * minor include fixes * (void) * naming fixes * More naming fixes * fbt: always build all libs * fbt: explicitly globbing libs; dist: logging SDK path * scripts: fixed lib path precedence * hal: bt: profiles: naming changes, support for passing params to a profile; include cleanup * ble: hid: added parameter processing for profile template * api sync * BLE HID: long name trim * Removed unused check * desktop: updated beacon status icon; ble: hid: cleaner device name management * desktop: updated status icon Co-authored-by: あく <alleteam@gmail.com> Co-authored-by: nminaylov <nm29719@gmail.com>
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
#include "serial_service.h"
|
||||
#include "app_common.h"
|
||||
#include <ble/ble.h>
|
||||
#include "gatt_char.h"
|
||||
#include <furi_ble/event_dispatcher.h>
|
||||
#include <furi_ble/gatt.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#include "serial_service_uuid.inc"
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "BtSerialSvc"
|
||||
|
||||
@@ -17,12 +19,12 @@ typedef enum {
|
||||
SerialSvcGattCharacteristicCount,
|
||||
} SerialSvcGattCharacteristicId;
|
||||
|
||||
static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattCharacteristicCount] = {
|
||||
static const BleGattCharacteristicParams ble_svc_serial_chars[SerialSvcGattCharacteristicCount] = {
|
||||
[SerialSvcGattCharacteristicRx] =
|
||||
{.name = "RX",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_RX_CHAR_UUID,
|
||||
.data.fixed.length = BLE_SVC_SERIAL_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_RX_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
@@ -31,8 +33,8 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
[SerialSvcGattCharacteristicTx] =
|
||||
{.name = "TX",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_TX_CHAR_UUID,
|
||||
.data.fixed.length = BLE_SVC_SERIAL_DATA_LEN_MAX,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_TX_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_INDICATE,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
@@ -42,7 +44,7 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
{.name = "Flow control",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = sizeof(uint32_t),
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_FLOW_CONTROL_UUID,
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_FLOW_CONTROL_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
||||
@@ -51,28 +53,28 @@ static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattChara
|
||||
[SerialSvcGattCharacteristicStatus] = {
|
||||
.name = "RPC status",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = sizeof(SerialServiceRpcStatus),
|
||||
.uuid.Char_UUID_128 = SERIAL_SVC_RPC_STATUS_UUID,
|
||||
.data.fixed.length = sizeof(uint32_t),
|
||||
.uuid.Char_UUID_128 = BLE_SVC_SERIAL_RPC_STATUS_UUID,
|
||||
.uuid_type = UUID_TYPE_128,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
|
||||
|
||||
typedef struct {
|
||||
struct BleServiceSerial {
|
||||
uint16_t svc_handle;
|
||||
FlipperGattCharacteristicInstance chars[SerialSvcGattCharacteristicCount];
|
||||
BleGattCharacteristicInstance chars[SerialSvcGattCharacteristicCount];
|
||||
FuriMutex* buff_size_mtx;
|
||||
uint32_t buff_size;
|
||||
uint16_t bytes_ready_to_receive;
|
||||
SerialServiceEventCallback callback;
|
||||
void* context;
|
||||
} SerialSvc;
|
||||
GapSvcEventHandler* event_handler;
|
||||
};
|
||||
|
||||
static SerialSvc* serial_svc = NULL;
|
||||
|
||||
static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
||||
static BleEventAckStatus ble_svc_serial_event_handler(void* event, void* context) {
|
||||
BleServiceSerial* serial_svc = (BleServiceSerial*)context;
|
||||
BleEventAckStatus ret = BleEventNotAck;
|
||||
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;
|
||||
@@ -82,7 +84,7 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
if(attribute_modified->Attr_Handle ==
|
||||
serial_svc->chars[SerialSvcGattCharacteristicRx].handle + 2) {
|
||||
// Descriptor handle
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
FURI_LOG_D(TAG, "RX descriptor event");
|
||||
} else if(
|
||||
attribute_modified->Attr_Handle ==
|
||||
@@ -111,13 +113,12 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
FURI_LOG_D(TAG, "Available buff size: %ld", buff_free_size);
|
||||
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
} else if(
|
||||
attribute_modified->Attr_Handle ==
|
||||
serial_svc->chars[SerialSvcGattCharacteristicStatus].handle + 1) {
|
||||
SerialServiceRpcStatus* rpc_status =
|
||||
(SerialServiceRpcStatus*)attribute_modified->Attr_Data;
|
||||
if(*rpc_status == SerialServiceRpcStatusNotActive) {
|
||||
bool* rpc_status = (bool*)attribute_modified->Attr_Data;
|
||||
if(!*rpc_status) {
|
||||
if(serial_svc->callback) {
|
||||
SerialServiceEvent event = {
|
||||
.event = SerialServiceEventTypesBleResetRequest,
|
||||
@@ -134,43 +135,47 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
||||
};
|
||||
serial_svc->callback(event, serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
ret = BleEventAckFlowEnable;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void serial_svc_update_rpc_char(SerialServiceRpcStatus status) {
|
||||
flipper_gatt_characteristic_update(
|
||||
typedef enum {
|
||||
SerialServiceRpcStatusNotActive = 0UL,
|
||||
SerialServiceRpcStatusActive = 1UL,
|
||||
} SerialServiceRpcStatus;
|
||||
|
||||
static void
|
||||
ble_svc_serial_update_rpc_char(BleServiceSerial* serial_svc, SerialServiceRpcStatus status) {
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle, &serial_svc->chars[SerialSvcGattCharacteristicStatus], &status);
|
||||
}
|
||||
|
||||
void serial_svc_start() {
|
||||
UNUSED(serial_svc_chars);
|
||||
tBleStatus status;
|
||||
serial_svc = malloc(sizeof(SerialSvc));
|
||||
// Register event handler
|
||||
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
|
||||
BleServiceSerial* ble_svc_serial_start(void) {
|
||||
BleServiceSerial* serial_svc = malloc(sizeof(BleServiceSerial));
|
||||
|
||||
// Add service
|
||||
status = aci_gatt_add_service(
|
||||
UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &serial_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to add Serial service: %d", status);
|
||||
serial_svc->event_handler =
|
||||
ble_event_dispatcher_register_svc_handler(ble_svc_serial_event_handler, serial_svc);
|
||||
|
||||
if(!ble_gatt_service_add(
|
||||
UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &serial_svc->svc_handle)) {
|
||||
free(serial_svc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Add characteristics
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_init(
|
||||
serial_svc->svc_handle, &serial_svc_chars[i], &serial_svc->chars[i]);
|
||||
ble_gatt_characteristic_init(
|
||||
serial_svc->svc_handle, &ble_svc_serial_chars[i], &serial_svc->chars[i]);
|
||||
}
|
||||
|
||||
serial_svc_update_rpc_char(SerialServiceRpcStatusNotActive);
|
||||
// Allocate buffer size mutex
|
||||
ble_svc_serial_update_rpc_char(serial_svc, SerialServiceRpcStatusNotActive);
|
||||
serial_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
|
||||
return serial_svc;
|
||||
}
|
||||
|
||||
void serial_svc_set_callbacks(
|
||||
void ble_svc_serial_set_callbacks(
|
||||
BleServiceSerial* serial_svc,
|
||||
uint16_t buff_size,
|
||||
SerialServiceEventCallback callback,
|
||||
void* context) {
|
||||
@@ -181,13 +186,13 @@ void serial_svc_set_callbacks(
|
||||
serial_svc->bytes_ready_to_receive = buff_size;
|
||||
|
||||
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
||||
flipper_gatt_characteristic_update(
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle,
|
||||
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
||||
&buff_size_reversed);
|
||||
}
|
||||
|
||||
void serial_svc_notify_buffer_is_empty() {
|
||||
void ble_svc_serial_notify_buffer_is_empty(BleServiceSerial* serial_svc) {
|
||||
furi_assert(serial_svc);
|
||||
furi_assert(serial_svc->buff_size_mtx);
|
||||
|
||||
@@ -197,7 +202,7 @@ void serial_svc_notify_buffer_is_empty() {
|
||||
serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
|
||||
|
||||
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
||||
flipper_gatt_characteristic_update(
|
||||
ble_gatt_characteristic_update(
|
||||
serial_svc->svc_handle,
|
||||
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
||||
&buff_size_reversed);
|
||||
@@ -205,35 +210,26 @@ void serial_svc_notify_buffer_is_empty() {
|
||||
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
||||
}
|
||||
|
||||
void serial_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(serial_svc) {
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
flipper_gatt_characteristic_delete(serial_svc->svc_handle, &serial_svc->chars[i]);
|
||||
}
|
||||
// Delete service
|
||||
status = aci_gatt_del_service(serial_svc->svc_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
|
||||
}
|
||||
// Delete buffer size mutex
|
||||
furi_mutex_free(serial_svc->buff_size_mtx);
|
||||
free(serial_svc);
|
||||
serial_svc = NULL;
|
||||
void ble_svc_serial_stop(BleServiceSerial* serial_svc) {
|
||||
furi_check(serial_svc);
|
||||
|
||||
ble_event_dispatcher_unregister_svc_handler(serial_svc->event_handler);
|
||||
|
||||
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_delete(serial_svc->svc_handle, &serial_svc->chars[i]);
|
||||
}
|
||||
ble_gatt_service_delete(serial_svc->svc_handle);
|
||||
furi_mutex_free(serial_svc->buff_size_mtx);
|
||||
free(serial_svc);
|
||||
}
|
||||
|
||||
bool serial_svc_is_started() {
|
||||
return serial_svc != NULL;
|
||||
}
|
||||
|
||||
bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
|
||||
if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
bool ble_svc_serial_update_tx(BleServiceSerial* serial_svc, uint8_t* data, uint16_t data_len) {
|
||||
if(data_len > BLE_SVC_SERIAL_DATA_LEN_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(uint16_t remained = data_len; remained > 0;) {
|
||||
uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained);
|
||||
uint8_t value_len = MIN(BLE_SVC_SERIAL_CHAR_VALUE_LEN_MAX, remained);
|
||||
uint16_t value_offset = data_len - remained;
|
||||
remained -= value_len;
|
||||
|
||||
@@ -256,7 +252,8 @@ bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void serial_svc_set_rpc_status(SerialServiceRpcStatus status) {
|
||||
void ble_svc_serial_set_rpc_active(BleServiceSerial* serial_svc, bool active) {
|
||||
furi_assert(serial_svc);
|
||||
serial_svc_update_rpc_char(status);
|
||||
ble_svc_serial_update_rpc_char(
|
||||
serial_svc, active ? SerialServiceRpcStatusActive : SerialServiceRpcStatusNotActive);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user