-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctx.c
266 lines (216 loc) · 7.93 KB
/
ctx.c
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
265
266
/*****************************************************************************
* Steve Moskovchenko (aka evil wombat) *
* University of Maryland *
* stevenm86 at gmail dot com *
* http://wam.umd.edu/~stevenm/carnetix.html *
* *
* Linux and Mac OS API for the Carnetix P2140 power supply. *
* The program uses libusb, which works on Mac OS X, Linux, and Windows. *
* *
* Released under the BSD License. See License.txt for info. *
* *
* Version 0.2. Enjoy! *
* *
****************************************************************************/
/*
* This is just a small demo of the API. It just gets the values and parameters,
* and prints them to the console. All the really important stuff is in ctxapi.c
* and ctxapi.h. ctxapi.h also contains the documentation for all the functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>
#include "ctxapi.h"
static char const ctxParamsDesc[][3][22] = {
{"sd_dly", "Shutdown Delay", "seconds"},
{"dmt", "Dead Man Timer", "hours"},
{"dlyon", "Startup Delay", "seconds"},
{"bu_lo", "Bootup Lockout Time", "seconds"},
{"sd_lo", "Shutdown Lockout Time", "seconds"},
{"lobatt", "Low-battery Threshold", "volts"},
{"jumpers", "Jumpers", "bits"}, /* jumpers should be padded */
{"acpi_dly", "ACPI Delay", "seconds"},
{"acpi_dur", "ACPI Duration", "seconds"},
{"low_temp", "Low Temperature Limit", "degrees C"},
0,
};
int ctxPrintValues(struct ctxValues * val)
{
if(!val)
return -1;
printf("Batt Voltage: %f V\nBatt Current: %f A\n\n", val->battVoltage, val->battCurrent);
printf("Pri Voltage : %f V\nPri Current : %f A\n\n", val->priVoltage, val->priCurrent);
printf("Sec Voltage : %f V\nSec Current : %f A\n\n", val->secVoltage, val->secCurrent);
printf("Temperature : %f C\n", val->temperature);
printf("State: %d (%s)\n", val->state, ctxStateNameToText(val->state));
printf("\nLED State (0x%04X):\n", val->ledValue);
if(val->ledValue & LED_IGNITION)
printf("\tIgnition\n");
if(val->ledValue & LED_PULSESTART)
printf("\tPulse Start\n");
if(val->ledValue & LED_FANFAULT)
printf("\tFan Fault\n");
if(val->ledValue & LED_DELAYON)
printf("\tDelay On\n");
if(val->ledValue & LED_ACPI)
printf("\tACPI\n");
if(val->ledValue & LED_POWERGOOD)
printf("\tPower Good\n");
if(val->ledValue & LED_PS_ON)
printf("\tPower Supply On\n");
if(val->ledValue & LED_FANON)
printf("\tFan On\n");
if(val->ledValue & LED_VOLTAGE_OK)
printf("\tBattery Voltage OK\n");
if(val->ledValue & LED_PRICUR_OK)
printf("\tPrimary Current OK\n");
if(val->ledValue & LED_SECCUR_OK)
printf("\tSecondary Current OK\n");
return 0;
}
int ctxPrintParams(struct ctxParams * prm)
{
if(!prm)
return -1;
printf("SD_DELAY : %.1f sec\n", prm->sd_dly);
printf("DMT : %.1f hr\n", prm->dmt);
printf("DLYON : %.1f sec\n", prm->dlyon);
printf("BU_LO : %.1f sec\n", prm->bu_lo);
printf("SD_LO : %.1f sec\n", prm->sd_lo);
printf("LOBATT : %f V\n", prm->lobatt);
printf("Jumpers : 0x%02X\n", prm->softJumpers);
printf("ACPI Dly : %.1f sec\n", prm->acpiDelay);
printf("ACPI Dur : %.1f sec\n", prm->acpiDuration);
printf("Low Temp : %f C\n", prm->lowTemp);
return 0;
}
int main(int argc, char ** argv)
{
struct ctxValues val;
struct ctxParams prm;
char buf[25];
int param_id;
union { double d; unsigned char uc; } param_val;
printf("Carnetix P2140 Linux Tool\nbrought to you by an evil wombat\nHere goes nothing...\n");
if(argc == 1)
{
printf("Usage: %s COMMAND\n", argv[0]);
printf("Commands are:\n\t-pri-on \n\t-pri-off\n\t-sec-on\n\t-sec-off\n\t-p5v-on\n\t-p5v-off\n\t-stat\n\t-config [<param> <value>]\n");
param_id = 0;
while (ctxParamsDesc[param_id][0][0]) {
printf("\t\t%s | %s (%s)\n", ctxParamsDesc[param_id][0], ctxParamsDesc[param_id][1],
ctxParamsDesc[param_id][2]);
++param_id;
}
return -1;
}
usb_dev_handle * hDev = ctxInit();
if(!hDev)
{
printf("\nNo device!\n");
return -1;
}
if(strcmp(argv[1], "-stat") == 0)
{
ctxGetFWVersion(hDev, buf, 25);
printf("Firmware Version is: %s\n", buf);
if(ctxReadValues(hDev, &val) == 0)
{
printf("\nPower Supply Readings:\n");
ctxPrintValues(&val);
} else
{
printf("Error reading values.\n");
ctxClose(hDev);
return -1;
}
}
if(strcmp(argv[1], "-config") == 0)
{
ctxGetFWVersion(hDev, buf, 25);
printf("Firmware Version is: %s\n", buf);
if(ctxReadParams(hDev, &prm) == 0)
{
if (argc == 4)
{
param_id = 0;
while (ctxParamsDesc[param_id][0][0]) {
if (!strcmp(argv[2], ctxParamsDesc[param_id][0])) {
if (!strcmp(argv[2], "jumpers")) {
int temp_val;
if (!sscanf(argv[3], "%i", &temp_val)) {
printf("Invalid value.\n");
return -1;
}
param_val.uc = (unsigned char) temp_val;
} else {
float temp_val;
if (!sscanf(argv[3], "%f", &temp_val)) {
printf("Invalid value.\n");
return -1;
}
param_val.d = (double) temp_val;
}
((double*) &prm)[param_id] = param_val.d;
ctxWriteParams(hDev, &prm);
ctxReadParams(hDev, &prm);
break;
}
++param_id;
}
}
printf("\nPower Supply Configuration:\n");
ctxPrintParams(&prm);
} else
{
printf("Error reading configuration.\n");
ctxClose(hDev);
return -1;
}
}
if(strcmp(argv[1], "-pri-on") == 0)
{
if(ctxPriOn(hDev) == 0)
printf("Primary supply on.\n");
else
printf("Error enabling primary supply.\n");
}
if(strcmp(argv[1], "-pri-off") == 0)
{
if(ctxPriOff(hDev) == 0)
printf("Primary supply off.\n... but for some reason we are still here.\n");
else
printf("Error disabling primary supply.\n");
}
if(strcmp(argv[1], "-sec-on") == 0)
{
if(ctxSecOn(hDev) == 0)
printf("Secondary supply on.\n");
else
printf("Error enabling secondary supply.\n");
}
if(strcmp(argv[1], "-sec-off") == 0)
{
if(ctxSecOff(hDev) == 0)
printf("Secondary supply off.\n");
else
printf("Error disabling secondary supply.\n");
}
if(strcmp(argv[1], "-p5v-on") == 0)
{
if(ctxP5VOn(hDev) == 0)
printf("P5V supply on.\n");
else
printf("Error enabling P5V supply.\n");
}
if(strcmp(argv[1], "-p5v-off") == 0)
{
if(ctxP5VOff(hDev) == 0)
printf("P5V supply off.\n");
else
printf("Error disabling P5V supply.\n");
}
ctxClose(hDev);
return 0;
}