]> sigrok.org Git - libsigrok.git/blame - src/hardware/uni-t-ut32x/protocol.c
uni-t-ut32x: use ASCII literals in packet parser, symbols for magic numbers
[libsigrok.git] / src / hardware / uni-t-ut32x / protocol.c
CommitLineData
3877dde4
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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
6ec6c43b 20#include <config.h>
d6ff054a
BV
21#include <string.h>
22#include <math.h>
515ab088 23#include "protocol.h"
d6ff054a 24
4b319782
GS
25#define SEP "\r\n"
26#define BLANK ':'
27#define NEG ';'
28
29/*
30 * Get a temperature value from a four-character buffer. The value is
31 * encoded in ASCII and the unit is deci-degrees (tenths of degrees).
32 */
d6ff054a 33static float parse_temperature(unsigned char *buf)
3877dde4 34{
d6ff054a
BV
35 float temp;
36 int i;
37 gboolean negative;
38
39 negative = FALSE;
40 temp = 0.0;
41 for (i = 0; i < 4; i++) {
4b319782 42 if (buf[i] == BLANK)
d6ff054a 43 continue;
4b319782 44 if (buf[i] == NEG) {
d6ff054a
BV
45 if (negative) {
46 sr_dbg("Double negative sign!");
47 return NAN;
d6ff054a 48 }
4b319782
GS
49 negative = TRUE;
50 continue;
d6ff054a 51 }
4b319782 52 if (buf[i] < '0' || buf[i] > '9') {
d6ff054a
BV
53 sr_dbg("Invalid digit '%.2x'!", buf[i]);
54 return NAN;
55 }
56 temp *= 10;
4b319782 57 temp += buf[i] - '0';
d6ff054a
BV
58 }
59 temp /= 10;
60 if (negative)
61 temp = -temp;
3877dde4 62
d6ff054a
BV
63 return temp;
64}
65
66static void process_packet(struct sr_dev_inst *sdi)
67{
3877dde4 68 struct dev_context *devc;
d6ff054a 69 struct sr_datafeed_packet packet;
16544b38
UH
70 struct sr_datafeed_analog analog;
71 struct sr_analog_encoding encoding;
72 struct sr_analog_meaning meaning;
73 struct sr_analog_spec spec;
d6ff054a
BV
74 GString *spew;
75 float temp;
76 int i;
77 gboolean is_valid;
3877dde4 78
d6ff054a
BV
79 devc = sdi->priv;
80 sr_dbg("Received full 19-byte packet.");
81 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
82 spew = g_string_sized_new(60);
83 for (i = 0; i < devc->packet_len; i++)
84 g_string_append_printf(spew, "%.2x ", devc->packet[i]);
85 sr_spew("%s", spew->str);
86 g_string_free(spew, TRUE);
87 }
3877dde4 88
d6ff054a 89 is_valid = TRUE;
4b319782
GS
90 if (devc->packet[1] == NEG && devc->packet[2] == NEG
91 && devc->packet[3] == NEG && devc->packet[4] == NEG)
ba7dd8bb 92 /* No measurement: missing channel, empty storage location, ... */
d6ff054a 93 is_valid = FALSE;
3877dde4 94
d6ff054a
BV
95 temp = parse_temperature(devc->packet + 1);
96 if (isnan(temp))
97 is_valid = FALSE;
98
99 if (is_valid) {
1db537c3 100 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
16544b38
UH
101 analog.meaning->mq = SR_MQ_TEMPERATURE;
102 analog.meaning->mqflags = 0;
4b319782 103 switch (devc->packet[5] - '0') {
d6ff054a 104 case 1:
16544b38 105 analog.meaning->unit = SR_UNIT_CELSIUS;
d6ff054a
BV
106 break;
107 case 2:
16544b38 108 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
d6ff054a
BV
109 break;
110 case 3:
16544b38 111 analog.meaning->unit = SR_UNIT_KELVIN;
d6ff054a
BV
112 break;
113 default:
114 /* We can still pass on the measurement, whatever it is. */
115 sr_dbg("Unknown unit 0x%.2x.", devc->packet[5]);
116 }
4b319782 117 switch (devc->packet[13] - '0') {
d6ff054a 118 case 0:
ba7dd8bb 119 /* Channel T1. */
16544b38 120 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 0));
d6ff054a
BV
121 break;
122 case 1:
ba7dd8bb 123 /* Channel T2. */
16544b38 124 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 1));
d6ff054a
BV
125 break;
126 case 2:
127 case 3:
ba7dd8bb 128 /* Channel T1-T2. */
16544b38
UH
129 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 2));
130 analog.meaning->mqflags |= SR_MQFLAG_RELATIVE;
d6ff054a
BV
131 break;
132 default:
ba7dd8bb 133 sr_err("Unknown channel 0x%.2x.", devc->packet[13]);
d6ff054a
BV
134 is_valid = FALSE;
135 }
136 if (is_valid) {
137 analog.num_samples = 1;
138 analog.data = &temp;
16544b38 139 packet.type = SR_DF_ANALOG;
d6ff054a 140 packet.payload = &analog;
695dc859 141 sr_session_send(sdi, &packet);
16544b38 142 g_slist_free(analog.meaning->channels);
d6ff054a
BV
143 }
144 }
145
9e111972
GS
146 /*
147 * We count packets even if the measurement was invalid. This way
148 * a sample limit on "Memory" data source still works: Unused
149 * memory slots come through as "----" measurements.
150 */
151 sr_sw_limits_update_samples_read(&devc->limits, 1);
152 if (sr_sw_limits_check(&devc->limits))
d2f7c417 153 sr_dev_acquisition_stop(sdi);
3877dde4 154}
6513f97f 155
55462b8b 156SR_PRIV void LIBUSB_CALL uni_t_ut32x_receive_transfer(struct libusb_transfer *transfer)
6513f97f 157{
d6ff054a
BV
158 struct dev_context *devc;
159 struct sr_dev_inst *sdi;
160 int hid_payload_len, ret;
161
162 sdi = transfer->user_data;
163 devc = sdi->priv;
164 if (transfer->actual_length == 8) {
165 /* CH9325 encodes length in low nibble of first byte, with
166 * bytes 1-7 being the (padded) payload. */
167 hid_payload_len = transfer->buffer[0] & 0x0f;
168 memcpy(devc->packet + devc->packet_len, transfer->buffer + 1,
169 hid_payload_len);
170 devc->packet_len += hid_payload_len;
171 if (devc->packet_len >= 2
4b319782
GS
172 && devc->packet[devc->packet_len - 2] == SEP[0]
173 && devc->packet[devc->packet_len - 1] == SEP[1]) {
d6ff054a 174 /* Got end of packet, but do we have a complete packet? */
4b319782 175 if (devc->packet_len == PACKET_SIZE)
d6ff054a
BV
176 process_packet(sdi);
177 /* Either way, done with it. */
178 devc->packet_len = 0;
4b319782 179 } else if (devc->packet_len > PACKET_SIZE) {
d6ff054a
BV
180 /* Guard against garbage from the device overrunning
181 * our packet buffer. */
182 sr_dbg("Buffer overrun!");
183 devc->packet_len = 0;
184 }
185 }
186
187 /* Get the next transfer (unless we're shutting down). */
188 if (sdi->status != SR_ST_STOPPING) {
189 if ((ret = libusb_submit_transfer(devc->xfer)) != 0) {
190 sr_dbg("Failed to resubmit transfer: %s", libusb_error_name(ret));
191 sdi->status = SR_ST_STOPPING;
192 libusb_free_transfer(devc->xfer);
193 }
194 } else
195 libusb_free_transfer(devc->xfer);
6513f97f
BV
196
197}
198
d6ff054a
BV
199SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data)
200{
201 struct drv_context *drvc;
202 struct dev_context *devc;
4f840ce9 203 struct sr_dev_driver *di;
d6ff054a 204 struct sr_dev_inst *sdi;
d6ff054a
BV
205 struct sr_usb_dev_inst *usb;
206 struct timeval tv;
6c60facc 207 int len, ret;
d6ff054a
BV
208 unsigned char cmd[2];
209
210 (void)fd;
211 (void)revents;
8bd3daa4 212
d6ff054a
BV
213 if (!(sdi = cb_data))
214 return TRUE;
215
4f840ce9 216 di = sdi->driver;
41812aca 217 drvc = di->context;
4f840ce9 218
d6ff054a
BV
219 if (!(devc = sdi->priv))
220 return TRUE;
221
222 memset(&tv, 0, sizeof(struct timeval));
223 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
224 NULL);
225
226 if (sdi->status == SR_ST_STOPPING) {
102f1239 227 usb_source_remove(sdi->session, drvc->sr_ctx);
bee2b016 228 std_session_send_df_end(sdi);
d6ff054a
BV
229
230 /* Tell the device to stop sending USB packets. */
231 usb = sdi->conn;
232 cmd[0] = 0x01;
233 cmd[1] = CMD_STOP;
234 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
235 if (ret != 0 || len != 2) {
236 /* Warning only, doesn't matter. */
237 sr_dbg("Failed to send stop command: %s", libusb_error_name(ret));
238 }
239
240 sdi->status = SR_ST_ACTIVE;
241 return TRUE;
242 }
243
244 return TRUE;
245}