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