Files
Momentum-Firmware/targets/f7/ble_glue/services/dev_info_service.c
T
hedger 60a9d7e6cf ble: profile rework (#3272)
* ble: profile rework, initial
* apps: hid: fix for pairing cleanup
* app: hid: select transport based on #define
* fixing PVS warnings
* ble: serial service: fixed uid naming
* bt service: on-demand dialog init; ble profiles: docs; battery svc: proper update
* Added shci_cmd_resp_wait/shci_cmd_resp_release impl with semaphore
* app: hid: separated transport code
* ble: fixed service init order for serial svc; moved hardfault check to ble_glue
* cli: ps: added thread prio to output, fixed heap display
* ble_glue: naming changes; separate thread for event processing;
* furi: added runtime stats; cli: added cpu% to `ps`
* cli: fixed thread time calculation
* furi: added getter for thread priority
* fixing pvs warnings
* hid profile: fixed naming
* more naming fixes
* hal: ble init small cleanup
* cleanup & draft beacon api
* f18: api sync
* apps: moved example_custom_font from debug to examples
* BLE extra beacon demo app
* naming fix
* UI fixes for demo app (wip)
* desktop, ble svc: added statusbar icon for beacon
* minor cleanup
* Minor cleanup & naming fixes
* api sync
* Removed stale header
* hal: added FURI_BLE_EXTRA_LOG for extra logging; comments & code cleanup
* naming & macro fixes
* quick fixes from review
* Eliminated stock svc_ctl
* cli: ps: removed runtime stats
* minor include fixes
* (void)
* naming fixes
* More naming fixes
* fbt: always build all libs
* fbt: explicitly globbing libs; dist: logging SDK path
* scripts: fixed lib path precedence
* hal: bt: profiles: naming changes, support for passing params to a profile; include cleanup
* ble: hid: added parameter processing for profile template
* api sync
* BLE HID: long name trim
* Removed unused check
* desktop: updated beacon status icon; ble: hid: cleaner device name management
* desktop: updated status icon

Co-authored-by: あく <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2024-02-16 14:20:45 +07:00

152 lines
5.9 KiB
C

#include "dev_info_service.h"
#include "app_common.h"
#include <furi_ble/gatt.h>
#include <ble/ble.h>
#include <furi.h>
#include <protobuf_version.h>
#include <lib/toolbox/version.h>
#include "dev_info_service_uuid.inc"
#define TAG "BtDevInfoSvc"
typedef enum {
DevInfoSvcGattCharacteristicMfgName = 0,
DevInfoSvcGattCharacteristicSerial,
DevInfoSvcGattCharacteristicFirmwareRev,
DevInfoSvcGattCharacteristicSoftwareRev,
DevInfoSvcGattCharacteristicRpcVersion,
DevInfoSvcGattCharacteristicCount,
} DevInfoSvcGattCharacteristicId;
#define DEVICE_INFO_HARDWARE_REV_SIZE (4)
#define DEVICE_INFO_SOFTWARE_REV_SIZE (40)
struct BleServiceDevInfo {
uint16_t service_handle;
BleGattCharacteristicInstance characteristics[DevInfoSvcGattCharacteristicCount];
};
static const char dev_info_man_name[] = "Flipper Devices Inc.";
static const char dev_info_serial_num[] = "1.0";
static const char dev_info_rpc_version[] = TOSTRING(PROTOBUF_MAJOR_VERSION.PROTOBUF_MINOR_VERSION);
static char hardware_revision[DEVICE_INFO_HARDWARE_REV_SIZE] = {0};
static char software_revision[DEVICE_INFO_SOFTWARE_REV_SIZE] = {0};
static bool
dev_info_char_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
*data_len = (uint16_t)strlen(context); //-V1029
if(data) {
*data = (const uint8_t*)context;
}
return false;
}
static const BleGattCharacteristicParams ble_svc_dev_info_chars[DevInfoSvcGattCharacteristicCount] =
{[DevInfoSvcGattCharacteristicMfgName] =
{.name = "Manufacturer Name",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = sizeof(dev_info_man_name) - 1,
.data.fixed.ptr = (const uint8_t*)&dev_info_man_name,
.uuid.Char_UUID_16 = MANUFACTURER_NAME_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[DevInfoSvcGattCharacteristicSerial] =
{.name = "Serial Number",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = sizeof(dev_info_serial_num) - 1,
.data.fixed.ptr = (const uint8_t*)&dev_info_serial_num,
.uuid.Char_UUID_16 = SERIAL_NUMBER_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[DevInfoSvcGattCharacteristicFirmwareRev] =
{.name = "Firmware Revision",
.data_prop_type = FlipperGattCharacteristicDataCallback,
.data.callback.context = hardware_revision,
.data.callback.fn = dev_info_char_data_callback,
.uuid.Char_UUID_16 = FIRMWARE_REVISION_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[DevInfoSvcGattCharacteristicSoftwareRev] =
{.name = "Software Revision",
.data_prop_type = FlipperGattCharacteristicDataCallback,
.data.callback.context = software_revision,
.data.callback.fn = dev_info_char_data_callback,
.uuid.Char_UUID_16 = SOFTWARE_REVISION_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[DevInfoSvcGattCharacteristicRpcVersion] = {
.name = "RPC Version",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = sizeof(dev_info_rpc_version) - 1,
.data.fixed.ptr = (const uint8_t*)&dev_info_rpc_version,
.uuid.Char_UUID_128 = DEV_INVO_RPC_VERSION_UID,
.uuid_type = UUID_TYPE_128,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT}};
BleServiceDevInfo* ble_svc_dev_info_start(void) {
BleServiceDevInfo* dev_info_svc = malloc(sizeof(BleServiceDevInfo));
snprintf(
software_revision,
sizeof(software_revision),
"%s %s %s %s",
version_get_githash(NULL),
version_get_gitbranch(NULL),
version_get_gitbranchnum(NULL),
version_get_builddate(NULL));
snprintf(hardware_revision, sizeof(hardware_revision), "%d", version_get_target(NULL));
// Add Device Information Service
uint16_t uuid = DEVICE_INFORMATION_SERVICE_UUID;
if(!ble_gatt_service_add(
UUID_TYPE_16,
(Service_UUID_t*)&uuid,
PRIMARY_SERVICE,
1 + 2 * DevInfoSvcGattCharacteristicCount,
&dev_info_svc->service_handle)) {
free(dev_info_svc);
return NULL;
}
for(size_t i = 0; i < DevInfoSvcGattCharacteristicCount; i++) {
ble_gatt_characteristic_init(
dev_info_svc->service_handle,
&ble_svc_dev_info_chars[i],
&dev_info_svc->characteristics[i]);
ble_gatt_characteristic_update(
dev_info_svc->service_handle, &dev_info_svc->characteristics[i], NULL);
}
return dev_info_svc;
}
void ble_svc_dev_info_stop(BleServiceDevInfo* dev_info_svc) {
furi_assert(dev_info_svc);
/* Delete service characteristics */
for(size_t i = 0; i < DevInfoSvcGattCharacteristicCount; i++) {
ble_gatt_characteristic_delete(
dev_info_svc->service_handle, &dev_info_svc->characteristics[i]);
}
/* Delete service */
ble_gatt_service_delete(dev_info_svc->service_handle);
free(dev_info_svc);
}