+++ /dev/null
-/*
- * This file is part of the libsigrok project.
- *
- * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <glib.h>
-#include <libusb.h>
-#include <stdlib.h>
-#include <string.h>
-#include <libsigrok/libsigrok.h>
-#include "libsigrok-internal.h"
-#include "protocol.h"
-
-#define VICTOR_VID 0x1244
-#define VICTOR_PID 0xd237
-#define VICTOR_INTERFACE 0
-#define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
-
-static const uint32_t scanopts[] = {
- SR_CONF_CONN,
-};
-
-static const uint32_t drvopts[] = {
- SR_CONF_MULTIMETER,
-};
-
-static const uint32_t devopts[] = {
- SR_CONF_CONTINUOUS,
- SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
- SR_CONF_LIMIT_MSEC | SR_CONF_SET,
- SR_CONF_CONN | SR_CONF_GET,
-};
-
-static GSList *scan(struct sr_dev_driver *di, GSList *options)
-{
- struct drv_context *drvc;
- struct dev_context *devc;
- struct sr_dev_inst *sdi;
- struct libusb_device_descriptor des;
- libusb_device **devlist;
- GSList *devices;
- int i;
- char connection_id[64];
-
- (void)options;
-
- drvc = di->context;
-
- devices = NULL;
- libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
- for (i = 0; devlist[i]; i++) {
- libusb_get_device_descriptor(devlist[i], &des);
-
- if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
- continue;
-
- if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
- continue;
-
- sdi = g_malloc0(sizeof(struct sr_dev_inst));
- sdi->status = SR_ST_INACTIVE;
- sdi->vendor = g_strdup("Victor");
- sdi->connection_id = g_strdup(connection_id);
- devc = g_malloc0(sizeof(struct dev_context));
- sr_sw_limits_init(&devc->limits);
- sdi->priv = devc;
-
- sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
- sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
- libusb_get_device_address(devlist[i]), NULL);
- sdi->inst_type = SR_INST_USB;
-
- devices = g_slist_append(devices, sdi);
- }
- libusb_free_device_list(devlist, 1);
-
- return std_scan_complete(di, devices);
-}
-
-static int dev_open(struct sr_dev_inst *sdi)
-{
- struct sr_dev_driver *di = sdi->driver;
- struct drv_context *drvc = di->context;
- struct sr_usb_dev_inst *usb;
- int ret;
-
- usb = sdi->conn;
-
- ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
- if (ret != SR_OK)
- return ret;
-
- if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
- if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
- sr_err("Failed to detach kernel driver: %s.",
- libusb_error_name(ret));
- return SR_ERR;
- }
- }
-
- if ((ret = libusb_claim_interface(usb->devhdl,
- VICTOR_INTERFACE))) {
- sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
- return SR_ERR;
- }
-
- return SR_OK;
-}
-
-static int dev_close(struct sr_dev_inst *sdi)
-{
- struct sr_usb_dev_inst *usb;
-
- usb = sdi->conn;
-
- if (!usb->devhdl)
- return SR_ERR_BUG;
-
- libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
- libusb_close(usb->devhdl);
- usb->devhdl = NULL;
-
- return SR_OK;
-}
-
-static int config_get(uint32_t key, GVariant **data,
- const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
-{
- struct dev_context *devc = sdi->priv;
- struct sr_usb_dev_inst *usb;
-
- (void)cg;
-
- switch (key) {
- case SR_CONF_CONN:
- if (!sdi || !sdi->conn)
- return SR_ERR_ARG;
- usb = sdi->conn;
- *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
- break;
- case SR_CONF_LIMIT_SAMPLES:
- case SR_CONF_LIMIT_MSEC:
- return sr_sw_limits_config_get(&devc->limits, key, data);
- default:
- return SR_ERR_NA;
- }
-
- return SR_OK;
-}
-
-static int config_set(uint32_t key, GVariant *data,
- const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
-{
- struct dev_context *devc;
-
- (void)cg;
-
- devc = sdi->priv;
-
- return sr_sw_limits_config_set(&devc->limits, key, data);
-}
-
-static int config_list(uint32_t key, GVariant **data,
- const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
-{
- return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
-}
-
-static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
-{
- struct dev_context *devc;
- struct sr_dev_inst *sdi;
- int ret;
-
- sdi = transfer->user_data;
- devc = sdi->priv;
- if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
- /* USB device was unplugged. */
- sr_dev_acquisition_stop(sdi);
- } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
- sr_dbg("Got %d-byte packet.", transfer->actual_length);
- if (transfer->actual_length == DMM_DATA_SIZE) {
- victor_dmm_receive_data(sdi, transfer->buffer);
- if (sr_sw_limits_check(&devc->limits))
- sr_dev_acquisition_stop(sdi);
- }
- }
- /* Anything else is either an error or a timeout, which is fine:
- * we were just going to send another transfer request anyway. */
-
- if (sdi->status == SR_ST_ACTIVE) {
- /* Send the same request again. */
- if ((ret = libusb_submit_transfer(transfer) != 0)) {
- sr_err("Unable to resubmit transfer: %s.",
- libusb_error_name(ret));
- g_free(transfer->buffer);
- libusb_free_transfer(transfer);
- sr_dev_acquisition_stop(sdi);
- }
- } else {
- /* This was the last transfer we're going to receive, so
- * clean up now. */
- g_free(transfer->buffer);
- libusb_free_transfer(transfer);
- }
-}
-
-static int handle_events(int fd, int revents, void *cb_data)
-{
- struct dev_context *devc;
- struct drv_context *drvc;
- struct sr_dev_inst *sdi;
- struct sr_dev_driver *di;
- struct timeval tv;
-
- (void)fd;
- (void)revents;
-
- sdi = cb_data;
- devc = sdi->priv;
- di = sdi->driver;
- drvc = di->context;
-
- if (sr_sw_limits_check(&devc->limits))
- sr_dev_acquisition_stop(sdi);
-
- if (sdi->status == SR_ST_STOPPING) {
- usb_source_remove(sdi->session, drvc->sr_ctx);
- dev_close(sdi);
- std_session_send_df_end(sdi);
- }
-
- memset(&tv, 0, sizeof(struct timeval));
- libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
- NULL);
-
- return TRUE;
-}
-
-static int dev_acquisition_start(const struct sr_dev_inst *sdi)
-{
- struct sr_dev_driver *di = sdi->driver;
- struct drv_context *drvc = di->context;
- struct sr_usb_dev_inst *usb;
- struct libusb_transfer *transfer;
- int ret;
- unsigned char *buf;
-
- usb = sdi->conn;
-
- std_session_send_df_header(sdi);
-
- usb_source_add(sdi->session, drvc->sr_ctx, 100,
- handle_events, (void *)sdi);
-
- buf = g_malloc(DMM_DATA_SIZE);
- transfer = libusb_alloc_transfer(0);
- /* Each transfer request gets 100ms to arrive before it's restarted.
- * The device only sends 1 transfer/second no matter how many
- * times you ask, but we want to keep step with the USB events
- * handling above. */
- libusb_fill_interrupt_transfer(transfer, usb->devhdl,
- VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
- (struct sr_dev_inst *)sdi, 100);
- if ((ret = libusb_submit_transfer(transfer) != 0)) {
- sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
- libusb_free_transfer(transfer);
- g_free(buf);
- return SR_ERR;
- }
-
- return SR_OK;
-}
-
-static int dev_acquisition_stop(struct sr_dev_inst *sdi)
-{
- sdi->status = SR_ST_STOPPING;
-
- return SR_OK;
-}
-
-static struct sr_dev_driver victor_dmm_driver_info = {
- .name = "victor-dmm",
- .longname = "Victor DMMs",
- .api_version = 1,
- .init = std_init,
- .cleanup = std_cleanup,
- .scan = scan,
- .dev_list = std_dev_list,
- .dev_clear = std_dev_clear,
- .config_get = config_get,
- .config_set = config_set,
- .config_list = config_list,
- .dev_open = dev_open,
- .dev_close = dev_close,
- .dev_acquisition_start = dev_acquisition_start,
- .dev_acquisition_stop = dev_acquisition_stop,
- .context = NULL,
-};
-SR_REGISTER_DEV_DRIVER(victor_dmm_driver_info);
+++ /dev/null
-/*
- * This file is part of the libsigrok project.
- *
- * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <glib.h>
-#include <string.h>
-#include <math.h>
-#include <libsigrok/libsigrok.h>
-#include "libsigrok-internal.h"
-#include "protocol.h"
-
-/* Reverse the high nibble into the low nibble */
-static uint8_t decode_digit(uint8_t in)
-{
- uint8_t out, i;
-
- out = 0;
- in >>= 4;
- for (i = 0x08; i; i >>= 1) {
- out >>= 1;
- if (in & i)
- out |= 0x08;
- }
-
- return out;
-}
-
-static void decode_buf(struct sr_dev_inst *sdi, unsigned char *data)
-{
- struct sr_datafeed_packet packet;
- struct sr_datafeed_analog analog;
- struct sr_analog_encoding encoding;
- struct sr_analog_meaning meaning;
- struct sr_analog_spec spec;
- struct dev_context *devc;
- long factor, ivalue;
- uint8_t digits[4];
- gboolean is_duty, is_continuity, is_diode, is_ac, is_dc, is_auto;
- gboolean is_hold, is_max, is_min, is_relative, minus;
- float fvalue;
-
- devc = sdi->priv;
-
- digits[0] = decode_digit(data[12]);
- digits[1] = decode_digit(data[11]);
- digits[2] = decode_digit(data[10]);
- digits[3] = decode_digit(data[9]);
-
- if (digits[0] == 0x0f && digits[1] == 0x00 && digits[2] == 0x0a &&
- digits[3] == 0x0f)
- /* The "over limit" (OL) display comes through like this */
- ivalue = -1;
- else if (digits[0] > 9 || digits[1] > 9 || digits[2] > 9 || digits[3] > 9)
- /* An invalid digit in any position denotes no value. */
- ivalue = -2;
- else {
- ivalue = digits[0] * 1000;
- ivalue += digits[1] * 100;
- ivalue += digits[2] * 10;
- ivalue += digits[3];
- }
-
- /* Decimal point position */
- factor = 0;
- switch (data[7] >> 4) {
- case 0x00:
- factor = 0;
- break;
- case 0x02:
- factor = 1;
- break;
- case 0x04:
- factor = 2;
- break;
- case 0x08:
- factor = 3;
- break;
- default:
- sr_err("Unknown decimal point byte: 0x%.2x.", data[7]);
- break;
- }
-
- /* Minus flag */
- minus = data[2] & 0x01;
-
- /* Mode detail symbols on the right side of the digits */
- is_duty = is_continuity = is_diode = FALSE;
- switch (data[4]) {
- case 0x00:
- /* None. */
- break;
- case 0x01:
- /* Micro */
- factor += 6;
- break;
- case 0x02:
- /* Milli */
- factor += 3;
- break;
- case 0x04:
- /* Kilo */
- ivalue *= 1000;
- break;
- case 0x08:
- /* Mega */
- ivalue *= 1000000;
- break;
- case 0x10:
- /* Continuity shows up as Ohm + this bit */
- is_continuity = TRUE;
- break;
- case 0x20:
- /* Diode tester is Volt + this bit */
- is_diode = TRUE;
- break;
- case 0x40:
- is_duty = TRUE;
- break;
- case 0x80:
- /* Never seen */
- sr_dbg("Unknown mode right detail: 0x%.2x.", data[4]);
- break;
- default:
- sr_dbg("Unknown/invalid mode right detail: 0x%.2x.", data[4]);
- break;
- }
-
- /* Scale flags on the right, continued */
- is_max = is_min = FALSE;
- if (data[5] & 0x04)
- is_max = TRUE;
- if (data[5] & 0x08)
- is_min = TRUE;
- if (data[5] & 0x40)
- /* Nano */
- factor += 9;
-
- /* Mode detail symbols on the left side of the digits */
- is_auto = is_dc = is_ac = is_hold = is_relative = FALSE;
- if (data[6] & 0x04)
- is_auto = TRUE;
- if (data[6] & 0x08)
- is_dc = TRUE;
- if (data[6] & 0x10)
- is_ac = TRUE;
- if (data[6] & 0x20)
- is_relative = TRUE;
- if (data[6] & 0x40)
- is_hold = TRUE;
-
- fvalue = (float)ivalue / pow(10, factor);
- if (minus)
- fvalue = -fvalue;
-
- sr_analog_init(&analog, &encoding, &meaning, &spec, 4);
-
- /* Measurement mode */
- meaning.channels = sdi->channels;
- meaning.mq = 0;
- switch (data[3]) {
- case 0x00:
- if (is_duty) {
- meaning.mq = SR_MQ_DUTY_CYCLE;
- meaning.unit = SR_UNIT_PERCENTAGE;
- } else
- sr_dbg("Unknown measurement mode: %.2x.", data[3]);
- break;
- case 0x01:
- if (is_diode) {
- meaning.mq = SR_MQ_VOLTAGE;
- meaning.unit = SR_UNIT_VOLT;
- meaning.mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
- if (ivalue < 0)
- fvalue = NAN;
- } else {
- if (ivalue < 0)
- break;
- meaning.mq = SR_MQ_VOLTAGE;
- meaning.unit = SR_UNIT_VOLT;
- if (is_ac)
- meaning.mqflags |= SR_MQFLAG_AC;
- if (is_dc)
- meaning.mqflags |= SR_MQFLAG_DC;
- }
- break;
- case 0x02:
- meaning.mq = SR_MQ_CURRENT;
- meaning.unit = SR_UNIT_AMPERE;
- if (is_ac)
- meaning.mqflags |= SR_MQFLAG_AC;
- if (is_dc)
- meaning.mqflags |= SR_MQFLAG_DC;
- break;
- case 0x04:
- if (is_continuity) {
- meaning.mq = SR_MQ_CONTINUITY;
- meaning.unit = SR_UNIT_BOOLEAN;
- fvalue = ivalue < 0 ? 0.0 : 1.0;
- } else {
- meaning.mq = SR_MQ_RESISTANCE;
- meaning.unit = SR_UNIT_OHM;
- if (ivalue < 0)
- fvalue = INFINITY;
- }
- break;
- case 0x08:
- /* Never seen */
- sr_dbg("Unknown measurement mode: 0x%.2x.", data[3]);
- break;
- case 0x10:
- meaning.mq = SR_MQ_FREQUENCY;
- meaning.unit = SR_UNIT_HERTZ;
- break;
- case 0x20:
- meaning.mq = SR_MQ_CAPACITANCE;
- meaning.unit = SR_UNIT_FARAD;
- break;
- case 0x40:
- meaning.mq = SR_MQ_TEMPERATURE;
- meaning.unit = SR_UNIT_CELSIUS;
- break;
- case 0x80:
- meaning.mq = SR_MQ_TEMPERATURE;
- meaning.unit = SR_UNIT_FAHRENHEIT;
- break;
- default:
- sr_dbg("Unknown/invalid measurement mode: 0x%.2x.", data[3]);
- break;
- }
- if (meaning.mq == 0)
- return;
-
- if (is_auto)
- meaning.mqflags |= SR_MQFLAG_AUTORANGE;
- if (is_hold)
- meaning.mqflags |= SR_MQFLAG_HOLD;
- if (is_max)
- meaning.mqflags |= SR_MQFLAG_MAX;
- if (is_min)
- meaning.mqflags |= SR_MQFLAG_MIN;
- if (is_relative)
- meaning.mqflags |= SR_MQFLAG_RELATIVE;
-
- analog.data = &fvalue;
- analog.num_samples = 1;
-
- packet.type = SR_DF_ANALOG;
- packet.payload = &analog;
- sr_session_send(sdi, &packet);
-
- sr_sw_limits_update_samples_read(&devc->limits, 1);
-}
-
-SR_PRIV int victor_dmm_receive_data(struct sr_dev_inst *sdi, unsigned char *buf)
-{
- static const unsigned char obfuscation[DMM_DATA_SIZE] = "jodenxunickxia";
- static const unsigned char shuffle[DMM_DATA_SIZE] = {
- 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
- };
- GString *dbg;
- int i;
- unsigned char data[DMM_DATA_SIZE];
-
- for (i = 0; i < DMM_DATA_SIZE && buf[i] == 0; i++);
- if (i == DMM_DATA_SIZE) {
- /* This DMM outputs all zeroes from time to time, just ignore it. */
- sr_dbg("Received all zeroes.");
- return SR_OK;
- }
-
- /* Deobfuscate and reorder data. */
- for (i = 0; i < DMM_DATA_SIZE; i++)
- data[shuffle[i]] = (buf[i] - obfuscation[i]) & 0xff;
-
- if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
- dbg = g_string_sized_new(128);
- g_string_printf(dbg, "Deobfuscated.");
- for (i = 0; i < DMM_DATA_SIZE; i++)
- g_string_append_printf(dbg, " %.2x", data[i]);
- sr_spew("%s", dbg->str);
- g_string_free(dbg, TRUE);
- }
-
- decode_buf(sdi, data);
-
- return SR_OK;
-}