ibam.inl

filter: Statementstmt 0.0% 0 / 481 Branchbran 0.0% 0 / 592 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 1073 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Ibam.xs ↑ Summary blib/lib/Sys/Ibam.pm »
1 // IBAM, the Intelligent Battery Monitor
2 // Copyright (C) 2001-2003, Sebastian Ritterbusch (IBAM@Ritterbusch.de)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18  
19 #include <iostream>
20 #include <sstream>
21 #include <fstream>
22 #include <string>
23 #include <math.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <locale.h> // thus I may prevent evil krells to change to others..
29  
30 #include <sys/stat.h> // for mkdir
31 #include <sys/types.h> // for mkdir
32  
33 #define PMU_PWR_AC_PRESENT 0x00000001
34  
35 /* Arguments, with symbols from linux/apm_bios.h. Information is
36 + from the Get Power Status (0x0a) call unless otherwise noted.
37 +
38 + 0) Linux driver version (this will change if format changes)
39 + 1) APM BIOS Version. Usually 1.0 or 1.1.
40 + 2) APM flags from APM Installation Check (0x00):
41 + bit 0: APM_16_BIT_SUPPORT
42 + bit 1: APM_32_BIT_SUPPORT
43 + bit 2: APM_IDLE_SLOWS_CLOCK
44 + bit 3: APM_BIOS_DISABLED
45 + bit 4: APM_BIOS_DISENGAGED
46 + 3) AC line status
47 + 0x00: Off-line
48 + 0x01: On-line
49 + 0x02: On backup power (APM BIOS 1.1 only)
50 + 0xff: Unknown
51 + 4) Battery status
52 + 0x00: High
53 + 0x01: Low
54 + 0x02: Critical
55 + 0x03: Charging
56 + 0xff: Unknown
57 + 5) Battery flag
58 + bit 0: High
59 + bit 1: Low
60 + bit 2: Critical
61 + bit 3: Charging
62 + bit 7: No system battery
63 + 0xff: Unknown
64 + 6) Remaining battery life (percentage of charge):
65 + 0-100: valid
66 + -1: Unknown
67 + 7) Remaining battery life (time units):
68 + Number of remaining minutes or seconds
69 + -1: Unknown
70 + 8) min = minutes; sec = seconds */
71  
72 0 inline int battery_status::onBattery(void) const { return acLineStatus==0; }
73 0 inline int battery_status::charging(void) const { return chargeStatus; }
74 0 inline int battery_status::percent(void) const { return remainingBatteryPercent; }
75 0 inline int battery_status::seconds(void) const { return remainingBatteryLifeSeconds; }
76  
77 0 inline battery_status::battery_status(string path)
78 {
79 0 ▶ Path = path;
Branch: 1 branch
BranchTrueFalseTotal
Path = path; 0 0 0
80 0 }
81  
82 0 inline battery_status::~battery_status(void) { }
83  
84 inline void battery_status::update(void)
85 {
86 cout << "battery_status::update() called. This should never happen!" << endl;
87 }
88  
89 0 ▶inline apm_status::apm_status(string path) : battery_status(path)
Branch: 2 branches
BranchTrueFalseTotal
inline apm_status::apm_status(string path) : battery_status(path) 0 0 0
inline apm_status::apm_status(string path) : battery_status(path) 0 0 0
90 {
91 0 ▶ update();
Branch: 1 branch
BranchTrueFalseTotal
update(); 0 0 0
92 0 }
93  
94 0 ▶inline pmu_status::pmu_status(string path) : battery_status(path)
Branch: 2 branches
BranchTrueFalseTotal
inline pmu_status::pmu_status(string path) : battery_status(path) 0 0 0
inline pmu_status::pmu_status(string path) : battery_status(path) 0 0 0
95 {
96 0 ▶ update();
Branch: 1 branch
BranchTrueFalseTotal
update(); 0 0 0
97 0 }
98  
99 inline acpi_status::acpi_status(string path) : battery_status(path)
100 {
101 update();
102 }
103  
104 0 inline void apm_status::update(void)
105 {
106 0 ▶ ifstream in;
Branch: 1 branch
BranchTrueFalseTotal
ifstream in; 0 0 0
107 int i;
108 0 ▶ in.open(Path.c_str());
Branch: 1 branch
BranchTrueFalseTotal
in.open(Path.c_str()); 0 0 0
109 0 ▶ for(i=0;i<10 && in.fail();i++)
Branch: 4 branches
BranchTrueFalseTotal
for(i=0;i<10 && in.fail();i++) 0 0 0
for(i=0;i<10 && in.fail();i++) 0 0 0
for(i=0;i<10 && in.fail();i++) 0 0 0
for(i=0;i<10 && in.fail();i++) 0 0 0
110 0 ▶ in.open(Path.c_str());
Branch: 1 branch
BranchTrueFalseTotal
in.open(Path.c_str()); 0 0 0
111 0 ▶ if(in.fail())
Branch: 2 branches
BranchTrueFalseTotal
if(in.fail()) 0 0 0
if(in.fail()) 0 0 0
112 {
113 0 acLineStatus=0;
114 0 batteryStatus=0;
115 0 remainingBatteryPercent=-1;
116 0 remainingBatteryLifeSeconds=-1;
117 0 return;
118 }
119 0 string driverVersion, biosVersion;
120 int apmFlags, batteryFlag;
121 char c,d;
122 0 ▶ in >> driverVersion;
Branch: 1 branch
BranchTrueFalseTotal
in >> driverVersion; 0 0 0
123 0 ▶ in >> biosVersion;
Branch: 1 branch
BranchTrueFalseTotal
in >> biosVersion; 0 0 0
124 0 ▶ in >> c >> d; // 0x
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; // 0x 0 0 0
in >> c >> d; // 0x 0 0 0
125 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
126 0 ▶ apmFlags=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0');
Branch: 2 branches
BranchTrueFalseTotal
apmFlags=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
apmFlags=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
127 0 ▶ in >> c >> d; // 0x
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; // 0x 0 0 0
in >> c >> d; // 0x 0 0 0
128 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
129 0 ▶ acLineStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0');
Branch: 2 branches
BranchTrueFalseTotal
acLineStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
acLineStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
130 0 ▶ in >> c >> d; // 0x
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; // 0x 0 0 0
in >> c >> d; // 0x 0 0 0
131 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
132 0 ▶ batteryStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0');
Branch: 2 branches
BranchTrueFalseTotal
batteryStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
batteryStatus=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
133 0 ▶ in >> c >> d; // 0x
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; // 0x 0 0 0
in >> c >> d; // 0x 0 0 0
134 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
135 0 ▶ batteryFlag=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0');
Branch: 2 branches
BranchTrueFalseTotal
batteryFlag=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
batteryFlag=(c>'9'?c-'a'+10:c-'0')*16+(d>'9'?d-'a'+10:d-'0'); 0 0 0
136 0 chargeStatus = (batteryStatus&8)!=0;
137 0 ▶ in >> remainingBatteryPercent >> c; // %
Branch: 2 branches
BranchTrueFalseTotal
in >> remainingBatteryPercent >> c; // % 0 0 0
in >> remainingBatteryPercent >> c; // % 0 0 0
138 0 string minsec;
139 0 ▶ in >> remainingBatteryLifeSeconds >> minsec;
Branch: 2 branches
BranchTrueFalseTotal
in >> remainingBatteryLifeSeconds >> minsec; 0 0 0
in >> remainingBatteryLifeSeconds >> minsec; 0 0 0
140 0 ▶ if(minsec=="min") remainingBatteryLifeSeconds*=60;
Branch: 2 branches
BranchTrueFalseTotal
if(minsec=="min") remainingBatteryLifeSeconds*=60; 0 0 0
if(minsec=="min") remainingBatteryLifeSeconds*=60; 0 0 0
141 #ifdef DEBUG
142 cout << "Driver Version: " << driverVersion << endl;
143 cout << "Bios Version: " << biosVersion << endl;
144 cout << "APM Flags: " << apmFlags << endl;
145 cout << "AC Line Status: " << acLineStatus << endl;
146 cout << "Battery Status: " << batteryStatus << endl;
147 cout << "Battery Flag: " << batteryFlag << endl;
148 cout << "Remaining Percent: " << remainingBatteryPercent << endl;
149 cout << "Remaining Seconds: " << remainingBatteryLifeSeconds << endl;
150 #endif
151 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
152  
153 0 inline void pmu_status::update(void)
154 {
155 0 ▶ ifstream in;
Branch: 1 branch
BranchTrueFalseTotal
ifstream in; 0 0 0
156 int i;
157 0 ▶ in.open((Path+"/info").c_str());
Branch: 2 branches
BranchTrueFalseTotal
in.open((Path+"/info").c_str()); 0 0 0
in.open((Path+"/info").c_str()); 0 0 0
158 0 ▶ for (i = 0; i < 10 && in.fail(); i++)
Branch: 4 branches
BranchTrueFalseTotal
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
159 0 ▶ in.open((Path+"/info").c_str());
Branch: 2 branches
BranchTrueFalseTotal
in.open((Path+"/info").c_str()); 0 0 0
in.open((Path+"/info").c_str()); 0 0 0
160 0 ▶ if (in.fail())
Branch: 2 branches
BranchTrueFalseTotal
if (in.fail()) 0 0 0
if (in.fail()) 0 0 0
161 {
162 0 acLineStatus = 0;
163 0 chargeStatus = 0;
164 0 remainingBatteryLifeSeconds = -1;
165 0 remainingBatteryPercent = -1;
166 0 return;
167 }
168  
169 0 ▶ stringbuf buf;
Branch: 1 branch
BranchTrueFalseTotal
stringbuf buf; 0 0 0
170 char c;
171 0 int d, cur_charge = 0, max_charge = 0;
172 0 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 0 0 0
173 0 ▶ in.get(buf, ':');
Branch: 1 branch
BranchTrueFalseTotal
in.get(buf, ':'); 0 0 0
174 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
175 0 ▶ if (i == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 2) 0 0 0
176 0 acLineStatus = d;
177 }
178 0 ▶ in.close();
Branch: 1 branch
BranchTrueFalseTotal
in.close(); 0 0 0
179 0 ▶ in.open((Path+"/battery_0").c_str());
Branch: 2 branches
BranchTrueFalseTotal
in.open((Path+"/battery_0").c_str()); 0 0 0
in.open((Path+"/battery_0").c_str()); 0 0 0
180 0 ▶ for (i = 0; i < 10 && in.fail(); i++)
Branch: 4 branches
BranchTrueFalseTotal
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
for (i = 0; i < 10 && in.fail(); i++) 0 0 0
181 0 ▶ in.open((Path+"/battery_0").c_str());
Branch: 2 branches
BranchTrueFalseTotal
in.open((Path+"/battery_0").c_str()); 0 0 0
in.open((Path+"/battery_0").c_str()); 0 0 0
182 0 ▶ if (in.fail()) {
Branch: 2 branches
BranchTrueFalseTotal
if (in.fail()) { 0 0 0
if (in.fail()) { 0 0 0
183 0 acLineStatus = 0;
184 0 chargeStatus = 0;
185 0 remainingBatteryLifeSeconds = -1;
186 0 remainingBatteryPercent = -1;
187 0 return;
188 }
189  
190 0 ▶ for (i = 0; i < 6; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 6; i++) { 0 0 0
191 0 ▶ in.get(buf, ':');
Branch: 1 branch
BranchTrueFalseTotal
in.get(buf, ':'); 0 0 0
192 0 ▶ in >> c >> d;
Branch: 2 branches
BranchTrueFalseTotal
in >> c >> d; 0 0 0
in >> c >> d; 0 0 0
193 0 ▶ if (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) 0 0 0
194 0 chargeStatus = (d&PMU_PWR_AC_PRESENT)==0;
195 0 ▶ if (i == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 1) 0 0 0
196 0 cur_charge = d;
197 0 ▶ if (i == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 2) 0 0 0
198 0 max_charge = d;
199 0 ▶ if (i == 5)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 5) 0 0 0
200 0 remainingBatteryLifeSeconds = d;
201 }
202  
203 0 remainingBatteryPercent = (int)(cur_charge*100/max_charge);
204 0 ▶}
Branch: 2 branches
BranchTrueFalseTotal
} 0 0 0
} 0 0 0
205  
206 inline void acpi_status::update(void)
207 {
208  
209 }
210  
211 0 inline void percent_data::size_to(int newpercents)
212 {
213 0 ▶ if(newpercents>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(newpercents>=maxpercents) 0 0 0
214 {
215 0 newpercents++;
216 0 ▶ double *time_for=new double[newpercents];
Branch: 1 branch
BranchTrueFalseTotal
double *time_for=new double[newpercents]; 0 0 0
217 0 ▶ double *time_deriv=new double[newpercents];
Branch: 1 branch
BranchTrueFalseTotal
double *time_deriv=new double[newpercents]; 0 0 0
218 0 ▶ int *samples=new int[newpercents];
Branch: 1 branch
BranchTrueFalseTotal
int *samples=new int[newpercents]; 0 0 0
219 int i;
220 0 ▶ for(i=0;i<maxpercents;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<maxpercents;i++) 0 0 0
221 {
222 0 time_for[i]=time_for_percent[i];
223 0 time_deriv[i]=time_deriv_for_percent[i];
224 0 samples[i]=time_samples[i];
225 }
226 0 ▶ for(;i<newpercents;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(;i<newpercents;i++) 0 0 0
227 0 time_for[i]=time_deriv[i]=samples[i]=0;
228
229 0 ▶ delete [] time_for_percent;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_for_percent; 0 0 0
230 0 ▶ delete [] time_deriv_for_percent;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_deriv_for_percent; 0 0 0
231 0 ▶ delete [] time_samples;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_samples; 0 0 0
232 0 time_for_percent=time_for;
233 0 time_deriv_for_percent=time_deriv;
234 0 time_samples=samples;
235 0 maxpercents=newpercents;
236 }
237 0 }
238
239 0 inline percent_data::percent_data(void) : maxpercents(101),
240 0 ▶ time_for_percent(new double[maxpercents]),
Branch: 1 branch
BranchTrueFalseTotal
time_for_percent(new double[maxpercents]), 0 0 0
241 0 ▶ time_deriv_for_percent(new double[maxpercents]),
Branch: 1 branch
BranchTrueFalseTotal
time_deriv_for_percent(new double[maxpercents]), 0 0 0
242 0 ▶ time_samples(new int[maxpercents])
Branch: 1 branch
BranchTrueFalseTotal
time_samples(new int[maxpercents]) 0 0 0
243 {
244 int i;
245 0 ▶ for(i=0;i<maxpercents;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<maxpercents;i++) 0 0 0
246 0 time_for_percent[i]=time_deriv_for_percent[i]=time_samples[i]=0;
247 0 }
248 0 inline percent_data::~percent_data(void)
249 {
250 0 ▶ delete [] time_for_percent;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_for_percent; 0 0 0
251 0 ▶ delete [] time_deriv_for_percent;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_deriv_for_percent; 0 0 0
252 0 ▶ delete [] time_samples;
Branch: 1 branch
BranchTrueFalseTotal
delete [] time_samples; 0 0 0
253 0 }
254 0 inline ostream & operator <<(ostream & o,const percent_data & a)
255 {
256 int i;
257 0 setlocale(LC_ALL,"en_US");
258 0 ▶ for(i=a.maxpercents-1;i>=0;i--)
Branch: 1 branch
BranchTrueFalseTotal
for(i=a.maxpercents-1;i>=0;i--) 0 0 0
259 0 ▶ if(a.time_samples[i])
Branch: 1 branch
BranchTrueFalseTotal
if(a.time_samples[i]) 0 0 0
260 {
261 0 ▶ if(a.time_deriv_for_percent[i]<0)
Branch: 1 branch
BranchTrueFalseTotal
if(a.time_deriv_for_percent[i]<0) 0 0 0
262 0 a.time_deriv_for_percent[i]=0;
263 0 o << i << '\t' << a.time_for_percent[i] << '\t' << sqrt(a.time_deriv_for_percent[i]) << '\t' << a.time_samples[i] << endl;
264 }
265 0 return o;
266 }
267 0 inline double percent_data::add_data(int percent,double time_for,int samples)
268 {
269 0 ▶ if(percent<0)
Branch: 1 branch
BranchTrueFalseTotal
if(percent<0) 0 0 0
270 0 return 0;
271 0 size_to(percent);
272 double ratio;
273 0 ▶ if(time_samples[percent])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[percent]) 0 0 0
274 0 ratio=time_for/time_for_percent[percent];
275 else
276 0 ratio=time_for/(IBAM_ASSUME_DEFAULT_BATTERY_MIN*60./100.);
277
278 0 double old_time_for_percent=time_for_percent[percent];
279
280 0 time_for_percent[percent]=
281 0 (time_for_percent[percent]*time_samples[percent]
282 0 +time_for*samples
283 0 )/(samples+time_samples[percent]);
284
285 0 time_deriv_for_percent[percent]=
286 0 ( (time_deriv_for_percent[percent]+old_time_for_percent*old_time_for_percent)*time_samples[percent]
287 0 +time_for*time_for*samples
288 0 )/(samples+time_samples[percent])-time_for_percent[percent]*time_for_percent[percent];
289
290 0 time_samples[percent]+=samples;
291 0 return ratio;
292 }
293 0 inline double percent_data::average(int a,int b) // average from a to b
294 {
295 0 ▶ if(a>b) { int c=a;a=b;b=c; }
Branch: 1 branch
BranchTrueFalseTotal
if(a>b) { int c=a;a=b;b=c; } 0 0 0
296 0 ▶ if(a<0)
Branch: 1 branch
BranchTrueFalseTotal
if(a<0) 0 0 0
297 {
298 0 a=0;
299 0 ▶ if(b<0)
Branch: 1 branch
BranchTrueFalseTotal
if(b<0) 0 0 0
300 0 b=0;
301 }
302 0 ▶ if(b>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(b>=maxpercents) 0 0 0
303 {
304 0 b=maxpercents-1;
305 0 ▶ if(a>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(a>=maxpercents) 0 0 0
306 0 a=b;
307 }
308 int i;
309 0 double su(0);
310 0 int co(0);
311 0 ▶ for(i=a;i<=b;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=a;i<=b;i++) 0 0 0
312 {
313 0 ▶ if(time_samples[i])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[i]) 0 0 0
314 {
315 0 su+=time_for_percent[i]*time_samples[i];
316 0 co+=time_samples[i];
317 }
318 }
319 0 ▶ if(co)
Branch: 1 branch
BranchTrueFalseTotal
if(co) 0 0 0
320 0 return (su/co);
321 0 int gotdata=0;
322 0 ▶ for(a--,b++;(a>0 || b<maxpercents-1) && gotdata<2;a--,b++)
Branch: 3 branches
BranchTrueFalseTotal
for(a--,b++;(a>0 || b<maxpercents-1) && gotdata<2;a--,b++) 0 0 0
for(a--,b++;(a>0 || b<maxpercents-1) && gotdata<2;a--,b++) 0 0 0
for(a--,b++;(a>0 || b<maxpercents-1) && gotdata<2;a--,b++) 0 0 0
323 {
324 0 ▶ if(a<0)
Branch: 1 branch
BranchTrueFalseTotal
if(a<0) 0 0 0
325 0 a=0;
326 0 ▶ if(b>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(b>=maxpercents) 0 0 0
327 0 b=maxpercents-1;
328 0 su+=time_for_percent[a]*time_samples[a]
329 0 +time_for_percent[b]*time_samples[b];
330 0 co+=time_samples[a]+time_samples[b];
331 0 ▶ if(time_samples[a] || time_samples[b])
Branch: 2 branches
BranchTrueFalseTotal
if(time_samples[a] || time_samples[b]) 0 0 0
if(time_samples[a] || time_samples[b]) 0 0 0
332 0 gotdata++;
333 }
334 0 ▶ if(co)
Branch: 1 branch
BranchTrueFalseTotal
if(co) 0 0 0
335 0 return (su/co);
336  
337 0 return (IBAM_ASSUME_DEFAULT_BATTERY_MIN*60/100);
338 }
339  
340 0 inline double percent_data::add_data(int percent,double time_for,double time_deriv_for,int samples)
341 {
342 0 ▶ if(percent<0)
Branch: 1 branch
BranchTrueFalseTotal
if(percent<0) 0 0 0
343 0 return 0;
344 0 size_to(percent);
345 double ratio;
346 0 ▶ if(time_samples[percent])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[percent]) 0 0 0
347 0 ratio=time_for/time_for_percent[percent];
348 else
349 0 ratio=time_for/average(percent,percent);
350
351 0 double old_time_for_percent=time_for_percent[percent];
352
353 0 time_for_percent[percent]=
354 0 (time_for_percent[percent]*time_samples[percent]
355 0 +time_for*samples
356 0 )/(samples+time_samples[percent]);
357
358 0 time_deriv_for_percent[percent]=
359 0 ( (time_deriv_for_percent[percent]+old_time_for_percent*old_time_for_percent)*time_samples[percent]
360 0 + (time_deriv_for+time_for*time_for)*samples
361 0 )/(samples+time_samples[percent])-time_for_percent[percent]*time_for_percent[percent];
362
363 0 time_samples[percent]+=samples;
364 0 return ratio;
365 }
366 0 inline istream & operator >>(istream & i,percent_data &a)
367 {
368 0 setlocale(LC_ALL,"en_US");
369  
370 0 ▶ while(!i.fail() && !i.eof())
Branch: 3 branches
BranchTrueFalseTotal
while(!i.fail() && !i.eof()) 0 0 0
while(!i.fail() && !i.eof()) 0 0 0
while(!i.fail() && !i.eof()) 0 0 0
371 {
372 int percent;
373 0 double time_for(-1);
374 0 double time_deriv_for(-1);
375 int samples;
376 0 ▶ i >> percent >> time_for >> time_deriv_for>> samples;
Branch: 4 branches
BranchTrueFalseTotal
i >> percent >> time_for >> time_deriv_for>> samples; 0 0 0
i >> percent >> time_for >> time_deriv_for>> samples; 0 0 0
i >> percent >> time_for >> time_deriv_for>> samples; 0 0 0
i >> percent >> time_for >> time_deriv_for>> samples; 0 0 0
377 0 ▶ if(time_for>=0)
Branch: 1 branch
BranchTrueFalseTotal
if(time_for>=0) 0 0 0
378 0 ▶ a.add_data(percent,time_for,time_deriv_for*time_deriv_for,samples);
Branch: 1 branch
BranchTrueFalseTotal
a.add_data(percent,time_for,time_deriv_for*time_deriv_for,samples); 0 0 0
379 }
380 0 return i;
381 }
382 0 inline istream & percent_data::import(istream & i)
383 {
384 0 setlocale(LC_ALL,"en_US");
385  
386 0 percent_data & a(*this);
387 0 double maxval=0;
388 0 ▶ while(!i.fail() && !i.eof())
Branch: 3 branches
BranchTrueFalseTotal
while(!i.fail() && !i.eof()) 0 0 0
while(!i.fail() && !i.eof()) 0 0 0
while(!i.fail() && !i.eof()) 0 0 0
389 {
390 int val;
391 0 double time_for(-1);
392 int samples;
393 0 ▶ i >> val >> time_for >> samples;
Branch: 3 branches
BranchTrueFalseTotal
i >> val >> time_for >> samples; 0 0 0
i >> val >> time_for >> samples; 0 0 0
i >> val >> time_for >> samples; 0 0 0
394 0 ▶ if(val>maxval)
Branch: 1 branch
BranchTrueFalseTotal
if(val>maxval) 0 0 0
395 0 maxval=val;
396 0 ▶ if(time_for>=0)
Branch: 1 branch
BranchTrueFalseTotal
if(time_for>=0) 0 0 0
397 0 ▶ a.add_data(int(double(val)/maxval*100+.5),time_for*maxval/100,samples/10+1);
Branch: 1 branch
BranchTrueFalseTotal
a.add_data(int(double(val)/maxval*100+.5),time_for*maxval/100,samples/10+1); 0 0 0
398 }
399 0 return i;
400 }
401 0 inline double percent_data::remain(int percent)
402 {
403 0 double r=0;
404 0 size_to(percent);
405 int i;
406 0 ▶ for(i=percent;i>0;i--)
Branch: 1 branch
BranchTrueFalseTotal
for(i=percent;i>0;i--) 0 0 0
407 {
408 0 ▶ if(time_samples[i])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[i]) 0 0 0
409 0 r+=time_for_percent[i];
410 else
411 {
412 0 int down=i-15;
413 0 int up=i+15;
414 0 ▶ if(down<0)
Branch: 1 branch
BranchTrueFalseTotal
if(down<0) 0 0 0
415 0 down=0;
416 0 ▶ if(up>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(up>=maxpercents) 0 0 0
417 0 up=maxpercents-1;
418 0 r+=average(down,up);
419 }
420 }
421 0 return r;
422 }
423 0 inline double percent_data::inverted_remain(int percent)
424 {
425 0 double r=0;
426 0 size_to(percent);
427 int i;
428 0 ▶ for(i=percent+1;i<maxpercents;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=percent+1;i<maxpercents;i++) 0 0 0
429 {
430 0 ▶ if(time_samples[i])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[i]) 0 0 0
431 0 r+=time_for_percent[i];
432 else
433 {
434 0 int down=i-15;
435 0 int up=i+15;
436 0 ▶ if(down<0)
Branch: 1 branch
BranchTrueFalseTotal
if(down<0) 0 0 0
437 0 down=0;
438 0 ▶ if(up>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(up>=maxpercents) 0 0 0
439 0 up=maxpercents-1;
440 0 r+=average(down,up);
441 }
442 }
443 0 return r;
444 }
445 0 inline double percent_data::total(void)
446 {
447 0 double r=0;
448 int i;
449 0 ▶ for(i=maxpercents-1;i>0;i--)
Branch: 1 branch
BranchTrueFalseTotal
for(i=maxpercents-1;i>0;i--) 0 0 0
450 {
451 0 ▶ if(time_samples[i])
Branch: 1 branch
BranchTrueFalseTotal
if(time_samples[i]) 0 0 0
452 0 r+=time_for_percent[i];
453 else
454 {
455 0 int down=i-15;
456 0 int up=i+15;
457 0 ▶ if(down<0)
Branch: 1 branch
BranchTrueFalseTotal
if(down<0) 0 0 0
458 0 down=0;
459 0 ▶ if(up>=maxpercents)
Branch: 1 branch
BranchTrueFalseTotal
if(up>=maxpercents) 0 0 0
460 0 up=maxpercents-1;
461 0 r+=average(down,up);
462 }
463 }
464 0 return r;
465 }
466  
467 0 inline ibam::ibam(void) :
468 0 data_changed(0),
469 0 battery_loaded(0),battery_changed(0),
470 0 charge_loaded(0),charge_changed(0),
471 0 profile_changed(0),adaptive_damping_battery(15),
472 0 adaptive_damping_charge(15),
473 0 lasttime(time(NULL)),lastpercent(0),lastratio(1),
474 0 laststatus(-1),
475 0 currenttime(time(NULL)),isvalid(0),profile_logging(1),
476 0 ▶ profile_number(0),profile_active(0)
Branch: 2 branches
BranchTrueFalseTotal
profile_number(0),profile_active(0) 0 0 0
profile_number(0),profile_active(0) 0 0 0
477 {
478 0 ▶ string pmu_path = "/proc/pmu";
Branch: 1 branch
BranchTrueFalseTotal
string pmu_path = "/proc/pmu"; 0 0 0
479 0 ▶ ifstream pmu;
Branch: 1 branch
BranchTrueFalseTotal
ifstream pmu; 0 0 0
480 0 ▶ pmu.open((pmu_path+"/info").c_str());
Branch: 2 branches
BranchTrueFalseTotal
pmu.open((pmu_path+"/info").c_str()); 0 0 0
pmu.open((pmu_path+"/info").c_str()); 0 0 0
481 0 ▶ if (pmu.is_open()) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmu.is_open()) { 0 0 0
482 #ifdef DEBUG
483 cout << "using pmu" << endl;
484 #endif
485 0 ▶ pmu.close();
Branch: 1 branch
BranchTrueFalseTotal
pmu.close(); 0 0 0
486 0 ▶ apm = new pmu_status();
Branch: 4 branches
BranchTrueFalseTotal
apm = new pmu_status(); 0 0 0
apm = new pmu_status(); 0 0 0
apm = new pmu_status(); 0 0 0
apm = new pmu_status(); 0 0 0
487 } else {
488 #ifdef DEBUG
489 cout << "using apm" << endl;
490 #endif
491 0 ▶ apm = new apm_status();
Branch: 4 branches
BranchTrueFalseTotal
apm = new apm_status(); 0 0 0
apm = new apm_status(); 0 0 0
apm = new apm_status(); 0 0 0
apm = new apm_status(); 0 0 0
492 }
493 0 ▶ home=getenv("HOME");
Branch: 1 branch
BranchTrueFalseTotal
home=getenv("HOME"); 0 0 0
494 0 ▶ if(home!="")
Branch: 2 branches
BranchTrueFalseTotal
if(home!="") 0 0 0
if(home!="") 0 0 0
495 0 ▶ home+="/";
Branch: 1 branch
BranchTrueFalseTotal
home+="/"; 0 0 0
496 0 ▶ mkdir((home+".ibam").c_str(),0755);
Branch: 1 branch
BranchTrueFalseTotal
mkdir((home+".ibam").c_str(),0755); 0 0 0
497 0 ▶ ifstream in((home+".ibam/ibam.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ifstream in((home+".ibam/ibam.rc").c_str()); 0 0 0
ifstream in((home+".ibam/ibam.rc").c_str()); 0 0 0
498 0 string saveversion;
499 0 ▶ in >> saveversion;
Branch: 1 branch
BranchTrueFalseTotal
in >> saveversion; 0 0 0
500 0 ▶ if(saveversion==VERSION)
Branch: 2 branches
BranchTrueFalseTotal
if(saveversion==VERSION) 0 0 0
if(saveversion==VERSION) 0 0 0
501 0 ▶ in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active;
Branch: 9 branches
BranchTrueFalseTotal
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
in >> lasttime >> lastpercent >> lastratio >> laststatus >> adaptive_damping_battery >> adaptive_damping_charge >> profile_logging >> profile_number >> profile_active; 0 0 0
502 else
503 0 data_changed=1; // force update
504 0 ▶ in.close();
Branch: 1 branch
BranchTrueFalseTotal
in.close(); 0 0 0
505
506 0 ▶ if(adaptive_damping_battery<2)
Branch: 1 branch
BranchTrueFalseTotal
if(adaptive_damping_battery<2) 0 0 0
507 0 adaptive_damping_battery=2;
508 0 ▶ if(adaptive_damping_charge<2)
Branch: 1 branch
BranchTrueFalseTotal
if(adaptive_damping_charge<2) 0 0 0
509 0 adaptive_damping_charge=2;
510 0 ▶ if(adaptive_damping_battery>200)
Branch: 1 branch
BranchTrueFalseTotal
if(adaptive_damping_battery>200) 0 0 0
511 0 adaptive_damping_battery=200;
512 0 ▶ if(adaptive_damping_charge>200)
Branch: 1 branch
BranchTrueFalseTotal
if(adaptive_damping_charge>200) 0 0 0
513 0 adaptive_damping_charge=200;
514
515 0 currentpercent=apm->percent();
516 0 ▶ if(currentpercent!=-1)
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent!=-1) 0 0 0
517 0 isvalid=1;
518  
519 0 ▶ currentstatus=apm->onBattery()?1:apm->charging()?2:0;
Branch: 2 branches
BranchTrueFalseTotal
currentstatus=apm->onBattery()?1:apm->charging()?2:0; 0 0 0
currentstatus=apm->onBattery()?1:apm->charging()?2:0; 0 0 0
520  
521 0 ▶ if(currentstatus!=laststatus)
Branch: 1 branch
BranchTrueFalseTotal
if(currentstatus!=laststatus) 0 0 0
522 0 lastratio=1;
523 0 }
524  
525 0 inline void ibam::update(void)
526 {
527 0 save();
528 0 apm->update();
529 0 currenttime=time(NULL);
530 0 currentpercent=apm->percent();
531 0 ▶ if(currentpercent!=-1)
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent!=-1) 0 0 0
532 0 isvalid=1;
533 else
534 0 isvalid=0;
535
536 0 ▶ currentstatus=apm->onBattery()?1:apm->charging()?2:0;
Branch: 2 branches
BranchTrueFalseTotal
currentstatus=apm->onBattery()?1:apm->charging()?2:0; 0 0 0
currentstatus=apm->onBattery()?1:apm->charging()?2:0; 0 0 0
537  
538 0 ▶ if(currentstatus!=laststatus)
Branch: 1 branch
BranchTrueFalseTotal
if(currentstatus!=laststatus) 0 0 0
539 0 lastratio=1;
540 0 }
541  
542 0 inline int ibam::valid(void) const { return isvalid; }
543  
544 0 inline void ibam::import(void)
545 {
546 {
547 0 ▶ ifstream in(".ibam.battery.rc");
Branch: 1 branch
BranchTrueFalseTotal
ifstream in(".ibam.battery.rc"); 0 0 0
548 0 ▶ battery.import(in);
Branch: 1 branch
BranchTrueFalseTotal
battery.import(in); 0 0 0
549 0 battery_changed=1;
550 0 }
551 {
552 0 ▶ ifstream in(".ibam.charge.rc");
Branch: 1 branch
BranchTrueFalseTotal
ifstream in(".ibam.charge.rc"); 0 0 0
553 0 ▶ charge.import(in);
Branch: 1 branch
BranchTrueFalseTotal
charge.import(in); 0 0 0
554 0 charge_changed=1;
555 0 }
556 0 }
557  
558 0 inline void ibam::load_battery(void)
559 {
560 0 ▶ if(!battery_loaded)
Branch: 1 branch
BranchTrueFalseTotal
if(!battery_loaded) 0 0 0
561 {
562 0 ▶ ifstream in((home+".ibam/battery.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ifstream in((home+".ibam/battery.rc").c_str()); 0 0 0
ifstream in((home+".ibam/battery.rc").c_str()); 0 0 0
563 0 ▶ in >> battery;
Branch: 1 branch
BranchTrueFalseTotal
in >> battery; 0 0 0
564 0 battery_loaded=1;
565 0 }
566 0 }
567  
568 0 inline void ibam::load_charge(void)
569 {
570 0 ▶ if(!charge_loaded)
Branch: 1 branch
BranchTrueFalseTotal
if(!charge_loaded) 0 0 0
571 {
572 0 ▶ ifstream in((home+".ibam/charge.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ifstream in((home+".ibam/charge.rc").c_str()); 0 0 0
ifstream in((home+".ibam/charge.rc").c_str()); 0 0 0
573 0 ▶ in >> charge;
Branch: 1 branch
BranchTrueFalseTotal
in >> charge; 0 0 0
574 0 charge_loaded=1;
575 0 }
576 0 }
577  
578 0 inline void ibam::update_statistics(void)
579 {
580 0 ▶ if(currentstatus==laststatus &&
Branch: 1 branch
BranchTrueFalseTotal
if(currentstatus==laststatus && 0 0 0
581 0 ▶ currenttime-lasttime<IBAM_IGNORE_DATA_AFTER_X_SECONDS)
Branch: 1 branch
BranchTrueFalseTotal
currenttime-lasttime<IBAM_IGNORE_DATA_AFTER_X_SECONDS) 0 0 0
582 {
583 0 switch(currentstatus)
584 {
585 0 case 1: // on battery
586 0 ▶ if(currentpercent<lastpercent)
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent<lastpercent) 0 0 0
587 {
588 0 load_battery();
589 0 double sec_per_min=(currenttime-lasttime)/double(lastpercent-currentpercent);
590 0 double last_av=battery.average(currentpercent,lastpercent);
591
592 0 ▶ if(fabs(last_av-sec_per_min)<1.01*fabs(last_av*lastratio-sec_per_min))
Branch: 1 branch
BranchTrueFalseTotal
if(fabs(last_av-sec_per_min)<1.01*fabs(last_av*lastratio-sec_per_min)) 0 0 0
593 {
594 0 ▶ if((lastratio<1 && last_av<sec_per_min)
Branch: 2 branches
BranchTrueFalseTotal
if((lastratio<1 && last_av<sec_per_min) 0 0 0
if((lastratio<1 && last_av<sec_per_min) 0 0 0
595 0 ▶ || (lastratio>1 && last_av>sec_per_min))
Branch: 2 branches
BranchTrueFalseTotal
|| (lastratio>1 && last_av>sec_per_min)) 0 0 0
|| (lastratio>1 && last_av>sec_per_min)) 0 0 0
596 0 adaptive_damping_battery*=1.01;
597 else
598 0 adaptive_damping_battery*=.99;
599 }
600 0 ▶ if(sec_per_min>=IBAM_MINIMAL_SECONDS_PER_PERCENT
Branch: 1 branch
BranchTrueFalseTotal
if(sec_per_min>=IBAM_MINIMAL_SECONDS_PER_PERCENT 0 0 0
601 0 ▶ && sec_per_min<=IBAM_MAXIMAL_SECONDS_PER_PERCENT)
Branch: 1 branch
BranchTrueFalseTotal
&& sec_per_min<=IBAM_MAXIMAL_SECONDS_PER_PERCENT) 0 0 0
602 {
603 int i;
604 0 last_sec_per_min=sec_per_min;
605 0 last_sec_per_min_prediction=last_av;
606 0 profile_changed=1;
607
608 0 ▶ for(i=currentpercent;i<=lastpercent;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=currentpercent;i<=lastpercent;i++) 0 0 0
609 0 lastratio=(lastratio*adaptive_damping_battery+battery.add_data(i,sec_per_min))/(adaptive_damping_battery+1);
610 0 battery_changed=1;
611 0 data_changed=1;
612 }
613 } else
614 0 ▶ if(currentpercent>lastpercent) // strange data
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent>lastpercent) // strange data 0 0 0
615 {
616 0 data_changed=1; // discard
617 0 ▶ if(profile_logging && profile_active)
Branch: 2 branches
BranchTrueFalseTotal
if(profile_logging && profile_active) 0 0 0
if(profile_logging && profile_active) 0 0 0
618 0 profile_number++;
619 0 profile_active=0;
620 }
621 0 break;
622 0 case 2: // charging
623 0 ▶ if(currentpercent>lastpercent)
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent>lastpercent) 0 0 0
624 {
625 0 load_charge();
626 double sec_per_min;
627 0 sec_per_min=(currenttime-lasttime)/double(currentpercent-lastpercent);
628 0 double last_av=charge.average(lastpercent,currentpercent);
629
630 0 ▶ if(fabs(last_av-sec_per_min)<1.01*fabs(last_av/lastratio-sec_per_min))
Branch: 1 branch
BranchTrueFalseTotal
if(fabs(last_av-sec_per_min)<1.01*fabs(last_av/lastratio-sec_per_min)) 0 0 0
631 {
632 0 ▶ if((lastratio>1 && last_av<sec_per_min)
Branch: 2 branches
BranchTrueFalseTotal
if((lastratio>1 && last_av<sec_per_min) 0 0 0
if((lastratio>1 && last_av<sec_per_min) 0 0 0
633 0 ▶ || (lastratio<1 && last_av>sec_per_min))
Branch: 2 branches
BranchTrueFalseTotal
|| (lastratio<1 && last_av>sec_per_min)) 0 0 0
|| (lastratio<1 && last_av>sec_per_min)) 0 0 0
634 0 adaptive_damping_charge*=1.01;
635 else
636 0 adaptive_damping_charge*=.99;
637 }
638
639 0 ▶ if(sec_per_min<=IBAM_MAXIMAL_SECONDS_PER_PERCENT
Branch: 1 branch
BranchTrueFalseTotal
if(sec_per_min<=IBAM_MAXIMAL_SECONDS_PER_PERCENT 0 0 0
640 0 ▶ && sec_per_min>=IBAM_MINIMAL_SECONDS_PER_PERCENT)
Branch: 1 branch
BranchTrueFalseTotal
&& sec_per_min>=IBAM_MINIMAL_SECONDS_PER_PERCENT) 0 0 0
641 {
642 int i;
643 0 last_sec_per_min=sec_per_min;
644 0 last_sec_per_min_prediction=last_av;
645 0 profile_changed=1;
646
647 0 ▶ for(i=currentpercent;i>=lastpercent;i--)
Branch: 1 branch
BranchTrueFalseTotal
for(i=currentpercent;i>=lastpercent;i--) 0 0 0
648 0 lastratio=(lastratio*adaptive_damping_charge+1/charge.add_data(i,sec_per_min))/(adaptive_damping_charge+1);
649 0 charge_changed=1;
650 0 data_changed=1;
651 }
652 } else
653 0 ▶ if(currentpercent<lastpercent) // strange data
Branch: 1 branch
BranchTrueFalseTotal
if(currentpercent<lastpercent) // strange data 0 0 0
654 {
655 0 ▶ if(profile_logging && profile_active)
Branch: 2 branches
BranchTrueFalseTotal
if(profile_logging && profile_active) 0 0 0
if(profile_logging && profile_active) 0 0 0
656 0 profile_number++;
657 0 profile_active=0;
658  
659 0 data_changed=1; // discard
660 }
661 0 break;
662 0 default: // full or no battery
663 0 break;
664 }
665 0 } else
666 {
667 0 ▶ if(profile_logging && profile_active)
Branch: 2 branches
BranchTrueFalseTotal
if(profile_logging && profile_active) 0 0 0
if(profile_logging && profile_active) 0 0 0
668 0 profile_number++;
669 0 profile_active=0;
670 0 data_changed=1;
671 }
672 0 }
673  
674 0 inline void ibam::ignore_statistics(void)
675 {
676 0 data_changed=1;
677 0 }
678  
679 0 inline string ibam::profile_filename(int n,int type) const
680 {
681 char b[20];
682 0 char *status_text[4]={"full","battery","charge",""};
683 0 sprintf(b,"profile-%03d-%s",n,status_text[(type&3)]);
684 0 ▶ return (home+".ibam/")+b;
Branch: 2 branches
BranchTrueFalseTotal
return (home+".ibam/")+b; 0 0 0
return (home+".ibam/")+b; 0 0 0
685 }
686  
687 0 inline int ibam::current_profile_number(void) const { return profile_number; }
688 0 inline int ibam::current_profile_type(void) const { return currentstatus; }
689  
690 0 inline void ibam::save(void)
691 {
692 0 ▶ if(profile_changed && profile_logging)
Branch: 2 branches
BranchTrueFalseTotal
if(profile_changed && profile_logging) 0 0 0
if(profile_changed && profile_logging) 0 0 0
693 {
694 0 profile_number%=IBAM_MAXIMAL_PROFILES;
695 0 ▶ string filename=profile_filename(profile_number,currentstatus);
Branch: 1 branch
BranchTrueFalseTotal
string filename=profile_filename(profile_number,currentstatus); 0 0 0
696 0 ▶ ofstream out(filename.c_str(),ios::app);
Branch: 1 branch
BranchTrueFalseTotal
ofstream out(filename.c_str(),ios::app); 0 0 0
697 0 ▶ out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl;
Branch: 6 branches
BranchTrueFalseTotal
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
out << currentpercent << '\t' << last_sec_per_min << '\t' << last_sec_per_min_prediction << endl; 0 0 0
698 0 ▶ if(profile_active==0)
Branch: 1 branch
BranchTrueFalseTotal
if(profile_active==0) 0 0 0
699 0 data_changed=1;
700 0 profile_active=1;
701 0 profile_changed=0;
702 0 }
703 0 ▶ if(battery_changed)
Branch: 1 branch
BranchTrueFalseTotal
if(battery_changed) 0 0 0
704 {
705 0 ▶ ofstream out((home+".ibam/battery.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ofstream out((home+".ibam/battery.rc").c_str()); 0 0 0
ofstream out((home+".ibam/battery.rc").c_str()); 0 0 0
706 0 ▶ out << battery;
Branch: 1 branch
BranchTrueFalseTotal
out << battery; 0 0 0
707 0 battery_changed=0;
708 0 }
709 0 ▶ if(charge_changed)
Branch: 1 branch
BranchTrueFalseTotal
if(charge_changed) 0 0 0
710 {
711 0 ▶ ofstream out((home+".ibam/charge.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ofstream out((home+".ibam/charge.rc").c_str()); 0 0 0
ofstream out((home+".ibam/charge.rc").c_str()); 0 0 0
712 0 ▶ out << charge;
Branch: 1 branch
BranchTrueFalseTotal
out << charge; 0 0 0
713 0 charge_changed=0;
714 0 }
715 0 ▶ if(data_changed)
Branch: 1 branch
BranchTrueFalseTotal
if(data_changed) 0 0 0
716 {
717 0 ▶ ofstream out((home+".ibam/ibam.rc").c_str());
Branch: 2 branches
BranchTrueFalseTotal
ofstream out((home+".ibam/ibam.rc").c_str()); 0 0 0
ofstream out((home+".ibam/ibam.rc").c_str()); 0 0 0
718 0 ▶ out << VERSION << '\t' << currenttime
Branch: 3 branches
BranchTrueFalseTotal
out << VERSION << '\t' << currenttime 0 0 0
out << VERSION << '\t' << currenttime 0 0 0
out << VERSION << '\t' << currenttime 0 0 0
719 0 ▶ << '\t' << currentpercent << '\t'
Branch: 3 branches
BranchTrueFalseTotal
<< '\t' << currentpercent << '\t' 0 0 0
<< '\t' << currentpercent << '\t' 0 0 0
<< '\t' << currentpercent << '\t' 0 0 0
720 0 ▶ << lastratio << '\t' << currentstatus
Branch: 2 branches
BranchTrueFalseTotal
<< lastratio << '\t' << currentstatus 0 0 0
<< lastratio << '\t' << currentstatus 0 0 0
721 0 ▶ << '\t' << adaptive_damping_battery
Branch: 3 branches
BranchTrueFalseTotal
<< '\t' << adaptive_damping_battery 0 0 0
<< '\t' << adaptive_damping_battery 0 0 0
<< '\t' << adaptive_damping_battery 0 0 0
722 0 ▶ << '\t' << adaptive_damping_charge
Branch: 2 branches
BranchTrueFalseTotal
<< '\t' << adaptive_damping_charge 0 0 0
<< '\t' << adaptive_damping_charge 0 0 0
723 0 ▶ << '\t' << profile_logging
Branch: 1 branch
BranchTrueFalseTotal
<< '\t' << profile_logging 0 0 0
724 0 ▶ << '\t' << profile_number
Branch: 2 branches
BranchTrueFalseTotal
<< '\t' << profile_number 0 0 0
<< '\t' << profile_number 0 0 0
725 0 ▶ << '\t' << profile_active << endl;
Branch: 4 branches
BranchTrueFalseTotal
<< '\t' << profile_active << endl; 0 0 0
<< '\t' << profile_active << endl; 0 0 0
<< '\t' << profile_active << endl; 0 0 0
<< '\t' << profile_active << endl; 0 0 0
726 0 lasttime=currenttime;
727 0 lastpercent=currentpercent;
728 0 laststatus=currentstatus;
729 0 data_changed=0;
730 0 }
731 0 }
732  
733 0 inline void ibam::set_profile_logging(int a) { data_changed=(a!=profile_logging);profile_logging=a; }
734 0 inline int ibam::profile_logging_setting(void) const { return profile_logging; }
735
736 0 inline int ibam::seconds_left_battery_bios(void) { return apm->seconds(); }
737 0 inline int ibam::seconds_left_battery(void) { load_battery(); return int(battery.remain(currentpercent)+.5); }
738 0 inline int ibam::seconds_left_battery_adaptive(void) { load_battery(); return int(battery.remain(currentpercent)*lastratio+.5); }
739  
740 0 inline int ibam::percent_battery_bios(void) { return apm->percent(); }
741 0 inline int ibam::percent_battery(void) { load_battery(); return int((100.*seconds_left_battery())/battery.total()+.5); }
742
743 0 inline int ibam::seconds_left_charge(void) { load_charge(); return int(charge.inverted_remain(currentpercent)+.5); }
744 0 inline int ibam::seconds_left_charge_adaptive(void) { load_charge(); return int(charge.inverted_remain(currentpercent)/lastratio+.5); }
745  
746 0 inline int ibam::percent_charge(void) { load_charge(); return 100-int((100.*seconds_left_charge())/charge.total()+.5); }
747  
748 0 inline int ibam::seconds_battery_total(void) { load_battery(); return int(battery.total()+.5); }
749 0 inline int ibam::seconds_battery_total_adaptive(void){ load_battery(); return int(battery.total()*lastratio+.5); }
750  
751 0 inline int ibam::seconds_charge_total(void) { load_charge(); return int(charge.total()+.5); }
752 0 inline int ibam::seconds_charge_total_adaptive(void) { load_charge(); return int(charge.total()/lastratio+.5); }
753  
754 0 inline int ibam::seconds_battery_correction(void)
755 {
756 0 ▶ if(currentstatus!=laststatus || currentstatus==0
Branch: 2 branches
BranchTrueFalseTotal
if(currentstatus!=laststatus || currentstatus==0 0 0 0
if(currentstatus!=laststatus || currentstatus==0 0 0 0
757 0 ▶ || lastpercent!=currentpercent)
Branch: 1 branch
BranchTrueFalseTotal
|| lastpercent!=currentpercent) 0 0 0
758 0 return 0;
759 0 ▶ if(currentstatus==1)
Branch: 1 branch
BranchTrueFalseTotal
if(currentstatus==1) 0 0 0
760 0 return lasttime-currenttime;
761 0 load_battery();
762 0 load_charge();
763 0 return int((currenttime-lasttime)*(battery.average(currentpercent-1,currentpercent+1)/charge.average(currentpercent-1,currentpercent+1))+.5);
764 }
765  
766 0 inline int ibam::seconds_charge_correction(void)
767 {
768 0 ▶ if(currentstatus!=laststatus || currentstatus==0
Branch: 2 branches
BranchTrueFalseTotal
if(currentstatus!=laststatus || currentstatus==0 0 0 0
if(currentstatus!=laststatus || currentstatus==0 0 0 0
769 0 ▶ || lastpercent!=currentpercent)
Branch: 1 branch
BranchTrueFalseTotal
|| lastpercent!=currentpercent) 0 0 0
770 0 return 0;
771 0 ▶ if(currentstatus==2)
Branch: 1 branch
BranchTrueFalseTotal
if(currentstatus==2) 0 0 0
772 0 return lasttime-currenttime;
773 0 load_battery();
774 0 load_charge();
775 0 return int((currenttime-lasttime)/(battery.average(currentpercent-1,currentpercent+1)/charge.average(currentpercent-1,currentpercent+1))+.5);
776 }
777  
778 0 inline int ibam::onBattery(void) { return apm->onBattery(); }
779 0 inline int ibam::charging(void) { return apm->charging(); }
780  
781 inline double percent_data::average_derivation(int a,int b) // average standard derivation from a to b
782 {
783 if(a>b) { int c=a;a=b;b=c; }
784 if(a<0)
785 {
786 a=0;
787 if(b<0)
788 b=0;
789 }
790 if(b>=maxpercents)
791 {
792 b=maxpercents-1;
793 if(a>=maxpercents)
794 a=b;
795 }
796 int i;
797 double su(0);
798 int co(0);
799 for(i=a;i<=b;i++)
800 {
801 if(time_samples[i])
802 {
803 if(time_deriv_for_percent[i]>0)
804 su+=sqrt(time_deriv_for_percent[i])*time_samples[i];
805 co+=time_samples[i];
806 }
807 }
808 if(co)
809 return (su/co);
810 int gotdata=0;
811 for(a--,b++;(a>0 || b<maxpercents-1) && gotdata<2;a--,b++)
812 {
813 if(a<0)
814 a=0;
815 if(b>=maxpercents)
816 b=maxpercents-1;
817 if(time_deriv_for_percent[a]>0 && time_samples[a])
818 su+=sqrt(time_deriv_for_percent[a])*time_samples[a];
819 if(time_deriv_for_percent[b]>0 && time_samples[b])
820 su+=sqrt(time_deriv_for_percent[b])*time_samples[b];
821 co+=time_samples[a]+time_samples[b];
822 if(time_samples[a] || time_samples[b])
823 gotdata++;
824 }
825 if(co)
826 return (su/co);
827  
828 return 20;
829 }
« Ibam.xs ↑ Summary blib/lib/Sys/Ibam.pm »