2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 static float parse_temperature(unsigned char *buf)
33 for (i = 0; i < 4; i++) {
38 sr_dbg("Double negative sign!");
45 if (buf[i] < 0x30 || buf[i] > 0x39) {
46 sr_dbg("Invalid digit '%.2x'!", buf[i]);
50 temp += (buf[i] - 0x30);
59 static void process_packet(struct sr_dev_inst *sdi)
61 struct dev_context *devc;
62 struct sr_datafeed_packet packet;
63 struct sr_datafeed_analog_old analog;
70 sr_dbg("Received full 19-byte packet.");
71 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
72 spew = g_string_sized_new(60);
73 for (i = 0; i < devc->packet_len; i++)
74 g_string_append_printf(spew, "%.2x ", devc->packet[i]);
75 sr_spew("%s", spew->str);
76 g_string_free(spew, TRUE);
80 if (devc->packet[1] == 0x3b && devc->packet[2] == 0x3b
81 && devc->packet[3] == 0x3b && devc->packet[4] == 0x3b)
82 /* No measurement: missing channel, empty storage location, ... */
85 temp = parse_temperature(devc->packet + 1);
90 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
91 analog.mq = SR_MQ_TEMPERATURE;
93 switch (devc->packet[5] - 0x30) {
95 analog.unit = SR_UNIT_CELSIUS;
98 analog.unit = SR_UNIT_FAHRENHEIT;
101 analog.unit = SR_UNIT_KELVIN;
104 /* We can still pass on the measurement, whatever it is. */
105 sr_dbg("Unknown unit 0x%.2x.", devc->packet[5]);
107 switch (devc->packet[13] - 0x30) {
110 analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 0));
114 analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 1));
119 analog.channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 2));
120 analog.mqflags |= SR_MQFLAG_RELATIVE;
123 sr_err("Unknown channel 0x%.2x.", devc->packet[13]);
127 analog.num_samples = 1;
129 packet.type = SR_DF_ANALOG_OLD;
130 packet.payload = &analog;
131 sr_session_send(sdi, &packet);
132 g_slist_free(analog.channels);
136 /* We count packets even if the temperature was invalid. This way
137 * a sample limit on "Memory" data source still works: unused
138 * memory slots come through as "----" measurements. */
140 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
141 sdi->driver->dev_acquisition_stop(sdi);
146 SR_PRIV void LIBUSB_CALL uni_t_ut32x_receive_transfer(struct libusb_transfer *transfer)
148 struct dev_context *devc;
149 struct sr_dev_inst *sdi;
150 int hid_payload_len, ret;
152 sdi = transfer->user_data;
154 if (transfer->actual_length == 8) {
155 /* CH9325 encodes length in low nibble of first byte, with
156 * bytes 1-7 being the (padded) payload. */
157 hid_payload_len = transfer->buffer[0] & 0x0f;
158 memcpy(devc->packet + devc->packet_len, transfer->buffer + 1,
160 devc->packet_len += hid_payload_len;
161 if (devc->packet_len >= 2
162 && devc->packet[devc->packet_len - 2] == 0x0d
163 && devc->packet[devc->packet_len - 1] == 0x0a) {
164 /* Got end of packet, but do we have a complete packet? */
165 if (devc->packet_len == 19)
167 /* Either way, done with it. */
168 devc->packet_len = 0;
169 } else if (devc->packet_len > 19) {
170 /* Guard against garbage from the device overrunning
171 * our packet buffer. */
172 sr_dbg("Buffer overrun!");
173 devc->packet_len = 0;
177 /* Get the next transfer (unless we're shutting down). */
178 if (sdi->status != SR_ST_STOPPING) {
179 if ((ret = libusb_submit_transfer(devc->xfer)) != 0) {
180 sr_dbg("Failed to resubmit transfer: %s", libusb_error_name(ret));
181 sdi->status = SR_ST_STOPPING;
182 libusb_free_transfer(devc->xfer);
185 libusb_free_transfer(devc->xfer);
189 SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data)
191 struct drv_context *drvc;
192 struct dev_context *devc;
193 struct sr_dev_driver *di;
194 struct sr_dev_inst *sdi;
195 struct sr_usb_dev_inst *usb;
198 unsigned char cmd[2];
203 if (!(sdi = cb_data))
209 if (!(devc = sdi->priv))
212 memset(&tv, 0, sizeof(struct timeval));
213 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
216 if (sdi->status == SR_ST_STOPPING) {
217 usb_source_remove(sdi->session, drvc->sr_ctx);
218 std_session_send_df_end(sdi);
220 /* Tell the device to stop sending USB packets. */
224 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
225 if (ret != 0 || len != 2) {
226 /* Warning only, doesn't matter. */
227 sr_dbg("Failed to send stop command: %s", libusb_error_name(ret));
230 sdi->status = SR_ST_ACTIVE;