]> mj.ucw.cz Git - home-hw.git/blob - bsb/daemon/burrow-bsb.c
BSB: Minor fixes of daemon
[home-hw.git] / bsb / daemon / burrow-bsb.c
1 /*
2  *      Boiler System Bus Interface Daemon
3  *
4  *      (c) 2020 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 #include <libusb.h>
16
17 static struct libusb_context *usb_ctxt;
18 static struct libusb_device_handle *devh;
19
20 typedef unsigned char byte;
21 typedef unsigned int uint;
22
23 static void die(const char *msg, ...)
24 {
25         va_list args;
26         va_start(args, msg);
27
28         vfprintf(stderr, msg, args);
29         fputc('\n', stderr);
30
31         exit(1);
32 }
33
34 static void usb_error(const char *msg, ...)
35 {
36         va_list args;
37         va_start(args, msg);
38         vfprintf(stderr, msg, args);
39         fputc('\n', stderr);
40         va_end(args);
41
42         if (devh) {
43                 libusb_close(devh);
44                 devh = NULL;
45         }
46 }
47
48 static void open_device(void)
49 {
50         int err;
51         libusb_device **devlist;
52         ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
53         if (devn < 0)
54                 die("Cannot enumerate USB devices: error %d", (int) devn);
55
56         for (ssize_t i=0; i<devn; i++) {
57                 struct libusb_device_descriptor desc;
58                 libusb_device *dev = devlist[i];
59                 if (!libusb_get_device_descriptor(dev, &desc)) {
60                         if (desc.idVendor == 0x4242 && desc.idProduct == 0x0003) {
61                                 fprintf(stderr, "Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
62
63                                 if (err = libusb_open(dev, &devh)) {
64                                         usb_error("Cannot open device: error %d", err);
65                                         goto out;
66                                 }
67                                 libusb_reset_device(devh);
68                                 if (err = libusb_claim_interface(devh, 0)) {
69                                         usb_error("Cannot claim interface: error %d", err);
70                                         goto out;
71                                 }
72
73                                 goto out;
74                         }
75                 }
76         }
77
78 out:
79         libusb_free_device_list(devlist, 1);
80 }
81
82 static inline uint get_u32_le(byte *p)
83 {
84         return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
85 }
86
87 static const char * const stat_names[] = {
88         "rx_noise",
89         "rx_errors",
90         "rx_invalid",
91         "rx_overruns",
92         "rx_timeouts",
93         "rx_bad_crc",
94         "rx_ok",
95 };
96
97 static void show_stats(byte *resp, uint len)
98 {
99         printf("# Stats:");
100         for (uint i=0; 4*i + 3 < (uint) len; i++)
101                 printf(" %s=%u", (i < sizeof(stat_names) / sizeof(stat_names[0]) ? stat_names[i] : "?"), get_u32_le(resp+4*i));
102         printf("\n");
103
104         fflush(stdout);
105 }
106
107 static void show_packet(byte *pkt, uint len)
108 {
109 #if 1
110         printf(":");
111         for (uint i=0; i<len; i++)
112                 printf(" %02x", pkt[i]);
113         putchar('\n');
114 #endif
115
116         printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[2]);
117         switch (pkt[4]) {
118                 case 2:
119                         printf("INFO %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
120                         for (uint i=9; i<len; i++)
121                                 printf(" %02x", pkt[i]);
122                         putchar('\n');
123                         break;
124                 case 6:
125                         printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
126                         break;
127                 case 7:
128                         printf("RET %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
129                         for (uint i=9; i<len; i++)
130                                 printf(" %02x", pkt[i]);
131                         putchar('\n');
132                         break;
133                 default:
134                         printf("??? type=%02x\n", pkt[4]);
135         }
136
137         fflush(stdout);
138 }
139
140 int main(void)
141 {
142         int err;
143         if (err = libusb_init(&usb_ctxt))
144                 die("Cannot initialize libusb: error %d", err);
145         // libusb_set_debug(usb_ctxt, 3);
146         open_device();
147
148         time_t last_stats = 0;
149         int received;
150         byte resp[64];
151
152         for (;;) {
153                 if (!devh) {
154                         fprintf(stderr, "Waiting for device to appear...\n");
155                         while (!devh) {
156                                 sleep(5);
157                                 open_device();
158                         }
159                 }
160
161                 time_t now = time(NULL);
162                 if (last_stats + 60 < now) {
163                         if ((received = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, sizeof(resp), 1000)) < 0) {
164                                 usb_error("Receive failed: error %d", received);
165                                 continue;
166                         }
167
168                         show_stats(resp, received);
169                         last_stats = now;
170                 }
171
172 #if 0
173                 if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
174                         usb_error("Receive failed: error %d", err);
175                         continue;
176                 }
177
178                 printf("Received %d bytes\n", received);
179                 if (received < 4) {
180                         usb_error("Receive failed: unexpected response size %d", received);
181                         continue;
182                 }
183
184                 printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
185 #endif
186
187                 if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 1000)) {
188                         if (err != LIBUSB_ERROR_TIMEOUT)
189                                 usb_error("Receive failed: error %d", err);
190                         continue;
191                 }
192
193                 show_packet(resp, received);
194         }
195 }