2 * Boiler System Bus Interface Daemon
4 * (c) 2020 Martin Mares <mj@ucw.cz>
17 static struct libusb_context *usb_ctxt;
18 static struct libusb_device_handle *devh;
20 typedef unsigned char byte;
21 typedef unsigned int uint;
23 static void die(const char *msg, ...)
28 vfprintf(stderr, msg, args);
34 static void usb_error(const char *msg, ...)
38 vfprintf(stderr, msg, args);
48 static void open_device(void)
51 libusb_device **devlist;
52 ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
54 die("Cannot enumerate USB devices: error %d", (int) devn);
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));
63 if (err = libusb_open(dev, &devh)) {
64 usb_error("Cannot open device: error %d", err);
67 libusb_reset_device(devh);
68 if (err = libusb_claim_interface(devh, 0)) {
69 usb_error("Cannot claim interface: error %d", err);
79 libusb_free_device_list(devlist, 1);
82 static inline uint get_u32_le(byte *p)
84 return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
87 static const char * const stat_names[] = {
97 static void show_stats(byte *resp, uint len)
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));
107 static void show_packet(byte *pkt, uint len)
111 for (uint i=0; i<len; i++)
112 printf(" %02x", pkt[i]);
116 printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[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]);
125 printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
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]);
134 printf("??? type=%02x\n", pkt[4]);
143 if (err = libusb_init(&usb_ctxt))
144 die("Cannot initialize libusb: error %d", err);
145 // libusb_set_debug(usb_ctxt, 3);
148 time_t last_stats = 0;
154 fprintf(stderr, "Waiting for device to appear...\n");
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);
168 show_stats(resp, received);
173 if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
174 usb_error("Receive failed: error %d", err);
178 printf("Received %d bytes\n", received);
180 usb_error("Receive failed: unexpected response size %d", received);
184 printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
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);
193 show_packet(resp, received);