-
Notifications
You must be signed in to change notification settings - Fork 3
/
rwwattson.c
executable file
·230 lines (196 loc) · 6.21 KB
/
rwwattson.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
/* openwattson - rwwattson.c library functions
* This is a library of functions common to Linux and Windows
*
* Version 0.1
*
* Control Wattson power meter
*
* Copyright 2010, Kary Främling, Jan Nyman
* This program is published under the GNU General Public license
*/
#include "rwwattson.h"
#include <strings.h> //bzero()
/********************************************************************/
/* get_current_power_with_retry
* Return current power reading. Retry a maximum of MAXRETRIES times
* if an error occurs. Negative value is returned in case of error,
* greater than or equal to zero if successful.
*
* Input: Handle to Wattson
*
* Returns: Current power consumption
*
********************************************************************/
int get_current_power_with_retry(WATTSON wattson)
{
int power;
int i;
for ( i=0 ; i<MAXRETRIES ; i++ ) {
power = get_current_power(wattson);
if ( power >= 0 )
break;
mySleep(500000); // Give enough time for recovering
}
return power;
}
/********************************************************************/
/* get_current_generated_power_with_retry
* Return current generated power reading. Retry a maximum of MAXRETRIES times
* if an error occurs. Negative value is returned in case of error,
* greater than or equal to zero if successful.
*
* Input: Handle to Wattson
*
* Returns: Current generated power consumption
*
********************************************************************/
int get_current_generated_power_with_retry(WATTSON wattson)
{
int power;
int i;
for ( i=0 ; i<MAXRETRIES ; i++ ) {
power = get_current_generated_power(wattson);
if ( power >= 0 )
break;
mySleep(500000); // Give enough time for recovering
}
return power;
}
/********************************************************************/
/********************************************************************/
/* get_current_power
* Return current power reading. Negative value is returned in case of error,
* greater than or equal to zero if successful.
* This error handling is needed because sometimes Wattson returns
* negative values od some other error. This is probably due to some
* "misuse" of the protocol due to our restricted knowledge about it.
*
* Input: Handle to Wattson
*
* Returns: Current power consumption
*
********************************************************************/
int get_current_power(WATTSON wattson)
{
char cmd[] = "nowp";
//char cmd[] = "nowA00009 00\n";
char result[REPLY_BUF_SIZE];
unsigned int power = 0;
tcflush(wattson, TCIOFLUSH); // Flush everything just in case
if (writeport(wattson, cmd) <= 0)
return(-1);
mySleep(500000); // Give enough time for replying
bzero(result, REPLY_BUF_SIZE); // Clear the buffer before readport
if (readport(wattson, result) <= 0)
return(-2);
if ( result[0] == 'p' ) {
sscanf(result+1, "%x", &power);
}
else {
return(-3); // Error in power value
}
return power;
}
/********************************************************************/
/* get_current_generated_power
* Return current generated power reading.
*
* Input: Handle to Wattson
*
* Returns: Current generated power
*
********************************************************************/
int get_current_generated_power(WATTSON wattson)
{
char cmd[] = "noww";
//char cmd[] = "nowA00009 00\n";
char result[REPLY_BUF_SIZE];
unsigned int power = 0;
tcflush(wattson, TCIOFLUSH); // Flush everything just in case
if (writeport(wattson, cmd) <= 0)
return(-1);
mySleep(500000); // Give enough time for replying
bzero(result, REPLY_BUF_SIZE); // Clear the buffer before readport
if (readport(wattson, result) < 0)
return(-2);
if ( result[0] == 'w' ) {
sscanf(result+1, "%x", &power);
}
else {
return(-3); // Error in power value
}
return power;
}
/********************************************************************
* get_configuration()
*
* read setup parameters from openwattson.conf
* It searches in this sequence:
* 1. Path to config file including filename given as parameter
* 2. ./openwattson.conf
* 3. /usr/local/etc/openwattson.conf
* 4. /etc/openwattson.conf
*
* See file openwattson.conf-dist for the format and option names/values
*
* input: config file name with full path - pointer to string
*
* output: struct config populated with valid settings either
* from config file or defaults
*
* returns: 0 = OK
* -1 = no config file or file open error
*
********************************************************************/
int get_configuration(struct config_type *config, char *path)
{
FILE *fptr;
char inputline[1000] = "";
char token[100] = "";
char val[100] = "";
char val2[100] = "";
// First we set everything to defaults - faster than many if statements
strcpy(config->serial_device_name, DEFAULT_SERIAL_DEVICE); // Name of serial device
// open the config file
fptr = NULL;
if (path != NULL)
fptr = fopen(path, "r"); //first try the parameter given
if (fptr == NULL) //then try default search
{
if ((fptr = fopen("openwattson.conf", "r")) == NULL)
{
if ((fptr = fopen("/usr/local/etc/openwattson.conf", "r")) == NULL)
{
if ((fptr = fopen("/etc/openwattson.conf", "r")) == NULL)
{
//Give up and use defaults
return(-1);
}
}
}
}
while (fscanf(fptr, "%[^\n]\n", inputline) != EOF)
{
sscanf(inputline, "%[^= \t]%*[ \t=]%s%*[, \t]%s%*[^\n]", token, val, val2);
if (token[0] == '#') // comment
continue;
if ((strcmp(token,"SERIAL_DEVICE")==0) && (strlen(val) != 0))
{
strcpy(config->serial_device_name,val);
continue;
}
}
return (0);
}
/********************************************************************
* initialize resets Wattson to cold start (rewind and start over)
*
* Input: device number of the already open serial port
*
* Returns: 0 if fail, 1 if success
*
********************************************************************/
int initialize(WATTSON wattson)
{
return 1;
}