Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ble/hal/blercu/bleservices/blercuremotecontrolservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class BleRcuRemoteControlService
{
m_lastKeypressChangedSlots.addSlot(func);
}
inline void addRawBatteryVolatageChangedSlot(const Slot<const std::vector<uint8_t> &> &func)
{
m_rawBatteryVoltageChangedSlots.addSlot(func);
}
inline void addAdvConfigChangedSlot(const Slot<uint8_t> &func)
{
m_advConfigChangedSlots.addSlot(func);
Expand All @@ -77,6 +81,7 @@ class BleRcuRemoteControlService
Slots<uint8_t> m_unpairReasonChangedSlots;
Slots<uint8_t, std::string> m_rebootReasonChangedSlots;
Slots<uint8_t> m_lastKeypressChangedSlots;
Slots<const std::vector<uint8_t> &> m_rawBatteryVoltageChangedSlots;
Slots<uint8_t> m_advConfigChangedSlots;
Slots<const std::vector<uint8_t> &> m_advConfigCustomListChangedSlots;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const BleUuid GattRemoteControlService::m_lastKeypressCharUuid(BleUuid::LastKeyp
const BleUuid GattRemoteControlService::m_advConfigCharUuid(BleUuid::AdvertisingConfig);
const BleUuid GattRemoteControlService::m_advConfigCustomListCharUuid(BleUuid::AdvertisingConfigCustomList);
const BleUuid GattRemoteControlService::m_assertReportCharUuid(BleUuid::AssertReport);
const BleUuid GattRemoteControlService::m_rawBatteryVoltageCharUuid(BleUuid::RawBatteryVoltage);

using namespace std;

Expand Down Expand Up @@ -182,6 +183,12 @@ bool GattRemoteControlService::start(const shared_ptr<const BleGattService> &gat
XLOGD_WARN("failed to get optional Assert Reporting characteristic, continuing anyway...");
}
}
if (!m_rawBatteryVoltageCharacteristic || !m_rawBatteryVoltageCharacteristic->isValid()) {
m_rawBatteryVoltageCharacteristic = gattService->characteristic(m_rawBatteryVoltageCharUuid);
if (!m_rawBatteryVoltageCharacteristic || !m_rawBatteryVoltageCharacteristic->isValid()) {
XLOGD_WARN("failed to get Raw Battery Voltage characteristic, continuing anyway...");
}
}

// check we're not already started
if (m_stateMachine.state() != IdleState) {
Expand Down Expand Up @@ -228,6 +235,7 @@ void GattRemoteControlService::onEnteredState(int state)
m_rebootReasonCharacteristic.reset();
m_rcuActionCharacteristic.reset();
m_assertReportCharacteristic.reset();
m_rawBatteryVoltageCharacteristic.reset();


} else if (state == RetrieveInitialValuesState) {
Expand All @@ -237,6 +245,7 @@ void GattRemoteControlService::onEnteredState(int state)
requestAdvConfigCustomList();
requestUnpairReason();
requestRebootReason();
requestRawBatteryVoltage();

m_readySlots.invoke();

Expand Down Expand Up @@ -525,6 +534,45 @@ void GattRemoteControlService::requestLastKeypress()
}
}

// -----------------------------------------------------------------------------
/*!
\internal

Sends a request to org.bluez.GattCharacteristic1.Value() to get the value
propery of the characteristic which contains the raw battery voltage.

*/
void GattRemoteControlService::requestRawBatteryVoltage()
{
// lambda invoked when the request returns
auto replyHandler = [this](PendingReply<std::vector<uint8_t>> *reply)
{
// check for errors
if (reply->isError()) {
XLOGD_ERROR("Failed to read raw battery voltage due to <%s>", reply->errorMessage().c_str());
} else {
std::vector<uint8_t> value;
value = reply->result();

if (value.size() == 3) {
onRawBatteryVoltageChanged(value);
} else {
XLOGD_ERROR("Raw battery voltage received has invalid length (%d bytes)", value.size());
}
}
};


if (m_rawBatteryVoltageCharacteristic && m_rawBatteryVoltageCharacteristic->isValid()) {

// send a request to the bluez daemon to read the characteristic
m_rawBatteryVoltageCharacteristic->readValue(PendingReply<std::vector<uint8_t>>(m_isAlive, replyHandler));

} else {
XLOGD_WARN("Raw battery voltage characteristic is not valid, check that the remote firmware version supports this feature.");
}
}

// -----------------------------------------------------------------------------
/*!
\internal
Expand Down Expand Up @@ -601,7 +649,38 @@ void GattRemoteControlService::requestAdvConfigCustomList()
}
}

// -----------------------------------------------------------------------------
/*!
\internal

Internal slot called when a notification from the remote device is sent
due to raw battery voltage changing.
*/
void GattRemoteControlService::onRawBatteryVoltageChanged(const std::vector<uint8_t> &newValue) {
m_unloadedVoltage = newValue[0];
m_loadedVoltage = newValue[1];
m_voltagePercentage = newValue[2];

// Formats an 8-bit unsigned integer as a voltage string.
// Upper 2 bits = whole voltage (0-3v), lower 6 bits = 1/64v increments.
// Example: 137 (0x89 = 0b10001001)
// - Upper 2 bits: 0b10 = 2v
// - Lower 6 bits: 0b001001 = 9 = 9/64 = 0.14v
// - Result: "2.14v"
auto formatVoltage = [](uint8_t value) -> std::string {
const uint8_t wholeVolts = (value >> 6) & 0x03; // Upper 2 bits
const uint8_t fractionalBits = value & 0x3F; // Lower 6 bits
const float totalVolts = wholeVolts + (fractionalBits / 64.0f);

char buffer[16];
snprintf(buffer, sizeof(buffer), "%.2fv", totalVolts);
return buffer;
};

XLOGD_TELEMETRY("Successfully read raw battery voltage characteristic, unloaded = %s, loaded = %s, percentage = %u%%",
formatVoltage(m_unloadedVoltage).c_str(), formatVoltage(m_loadedVoltage).c_str(), (unsigned int)m_voltagePercentage);
m_rawBatteryVoltageChangedSlots.invoke(newValue);
}

// -----------------------------------------------------------------------------
/*!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ class GattRemoteControlService : public BleRcuRemoteControlService
void requestRebootReason();
void requestAssertReport();
void requestLastKeypress();
void requestRawBatteryVoltage();
void requestAdvConfig();
void requestAdvConfigCustomList();

void onRawBatteryVoltageChanged(const std::vector<uint8_t> &newValue);
void onWriteAdvConfigReply(PendingReply<> *reply);
void onWriteAdvConfigCustomListReply(PendingReply<> *reply);

Expand All @@ -112,13 +114,17 @@ class GattRemoteControlService : public BleRcuRemoteControlService
std::shared_ptr<BleGattCharacteristic> m_advConfigCharacteristic;
std::shared_ptr<BleGattCharacteristic> m_advConfigCustomListCharacteristic;
std::shared_ptr<BleGattCharacteristic> m_assertReportCharacteristic;
std::shared_ptr<BleGattCharacteristic> m_rawBatteryVoltageCharacteristic;

StateMachine m_stateMachine;

uint8_t m_unpairReason;
uint8_t m_rebootReason;
uint8_t m_rcuAction;
uint8_t m_lastKeypress;
uint8_t m_unloadedVoltage;
uint8_t m_loadedVoltage;
uint8_t m_voltagePercentage;
uint8_t m_advConfig;
std::vector<uint8_t> m_advConfigCustomList;

Expand All @@ -135,6 +141,7 @@ class GattRemoteControlService : public BleRcuRemoteControlService
static const BleUuid m_advConfigCharUuid;
static const BleUuid m_advConfigCustomListCharUuid;
static const BleUuid m_assertReportCharUuid;
static const BleUuid m_rawBatteryVoltageCharUuid;

private:
static const Event::Type StartServiceRequestEvent = Event::Type(Event::User + 1);
Expand Down
1 change: 1 addition & 0 deletions src/ble/hal/utils/bleuuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ bool ble_uuid_names_init(void)
BleUuid(BleUuid::AdvertisingConfig, std::string("Advertising Config"));
BleUuid(BleUuid::AdvertisingConfigCustomList, std::string("Advertising Config Custom List"));
BleUuid(BleUuid::AssertReport, std::string("Assert Report"));
BleUuid(BleUuid::RawBatteryVoltage, std::string("Raw Battery Voltage"));
BleUuid(BleUuid::InfraredSignalReference, std::string("Infrared Signal Reference"));
BleUuid(BleUuid::InfraredSignalConfiguration, std::string("Infrared Signal Configuration"));
BleUuid(BleUuid::FirmwarePacketWindowSize, std::string("Firmware Packet Window Size"));
Expand Down
1 change: 1 addition & 0 deletions src/ble/hal/utils/bleuuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class BleUuid
AdvertisingConfig = 0xed05,
AdvertisingConfigCustomList = 0xed06,
AssertReport = 0xed07,
RawBatteryVoltage = 0xed08,
};

enum DescriptorType {
Expand Down