libsigrok  0.5.2
sigrok hardware access and backend library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
serial_hid_victor.c
Go to the documentation of this file.
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 
58 static const struct vid_pid_item vid_pid_items_victor[] = {
59  { 0x1244, 0xd237, },
60  ALL_ZERO
61 };
62 
63 /* Reverse bits within a byte. */
64 static uint8_t bit_reverse(uint8_t b)
65 {
66  static const uint8_t rev_nibble[] = {
67  0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
68  0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f,
69  };
70 
71  return (rev_nibble[(b >> 4) & 0xf]) |
72  (rev_nibble[b & 0xf] << 4);
73 }
74 
75 /*
76  * The cable receives data by means of HID reports (simple data stream,
77  * HID report encapsulation was already trimmed). Assume that received
78  * data "is aligned", cope with zero or one 14-byte packets here, but
79  * don't try to even bother with odd-length reception units. Also drop
80  * the "all-zero" packets here which victor_dmm_receive_data() used to
81  * eliminate at the device driver level in the past.
82  */
83 static int victor_unobfuscate(uint8_t *rx_buf, size_t rx_len,
84  uint8_t *ret_buf)
85 {
86  static const uint8_t obfuscation[VICTOR_DMM_PACKET_LENGTH] = {
87  'j', 'o', 'd', 'e', 'n', 'x', 'u', 'n', 'i', 'c', 'k', 'x', 'i', 'a',
88  };
89  static const uint8_t shuffle[VICTOR_DMM_PACKET_LENGTH] = {
90  6, 13, 5, 11, 2, 7, 9, 8, 3, 10, 12, 0, 4, 1
91  };
92 
93  GString *txt;
94  int is_zero;
95  size_t idx, to_idx;
96 
98  txt = sr_hexdump_new(rx_buf, rx_len);
99  sr_spew("Received %zu bytes: %s.", rx_len, txt->str);
100  sr_hexdump_free(txt);
101  }
102 
103  /* Pass unexpected data in verbatim form. */
104  if (rx_len != VICTOR_DMM_PACKET_LENGTH) {
105  memcpy(ret_buf, rx_buf, rx_len);
106  return rx_len;
107  }
108 
109  /* Check for and discard all-zero packets. */
110  is_zero = 1;
111  for (idx = 0; is_zero && idx < VICTOR_DMM_PACKET_LENGTH; idx++) {
112  if (rx_buf[idx])
113  is_zero = 0;
114  }
115  if (is_zero) {
116  sr_dbg("Received all zeroes packet, discarding.");
117  return 0;
118  }
119 
120  /*
121  * Unobfuscate data bytes by subtracting a magic pattern, shuffle
122  * the bits and bytes into the DMM chip's original order.
123  */
124  for (idx = 0; idx < VICTOR_DMM_PACKET_LENGTH; idx++) {
125  to_idx = VICTOR_DMM_PACKET_LENGTH - 1 - shuffle[idx];
126  ret_buf[to_idx] = bit_reverse(rx_buf[idx] - obfuscation[idx]);
127  }
128 
129  if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
130  txt = sr_hexdump_new(ret_buf, idx);
131  sr_spew("Deobfuscated: %s.", txt->str);
132  sr_hexdump_free(txt);
133  }
134 
135  return rx_len;
136 }
137 
138 /*
139  * Read into a local buffer, and unobfuscate into the caller's buffer.
140  * Always receive full DMM packets.
141  */
142 static int victor_read_bytes(struct sr_serial_dev_inst *serial,
143  uint8_t *data, int space, unsigned int timeout)
144 {
145  uint8_t buf[VICTOR_DMM_PACKET_LENGTH];
146  int rc;
147 
148  if (space != sizeof(buf))
149  space = sizeof(buf);
150  rc = ser_hid_hidapi_get_data(serial, 0, buf, space, timeout);
151  if (rc == SR_ERR_TIMEOUT)
152  return 0;
153  if (rc < 0)
154  return rc;
155  if (rc == 0)
156  return 0;
157 
158  return victor_unobfuscate(buf, rc, data);
159 }
160 
161 /* Victor DMM cables are read-only. Just pretend successful transmission. */
162 static int victor_write_bytes(struct sr_serial_dev_inst *serial,
163  const uint8_t *data, int size)
164 {
165  (void)serial;
166  (void)data;
167 
168  return size;
169 }
170 
171 static struct ser_hid_chip_functions chip_victor = {
172  .chipname = "victor",
173  .chipdesc = "Victor DMM scrambler",
174  .vid_pid_items = vid_pid_items_victor,
175  .max_bytes_per_request = VICTOR_DMM_PACKET_LENGTH,
176  /*
177  * The USB HID connection has no concept of UART bitrate or
178  * frame format. Silently ignore the parameters.
179  */
180  .set_params = std_dummy_set_params,
181  .read_bytes = victor_read_bytes,
182  .write_bytes = victor_write_bytes,
183 };
184 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor = &chip_victor;
185 
186 #else
187 
188 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_victor = NULL;
189 
190 #endif
191 #endif
Output very noisy debug messages.
Definition: libsigrok.h:100
The public libsigrok header file to be used by frontends.
SR_PRIV GString * sr_hexdump_new(const uint8_t *data, const size_t len)
Convert a sequence of bytes to its textual representation ("hex dump").
Definition: strutil.c:603
SR_PRIV void sr_hexdump_free(GString *s)
Free a hex dump text that was created by sr_hexdump_new().
Definition: strutil.c:623
#define SR_PRIV
Definition: libsigrok.h:128
A timeout occurred.
Definition: libsigrok.h:75
int sr_log_loglevel_get(void)
Get the libsigrok loglevel.
Definition: log.c:107
SR_PRIV int ser_hid_hidapi_get_data(struct sr_serial_dev_inst *serial, uint8_t ep, uint8_t *data, size_t len, int timeout)