]> mj.ucw.cz Git - home-hw.git/blobdiff - bsb/daemon/burrow-bsb.c
BSB: Firmware handles replies
[home-hw.git] / bsb / daemon / burrow-bsb.c
index 714f811600ff09a5038e56280983c9cc5dc4c02e..d20392aaa48cd1307db7ae9c19fa01a124aaf9b6 100644 (file)
@@ -18,8 +18,11 @@ static struct libusb_context *usb_ctxt;
 static struct libusb_device_handle *devh;
 
 typedef unsigned char byte;
+typedef uint32_t u32;
 typedef unsigned int uint;
 
+#include "../firmware/interface.h"
+
 static void die(const char *msg, ...)
 {
        va_list args;
@@ -57,7 +60,7 @@ static void open_device(void)
                struct libusb_device_descriptor desc;
                libusb_device *dev = devlist[i];
                if (!libusb_get_device_descriptor(dev, &desc)) {
-                       if (desc.idVendor == 0x4242 && desc.idProduct == 0x0003) {
+                       if (desc.idVendor == BSB_USB_VENDOR && desc.idProduct == BSB_USB_PRODUCT) {
                                fprintf(stderr, "Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
 
                                if (err = libusb_open(dev, &devh)) {
@@ -85,13 +88,9 @@ static inline uint get_u32_le(byte *p)
 }
 
 static const char * const stat_names[] = {
-       "rx_noise",
-       "rx_errors",
-       "rx_invalid",
-       "rx_overruns",
-       "rx_timeouts",
-       "rx_bad_crc",
-       "rx_ok",
+#define P(x) #x,
+       BSB_STATS
+#undef P
 };
 
 static void show_stats(byte *resp, uint len)
@@ -106,14 +105,29 @@ static void show_stats(byte *resp, uint len)
 
 static void show_packet(byte *pkt, uint len)
 {
+       if (!len) {
+               printf("ERROR: Received empty frame!\n");
+               return;
+       }
+
+       byte status = *pkt++;
+       len--;
+
+       if (!len) {
+               printf("Transmit status: %u\n", status);
+               return;
+       }
+
 #if 1
-       printf(":");
+       printf(": [%d]", status);
        for (uint i=0; i<len; i++)
                printf(" %02x", pkt[i]);
        putchar('\n');
 #endif
 
-       printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[2]);
+       printf("%02x -> %02x: ", pkt[BF_SRC] ^ 0x80, pkt[BF_DEST]);
+       if (status)
+               printf("[REPLY] ");
        switch (pkt[4]) {
                case 2:
                        printf("INFO %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
@@ -145,7 +159,9 @@ int main(void)
        // libusb_set_debug(usb_ctxt, 3);
        open_device();
 
+       time_t now = time(NULL);
        time_t last_stats = 0;
+       time_t last_query = now;
        int received;
        byte resp[64];
 
@@ -158,7 +174,7 @@ int main(void)
                        }
                }
 
-               time_t now = time(NULL);
+               now = time(NULL);
                if (last_stats + 60 < now) {
                        if ((received = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, sizeof(resp), 1000)) < 0) {
                                usb_error("Receive failed: error %d", received);
@@ -169,6 +185,18 @@ int main(void)
                        last_stats = now;
                }
 
+               if (last_query + 10 < now) {
+                       byte pkt[] = { 0xdc, 0xc2, 0x00, 0x0b, 0x06, 0x3d, 0x2e, 0x11, 0x25, 0x00, 0x00 };
+                       if (err = libusb_bulk_transfer(devh, 0x01, pkt, sizeof(pkt), &received, 2000)) {
+                               printf("Send failed: error %d\n", err);
+                               // usb_error("Receive failed: error %d", received);
+                               // continue;
+                       } else {
+                               printf("Send OK: %d bytes\n", received);
+                       }
+                       last_query = now;
+               }
+
 #if 0
                if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
                        usb_error("Receive failed: error %d", err);