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