-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxPowerUSB.hpp
264 lines (240 loc) · 6.63 KB
/
LinuxPowerUSB.hpp
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
#ifndef __LINUX_POWER_USB_H__
#define __LINUX_POWER_USB_H__
#include"powerusb.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <unistd.h>
#include <cstdarg>
#include <PwrUSBImp.h>
/**
* Exception for errors interacting with the USB interface and commanding changes
*/
class LinuxPowerUSBError : public virtual std::exception {
std::string message;
public:
template <typename T, typename... Args>
LinuxPowerUSBError( T msg, Args... args ) : std::exception() {
int len = std::snprintf( nullptr, 0, msg, args... );
char buf[len+100];
std::snprintf( buf, sizeof(buf), msg, args... );
message = std::string(buf);
}
virtual const char* what() const throw() {
return message.c_str();
}
};
/**
* Exception for command line errors.
*/
class LinuxPowerUSBArgsError : public virtual LinuxPowerUSBError {
public:
template <typename T, typename...Args>
LinuxPowerUSBArgsError( T msg, Args...args ) : LinuxPowerUSBError(msg, args...) {}
};
/***
* Main class for interacting with the PowerUSB device via the HID library interface class, PowerUSB.
*/
class LinuxPowerUSB
{
static bool debugging;
public:
template <typename T, typename... Args>
void report( T fmt, Args... args ) {
int len = std::snprintf( nullptr, 0, fmt, args... );
char buf[len+100];
std::snprintf( buf, sizeof(buf), fmt, args... );
printf( "%s", buf );
}
private:
template <typename T, typename... Args>
void debug( T fmt, Args... args ) {
int len = std::snprintf( nullptr, 0, fmt, args... );
char buf[len+100];
std::snprintf( buf, sizeof(buf), fmt, args... );
if( debugging ) printf( "%s", buf );
}
public :
PowerUSB pwrusb;
template <typename T, typename... Args>
void error( T fmt, Args... args ) {
int len = std::snprintf( nullptr, 0, fmt, args... );
char buf[len+100];
std::snprintf( buf, sizeof(buf), fmt, args... );
fprintf( stderr, "%s", buf );
}
LinuxPowerUSB() :pwrusb()
{
}
virtual ~LinuxPowerUSB() { pwrusb.close(); }
void setDebug(bool how) { debugging = how; PowerUSB::debugging = how; }
bool Setup() {
init();
return checkStatus();
}
void init()
{
int i = -1;
int v = pwrusb.init( &i );
debug("init(&i) = %d max units, model=%d\n", v, i );
}
bool checkStatus()
{
int status = pwrusb.checkStatus();
debug("checkStatus = %d\n", status);
if(!status)
{
throw LinuxPowerUSBError("LinuxPowerUSB Not Connected");
}
return status;
}
void setPortDefaultOn( int p ) {
if( p > 0 && p < 4 ) {
pwrusb.setDefaultState(
p == 1 ? true : PWRUSB_NO_CHANGE,
p == 2 ? true : PWRUSB_NO_CHANGE,
p == 3 ? true : PWRUSB_NO_CHANGE );
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
void setPortDefaultOff( int p ) {
if( p > 0 && p < 4 ) {
pwrusb.setDefaultState(
p == 1 ? false : PWRUSB_NO_CHANGE,
p == 2 ? false : PWRUSB_NO_CHANGE,
p == 3 ? false : PWRUSB_NO_CHANGE );
return;
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
void setPortDefaultState( int p, bool how ) {
if( p > 0 && p < 4 ) {
pwrusb.setDefaultState(
p == 1 ? how : PWRUSB_NO_CHANGE,
p == 2 ? how : PWRUSB_NO_CHANGE,
p == 3 ? how : PWRUSB_NO_CHANGE );
return;
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
void setDefaults( int state1, int state2, int state3 ) {
pwrusb.setDefaultState( state1, state2, state3 );
}
void setPortStates( int state1, int state2, int state3 ) {
pwrusb.setPort( state1, state2, state3 );
}
void setPortState( int p, bool state ) {
if( p > 0 && p < 4 ) {
pwrusb.setPort(
p == 1 ? state : PWRUSB_NO_CHANGE,
p == 2 ? state : PWRUSB_NO_CHANGE,
p == 3 ? state : PWRUSB_NO_CHANGE );
return;
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
void setPortOn( int port ) {
setPortState( port, true );
}
int setCurrentDevice( int index ) {
return pwrusb.selectDevice( index );
}
int getCurrentDevice() {
return pwrusb.getCurrentDevice();
}
void setPortOff( int port ) {
setPortState( port, false );
}
void getOutputStates( int states[7] ) {
pwrusb.getOutputState( states );
}
int setOutputState(int outputs[]) {
return pwrusb.setOutputState(outputs);
}
void setIODirection( int directions[7] ) {
pwrusb.setIODirection( directions );
}
void getInputStates( int states[7] ) {
pwrusb.getInputState( states );
}
int getVersion() {
return pwrusb.getFirmwareVersion();
}
std::string getModel() {
return pwrusb.getModelName();
}
bool powerCycleIn( int seconds ) {
return pwrusb.powerCycle( seconds) > 0;
}
bool sendHeartBeat() {
return pwrusb.sendHeartBeat() > 0;
}
bool closeAll() {
return pwrusb.close() > 0;
}
bool setupPortToggle( int port, int onTimeSecs, int offTimeSecs ) {
return pwrusb.generateClock( port, onTimeSecs, offTimeSecs ) > 0;
}
bool resetPowerUSB() {
return pwrusb.resetBoard() > 0;
}
int setOverload( int overload ) {
return pwrusb.setOverload( overload );
}
/**
* Return -1 = not connected, 0 = not in overload, >= 1 in overload
*/
int inOverload() {
return pwrusb.getOverload();
}
/**
* Starts watchdog in the PowerUSB.
* HbTimeSec: Expected time for heartbeat
* numHbMisses: Number of accepted consecutive misses in heartbeat
* resetTimeSec: Amount of time to switch off the computer outlet
*/
bool setWatchDogInterval( int heartBeatSecs, int numMissesAllowed, int resetTimeSecs ) {
return pwrusb.startWatchdogTimer( heartBeatSecs, numMissesAllowed, resetTimeSecs ) > 0;
}
bool stopWatchDog() {
return pwrusb.stopWatchdogTimer() > 0;
}
/**
* Get the current state of Watchdog in PowerUSB
* Return 0: watchdog is not running, 1: Watchdog is active, 2: In PowerCycle phase
*/
int getWatchdogStatus() {
return pwrusb.getWatchdogStatus();
}
bool getPortState( int p ) {
int ps[3] = { p ==1, p ==2, p ==3 };
if( p > 0 && p < 4 ) {
if(pwrusb.readPortState(&ps[0], &ps[1], &ps[2]) > 0 ) {
return(ps[p-1] );
}
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
bool getPortDefaultState( int p ) {
int ps[3] = { p == 1, p == 2, p == 3 };
if( p > 0 && p < 4 ) {
if( pwrusb.readDefaultPortState(&ps[0], &ps[1], &ps[2]) > 0 ) {
return ps[p-1];
}
}
throw LinuxPowerUSBArgsError("Invalid port number: %d", p );
}
void reportStatus(int status, int port, bool verbose = true)
{
if( verbose ) report("Port %d = %s\n", port, status ? "On" : "Off");
else report("%s\n", status ? "On" : "Off");
}
void reportDefault(int status, int port, bool verbose = true)
{
if( verbose ) report("Default Port %d State = %s\n", port, status ? "On" : "Off");
else report("%s\n", status ? "On" : "Off");
}
};
bool LinuxPowerUSB::debugging;
#endif