]> mj.ucw.cz Git - home-hw.git/blob - bsb/daemon/burrow-bsb.c
BSB: Daemon logs packets and statistics
[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
105 static void show_packet(byte *pkt, uint len)
106 {
107 #if 1
108         printf(":");
109         for (uint i=0; i<len; i++)
110                 printf(" %02x", pkt[i]);
111         putchar('\n');
112 #endif
113
114         printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[2]);
115         switch (pkt[4]) {
116                 case 2:
117                         printf("INFO %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
118                         for (uint i=9; i<len; i++)
119                                 printf(" %02x", pkt[i]);
120                         putchar('\n');
121                         break;
122                 case 6:
123                         printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
124                         break;
125                 case 7:
126                         printf("RET %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
127                         for (uint i=9; i<len; i++)
128                                 printf(" %02x", pkt[i]);
129                         putchar('\n');
130                         break;
131                 default:
132                         printf("??? type=%02x\n", pkt[4]);
133         }
134 }
135
136 int main(void)
137 {
138         int err;
139         if (err = libusb_init(&usb_ctxt))
140                 die("Cannot initialize libusb: error %d", err);
141         // libusb_set_debug(usb_ctxt, 3);
142         open_device();
143
144         time_t last_stats = 0;
145         int received;
146         byte resp[64];
147
148         for (;;) {
149                 if (!devh) {
150                         fprintf(stderr, "Waiting for device to appear...\n");
151                         while (!devh) {
152                                 sleep(5);
153                                 open_device();
154                         }
155                 }
156
157                 time_t now = time(NULL);
158                 if (last_stats + 10 < now) {
159                         if ((received = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, sizeof(resp), 1000)) < 0) {
160                                 usb_error("Receive failed: error %d", received);
161                                 continue;
162                         }
163
164                         show_stats(resp, received);
165                         last_stats = now;
166                 }
167
168 #if 0
169                 if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
170                         usb_error("Receive failed: error %d", err);
171                         continue;
172                 }
173
174                 printf("Received %d bytes\n", received);
175                 if (received < 4) {
176                         usb_error("Receive failed: unexpected response size %d", received);
177                         continue;
178                 }
179
180                 printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
181 #endif
182
183                 if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 1000)) {
184                         if (err != LIBUSB_ERROR_TIMEOUT)
185                                 usb_error("Receive failed: error %d", err);
186                         continue;
187                 }
188
189                 show_packet(resp, received);
190         }
191 }