summaryrefslogtreecommitdiff
path: root/sys-power/acpitool/files/acpitool-0.5.1-battery.patch
blob: 76428ca7d970e4568afb1b349f8600526980fe78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
Author: Evgeni Golov <evgeni@debian.org>, Michael Meskes <meskes@debian.org>
Description: Fix reading of battery information.

--- src/acpitool.h
+++ src/acpitool.h
@@ -39,6 +39,9 @@
     char Serial[13];
     char Bat_Type[13];
     char Voltage_Now[13];
+    char Charge_Now[13];
+    char Charge_Full[13];
+    char Charge_Full_Design[13];
 };
 
 
--- src/battery.cpp
+++ src/battery.cpp
@@ -107,6 +107,9 @@
 	    memset(Batt_Info[i]->Serial, '\0', 13);
 	    memset(Batt_Info[i]->Bat_Type, '\0', 13); 
 	    memset(Batt_Info[i]->Voltage_Now, '\0', 13); 
+	    memset(Batt_Info[i]->Charge_Now, '\0', 13);
+	    memset(Batt_Info[i]->Charge_Full, '\0', 13);
+	    memset(Batt_Info[i]->Charge_Full_Design, '\0', 13);
 	    
 	    // initialize all struct members to blanks --> avoid rubbish in output //
 			
@@ -139,7 +142,12 @@
 		case 1 : 
 		{	
 	    	    Present_Batteries++;
-		    Remaining_Percentage = float(atoi(Batt_Info[i]->Remaining_Cap)) / float(atoi(Batt_Info[i]->LastFull_Cap)) * 100.0;
+
+		    if (strcmp(Batt_Info[i]->Charge_Now,"")!=0 &&
+			strcmp(Batt_Info[i]->Charge_Now,"unknown")!=0)
+		    	Remaining_Percentage = float(atoi(Batt_Info[i]->Charge_Now)) / float(atoi(Batt_Info[i]->Charge_Full)) * 100.0;
+		    else
+			Remaining_Percentage = float(atoi(Batt_Info[i]->Remaining_Cap)) / float(atoi(Batt_Info[i]->LastFull_Cap)) * 100.0;
 		    
 		    /* from Alan Pope : some broken Dell batteries report a remaining capacity bigger
 		       than their last full capacity or their design capacity. This led acpitool to report
@@ -153,19 +161,24 @@
             	    else
             		Precision = 4;
             	    
-		    if( strncmp(Batt_Info[i]->Charging_State,"char",4)==0 ) 
+		    if(strncasecmp(Batt_Info[i]->Charging_State,"char",4)==0)
 		    {
 			Is_Charging = 1;
 		    }
 		    else
 		    {
-			if(strncmp(Batt_Info[i]->Charging_State,"disch",5)==0) Is_Discharging = 1;
+			if(strncasecmp(Batt_Info[i]->Charging_State,"disch",5)==0) Is_Discharging = 1;
 		    }
 		    		    
 	    	    if(Show_Time)      // calculate remaining or charging time only if present battery rate != 0 //
 	    	    {
-			if(Is_Charging)
-			  Remaining_Time = (float(atoi(Batt_Info[i]->LastFull_Cap)) - float(atoi(Batt_Info[i]->Remaining_Cap))) / float(atoi(Batt_Info[i]->Present_Rate)); 
+			if(Is_Charging) {
+			  if (strcmp(Batt_Info[i]->Charge_Now,"")!=0 &&
+			      strcmp(Batt_Info[i]->Charge_Now,"unknown")!=0)
+			  	Remaining_Time = (float(atoi(Batt_Info[i]->Charge_Full)) - float(atoi(Batt_Info[i]->Charge_Now))) / float(atoi(Batt_Info[i]->Present_Rate));
+			  else
+			  	Remaining_Time = (float(atoi(Batt_Info[i]->LastFull_Cap)) - float(atoi(Batt_Info[i]->Remaining_Cap))) / float(atoi(Batt_Info[i]->Present_Rate)); 
+			}
 			else
 			  Remaining_Time = float(atoi(Batt_Info[i]->Remaining_Cap)) / float(atoi(Batt_Info[i]->Present_Rate)); 
 			// this represents hours //
@@ -180,9 +193,11 @@
 			Minutes = Time_In_Seconds / 60;
 			Time_In_Seconds = Time_In_Seconds - (Minutes * 60);
 	    	    }
-		    
+		   
 		    if(atoi(Batt_Info[i]->Design_Cap) > 0)
 		      Battery_Left_Percent = float(atoi(Batt_Info[i]->LastFull_Cap)) / float(atoi(Batt_Info[i]->Design_Cap)) * 100.0;
+		    else if(atoi(Batt_Info[i]->Charge_Full_Design) > 0)
+		      Battery_Left_Percent = float(atoi(Batt_Info[i]->Charge_Full)) / float(atoi(Batt_Info[i]->Charge_Full_Design)) * 100.0;
 		    else
 		      Battery_Left_Percent = -1.0;
 		      			  
@@ -207,8 +222,15 @@
 				    <<setfill('0')<<setw(2)<<Time_In_Seconds;
 				 cout<<endl;
 				 
-				 cout<<"    Design capacity    : "<<Batt_Info[i]->Design_Cap<<endl;
-				 cout<<"    Last full capacity : "<<Batt_Info[i]->LastFull_Cap;
+		    		 if(atoi(Batt_Info[i]->Design_Cap) > 0)
+				 	cout<<"    Design capacity    : "<<Batt_Info[i]->Design_Cap<<endl;
+				 else if(atoi(Batt_Info[i]->Charge_Full_Design) > 0)
+				 	cout<<"    Design capacity    : "<<Batt_Info[i]->Charge_Full_Design<<endl;
+
+		    		 if(atoi(Batt_Info[i]->LastFull_Cap) > 0)
+					cout<<"    Last full capacity : "<<Batt_Info[i]->LastFull_Cap;
+				 else if(atoi(Batt_Info[i]->Charge_Full) > 0)
+					cout<<"    Last full capacity : "<<Batt_Info[i]->Charge_Full;
 				 
 				 if(Battery_Left_Percent<100.0)
 				 {
@@ -327,7 +349,7 @@
 
 
 
-int Get_Battery_Info_from_Proc(const int bat_nr, Battery_Info *bat_info, int verbose)
+int Get_Battery_Info_from_Proc(const int bat_nr, Battery_Info *batt_info, int verbose)
 {
     ifstream file_in;
     char filename[4][65], str[100], temp[100];
@@ -378,7 +400,7 @@
     {
 	if(!verbose)
    	{
-   	    bat_info->Battery_Present = 2;     // 2 represents error value //
+   	    batt_info->Battery_Present = 2;     // 2 represents error value //
    	    return 0;
    	}
    	else
@@ -422,24 +444,24 @@
     	file_in.getline(str, 100);
     	strncpy(temp, str+25, 4);
     	if(strncmp(temp,"yes",3)==0)
-    	    bat_info->Battery_Present = 1;               //yes, we have a battery //
+    	    batt_info->Battery_Present = 1;               //yes, we have a battery //
     	else
     	{
-    	    bat_info->Battery_Present = 0;
+    	    batt_info->Battery_Present = 0;
     	    return 0;                  //bail out if battery is not present //
     	}
     	
 	// then get the design capacity //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Design_Cap, str+25, 9);
+    	strncpy(batt_info->Design_Cap, str+25, 9);
 	
     	// then get the last full capacity //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->LastFull_Cap, str+25, 9);
+    	strncpy(batt_info->LastFull_Cap, str+25, 9);
 	
-	if (strncmp(bat_info->LastFull_Cap,"unknown",7)==0)
+	if (strncmp(batt_info->LastFull_Cap,"unknown",7)==0)
 	{
-    	    bat_info->Battery_Present = 0;
+    	    batt_info->Battery_Present = 0;
     	    return 0;                  //bail out if battery is not present //
     	}
 	/* some Dell laptops seem to report a 2nd battery as being present, while it is NOT, but then report the 
@@ -449,21 +471,21 @@
     
     	// then get the technology //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Technology, str+25, 12);
+    	strncpy(batt_info->Technology, str+25, 12);
     
     	// then get the model number //
     	for(int t=0; t<5; t++)
 	file_in.getline(str, 100);            //skip 5 lines //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Model, str+25, 12);
+    	strncpy(batt_info->Model, str+25, 12);
     
     	// then get the serial number //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Serial, str+25, 12);
+    	strncpy(batt_info->Serial, str+25, 12);
     
     	// then get the battery type //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Bat_Type, str+25, 12);
+    	strncpy(batt_info->Bat_Type, str+25, 12);
     
     	file_in.close();
     	
@@ -480,17 +502,17 @@
     	// then get the charging state //
     	file_in.getline(str, 100); file_in.getline(str, 100);     // skip first 2 lines //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Charging_State, str+25, 12);
-	if (strncmp(bat_info->Charging_State,"unknown",7)==0) strncpy(bat_info->Charging_State, "charged",7);
+    	strncpy(batt_info->Charging_State, str+25, 12);
+	if (strncmp(batt_info->Charging_State,"unknown",7)==0) strncpy(batt_info->Charging_State, "charged",7);
 	/* on older kernels, like 2.4.22, the charging state is reported as "unknown", whereas in recent kernels
 	   this was changed to "charged". */  
 
     	// then get the charging rate //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Present_Rate, str+25, 9);
-	if (strncmp(bat_info->Charging_State,"charged",7)==0)
+    	strncpy(batt_info->Present_Rate, str+25, 9);
+	if (strncmp(batt_info->Charging_State,"charged",7)==0)
 	{
-	    if (strncmp(bat_info->Present_Rate, "unknown",7)==0) strncpy(bat_info->Present_Rate, "0      ",7);
+	    if (strncmp(batt_info->Present_Rate, "unknown",7)==0) strncpy(batt_info->Present_Rate, "0      ",7);
 	}    
 	/* some batteries report the present rate as "unknown", even when they report the battery as being charged.
 	   If the battery is charged, the rate should be 0 */     
@@ -498,12 +520,12 @@
 
     	// then get the remaining capacity //
     	file_in.getline(str, 100);
-    	strncpy(bat_info->Remaining_Cap, str+25, 9);
+    	strncpy(batt_info->Remaining_Cap, str+25, 9);
     
     	file_in.close();
 	}
 	else      // battery dir is readable but empty : only . and .. at most //
-	    bat_info->Battery_Present = 3;   
+	    batt_info->Battery_Present = 3;   
 	
 	return 0;
 }
@@ -513,8 +535,8 @@
 int Get_Battery_Info_from_Sys(const int bat_nr, Battery_Info *batt_info, int verbose)
 {
     ifstream file_in;
-    char filename[6][65], str[100], temp[100];
-    int bat_count = 0, start = 0, findex = 0;
+    char filename[6][65], str[100], temp[100], attr[100];
+    int bat_count = 0, start = 0, findex = 0, value = 0;
     DIR *battery_dir;
     char *name, *dirname;
        
@@ -613,165 +635,104 @@
 	    return -1;
     	}
     	
-    	memset(str, '\0', 100);
-	for(int t=0; t<5; t++)
-	    fgets(str, 100, power_fp);            /* skip first 5 lines */
-    	
-    	/* get battery status (full, charging, ...) */
-    	memset(str, '\0', 100);
-    	fgets(str, 100, power_fp);
-    	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Charging_State, temp, 12);
-    	}   
-	
-	    
-	/* get battery presence (0 or 1) */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);   
-	if (strlen(str)>0)
-	{
-	    memset(temp, '\0', 100);
-	    strncpy(temp, str+21, 1);
-    	    if(strncmp(temp,"1",1)==0)
-    	        batt_info->Battery_Present = 1;               /* yes, we have a battery */
-    	    else
-    	    {
-    		batt_info->Battery_Present = 0;
-    		printf(" Battery is not present, bailing out. \n");
-    		return 0;                                    /* bail out if battery is not present */
-    	    }
-    	}
-    	    
-    	    
-    	/* get technology */    
-    	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);   
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Technology, temp, 12);
-    	} 
-    	else
-    	    strncpy(batt_info->Technology, "unknown", 7);
-    	    
-    	    
-    	    
-    	//printf(" \n bat_info_tech = %s \n\n ",  batt_info->Technology);    
-    	
-    	
-
-	fgets(str, 100, power_fp);    	/* skip 1 line */	
-
-
-	/* get voltage_now */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Voltage_Now, temp, 12);
-    	}    
-    	else
-    	    strncpy(batt_info->Voltage_Now, "unknown", 7);
-
-    	
-	/* get current_now, which I believe is the charging rate ? */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Present_Rate, temp, 12);
-    	}       
-    	else
-    	    strncpy(batt_info->Present_Rate, "unknown", 7);
-
-	
-	/* get charge_full_design */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Design_Cap, temp, 12);
-    	}          
-    	else
-    	    strncpy(batt_info->Design_Cap, "unknown", 7);
-    	    
-    	    
-    	//printf(" \n bat_info_design_cap = %s \n ",  batt_info->Design_Cap);    
-
-
-	/* get charge_full, which is the last full capacity I guess ? */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);   
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->LastFull_Cap, temp, 12);
-    	}       
-    	else
-    	    strncpy(batt_info->LastFull_Cap, "unknown", 7);
-
-
-	//printf(" \n bat_info_lastfull_cap = %s \n\n ",  batt_info->LastFull_Cap); 
-
-
-	/* get charge_now */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);   
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Remaining_Cap, temp, 12);
-    	}       
-    	else
-    	    strncpy(batt_info->Remaining_Cap, "unknown", 7);
-
-	//printf(" \n bat_info_remaining_cap = %s \n\n ",  batt_info->Remaining_Cap); 
-
-
-	/* get model_name */  
-	memset(str, '\0', 100);  
-	fgets(str, 100, power_fp);   
-	if (strlen(str)>0)
-    	{
-    	    memset(temp, '\0', 100);
-	    strncpy(temp, str+24, 12);         // use strncpy here because sscanf chokes on blanks in this one ? //
-
-    	    memset(str, '\0', 100);
-    	    sscanf(temp, "%[^\n]", str);       // strip trailing \n, fucks up output //
-    	    
-    	    strncpy(batt_info->Model, str, 12);
+	strncpy(batt_info->Technology, "unknown", 7);
+	strncpy(batt_info->Voltage_Now, "unknown", 7);
+	strncpy(batt_info->Charge_Now, "unknown", 7);
+	strncpy(batt_info->Charge_Full, "unknown", 7);
+	strncpy(batt_info->Charge_Full_Design, "unknown", 7);
+	strncpy(batt_info->Present_Rate, "unknown", 7);
+	strncpy(batt_info->Design_Cap, "unknown", 7);
+	strncpy(batt_info->LastFull_Cap, "unknown", 7);
+	strncpy(batt_info->Remaining_Cap, "unknown", 7);
+	strncpy(batt_info->Model, "unknown", 7);
+	strncpy(batt_info->Serial, "unknown", 7);
+
+	// see linux-2.6/drivers/power/power_supply_sysfs.c
+	// there can be different number of lines, so read up to 40 lines
+	for(int t=0; t<40; t++) {
+    		memset(str, '\0', 100);
+		memset(attr, '\0', 100);
+    		memset(temp, '\0', 100);
+		fgets(str, 100, power_fp);
+		sscanf(str, "%[^=]s %*s %*[^\n]", attr);
+    		sscanf(str, "%*[^=] %*c %s %*[^\n]",temp); 
+		if (strcmp(attr,"POWER_SUPPLY_STATUS")==0) {
+    			strncpy(batt_info->Charging_State, temp, 12);
+    		}   
+		else if (strcmp(attr,"POWER_SUPPLY_TYPE")==0) {
+			strncpy(batt_info->Bat_Type, temp, 12);
+    		}
+		else if (strcmp(attr,"POWER_SUPPLY_TECHNOLOGY")==0) {
+    			strncpy(batt_info->Technology, temp, 12);
+    		} 
+		else if (strcmp(attr,"POWER_SUPPLY_VOLTAGE_NOW")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mV", value);
+    	    		strncpy(batt_info->Voltage_Now, temp, 12);
+    		}    
+		else if (strcmp(attr,"POWER_SUPPLY_CURRENT_NOW")==0 ||
+			 strcmp(attr,"POWER_SUPPLY_POWER_NOW")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i", value);
+			strncpy(batt_info->Present_Rate, temp, 9);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_CHARGE_NOW")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mA", value);
+			strncpy(batt_info->Charge_Now, temp, 12);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_CHARGE_FULL_DESIGN")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mA", value);
+			strncpy(batt_info->Charge_Full_Design, temp, 12);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_CHARGE_FULL")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mA", value);
+			strncpy(batt_info->Charge_Full, temp, 12);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_ENERGY_FULL_DESIGN")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mWh", value);
+			strncpy(batt_info->Design_Cap, temp, 9);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_ENERGY_FULL")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mWh", value);
+			strncpy(batt_info->LastFull_Cap, temp, 9);
+		}
+		else if (strcmp(attr,"POWER_SUPPLY_ENERGY_NOW")==0) {
+			value = atoi(temp) / 1000;
+			snprintf(temp, sizeof(temp), "%i mWh", value);
+			strncpy(batt_info->Remaining_Cap, temp, 9);
+    		}       
+		else if (strcmp(attr,"POWER_SUPPLY_MODEL_NAME")==0) {
+			strncpy(batt_info->Model, temp, 12);
+    		}
+		else if (strcmp(attr,"POWER_SUPPLY_SERIAL_NUMBER")==0) {
+			strncpy(batt_info->Serial, temp, 12);
+    		}       
+		else if (strcmp(attr,"POWER_SUPPLY_PRESENT")==0) {
+			if(strncmp(temp,"1",1)==0) {
+				batt_info->Battery_Present = 1;
+			}
+			else {
+				batt_info->Battery_Present = 0;
+				printf(" Battery is not present, bailing out. \n");
+				return 0;
+    			}
+           	}       
     	}       
+	if (strcmp(batt_info->Charge_Now,"")!=0 &&
+	   strcmp(batt_info->Charge_Now,"unknown")!=0)
+		snprintf(temp, sizeof(temp), "%s mA", batt_info->Present_Rate);
     	else
-    	    strncpy(batt_info->Model, "unknown", 7);
+		snprintf(temp, sizeof(temp), "%s mW", batt_info->Present_Rate);
 
-	fgets(str, 100, power_fp);   
+	strncpy(batt_info->Present_Rate, temp, 9);
 	
-	/* get serial */    
-	memset(str, '\0', 100);
-	fgets(str, 100, power_fp);   
-	if (strlen(str)!=0)
-    	{
-    	    memset(temp, '\0', 100);
-    	    sscanf(str, "%*[^=] %*c %s %[^\n]",temp); 
-    	    strncpy(batt_info->Serial, temp, 12);
-    	}       
-    	else
-    	    strncpy(batt_info->Serial, "unknown", 7);
-    
     	fclose(power_fp);
-	}
+    }
     else      // battery dir is readable but empty : only . and .. at most //
         batt_info->Battery_Present = 3;   
     return 0;