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[] = {
102 static void show_stats(byte *resp, uint len)
105 for (uint i=0; 4*i + 3 < (uint) len; i++)
106 printf(" %s=%u", (i < sizeof(stat_names) / sizeof(stat_names[0]) ? stat_names[i] : "?"), get_u32_le(resp+4*i));
112 static void show_packet(byte *pkt, uint len)
115 printf("Received status %u\n", pkt[0]);
121 for (uint i=0; i<len; i++)
122 printf(" %02x", pkt[i]);
126 printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[2]);
129 printf("INFO %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
130 for (uint i=9; i<len; i++)
131 printf(" %02x", pkt[i]);
135 printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
138 printf("RET %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
139 for (uint i=9; i<len; i++)
140 printf(" %02x", pkt[i]);
144 printf("??? type=%02x\n", pkt[4]);
153 if (err = libusb_init(&usb_ctxt))
154 die("Cannot initialize libusb: error %d", err);
155 // libusb_set_debug(usb_ctxt, 3);
158 time_t now = time(NULL);
159 time_t last_stats = 0;
160 time_t last_query = now;
166 fprintf(stderr, "Waiting for device to appear...\n");
174 if (last_stats + 60 < now) {
175 if ((received = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, sizeof(resp), 1000)) < 0) {
176 usb_error("Receive failed: error %d", received);
180 show_stats(resp, received);
184 if (last_query + 10 < now) {
185 byte pkt[] = { 0xdc, 0xc2, 0x00, 0x0b, 0x06, 0x3d, 0x2e, 0x11, 0x25 };
186 if (err = libusb_bulk_transfer(devh, 0x01, pkt, sizeof(pkt), &received, 2000)) {
187 printf("Send failed: error %d\n", err);
188 // usb_error("Receive failed: error %d", received);
191 printf("Send OK: %d bytes\n", received);
197 if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
198 usb_error("Receive failed: error %d", err);
202 printf("Received %d bytes\n", received);
204 usb_error("Receive failed: unexpected response size %d", received);
208 printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
211 if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 1000)) {
212 if (err != LIBUSB_ERROR_TIMEOUT)
213 usb_error("Receive failed: error %d", err);
217 show_packet(resp, received);