diff --git a/applications/bt/bt_service/bt.c b/applications/bt/bt_service/bt.c index 91cfad81..4fae0cf6 100755 --- a/applications/bt/bt_service/bt.c +++ b/applications/bt/bt_service/bt.c @@ -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"); diff --git a/applications/bt/bt_service/bt.h b/applications/bt/bt_service/bt.h index ccf5e501..0a2a924c 100644 --- a/applications/bt/bt_service/bt.h +++ b/applications/bt/bt_service/bt.h @@ -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 diff --git a/applications/bt/bt_service/bt_api.c b/applications/bt/bt_service/bt_api.c deleted file mode 100755 index db709871..00000000 --- a/applications/bt/bt_service/bt_api.c +++ /dev/null @@ -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; -} diff --git a/applications/bt/bt_service/bt_i.h b/applications/bt/bt_service/bt_i.h index 624c6c64..882061ee 100644 --- a/applications/bt/bt_service/bt_i.h +++ b/applications/bt/bt_service/bt_i.h @@ -11,6 +11,7 @@ #include #include +#include #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; }; diff --git a/assets/Makefile b/assets/Makefile index efa07a1f..079b7b2a 100644 --- a/assets/Makefile +++ b/assets/Makefile @@ -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)/* diff --git a/firmware/targets/f6/ble-glue/gap.c b/firmware/targets/f6/ble-glue/gap.c index 73c8c0a7..eae5f87c 100644 --- a/firmware/targets/f6/ble-glue/gap.c +++ b/firmware/targets/f6/ble-glue/gap.c @@ -9,8 +9,6 @@ #include "battery_service.h" #include "serial_service.h" -#include -#include #include #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; } diff --git a/firmware/targets/f6/ble-glue/gap.h b/firmware/targets/f6/ble-glue/gap.h index 93e75122..5b82cfec 100644 --- a/firmware/targets/f6/ble-glue/gap.h +++ b/firmware/targets/f6/ble-glue/gap.h @@ -1,11 +1,31 @@ #pragma once +#include #include #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(); diff --git a/firmware/targets/f6/ble-glue/serial_service.c b/firmware/targets/f6/ble-glue/serial_service.c index a6f8e626..7de3529c 100644 --- a/firmware/targets/f6/ble-glue/serial_service.c +++ b/firmware/targets/f6/ble-glue/serial_service.c @@ -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; } diff --git a/firmware/targets/f6/ble-glue/serial_service.h b/firmware/targets/f6/ble-glue/serial_service.h index bdb924f8..b5a1c078 100644 --- a/firmware/targets/f6/ble-glue/serial_service.h +++ b/firmware/targets/f6/ble-glue/serial_service.h @@ -3,20 +3,22 @@ #include #include -#include - +#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 } diff --git a/firmware/targets/f6/furi-hal/furi-hal-bt.c b/firmware/targets/f6/furi-hal/furi-hal-bt.c index 01f4b919..97db87c7 100644 --- a/firmware/targets/f6/furi-hal/furi-hal-bt.c +++ b/firmware/targets/f6/furi-hal/furi-hal-bt.c @@ -4,7 +4,8 @@ #include #include #include -#include + +#include 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) { diff --git a/firmware/targets/f7/ble-glue/gap.c b/firmware/targets/f7/ble-glue/gap.c index 73c8c0a7..eae5f87c 100644 --- a/firmware/targets/f7/ble-glue/gap.c +++ b/firmware/targets/f7/ble-glue/gap.c @@ -9,8 +9,6 @@ #include "battery_service.h" #include "serial_service.h" -#include -#include #include #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; } diff --git a/firmware/targets/f7/ble-glue/gap.h b/firmware/targets/f7/ble-glue/gap.h index 93e75122..5b82cfec 100644 --- a/firmware/targets/f7/ble-glue/gap.h +++ b/firmware/targets/f7/ble-glue/gap.h @@ -1,11 +1,31 @@ #pragma once +#include #include #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(); diff --git a/firmware/targets/f7/ble-glue/serial_service.c b/firmware/targets/f7/ble-glue/serial_service.c index a6f8e626..7de3529c 100644 --- a/firmware/targets/f7/ble-glue/serial_service.c +++ b/firmware/targets/f7/ble-glue/serial_service.c @@ -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; } diff --git a/firmware/targets/f7/ble-glue/serial_service.h b/firmware/targets/f7/ble-glue/serial_service.h index bdb924f8..b5a1c078 100644 --- a/firmware/targets/f7/ble-glue/serial_service.h +++ b/firmware/targets/f7/ble-glue/serial_service.h @@ -3,20 +3,22 @@ #include #include -#include - +#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 } diff --git a/firmware/targets/f7/furi-hal/furi-hal-bt.c b/firmware/targets/f7/furi-hal/furi-hal-bt.c index 01f4b919..97db87c7 100644 --- a/firmware/targets/f7/furi-hal/furi-hal-bt.c +++ b/firmware/targets/f7/furi-hal/furi-hal-bt.c @@ -4,7 +4,8 @@ #include #include #include -#include + +#include 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) { diff --git a/firmware/targets/furi-hal-include/furi-hal-bt.h b/firmware/targets/furi-hal-include/furi-hal-bt.h index 89fab715..fe9a6de9 100644 --- a/firmware/targets/furi-hal-include/furi-hal-bt.h +++ b/firmware/targets/furi-hal-include/furi-hal-bt.h @@ -7,6 +7,10 @@ #include #include +#include +#include + +#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