mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2024-12-24 05:48:08 +03:00
[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:
parent
8c5813ef9b
commit
943fb1bced
@ -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;
|
||||
};
|
||||
|
@ -2,16 +2,20 @@ PROJECT_ROOT = $(abspath $(dir $(abspath $(firstword $(MAKEFILE_LIST))))..)
|
||||
|
||||
include $(PROJECT_ROOT)/assets/assets.mk
|
||||
|
||||
all_assets: $(ASSETS) $(PROTOBUF)
|
||||
all: icons protobuf
|
||||
|
||||
$(ASSETS): $(ASSETS_SOURCES) $(ASSETS_COMPILLER)
|
||||
@echo "\tASSETS\t\t" $@
|
||||
@$(ASSETS_COMPILLER) icons "$(ASSETS_SOURCE_DIR)" "$(ASSETS_COMPILED_DIR)"
|
||||
|
||||
icons: $(ASSETS)
|
||||
|
||||
$(PROTOBUF) &: $(PROTOBUF_SOURCES) $(PROTOBUF_COMPILER)
|
||||
@echo "\tPROTOBUF\t" $(PROTOBUF_FILENAMES)
|
||||
@$(PROJECT_ROOT)/lib/nanopb/generator/nanopb_generator.py -q -I$(PROTOBUF_SOURCE_DIR) -D$(PROTOBUF_COMPILED_DIR) $(PROTOBUF_SOURCES)
|
||||
|
||||
protobuf: $(PROTOBUF)
|
||||
|
||||
clean:
|
||||
@echo "\tCLEAN\t"
|
||||
@$(RM) $(ASSETS)
|
||||
@$(RM) $(ASSETS_COMPILED_DIR)/*
|
||||
|
@ -9,8 +9,6 @@
|
||||
#include "battery_service.h"
|
||||
#include "serial_service.h"
|
||||
|
||||
#include <applications/bt/bt_service/bt.h>
|
||||
#include <applications/rpc/rpc.h>
|
||||
#include <furi-hal.h>
|
||||
|
||||
#define GAP_TAG "BLE"
|
||||
@ -21,27 +19,26 @@
|
||||
#define BD_ADDR_SIZE_LOCAL 6
|
||||
|
||||
typedef struct {
|
||||
uint16_t gap_svc_handle;
|
||||
uint16_t dev_name_char_handle;
|
||||
uint16_t appearance_char_handle;
|
||||
uint16_t connection_handle;
|
||||
uint8_t adv_svc_uuid_len;
|
||||
uint8_t adv_svc_uuid[20];
|
||||
uint16_t gap_svc_handle;
|
||||
uint16_t dev_name_char_handle;
|
||||
uint16_t appearance_char_handle;
|
||||
uint16_t connection_handle;
|
||||
uint8_t adv_svc_uuid_len;
|
||||
uint8_t adv_svc_uuid[20];
|
||||
} GapSvc;
|
||||
|
||||
typedef struct {
|
||||
GapSvc gap_svc;
|
||||
GapState state;
|
||||
osMutexId_t state_mutex;
|
||||
uint8_t mac_address[BD_ADDR_SIZE_LOCAL];
|
||||
Bt* bt;
|
||||
Rpc* rpc;
|
||||
RpcSession* rpc_session;
|
||||
osTimerId advertise_timer;
|
||||
osThreadAttr_t thread_attr;
|
||||
osThreadId_t thread_id;
|
||||
osMessageQueueId_t command_queue;
|
||||
bool enable_adv;
|
||||
GapSvc gap_svc;
|
||||
GapState state;
|
||||
osMutexId_t state_mutex;
|
||||
uint8_t mac_address[BD_ADDR_SIZE_LOCAL];
|
||||
BleEventCallback on_event_cb;
|
||||
void* context;
|
||||
osTimerId advertise_timer;
|
||||
osThreadAttr_t thread_attr;
|
||||
osThreadId_t thread_id;
|
||||
osMessageQueueId_t command_queue;
|
||||
bool enable_adv;
|
||||
} Gap;
|
||||
|
||||
typedef enum {
|
||||
@ -84,14 +81,15 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
if (disconnection_complete_event->Connection_Handle == gap->gap_svc.connection_handle) {
|
||||
gap->gap_svc.connection_handle = 0;
|
||||
gap->state = GapStateIdle;
|
||||
FURI_LOG_I(GAP_TAG, "Disconnect from client. Close RPC session");
|
||||
rpc_close_session(gap->rpc_session);
|
||||
FURI_LOG_I(GAP_TAG, "Disconnect from client. Reason: %d", disconnection_complete_event->Reason);
|
||||
}
|
||||
if(gap->enable_adv) {
|
||||
// Restart advertising
|
||||
gap_start_advertising();
|
||||
furi_hal_power_insomnia_exit();
|
||||
}
|
||||
BleEvent event = {.type = BleEventTypeDisconnected};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -120,9 +118,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
case EVT_LE_CONN_COMPLETE:
|
||||
furi_hal_power_insomnia_enter();
|
||||
hci_le_connection_complete_event_rp0* connection_complete_event = (hci_le_connection_complete_event_rp0 *) meta_evt->data;
|
||||
FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x. Start RPC session", connection_complete_event->Connection_Handle);
|
||||
gap->rpc_session = rpc_open_session(gap->rpc);
|
||||
serial_svc_set_rpc_session(gap->rpc_session);
|
||||
FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x", connection_complete_event->Connection_Handle);
|
||||
|
||||
// Stop advertising as connection completed
|
||||
osTimerStop(gap->advertise_timer);
|
||||
@ -155,7 +151,8 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
uint32_t pin = rand() % 999999;
|
||||
aci_gap_pass_key_resp(gap->gap_svc.connection_handle, pin);
|
||||
FURI_LOG_I(GAP_TAG, "Pass key request event. Pin: %d", pin);
|
||||
bt_pin_code_show(gap->bt, pin);
|
||||
BleEvent event = {.type = BleEventTypePinCodeShow, .data.pin_code = pin};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -197,6 +194,8 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
aci_gap_terminate(gap->gap_svc.connection_handle, 5);
|
||||
} else {
|
||||
FURI_LOG_I(GAP_TAG, "Pairing complete");
|
||||
BleEvent event = {.type = BleEventTypeConnected};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -211,11 +210,6 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
return SVCCTL_UserEvtFlowEnable;
|
||||
}
|
||||
|
||||
void SVCCTL_SvcInit() {
|
||||
// Dummy function to prevent unused services initialization
|
||||
// TODO refactor (disable all services in WPAN config)
|
||||
}
|
||||
|
||||
static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
|
||||
gap->gap_svc.adv_svc_uuid_len = 1;
|
||||
if(uid_len == 2) {
|
||||
@ -263,7 +257,7 @@ static void gap_init_svc(Gap* gap) {
|
||||
tBleStatus status;
|
||||
uint32_t srd_bd_addr[2];
|
||||
|
||||
//HCI Reset to synchronise BLE Stack*/
|
||||
// HCI Reset to synchronise BLE Stack
|
||||
hci_reset();
|
||||
// Configure mac address
|
||||
gap_init_mac_address(gap);
|
||||
@ -341,7 +335,8 @@ static void gap_advertise_start(GapState new_state)
|
||||
FURI_LOG_E(GAP_TAG, "Set discoverable err: %d", status);
|
||||
}
|
||||
gap->state = new_state;
|
||||
bt_update_statusbar(gap->bt);
|
||||
BleEvent event = {.type = BleEventTypeStartAdvertising};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
|
||||
}
|
||||
|
||||
@ -355,17 +350,20 @@ static void gap_advertise_stop() {
|
||||
osTimerStop(gap->advertise_timer);
|
||||
aci_gap_set_non_discoverable();
|
||||
gap->state = GapStateIdle;
|
||||
bt_update_statusbar(gap->bt);
|
||||
}
|
||||
BleEvent event = {.type = BleEventTypeStopAdvertising};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
|
||||
void gap_start_advertising() {
|
||||
FURI_LOG_I(GAP_TAG, "Start advertising");
|
||||
gap->enable_adv = true;
|
||||
GapCommand command = GapCommandAdvFast;
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
}
|
||||
|
||||
void gap_stop_advertising() {
|
||||
FURI_LOG_I(GAP_TAG, "Stop advertising");
|
||||
gap->enable_adv = false;
|
||||
GapCommand command = GapCommandAdvStop;
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
@ -376,16 +374,13 @@ static void gap_advetise_timer_callback(void* context) {
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
}
|
||||
|
||||
bool gap_init() {
|
||||
bool gap_init(BleEventCallback on_event_cb, void* context) {
|
||||
if (APPE_Status() != BleGlueStatusStarted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
gap = furi_alloc(sizeof(Gap));
|
||||
srand(DWT->CYCCNT);
|
||||
// Open records
|
||||
gap->bt = furi_record_open("bt");
|
||||
gap->rpc = furi_record_open("rpc");
|
||||
// Create advertising timer
|
||||
gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL);
|
||||
// Initialization of GATT & GAP layer
|
||||
@ -418,6 +413,10 @@ bool gap_init() {
|
||||
adv_service_uid[1] = 0x30;
|
||||
|
||||
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
|
||||
|
||||
// Set callback
|
||||
gap->on_event_cb = on_event_cb;
|
||||
gap->context = context;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
BleEventTypeConnected,
|
||||
BleEventTypeDisconnected,
|
||||
BleEventTypeStartAdvertising,
|
||||
BleEventTypeStopAdvertising,
|
||||
BleEventTypePinCodeShow,
|
||||
} BleEventType;
|
||||
|
||||
typedef union {
|
||||
uint32_t pin_code;
|
||||
} BleEventData;
|
||||
|
||||
typedef struct {
|
||||
BleEventType type;
|
||||
BleEventData data;
|
||||
} BleEvent;
|
||||
|
||||
typedef void(*BleEventCallback) (BleEvent event, void* context);
|
||||
|
||||
typedef enum {
|
||||
GapStateIdle,
|
||||
GapStateAdvFast,
|
||||
@ -13,7 +33,7 @@ typedef enum {
|
||||
GapStateConnected,
|
||||
} GapState;
|
||||
|
||||
bool gap_init();
|
||||
bool gap_init(BleEventCallback on_event_cb, void* context);
|
||||
|
||||
void gap_start_advertising();
|
||||
|
||||
|
@ -6,36 +6,20 @@
|
||||
|
||||
#define SERIAL_SERVICE_TAG "serial service"
|
||||
|
||||
#define SERIAL_SVC_DATA_LEN_MAX 245
|
||||
|
||||
typedef struct {
|
||||
uint16_t svc_handle;
|
||||
uint16_t rx_char_handle;
|
||||
uint16_t tx_char_handle;
|
||||
RpcSession* rpc_session;
|
||||
osSemaphoreId_t rpc_sem;
|
||||
SerialSvcDataReceivedCallback on_received_cb;
|
||||
SerialSvcDataSentCallback on_sent_cb;
|
||||
void* context;
|
||||
} SerialSvc;
|
||||
|
||||
static SerialSvc* serial_svc;
|
||||
|
||||
static const uint8_t service_uuid[] = {0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f};
|
||||
static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
|
||||
void serial_svc_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
|
||||
size_t bytes_sent = 0;
|
||||
while(bytes_sent < bytes_len) {
|
||||
size_t bytes_remain = bytes_len - bytes_sent;
|
||||
if(bytes_remain > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
serial_svc_update_rx(&bytes[bytes_sent], SERIAL_SVC_DATA_LEN_MAX);
|
||||
bytes_sent += SERIAL_SVC_DATA_LEN_MAX;
|
||||
} else {
|
||||
serial_svc_update_rx(&bytes[bytes_sent], bytes_remain);
|
||||
bytes_sent += bytes_remain;
|
||||
}
|
||||
osSemaphoreAcquire(serial_svc->rpc_sem, osWaitForever);
|
||||
}
|
||||
}
|
||||
static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
|
||||
static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
|
||||
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
||||
@ -45,29 +29,31 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
|
||||
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
|
||||
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
|
||||
attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
|
||||
if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 2) {
|
||||
if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 2) {
|
||||
// Descriptor handle
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "TX descriptor event");
|
||||
} else if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 1) {
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "RX descriptor event");
|
||||
} else if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 1) {
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
|
||||
rpc_feed_bytes(serial_svc->rpc_session, attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, 1000);
|
||||
// serial_svc_update_rx(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length);
|
||||
if(serial_svc->on_received_cb) {
|
||||
serial_svc->on_received_cb(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
}
|
||||
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
|
||||
osSemaphoreRelease(serial_svc->rpc_sem);
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Ack received", blecore_evt->ecode);
|
||||
if(serial_svc->on_sent_cb) {
|
||||
serial_svc->on_sent_cb(serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void serial_svc_start(Rpc* rpc) {
|
||||
void serial_svc_start() {
|
||||
tBleStatus status;
|
||||
serial_svc = furi_alloc(sizeof(SerialSvc));
|
||||
serial_svc->rpc_sem = osSemaphoreNew(1, 0, NULL);
|
||||
// Register event handler
|
||||
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
|
||||
|
||||
@ -77,33 +63,39 @@ void serial_svc_start(Rpc* rpc) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add Serial service: %d", status);
|
||||
}
|
||||
|
||||
// Add TX characteristics
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
|
||||
// Add RX characteristics
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
|
||||
SERIAL_SVC_DATA_LEN_MAX,
|
||||
CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
|
||||
ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
10,
|
||||
CHAR_VALUE_LEN_VARIABLE,
|
||||
&serial_svc->tx_char_handle);
|
||||
&serial_svc->rx_char_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
|
||||
}
|
||||
|
||||
// Add RX characteristic
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
|
||||
// Add TX characteristic
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
|
||||
SERIAL_SVC_DATA_LEN_MAX,
|
||||
CHAR_PROP_READ | CHAR_PROP_INDICATE,
|
||||
ATTR_PERMISSION_AUTHEN_READ,
|
||||
GATT_DONT_NOTIFY_EVENTS,
|
||||
10,
|
||||
CHAR_VALUE_LEN_VARIABLE,
|
||||
&serial_svc->rx_char_handle);
|
||||
&serial_svc->tx_char_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
|
||||
}
|
||||
}
|
||||
|
||||
void serial_svc_set_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
|
||||
serial_svc->on_received_cb = on_received_cb;
|
||||
serial_svc->on_sent_cb = on_sent_cb;
|
||||
serial_svc->context = context;
|
||||
}
|
||||
|
||||
void serial_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(serial_svc) {
|
||||
@ -126,27 +118,18 @@ void serial_svc_stop() {
|
||||
}
|
||||
}
|
||||
|
||||
void serial_svc_set_rpc_session(RpcSession* rpc_session) {
|
||||
furi_assert(rpc_session);
|
||||
// Set session
|
||||
serial_svc->rpc_session = rpc_session;
|
||||
// Set callback
|
||||
rpc_set_send_bytes_callback(serial_svc->rpc_session, serial_svc_rpc_send_bytes_callback, NULL);
|
||||
}
|
||||
|
||||
|
||||
bool serial_svc_update_rx(uint8_t* data, uint8_t data_len) {
|
||||
bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) {
|
||||
if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
return false;
|
||||
}
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Updating char %d len", data_len);
|
||||
tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
|
||||
serial_svc->rx_char_handle,
|
||||
serial_svc->tx_char_handle,
|
||||
0,
|
||||
data_len,
|
||||
data);
|
||||
if(result) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating RX characteristic: %d", result);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating TX characteristic: %d", result);
|
||||
}
|
||||
return result != BLE_STATUS_SUCCESS;
|
||||
}
|
||||
|
@ -3,20 +3,22 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <rpc/rpc.h>
|
||||
|
||||
#define SERIAL_SVC_DATA_LEN_MAX (245)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void(*SerialSvcDataReceivedCallback)(uint8_t* buff, uint16_t size, void* context);
|
||||
typedef void(*SerialSvcDataSentCallback)(void* context);
|
||||
|
||||
void serial_svc_start();
|
||||
|
||||
void serial_svc_set_rpc_session(RpcSession* rpc_session);
|
||||
void serial_svc_set_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context);
|
||||
|
||||
void serial_svc_stop();
|
||||
|
||||
bool serial_svc_update_rx(uint8_t* data, uint8_t data_len);
|
||||
bool serial_svc_update_tx(uint8_t* data, uint8_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -4,7 +4,8 @@
|
||||
#include <stm32wbxx.h>
|
||||
#include <shci.h>
|
||||
#include <cmsis_os2.h>
|
||||
#include <gap.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
void furi_hal_bt_init() {
|
||||
// Explicitly tell that we are in charge of CLK48 domain
|
||||
@ -13,8 +14,9 @@ void furi_hal_bt_init() {
|
||||
APPE_Init();
|
||||
}
|
||||
|
||||
bool furi_hal_bt_init_app() {
|
||||
return gap_init();
|
||||
bool furi_hal_bt_init_app(BleEventCallback event_cb, void* context) {
|
||||
furi_assert(event_cb);
|
||||
return gap_init(event_cb, context);
|
||||
}
|
||||
|
||||
void furi_hal_bt_start_advertising() {
|
||||
@ -32,6 +34,17 @@ void furi_hal_bt_stop_advertising() {
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_bt_set_data_event_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
|
||||
serial_svc_set_callbacks(on_received_cb, on_sent_cb, context);
|
||||
}
|
||||
|
||||
bool furi_hal_bt_tx(uint8_t* data, uint16_t size) {
|
||||
if(size > FURI_HAL_BT_PACKET_SIZE_MAX) {
|
||||
return false;
|
||||
}
|
||||
return serial_svc_update_tx(data, size);
|
||||
}
|
||||
|
||||
void furi_hal_bt_dump_state(string_t buffer) {
|
||||
BleGlueStatus status = APPE_Status();
|
||||
if (status == BleGlueStatusStarted) {
|
||||
|
@ -9,8 +9,6 @@
|
||||
#include "battery_service.h"
|
||||
#include "serial_service.h"
|
||||
|
||||
#include <applications/bt/bt_service/bt.h>
|
||||
#include <applications/rpc/rpc.h>
|
||||
#include <furi-hal.h>
|
||||
|
||||
#define GAP_TAG "BLE"
|
||||
@ -21,27 +19,26 @@
|
||||
#define BD_ADDR_SIZE_LOCAL 6
|
||||
|
||||
typedef struct {
|
||||
uint16_t gap_svc_handle;
|
||||
uint16_t dev_name_char_handle;
|
||||
uint16_t appearance_char_handle;
|
||||
uint16_t connection_handle;
|
||||
uint8_t adv_svc_uuid_len;
|
||||
uint8_t adv_svc_uuid[20];
|
||||
uint16_t gap_svc_handle;
|
||||
uint16_t dev_name_char_handle;
|
||||
uint16_t appearance_char_handle;
|
||||
uint16_t connection_handle;
|
||||
uint8_t adv_svc_uuid_len;
|
||||
uint8_t adv_svc_uuid[20];
|
||||
} GapSvc;
|
||||
|
||||
typedef struct {
|
||||
GapSvc gap_svc;
|
||||
GapState state;
|
||||
osMutexId_t state_mutex;
|
||||
uint8_t mac_address[BD_ADDR_SIZE_LOCAL];
|
||||
Bt* bt;
|
||||
Rpc* rpc;
|
||||
RpcSession* rpc_session;
|
||||
osTimerId advertise_timer;
|
||||
osThreadAttr_t thread_attr;
|
||||
osThreadId_t thread_id;
|
||||
osMessageQueueId_t command_queue;
|
||||
bool enable_adv;
|
||||
GapSvc gap_svc;
|
||||
GapState state;
|
||||
osMutexId_t state_mutex;
|
||||
uint8_t mac_address[BD_ADDR_SIZE_LOCAL];
|
||||
BleEventCallback on_event_cb;
|
||||
void* context;
|
||||
osTimerId advertise_timer;
|
||||
osThreadAttr_t thread_attr;
|
||||
osThreadId_t thread_id;
|
||||
osMessageQueueId_t command_queue;
|
||||
bool enable_adv;
|
||||
} Gap;
|
||||
|
||||
typedef enum {
|
||||
@ -84,14 +81,15 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
if (disconnection_complete_event->Connection_Handle == gap->gap_svc.connection_handle) {
|
||||
gap->gap_svc.connection_handle = 0;
|
||||
gap->state = GapStateIdle;
|
||||
FURI_LOG_I(GAP_TAG, "Disconnect from client. Close RPC session");
|
||||
rpc_close_session(gap->rpc_session);
|
||||
FURI_LOG_I(GAP_TAG, "Disconnect from client. Reason: %d", disconnection_complete_event->Reason);
|
||||
}
|
||||
if(gap->enable_adv) {
|
||||
// Restart advertising
|
||||
gap_start_advertising();
|
||||
furi_hal_power_insomnia_exit();
|
||||
}
|
||||
BleEvent event = {.type = BleEventTypeDisconnected};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -120,9 +118,7 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
case EVT_LE_CONN_COMPLETE:
|
||||
furi_hal_power_insomnia_enter();
|
||||
hci_le_connection_complete_event_rp0* connection_complete_event = (hci_le_connection_complete_event_rp0 *) meta_evt->data;
|
||||
FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x. Start RPC session", connection_complete_event->Connection_Handle);
|
||||
gap->rpc_session = rpc_open_session(gap->rpc);
|
||||
serial_svc_set_rpc_session(gap->rpc_session);
|
||||
FURI_LOG_I(GAP_TAG, "Connection complete for connection handle 0x%x", connection_complete_event->Connection_Handle);
|
||||
|
||||
// Stop advertising as connection completed
|
||||
osTimerStop(gap->advertise_timer);
|
||||
@ -155,7 +151,8 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
uint32_t pin = rand() % 999999;
|
||||
aci_gap_pass_key_resp(gap->gap_svc.connection_handle, pin);
|
||||
FURI_LOG_I(GAP_TAG, "Pass key request event. Pin: %d", pin);
|
||||
bt_pin_code_show(gap->bt, pin);
|
||||
BleEvent event = {.type = BleEventTypePinCodeShow, .data.pin_code = pin};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -197,6 +194,8 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
aci_gap_terminate(gap->gap_svc.connection_handle, 5);
|
||||
} else {
|
||||
FURI_LOG_I(GAP_TAG, "Pairing complete");
|
||||
BleEvent event = {.type = BleEventTypeConnected};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -211,11 +210,6 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
|
||||
return SVCCTL_UserEvtFlowEnable;
|
||||
}
|
||||
|
||||
void SVCCTL_SvcInit() {
|
||||
// Dummy function to prevent unused services initialization
|
||||
// TODO refactor (disable all services in WPAN config)
|
||||
}
|
||||
|
||||
static void set_advertisment_service_uid(uint8_t* uid, uint8_t uid_len) {
|
||||
gap->gap_svc.adv_svc_uuid_len = 1;
|
||||
if(uid_len == 2) {
|
||||
@ -263,7 +257,7 @@ static void gap_init_svc(Gap* gap) {
|
||||
tBleStatus status;
|
||||
uint32_t srd_bd_addr[2];
|
||||
|
||||
//HCI Reset to synchronise BLE Stack*/
|
||||
// HCI Reset to synchronise BLE Stack
|
||||
hci_reset();
|
||||
// Configure mac address
|
||||
gap_init_mac_address(gap);
|
||||
@ -341,7 +335,8 @@ static void gap_advertise_start(GapState new_state)
|
||||
FURI_LOG_E(GAP_TAG, "Set discoverable err: %d", status);
|
||||
}
|
||||
gap->state = new_state;
|
||||
bt_update_statusbar(gap->bt);
|
||||
BleEvent event = {.type = BleEventTypeStartAdvertising};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
osTimerStart(gap->advertise_timer, INITIAL_ADV_TIMEOUT);
|
||||
}
|
||||
|
||||
@ -355,17 +350,20 @@ static void gap_advertise_stop() {
|
||||
osTimerStop(gap->advertise_timer);
|
||||
aci_gap_set_non_discoverable();
|
||||
gap->state = GapStateIdle;
|
||||
bt_update_statusbar(gap->bt);
|
||||
}
|
||||
BleEvent event = {.type = BleEventTypeStopAdvertising};
|
||||
gap->on_event_cb(event, gap->context);
|
||||
}
|
||||
|
||||
void gap_start_advertising() {
|
||||
FURI_LOG_I(GAP_TAG, "Start advertising");
|
||||
gap->enable_adv = true;
|
||||
GapCommand command = GapCommandAdvFast;
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
}
|
||||
|
||||
void gap_stop_advertising() {
|
||||
FURI_LOG_I(GAP_TAG, "Stop advertising");
|
||||
gap->enable_adv = false;
|
||||
GapCommand command = GapCommandAdvStop;
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
@ -376,16 +374,13 @@ static void gap_advetise_timer_callback(void* context) {
|
||||
furi_check(osMessageQueuePut(gap->command_queue, &command, 0, 0) == osOK);
|
||||
}
|
||||
|
||||
bool gap_init() {
|
||||
bool gap_init(BleEventCallback on_event_cb, void* context) {
|
||||
if (APPE_Status() != BleGlueStatusStarted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
gap = furi_alloc(sizeof(Gap));
|
||||
srand(DWT->CYCCNT);
|
||||
// Open records
|
||||
gap->bt = furi_record_open("bt");
|
||||
gap->rpc = furi_record_open("rpc");
|
||||
// Create advertising timer
|
||||
gap->advertise_timer = osTimerNew(gap_advetise_timer_callback, osTimerOnce, NULL, NULL);
|
||||
// Initialization of GATT & GAP layer
|
||||
@ -418,6 +413,10 @@ bool gap_init() {
|
||||
adv_service_uid[1] = 0x30;
|
||||
|
||||
set_advertisment_service_uid(adv_service_uid, sizeof(adv_service_uid));
|
||||
|
||||
// Set callback
|
||||
gap->on_event_cb = on_event_cb;
|
||||
gap->context = context;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
BleEventTypeConnected,
|
||||
BleEventTypeDisconnected,
|
||||
BleEventTypeStartAdvertising,
|
||||
BleEventTypeStopAdvertising,
|
||||
BleEventTypePinCodeShow,
|
||||
} BleEventType;
|
||||
|
||||
typedef union {
|
||||
uint32_t pin_code;
|
||||
} BleEventData;
|
||||
|
||||
typedef struct {
|
||||
BleEventType type;
|
||||
BleEventData data;
|
||||
} BleEvent;
|
||||
|
||||
typedef void(*BleEventCallback) (BleEvent event, void* context);
|
||||
|
||||
typedef enum {
|
||||
GapStateIdle,
|
||||
GapStateAdvFast,
|
||||
@ -13,7 +33,7 @@ typedef enum {
|
||||
GapStateConnected,
|
||||
} GapState;
|
||||
|
||||
bool gap_init();
|
||||
bool gap_init(BleEventCallback on_event_cb, void* context);
|
||||
|
||||
void gap_start_advertising();
|
||||
|
||||
|
@ -6,36 +6,20 @@
|
||||
|
||||
#define SERIAL_SERVICE_TAG "serial service"
|
||||
|
||||
#define SERIAL_SVC_DATA_LEN_MAX 245
|
||||
|
||||
typedef struct {
|
||||
uint16_t svc_handle;
|
||||
uint16_t rx_char_handle;
|
||||
uint16_t tx_char_handle;
|
||||
RpcSession* rpc_session;
|
||||
osSemaphoreId_t rpc_sem;
|
||||
SerialSvcDataReceivedCallback on_received_cb;
|
||||
SerialSvcDataSentCallback on_sent_cb;
|
||||
void* context;
|
||||
} SerialSvc;
|
||||
|
||||
static SerialSvc* serial_svc;
|
||||
|
||||
static const uint8_t service_uuid[] = {0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f};
|
||||
static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
|
||||
void serial_svc_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
|
||||
size_t bytes_sent = 0;
|
||||
while(bytes_sent < bytes_len) {
|
||||
size_t bytes_remain = bytes_len - bytes_sent;
|
||||
if(bytes_remain > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
serial_svc_update_rx(&bytes[bytes_sent], SERIAL_SVC_DATA_LEN_MAX);
|
||||
bytes_sent += SERIAL_SVC_DATA_LEN_MAX;
|
||||
} else {
|
||||
serial_svc_update_rx(&bytes[bytes_sent], bytes_remain);
|
||||
bytes_sent += bytes_remain;
|
||||
}
|
||||
osSemaphoreAcquire(serial_svc->rpc_sem, osWaitForever);
|
||||
}
|
||||
}
|
||||
static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
|
||||
|
||||
static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
|
||||
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
||||
@ -45,29 +29,31 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
|
||||
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
|
||||
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
|
||||
attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
|
||||
if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 2) {
|
||||
if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 2) {
|
||||
// Descriptor handle
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "TX descriptor event");
|
||||
} else if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 1) {
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "RX descriptor event");
|
||||
} else if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 1) {
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
|
||||
rpc_feed_bytes(serial_svc->rpc_session, attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, 1000);
|
||||
// serial_svc_update_rx(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length);
|
||||
if(serial_svc->on_received_cb) {
|
||||
serial_svc->on_received_cb(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
}
|
||||
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
|
||||
osSemaphoreRelease(serial_svc->rpc_sem);
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Ack received", blecore_evt->ecode);
|
||||
if(serial_svc->on_sent_cb) {
|
||||
serial_svc->on_sent_cb(serial_svc->context);
|
||||
}
|
||||
ret = SVCCTL_EvtAckFlowEnable;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void serial_svc_start(Rpc* rpc) {
|
||||
void serial_svc_start() {
|
||||
tBleStatus status;
|
||||
serial_svc = furi_alloc(sizeof(SerialSvc));
|
||||
serial_svc->rpc_sem = osSemaphoreNew(1, 0, NULL);
|
||||
// Register event handler
|
||||
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
|
||||
|
||||
@ -77,33 +63,39 @@ void serial_svc_start(Rpc* rpc) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add Serial service: %d", status);
|
||||
}
|
||||
|
||||
// Add TX characteristics
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
|
||||
// Add RX characteristics
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
|
||||
SERIAL_SVC_DATA_LEN_MAX,
|
||||
CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
|
||||
ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
|
||||
GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
10,
|
||||
CHAR_VALUE_LEN_VARIABLE,
|
||||
&serial_svc->tx_char_handle);
|
||||
&serial_svc->rx_char_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
|
||||
}
|
||||
|
||||
// Add RX characteristic
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
|
||||
// Add TX characteristic
|
||||
status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
|
||||
SERIAL_SVC_DATA_LEN_MAX,
|
||||
CHAR_PROP_READ | CHAR_PROP_INDICATE,
|
||||
ATTR_PERMISSION_AUTHEN_READ,
|
||||
GATT_DONT_NOTIFY_EVENTS,
|
||||
10,
|
||||
CHAR_VALUE_LEN_VARIABLE,
|
||||
&serial_svc->rx_char_handle);
|
||||
&serial_svc->tx_char_handle);
|
||||
if(status) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
|
||||
}
|
||||
}
|
||||
|
||||
void serial_svc_set_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
|
||||
serial_svc->on_received_cb = on_received_cb;
|
||||
serial_svc->on_sent_cb = on_sent_cb;
|
||||
serial_svc->context = context;
|
||||
}
|
||||
|
||||
void serial_svc_stop() {
|
||||
tBleStatus status;
|
||||
if(serial_svc) {
|
||||
@ -126,27 +118,18 @@ void serial_svc_stop() {
|
||||
}
|
||||
}
|
||||
|
||||
void serial_svc_set_rpc_session(RpcSession* rpc_session) {
|
||||
furi_assert(rpc_session);
|
||||
// Set session
|
||||
serial_svc->rpc_session = rpc_session;
|
||||
// Set callback
|
||||
rpc_set_send_bytes_callback(serial_svc->rpc_session, serial_svc_rpc_send_bytes_callback, NULL);
|
||||
}
|
||||
|
||||
|
||||
bool serial_svc_update_rx(uint8_t* data, uint8_t data_len) {
|
||||
bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) {
|
||||
if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
|
||||
return false;
|
||||
}
|
||||
FURI_LOG_D(SERIAL_SERVICE_TAG, "Updating char %d len", data_len);
|
||||
tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
|
||||
serial_svc->rx_char_handle,
|
||||
serial_svc->tx_char_handle,
|
||||
0,
|
||||
data_len,
|
||||
data);
|
||||
if(result) {
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating RX characteristic: %d", result);
|
||||
FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating TX characteristic: %d", result);
|
||||
}
|
||||
return result != BLE_STATUS_SUCCESS;
|
||||
}
|
||||
|
@ -3,20 +3,22 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <rpc/rpc.h>
|
||||
|
||||
#define SERIAL_SVC_DATA_LEN_MAX (245)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void(*SerialSvcDataReceivedCallback)(uint8_t* buff, uint16_t size, void* context);
|
||||
typedef void(*SerialSvcDataSentCallback)(void* context);
|
||||
|
||||
void serial_svc_start();
|
||||
|
||||
void serial_svc_set_rpc_session(RpcSession* rpc_session);
|
||||
void serial_svc_set_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context);
|
||||
|
||||
void serial_svc_stop();
|
||||
|
||||
bool serial_svc_update_rx(uint8_t* data, uint8_t data_len);
|
||||
bool serial_svc_update_tx(uint8_t* data, uint8_t data_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -4,7 +4,8 @@
|
||||
#include <stm32wbxx.h>
|
||||
#include <shci.h>
|
||||
#include <cmsis_os2.h>
|
||||
#include <gap.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
void furi_hal_bt_init() {
|
||||
// Explicitly tell that we are in charge of CLK48 domain
|
||||
@ -13,8 +14,9 @@ void furi_hal_bt_init() {
|
||||
APPE_Init();
|
||||
}
|
||||
|
||||
bool furi_hal_bt_init_app() {
|
||||
return gap_init();
|
||||
bool furi_hal_bt_init_app(BleEventCallback event_cb, void* context) {
|
||||
furi_assert(event_cb);
|
||||
return gap_init(event_cb, context);
|
||||
}
|
||||
|
||||
void furi_hal_bt_start_advertising() {
|
||||
@ -32,6 +34,17 @@ void furi_hal_bt_stop_advertising() {
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_bt_set_data_event_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
|
||||
serial_svc_set_callbacks(on_received_cb, on_sent_cb, context);
|
||||
}
|
||||
|
||||
bool furi_hal_bt_tx(uint8_t* data, uint16_t size) {
|
||||
if(size > FURI_HAL_BT_PACKET_SIZE_MAX) {
|
||||
return false;
|
||||
}
|
||||
return serial_svc_update_tx(data, size);
|
||||
}
|
||||
|
||||
void furi_hal_bt_dump_state(string_t buffer) {
|
||||
BleGlueStatus status = APPE_Status();
|
||||
if (status == BleGlueStatusStarted) {
|
||||
|
@ -7,6 +7,10 @@
|
||||
|
||||
#include <m-string.h>
|
||||
#include <stdbool.h>
|
||||
#include <gap.h>
|
||||
#include <serial_service.h>
|
||||
|
||||
#define FURI_HAL_BT_PACKET_SIZE_MAX SERIAL_SVC_DATA_LEN_MAX
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -17,10 +21,10 @@ extern "C" {
|
||||
void furi_hal_bt_init();
|
||||
|
||||
/** Start BLE app
|
||||
*
|
||||
* @return true if app inited
|
||||
*/
|
||||
bool furi_hal_bt_init_app();
|
||||
* @param event_cb - BleEventCallback instance
|
||||
* @param context - pointer to context
|
||||
*/
|
||||
bool furi_hal_bt_init_app(BleEventCallback event_cb, void* context);
|
||||
|
||||
/** Start advertising
|
||||
*/
|
||||
@ -48,10 +52,20 @@ void furi_hal_bt_dump_state(string_t buffer);
|
||||
*/
|
||||
bool furi_hal_bt_is_alive();
|
||||
|
||||
/** Wait for Core2 startup
|
||||
*
|
||||
* @return true if success, otherwise timeouted
|
||||
/** Set data event callbacks
|
||||
* @param on_received_cb - SerialSvcDataReceivedCallback instance
|
||||
* @param on_sent_cb - SerialSvcDataSentCallback instance
|
||||
* @param context - pointer to context
|
||||
*/
|
||||
void furi_hal_bt_set_data_event_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context);
|
||||
|
||||
/** Send data through BLE
|
||||
* @param data - data buffer
|
||||
* @param size - data buffer size
|
||||
*/
|
||||
bool furi_hal_bt_tx(uint8_t* data, uint16_t size);
|
||||
|
||||
/** Wait for Core2 startup */
|
||||
bool furi_hal_bt_wait_startup();
|
||||
|
||||
/** Lock shared access to flash controller
|
||||
|
Loading…
Reference in New Issue
Block a user