forked from mhaehnel/SmartDroid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
194 lines (166 loc) · 5.77 KB
/
main.cc
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
#include <iostream>
#include <hidapi/hidapi.h>
#include <tclap/CmdLine.h>
#include <type_traits>
//USB identifiers
const int vendor = 0x04d8;
const int product = 0x003f;
enum class Command {
RequestData = 0x37,
RequestStartStop = 0x80,
RequestStatus = 0x81,
RequestOnOff = 0x82,
RequestVersion = 0x83,
NONE = 0x00
};
class ReadType : public TCLAP::Constraint<std::string>
{
virtual bool check(const std::string& value) const override {
return value == "voltage" || value == "current" || value == "raw" ||
value == "power" || value == "energy" ||
value == "on" || value == "measure";
}
virtual std::string description() const override {
return "voltage|power|current|energy|measure|on|raw";
}
virtual std::string shortID() const override {
return description();
}
};
class ActionType : public TCLAP::Constraint<std::string>
{
virtual bool check(const std::string& value) const override {
return value == "on" || value == "off" ||
value == "start" || value == "stop";
}
virtual std::string description() const override {
return "on|off|start|stop";
}
virtual std::string shortID() const override {
return description();
}
};
//returns -1 if no devices were found, else 0
int listDevices();
int readValue(const std::string& m, const std::string& device);
int performAction(const std::string& m, const std::string& device);
int main(int argc, char** argv)
{
try {
TCLAP::CmdLine cmd("Odroid SmartPower client", ' ', "0.1");
TCLAP::ValueArg<std::string> device("d","device","Device to use", false, "", "usb-id");
TCLAP::ValueArg<std::string> meter("m","metric","Metric to output",false,"none",new ReadType());
TCLAP::ValueArg<std::string> action("a","action","Perform action",false,"none",new ActionType());
TCLAP::SwitchArg list("l","list","Lists available devices",false);
cmd.add(device);
std::vector<TCLAP::Arg*> lst {&meter, &action, &list};
cmd.xorAdd(lst);
cmd.parse(argc,argv);
if (list.getValue()) return listDevices();
if (meter.getValue() == "none" && action.getValue() == "none")
{
std::cerr << "You must either specify a metric to read o an action!"
<< std::endl << "Please use -m or -a" << std::endl;
return -2;
}
if (meter.getValue() != "none") return readValue(meter.getValue(),device.getValue());
if (action.getValue() != "none") return performAction(action.getValue(),device.getValue());
__builtin_unreachable();
} catch (TCLAP::ArgException &e) {
std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
}
}
void sendCmd(hid_device* dev, Command cmd) {
unsigned char buf[65] = {'\0'};
buf[1] = static_cast<unsigned char>(cmd);
buf[2] = 0x0; //param;
//TODO: Error checking!
hid_write(dev,buf,65);
}
int performAction(const std::string& action, const std::string& device) {
hid_device* dev = hid_open_path(device.c_str());
if (dev == nullptr) {
std::cerr << "Could not open device " << device;
return -1;
}
hid_set_nonblocking(dev,false);
unsigned char status[65];
sendCmd(dev,Command::RequestStatus);
//TODO: Error checking!
hid_read(dev,status,65);
if ((action == "on" && status[2] != 1) ||
(action == "off" && status[2] == 1)) {
sendCmd(dev,Command::RequestOnOff);
return 0;
} else if ((action == "start" && status[1] != 1) ||
(action == "stop" && status[1] == 1)) {
sendCmd(dev,Command::RequestStartStop);
return 0;
}
return -1;
}
float bufToFloat(char* buf, int start, int len) {
std::stringstream str(std::string(buf+start,len));
float val;
str >> val;
return val;
}
int readValue(const std::string& m, const std::string& device)
{
hid_device* dev = hid_open_path(device.c_str());
if (dev == nullptr) {
std::cerr << "Could not open device " << device;
return -2;
}
hid_set_nonblocking(dev,false);
unsigned char buf[65] = {'\0'};
char* b2 = (char*)(buf);
if (m == "on" || m == "measure") {
sendCmd(dev,Command::RequestStatus);
} else {
sendCmd(dev,Command::RequestData);
}
//TODO: error handling?
hid_read(dev,buf,65);
hid_close(dev);
if (m == "raw") {
for (auto c : buf) {
if (c < 0x20 || c > 127 || c == 134) {
std::cout << "\\" << std::hex << static_cast<unsigned>(c) << std::dec;
} else {
std::cout << c;
}
}
} else if (m == "on") {
std::cout << (buf[2] == 1) << std::endl;
return (buf[2] == 1);
} else if (m == "measure") {
std::cout << (buf[1] == 1) << std::endl;
return (buf[1] == 1);
} else if (m == "voltage") {
std::cout << bufToFloat(b2,2,5);
} else if (m == "current") {
std::cout << bufToFloat(b2,11,5);
} else if (m == "power") {
std::cout << bufToFloat(b2,17,6);
} else if (m == "energy") {
std::cout << bufToFloat(b2,24,9);
} else {
__builtin_unreachable();
}
std::cout << std::endl;
return 0;
}
int listDevices()
{
std::cout << "Listing devices:" << std::endl;
std::cout << "================" << std::endl << std::endl;
hid_device_info *info = hid_enumerate(vendor,product);
if (info == nullptr) return -1;
while (info != nullptr) {
std::wcout << info->path << " " << info->manufacturer_string << " " << info->product_string << std::endl;
info = info->next;
}
hid_free_enumeration(info);
return 0;
}