22 |
#include <iostream> |
#include <iostream> |
23 |
#include <fstream> |
#include <fstream> |
24 |
#include <sstream> |
#include <sstream> |
25 |
|
#include <stdexcept> |
26 |
|
|
27 |
AcpiParser::AcpiParser() |
AcpiParser::AcpiParser() |
28 |
{ |
{ |
39 |
{ |
{ |
40 |
char buffer[1024]; |
char buffer[1024]; |
41 |
std::ifstream in; |
std::ifstream in; |
42 |
in.open("/proc/acpi/processor/CPU1/throttling"); |
|
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; |
int linecounter = 0; |
48 |
|
mStateDescriptions.clear(); |
49 |
|
|
50 |
while (! in.eof() ) |
while (! in.eof() ) |
51 |
{ |
{ |
52 |
in.getline(buffer,1024); |
in.getline(buffer,1024); |
56 |
unsigned int pos = line.rfind(' '); |
unsigned int pos = line.rfind(' '); |
57 |
if (pos == std::string::npos) |
if (pos == std::string::npos) |
58 |
continue; |
continue; |
59 |
|
pos++; |
60 |
|
|
61 |
std::string value = line.substr(pos, line.size() - pos); |
std::string value = line.substr(pos, line.size() - pos); |
62 |
|
//std::cout << "Value: (" << linecounter << ") = " << value << std::endl; |
63 |
|
|
64 |
switch (linecounter) |
switch (linecounter) |
65 |
{ |
{ |
70 |
{ |
{ |
71 |
std::string tmp = value.substr(1, value.size() -1); |
std::string tmp = value.substr(1, value.size() -1); |
72 |
mCurrentState = atoi(tmp.c_str() ); |
mCurrentState = atoi(tmp.c_str() ); |
73 |
|
//std::cout << "tmp: >" << tmp << "< = " << std::endl; |
74 |
break; |
break; |
75 |
} |
} |
76 |
default: |
default: |
79 |
} |
} |
80 |
|
|
81 |
in.close(); |
in.close(); |
82 |
|
//std::cout << "Current state:" << mCurrentState <<std::endl; |
83 |
|
//std::cout << "Numstates:" << mNumstates << std::endl; |
84 |
} |
} |
85 |
|
|
86 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
bool AcpiParser::SetState(int newState) |
void AcpiParser::SetState(int newState) |
111 |
{ |
{ |
112 |
if (newState > mNumstates) |
if (newState > mNumstates) |
113 |
return false; |
throw std::invalid_argument("newState too small"); |
114 |
|
|
|
bool success = false; |
|
115 |
std::ofstream out; |
std::ofstream out; |
116 |
out.open("/proc/acpi/processor/CPU1/throttling"); |
out.open("/proc/acpi/processor/CPU1/throttling"); |
117 |
|
|
118 |
if ( out ) |
if (!out) |
119 |
{ |
throw std::runtime_error("Could not open file"); |
|
std::stringstream buf; |
|
|
buf << newState << "\n"; |
|
|
out << buf.str(); |
|
|
out.close(); |
|
|
success = true; |
|
|
} |
|
120 |
|
|
121 |
return success; |
|
122 |
|
std::stringstream buf; |
123 |
|
buf << newState << "\n"; |
124 |
|
out << buf.str(); |
125 |
|
out.close(); |
126 |
} |
} |