]> sigrok.org Git - libsigrok.git/blame - src/serial_hid_victor.c
serial-hid: add support for (scrambling) Victor DMM cables
[libsigrok.git] / src / serial_hid_victor.c
CommitLineData
0cdb72c8
GS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * This implements serial transport primitives for Victor DMM cables,
22 * which forward normal DMM chips' protocols, but scramble the data in
23 * the process of forwarding. Just undoing the cable's scrambling at
24 * the serial communication level allows full re-use of existing DMM
25 * drivers, instead of creating Victor DMM specific support code.
26 *
27 * The cable's scrambling is somewhat complex:
28 * - The order of bits within the bytes gets reversed.
29 * - The order of bytes within the packet gets shuffled (randomly).
30 * - The byte values randomly get mapped to other values by adding a
31 * sequence of magic values to packet's byte values.
32 * None of this adds any value to the DMM chip vendor's protocol. It's
33 * mere obfuscation and extra complexity for the receiving application.
34 */
35
36#include <config.h>
37#include <glib.h>
38#include <libsigrok/libsigrok.h>
39#include "libsigrok-internal.h"
40#include "serial_hid.h"
41#include <string.h>
42
43/** @cond PRIVATE */
44#define LOG_PREFIX "serial-victor"
45/** @endcond */
46
47#ifdef HAVE_SERIAL_COMM
48#ifdef HAVE_LIBHIDAPI
49
50/**
51 * @file
52 *
53 * Support serial-over-HID, specifically the Victor 70/86 DMM cables.
54 */
55
56#define VICTOR_DMM_PACKET_LENGTH 14
57
58static const struct vid_pid_item vid_pid_items_victor[] = {
59 { 0x1244, 0xd237, },
60 VID_PID_TERM,
61};
62
63static int victor_set_params(struct sr_serial_dev_inst *serial,
64 int baudrate, int bits, int parity, int stopbits,
65 int flowcontrol, int rts, int dtr)
66{
67 /*
68 * The USB HID connection has no concept of UART bitrate or
69 * frame format. Silently ignore the parameters.
70 */
71 (void)serial;
72 (void)baudrate;
73 (void)bits;
74 (void)parity;
75 (void)stopbits;
76 (void)flowcontrol;
77 (void)rts;
78 (void)dtr;
79
80 return SR_OK;
81}
82
83/* Reverse bits within a byte. */
84static uint8_t bit_reverse(uint8_t b)
85{
86 static const uint8_t rev_nibble[] = {
87 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
88 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f,
89 };
90
91 return (rev_nibble[(b >> 4) & 0xf]) |
92 (rev_nibble[b & 0xf] << 4);
93}
94
95/*
96 * The cable receives data by means of HID reports (simple data stream,
97 * HID report encapsulation was already trimmed). Assume that received
98 * data "is aligned", cope with zero or one 14-byte packets here, but
99 * don't try to even bother with odd-length reception units. Also drop
100 * the "all-zero" packets here which victor_dmm_receive_data() used to
101 * eliminate at the device driver level in the past.
102 */
103static int victor_unobfuscate(uint8_t *rx_buf, size_t rx_len,
104 uint8_t *ret_buf)
105{
106 static const uint8_t obfuscation[VICTOR_DMM_PACKET_LENGTH] = {
107 'j', 'o', 'd', 'e', 'n', 'x', 'u', 'n', 'i', 'c', 'k', 'x', 'i', 'a',
108 };
109 static const uint8_t shuffle[VICTOR_DMM_PACKET_LENGTH] = {
110 6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
111 };
112
113 GString *txt;
114 int is_zero;
115 size_t idx, to_idx;
116
117 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
118 txt = sr_hexdump_new(rx_buf, rx_len);
119 sr_spew("Received %zu bytes: %s.", rx_len, txt->str);
120 sr_hexdump_free(txt);
121 }
122
123 /* Pass unexpected data in verbatim form. */
124 if (rx_len != VICTOR_DMM_PACKET_LENGTH) {
125 memcpy(ret_buf, rx_buf, rx_len);
126 return rx_len;
127 }
128
129 /* Check for and discard all-zero packets. */
130 is_zero = 1;
131 for (idx = 0; is_zero && idx < VICTOR_DMM_PACKET_LENGTH; idx++) {
132 if (rx_buf[idx])
133 is_zero = 0;
134 }
135 if (is_zero) {
136 sr_dbg("Received all zeroes packet, discarding.");
137 return 0;
138 }
139
140 /*
141 * Unobfuscate data bytes by subtracting a magic pattern, shuffle
142 * the bits and bytes into the DMM chip's original order.
143 */
144 for (idx = 0; idx < VICTOR_DMM_PACKET_LENGTH; idx++) {
145 to_idx = VICTOR_DMM_PACKET_LENGTH - 1 - shuffle[idx];
146 ret_buf[to_idx] = bit_reverse(rx_buf[idx] - obfuscation[idx]);
147 }
148
149 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
150 txt = sr_hexdump_new(ret_buf, idx);
151 sr_spew("Deobfuscated: %s.", txt->str);
152 sr_hexdump_free(txt);
153 }
154
155 return rx_len;
156}
157
158/*
159 * Read into a local buffer, and unobfuscate into the caller's buffer.
160 * Always receive full DMM packets.
161 */
162static int victor_read_bytes(struct sr_serial_dev_inst *serial,
163 uint8_t *data, int space, unsigned int timeout)
164{
165 uint8_t buf[VICTOR_DMM_PACKET_LENGTH];
166 int rc;
167
168 if (space != sizeof(buf))
169 space = sizeof(buf);
170 rc = ser_hid_hidapi_get_data(serial, 0, buf, space, timeout);
171 if (rc == SR_ERR_TIMEOUT)
172 return 0;
173 if (rc < 0)
174 return rc;
175 if (rc == 0)
176 return 0;
177
178 return victor_unobfuscate(buf, rc, data);
179}
180
181/* Victor DMM cables are read-only. Just pretend successful transmission. */
182static int victor_write_bytes(struct sr_serial_dev_inst *serial,
183 const uint8_t *data, int size)
184{
185 (void)serial;
186 (void)data;
187
188 return size;
189}
190
191static struct ser_hid_chip_functions chip_victor = {
192 .chipname = "victor",
193 .chipdesc = "Victor DMM scrambler",
194 .vid_pid_items = vid_pid_items_victor,
195 .max_bytes_per_request = VICTOR_DMM_PACKET_LENGTH,
196 .set_params = victor_set_params,
197 .read_bytes = victor_read_bytes,
198 .write_bytes = victor_write_bytes,
199};
200SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor = &chip_victor;
201
202#else
203
204SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor = NULL;
205
206#endif
207#endif