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