forked from elpeo/rbtemper
-
Notifications
You must be signed in to change notification settings - Fork 29
/
pcsensor.c
368 lines (304 loc) · 8.39 KB
/
pcsensor.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* pcsensor.c by Michitaka Ohno (c) 2011 ([email protected])
* based oc pcsensor.c by Juan Carlos Perez (c) 2011 ([email protected])
* based on Temper.c by Robert Kavaler (c) 2009 (relavak.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY Juan Carlos Perez ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Robert kavaler BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "pcsensor.h"
#define INTERFACE1 (0x00)
#define INTERFACE2 (0x01)
#define SUPPORTED_DEVICES (2)
const static unsigned short vendor_id[] = {
0x1130,
0x0c45
};
const static unsigned short product_id[] = {
0x660c,
0x7401
};
const static char uTemperatura[] = { 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 };
const static char uIni1[] = { 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 };
const static char uIni2[] = { 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 };
const static char uCmd0[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
const static char uCmd1[] = { 10, 11, 12, 13, 0, 0, 2, 0 };
const static char uCmd2[] = { 10, 11, 12, 13, 0, 0, 1, 0 };
const static char uCmd3[] = { 0x52, 0, 0, 0, 0, 0, 0, 0 };
const static char uCmd4[] = { 0x54, 0, 0, 0, 0, 0, 0, 0 };
const static int reqIntLen=8;
const static int reqBulkLen=8;
const static int timeout=5000; /* timeout in ms */
static int debug=0;
static int device_type(usb_dev_handle *lvr_winusb){
struct usb_device *dev;
int i;
dev = usb_device(lvr_winusb);
for(i =0;i < SUPPORTED_DEVICES;i++){
if (dev->descriptor.idVendor == vendor_id[i] &&
dev->descriptor.idProduct == product_id[i] ) {
return i;
}
}
return -1;
}
static int usb_detach(usb_dev_handle *lvr_winusb, int iInterface) {
int ret;
ret = usb_detach_kernel_driver_np(lvr_winusb, iInterface);
if(ret) {
if(errno == ENODATA) {
if(debug) {
printf("Device already detached\n");
}
} else {
if(debug) {
printf("Detach failed: %s[%d]\n",
strerror(errno), errno);
printf("Continuing anyway\n");
}
}
} else {
if(debug) {
printf("detach successful\n");
}
}
return ret;
}
static usb_dev_handle *find_lvr_winusb() {
struct usb_bus *bus;
struct usb_device *dev;
int i;
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
for(i =0;i < SUPPORTED_DEVICES;i++){
if (dev->descriptor.idVendor == vendor_id[i] &&
dev->descriptor.idProduct == product_id[i] ) {
usb_dev_handle *handle;
if(debug) {
printf("lvr_winusb with Vendor Id: %x and Product Id: %x found.\n", vendor_id[i], product_id[i]);
}
if (!(handle = usb_open(dev))) {
if(debug){
printf("Could not open USB device\n");
}
return NULL;
}
return handle;
}
}
}
}
return NULL;
}
static usb_dev_handle* setup_libusb_access() {
usb_dev_handle *lvr_winusb;
if(debug) {
usb_set_debug(255);
} else {
usb_set_debug(0);
}
usb_init();
usb_find_busses();
usb_find_devices();
if(!(lvr_winusb = find_lvr_winusb())) {
if(debug){
printf("Couldn't find the USB device, Exiting\n");
}
return NULL;
}
usb_detach(lvr_winusb, INTERFACE1);
usb_detach(lvr_winusb, INTERFACE2);
if (usb_set_configuration(lvr_winusb, 0x01) < 0) {
if(debug){
printf("Could not set configuration 1\n");
}
return NULL;
}
// Microdia tiene 2 interfaces
if (usb_claim_interface(lvr_winusb, INTERFACE1) < 0) {
if(debug){
printf("Could not claim interface\n");
}
return NULL;
}
if (usb_claim_interface(lvr_winusb, INTERFACE2) < 0) {
if(debug){
printf("Could not claim interface\n");
}
return NULL;
}
return lvr_winusb;
}
static int ini_control_transfer(usb_dev_handle *dev) {
int r,i;
char question[] = { 0x01,0x01 };
r = usb_control_msg(dev, 0x21, 0x09, 0x0201, 0x00, (char *) question, 2, timeout);
if( r < 0 )
{
if(debug){
printf("USB control write");
}
return -1;
}
if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF);
printf("\n");
}
return 0;
}
static int control_transfer(usb_dev_handle *dev, const char *pquestion) {
int r,i;
char question[reqIntLen];
memcpy(question, pquestion, sizeof question);
r = usb_control_msg(dev, 0x21, 0x09, 0x0200, 0x01, (char *) question, reqIntLen, timeout);
if( r < 0 )
{
if(debug){
printf("USB control write");
}
return -1;
}
if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",question[i] & 0xFF);
printf("\n");
}
return 0;
}
static int interrupt_read(usb_dev_handle *dev) {
int r,i;
char answer[reqIntLen];
bzero(answer, reqIntLen);
r = usb_interrupt_read(dev, 0x82, answer, reqIntLen, timeout);
if( r != reqIntLen )
{
if(debug){
printf("USB interrupt read");
}
return -1;
}
if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF);
printf("\n");
}
return 0;
}
static int interrupt_read_temperatura(usb_dev_handle *dev, float *tempC) {
int r,i, temperature;
char answer[reqIntLen];
bzero(answer, reqIntLen);
r = usb_interrupt_read(dev, 0x82, answer, reqIntLen, timeout);
if( r != reqIntLen )
{
if(debug){
printf("USB interrupt read");
}
return -1;
}
if(debug) {
for (i=0;i<reqIntLen; i++) printf("%02x ",answer[i] & 0xFF);
printf("\n");
}
temperature = (answer[3] & 0xFF) + (answer[2] << 8);
*tempC = temperature * (125.0 / 32000.0);
return 0;
}
static int get_data(usb_dev_handle *dev, char *buf, int len){
return usb_control_msg(dev, 0xa1, 1, 0x300, 0x01, (char *)buf, len, timeout);
}
static int get_temperature(usb_dev_handle *dev, float *tempC){
char buf[256];
int ret, temperature, i;
control_transfer(dev, uCmd1 );
control_transfer(dev, uCmd4 );
for(i = 0; i < 7; i++) {
control_transfer(dev, uCmd0 );
}
control_transfer(dev, uCmd2 );
ret = get_data(dev, buf, 256);
if(ret < 2) {
return -1;
}
temperature = (buf[1] & 0xFF) + (buf[0] << 8);
*tempC = temperature * (125.0 / 32000.0);
return 0;
}
usb_dev_handle* pcsensor_open(){
usb_dev_handle *lvr_winusb = NULL;
char buf[256];
int i, ret;
if (!(lvr_winusb = setup_libusb_access())) {
return NULL;
}
switch(device_type(lvr_winusb)){
case 0:
control_transfer(lvr_winusb, uCmd1 );
control_transfer(lvr_winusb, uCmd3 );
control_transfer(lvr_winusb, uCmd2 );
ret = get_data(lvr_winusb, buf, 256);
if(debug){
printf("Other Stuff (%d bytes):\n", ret);
for(i = 0; i < ret; i++) {
printf(" %02x", buf[i] & 0xFF);
if(i % 16 == 15) {
printf("\n");
}
}
printf("\n");
}
break;
case 1:
if (ini_control_transfer(lvr_winusb) < 0) {
fprintf(stderr, "Failed to ini_control_transfer (device_type 1)");
return NULL;
}
control_transfer(lvr_winusb, uTemperatura );
interrupt_read(lvr_winusb);
control_transfer(lvr_winusb, uIni1 );
interrupt_read(lvr_winusb);
control_transfer(lvr_winusb, uIni2 );
interrupt_read(lvr_winusb);
interrupt_read(lvr_winusb);
break;
}
if(debug){
printf("device_type=%d\n", device_type(lvr_winusb));
}
return lvr_winusb;
}
void pcsensor_close(usb_dev_handle* lvr_winusb){
usb_release_interface(lvr_winusb, INTERFACE1);
usb_release_interface(lvr_winusb, INTERFACE2);
usb_close(lvr_winusb);
}
float pcsensor_get_temperature(usb_dev_handle* lvr_winusb){
float tempc;
int ret;
switch(device_type(lvr_winusb)){
case 0:
ret = get_temperature(lvr_winusb, &tempc);
break;
case 1:
control_transfer(lvr_winusb, uTemperatura );
ret = interrupt_read_temperatura(lvr_winusb, &tempc);
break;
}
if(ret < 0){
return FLT_MIN;
}
return tempc;
}