Repository navigation
Expand file tree
/
Copy pathfpusb.c
More file actions
127 lines (108 loc) · 3.06 KB
/
Copy pathfpusb.c
File metadata and controls
127 lines (108 loc) · 3.06 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <math.h>
#include <dlfcn.h>
#include <libusb.h>
#include "../common/fpusb.h"
#define PROC_STAT "/proc/stat"
libusb_context *ctx=NULL;
struct libusb_device_handle *devhaccess;
int verbosity=0;
void usb_exit ( int sig )
{
libusb_release_interface (devhaccess, 0);
libusb_close (devhaccess);
libusb_exit (ctx);
exit(sig);
}
void msg (int level, const char *message, ...)
{
va_list args;
if (verbosity >= level) {
va_start (args, message);
vfprintf (stderr, message, args);
fputc ('\n', stderr);
va_end (args);
}
}
void usage(char** argv)
{
printf ("usage: %s [-vh] -r <radiateur> -o <ordre>\n",argv[0]);
printf (" -v : increase verbosity\n");
printf (" <radiateur>: numéro de radiateur (0)\n");
printf (" <ordre> à passer\n");
printf (" Confort: %d, Hors gel: %d, Arrêt: %d, Reduit: %d\n",REQ_CONF, REQ_HGEL, REQ_ARRT, REQ_REDU);
}
int main (int argc, char** argv)
{
char c, ordre=0, radiateur = 0;
int ret;
/* command line arguments */
while ((c = getopt (argc, argv, "hvr:o:")) != 255)
{
msg (2, "processing argv %c",c);
switch (c)
{
case 'r': // not used yet
radiateur = atoi(optarg);
break;
case 'o':
ordre = atoi(optarg);
break;
case 'v': /* verbosity */
verbosity++;
break;
default:
msg (0, "unknown argument: %d\n", c);
case 'h': /* usage */
usage(argv);
exit (EXIT_FAILURE);
}
}
msg (2,"verbose : %d",verbosity);
msg (2,"ordre : %d",ordre);
msg (2,"radiateur : %d",radiateur);
/* check for param values */
if ( (ordre < REQ_CONF) || (ordre > REQ_REDU) ) {
msg (0, "ordre %d is not valid !", ordre);
usage(argv);
exit(EXIT_FAILURE);
}
/* USB initialisation */
if (libusb_init (&ctx) != 0) {
perror ("libusb_init error");
exit(EXIT_FAILURE);
}
libusb_set_debug (ctx, 4);
if ( (devhaccess = libusb_open_device_with_vid_pid (ctx, VID, PID)) == 0) {
perror ("libusb_open_device_with_vid_pid error");
libusb_exit(ctx);
exit(EXIT_FAILURE);
}
if (libusb_claim_interface (devhaccess, 0) != 0) {
perror ("libusb_claim_interface error");
usb_exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
signal (SIGTERM, usb_exit);
signal (SIGINT, usb_exit);
/* main loop */
ret = libusb_control_transfer ( devhaccess,
LIBUSB_REQUEST_TYPE_VENDOR,
ordre,
radiateur,
0,
NULL,
0,
1000);
if (ret != 0) {
perror("libusb_control_transfer error");
usb_exit (EXIT_FAILURE);
}
usb_exit(0);
exit(EXIT_SUCCESS);
} // main