]> sigrok.org Git - libsigrok.git/blame - hardware/appa-55ii/protocol.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / appa-55ii / protocol.c
CommitLineData
5e7a8e57
AJ
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
81a9ab72
AJ
20#include <string.h>
21#include <math.h>
5e7a8e57
AJ
22#include "protocol.h"
23
81a9ab72
AJ
24typedef enum {
25 LIVE_DATA = 0x00,
26 LOG_METADATA = 0x11,
27 LOG_DATA = 0x14,
28 LOG_START = 0x18,
29 LOG_END = 0x19,
9ee78f23 30} packet_type;
81a9ab72
AJ
31
32static gboolean appa_55ii_checksum(const uint8_t *buf)
33{
7574e58c
BV
34 int i, size, checksum;
35
36 size = buf[3] + 4;
37 checksum = 0;
38 for (i = 0; i < size; i++)
81a9ab72 39 checksum += buf[i];
7574e58c 40
81a9ab72
AJ
41 return buf[size] == (checksum & 0xFF);
42}
43
44SR_PRIV gboolean appa_55ii_packet_valid(const uint8_t *buf)
45{
46 if (buf[0] == 0x55 && buf[1] == 0x55 && buf[3] <= 32
7574e58c 47 && appa_55ii_checksum(buf))
81a9ab72 48 return TRUE;
7574e58c 49
81a9ab72
AJ
50 return FALSE;
51}
52
53static uint64_t appa_55ii_flags(const uint8_t *buf)
54{
7574e58c
BV
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;
81a9ab72
AJ
68
69 return flags;
70}
71
ba7dd8bb 72static float appa_55ii_temp(const uint8_t *buf, int ch)
81a9ab72 73{
7574e58c
BV
74 const uint8_t *ptr;
75 int16_t temp;
76 uint8_t flags;
77
ba7dd8bb 78 ptr = buf + 4 + 14 + 3 * ch;
7574e58c
BV
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;
81a9ab72
AJ
88}
89
90static void appa_55ii_live_data(struct sr_dev_inst *sdi, const uint8_t *buf)
91{
76b4d4f4 92 struct dev_context *devc;
81a9ab72 93 struct sr_datafeed_packet packet;
7574e58c 94 struct sr_datafeed_analog analog;
ba7dd8bb 95 struct sr_channel *ch;
7574e58c 96 float values[APPA_55II_NUM_PROBES], *val_ptr;
81a9ab72
AJ
97 int i;
98
76b4d4f4
UH
99 devc = sdi->priv;
100
81a9ab72
AJ
101 if (devc->data_source != DATA_SOURCE_LIVE)
102 return;
103
7574e58c
BV
104 val_ptr = values;
105 memset(&analog, 0, sizeof(analog));
81a9ab72
AJ
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
7574e58c 112 for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
ba7dd8bb
UH
113 ch = g_slist_nth_data(sdi->channels, i);
114 if (!ch->enabled)
81a9ab72 115 continue;
ba7dd8bb 116 analog.channels = g_slist_append(analog.channels, ch);
81a9ab72
AJ
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);
ba7dd8bb 123 g_slist_free(analog.channels);
81a9ab72
AJ
124
125 devc->num_samples++;
126}
127
128static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
129{
7574e58c
BV
130 struct dev_context *devc;
131
132 devc = sdi->priv;
81a9ab72
AJ
133 devc->num_log_records = (buf[5] << 8) + buf[4];
134}
135
136static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
137{
7574e58c
BV
138 struct dev_context *devc;
139 struct sr_datafeed_packet packet;
76b4d4f4 140 struct sr_datafeed_analog analog;
ba7dd8bb 141 struct sr_channel *ch;
7574e58c
BV
142 float values[APPA_55II_NUM_PROBES], *val_ptr;
143 const uint8_t *buf;
144 int16_t temp;
145 int offset, i;
146
147 devc = sdi->priv;
148 offset = 0;
81a9ab72
AJ
149
150 while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
7574e58c
BV
151 buf = devc->log_buf + offset;
152 val_ptr = values;
81a9ab72 153
76b4d4f4 154 /* FIXME: Timestamp should be sent in the packet. */
81a9ab72
AJ
155 sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
156
7574e58c 157 memset(&analog, 0, sizeof(analog));
81a9ab72
AJ
158 analog.num_samples = 1;
159 analog.mq = SR_MQ_TEMPERATURE;
160 analog.unit = SR_UNIT_CELSIUS;
161 analog.data = values;
162
7574e58c
BV
163 for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
164 temp = RL16(buf + 12 + 2 * i);
ba7dd8bb
UH
165 ch = g_slist_nth_data(sdi->channels, i);
166 if (!ch->enabled)
81a9ab72 167 continue;
ba7dd8bb 168 analog.channels = g_slist_append(analog.channels, ch);
81a9ab72
AJ
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);
ba7dd8bb 175 g_slist_free(analog.channels);
81a9ab72
AJ
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
186static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
187{
7574e58c
BV
188 struct dev_context *devc;
189 const uint8_t *ptr;
190 unsigned int size;
191 int s;
81a9ab72 192
7574e58c 193 devc = sdi->priv;
81a9ab72
AJ
194 if (devc->data_source != DATA_SOURCE_MEMORY)
195 return;
196
76b4d4f4 197 ptr = buf + 4;
7574e58c 198 size = buf[3];
81a9ab72 199 while (size > 0) {
7574e58c 200 s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
81a9ab72
AJ
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
210static void appa_55ii_log_end(struct sr_dev_inst *sdi)
211{
7574e58c 212 struct dev_context *devc;
81a9ab72 213
7574e58c 214 devc = sdi->priv;
81a9ab72
AJ
215 if (devc->data_source != DATA_SOURCE_MEMORY)
216 return;
217
218 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
219}
220
221static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
7574e58c 222 const uint8_t *buf, int len)
81a9ab72
AJ
223{
224 if (len < 5)
7574e58c
BV
225 /* Need more data. */
226 return NULL;
81a9ab72
AJ
227
228 if (buf[0] != 0x55 || buf[1] != 0x55)
7574e58c
BV
229 /* Try to re-synchronize on a packet start. */
230 return buf + 1;
81a9ab72 231
7574e58c
BV
232 if (len < 5 + buf[3])
233 /* Need more data. */
234 return NULL;
81a9ab72
AJ
235
236 if (!appa_55ii_checksum(buf))
7574e58c
BV
237 /* Skip broken packet. */
238 return buf + 4 + buf[3] + 1;
81a9ab72 239
9ee78f23 240 switch ((packet_type)buf[2]) {
7574e58c
BV
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;
76b4d4f4
UH
255 default:
256 sr_warn("Invalid packet type: 0x%02x.", buf[2]);
257 break;
81a9ab72
AJ
258 }
259
260 return buf + 4 + buf[3] + 1;
261}
262
5e7a8e57
AJ
263SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
264{
81a9ab72 265 struct sr_dev_inst *sdi;
5e7a8e57 266 struct dev_context *devc;
81a9ab72 267 struct sr_serial_dev_inst *serial;
7574e58c 268 int64_t time;
81a9ab72
AJ
269 const uint8_t *ptr, *next_ptr, *end_ptr;
270 int len;
5e7a8e57
AJ
271
272 (void)fd;
273
81a9ab72 274 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
5e7a8e57 275 return TRUE;
81a9ab72
AJ
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(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;
5e7a8e57 292
81a9ab72
AJ
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);
5e7a8e57 306 return TRUE;
81a9ab72 307 }
5e7a8e57 308
81a9ab72 309 if (devc->limit_msec) {
7574e58c 310 time = (g_get_monotonic_time() - devc->start_time) / 1000;
81a9ab72
AJ
311 if (time > (int64_t)devc->limit_msec) {
312 sr_info("Requested time limit reached.");
76b4d4f4
UH
313 sdi->driver->dev_acquisition_stop(sdi,
314 devc->session_cb_data);
81a9ab72
AJ
315 return TRUE;
316 }
5e7a8e57
AJ
317 }
318
319 return TRUE;
320}