1 |
torben |
24 |
/*************************************************************************** |
2 |
|
|
* Copyright (C) 2007 by Torben Nielsen * |
3 |
|
|
* torben@t-hoerup.dk * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the * |
17 |
|
|
* Free Software Foundation, Inc., * |
18 |
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
#include "acpiparser.h" |
21 |
|
|
|
22 |
|
|
#include <iostream> |
23 |
|
|
#include <fstream> |
24 |
|
|
#include <sstream> |
25 |
|
|
|
26 |
|
|
AcpiParser::AcpiParser() |
27 |
|
|
{ |
28 |
|
|
mNumstates = 0; |
29 |
|
|
mCurrentState = 0; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
AcpiParser::~AcpiParser() |
34 |
|
|
{ |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
void AcpiParser::ReadAcpiFile() |
38 |
|
|
{ |
39 |
|
|
char buffer[1024]; |
40 |
|
|
std::ifstream in; |
41 |
|
|
in.open("/proc/acpi/processor/CPU1/throttling"); |
42 |
|
|
|
43 |
|
|
int linecounter = 0; |
44 |
|
|
|
45 |
|
|
while (! in.eof() ) |
46 |
|
|
{ |
47 |
|
|
in.getline(buffer,1024); |
48 |
|
|
std::string line(buffer); |
49 |
|
|
linecounter++; |
50 |
|
|
|
51 |
|
|
unsigned int pos = line.rfind(' '); |
52 |
|
|
if (pos == std::string::npos) |
53 |
|
|
continue; |
54 |
|
|
|
55 |
|
|
std::string value = line.substr(pos, line.size() - pos); |
56 |
|
|
|
57 |
|
|
switch (linecounter) |
58 |
|
|
{ |
59 |
|
|
case 1: |
60 |
|
|
mNumstates = atoi(value.c_str()); |
61 |
|
|
break; |
62 |
|
|
case 2: |
63 |
|
|
{ |
64 |
|
|
std::string tmp = value.substr(1, value.size() -1); |
65 |
|
|
mCurrentState = atoi(tmp.c_str() ); |
66 |
|
|
break; |
67 |
|
|
} |
68 |
|
|
default: |
69 |
|
|
mStateDescriptions.push_back( value ); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
in.close(); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
int AcpiParser::GetCurrentState() |
78 |
|
|
{ |
79 |
|
|
return mCurrentState; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
int AcpiParser::GetStateCount() |
85 |
|
|
{ |
86 |
|
|
return mNumstates; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
|
90 |
|
|
std::string AcpiParser::GetStateDescription(unsigned int stateNumber) |
91 |
|
|
{ |
92 |
|
|
if (stateNumber >= mStateDescriptions.size() ) |
93 |
|
|
return ""; |
94 |
|
|
|
95 |
|
|
return mStateDescriptions[stateNumber]; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
bool AcpiParser::SetState(int newState) |
101 |
|
|
{ |
102 |
|
|
if (newState > mNumstates) |
103 |
|
|
return false; |
104 |
|
|
|
105 |
|
|
bool success = false; |
106 |
|
|
std::ofstream out; |
107 |
|
|
out.open("/proc/acpi/processor/CPU1/throttling"); |
108 |
|
|
|
109 |
|
|
if ( out ) |
110 |
|
|
{ |
111 |
|
|
std::stringstream buf; |
112 |
|
|
buf << newState << "\n"; |
113 |
|
|
out << buf.str(); |
114 |
|
|
out.close(); |
115 |
|
|
success = true; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
return success; |
119 |
|
|
} |