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/>.
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).
33 static float parse_temperature(unsigned char *buf)
41 for (i = 0; i < 4; i++) {
46 sr_dbg("Double negative sign!");
52 if (buf[i] < '0' || buf[i] > '9') {
53 sr_dbg("Invalid digit '%.2x'!", buf[i]);
66 static void process_packet(struct sr_dev_inst *sdi, uint8_t *pkt, size_t len)
68 struct dev_context *devc;
69 struct sr_datafeed_packet packet;
70 struct sr_datafeed_analog analog;
71 struct sr_analog_encoding encoding;
72 struct sr_analog_meaning meaning;
73 struct sr_analog_spec spec;
78 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
79 spew = sr_hexdump_new(pkt, len);
80 sr_spew("Got a packet, len %zu, bytes%s", len, spew->str);
81 sr_hexdump_free(spew);
83 if (len != PACKET_SIZE)
85 if (pkt[17] != SEP[0] || pkt[18] != SEP[1])
87 if (pkt[8] != '0' || pkt[16] != '1')
89 sr_dbg("Processing 19-byte packet.");
92 if (pkt[1] == NEG && pkt[2] == NEG && pkt[3] == NEG && pkt[4] == NEG)
93 /* No measurement: missing channel, empty storage location, ... */
96 temp = parse_temperature(&pkt[1]);
101 memset(&packet, 0, sizeof(packet));
102 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
103 analog.meaning->mq = SR_MQ_TEMPERATURE;
104 analog.meaning->mqflags = 0;
105 switch (pkt[5] - '0') {
107 analog.meaning->unit = SR_UNIT_CELSIUS;
110 analog.meaning->unit = SR_UNIT_FAHRENHEIT;
113 analog.meaning->unit = SR_UNIT_KELVIN;
116 /* We can still pass on the measurement, whatever it is. */
117 sr_dbg("Unknown unit 0x%.2x.", pkt[5]);
119 switch (pkt[13] - '0') {
122 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 0));
126 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 1));
131 analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 2));
132 analog.meaning->mqflags |= SR_MQFLAG_RELATIVE;
135 sr_err("Unknown channel 0x%.2x.", pkt[13]);
139 analog.num_samples = 1;
141 packet.type = SR_DF_ANALOG;
142 packet.payload = &analog;
143 sr_session_send(sdi, &packet);
144 g_slist_free(analog.meaning->channels);
149 * We count packets even if the measurement was invalid. This way
150 * a sample limit on "Memory" data source still works: Unused
151 * memory slots come through as "----" measurements.
154 sr_sw_limits_update_samples_read(&devc->limits, 1);
155 if (sr_sw_limits_check(&devc->limits))
156 sr_dev_acquisition_stop(sdi);
159 static int process_buffer(struct sr_dev_inst *sdi)
161 struct dev_context *devc;
166 * Specifically do not insist on finding the packet boundary at
167 * the end of the most recently received data chunk. Serial
168 * ports might involve hardware buffers (FIFO). We want to sync
169 * as fast as possible.
171 * Handle the synchronized situation first. Process complete
172 * packets that reside at the start of the buffer. Then fallback
173 * to incomplete or unaligned packets if the receive buffer
174 * still contains data bytes. (Depending on the bitrate and the
175 * poll interval, we may always end up in the manual search. But
176 * considering the update rate - two or three packets per second
177 * - this is not an issue.)
180 pkt = &devc->packet[0];
181 while (devc->packet_len >= PACKET_SIZE &&
182 pkt[PACKET_SIZE - 2] == SEP[0] &&
183 pkt[PACKET_SIZE - 1] == SEP[1]) {
184 process_packet(sdi, &pkt[0], PACKET_SIZE);
185 remain = devc->packet_len - PACKET_SIZE;
187 memmove(&pkt[0], &pkt[PACKET_SIZE], remain);
188 devc->packet_len -= PACKET_SIZE;
192 * The 'for' loop and the increment upon re-iteration after
193 * setting the loop var to zero is not an issue. The marker has
194 * two bytes, so effectively starting the search at offset 1 is
195 * fine for the specific packet layout.
197 for (idx = 0; idx < devc->packet_len; idx++) {
200 if (pkt[idx - 1] != SEP[0] || pkt[idx] != SEP[1])
202 /* Found a packet that spans up to and including 'idx'. */
204 process_packet(sdi, &pkt[0], idx);
205 remain = devc->packet_len - idx;
207 memmove(&pkt[0], &pkt[idx], remain);
208 devc->packet_len -= idx;
215 SR_PRIV void LIBUSB_CALL uni_t_ut32x_receive_transfer(struct libusb_transfer *transfer)
217 struct dev_context *devc;
218 struct sr_dev_inst *sdi;
219 int hid_payload_len, ret;
221 sdi = transfer->user_data;
223 if (transfer->actual_length == 8) {
224 /* CH9325 encodes length in low nibble of first byte, with
225 * bytes 1-7 being the (padded) payload. */
226 hid_payload_len = transfer->buffer[0] & 0x0f;
227 memcpy(devc->packet + devc->packet_len, transfer->buffer + 1,
229 devc->packet_len += hid_payload_len;
231 * Discard receive data when the buffer is exhausted. This shall
232 * allow to (re-)synchronize to the data stream when we find it
233 * in an arbitrary state. Check the receive buffer for packets.
235 if (devc->packet_len == sizeof(devc->packet)) {
236 process_packet(sdi, &devc->packet[0], devc->packet_len);
237 devc->packet_len = 0;
242 /* Get the next transfer (unless we're shutting down). */
243 if (sdi->status != SR_ST_STOPPING) {
244 if ((ret = libusb_submit_transfer(devc->xfer)) != 0) {
245 sr_dbg("Failed to resubmit transfer: %s", libusb_error_name(ret));
246 sdi->status = SR_ST_STOPPING;
247 libusb_free_transfer(devc->xfer);
250 libusb_free_transfer(devc->xfer);
254 SR_PRIV int uni_t_ut32x_handle_events(int fd, int revents, void *cb_data)
256 struct drv_context *drvc;
257 struct dev_context *devc;
258 struct sr_dev_driver *di;
259 struct sr_dev_inst *sdi;
260 struct sr_usb_dev_inst *usb;
263 unsigned char cmd[2];
268 if (!(sdi = cb_data))
274 if (!(devc = sdi->priv))
277 memset(&tv, 0, sizeof(struct timeval));
278 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
281 if (sdi->status == SR_ST_STOPPING) {
282 usb_source_remove(sdi->session, drvc->sr_ctx);
283 std_session_send_df_end(sdi);
285 /* Tell the device to stop sending USB packets. */
289 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
290 if (ret != 0 || len != 2) {
291 /* Warning only, doesn't matter. */
292 sr_dbg("Failed to send stop command: %s", libusb_error_name(ret));
295 sdi->status = SR_ST_ACTIVE;