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