Skip to content

Commit

Permalink
Refactor battery table and return some information even if advanced i…
Browse files Browse the repository at this point in the history
…nformation is missing
  • Loading branch information
Marek Cirkos authored and fmanco committed Dec 7, 2018
1 parent bf4c0a3 commit 6a64e35
Showing 1 changed file with 114 additions and 119 deletions.
233 changes: 114 additions & 119 deletions osquery/tables/system/darwin/battery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -80,130 +80,125 @@
return (__bridge_transfer NSDictionary*)properties;
}

QueryData genBatteryInfo(QueryContext& context) {
QueryData results;
Row r;
@autoreleasepool {
NSDictionary* batteryInfo = getIopmBatteryInfo();
NSDictionary* advancedBatteryInfo = getIopmpsBatteryInfo();

// Don't return any rows if we don't have battery data.
if (batteryInfo == nullptr && advancedBatteryInfo == nullptr) {
return results;
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSManufacturerKey]) {
r["manufacturer"] = TEXT([[advancedBatteryInfo
objectForKey:@kIOPMPSManufacturerKey] UTF8String]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSManufactureDateKey]) {
// Date is published in a bitfield per the Smart Battery Data spec rev 1.1
// in section 5.1.26 Bits 0...4 => day (value 1-31; 5 bits) Bits 5...8 =>
// month (value 1-12; 4 bits) Bits 9...15 => years since 1980 (value
// 0-127; 7 bits)
int dateMask = [[advancedBatteryInfo
objectForKey:@kIOPMPSManufactureDateKey] intValue];
int day = dateMask & 31;
int month = (dateMask >> 5) & 15;
int year = (dateMask >> 9) + 1980;

NSCalendar* calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateComponents* components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
NSDate* date = [calendar dateFromComponents:components];

r["manufacture_date"] = INTEGER([date timeIntervalSince1970]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMDeviceNameKey]) {
r["model"] = TEXT(
[[advancedBatteryInfo objectForKey:@kIOPMDeviceNameKey] UTF8String]);
}

if ([batteryInfo objectForKey:@kIOPSHardwareSerialNumberKey]) {
r["serial_number"] = TEXT([
[batteryInfo objectForKey:@kIOPSHardwareSerialNumberKey] UTF8String]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSCycleCountKey]) {
r["cycle_count"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSCycleCountKey] intValue]);
}

if ([batteryInfo objectForKey:@kIOPSBatteryHealthKey]) {
r["health"] =
TEXT([[batteryInfo objectForKey:@kIOPSBatteryHealthKey] UTF8String]);
}

if ([batteryInfo objectForKey:@kIOPSBatteryHealthConditionKey]) {
r["condition"] = TEXT([[batteryInfo
objectForKey:@kIOPSBatteryHealthConditionKey] UTF8String]);
} else {
r["condition"] = TEXT("Normal");
}

if ([batteryInfo objectForKey:@kIOPSPowerSourceStateKey]) {
r["state"] = TEXT(
[[batteryInfo objectForKey:@kIOPSPowerSourceStateKey] UTF8String]);
}

if ([batteryInfo objectForKey:@kIOPSIsChargingKey]) {
r["charging"] =
INTEGER([[batteryInfo objectForKey:@kIOPSIsChargingKey] intValue]);
}

if ([batteryInfo objectForKey:@kIOPSIsChargedKey]) {
r["charged"] = INTEGER(1);
} else {
r["charged"] = INTEGER(0);
}

if ([advancedBatteryInfo objectForKey:@"DesignCapacity"]) {
r["designed_capacity"] = INTEGER(
[[advancedBatteryInfo objectForKey:@"DesignCapacity"] intValue]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSMaxCapacityKey]) {
r["max_capacity"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSMaxCapacityKey] intValue]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSCurrentCapacityKey]) {
r["current_capacity"] = INTEGER([[advancedBatteryInfo
objectForKey:@kIOPMPSCurrentCapacityKey] intValue]);
}

if ([batteryInfo objectForKey:@kIOPSCurrentCapacityKey]) {
r["percent_remaining"] = INTEGER(
[[batteryInfo objectForKey:@kIOPSCurrentCapacityKey] intValue]);
}

if ([advancedBatteryInfo objectForKey:@kIOPMPSAmperageKey]) {
r["amperage"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSAmperageKey] intValue]);
}
BOOL genIopmBatteryInfo(Row& r) {
NSDictionary* batteryInfo = getIopmBatteryInfo();
if (batteryInfo == nullptr) {
return NO;
}
if ([batteryInfo objectForKey:@kIOPSHardwareSerialNumberKey]) {
r["serial_number"] = TEXT([
[batteryInfo objectForKey:@kIOPSHardwareSerialNumberKey] UTF8String]);
}
if ([batteryInfo objectForKey:@kIOPSBatteryHealthKey]) {
r["health"] =
TEXT([[batteryInfo objectForKey:@kIOPSBatteryHealthKey] UTF8String]);
}
if ([batteryInfo objectForKey:@kIOPSBatteryHealthConditionKey]) {
r["condition"] = TEXT([[batteryInfo
objectForKey:@kIOPSBatteryHealthConditionKey] UTF8String]);
} else {
r["condition"] = TEXT("Normal");
}
if ([batteryInfo objectForKey:@kIOPSPowerSourceStateKey]) {
r["state"] = TEXT(
[[batteryInfo objectForKey:@kIOPSPowerSourceStateKey] UTF8String]);
}
if ([batteryInfo objectForKey:@kIOPSIsChargingKey]) {
r["charging"] =
INTEGER([[batteryInfo objectForKey:@kIOPSIsChargingKey] intValue]);
}
if ([batteryInfo objectForKey:@kIOPSIsChargedKey]) {
r["charged"] = INTEGER(1);
} else {
r["charged"] = INTEGER(0);
}
if ([batteryInfo objectForKey:@kIOPSCurrentCapacityKey]) {
r["percent_remaining"] = INTEGER(
[[batteryInfo objectForKey:@kIOPSCurrentCapacityKey] intValue]);
}
if ([batteryInfo objectForKey:@kIOPSTimeToEmptyKey]) {
r["minutes_until_empty"] =
INTEGER([[batteryInfo objectForKey:@kIOPSTimeToEmptyKey] intValue]);
}
if ([batteryInfo objectForKey:@kIOPSTimeToFullChargeKey]) {
r["minutes_to_full_charge"] = INTEGER(
[[batteryInfo objectForKey:@kIOPSTimeToFullChargeKey] intValue]);
}
return YES;
}

if ([advancedBatteryInfo objectForKey:@kIOPSVoltageKey]) {
r["voltage"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPSVoltageKey] intValue]);
}
BOOL genAdvancedBatteryInfo(Row& r) {
NSDictionary* advancedBatteryInfo = getIopmpsBatteryInfo();
if (advancedBatteryInfo == nullptr) {
return NO;
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSManufacturerKey]) {
r["manufacturer"] = TEXT([[advancedBatteryInfo
objectForKey:@kIOPMPSManufacturerKey] UTF8String]);
}

if ([batteryInfo objectForKey:@kIOPSTimeToEmptyKey]) {
r["minutes_until_empty"] =
INTEGER([[batteryInfo objectForKey:@kIOPSTimeToEmptyKey] intValue]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSManufactureDateKey]) {
// Date is published in a bitfield per the Smart Battery Data spec rev 1.1
// in section 5.1.26 Bits 0...4 => day (value 1-31; 5 bits) Bits 5...8 =>
// month (value 1-12; 4 bits) Bits 9...15 => years since 1980 (value
// 0-127; 7 bits)
int dateMask = [[advancedBatteryInfo
objectForKey:@kIOPMPSManufactureDateKey] intValue];
int day = dateMask & 31;
int month = (dateMask >> 5) & 15;
int year = (dateMask >> 9) + 1980;

NSCalendar* calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSDateComponents* components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
NSDate* date = [calendar dateFromComponents:components];

r["manufacture_date"] = INTEGER([date timeIntervalSince1970]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMDeviceNameKey]) {
r["model"] = TEXT(
[[advancedBatteryInfo objectForKey:@kIOPMDeviceNameKey] UTF8String]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSCycleCountKey]) {
r["cycle_count"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSCycleCountKey] intValue]);
}
if ([advancedBatteryInfo objectForKey:@"DesignCapacity"]) {
r["designed_capacity"] = INTEGER(
[[advancedBatteryInfo objectForKey:@"DesignCapacity"] intValue]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSMaxCapacityKey]) {
r["max_capacity"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSMaxCapacityKey] intValue]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSCurrentCapacityKey]) {
r["current_capacity"] = INTEGER([[advancedBatteryInfo
objectForKey:@kIOPMPSCurrentCapacityKey] intValue]);
}
if ([advancedBatteryInfo objectForKey:@kIOPMPSAmperageKey]) {
r["amperage"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPMPSAmperageKey] intValue]);
}
if ([advancedBatteryInfo objectForKey:@kIOPSVoltageKey]) {
r["voltage"] = INTEGER(
[[advancedBatteryInfo objectForKey:@kIOPSVoltageKey] intValue]);
}
return YES;
}

if ([batteryInfo objectForKey:@kIOPSTimeToFullChargeKey]) {
r["minutes_to_full_charge"] = INTEGER(
[[batteryInfo objectForKey:@kIOPSTimeToFullChargeKey] intValue]);
QueryData genBatteryInfo(QueryContext& context) {
Row row;
@autoreleasepool {
if (genIopmBatteryInfo(row)){
genAdvancedBatteryInfo(row);
}
}
results.push_back(r);
QueryData results;
results.push_back(row);
return results;
}

Expand Down

0 comments on commit 6a64e35

Please sign in to comment.