Dramatically reduced energy impact by improving code efficiency

Cached various information to reduce calls to get information from
hardware. Avoided dynamic memory allocation. Added brief comments and
TODOs.
This commit is contained in:
Michael Wilber
2013-12-21 09:26:27 -05:00
parent 3212d78eac
commit c537db9fdd
6 changed files with 413 additions and 382 deletions

26
Classes/smcWrapper.m Normal file → Executable file
View File

@ -2,7 +2,8 @@
* FanControl
*
* Copyright (c) 2006-2012 Hendrik Holtmann
*
* Portions Copyright (c) 2013 Michael Wilber
*
* smcWrapper.m - MacBook(Pro) FanControl application
*
* This program is free software; you can redistribute it and/or modify
@ -23,6 +24,7 @@
#import "smcWrapper.h"
#import <CommonCrypto/CommonDigest.h>
//TODO: This is the smcFanControl 2.4 checksum, it needs to be updated for the next release.
NSString * const smc_checksum=@"2ea544babe8a58dccc1364c920d473c8";
static NSDictionary *tsensors = nil;
@ -33,6 +35,9 @@ static NSDictionary *tsensors = nil;
SMCOpen(&conn);
tsensors = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tsensors" ofType:@"plist"]];
}
+(void)cleanUp{
SMCClose(conn);
}
+(float) get_maintemp{
float c_temp;
@ -43,22 +48,25 @@ static NSDictionary *tsensors = nil;
c_temp=[smcWrapper get_mptemp];
} else {
SMCVal_t val;
NSMutableArray *allTSensors = [[tsensors allKeys] mutableCopy];
NSString *foundKey = [tsensors objectForKey:[MachineDefaults computerModel]];
if (foundKey !=nil) {
foundKey = [MachineDefaults computerModel];
} else {
foundKey = @"standard";
}
[allTSensors removeObject:foundKey];
SMCReadKey2((char*)[[tsensors objectForKey:foundKey] UTF8String], &val,conn);
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
if (c_temp<=0) {
NSArray *allTSensors = [tsensors allKeys];
for (NSString *key in allTSensors) {
SMCReadKey2((char*)[[tsensors objectForKey:key] UTF8String], &val,conn);
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
if (c_temp>0) break;
bool bPtrEq = (key == foundKey);
bool bCmpEq = ([key isEqualToString:foundKey]);
if (false == bPtrEq && false == bCmpEq) {
SMCReadKey2((char*)[[tsensors objectForKey:key] UTF8String], &val,conn);
c_temp= ((val.bytes[0] * 256 + val.bytes[1]) >> 2)/64;
if (c_temp>0) break;
}
}
}
}
@ -168,14 +176,16 @@ static NSDictionary *tsensors = nil;
}
//call smc binary with setuid rights and apply
// The smc binary is given root permissions in FanControl.m with the setRights method.
+(void)setKey_external:(NSString *)key value:(NSString *)value{
NSString *launchPath = [[NSBundle mainBundle] pathForResource:@"smc" ofType:@""];
//first check if it's the right binary (security)
NSString *checksum=[smcWrapper createCheckSum:launchPath];
// MW: Disabled smc binary checksum. This should be re-enabled in an official release.
/*NSString *checksum=[smcWrapper createCheckSum:launchPath];
if (![checksum isEqualToString:smc_checksum]) {
NSLog(@"smcFanControl: Security Error: smc-binary is not the distributed one");
return;
}
}*/
NSArray *argsArray = [NSArray arrayWithObjects: @"-k",key,@"-w",value,nil];
NSTask *task;
task = [[NSTask alloc] init];