]> sigrok.org Git - libsigrok.git/blob - src/hardware/uni-t-ut32x/protocol.c
dev_acquisition_{start,stop}(): Drop duplicate 'cb_data' parameter.
[libsigrok.git] / src / hardware / uni-t-ut32x / protocol.c
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
20 #include <config.h>
21 #include <string.h>
22 #include <math.h>
23 #include "protocol.h"
24
25 extern struct sr_dev_driver uni_t_ut32x_driver_info;
26
27 static float parse_temperature(unsigned char *buf)
28 {
29         float temp;
30         int i;
31         gboolean negative;
32
33         negative = FALSE;
34         temp = 0.0;
35         for (i = 0; i < 4; i++) {
36                 if (buf[i] == 0x3a)
37                         continue;
38                 if (buf[i] == 0x3b) {
39                         if (negative) {
40                                 sr_dbg("Double negative sign!");
41                                 return NAN;
42                         } else {
43                                 negative = TRUE;
44                                 continue;
45                         }
46                 }
47                 if (buf[i] < 0x30 || buf[i] > 0x39) {
48                         sr_dbg("Invalid digit '%.2x'!", buf[i]);
49                         return NAN;
50                 }
51                 temp *= 10;
52                 temp += (buf[i] - 0x30);
53         }
54         temp /= 10;
55         if (negative)
56                 temp = -temp;
57
58         return temp;
59 }
60
61 static void process_packet(struct sr_dev_inst *sdi)
62 {
63         struct dev_context *devc;
64         struct sr_datafeed_packet packet;
65         struct sr_datafeed_analog_old analog;
66         GString *spew;
67         float temp;
68         int i;
69         gboolean is_valid;
70
71         devc = sdi->priv;
72         sr_dbg("Received full 19-byte packet.");
73         if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
74                 spew = g_string_sized_new(60);
75                 for (i = 0; i < devc->packet_len; i++)
76                         g_string_append_printf(spew, "%.2x ", devc->packet[i]);
77                 sr_spew("%s", spew->str);
78                 g_string_free(spew, TRUE);
79         }
80
81         is_valid = TRUE;
82         if (devc->packet[1] == 0x3b && devc->packet[2] == 0x3b
83                         && devc->packet[3] == 0x3b && devc->packet[4] == 0x3b)
84                 /* No measurement: missing channel, empty storage location, ... */
85                 is_valid = FALSE;
86
87         temp = parse_temperature(devc->packet + 1);
88         if (isnan(temp))
89                 is_valid = FALSE;
90
91         if (is_valid) {
92                 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
93                 analog.mq = SR_MQ_TEMPERATURE;
94                 analog.mqflags = 0;
95                 switch (devc->packet[5] - 0x30) {
96                 case 1:
97                         analog.unit = SR_UNIT_CELSIUS;
98                         break;
99                 case 2:
100                         analog.unit = SR_UNIT_FAHRENHEIT;
101                         break;
102                 case 3:
103                         analog.unit = SR_UNIT_KELVIN;
104                         break;
105                 default:
106                         /* We can still pass on the measurement, whatever it is. */
107                         sr_dbg("Unknown unit 0x%.2x.", devc->packet[5]);
108                 }
109                 switch (devc->packet[13] - 0x30) {
110                 case 0:
111                         /* Channel T1. */
112                         analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 0));
113                         break;
114                 case 1:
115                         /* Channel T2. */
116                         analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 1));
117                         break;
118                 case 2:
119                 case 3:
120                         /* Channel T1-T2. */
121                         analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 2));
122                         analog.mqflags |= SR_MQFLAG_RELATIVE;
123                         break;
124                 default:
125                         sr_err("Unknown channel 0x%.2x.", devc->packet[13]);
126                         is_valid = FALSE;
127                 }
128                 if (is_valid) {
129                         analog.num_samples = 1;
130                         analog.data = &temp;
131                         packet.type = SR_DF_ANALOG_OLD;
132                         packet.payload = &analog;
133                         sr_session_send(sdi, &packet);
134                         g_slist_free(analog.channels);
135                 }
136         }
137
138         /* We count packets even if the temperature was invalid. This way
139          * a sample limit on "Memory" data source still works: unused
140          * memory slots come through as "----" measurements. */
141         devc->num_samples++;
142         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
143                 sdi->driver->dev_acquisition_stop(sdi);
144         }
145
146 }
147
148 SR_PRIV void LIBUSB_CALL uni_t_ut32x_receive_transfer(struct libusb_transfer *transfer)
149 {
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);
188
189 }
190
191 SR_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;
195         struct sr_dev_driver *di;
196         struct sr_dev_inst *sdi;
197         struct sr_usb_dev_inst *usb;
198         struct timeval tv;
199         int len, ret;
200         unsigned char cmd[2];
201
202         (void)fd;
203         (void)revents;
204
205         if (!(sdi = cb_data))
206                 return TRUE;
207
208         di = sdi->driver;
209         drvc = di->context;
210
211         if (!(devc = sdi->priv))
212                 return TRUE;
213
214         memset(&tv, 0, sizeof(struct timeval));
215         libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
216                         NULL);
217
218         if (sdi->status == SR_ST_STOPPING) {
219                 usb_source_remove(sdi->session, drvc->sr_ctx);
220                 std_session_send_df_header(sdi, LOG_PREFIX);
221
222                 /* Tell the device to stop sending USB packets. */
223                 usb = sdi->conn;
224                 cmd[0] = 0x01;
225                 cmd[1] = CMD_STOP;
226                 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
227                 if (ret != 0 || len != 2) {
228                         /* Warning only, doesn't matter. */
229                         sr_dbg("Failed to send stop command: %s", libusb_error_name(ret));
230                 }
231
232                 sdi->status = SR_ST_ACTIVE;
233                 return TRUE;
234         }
235
236         return TRUE;
237 }