mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-04-10 05:15:02 +00:00
[Linux] Detect which pod is the primary
This commit is contained in:
@@ -26,7 +26,7 @@ public:
|
|||||||
// Struct to hold battery level and status
|
// Struct to hold battery level and status
|
||||||
struct BatteryState
|
struct BatteryState
|
||||||
{
|
{
|
||||||
int level = 0; // Battery level (0-100), -1 if unknown
|
quint8 level = 0; // Battery level (0-100), 0 if unknown
|
||||||
BatteryStatus status = BatteryStatus::Unknown;
|
BatteryStatus status = BatteryStatus::Unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ public:
|
|||||||
states[Component::Case] = {};
|
states[Component::Case] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the battery status packet
|
// Parse the battery status packet and detect primary/secondary pods
|
||||||
bool parsePacket(const QByteArray &packet)
|
bool parsePacket(const QByteArray &packet)
|
||||||
{
|
{
|
||||||
if (!packet.startsWith(AirPodsPackets::Parse::BATTERY_STATUS))
|
if (!packet.startsWith(AirPodsPackets::Parse::BATTERY_STATUS))
|
||||||
@@ -53,10 +53,12 @@ public:
|
|||||||
return false; // Invalid count or size mismatch
|
return false; // Invalid count or size mismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy current states; only included components will be updated
|
|
||||||
QMap<Component, BatteryState> newStates = states;
|
QMap<Component, BatteryState> newStates = states;
|
||||||
|
|
||||||
// Parse each component
|
// Track pods to determine primary and secondary based on order
|
||||||
|
QList<Component> podsInPacket;
|
||||||
|
podsInPacket.reserve(2);
|
||||||
|
|
||||||
for (quint8 i = 0; i < batteryCount; ++i)
|
for (quint8 i = 0; i < batteryCount; ++i)
|
||||||
{
|
{
|
||||||
int offset = 7 + (5 * i);
|
int offset = 7 + (5 * i);
|
||||||
@@ -69,19 +71,32 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map byte value to component
|
|
||||||
Component comp = static_cast<Component>(type);
|
Component comp = static_cast<Component>(type);
|
||||||
|
auto level = static_cast<quint8>(packet[offset + 2]);
|
||||||
// Extract level and status
|
|
||||||
int level = static_cast<quint8>(packet[offset + 2]);
|
|
||||||
auto status = static_cast<BatteryStatus>(packet[offset + 3]);
|
auto status = static_cast<BatteryStatus>(packet[offset + 3]);
|
||||||
|
|
||||||
// Update the state for this component
|
|
||||||
newStates[comp] = {level, status};
|
newStates[comp] = {level, status};
|
||||||
|
|
||||||
|
// If this is a pod (Left or Right), add it to the list
|
||||||
|
if (comp == Component::Left || comp == Component::Right)
|
||||||
|
{
|
||||||
|
podsInPacket.append(comp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply updates; unmentioned components retain old states
|
// Update states
|
||||||
states = newStates;
|
states = newStates;
|
||||||
|
|
||||||
|
// Set primary and secondary pods based on order
|
||||||
|
if (!podsInPacket.isEmpty())
|
||||||
|
{
|
||||||
|
primaryPod = podsInPacket[0]; // First pod is primary
|
||||||
|
}
|
||||||
|
if (podsInPacket.size() >= 2)
|
||||||
|
{
|
||||||
|
secondaryPod = podsInPacket[1]; // Second pod is secondary
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +110,7 @@ public:
|
|||||||
QString getComponentStatus(Component comp) const
|
QString getComponentStatus(Component comp) const
|
||||||
{
|
{
|
||||||
BatteryState state = getState(comp);
|
BatteryState state = getState(comp);
|
||||||
if (state.level == -1)
|
if (state.level == 0)
|
||||||
{
|
{
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
@@ -122,6 +137,11 @@ public:
|
|||||||
return QString("%1% (%2)").arg(state.level).arg(statusStr);
|
return QString("%1% (%2)").arg(state.level).arg(statusStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Component getPrimaryPod() const { return primaryPod; }
|
||||||
|
Component getSecondaryPod() const { return secondaryPod; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMap<Component, BatteryState> states;
|
QMap<Component, BatteryState> states;
|
||||||
|
Component primaryPod;
|
||||||
|
Component secondaryPod;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user