Skip to content

Commit 3b1e91b

Browse files
authored
Merge pull request #86 from tim-gromeyer/linux-detect-primary
[Linux] Detect which pod is the primary
2 parents d79ce02 + ce3c12f commit 3b1e91b

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

linux/battery.hpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Battery
2626
// Struct to hold battery level and status
2727
struct BatteryState
2828
{
29-
int level = 0; // Battery level (0-100), -1 if unknown
29+
quint8 level = 0; // Battery level (0-100), 0 if unknown
3030
BatteryStatus status = BatteryStatus::Unknown;
3131
};
3232

@@ -38,7 +38,7 @@ class Battery
3838
states[Component::Case] = {};
3939
}
4040

41-
// Parse the battery status packet
41+
// Parse the battery status packet and detect primary/secondary pods
4242
bool parsePacket(const QByteArray &packet)
4343
{
4444
if (!packet.startsWith(AirPodsPackets::Parse::BATTERY_STATUS))
@@ -53,10 +53,12 @@ class Battery
5353
return false; // Invalid count or size mismatch
5454
}
5555

56-
// Copy current states; only included components will be updated
5756
QMap<Component, BatteryState> newStates = states;
5857

59-
// Parse each component
58+
// Track pods to determine primary and secondary based on order
59+
QList<Component> podsInPacket;
60+
podsInPacket.reserve(2);
61+
6062
for (quint8 i = 0; i < batteryCount; ++i)
6163
{
6264
int offset = 7 + (5 * i);
@@ -69,19 +71,32 @@ class Battery
6971
return false;
7072
}
7173

72-
// Map byte value to component
7374
Component comp = static_cast<Component>(type);
74-
75-
// Extract level and status
76-
int level = static_cast<quint8>(packet[offset + 2]);
75+
auto level = static_cast<quint8>(packet[offset + 2]);
7776
auto status = static_cast<BatteryStatus>(packet[offset + 3]);
7877

79-
// Update the state for this component
8078
newStates[comp] = {level, status};
79+
80+
// If this is a pod (Left or Right), add it to the list
81+
if (comp == Component::Left || comp == Component::Right)
82+
{
83+
podsInPacket.append(comp);
84+
}
8185
}
8286

83-
// Apply updates; unmentioned components retain old states
87+
// Update states
8488
states = newStates;
89+
90+
// Set primary and secondary pods based on order
91+
if (!podsInPacket.isEmpty())
92+
{
93+
primaryPod = podsInPacket[0]; // First pod is primary
94+
}
95+
if (podsInPacket.size() >= 2)
96+
{
97+
secondaryPod = podsInPacket[1]; // Second pod is secondary
98+
}
99+
85100
return true;
86101
}
87102

@@ -95,7 +110,7 @@ class Battery
95110
QString getComponentStatus(Component comp) const
96111
{
97112
BatteryState state = getState(comp);
98-
if (state.level == -1)
113+
if (state.level == 0)
99114
{
100115
return "Unknown";
101116
}
@@ -122,6 +137,11 @@ class Battery
122137
return QString("%1% (%2)").arg(state.level).arg(statusStr);
123138
}
124139

140+
Component getPrimaryPod() const { return primaryPod; }
141+
Component getSecondaryPod() const { return secondaryPod; }
142+
125143
private:
126144
QMap<Component, BatteryState> states;
145+
Component primaryPod;
146+
Component secondaryPod;
127147
};

0 commit comments

Comments
 (0)