2021-09-10 00:11:32 +03:00
|
|
|
#include "serial_service.h"
|
|
|
|
#include "app_common.h"
|
2022-08-22 20:06:17 +03:00
|
|
|
#include <ble/ble.h>
|
2023-06-08 12:42:02 +03:00
|
|
|
#include "gatt_char.h"
|
2021-09-10 00:11:32 +03:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
2023-06-08 12:42:02 +03:00
|
|
|
#include "serial_service_uuid.inc"
|
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "BtSerialSvc"
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2023-06-08 12:42:02 +03:00
|
|
|
typedef enum {
|
|
|
|
SerialSvcGattCharacteristicTx = 0,
|
|
|
|
SerialSvcGattCharacteristicRx,
|
|
|
|
SerialSvcGattCharacteristicFlowCtrl,
|
|
|
|
SerialSvcGattCharacteristicStatus,
|
|
|
|
SerialSvcGattCharacteristicCount,
|
|
|
|
} SerialSvcGattCharacteristicId;
|
|
|
|
|
|
|
|
static const FlipperGattCharacteristicParams serial_svc_chars[SerialSvcGattCharacteristicCount] = {
|
|
|
|
[SerialSvcGattCharacteristicTx] =
|
|
|
|
{.name = "TX",
|
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
|
|
|
.uuid.Char_UUID_128 = SERIAL_SVC_TX_CHAR_UUID,
|
|
|
|
.uuid_type = UUID_TYPE_128,
|
|
|
|
.char_properties = CHAR_PROP_READ | CHAR_PROP_INDICATE,
|
|
|
|
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
|
|
|
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
|
|
|
.is_variable = CHAR_VALUE_LEN_VARIABLE},
|
|
|
|
[SerialSvcGattCharacteristicRx] =
|
|
|
|
{.name = "RX",
|
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = SERIAL_SVC_DATA_LEN_MAX,
|
|
|
|
.uuid.Char_UUID_128 = SERIAL_SVC_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,
|
|
|
|
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
|
|
|
.is_variable = CHAR_VALUE_LEN_VARIABLE},
|
|
|
|
[SerialSvcGattCharacteristicFlowCtrl] =
|
|
|
|
{.name = "Flow control",
|
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = sizeof(uint32_t),
|
|
|
|
.uuid.Char_UUID_128 = SERIAL_SVC_FLOW_CONTROL_UUID,
|
|
|
|
.uuid_type = UUID_TYPE_128,
|
|
|
|
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
|
|
|
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
|
|
|
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
|
|
|
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
|
|
|
[SerialSvcGattCharacteristicStatus] = {
|
|
|
|
.name = "RPC status",
|
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = sizeof(SerialServiceRpcStatus),
|
|
|
|
.uuid.Char_UUID_128 = SERIAL_SVC_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}};
|
|
|
|
|
2021-09-10 00:11:32 +03:00
|
|
|
typedef struct {
|
|
|
|
uint16_t svc_handle;
|
2023-06-08 12:42:02 +03:00
|
|
|
FlipperGattCharacteristicInstance chars[SerialSvcGattCharacteristicCount];
|
2022-07-20 13:56:33 +03:00
|
|
|
FuriMutex* buff_size_mtx;
|
2021-11-08 22:41:40 +03:00
|
|
|
uint32_t buff_size;
|
|
|
|
uint16_t bytes_ready_to_receive;
|
2021-12-08 14:28:01 +03:00
|
|
|
SerialServiceEventCallback callback;
|
2021-10-12 19:41:42 +03:00
|
|
|
void* context;
|
2021-09-10 00:11:32 +03:00
|
|
|
} SerialSvc;
|
|
|
|
|
2021-11-08 22:41:40 +03:00
|
|
|
static SerialSvc* serial_svc = NULL;
|
2021-09-13 14:25:37 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
|
2021-09-10 00:11:32 +03:00
|
|
|
SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
|
2022-01-05 19:10:18 +03:00
|
|
|
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
|
2021-09-10 00:11:32 +03:00
|
|
|
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
|
|
|
|
aci_gatt_attribute_modified_event_rp0* attribute_modified;
|
|
|
|
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
|
|
|
|
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
|
|
|
|
attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
|
2023-06-08 12:42:02 +03:00
|
|
|
if(attribute_modified->Attr_Handle ==
|
|
|
|
serial_svc->chars[SerialSvcGattCharacteristicRx].handle + 2) {
|
2021-09-10 00:11:32 +03:00
|
|
|
// Descriptor handle
|
|
|
|
ret = SVCCTL_EvtAckFlowEnable;
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_D(TAG, "RX descriptor event");
|
2023-06-08 12:42:02 +03:00
|
|
|
} else if(
|
|
|
|
attribute_modified->Attr_Handle ==
|
|
|
|
serial_svc->chars[SerialSvcGattCharacteristicRx].handle + 1) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_D(TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
|
2021-12-08 14:28:01 +03:00
|
|
|
if(serial_svc->callback) {
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(
|
|
|
|
furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) ==
|
|
|
|
FuriStatusOk);
|
2021-11-08 22:41:40 +03:00
|
|
|
if(attribute_modified->Attr_Data_Length > serial_svc->bytes_ready_to_receive) {
|
|
|
|
FURI_LOG_W(
|
2022-01-05 19:10:18 +03:00
|
|
|
TAG,
|
|
|
|
"Received %d, while was ready to receive %d bytes. Can lead to buffer overflow!",
|
|
|
|
attribute_modified->Attr_Data_Length,
|
|
|
|
serial_svc->bytes_ready_to_receive);
|
2021-11-08 22:41:40 +03:00
|
|
|
}
|
2022-01-05 19:10:18 +03:00
|
|
|
serial_svc->bytes_ready_to_receive -= MIN(
|
|
|
|
serial_svc->bytes_ready_to_receive, attribute_modified->Attr_Data_Length);
|
2021-12-08 14:28:01 +03:00
|
|
|
SerialServiceEvent event = {
|
|
|
|
.event = SerialServiceEventTypeDataReceived,
|
|
|
|
.data = {
|
|
|
|
.buffer = attribute_modified->Attr_Data,
|
|
|
|
.size = attribute_modified->Attr_Data_Length,
|
2022-01-05 19:10:18 +03:00
|
|
|
}};
|
|
|
|
uint32_t buff_free_size = serial_svc->callback(event, serial_svc->context);
|
2022-10-07 16:35:15 +03:00
|
|
|
FURI_LOG_D(TAG, "Available buff size: %ld", buff_free_size);
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
2021-10-12 19:41:42 +03:00
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
ret = SVCCTL_EvtAckFlowEnable;
|
2023-06-08 12:42:02 +03:00
|
|
|
} else if(
|
|
|
|
attribute_modified->Attr_Handle ==
|
|
|
|
serial_svc->chars[SerialSvcGattCharacteristicStatus].handle + 1) {
|
2022-10-14 20:21:23 +03:00
|
|
|
SerialServiceRpcStatus* rpc_status =
|
|
|
|
(SerialServiceRpcStatus*)attribute_modified->Attr_Data;
|
|
|
|
if(*rpc_status == SerialServiceRpcStatusNotActive) {
|
|
|
|
if(serial_svc->callback) {
|
|
|
|
SerialServiceEvent event = {
|
|
|
|
.event = SerialServiceEventTypesBleResetRequest,
|
|
|
|
};
|
|
|
|
serial_svc->callback(event, serial_svc->context);
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
|
|
|
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
|
2022-10-07 16:35:15 +03:00
|
|
|
FURI_LOG_T(TAG, "Ack received");
|
2021-12-08 14:28:01 +03:00
|
|
|
if(serial_svc->callback) {
|
|
|
|
SerialServiceEvent event = {
|
|
|
|
.event = SerialServiceEventTypeDataSent,
|
|
|
|
};
|
|
|
|
serial_svc->callback(event, serial_svc->context);
|
2021-10-12 19:41:42 +03:00
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
ret = SVCCTL_EvtAckFlowEnable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-14 20:21:23 +03:00
|
|
|
static void serial_svc_update_rpc_char(SerialServiceRpcStatus status) {
|
2023-06-08 12:42:02 +03:00
|
|
|
flipper_gatt_characteristic_update(
|
|
|
|
serial_svc->svc_handle, &serial_svc->chars[SerialSvcGattCharacteristicStatus], &status);
|
2022-10-14 20:21:23 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 19:41:42 +03:00
|
|
|
void serial_svc_start() {
|
2023-06-08 12:42:02 +03:00
|
|
|
UNUSED(serial_svc_chars);
|
2021-09-10 00:11:32 +03:00
|
|
|
tBleStatus status;
|
2022-02-18 22:53:46 +03:00
|
|
|
serial_svc = malloc(sizeof(SerialSvc));
|
2021-09-10 00:11:32 +03:00
|
|
|
// Register event handler
|
|
|
|
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
|
|
|
|
|
|
|
|
// Add service
|
2022-01-05 19:10:18 +03:00
|
|
|
status = aci_gatt_add_service(
|
2023-06-08 12:42:02 +03:00
|
|
|
UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &serial_svc->svc_handle);
|
2021-09-10 00:11:32 +03:00
|
|
|
if(status) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_E(TAG, "Failed to add Serial service: %d", status);
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
|
|
|
|
2023-06-08 12:42:02 +03:00
|
|
|
// 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]);
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 20:21:23 +03:00
|
|
|
serial_svc_update_rpc_char(SerialServiceRpcStatusNotActive);
|
2021-11-08 22:41:40 +03:00
|
|
|
// Allocate buffer size mutex
|
2022-07-20 13:56:33 +03:00
|
|
|
serial_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
|
2021-09-13 14:25:37 +03:00
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
void serial_svc_set_callbacks(
|
|
|
|
uint16_t buff_size,
|
|
|
|
SerialServiceEventCallback callback,
|
|
|
|
void* context) {
|
2021-11-08 22:41:40 +03:00
|
|
|
furi_assert(serial_svc);
|
2021-12-08 14:28:01 +03:00
|
|
|
serial_svc->callback = callback;
|
2021-10-12 19:41:42 +03:00
|
|
|
serial_svc->context = context;
|
2021-11-08 22:41:40 +03:00
|
|
|
serial_svc->buff_size = buff_size;
|
|
|
|
serial_svc->bytes_ready_to_receive = buff_size;
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2021-11-08 22:41:40 +03:00
|
|
|
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
2023-06-08 12:42:02 +03:00
|
|
|
flipper_gatt_characteristic_update(
|
2022-01-05 19:10:18 +03:00
|
|
|
serial_svc->svc_handle,
|
2023-06-08 12:42:02 +03:00
|
|
|
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
|
|
|
&buff_size_reversed);
|
2021-11-08 22:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void serial_svc_notify_buffer_is_empty() {
|
|
|
|
furi_assert(serial_svc);
|
|
|
|
furi_assert(serial_svc->buff_size_mtx);
|
|
|
|
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) == FuriStatusOk);
|
2021-11-08 22:41:40 +03:00
|
|
|
if(serial_svc->bytes_ready_to_receive == 0) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_D(TAG, "Buffer is empty. Notifying client");
|
2021-11-08 22:41:40 +03:00
|
|
|
serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2021-11-08 22:41:40 +03:00
|
|
|
uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
|
2023-06-08 12:42:02 +03:00
|
|
|
flipper_gatt_characteristic_update(
|
2022-01-05 19:10:18 +03:00
|
|
|
serial_svc->svc_handle,
|
2023-06-08 12:42:02 +03:00
|
|
|
&serial_svc->chars[SerialSvcGattCharacteristicFlowCtrl],
|
|
|
|
&buff_size_reversed);
|
2021-11-08 22:41:40 +03:00
|
|
|
}
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
|
2021-10-12 19:41:42 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:25:37 +03:00
|
|
|
void serial_svc_stop() {
|
|
|
|
tBleStatus status;
|
|
|
|
if(serial_svc) {
|
2023-06-08 12:42:02 +03:00
|
|
|
for(uint8_t i = 0; i < SerialSvcGattCharacteristicCount; i++) {
|
|
|
|
flipper_gatt_characteristic_delete(serial_svc->svc_handle, &serial_svc->chars[i]);
|
2022-10-14 20:21:23 +03:00
|
|
|
}
|
2021-09-13 14:25:37 +03:00
|
|
|
// Delete service
|
|
|
|
status = aci_gatt_del_service(serial_svc->svc_handle);
|
|
|
|
if(status) {
|
2021-11-12 16:04:35 +03:00
|
|
|
FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
|
2021-09-13 14:25:37 +03:00
|
|
|
}
|
2021-11-08 22:41:40 +03:00
|
|
|
// Delete buffer size mutex
|
2022-07-20 13:56:33 +03:00
|
|
|
furi_mutex_free(serial_svc->buff_size_mtx);
|
2021-09-13 14:25:37 +03:00
|
|
|
free(serial_svc);
|
|
|
|
serial_svc = NULL;
|
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
bool serial_svc_is_started() {
|
|
|
|
return serial_svc != NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:59:40 +03:00
|
|
|
bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
|
2021-09-15 19:58:32 +03:00
|
|
|
if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-08 16:59:40 +03:00
|
|
|
|
|
|
|
for(uint16_t remained = data_len; remained > 0;) {
|
|
|
|
uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained);
|
|
|
|
uint16_t value_offset = data_len - remained;
|
|
|
|
remained -= value_len;
|
|
|
|
|
2022-01-05 19:10:18 +03:00
|
|
|
tBleStatus result = aci_gatt_update_char_value_ext(
|
|
|
|
0,
|
|
|
|
serial_svc->svc_handle,
|
2023-06-08 12:42:02 +03:00
|
|
|
serial_svc->chars[SerialSvcGattCharacteristicTx].handle,
|
2022-01-05 19:10:18 +03:00
|
|
|
remained ? 0x00 : 0x02,
|
|
|
|
data_len,
|
|
|
|
value_offset,
|
|
|
|
value_len,
|
|
|
|
data + value_offset);
|
2021-12-08 16:59:40 +03:00
|
|
|
|
|
|
|
if(result) {
|
|
|
|
FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result);
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
2021-12-08 16:59:40 +03:00
|
|
|
|
|
|
|
return true;
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
2022-10-14 20:21:23 +03:00
|
|
|
|
|
|
|
void serial_svc_set_rpc_status(SerialServiceRpcStatus status) {
|
|
|
|
furi_assert(serial_svc);
|
|
|
|
serial_svc_update_rpc_char(status);
|
|
|
|
}
|