]> sigrok.org Git - libsigrok.git/blob - src/hardware/appa-55ii/protocol.c
appa-55ii: Convert to SR_DF_ANALOG.
[libsigrok.git] / src / hardware / appa-55ii / protocol.c
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 analog;
96         struct sr_analog_encoding encoding;
97         struct sr_analog_meaning meaning;
98         struct sr_analog_spec spec;
99         struct sr_channel *ch;
100         float values[APPA_55II_NUM_CHANNELS], *val_ptr;
101         int i;
102
103         devc = sdi->priv;
104
105         if (devc->data_source != DATA_SOURCE_LIVE)
106                 return;
107
108         val_ptr = values;
109         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
110         analog.num_samples = 1;
111         analog.meaning->mq = SR_MQ_TEMPERATURE;
112         analog.meaning->unit = SR_UNIT_CELSIUS;
113         analog.meaning->mqflags = appa_55ii_flags(buf);
114         analog.data = values;
115
116         for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) {
117                 ch = g_slist_nth_data(sdi->channels, i);
118                 if (!ch->enabled)
119                         continue;
120                 analog.meaning->channels = g_slist_append(analog.meaning->channels, ch);
121                 *val_ptr++ = appa_55ii_temp(buf, i);
122         }
123
124         packet.type = SR_DF_ANALOG;
125         packet.payload = &analog;
126         sr_session_send(sdi, &packet);
127         g_slist_free(analog.meaning->channels);
128
129         sr_sw_limits_update_samples_read(&devc->limits, 1);
130 }
131
132 static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
133 {
134         struct dev_context *devc;
135
136         devc = sdi->priv;
137         devc->num_log_records = (buf[5] << 8) + buf[4];
138 }
139
140 static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
141 {
142         struct dev_context *devc;
143         struct sr_datafeed_packet packet;
144         struct sr_datafeed_analog analog;
145         struct sr_analog_encoding encoding;
146         struct sr_analog_meaning meaning;
147         struct sr_analog_spec spec;
148         struct sr_channel *ch;
149         float values[APPA_55II_NUM_CHANNELS], *val_ptr;
150         const uint8_t *buf;
151         int16_t temp;
152         int offset, i;
153
154         devc = sdi->priv;
155         offset = 0;
156
157         while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
158                 buf = devc->log_buf + offset;
159                 val_ptr = values;
160
161                 /* FIXME: Timestamp should be sent in the packet. */
162                 sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
163
164                 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
165                 analog.num_samples = 1;
166                 analog.meaning->mq = SR_MQ_TEMPERATURE;
167                 analog.meaning->unit = SR_UNIT_CELSIUS;
168                 analog.data = values;
169
170                 for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) {
171                         temp = RL16(buf + 12 + 2 * i);
172                         ch = g_slist_nth_data(sdi->channels, i);
173                         if (!ch->enabled)
174                                 continue;
175                         analog.meaning->channels = g_slist_append(analog.meaning->channels, ch);
176                         *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10;
177                 }
178
179                 packet.type = SR_DF_ANALOG;
180                 packet.payload = &analog;
181                 sr_session_send(sdi, &packet);
182                 g_slist_free(analog.meaning->channels);
183
184                 sr_sw_limits_update_samples_read(&devc->limits, 1);
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
193 static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
194 {
195         struct dev_context *devc;
196         const uint8_t *ptr;
197         unsigned int size;
198         int s;
199
200         devc = sdi->priv;
201         if (devc->data_source != DATA_SOURCE_MEMORY)
202                 return;
203
204         ptr = buf + 4;
205         size = buf[3];
206         while (size > 0) {
207                 s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
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
217 static void appa_55ii_log_end(struct sr_dev_inst *sdi)
218 {
219         struct dev_context *devc;
220
221         devc = sdi->priv;
222         if (devc->data_source != DATA_SOURCE_MEMORY)
223                 return;
224
225         sdi->driver->dev_acquisition_stop(sdi);
226 }
227
228 static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
229                 const uint8_t *buf, int len)
230 {
231         if (len < 5)
232                 /* Need more data. */
233                 return NULL;
234
235         if (buf[0] != 0x55 || buf[1] != 0x55)
236                 /* Try to re-synchronize on a packet start. */
237                 return buf + 1;
238
239         if (len < 5 + buf[3])
240                 /* Need more data. */
241                 return NULL;
242
243         if (!appa_55ii_checksum(buf))
244                 /* Skip broken packet. */
245                 return buf + 4 + buf[3] + 1;
246
247         switch ((packet_type)buf[2]) {
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;
262         default:
263                 sr_warn("Invalid packet type: 0x%02x.", buf[2]);
264                 break;
265         }
266
267         return buf + 4 + buf[3] + 1;
268 }
269
270 SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
271 {
272         struct sr_dev_inst *sdi;
273         struct dev_context *devc;
274         struct sr_serial_dev_inst *serial;
275         const uint8_t *ptr, *next_ptr, *end_ptr;
276         int len;
277
278         (void)fd;
279
280         if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
281                 return TRUE;
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;
286         len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len);
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;
298
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
309         if (sr_sw_limits_check(&devc->limits)) {
310                 sdi->driver->dev_acquisition_stop(sdi);
311                 return TRUE;
312         }
313
314         return TRUE;
315 }