]>
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 | |
4b319782 GS |
25 | #define SEP "\r\n" |
26 | #define BLANK ':' | |
27 | #define NEG ';' | |
28 | ||
29 | /* | |
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). | |
32 | */ | |
d6ff054a | 33 | static float parse_temperature(unsigned char *buf) |
3877dde4 | 34 | { |
d6ff054a BV |
35 | float temp; |
36 | int i; | |
37 | gboolean negative; | |
38 | ||
39 | negative = FALSE; | |
40 | temp = 0.0; | |
41 | for (i = 0; i < 4; i++) { | |
4b319782 | 42 | if (buf[i] == BLANK) |
d6ff054a | 43 | continue; |
4b319782 | 44 | if (buf[i] == NEG) { |
d6ff054a BV |
45 | if (negative) { |
46 | sr_dbg("Double negative sign!"); | |
47 | return NAN; | |
d6ff054a | 48 | } |
4b319782 GS |
49 | negative = TRUE; |
50 | continue; | |
d6ff054a | 51 | } |
4b319782 | 52 | if (buf[i] < '0' || buf[i] > '9') { |
d6ff054a BV |
53 | sr_dbg("Invalid digit '%.2x'!", buf[i]); |
54 | return NAN; | |
55 | } | |
56 | temp *= 10; | |
4b319782 | 57 | temp += buf[i] - '0'; |
d6ff054a BV |
58 | } |
59 | temp /= 10; | |
60 | if (negative) | |
61 | temp = -temp; | |
3877dde4 | 62 | |
d6ff054a BV |
63 | return temp; |
64 | } | |
65 | ||
a0ba75bd | 66 | static void process_packet(struct sr_dev_inst *sdi, uint8_t *pkt, size_t len) |
d6ff054a | 67 | { |
3877dde4 | 68 | struct dev_context *devc; |
d6ff054a | 69 | struct sr_datafeed_packet packet; |
16544b38 UH |
70 | struct sr_datafeed_analog analog; |
71 | struct sr_analog_encoding encoding; | |
72 | struct sr_analog_meaning meaning; | |
73 | struct sr_analog_spec spec; | |
d6ff054a BV |
74 | GString *spew; |
75 | float temp; | |
d6ff054a | 76 | gboolean is_valid; |
3877dde4 | 77 | |
d6ff054a | 78 | if (sr_log_loglevel_get() >= SR_LOG_SPEW) { |
a0ba75bd GS |
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); | |
d6ff054a | 82 | } |
a0ba75bd GS |
83 | if (len != PACKET_SIZE) |
84 | return; | |
85 | if (pkt[17] != SEP[0] || pkt[18] != SEP[1]) | |
86 | return; | |
87 | if (pkt[8] != '0' || pkt[16] != '1') | |
88 | return; | |
89 | sr_dbg("Processing 19-byte packet."); | |
3877dde4 | 90 | |
d6ff054a | 91 | is_valid = TRUE; |
a0ba75bd | 92 | if (pkt[1] == NEG && pkt[2] == NEG && pkt[3] == NEG && pkt[4] == NEG) |
ba7dd8bb | 93 | /* No measurement: missing channel, empty storage location, ... */ |
d6ff054a | 94 | is_valid = FALSE; |
3877dde4 | 95 | |
a0ba75bd | 96 | temp = parse_temperature(&pkt[1]); |
d6ff054a BV |
97 | if (isnan(temp)) |
98 | is_valid = FALSE; | |
99 | ||
100 | if (is_valid) { | |
a0ba75bd | 101 | memset(&packet, 0, sizeof(packet)); |
1db537c3 | 102 | sr_analog_init(&analog, &encoding, &meaning, &spec, 1); |
16544b38 UH |
103 | analog.meaning->mq = SR_MQ_TEMPERATURE; |
104 | analog.meaning->mqflags = 0; | |
a0ba75bd | 105 | switch (pkt[5] - '0') { |
d6ff054a | 106 | case 1: |
16544b38 | 107 | analog.meaning->unit = SR_UNIT_CELSIUS; |
d6ff054a BV |
108 | break; |
109 | case 2: | |
16544b38 | 110 | analog.meaning->unit = SR_UNIT_FAHRENHEIT; |
d6ff054a BV |
111 | break; |
112 | case 3: | |
16544b38 | 113 | analog.meaning->unit = SR_UNIT_KELVIN; |
d6ff054a BV |
114 | break; |
115 | default: | |
116 | /* We can still pass on the measurement, whatever it is. */ | |
a0ba75bd | 117 | sr_dbg("Unknown unit 0x%.2x.", pkt[5]); |
d6ff054a | 118 | } |
a0ba75bd | 119 | switch (pkt[13] - '0') { |
d6ff054a | 120 | case 0: |
ba7dd8bb | 121 | /* Channel T1. */ |
16544b38 | 122 | analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 0)); |
d6ff054a BV |
123 | break; |
124 | case 1: | |
ba7dd8bb | 125 | /* Channel T2. */ |
16544b38 | 126 | analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 1)); |
d6ff054a BV |
127 | break; |
128 | case 2: | |
129 | case 3: | |
ba7dd8bb | 130 | /* Channel T1-T2. */ |
16544b38 UH |
131 | analog.meaning->channels = g_slist_append(NULL, g_slist_nth_data(sdi->channels, 2)); |
132 | analog.meaning->mqflags |= SR_MQFLAG_RELATIVE; | |
d6ff054a BV |
133 | break; |
134 | default: | |
a0ba75bd | 135 | sr_err("Unknown channel 0x%.2x.", pkt[13]); |
d6ff054a BV |
136 | is_valid = FALSE; |
137 | } | |
138 | if (is_valid) { | |
139 | analog.num_samples = 1; | |
140 | analog.data = &temp; | |
16544b38 | 141 | packet.type = SR_DF_ANALOG; |
d6ff054a | 142 | packet.payload = &analog; |
695dc859 | 143 | sr_session_send(sdi, &packet); |
16544b38 | 144 | g_slist_free(analog.meaning->channels); |
d6ff054a BV |
145 | } |
146 | } | |
147 | ||
9e111972 GS |
148 | /* |
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. | |
152 | */ | |
a0ba75bd | 153 | devc = sdi->priv; |
9e111972 GS |
154 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
155 | if (sr_sw_limits_check(&devc->limits)) | |
d2f7c417 | 156 | sr_dev_acquisition_stop(sdi); |
3877dde4 | 157 | } |
6513f97f | 158 | |
f0e6b41f GS |
159 | static int process_buffer(struct sr_dev_inst *sdi) |
160 | { | |
161 | struct dev_context *devc; | |
162 | uint8_t *pkt; | |
163 | size_t remain, idx; | |
164 | ||
165 | /* | |
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. | |
170 | * | |
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.) | |
178 | */ | |
179 | devc = sdi->priv; | |
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; | |
186 | if (remain) | |
187 | memmove(&pkt[0], &pkt[PACKET_SIZE], remain); | |
188 | devc->packet_len -= PACKET_SIZE; | |
189 | } | |
190 | ||
191 | /* | |
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. | |
196 | */ | |
197 | for (idx = 0; idx < devc->packet_len; idx++) { | |
198 | if (idx < 1) | |
199 | continue; | |
200 | if (pkt[idx - 1] != SEP[0] || pkt[idx] != SEP[1]) | |
201 | continue; | |
202 | /* Found a packet that spans up to and including 'idx'. */ | |
203 | idx++; | |
204 | process_packet(sdi, &pkt[0], idx); | |
205 | remain = devc->packet_len - idx; | |
206 | if (remain) | |
207 | memmove(&pkt[0], &pkt[idx], remain); | |
208 | devc->packet_len -= idx; | |
209 | idx = 0; | |
210 | } | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
164c5ae5 GS |
215 | /* Gets invoked when RX data is available. */ |
216 | static int ut32x_receive_data(struct sr_dev_inst *sdi) | |
6513f97f | 217 | { |
d6ff054a | 218 | struct dev_context *devc; |
164c5ae5 GS |
219 | struct sr_serial_dev_inst *serial; |
220 | size_t len; | |
d6ff054a | 221 | |
d6ff054a | 222 | devc = sdi->priv; |
164c5ae5 GS |
223 | serial = sdi->conn; |
224 | ||
225 | /* | |
226 | * Discard receive data when the buffer is exhausted. This shall | |
227 | * allow to (re-)synchronize to the data stream when we find it | |
228 | * in an arbitrary state. Drain more data from the serial port, | |
229 | * and check the receive buffer for packets. | |
230 | */ | |
231 | if (devc->packet_len == sizeof(devc->packet)) { | |
232 | process_packet(sdi, &devc->packet[0], devc->packet_len); | |
233 | devc->packet_len = 0; | |
d6ff054a | 234 | } |
164c5ae5 GS |
235 | len = sizeof(devc->packet) - devc->packet_len; |
236 | len = serial_read_nonblocking(serial, | |
237 | &devc->packet[devc->packet_len], len); | |
238 | if (!len) | |
239 | return 0; | |
d6ff054a | 240 | |
164c5ae5 GS |
241 | devc->packet_len += len; |
242 | process_buffer(sdi); | |
6513f97f | 243 | |
164c5ae5 | 244 | return 0; |
6513f97f BV |
245 | } |
246 | ||
164c5ae5 GS |
247 | /* Gets periodically invoked by the glib main loop. */ |
248 | SR_PRIV int ut32x_handle_events(int fd, int revents, void *cb_data) | |
d6ff054a | 249 | { |
d6ff054a | 250 | struct sr_dev_inst *sdi; |
164c5ae5 GS |
251 | struct sr_serial_dev_inst *serial; |
252 | uint8_t cmd; | |
d6ff054a BV |
253 | |
254 | (void)fd; | |
8bd3daa4 | 255 | |
164c5ae5 GS |
256 | sdi = cb_data; |
257 | if (!sdi) | |
d6ff054a | 258 | return TRUE; |
164c5ae5 GS |
259 | serial = sdi->conn; |
260 | if (!serial) | |
d6ff054a BV |
261 | return TRUE; |
262 | ||
164c5ae5 GS |
263 | if (revents & G_IO_IN) |
264 | ut32x_receive_data(sdi); | |
d6ff054a BV |
265 | |
266 | if (sdi->status == SR_ST_STOPPING) { | |
164c5ae5 | 267 | serial_source_remove(sdi->session, serial); |
bee2b016 | 268 | std_session_send_df_end(sdi); |
d6ff054a | 269 | sdi->status = SR_ST_ACTIVE; |
164c5ae5 GS |
270 | |
271 | /* Tell the device to stop sending data. */ | |
272 | cmd = CMD_STOP; | |
273 | serial_write_blocking(serial, &cmd, sizeof(cmd), 0); | |
d6ff054a BV |
274 | } |
275 | ||
276 | return TRUE; | |
277 | } |