1 |
/*************************************************************************** |
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 |
#include <stdexcept> |
26 |
|
27 |
AcpiParser::AcpiParser() |
28 |
{ |
29 |
mNumstates = 0; |
30 |
mCurrentState = 0; |
31 |
} |
32 |
|
33 |
|
34 |
AcpiParser::~AcpiParser() |
35 |
{ |
36 |
} |
37 |
|
38 |
void AcpiParser::ReadAcpiFile() |
39 |
{ |
40 |
char buffer[1024]; |
41 |
std::ifstream in; |
42 |
|
43 |
in.open("/proc/acpi/processor/CPU1/throttling"); |
44 |
if (!in) |
45 |
throw std::runtime_error("Could not open file"); |
46 |
|
47 |
int linecounter = 0; |
48 |
mStateDescriptions.clear(); |
49 |
|
50 |
while (! in.eof() ) |
51 |
{ |
52 |
in.getline(buffer,1024); |
53 |
std::string line(buffer); |
54 |
linecounter++; |
55 |
|
56 |
unsigned int pos = line.rfind(' '); |
57 |
if (pos == std::string::npos) |
58 |
continue; |
59 |
pos++; |
60 |
|
61 |
std::string value = line.substr(pos, line.size() - pos); |
62 |
//std::cout << "Value: (" << linecounter << ") = " << value << std::endl; |
63 |
|
64 |
switch (linecounter) |
65 |
{ |
66 |
case 1: |
67 |
mNumstates = atoi(value.c_str()); |
68 |
break; |
69 |
case 2: |
70 |
{ |
71 |
std::string tmp = value.substr(1, value.size() -1); |
72 |
mCurrentState = atoi(tmp.c_str() ); |
73 |
//std::cout << "tmp: >" << tmp << "< = " << std::endl; |
74 |
break; |
75 |
} |
76 |
default: |
77 |
mStateDescriptions.push_back( value ); |
78 |
} |
79 |
} |
80 |
|
81 |
in.close(); |
82 |
//std::cout << "Current state:" << mCurrentState <<std::endl; |
83 |
//std::cout << "Numstates:" << mNumstates << std::endl; |
84 |
} |
85 |
|
86 |
|
87 |
int AcpiParser::GetCurrentState() |
88 |
{ |
89 |
return mCurrentState; |
90 |
} |
91 |
|
92 |
|
93 |
|
94 |
int AcpiParser::GetStateCount() |
95 |
{ |
96 |
return mNumstates; |
97 |
} |
98 |
|
99 |
|
100 |
std::string AcpiParser::GetStateDescription(unsigned int stateNumber) |
101 |
{ |
102 |
if (stateNumber >= mStateDescriptions.size() ) |
103 |
return ""; |
104 |
|
105 |
return mStateDescriptions[stateNumber]; |
106 |
} |
107 |
|
108 |
|
109 |
|
110 |
void AcpiParser::SetState(int newState) |
111 |
{ |
112 |
if (newState > mNumstates) |
113 |
throw std::invalid_argument("newState too small"); |
114 |
|
115 |
std::ofstream out; |
116 |
out.open("/proc/acpi/processor/CPU1/throttling"); |
117 |
|
118 |
if (!out) |
119 |
throw std::runtime_error("Could not open file"); |
120 |
|
121 |
|
122 |
std::stringstream buf; |
123 |
buf << newState << "\n"; |
124 |
out << buf.str(); |
125 |
out.close(); |
126 |
} |