[FL-1721] Bluetooth refactoring (#747)
* ble-glue: add on connect \ disconnect, on send \ received callbacks * bt: rework service with furi-hal-bt and callbacks * bt: update battery level on connect * ble-glue: set one callback with parameters * bt: rework callbacks, remove unused API * ble-glue: remove dead code * furi-hal-bt: add documentation * ble-glue: apply changes for f7 target * bt: rename RpcInstance -> Rpc * ble: add disconnection reason * ble: don't open bt record from GAP * bt: fix RPC session close * Assets: separate targets for icons and protobuf Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -33,7 +33,10 @@ static void bt_battery_level_changed_callback(const void* _event, void* context)
|
||||
Bt* bt = context;
|
||||
const PowerEvent* event = _event;
|
||||
if(event->type == PowerEventTypeBatteryLevelChanged) {
|
||||
bt_update_battery_level(bt, event->data.battery_level);
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypeUpdateBatteryLevel,
|
||||
.data.battery_level = event->data.battery_level};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,9 +64,84 @@ Bt* bt_alloc() {
|
||||
PubSub* power_pubsub = power_get_pubsub(bt->power);
|
||||
subscribe_pubsub(power_pubsub, bt_battery_level_changed_callback, bt);
|
||||
|
||||
// RPC
|
||||
bt->rpc = furi_record_open("rpc");
|
||||
bt->rpc_sem = osSemaphoreNew(1, 0, NULL);
|
||||
|
||||
return bt;
|
||||
}
|
||||
|
||||
// Called from GAP thread from Serial service
|
||||
static void bt_on_data_received_callback(uint8_t* data, uint16_t size, void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
|
||||
size_t bytes_processed = rpc_feed_bytes(bt->rpc_session, data, size, 1000);
|
||||
if(bytes_processed != size) {
|
||||
FURI_LOG_E(BT_SERVICE_TAG, "Only %d of %d bytes processed by RPC", bytes_processed, size);
|
||||
}
|
||||
}
|
||||
|
||||
// Called from GAP thread from Serial service
|
||||
static void bt_on_data_sent_callback(void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
|
||||
osSemaphoreRelease(bt->rpc_sem);
|
||||
}
|
||||
|
||||
// Called from RPC thread
|
||||
static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
|
||||
size_t bytes_sent = 0;
|
||||
while(bytes_sent < bytes_len) {
|
||||
size_t bytes_remain = bytes_len - bytes_sent;
|
||||
if(bytes_remain > FURI_HAL_BT_PACKET_SIZE_MAX) {
|
||||
furi_hal_bt_tx(&bytes[bytes_sent], FURI_HAL_BT_PACKET_SIZE_MAX);
|
||||
bytes_sent += FURI_HAL_BT_PACKET_SIZE_MAX;
|
||||
} else {
|
||||
furi_hal_bt_tx(&bytes[bytes_sent], bytes_remain);
|
||||
bytes_sent += bytes_remain;
|
||||
}
|
||||
osSemaphoreAcquire(bt->rpc_sem, osWaitForever);
|
||||
}
|
||||
}
|
||||
|
||||
// Called from GAP thread
|
||||
static void bt_on_gap_event_callback(BleEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
|
||||
if(event.type == BleEventTypeConnected) {
|
||||
FURI_LOG_I(BT_SERVICE_TAG, "Open RPC connection");
|
||||
bt->rpc_session = rpc_open_session(bt->rpc);
|
||||
rpc_set_send_bytes_callback(bt->rpc_session, bt_rpc_send_bytes_callback, bt);
|
||||
furi_hal_bt_set_data_event_callbacks(
|
||||
bt_on_data_received_callback, bt_on_data_sent_callback, bt);
|
||||
// Update battery level
|
||||
PowerInfo info;
|
||||
power_get_info(bt->power, &info);
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypeUpdateBatteryLevel, .data.battery_level = info.charge};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
} else if(event.type == BleEventTypeDisconnected) {
|
||||
FURI_LOG_I(BT_SERVICE_TAG, "Close RPC connection");
|
||||
if(bt->rpc_session) {
|
||||
rpc_close_session(bt->rpc_session);
|
||||
bt->rpc_session = NULL;
|
||||
}
|
||||
} else if(event.type == BleEventTypeStartAdvertising || event.type == BleEventTypeStopAdvertising) {
|
||||
BtMessage message = {.type = BtMessageTypeUpdateStatusbar};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
} else if(event.type == BleEventTypePinCodeShow) {
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypePinCodeShow, .data.pin_code = event.data.pin_code};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t bt_srv() {
|
||||
Bt* bt = bt_alloc();
|
||||
furi_record_create("bt", bt);
|
||||
@@ -72,11 +150,10 @@ int32_t bt_srv() {
|
||||
FURI_LOG_E(BT_SERVICE_TAG, "Core2 startup failed");
|
||||
} else {
|
||||
view_port_enabled_set(bt->statusbar_view_port, true);
|
||||
if(furi_hal_bt_init_app()) {
|
||||
if(furi_hal_bt_init_app(bt_on_gap_event_callback, bt)) {
|
||||
FURI_LOG_I(BT_SERVICE_TAG, "BLE stack started");
|
||||
if(bt->bt_settings.enabled) {
|
||||
furi_hal_bt_start_advertising();
|
||||
FURI_LOG_I(BT_SERVICE_TAG, "Start advertising");
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed");
|
||||
|
@@ -9,12 +9,6 @@ extern "C" {
|
||||
|
||||
typedef struct Bt Bt;
|
||||
|
||||
void bt_update_statusbar(Bt* bt);
|
||||
|
||||
void bt_update_battery_level(Bt* bt, uint8_t battery_level);
|
||||
|
||||
bool bt_pin_code_show(Bt* bt, uint32_t pin_code);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -1,21 +0,0 @@
|
||||
#include "bt.h"
|
||||
#include "bt_i.h"
|
||||
|
||||
void bt_update_statusbar(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
BtMessage message = {.type = BtMessageTypeUpdateStatusbar};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
}
|
||||
|
||||
void bt_update_battery_level(Bt* bt, uint8_t battery_level) {
|
||||
furi_assert(bt);
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypeUpdateBatteryLevel, .data.battery_level = battery_level};
|
||||
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
}
|
||||
|
||||
bool bt_pin_code_show(Bt* bt, uint32_t pin_code) {
|
||||
furi_assert(bt);
|
||||
BtMessage message = {.type = BtMessageTypePinCodeShow, .data.pin_code = pin_code};
|
||||
return osMessageQueuePut(bt->message_queue, &message, 0, 0) == osOK;
|
||||
}
|
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <power/power_service/power.h>
|
||||
#include <applications/rpc/rpc.h>
|
||||
|
||||
#include "../bt_settings.h"
|
||||
|
||||
@@ -38,4 +39,7 @@ struct Bt {
|
||||
DialogsApp* dialogs;
|
||||
DialogMessage* dialog_message;
|
||||
Power* power;
|
||||
Rpc* rpc;
|
||||
RpcSession* rpc_session;
|
||||
osSemaphoreId_t rpc_sem;
|
||||
};
|
||||
|
Reference in New Issue
Block a user