Skip to content

Commit 0eb76e9

Browse files
committed
Battery (macOS): improve manufacture info detection
1 parent 8b1ee64 commit 0eb76e9

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/detection/battery/battery_apple.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
4444

4545
ffCfDictGetString(properties, CFSTR(kIOPMDeviceNameKey), &battery->modelName);
4646
ffCfDictGetString(properties, CFSTR(kIOPMPSSerialKey), &battery->serial);
47+
ffCfDictGetString(properties, CFSTR(kIOPMPSManufacturerKey), &battery->manufacturer);
4748

4849
if (!ffCfDictGetBool(properties, CFSTR("built-in"), &boolValue) && boolValue)
4950
{
50-
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
51+
if (!battery->manufacturer.length)
52+
ffStrbufAppendS(&battery->manufacturer, "Apple Inc.");
5153
ffStrbufAppendS(&battery->technology, "Lithium");
5254
if (!battery->modelName.length)
5355
ffStrbufAppendS(&battery->modelName, "Built-in");
@@ -68,17 +70,28 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
6870
ffStrbufTrimRight(&battery->status, ' ');
6971
ffStrbufTrimRight(&battery->status, ',');
7072

71-
CFDictionaryRef batteryData;
72-
if (ffCfDictGetDict(properties, CFSTR("BatteryData"), &batteryData) == NULL)
73+
int sbdsManufactureDate = 0;
74+
if (ffCfDictGetInt(properties, CFSTR(kIOPMPSManufactureDateKey), &sbdsManufactureDate) == NULL)
7375
{
74-
char manufactureDate[sizeof(uint64_t)];
75-
if (ffCfDictGetInt64(batteryData, CFSTR(kIOPMPSManufactureDateKey), (int64_t*) manufactureDate) == NULL)
76+
int day = sbdsManufactureDate & 0b11111;
77+
int month = (sbdsManufactureDate >> 5) & 0b1111;
78+
int year = (sbdsManufactureDate >> 9) + 1800;
79+
ffStrbufSetF(&battery->manufactureDate, "%.4d-%.2d-%.2d", year, month, day);
80+
}
81+
else
82+
{
83+
CFDictionaryRef batteryData;
84+
if (ffCfDictGetDict(properties, CFSTR("BatteryData"), &batteryData) == NULL)
7685
{
77-
// https://github.com/AsahiLinux/linux/blob/b5c05cbffb0488c7618106926d522cc3b43d93d5/drivers/power/supply/macsmc_power.c#L410-L419
78-
int year = (manufactureDate[0] - '0') * 10 + (manufactureDate[1] - '0') + 2000 - 8;
79-
int month = (manufactureDate[2] - '0') * 10 + (manufactureDate[3] - '0');
80-
int day = (manufactureDate[4] - '0') * 10 + (manufactureDate[3] - '5');
81-
ffStrbufSetF(&battery->manufactureDate, "%.4d-%.2d-%.2d", year, month, day);
86+
char manufactureDate[sizeof(uint64_t)];
87+
if (ffCfDictGetInt64(batteryData, CFSTR(kIOPMPSManufactureDateKey), (int64_t*) manufactureDate) == NULL)
88+
{
89+
// https://github.com/AsahiLinux/linux/blob/b5c05cbffb0488c7618106926d522cc3b43d93d5/drivers/power/supply/macsmc_power.c#L410-L419
90+
int year = (manufactureDate[0] - '0') * 10 + (manufactureDate[1] - '0') + 2000 - 8;
91+
int month = (manufactureDate[2] - '0') * 10 + (manufactureDate[3] - '0');
92+
int day = (manufactureDate[4] - '0') * 10 + (manufactureDate[3] - '5');
93+
ffStrbufSetF(&battery->manufactureDate, "%.4d-%.2d-%.2d", year, month, day);
94+
}
8295
}
8396
}
8497

src/detection/battery/battery_windows.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ const char* detectBySmbios(FFBatteryResult* battery)
203203
}
204204
else if (data->Header.Length > offsetof(FFSmbiosPortableBattery, SbdsManufactureDate))
205205
{
206-
uint16_t day = data->SbdsManufactureDate & 0b11111;
207-
uint16_t month = (data->SbdsManufactureDate >> 5) & 0b1111;
208-
uint16_t year = (data->SbdsManufactureDate >> 9) + 1800;
206+
int day = data->SbdsManufactureDate & 0b11111;
207+
int month = (data->SbdsManufactureDate >> 5) & 0b1111;
208+
int year = (data->SbdsManufactureDate >> 9) + 1800;
209209
ffStrbufSetF(&battery->manufactureDate, "%.4d-%.2d-%.2d", year, month, day);
210210
}
211211

0 commit comments

Comments
 (0)