#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <libusb.h>
+#define VISOMAT_DEVICE_VID 0x1247
+#define VISOMAT_DEVICE_PID 0x00f8
+#define VISOMAT_CONFIGURATION 1
+#define VISOMAT_INTERFACE 1
+#define VISOMAT_EP_IN 0x82
+#define VISOMAT_EP_OUT 0x03
+
#define STX 0x02
#define ETX 0x03
-#define DEVICE_VID 0x1247
-#define DEVICE_PID 0x00f8
-
-#define EP_IN 0x82
-#define EP_OUT 0x03
-
#define BASE_YEAR 2000
typedef enum {
};
struct pressure {
+ unsigned int flag; /* XXX Maybe this means arrhythmia? */
unsigned int systolic;
unsigned int diastolic;
unsigned int pulses;
pulse_pressure = p->systolic - p->diastolic;
printf("%u;", pulse_pressure);
+ if (p->flag)
+ printf("x");
+
+#if 0
+ /* The original software does not seem to be doing that */
if (p->pulses > 100)
printf("tachycardia");
else if (p->pulses < 60)
printf("bradycardia");
+#endif
printf("\n");
}
-/* TODO separate better decoding data from printing it */
-static int decode_eeprom(unsigned char *buffer, unsigned int len)
+/* TODO: it would be better to separate decoding data from printing it */
+static int decode_eeprom(unsigned char *buffer,
+ unsigned int len,
+ unsigned int user_mask)
{
int ret;
unsigned int n;
/* i tracks the bytes consumed */
i += 1;
-
ret = sscanf((char *)(buffer + i), "%1u%02u",
&user_id, &num_records);
if (ret != 2)
/* user_id and num_records take 3 bytes */
i += 3;
- printf("# User: %d\n", user_id);
+ /*
+ * when there are no records, there is a dummy byte
+ * which has to be consumed
+ */
+ if (num_records == 0)
+ i += 1;
for (j = 0; j < num_records; j++) {
ret = extract_datetime(buffer + i, &d);
i += 10;
ret = sscanf((char *)(buffer + i),
- "%04u%03u%03u",
+ "%1u%03u%03u%03u",
+ &p.flag,
&p.systolic,
&p.diastolic,
&p.pulses);
- if (ret != 3)
+ if (ret != 4)
return -EINVAL;
/* pressure data is 10 bytes */
i += 10;
- print_record_csv_compat(&d, &p);
+ /* TODO: split out the printing part */
+ if (user_id & user_mask) {
+ if (j == 0)
+ printf("# User: %d\n", user_id);
+ print_record_csv_compat(&d, &p);
+ }
}
}
}
request[4] = ETX;
transferred = 0;
- ret = libusb_bulk_transfer(dev, EP_OUT, request, sizeof(request), &transferred, 0);
+ ret = libusb_bulk_transfer(dev, VISOMAT_EP_OUT, request, sizeof(request), &transferred, 0);
if (ret != 0 || transferred != sizeof(request)) {
fprintf(stderr, "Error: sending request: %d (%s)\ttransferred: %d (expected %zu)\n",
ret, libusb_error_name(ret), transferred, sizeof(request));
unsigned int j;
transferred = 0;
- ret = libusb_bulk_transfer(dev, EP_IN, response, sizeof(response), &transferred, 5000);
+ ret = libusb_bulk_transfer(dev, VISOMAT_EP_IN, response, sizeof(response), &transferred, 5000);
if (ret != 0) {
fprintf(stderr, "Error getting response: %d (%s)\ttransferred: %d (expected %zu)\n",
ret, libusb_error_name(ret), transferred, sizeof(response));
/* Candidates for a future public API, if a shared library will ever be made */
#define visomat_device libusb_device_handle
-static int visomat_dump_eeprom(visomat_device *dev)
+static int visomat_dump_eeprom(visomat_device *dev, unsigned int user_mask)
{
/* Assuming an EEPROM of 1 KiB */
unsigned char buffer[1024] = { 0 };
if (ret < 0)
return ret;
- ret = decode_eeprom(buffer, ret);
+ ret = decode_eeprom(buffer, ret, user_mask);
if (ret < 0)
return ret;
{
int ret;
libusb_device_handle *dev;
+ int current_configuration;
+
+ ret = libusb_init(NULL);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_init failed: %s\n",
+ libusb_error_name(ret));
+ goto out;
+ }
- libusb_init(NULL);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
- dev = libusb_open_device_with_vid_pid(NULL, DEVICE_VID, DEVICE_PID);
+ dev = libusb_open_device_with_vid_pid(NULL,
+ VISOMAT_DEVICE_VID,
+ VISOMAT_DEVICE_PID);
if (dev == NULL) {
- fprintf(stderr, "Couldn't open device.\n");
- ret = -ENODEV;
+ fprintf(stderr, "libusb_open failed: %s\n", strerror(errno));
+ ret = -errno;
goto out_libusb_exit;
}
- libusb_set_configuration(dev, 1);
- libusb_detach_kernel_driver(dev, 1);
- libusb_claim_interface(dev, 1);
+ current_configuration = -1;
+ ret = libusb_get_configuration(dev, ¤t_configuration);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_get_configuration failed: %s\n",
+ libusb_error_name(ret));
+ goto out_libusb_close;
+ }
+
+ if (current_configuration != VISOMAT_CONFIGURATION) {
+ ret = libusb_set_configuration(dev, VISOMAT_CONFIGURATION);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_set_configuration failed: %s\n",
+ libusb_error_name(ret));
+ fprintf(stderr, "Cannot set configuration %d\n",
+ VISOMAT_CONFIGURATION);
+ goto out_libusb_close;
+ }
+ }
+
+ libusb_set_auto_detach_kernel_driver(dev, 1);
+
+ ret = libusb_claim_interface(dev, VISOMAT_INTERFACE);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_claim_interface failed: %s\n",
+ libusb_error_name(ret));
+ fprintf(stderr, "Cannot claim interface %d\n",
+ VISOMAT_INTERFACE);
+ goto out_libusb_close;
+ }
+
+ /* Checking that the configuration has not changed, as suggested in
+ * http://libusb.sourceforge.net/api-1.0/caveats.html
+ */
+ current_configuration = -1;
+ ret = libusb_get_configuration(dev, ¤t_configuration);
+ if (ret < 0) {
+ fprintf(stderr, "libusb_get_configuration after claim failed: %s\n",
+ libusb_error_name(ret));
+ goto out_libusb_release_interface;
+ }
+
+ if (current_configuration != VISOMAT_CONFIGURATION) {
+ fprintf(stderr, "libusb configuration changed (expected: %d, current: %d\n",
+ VISOMAT_CONFIGURATION, current_configuration);
+ ret = -EINVAL;
+ goto out_libusb_release_interface;
+ }
ret = visomat_get_datetime(dev);
if (ret < 0)
- goto out;
+ goto out_libusb_release_interface;
- ret = visomat_dump_eeprom(dev);
+ ret = visomat_dump_eeprom(dev, 0x01 | 0x02);
+ if (ret < 0)
+ goto out_libusb_release_interface;
-out:
+out_libusb_release_interface:
+ libusb_release_interface(dev, VISOMAT_INTERFACE);
+out_libusb_close:
libusb_close(dev);
+ dev = NULL;
out_libusb_exit:
libusb_exit(NULL);
+out:
return ret;
}