-
Notifications
You must be signed in to change notification settings - Fork 0
/
usbtemp.c
314 lines (239 loc) · 6.76 KB
/
usbtemp.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <stdio.h>
#include "k_ds18b20.h"
struct applet_arg {
void *u;
int argc;
char **argv;
};
struct applet {
char *name;
int (*fn)(struct applet_arg *);
char *usage;
char *explaination;
};
static int get_number_of_sensors(struct s_usb_device *device) {
int sensors;
sensors = kds_get_number_of_sensors(device);
if ( sensors < 0 ) {
fprintf(stderr, "Cannot obtain number of sensors!\n");
return -1;
}
else if ( sensors == 0 ) {
fprintf(stderr, "No sensors attached!\n");
return -1;
}
return sensors;
}
int applet_kds_config(struct applet_arg *arg) {
int rv, cfg, cur_sensor, sensors;
if ( arg->argc < 3 ) {
fprintf(stderr, "Missing argument!\n");
return 1;
}
if ( (sensors = get_number_of_sensors((struct s_usb_device *)arg->u)) < 0 )
return 1;
cur_sensor = atoi(arg->argv[2]);
if ( cur_sensor >= sensors )
return 1;
if (arg->argc == 4) {
cfg = atoi(arg->argv[3]);
rv = kds_configure((struct s_usb_device *)arg->u, cur_sensor, cfg);
if (rv == 0) {
printf("OK\n");
return 0;
}
if ( rv == KDS_ERANGE )
fprintf(stderr, "Invalid argument!\n");
else
fprintf(stderr, "Error %d!\n", rv);
return 1;
}
if (arg->argc == 3) {
rv = kds_configure_get((struct s_usb_device *)arg->u, cur_sensor, &cfg);
if (!rv) {
printf("S:%i C: %i.\n", cur_sensor, cfg);
return 0;
}
}
return 1;
}
/* Detect new sensors. */
int applet_kds_detect(struct applet_arg *arg) {
if ( kds_detect_sensors((struct s_usb_device *)arg->u) == 0 ) {
printf("OK\n");
return 0;
}
return 1;
}
int applet_kds_measure(struct applet_arg *arg) {
int rv;
rv = kds_measure((struct s_usb_device *)arg->u);
if ( rv == 0 ) {
printf("OK\n");
return 0;
}
if ( rv == KDS_DEVBUSY )
fprintf(stderr, "Device busy!\n");
else
fprintf(stderr, "Error %d!\n", rv);
return 1;
}
int applet_kds_modeget(struct applet_arg *arg) {
struct s_usb_device *device;
int r_bytes;
char r_buffer[16];
device = (struct s_usb_device *)arg->u;
r_bytes = s_usb_send_request(device, USB_GETMODE, 0, (char*)&r_buffer, sizeof(r_buffer));
if ( r_bytes == 1 ) {
printf("Mode: %d\n", (int)r_buffer[0]);
return 0;
}
return 1;
}
int applet_kds_modeset(struct applet_arg *arg) {
struct s_usb_device *device;
int r_bytes;
char r_buffer[16];
int cfg;
if ( arg->argc < 3 ) {
fprintf(stderr, "Missing argument!\n");
return 1;
}
cfg = atoi(arg->argv[2]);
if ( cfg < 0 || cfg > 1 ) {
fprintf(stderr, "Invalid argument!\n");
return 1;
}
device = (struct s_usb_device *)arg->u;
r_bytes = s_usb_send_request(device, USB_SETMODE, cfg, (char*)&r_buffer, sizeof(r_buffer));
if ( r_bytes < 1 )
return 1;
printf("Mode set: %d\n", (int)r_buffer[0]);
return 0;
}
int applet_kds_readrom(struct applet_arg *arg) {
struct s_usb_device *device;
char rom[OW_ROMCODE_SIZE];
int i, cur_sensor, sensors;
device = (struct s_usb_device *)arg->u;
if ( (sensors = get_number_of_sensors(device)) < 0 )
return 1;
if ( arg->argc == 3 ) {
cur_sensor = atoi(arg->argv[2]);
if ( cur_sensor < sensors )
sensors = cur_sensor + 1;
else
return 1;
}
for ( cur_sensor = 0; cur_sensor < sensors; cur_sensor++ ) {
if ( kds_read_rom(device, cur_sensor, rom) == 0 ) {
printf("S:%d R: ", cur_sensor);
for ( i = 0; i < OW_ROMCODE_SIZE; i++ ) {
printf("%02x", (unsigned char)rom[i]);
}
printf("\n");
}
}
return 0;
}
int applet_kds_readtemp(struct applet_arg *arg) {
struct s_usb_device *device;
int T, cur_sensor, sensors;
device = (struct s_usb_device *)arg->u;
if ( (sensors = get_number_of_sensors(device)) < 0 )
return 1;
if ( arg->argc == 3 ) {
cur_sensor = atoi(arg->argv[2]);
if ( cur_sensor < sensors )
sensors = cur_sensor + 1;
else
return 1;
}
for ( cur_sensor = 0; cur_sensor < sensors; cur_sensor++ ) {
if ( kds_read_temperature(device, cur_sensor, &T) == 0 )
printf("S:%d T: %.2f °C\n", cur_sensor, (float)(T)/16);
else
fprintf(stderr, "S:%d not present.\n", cur_sensor);
}
return 0;
}
struct applet applets[] = {
{ "config", applet_kds_config, "[sensor] [0-3]", "Set precision of measurement representation of chosen sensor [sensor]: 0 = 9 bit ... 3 = 12 bit." },
{ "detect", applet_kds_detect, "", "Detect DS18X20 sensors on 1W bus" },
{ "getmode", applet_kds_modeget, "", "Get probe mode" },
{ "measure", applet_kds_measure, "", "Start measurement process"},
{ "rom", applet_kds_readrom, "[?sensor]", "Display ROM (sensor serial number) from chosen sensor (if passed [sensor]) or from all." },
{ "setmode", applet_kds_modeset, "[mode: 0|1]", "Set probe mode" },
{ "temp", applet_kds_readtemp, "[?sensor]", "Display temperature from chosen sensor (if passed [sensor]) or from all." },
{ NULL, NULL, NULL, NULL }
};
static unsigned int str_match(const char *a, const char *b) {
unsigned int i;
i = 0;
while ( *(a + i) && *(b + i) && *(a + i) == *(b + i) )
i++;
return i;
}
static int find_applet(int argc, char **argv, struct applet **sel_applet) {
struct applet *cur_command;
unsigned int cur_match, sel_match, len;
*sel_applet = NULL;
if ( argc < 2 )
return -1;
sel_match = 0;
len = strlen(argv[1]);
for ( cur_command = applets; cur_command->name; cur_command++ ) {
cur_match = str_match(cur_command->name, argv[1]);
if ( cur_match != len )
continue;
if ( cur_match > sel_match ) {
sel_match = cur_match;
*sel_applet = cur_command;
}
else if ( cur_match == sel_match )
/* We have found another match (command): ambiguity, break! */
return -1;
}
return sel_match;
}
static void print_usage(char* name) {
struct applet *cur_applet;
fprintf(stdout, "Usage:\n");
for ( cur_applet = applets; cur_applet->name; cur_applet++ ) {
fprintf(stdout, "%s %s %s\n", name, cur_applet->name, cur_applet->usage);
if (cur_applet->explaination != NULL )
fprintf(stdout, "\t%s\n\n", cur_applet->explaination);
}
}
int main(int argc, char **argv) {
struct s_usb_device *device;
struct applet *applet;
struct applet_arg arg;
int rv;
printf("USBtemp CLI 2016\n\n");
applet = NULL;
rv = find_applet(argc, argv, &applet);
if ( rv < 0 ) {
print_usage(argv[0]);
return 1;
}
else if ( rv == 0 ) {
fprintf(stdout, "Applet not found!\n");
return 1;
}
/* Obtain USB device. */
device = NULL;
if ( kds_get_device(&device) == KDS_NODEVICE ) {
fprintf(stderr, "Could not connect to USB device!\n");
return 1;
}
arg.argc = argc;
arg.argv = argv;
arg.u = (void *)device;
/* Run applet. */
if ( applet != NULL ) {
rv = applet->fn(&arg);
}
kds_destroy_device(device);
return rv;
}