]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org> | |
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 <string.h> | |
21 | #include <math.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | typedef enum { | |
25 | LIVE_DATA = 0x00, | |
26 | LOG_METADATA = 0x11, | |
27 | LOG_DATA = 0x14, | |
28 | LOG_START = 0x18, | |
29 | LOG_END = 0x19, | |
30 | } packet_type; | |
31 | ||
32 | static gboolean appa_55ii_checksum(const uint8_t *buf) | |
33 | { | |
34 | int i, size, checksum; | |
35 | ||
36 | size = buf[3] + 4; | |
37 | checksum = 0; | |
38 | for (i = 0; i < size; i++) | |
39 | checksum += buf[i]; | |
40 | ||
41 | return buf[size] == (checksum & 0xFF); | |
42 | } | |
43 | ||
44 | SR_PRIV gboolean appa_55ii_packet_valid(const uint8_t *buf) | |
45 | { | |
46 | if (buf[0] == 0x55 && buf[1] == 0x55 && buf[3] <= 32 | |
47 | && appa_55ii_checksum(buf)) | |
48 | return TRUE; | |
49 | ||
50 | return FALSE; | |
51 | } | |
52 | ||
53 | static uint64_t appa_55ii_flags(const uint8_t *buf) | |
54 | { | |
55 | uint8_t disp_mode; | |
56 | uint64_t flags; | |
57 | ||
58 | disp_mode = buf[4 + 13]; | |
59 | flags = 0; | |
60 | if ((disp_mode & 0xF0) == 0x20) | |
61 | flags |= SR_MQFLAG_HOLD; | |
62 | if ((disp_mode & 0x0C) == 0x04) | |
63 | flags |= SR_MQFLAG_MAX; | |
64 | if ((disp_mode & 0x0C) == 0x08) | |
65 | flags |= SR_MQFLAG_MIN; | |
66 | if ((disp_mode & 0x0C) == 0x0C) | |
67 | flags |= SR_MQFLAG_AVG; | |
68 | ||
69 | return flags; | |
70 | } | |
71 | ||
72 | static float appa_55ii_temp(const uint8_t *buf, int ch) | |
73 | { | |
74 | const uint8_t *ptr; | |
75 | int16_t temp; | |
76 | uint8_t flags; | |
77 | ||
78 | ptr = buf + 4 + 14 + 3 * ch; | |
79 | temp = RL16(ptr); | |
80 | flags = ptr[2]; | |
81 | ||
82 | if (flags & 0x60) | |
83 | return INFINITY; | |
84 | else if (flags & 1) | |
85 | return (float)temp / 10; | |
86 | else | |
87 | return (float)temp; | |
88 | } | |
89 | ||
90 | static void appa_55ii_live_data(struct sr_dev_inst *sdi, const uint8_t *buf) | |
91 | { | |
92 | struct dev_context *devc; | |
93 | struct sr_datafeed_packet packet; | |
94 | struct sr_datafeed_analog analog; | |
95 | struct sr_channel *ch; | |
96 | float values[APPA_55II_NUM_CHANNELS], *val_ptr; | |
97 | int i; | |
98 | ||
99 | devc = sdi->priv; | |
100 | ||
101 | if (devc->data_source != DATA_SOURCE_LIVE) | |
102 | return; | |
103 | ||
104 | val_ptr = values; | |
105 | memset(&analog, 0, sizeof(analog)); | |
106 | analog.num_samples = 1; | |
107 | analog.mq = SR_MQ_TEMPERATURE; | |
108 | analog.unit = SR_UNIT_CELSIUS; | |
109 | analog.mqflags = appa_55ii_flags(buf); | |
110 | analog.data = values; | |
111 | ||
112 | for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) { | |
113 | ch = g_slist_nth_data(sdi->channels, i); | |
114 | if (!ch->enabled) | |
115 | continue; | |
116 | analog.channels = g_slist_append(analog.channels, ch); | |
117 | *val_ptr++ = appa_55ii_temp(buf, i); | |
118 | } | |
119 | ||
120 | packet.type = SR_DF_ANALOG; | |
121 | packet.payload = &analog; | |
122 | sr_session_send(devc->session_cb_data, &packet); | |
123 | g_slist_free(analog.channels); | |
124 | ||
125 | devc->num_samples++; | |
126 | } | |
127 | ||
128 | static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf) | |
129 | { | |
130 | struct dev_context *devc; | |
131 | ||
132 | devc = sdi->priv; | |
133 | devc->num_log_records = (buf[5] << 8) + buf[4]; | |
134 | } | |
135 | ||
136 | static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi) | |
137 | { | |
138 | struct dev_context *devc; | |
139 | struct sr_datafeed_packet packet; | |
140 | struct sr_datafeed_analog analog; | |
141 | struct sr_channel *ch; | |
142 | float values[APPA_55II_NUM_CHANNELS], *val_ptr; | |
143 | const uint8_t *buf; | |
144 | int16_t temp; | |
145 | int offset, i; | |
146 | ||
147 | devc = sdi->priv; | |
148 | offset = 0; | |
149 | ||
150 | while (devc->log_buf_len >= 20 && devc->num_log_records > 0) { | |
151 | buf = devc->log_buf + offset; | |
152 | val_ptr = values; | |
153 | ||
154 | /* FIXME: Timestamp should be sent in the packet. */ | |
155 | sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]); | |
156 | ||
157 | memset(&analog, 0, sizeof(analog)); | |
158 | analog.num_samples = 1; | |
159 | analog.mq = SR_MQ_TEMPERATURE; | |
160 | analog.unit = SR_UNIT_CELSIUS; | |
161 | analog.data = values; | |
162 | ||
163 | for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) { | |
164 | temp = RL16(buf + 12 + 2 * i); | |
165 | ch = g_slist_nth_data(sdi->channels, i); | |
166 | if (!ch->enabled) | |
167 | continue; | |
168 | analog.channels = g_slist_append(analog.channels, ch); | |
169 | *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10; | |
170 | } | |
171 | ||
172 | packet.type = SR_DF_ANALOG; | |
173 | packet.payload = &analog; | |
174 | sr_session_send(devc->session_cb_data, &packet); | |
175 | g_slist_free(analog.channels); | |
176 | ||
177 | devc->num_samples++; | |
178 | devc->log_buf_len -= 20; | |
179 | offset += 20; | |
180 | devc->num_log_records--; | |
181 | } | |
182 | ||
183 | memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len); | |
184 | } | |
185 | ||
186 | static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf) | |
187 | { | |
188 | struct dev_context *devc; | |
189 | const uint8_t *ptr; | |
190 | unsigned int size; | |
191 | int s; | |
192 | ||
193 | devc = sdi->priv; | |
194 | if (devc->data_source != DATA_SOURCE_MEMORY) | |
195 | return; | |
196 | ||
197 | ptr = buf + 4; | |
198 | size = buf[3]; | |
199 | while (size > 0) { | |
200 | s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len); | |
201 | memcpy(devc->log_buf + devc->log_buf_len, ptr, s); | |
202 | devc->log_buf_len += s; | |
203 | size -= s; | |
204 | ptr += s; | |
205 | ||
206 | appa_55ii_log_data_parse(sdi); | |
207 | } | |
208 | } | |
209 | ||
210 | static void appa_55ii_log_end(struct sr_dev_inst *sdi) | |
211 | { | |
212 | struct dev_context *devc; | |
213 | ||
214 | devc = sdi->priv; | |
215 | if (devc->data_source != DATA_SOURCE_MEMORY) | |
216 | return; | |
217 | ||
218 | sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data); | |
219 | } | |
220 | ||
221 | static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi, | |
222 | const uint8_t *buf, int len) | |
223 | { | |
224 | if (len < 5) | |
225 | /* Need more data. */ | |
226 | return NULL; | |
227 | ||
228 | if (buf[0] != 0x55 || buf[1] != 0x55) | |
229 | /* Try to re-synchronize on a packet start. */ | |
230 | return buf + 1; | |
231 | ||
232 | if (len < 5 + buf[3]) | |
233 | /* Need more data. */ | |
234 | return NULL; | |
235 | ||
236 | if (!appa_55ii_checksum(buf)) | |
237 | /* Skip broken packet. */ | |
238 | return buf + 4 + buf[3] + 1; | |
239 | ||
240 | switch ((packet_type)buf[2]) { | |
241 | case LIVE_DATA: | |
242 | appa_55ii_live_data(sdi, buf); | |
243 | break; | |
244 | case LOG_METADATA: | |
245 | appa_55ii_log_metadata(sdi, buf); | |
246 | break; | |
247 | case LOG_DATA: | |
248 | appa_55ii_log_data(sdi, buf); | |
249 | break; | |
250 | case LOG_START: | |
251 | break; | |
252 | case LOG_END: | |
253 | appa_55ii_log_end(sdi); | |
254 | break; | |
255 | default: | |
256 | sr_warn("Invalid packet type: 0x%02x.", buf[2]); | |
257 | break; | |
258 | } | |
259 | ||
260 | return buf + 4 + buf[3] + 1; | |
261 | } | |
262 | ||
263 | SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data) | |
264 | { | |
265 | struct sr_dev_inst *sdi; | |
266 | struct dev_context *devc; | |
267 | struct sr_serial_dev_inst *serial; | |
268 | int64_t time; | |
269 | const uint8_t *ptr, *next_ptr, *end_ptr; | |
270 | int len; | |
271 | ||
272 | (void)fd; | |
273 | ||
274 | if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN) | |
275 | return TRUE; | |
276 | serial = sdi->conn; | |
277 | ||
278 | /* Try to get as much data as the buffer can hold. */ | |
279 | len = sizeof(devc->buf) - devc->buf_len; | |
280 | len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len); | |
281 | if (len < 1) { | |
282 | sr_err("Serial port read error: %d.", len); | |
283 | return FALSE; | |
284 | } | |
285 | devc->buf_len += len; | |
286 | ||
287 | /* Now look for packets in that data. */ | |
288 | ptr = devc->buf; | |
289 | end_ptr = ptr + devc->buf_len; | |
290 | while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr))) | |
291 | ptr = next_ptr; | |
292 | ||
293 | /* If we have any data left, move it to the beginning of our buffer. */ | |
294 | memmove(devc->buf, ptr, end_ptr - ptr); | |
295 | devc->buf_len -= ptr - devc->buf; | |
296 | ||
297 | /* If buffer is full and no valid packet was found, wipe buffer. */ | |
298 | if (devc->buf_len >= sizeof(devc->buf)) { | |
299 | devc->buf_len = 0; | |
300 | return FALSE; | |
301 | } | |
302 | ||
303 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
304 | sr_info("Requested number of samples reached."); | |
305 | sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data); | |
306 | return TRUE; | |
307 | } | |
308 | ||
309 | if (devc->limit_msec) { | |
310 | time = (g_get_monotonic_time() - devc->start_time) / 1000; | |
311 | if (time > (int64_t)devc->limit_msec) { | |
312 | sr_info("Requested time limit reached."); | |
313 | sdi->driver->dev_acquisition_stop(sdi, | |
314 | devc->session_cb_data); | |
315 | return TRUE; | |
316 | } | |
317 | } | |
318 | ||
319 | return TRUE; | |
320 | } |