]> sigrok.org Git - libsigrok.git/blame - hardware/appa-55ii/protocol.c
gmc-mh-1x-2x: Use standard API callback names for now.
[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,
30} PacketType;
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
72static float appa_55ii_temp(const uint8_t *buf, int probe)
73{
7574e58c
BV
74 const uint8_t *ptr;
75 int16_t temp;
76 uint8_t flags;
77
78 ptr = buf + 4 + 14 + 3 * probe;
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{
92 struct dev_context *devc = sdi->priv;
93 struct sr_datafeed_packet packet;
7574e58c
BV
94 struct sr_datafeed_analog analog;
95 struct sr_probe *probe;
96 float values[APPA_55II_NUM_PROBES], *val_ptr;
81a9ab72
AJ
97 int i;
98
99 if (devc->data_source != DATA_SOURCE_LIVE)
100 return;
101
7574e58c
BV
102 val_ptr = values;
103 memset(&analog, 0, sizeof(analog));
81a9ab72
AJ
104 analog.num_samples = 1;
105 analog.mq = SR_MQ_TEMPERATURE;
106 analog.unit = SR_UNIT_CELSIUS;
107 analog.mqflags = appa_55ii_flags(buf);
108 analog.data = values;
109
7574e58c
BV
110 for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
111 probe = g_slist_nth_data(sdi->probes, i);
81a9ab72
AJ
112 if (!probe->enabled)
113 continue;
114 analog.probes = g_slist_append(analog.probes, probe);
115 *val_ptr++ = appa_55ii_temp(buf, i);
116 }
117
118 packet.type = SR_DF_ANALOG;
119 packet.payload = &analog;
120 sr_session_send(devc->session_cb_data, &packet);
121 g_slist_free(analog.probes);
122
123 devc->num_samples++;
124}
125
126static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
127{
7574e58c
BV
128 struct dev_context *devc;
129
130 devc = sdi->priv;
81a9ab72
AJ
131 devc->num_log_records = (buf[5] << 8) + buf[4];
132}
133
134static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
135{
7574e58c
BV
136 struct dev_context *devc;
137 struct sr_datafeed_packet packet;
138 struct sr_datafeed_analog analog = { 0 };
139 struct sr_probe *probe;
140 float values[APPA_55II_NUM_PROBES], *val_ptr;
141 const uint8_t *buf;
142 int16_t temp;
143 int offset, i;
144
145 devc = sdi->priv;
146 offset = 0;
81a9ab72
AJ
147
148 while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
7574e58c
BV
149 buf = devc->log_buf + offset;
150 val_ptr = values;
81a9ab72
AJ
151
152 /* FIXME: timestamp should be sent in the packet */
153 sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
154
7574e58c 155 memset(&analog, 0, sizeof(analog));
81a9ab72
AJ
156 analog.num_samples = 1;
157 analog.mq = SR_MQ_TEMPERATURE;
158 analog.unit = SR_UNIT_CELSIUS;
159 analog.data = values;
160
7574e58c
BV
161 for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
162 temp = RL16(buf + 12 + 2 * i);
163 probe = g_slist_nth_data(sdi->probes, i);
81a9ab72
AJ
164 if (!probe->enabled)
165 continue;
166 analog.probes = g_slist_append(analog.probes, probe);
167 *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10;
168 }
169
170 packet.type = SR_DF_ANALOG;
171 packet.payload = &analog;
172 sr_session_send(devc->session_cb_data, &packet);
173 g_slist_free(analog.probes);
174
175 devc->num_samples++;
176 devc->log_buf_len -= 20;
177 offset += 20;
178 devc->num_log_records--;
179 }
180
181 memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len);
182}
183
184static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
185{
7574e58c
BV
186 struct dev_context *devc;
187 const uint8_t *ptr;
188 unsigned int size;
189 int s;
81a9ab72 190
7574e58c 191 devc = sdi->priv;
81a9ab72
AJ
192 if (devc->data_source != DATA_SOURCE_MEMORY)
193 return;
194
7574e58c
BV
195 ptr = buf + 4;;
196 size = buf[3];
81a9ab72 197 while (size > 0) {
7574e58c 198 s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
81a9ab72
AJ
199 memcpy(devc->log_buf + devc->log_buf_len, ptr, s);
200 devc->log_buf_len += s;
201 size -= s;
202 ptr += s;
203
204 appa_55ii_log_data_parse(sdi);
205 }
206}
207
208static void appa_55ii_log_end(struct sr_dev_inst *sdi)
209{
7574e58c 210 struct dev_context *devc;
81a9ab72 211
7574e58c 212 devc = sdi->priv;
81a9ab72
AJ
213 if (devc->data_source != DATA_SOURCE_MEMORY)
214 return;
215
216 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
217}
218
219static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
7574e58c 220 const uint8_t *buf, int len)
81a9ab72
AJ
221{
222 if (len < 5)
7574e58c
BV
223 /* Need more data. */
224 return NULL;
81a9ab72
AJ
225
226 if (buf[0] != 0x55 || buf[1] != 0x55)
7574e58c
BV
227 /* Try to re-synchronize on a packet start. */
228 return buf + 1;
81a9ab72 229
7574e58c
BV
230 if (len < 5 + buf[3])
231 /* Need more data. */
232 return NULL;
81a9ab72
AJ
233
234 if (!appa_55ii_checksum(buf))
7574e58c
BV
235 /* Skip broken packet. */
236 return buf + 4 + buf[3] + 1;
81a9ab72
AJ
237
238 switch ((PacketType) buf[2]) {
7574e58c
BV
239 case LIVE_DATA:
240 appa_55ii_live_data(sdi, buf);
241 break;
242 case LOG_METADATA:
243 appa_55ii_log_metadata(sdi, buf);
244 break;
245 case LOG_DATA:
246 appa_55ii_log_data(sdi, buf);
247 break;
248 case LOG_START:
249 break;
250 case LOG_END:
251 appa_55ii_log_end(sdi);
252 break;
81a9ab72
AJ
253 }
254
255 return buf + 4 + buf[3] + 1;
256}
257
5e7a8e57
AJ
258SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
259{
81a9ab72 260 struct sr_dev_inst *sdi;
5e7a8e57 261 struct dev_context *devc;
81a9ab72 262 struct sr_serial_dev_inst *serial;
7574e58c 263 int64_t time;
81a9ab72
AJ
264 const uint8_t *ptr, *next_ptr, *end_ptr;
265 int len;
5e7a8e57
AJ
266
267 (void)fd;
268
81a9ab72 269 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
5e7a8e57 270 return TRUE;
81a9ab72
AJ
271 serial = sdi->conn;
272
273 /* Try to get as much data as the buffer can hold. */
274 len = sizeof(devc->buf) - devc->buf_len;
275 len = serial_read(serial, devc->buf + devc->buf_len, len);
276 if (len < 1) {
277 sr_err("Serial port read error: %d.", len);
278 return FALSE;
279 }
280 devc->buf_len += len;
281
282 /* Now look for packets in that data. */
283 ptr = devc->buf;
284 end_ptr = ptr + devc->buf_len;
285 while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr)))
286 ptr = next_ptr;
5e7a8e57 287
81a9ab72
AJ
288 /* If we have any data left, move it to the beginning of our buffer. */
289 memmove(devc->buf, ptr, end_ptr - ptr);
290 devc->buf_len -= ptr - devc->buf;
291
292 /* If buffer is full and no valid packet was found, wipe buffer. */
293 if (devc->buf_len >= sizeof(devc->buf)) {
294 devc->buf_len = 0;
295 return FALSE;
296 }
297
298 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
299 sr_info("Requested number of samples reached.");
300 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
5e7a8e57 301 return TRUE;
81a9ab72 302 }
5e7a8e57 303
81a9ab72 304 if (devc->limit_msec) {
7574e58c 305 time = (g_get_monotonic_time() - devc->start_time) / 1000;
81a9ab72
AJ
306 if (time > (int64_t)devc->limit_msec) {
307 sr_info("Requested time limit reached.");
308 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
309 return TRUE;
310 }
5e7a8e57
AJ
311 }
312
313 return TRUE;
314}