From 5380b247af47dea198aaf2c68ac7aaf0cc8ef69e Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 30 Apr 2020 19:23:59 +0200 Subject: [PATCH] Update the example application The application is now responsible for setting up the USB based I/O stream. --- examples/common.c | 38 +++++++++++++++++++++++++++++++++++++- examples/dctool_scan.c | 8 ++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/examples/common.c b/examples/common.c index cfabc03..d4e4ad4 100644 --- a/examples/common.c +++ b/examples/common.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "common.h" @@ -398,6 +399,41 @@ dctool_file_read (const char *filename) return buffer; } +static dc_status_t +dctool_usb_open (dc_iostream_t **out, dc_context_t *context, dc_descriptor_t *descriptor) +{ + dc_status_t status = DC_STATUS_SUCCESS; + dc_iostream_t *iostream = NULL; + + // Discover the usb device. + dc_iterator_t *iterator = NULL; + dc_usb_device_t *device = NULL; + dc_usb_iterator_new (&iterator, context, descriptor); + while (dc_iterator_next (iterator, &device) == DC_STATUS_SUCCESS) { + break; + } + dc_iterator_free (iterator); + + if (device == NULL) { + ERROR ("No dive computer found."); + status = DC_STATUS_NODEVICE; + goto cleanup; + } + + // Open the usb device. + status = dc_usb_open (&iostream, context, device); + if (status != DC_STATUS_SUCCESS) { + ERROR ("Failed to open the usb device."); + goto cleanup; + } + + *out = iostream; + +cleanup: + dc_usb_device_free (device); + return status; +} + static dc_status_t dctool_usbhid_open (dc_iostream_t **out, dc_context_t *context, dc_descriptor_t *descriptor) { @@ -532,7 +568,7 @@ dctool_iostream_open (dc_iostream_t **iostream, dc_context_t *context, dc_descri case DC_TRANSPORT_SERIAL: return dc_serial_open (iostream, context, devname); case DC_TRANSPORT_USB: - return DC_STATUS_SUCCESS; + return dctool_usb_open(iostream, context, descriptor); case DC_TRANSPORT_USBHID: return dctool_usbhid_open(iostream, context, descriptor); case DC_TRANSPORT_IRDA: diff --git a/examples/dctool_scan.c b/examples/dctool_scan.c index b96d014..a68d46b 100644 --- a/examples/dctool_scan.c +++ b/examples/dctool_scan.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include "dctool.h" @@ -59,6 +60,9 @@ scan (dc_context_t *context, dc_descriptor_t *descriptor, dc_transport_t transpo case DC_TRANSPORT_BLUETOOTH: status = dc_bluetooth_iterator_new (&iterator, context, descriptor); break; + case DC_TRANSPORT_USB: + status = dc_usb_iterator_new (&iterator, context, descriptor); + break; case DC_TRANSPORT_USBHID: status = dc_usbhid_iterator_new (&iterator, context, descriptor); break; @@ -90,6 +94,10 @@ scan (dc_context_t *context, dc_descriptor_t *descriptor, dc_transport_t transpo dc_bluetooth_device_get_name (device)); dc_bluetooth_device_free (device); break; + case DC_TRANSPORT_USB: + printf ("%04x:%04x\n", dc_usb_device_get_vid (device), dc_usb_device_get_pid (device)); + dc_usb_device_free (device); + break; case DC_TRANSPORT_USBHID: printf ("%04x:%04x\n", dc_usbhid_device_get_vid (device), dc_usbhid_device_get_pid (device)); dc_usbhid_device_free (device);