2021-09-10 00:11:32 +03:00
|
|
|
#include "battery_service.h"
|
|
|
|
#include "app_common.h"
|
2024-02-16 10:20:45 +03:00
|
|
|
#include <core/check.h>
|
|
|
|
#include <furi_ble/gatt.h>
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2022-08-22 20:06:17 +03:00
|
|
|
#include <ble/ble.h>
|
2021-09-10 00:11:32 +03:00
|
|
|
|
|
|
|
#include <furi.h>
|
2024-02-16 10:20:45 +03:00
|
|
|
|
|
|
|
#include <m-list.h>
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2021-11-12 16:04:35 +03:00
|
|
|
#define TAG "BtBatterySvc"
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2022-05-24 16:42:02 +03:00
|
|
|
enum {
|
2024-02-16 10:20:45 +03:00
|
|
|
/* Common states */
|
2022-05-24 16:42:02 +03:00
|
|
|
BatterySvcPowerStateUnknown = 0b00,
|
|
|
|
BatterySvcPowerStateUnsupported = 0b01,
|
2024-02-16 10:20:45 +03:00
|
|
|
/* Level states */
|
2022-05-24 16:42:02 +03:00
|
|
|
BatterySvcPowerStateGoodLevel = 0b10,
|
|
|
|
BatterySvcPowerStateCriticallyLowLevel = 0b11,
|
2024-02-16 10:20:45 +03:00
|
|
|
/* Charging states */
|
2022-05-24 16:42:02 +03:00
|
|
|
BatterySvcPowerStateNotCharging = 0b10,
|
|
|
|
BatterySvcPowerStateCharging = 0b11,
|
2024-02-16 10:20:45 +03:00
|
|
|
/* Discharging states */
|
2022-05-24 16:42:02 +03:00
|
|
|
BatterySvcPowerStateNotDischarging = 0b10,
|
|
|
|
BatterySvcPowerStateDischarging = 0b11,
|
2024-02-16 10:20:45 +03:00
|
|
|
/* Battery states */
|
2022-05-24 16:42:02 +03:00
|
|
|
BatterySvcPowerStateBatteryNotPresent = 0b10,
|
|
|
|
BatterySvcPowerStateBatteryPresent = 0b11,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
2024-07-15 07:38:49 +03:00
|
|
|
uint8_t present : 2;
|
2022-05-24 16:42:02 +03:00
|
|
|
uint8_t discharging : 2;
|
2024-07-15 07:38:49 +03:00
|
|
|
uint8_t charging : 2;
|
|
|
|
uint8_t level : 2;
|
2022-05-24 16:42:02 +03:00
|
|
|
} BattrySvcPowerState;
|
|
|
|
|
|
|
|
_Static_assert(sizeof(BattrySvcPowerState) == 1, "Incorrect structure size");
|
|
|
|
|
|
|
|
#define BATTERY_POWER_STATE (0x2A1A)
|
|
|
|
|
2021-09-13 14:25:37 +03:00
|
|
|
static const uint16_t service_uuid = BATTERY_SERVICE_UUID;
|
2023-06-08 12:42:02 +03:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
BatterySvcGattCharacteristicBatteryLevel = 0,
|
|
|
|
BatterySvcGattCharacteristicPowerState,
|
|
|
|
BatterySvcGattCharacteristicCount,
|
|
|
|
} BatterySvcGattCharacteristicId;
|
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
static const BleGattCharacteristicParams battery_svc_chars[BatterySvcGattCharacteristicCount] = {
|
|
|
|
[BatterySvcGattCharacteristicBatteryLevel] =
|
|
|
|
{.name = "Battery Level",
|
2023-06-08 12:42:02 +03:00
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = 1,
|
2024-02-16 10:20:45 +03:00
|
|
|
.uuid.Char_UUID_16 = BATTERY_LEVEL_CHAR_UUID,
|
2023-06-08 12:42:02 +03:00
|
|
|
.uuid_type = UUID_TYPE_16,
|
|
|
|
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
|
|
|
.security_permissions = ATTR_PERMISSION_AUTHEN_READ,
|
|
|
|
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
2024-02-16 10:20:45 +03:00
|
|
|
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
|
|
|
[BatterySvcGattCharacteristicPowerState] = {
|
|
|
|
.name = "Power State",
|
|
|
|
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
|
|
|
.data.fixed.length = 1,
|
|
|
|
.uuid.Char_UUID_16 = BATTERY_POWER_STATE,
|
|
|
|
.uuid_type = UUID_TYPE_16,
|
|
|
|
.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}};
|
|
|
|
|
|
|
|
struct BleServiceBattery {
|
2023-06-08 12:42:02 +03:00
|
|
|
uint16_t svc_handle;
|
2024-02-16 10:20:45 +03:00
|
|
|
BleGattCharacteristicInstance chars[BatterySvcGattCharacteristicCount];
|
|
|
|
bool auto_update;
|
|
|
|
};
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
LIST_DEF(BatterySvcInstanceList, BleServiceBattery*, M_POD_OPLIST);
|
2021-09-13 14:25:37 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
/* We need to keep track of all battery service instances so that we can update
|
|
|
|
* them when the battery state changes. */
|
|
|
|
static BatterySvcInstanceList_t instances;
|
|
|
|
static bool instances_initialized = false;
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
BleServiceBattery* ble_svc_battery_start(bool auto_update) {
|
|
|
|
BleServiceBattery* battery_svc = malloc(sizeof(BleServiceBattery));
|
|
|
|
|
|
|
|
if(!ble_gatt_service_add(
|
|
|
|
UUID_TYPE_16,
|
|
|
|
(Service_UUID_t*)&service_uuid,
|
|
|
|
PRIMARY_SERVICE,
|
|
|
|
8,
|
|
|
|
&battery_svc->svc_handle)) {
|
|
|
|
free(battery_svc);
|
|
|
|
return NULL;
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
2023-06-08 12:42:02 +03:00
|
|
|
for(size_t i = 0; i < BatterySvcGattCharacteristicCount; i++) {
|
2024-02-16 10:20:45 +03:00
|
|
|
ble_gatt_characteristic_init(
|
2023-06-08 12:42:02 +03:00
|
|
|
battery_svc->svc_handle, &battery_svc_chars[i], &battery_svc->chars[i]);
|
2022-05-24 16:42:02 +03:00
|
|
|
}
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
battery_svc->auto_update = auto_update;
|
|
|
|
if(auto_update) {
|
|
|
|
if(!instances_initialized) {
|
|
|
|
BatterySvcInstanceList_init(instances);
|
|
|
|
instances_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BatterySvcInstanceList_push_back(instances, battery_svc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return battery_svc;
|
2021-09-13 14:25:37 +03:00
|
|
|
}
|
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
void ble_svc_battery_stop(BleServiceBattery* battery_svc) {
|
2024-03-19 17:43:52 +03:00
|
|
|
furi_check(battery_svc);
|
2024-02-16 10:20:45 +03:00
|
|
|
if(battery_svc->auto_update) {
|
|
|
|
BatterySvcInstanceList_it_t it;
|
|
|
|
for(BatterySvcInstanceList_it(it, instances); !BatterySvcInstanceList_end_p(it);
|
|
|
|
BatterySvcInstanceList_next(it)) {
|
|
|
|
if(*BatterySvcInstanceList_ref(it) == battery_svc) {
|
|
|
|
BatterySvcInstanceList_remove(instances, it);
|
|
|
|
break;
|
|
|
|
}
|
2021-09-13 14:25:37 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 00:11:32 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
for(size_t i = 0; i < BatterySvcGattCharacteristicCount; i++) {
|
|
|
|
ble_gatt_characteristic_delete(battery_svc->svc_handle, &battery_svc->chars[i]);
|
|
|
|
}
|
|
|
|
/* Delete Battery service */
|
|
|
|
ble_gatt_service_delete(battery_svc->svc_handle);
|
|
|
|
free(battery_svc);
|
2021-12-08 14:28:01 +03:00
|
|
|
}
|
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
bool ble_svc_battery_update_level(BleServiceBattery* battery_svc, uint8_t battery_charge) {
|
|
|
|
furi_check(battery_svc);
|
|
|
|
/* Update battery level characteristic */
|
|
|
|
return ble_gatt_characteristic_update(
|
2023-06-08 12:42:02 +03:00
|
|
|
battery_svc->svc_handle,
|
|
|
|
&battery_svc->chars[BatterySvcGattCharacteristicBatteryLevel],
|
|
|
|
&battery_charge);
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
2022-05-24 16:42:02 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
bool ble_svc_battery_update_power_state(BleServiceBattery* battery_svc, bool charging) {
|
|
|
|
furi_check(battery_svc);
|
|
|
|
|
|
|
|
/* Update power state characteristic */
|
2022-05-24 16:42:02 +03:00
|
|
|
BattrySvcPowerState power_state = {
|
|
|
|
.level = BatterySvcPowerStateUnsupported,
|
|
|
|
.present = BatterySvcPowerStateBatteryPresent,
|
|
|
|
};
|
2024-02-16 10:20:45 +03:00
|
|
|
if(charging) {
|
2022-05-24 16:42:02 +03:00
|
|
|
power_state.charging = BatterySvcPowerStateCharging;
|
|
|
|
power_state.discharging = BatterySvcPowerStateNotDischarging;
|
|
|
|
} else {
|
|
|
|
power_state.charging = BatterySvcPowerStateNotCharging;
|
|
|
|
power_state.discharging = BatterySvcPowerStateDischarging;
|
|
|
|
}
|
2023-06-08 12:42:02 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
return ble_gatt_characteristic_update(
|
2022-05-24 16:42:02 +03:00
|
|
|
battery_svc->svc_handle,
|
2023-06-08 12:42:02 +03:00
|
|
|
&battery_svc->chars[BatterySvcGattCharacteristicPowerState],
|
|
|
|
&power_state);
|
2022-05-24 16:42:02 +03:00
|
|
|
}
|
2024-02-16 10:20:45 +03:00
|
|
|
|
|
|
|
void ble_svc_battery_state_update(uint8_t* battery_level, bool* charging) {
|
|
|
|
if(!instances_initialized) {
|
|
|
|
#ifdef FURI_BLE_EXTRA_LOG
|
|
|
|
FURI_LOG_W(TAG, "Battery service not initialized");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BatterySvcInstanceList_it_t it;
|
|
|
|
for(BatterySvcInstanceList_it(it, instances); !BatterySvcInstanceList_end_p(it);
|
|
|
|
BatterySvcInstanceList_next(it)) {
|
|
|
|
BleServiceBattery* battery_svc = *BatterySvcInstanceList_ref(it);
|
|
|
|
if(battery_level) {
|
|
|
|
ble_svc_battery_update_level(battery_svc, *battery_level);
|
|
|
|
}
|
|
|
|
if(charging) {
|
|
|
|
ble_svc_battery_update_power_state(battery_svc, *charging);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|