#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;
return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
}
+static const char * const stat_names[] = {
+ "rx_noise",
+ "rx_errors",
+ "rx_invalid",
+ "rx_overruns",
+ "rx_timeouts",
+ "rx_bad_crc",
+ "rx_ok",
+};
+
+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");
+}
+
+static void show_packet(byte *pkt, uint len)
+{
+#if 1
+ printf(":");
+ for (uint i=0; i<len; i++)
+ printf(" %02x", pkt[i]);
+ putchar('\n');
+#endif
+
+ printf("%02x -> %02x: ", pkt[1] ^ 0x80, pkt[2]);
+ 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]);
+ }
+}
+
int main(void)
{
int err;
// libusb_set_debug(usb_ctxt, 3);
open_device();
+ time_t last_stats = 0;
+ int received;
+ byte resp[64];
+
for (;;) {
if (!devh) {
fprintf(stderr, "Waiting for device to appear...\n");
}
}
+ time_t now = time(NULL);
+ if (last_stats + 10 < 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;
+ }
+
#if 0
- int received;
- byte resp[64];
if (err = libusb_bulk_transfer(devh, 0x81, resp, 64, &received, 2000)) {
usb_error("Receive failed: error %d", err);
continue;
}
printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
-#elif 0
- int received;
- byte resp[64];
- if (err = libusb_interrupt_transfer(devh, 0x82, resp, 64, &received, 100)) {
- usb_error("Receive failed: error %d", err);
- continue;
- }
-
- printf("Interrupt received %d bytes\n", received);
- if (received < 4) {
- usb_error("Receive failed: unexpected response size %d", received);
- continue;
- }
+#endif
- printf("-> %02x%02x%02x%02x\n", resp[0], resp[1], resp[2], resp[3]);
-#else
- byte resp[64];
- int received;
- if ((received = libusb_control_transfer(devh, 0xc0, 0x00, 0, 0, resp, sizeof(resp), 100)) < 0) {
- usb_error("Receive failed: error %d", received);
+ 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("Stats:");
- for (int i=0; i+3 < received; i+=4)
- printf(" %u", get_u32_le(resp+i));
- printf("\n");
-#endif
-
- sleep(1);
+ show_packet(resp, received);
}
}