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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
|
/* $Id: DrvACPI.cpp $ */
/** @file
* DrvACPI - ACPI Host Driver.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_ACPI
#ifdef RT_OS_WINDOWS
# include <iprt/win/windows.h>
#endif
#include <VBox/vmm/pdmdrv.h>
#include <VBox/log.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include <iprt/uuid.h>
#ifdef RT_OS_LINUX
# include <iprt/critsect.h>
# include <iprt/dir.h>
# include <iprt/semaphore.h>
# include <iprt/stream.h>
#endif
#ifdef RT_OS_DARWIN
# include <Carbon/Carbon.h>
# include <IOKit/ps/IOPowerSources.h>
# include <IOKit/ps/IOPSKeys.h>
# undef PVM /* This still messed up in the 10.9 SDK. Sigh. */
#endif
#ifdef RT_OS_FREEBSD
# include <sys/ioctl.h>
# include <dev/acpica/acpiio.h>
# include <sys/types.h>
# include <sys/sysctl.h>
# include <stdio.h>
# include <errno.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#include "VBoxDD.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* ACPI driver instance data.
*
* @implements PDMIACPICONNECTOR
*/
typedef struct DRVACPI
{
/** The ACPI interface. */
PDMIACPICONNECTOR IACPIConnector;
/** The ACPI port interface. */
PPDMIACPIPORT pPort;
/** Pointer to the driver instance. */
PPDMDRVINS pDrvIns;
#ifdef RT_OS_LINUX
/** The current power source. */
PDMACPIPOWERSOURCE enmPowerSource;
/** true = one or more batteries preset, false = no battery present. */
bool fBatteryPresent;
/** No need to RTThreadPoke the poller when set. */
bool volatile fDontPokePoller;
/** Remaining battery capacity. */
PDMACPIBATCAPACITY enmBatteryRemainingCapacity;
/** Battery state. */
PDMACPIBATSTATE enmBatteryState;
/** Preset battery charging/discharging rate. */
uint32_t u32BatteryPresentRate;
/** The poller thread. */
PPDMTHREAD pPollerThread;
/** Synchronize access to the above fields.
* XXX A spinlock is probably cheaper ... */
RTCRITSECT CritSect;
/** Event semaphore the poller thread is sleeping on. */
RTSEMEVENT hPollerSleepEvent;
#endif
} DRVACPI, *PDRVACPI;
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) drvACPIQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
PDRVACPI pThis = PDMINS_2_DATA(pDrvIns, PDRVACPI);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIACPICONNECTOR, &pThis->IACPIConnector);
return NULL;
}
/**
* Get the current power source of the host system.
*
* @returns status code
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pPowerSource Pointer to the power source result variable.
*/
static DECLCALLBACK(int) drvACPIQueryPowerSource(PPDMIACPICONNECTOR pInterface,
PDMACPIPOWERSOURCE *pPowerSource)
{
#if defined(RT_OS_WINDOWS)
RT_NOREF(pInterface);
SYSTEM_POWER_STATUS powerStatus;
if (GetSystemPowerStatus(&powerStatus))
{
/* running on battery? */
if ( powerStatus.ACLineStatus == 0 /* Offline */
|| powerStatus.ACLineStatus == 255 /* Unknown */
&& (powerStatus.BatteryFlag & 15) /* high | low | critical | charging */
) /** @todo why is 'charging' included in the flag test? Add parenthesis around the right bits so the code is clearer. */
{
*pPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
}
/* running on AC link? */
else if (powerStatus.ACLineStatus == 1)
{
*pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
}
else
/* what the hell we're running on? */
{
*pPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
}
}
else
{
AssertMsgFailed(("Could not determine system power status, error: 0x%x\n",
GetLastError()));
*pPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
}
#elif defined (RT_OS_LINUX)
PDRVACPI pThis = RT_FROM_MEMBER(pInterface, DRVACPI, IACPIConnector);
RTCritSectEnter(&pThis->CritSect);
*pPowerSource = pThis->enmPowerSource;
RTCritSectLeave(&pThis->CritSect);
#elif defined (RT_OS_DARWIN)
RT_NOREF(pInterface);
*pPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
CFTypeRef pBlob = IOPSCopyPowerSourcesInfo();
CFArrayRef pSources = IOPSCopyPowerSourcesList(pBlob);
CFDictionaryRef pSource = NULL;
const void *psValue;
bool fResult;
if (CFArrayGetCount(pSources) > 0)
{
for (int i = 0; i < CFArrayGetCount(pSources); ++i)
{
pSource = IOPSGetPowerSourceDescription(pBlob, CFArrayGetValueAtIndex(pSources, i));
/* If the source is empty skip over to the next one. */
if(!pSource)
continue;
/* Skip all power sources which are currently not present like a
* second battery. */
if (CFDictionaryGetValue(pSource, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse)
continue;
/* Only internal power types are of interest. */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSTransportTypeKey), &psValue);
if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSInternalType), 0) == kCFCompareEqualTo)
{
/* Check which power source we are connect on. */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSPowerSourceStateKey), &psValue);
if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo)
*pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
else if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
*pPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
}
}
}
CFRelease(pBlob);
CFRelease(pSources);
#elif defined(RT_OS_FREEBSD)
RT_NOREF(pInterface);
int fAcLine = 0;
size_t cbParameter = sizeof(fAcLine);
int rc = sysctlbyname("hw.acpi.acline", &fAcLine, &cbParameter, NULL, 0);
if (!rc)
{
if (fAcLine == 1)
*pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
else if (fAcLine == 0)
*pPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
else
*pPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
}
else
{
AssertMsg(errno == ENOENT, ("rc=%d (%s)\n", rc, strerror(errno)));
*pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
}
#else /* !RT_OS_FREEBSD either - what could this be? */
RT_NOREF(pInterface);
*pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
#endif /* !RT_OS_FREEBSD */
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMIACPICONNECTOR,pfnQueryBatteryStatus}
*/
static DECLCALLBACK(int) drvACPIQueryBatteryStatus(PPDMIACPICONNECTOR pInterface, bool *pfPresent,
PPDMACPIBATCAPACITY penmRemainingCapacity,
PPDMACPIBATSTATE penmBatteryState,
uint32_t *pu32PresentRate)
{
/* default return values for all architectures */
*pfPresent = false; /* no battery present */
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
*penmRemainingCapacity = PDM_ACPI_BAT_CAPACITY_UNKNOWN;
*pu32PresentRate = UINT32_MAX; /* present rate is unknown */
#if defined(RT_OS_WINDOWS)
RT_NOREF(pInterface);
SYSTEM_POWER_STATUS powerStatus;
if (GetSystemPowerStatus(&powerStatus))
{
/* 128 means no battery present */
*pfPresent = !(powerStatus.BatteryFlag & 128);
/* just forward the value directly */
*penmRemainingCapacity = (PDMACPIBATCAPACITY)powerStatus.BatteryLifePercent;
/* we assume that we are discharging the battery if we are not on-line and
* not charge the battery */
uint32_t uBs = PDM_ACPI_BAT_STATE_CHARGED;
if (powerStatus.BatteryFlag & 8)
uBs = PDM_ACPI_BAT_STATE_CHARGING;
else if (powerStatus.ACLineStatus == 0 || powerStatus.ACLineStatus == 255)
uBs = PDM_ACPI_BAT_STATE_DISCHARGING;
if (powerStatus.BatteryFlag & 4)
uBs |= PDM_ACPI_BAT_STATE_CRITICAL;
*penmBatteryState = (PDMACPIBATSTATE)uBs;
/* on Windows it is difficult to request the present charging/discharging rate */
}
else
{
AssertMsgFailed(("Could not determine system power status, error: 0x%x\n",
GetLastError()));
}
#elif defined(RT_OS_LINUX)
PDRVACPI pThis = RT_FROM_MEMBER(pInterface, DRVACPI, IACPIConnector);
RTCritSectEnter(&pThis->CritSect);
*pfPresent = pThis->fBatteryPresent;
*penmRemainingCapacity = pThis->enmBatteryRemainingCapacity;
*penmBatteryState = pThis->enmBatteryState;
*pu32PresentRate = pThis->u32BatteryPresentRate;
RTCritSectLeave(&pThis->CritSect);
#elif defined(RT_OS_DARWIN)
RT_NOREF(pInterface);
CFTypeRef pBlob = IOPSCopyPowerSourcesInfo();
CFArrayRef pSources = IOPSCopyPowerSourcesList(pBlob);
CFDictionaryRef pSource = NULL;
const void *psValue;
bool fResult;
if (CFArrayGetCount(pSources) > 0)
{
for (int i = 0; i < CFArrayGetCount(pSources); ++i)
{
pSource = IOPSGetPowerSourceDescription(pBlob, CFArrayGetValueAtIndex(pSources, i));
/* If the source is empty skip over to the next one. */
if(!pSource)
continue;
/* Skip all power sources which are currently not present like a
* second battery. */
if (CFDictionaryGetValue(pSource, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse)
continue;
/* Only internal power types are of interest. */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSTransportTypeKey), &psValue);
if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSInternalType), 0) == kCFCompareEqualTo)
{
PDMACPIPOWERSOURCE powerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
/* First check which power source we are connect on. */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSPowerSourceStateKey), &psValue);
if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo)
powerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
else if ( fResult
&& CFStringCompare((CFStringRef)psValue, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo)
powerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
/* At this point the power source is present. */
*pfPresent = true;
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
int curCapacity = 0;
int maxCapacity = 1;
float remCapacity = 0.0f;
/* Fetch the current capacity value of the power source */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSCurrentCapacityKey), &psValue);
if (fResult)
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
/* Fetch the maximum capacity value of the power source */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSMaxCapacityKey), &psValue);
if (fResult)
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
/* Calculate the remaining capacity in percent */
remCapacity = ((float)curCapacity/(float)maxCapacity * PDM_ACPI_BAT_CAPACITY_MAX);
*penmRemainingCapacity = (PDMACPIBATCAPACITY)remCapacity;
if (powerSource == PDM_ACPI_POWER_SOURCE_BATTERY)
{
/* If we are on battery power we are discharging in every
* case */
*penmBatteryState = PDM_ACPI_BAT_STATE_DISCHARGING;
int timeToEmpty = -1;
/* Get the time till the battery source will be empty */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSTimeToEmptyKey), &psValue);
if (fResult)
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &timeToEmpty);
if (timeToEmpty != -1)
/* 0...1000 */
*pu32PresentRate = (uint32_t)roundf((remCapacity / ((float)timeToEmpty/60.0)) * 10.0);
}
if ( powerSource == PDM_ACPI_POWER_SOURCE_OUTLET
&& CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSIsChargingKey), &psValue))
{
/* We are running on an AC power source, but we also have a
* battery power source present. */
if (CFBooleanGetValue((CFBooleanRef)psValue) > 0)
{
/* This means charging. */
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGING;
int timeToFull = -1;
/* Get the time till the battery source will be charged */
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSTimeToFullChargeKey), &psValue);
if (fResult)
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &timeToFull);
if (timeToFull != -1)
/* 0...1000 */
*pu32PresentRate = (uint32_t)roundf((100.0-(float)remCapacity) / ((float)timeToFull/60.0)) * 10.0;
}
}
/* Check for critical */
int criticalValue = 20;
fResult = CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSDeadWarnLevelKey), &psValue);
if (fResult)
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &criticalValue);
if (remCapacity < criticalValue)
*penmBatteryState = (PDMACPIBATSTATE)(*penmBatteryState | PDM_ACPI_BAT_STATE_CRITICAL);
}
}
}
CFRelease(pBlob);
CFRelease(pSources);
#elif defined(RT_OS_FREEBSD)
RT_NOREF(pInterface);
/* We try to use /dev/acpi first and if that fails use the sysctls. */
bool fSuccess = true;
int FileAcpi = 0;
int rc = 0;
FileAcpi = open("/dev/acpi", O_RDONLY);
if (FileAcpi != -1)
{
bool fMilliWatt;
union acpi_battery_ioctl_arg BatteryIo;
memset(&BatteryIo, 0, sizeof(BatteryIo));
BatteryIo.unit = 0; /* Always use the first battery. */
/* Determine the power units first. */
if (ioctl(FileAcpi, ACPIIO_BATT_GET_BIF, &BatteryIo) == -1)
fSuccess = false;
else
{
if (BatteryIo.bif.units == ACPI_BIF_UNITS_MW)
fMilliWatt = true;
else
fMilliWatt = false; /* mA */
BatteryIo.unit = 0;
if (ioctl(FileAcpi, ACPIIO_BATT_GET_BATTINFO, &BatteryIo) == -1)
fSuccess = false;
else
{
if ((BatteryIo.battinfo.state & ACPI_BATT_STAT_NOT_PRESENT) == ACPI_BATT_STAT_NOT_PRESENT)
*pfPresent = false;
else
{
*pfPresent = true;
if (BatteryIo.battinfo.state & ACPI_BATT_STAT_DISCHARG)
*penmBatteryState = PDM_ACPI_BAT_STATE_DISCHARGING;
else if (BatteryIo.battinfo.state & ACPI_BATT_STAT_CHARGING)
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGING;
else
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
if (BatteryIo.battinfo.state & ACPI_BATT_STAT_CRITICAL)
*penmBatteryState = (PDMACPIBATSTATE)(*penmBatteryState | PDM_ACPI_BAT_STATE_CRITICAL);
}
if (BatteryIo.battinfo.cap != -1)
*penmRemainingCapacity = (PDMACPIBATCAPACITY)BatteryIo.battinfo.cap;
BatteryIo.unit = 0;
if (ioctl(FileAcpi, ACPIIO_BATT_GET_BST, &BatteryIo) == 0)
{
/* The rate can be either mW or mA but the ACPI device wants mW. */
if (BatteryIo.bst.rate != 0xffffffff)
{
if (fMilliWatt)
*pu32PresentRate = BatteryIo.bst.rate;
else if (BatteryIo.bst.volt != 0xffffffff)
{
/*
* The rate is in mA so we have to convert it.
* The current power rate can be calculated with P = U * I
*/
*pu32PresentRate = (uint32_t)( ( ((float)BatteryIo.bst.volt/1000.0)
* ((float)BatteryIo.bst.rate/1000.0))
* 1000.0);
}
}
}
}
}
close(FileAcpi);
}
else
fSuccess = false;
if (!fSuccess)
{
int fBatteryState = 0;
size_t cbParameter = sizeof(fBatteryState);
rc = sysctlbyname("hw.acpi.battery.state", &fBatteryState, &cbParameter, NULL, 0);
if (!rc)
{
if ((fBatteryState & ACPI_BATT_STAT_NOT_PRESENT) == ACPI_BATT_STAT_NOT_PRESENT)
*pfPresent = false;
else
{
*pfPresent = true;
if (fBatteryState & ACPI_BATT_STAT_DISCHARG)
*penmBatteryState = PDM_ACPI_BAT_STATE_DISCHARGING;
else if (fBatteryState & ACPI_BATT_STAT_CHARGING)
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGING;
else
*penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
if (fBatteryState & ACPI_BATT_STAT_CRITICAL)
*penmBatteryState = (PDMACPIBATSTATE)(*penmBatteryState | PDM_ACPI_BAT_STATE_CRITICAL);
/* Get battery level. */
int curCapacity = 0;
cbParameter = sizeof(curCapacity);
rc = sysctlbyname("hw.acpi.battery.life", &curCapacity, &cbParameter, NULL, 0);
if (!rc && curCapacity >= 0)
*penmRemainingCapacity = (PDMACPIBATCAPACITY)curCapacity;
/* The rate can't be determined with sysctls. */
}
}
}
#endif /* RT_OS_FREEBSD */
return VINF_SUCCESS;
}
#ifdef RT_OS_LINUX
/**
* Poller thread for /proc/acpi status files.
*
* Reading these files takes ages (several seconds) on some hosts, therefore
* start this thread. The termination of this thread may take some seconds
* on such a hosts!
*
* @param pDrvIns The driver instance data.
* @param pThread The thread.
*/
static DECLCALLBACK(int) drvACPIPoller(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
{
PDRVACPI pThis = PDMINS_2_DATA(pDrvIns, PDRVACPI);
if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
return VINF_SUCCESS;
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
ASMAtomicWriteBool(&pThis->fDontPokePoller, false);
PDMACPIPOWERSOURCE enmPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
PRTSTREAM pStrmStatus;
PRTSTREAM pStrmType;
RTDIR hDir = NIL_RTDIR;
RTDIRENTRY DirEntry;
char szLine[1024];
bool fBatteryPresent = false; /* one or more batteries present */
bool fCharging = false; /* one or more batteries charging */
bool fDischarging = false; /* one or more batteries discharging */
bool fCritical = false; /* one or more batteries in critical state */
bool fDataChanged; /* if battery status data changed during last poll */
int32_t maxCapacityTotal = 0; /* total capacity of all batteries */
int32_t currentCapacityTotal = 0; /* total current capacity of all batteries */
int32_t presentRateTotal = 0; /* total present (dis)charging rate of all batts */
PDMACPIBATCAPACITY enmBatteryRemainingCapacity; /* total remaining capacity of vbox batt */
uint32_t u32BatteryPresentRate; /* total present (dis)charging rate of vbox batt */
int rc = RTDirOpen(&hDir, "/sys/class/power_supply/");
if (RT_SUCCESS(rc))
{
/*
* The new /sys interface introduced with Linux 2.6.25.
*/
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTDirRead(hDir, &DirEntry, NULL);
if (RT_FAILURE(rc))
break;
if ( strcmp(DirEntry.szName, ".") == 0
|| strcmp(DirEntry.szName, "..") == 0)
continue;
#define POWER_OPEN(s, n) RTStrmOpenF("r", s, "/sys/class/power_supply/%s/" n, DirEntry.szName)
rc = POWER_OPEN(&pStrmType, "type");
if (RT_FAILURE(rc))
continue;
rc = RTStrmGetLine(pStrmType, szLine, sizeof(szLine));
if (RT_SUCCESS(rc))
{
if (strcmp(szLine, "Mains") == 0)
{
/* AC adapter */
rc = POWER_OPEN(&pStrmStatus, "online");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if ( RT_SUCCESS(rc)
&& strcmp(szLine, "1") == 0)
enmPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
else
enmPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
RTStrmClose(pStrmStatus);
}
}
else if (strcmp(szLine, "Battery") == 0)
{
/* Battery */
rc = POWER_OPEN(&pStrmStatus, "present");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
RTStrmClose(pStrmStatus);
if ( RT_SUCCESS(rc)
&& strcmp(szLine, "1") == 0)
{
fBatteryPresent = true;
rc = RTStrmOpenF("r", &pStrmStatus,
"/sys/class/power_supply/%s/status", DirEntry.szName);
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_SUCCESS(rc))
{
if (strcmp(szLine, "Discharging") == 0)
fDischarging = true;
else if (strcmp(szLine, "Charging") == 0)
fCharging = true;
}
RTStrmClose(pStrmStatus);
}
rc = POWER_OPEN(&pStrmStatus, "capacity_level");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if ( RT_SUCCESS(rc)
&& strcmp(szLine, "Critical") == 0)
fCritical = true;
RTStrmClose(pStrmStatus);
}
rc = POWER_OPEN(&pStrmStatus, "energy_full");
if (RT_FAILURE(rc))
rc = POWER_OPEN(&pStrmStatus, "charge_full");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_SUCCESS(rc))
{
int32_t maxCapacity = 0;
rc = RTStrToInt32Full(szLine, 0, &maxCapacity);
if ( RT_SUCCESS(rc)
&& maxCapacity > 0)
maxCapacityTotal += maxCapacity;
}
RTStrmClose(pStrmStatus);
}
rc = POWER_OPEN(&pStrmStatus, "energy_now");
if (RT_FAILURE(rc))
rc = POWER_OPEN(&pStrmStatus, "charge_now");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_SUCCESS(rc))
{
int32_t currentCapacity = 0;
rc = RTStrToInt32Full(szLine, 0, ¤tCapacity);
if ( RT_SUCCESS(rc)
&& currentCapacity > 0)
currentCapacityTotal += currentCapacity;
}
RTStrmClose(pStrmStatus);
}
rc = POWER_OPEN(&pStrmStatus, "power_now");
if (RT_FAILURE(rc))
rc = POWER_OPEN(&pStrmStatus, "current_now");
if (RT_SUCCESS(rc))
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_SUCCESS(rc))
{
int32_t presentRate = 0;
rc = RTStrToInt32Full(szLine, 0, &presentRate);
if ( RT_SUCCESS(rc)
&& presentRate > 0)
{
if (fDischarging)
presentRateTotal -= presentRate;
else
presentRateTotal += presentRate;
}
}
RTStrmClose(pStrmStatus);
}
}
}
}
}
RTStrmClose(pStrmType);
#undef POWER_OPEN
}
RTDirClose(hDir);
}
else /* !/sys */
{
/*
* The old /proc/acpi interface
*/
/*
* Read the status of the powerline-adapter.
*/
rc = RTDirOpen(&hDir, "/proc/acpi/ac_adapter/");
if (RT_SUCCESS(rc))
{
#define POWER_OPEN(s, n) RTStrmOpenF("r", s, "/proc/acpi/ac_adapter/%s/" n, DirEntry.szName)
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTDirRead(hDir, &DirEntry, NULL);
if (RT_FAILURE(rc))
break;
if ( strcmp(DirEntry.szName, ".") == 0
|| strcmp(DirEntry.szName, "..") == 0)
continue;
rc = POWER_OPEN(&pStrmStatus, "status");
if (RT_FAILURE(rc))
rc = POWER_OPEN(&pStrmStatus, "state");
if (RT_SUCCESS(rc))
{
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_FAILURE(rc))
break;
if ( strstr(szLine, "Status:") != NULL
|| strstr(szLine, "state:") != NULL)
{
if (strstr(szLine, "on-line") != NULL)
enmPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
else
enmPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
break;
}
}
RTStrmClose(pStrmStatus);
break;
}
}
RTDirClose(hDir);
#undef POWER_OPEN
}
/*
* Read the status of all batteries and collect it into one.
*/
rc = RTDirOpen(&hDir, "/proc/acpi/battery/");
if (RT_SUCCESS(rc))
{
#define POWER_OPEN(s, n) RTStrmOpenF("r", s, "/proc/acpi/battery/%s/" n, DirEntry.szName)
bool fThisBatteryPresent = false;
bool fThisDischarging = false;
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTDirRead(hDir, &DirEntry, NULL);
if (RT_FAILURE(rc))
break;
if ( strcmp(DirEntry.szName, ".") == 0
|| strcmp(DirEntry.szName, "..") == 0)
continue;
rc = POWER_OPEN(&pStrmStatus, "status");
/* there is a 2nd variant of that file */
if (RT_FAILURE(rc))
rc = POWER_OPEN(&pStrmStatus, "state");
if (RT_FAILURE(rc))
continue;
PRTSTREAM pStrmInfo;
rc = POWER_OPEN(&pStrmInfo, "info");
if (RT_FAILURE(rc))
{
RTStrmClose(pStrmStatus);
continue;
}
/* get 'present' status from the info file */
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTStrmGetLine(pStrmInfo, szLine, sizeof(szLine));
if (RT_FAILURE(rc))
break;
if (strstr(szLine, "present:") != NULL)
{
if (strstr(szLine, "yes") != NULL)
{
fThisBatteryPresent = true;
break;
}
}
}
if (fThisBatteryPresent)
{
fBatteryPresent = true;
RTStrmRewind(pStrmInfo);
/* get the maximum capacity from the info file */
while (pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTStrmGetLine(pStrmInfo, szLine, sizeof(szLine));
if (RT_FAILURE(rc))
break;
if (strstr(szLine, "last full capacity:") != NULL)
{
char *psz;
int32_t maxCapacity = 0;
rc = RTStrToInt32Ex(RTStrStripL(&szLine[19]), &psz, 0, &maxCapacity);
if (RT_FAILURE(rc))
maxCapacity = 0;
maxCapacityTotal += maxCapacity;
break;
}
}
/* get the current capacity/state from the status file */
int32_t presentRate = 0;
bool fGotRemainingCapacity = false;
bool fGotBatteryState = false;
bool fGotCapacityState = false;
bool fGotPresentRate = false;
while ( ( !fGotRemainingCapacity
|| !fGotBatteryState
|| !fGotCapacityState
|| !fGotPresentRate)
&& pThread->enmState == PDMTHREADSTATE_RUNNING)
{
rc = RTStrmGetLine(pStrmStatus, szLine, sizeof(szLine));
if (RT_FAILURE(rc))
break;
if (strstr(szLine, "remaining capacity:") != NULL)
{
char *psz;
int32_t currentCapacity = 0;
rc = RTStrToInt32Ex(RTStrStripL(&szLine[19]), &psz, 0, ¤tCapacity);
if ( RT_SUCCESS(rc)
&& currentCapacity > 0)
currentCapacityTotal += currentCapacity;
fGotRemainingCapacity = true;
}
else if (strstr(szLine, "charging state:") != NULL)
{
if (strstr(szLine + 15, "discharging") != NULL)
{
fDischarging = true;
fThisDischarging = true;
}
else if (strstr(szLine + 15, "charging") != NULL)
fCharging = true;
fGotBatteryState = true;
}
else if (strstr(szLine, "capacity state:") != NULL)
{
if (strstr(szLine + 15, "critical") != NULL)
fCritical = true;
fGotCapacityState = true;
}
if (strstr(szLine, "present rate:") != NULL)
{
char *psz;
rc = RTStrToInt32Ex(RTStrStripL(&szLine[13]), &psz, 0, &presentRate);
if (RT_FAILURE(rc))
presentRate = 0;
fGotPresentRate = true;
}
}
if (fThisDischarging)
presentRateTotal -= presentRate;
else
presentRateTotal += presentRate;
}
RTStrmClose(pStrmStatus);
RTStrmClose(pStrmInfo);
}
RTDirClose(hDir);
#undef POWER_OPEN
}
} /* /proc/acpi */
/* atomic update of the state */
RTCritSectEnter(&pThis->CritSect);
/* charging/discharging bits are mutual exclusive */
uint32_t uBs = PDM_ACPI_BAT_STATE_CHARGED;
if (fDischarging)
uBs = PDM_ACPI_BAT_STATE_DISCHARGING;
else if (fCharging)
uBs = PDM_ACPI_BAT_STATE_CHARGING;
if (fCritical)
uBs |= PDM_ACPI_BAT_STATE_CRITICAL;
if (maxCapacityTotal > 0 && currentCapacityTotal > 0)
{
if (presentRateTotal < 0)
presentRateTotal = -presentRateTotal;
/* calculate the percentage */
enmBatteryRemainingCapacity =
(PDMACPIBATCAPACITY)( ( (float)currentCapacityTotal
/ (float)maxCapacityTotal)
* PDM_ACPI_BAT_CAPACITY_MAX);
u32BatteryPresentRate =
(uint32_t)(( (float)presentRateTotal
/ (float)maxCapacityTotal) * 1000);
}
else
{
/* unknown capacity / state */
enmBatteryRemainingCapacity = PDM_ACPI_BAT_CAPACITY_UNKNOWN;
u32BatteryPresentRate = ~0;
}
if ( pThis->enmPowerSource == enmPowerSource
&& pThis->fBatteryPresent == fBatteryPresent
&& pThis->enmBatteryState == (PDMACPIBATSTATE) uBs
&& pThis->enmBatteryRemainingCapacity == enmBatteryRemainingCapacity
&& pThis->u32BatteryPresentRate == u32BatteryPresentRate)
{
fDataChanged = false;
}
else
{
fDataChanged = true;
pThis->enmPowerSource = enmPowerSource;
pThis->fBatteryPresent = fBatteryPresent;
pThis->enmBatteryState = (PDMACPIBATSTATE)uBs;
pThis->enmBatteryRemainingCapacity = enmBatteryRemainingCapacity;
pThis->u32BatteryPresentRate = u32BatteryPresentRate;
}
RTCritSectLeave(&pThis->CritSect);
if (fDataChanged)
pThis->pPort->pfnBatteryStatusChangeEvent(pThis->pPort);
/* wait a bit (e.g. Ubuntu/GNOME polls every 30 seconds) */
ASMAtomicWriteBool(&pThis->fDontPokePoller, true);
rc = RTSemEventWait(pThis->hPollerSleepEvent, 20000);
}
return VINF_SUCCESS;
}
static DECLCALLBACK(int) drvACPIPollerWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
{
PDRVACPI pThis = PDMINS_2_DATA(pDrvIns, PDRVACPI);
RTSemEventSignal(pThis->hPollerSleepEvent);
if (!ASMAtomicReadBool(&pThis->fDontPokePoller))
RTThreadPoke(pThread->Thread);
return VINF_SUCCESS;
}
#endif /* RT_OS_LINUX */
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
static DECLCALLBACK(void) drvACPIDestruct(PPDMDRVINS pDrvIns)
{
LogFlow(("drvACPIDestruct\n"));
PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
#ifdef RT_OS_LINUX
PDRVACPI pThis = PDMINS_2_DATA(pDrvIns, PDRVACPI);
if (pThis->hPollerSleepEvent != NIL_RTSEMEVENT)
{
RTSemEventDestroy(pThis->hPollerSleepEvent);
pThis->hPollerSleepEvent = NIL_RTSEMEVENT;
}
RTCritSectDelete(&pThis->CritSect);
#endif
}
/**
* Construct an ACPI driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
static DECLCALLBACK(int) drvACPIConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
{
RT_NOREF(pCfg, fFlags);
PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
PDRVACPI pThis = PDMINS_2_DATA(pDrvIns, PDRVACPI);
int rc = VINF_SUCCESS;
/*
* Init the static parts.
*/
pThis->pDrvIns = pDrvIns;
#ifdef RT_OS_LINUX
pThis->hPollerSleepEvent = NIL_RTSEMEVENT;
#endif
/* IBase */
pDrvIns->IBase.pfnQueryInterface = drvACPIQueryInterface;
/* IACPIConnector */
pThis->IACPIConnector.pfnQueryPowerSource = drvACPIQueryPowerSource;
pThis->IACPIConnector.pfnQueryBatteryStatus = drvACPIQueryBatteryStatus;
/*
* Validate the config.
*/
PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
/*
* Check that no-one is attached to us.
*/
AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
("Configuration error: Not possible to attach anything to this driver!\n"),
VERR_PDM_DRVINS_NO_ATTACH);
/*
* Query the ACPI port interface.
*/
pThis->pPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIACPIPORT);
if (!pThis->pPort)
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the ACPI port interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
#ifdef RT_OS_LINUX
/*
* Start the poller thread.
*/
rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pPollerThread, pThis, drvACPIPoller,
drvACPIPollerWakeup, 0, RTTHREADTYPE_INFREQUENT_POLLER, "ACPI Poller");
if (RT_FAILURE(rc))
return rc;
rc = RTCritSectInit(&pThis->CritSect);
if (RT_FAILURE(rc))
return rc;
rc = RTSemEventCreate(&pThis->hPollerSleepEvent);
#endif
return rc;
}
/**
* ACPI driver registration record.
*/
const PDMDRVREG g_DrvACPI =
{
/* u32Version */
PDM_DRVREG_VERSION,
/* szName */
"ACPIHost",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"ACPI Host Driver",
/* fFlags */
PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
/* fClass. */
PDM_DRVREG_CLASS_ACPI,
/* cMaxInstances */
~0U,
/* cbInstance */
sizeof(DRVACPI),
/* pfnConstruct */
drvACPIConstruct,
/* pfnDestruct */
drvACPIDestruct,
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
PDM_DRVREG_VERSION
};
|