#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
-#include <libusb-1.0/libusb.h>
+#include <libusb.h>
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;
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)) {
libusb_free_device_list(devlist, 1);
}
+static inline uint get_u32_le(byte *p)
+{
+ return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
+}
+
+static const char * const stat_names[] = {
+#define P(x) #x,
+ BSB_STATS
+#undef P
+};
+
+static void show_stats(byte *resp, uint len)
+{
+ printf("# Stats:");
+ for (uint i=0; 4*i + 3 < (uint) len; i++)
+ printf(" %s=%u", (i < sizeof(stat_names) / sizeof(stat_names[0]) ? stat_names[i] : "?"), get_u32_le(resp+4*i));
+ printf("\n");
+
+ fflush(stdout);
+}
+
+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(": [%d]", status);
+ for (uint i=0; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+#endif
+
+ 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]);
+ for (uint i=9; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+ break;
+ case 6:
+ printf("GET %04x:%04x\n", (pkt[6]<<8) | pkt[5], (pkt[7]<<8) | pkt[8]);
+ break;
+ case 7:
+ printf("RET %04x:%04x =", (pkt[5]<<8) | pkt[6], (pkt[7]<<8) | pkt[8]);
+ for (uint i=9; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+ break;
+ default:
+ printf("??? type=%02x\n", pkt[4]);
+ }
+
+ fflush(stdout);
+}
+
int main(void)
{
int err;
// 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];
+
for (;;) {
if (!devh) {
fprintf(stderr, "Waiting for device to appear...\n");
}
}
-#if 0
- int received;
- unsigned char resp[64];
- if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
- usb_error("Receive failed: error %d", err);
- continue;
+ 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);
+ continue;
+ }
+
+ show_stats(resp, received);
+ last_stats = now;
}
- printf("Received %d bytes\n", received);
- if (received < 4) {
- usb_error("Receive failed: unexpected response size %d", received);
- continue;
+ 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;
}
- printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
-#elif 0
- int received;
- unsigned char resp[64];
- if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 100)) {
+#if 0
+ if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
usb_error("Receive failed: error %d", err);
continue;
}
- printf("Interrupt received %d bytes\n", received);
+ printf("Received %d bytes\n", received);
if (received < 4) {
usb_error("Receive failed: unexpected response size %d", received);
continue;
}
printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
-#else
- unsigned char resp[64] = { 1, 2, 3, 4 };
- if ((err = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, 4, 100)) < 0) {
- usb_error("Receive failed: error %d", err);
+#endif
+
+ if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 1000)) {
+ if (err != LIBUSB_ERROR_TIMEOUT)
+ usb_error("Receive failed: error %d", err);
continue;
}
- printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
-#endif
-
- sleep(1);
+ show_packet(resp, received);
}
}