2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
32 static gboolean appa_55ii_checksum(const uint8_t *buf)
34 int i, size, checksum;
38 for (i = 0; i < size; i++)
41 return buf[size] == (checksum & 0xFF);
44 SR_PRIV gboolean appa_55ii_packet_valid(const uint8_t *buf)
46 if (buf[0] == 0x55 && buf[1] == 0x55 && buf[3] <= 32
47 && appa_55ii_checksum(buf))
53 static uint64_t appa_55ii_flags(const uint8_t *buf)
58 disp_mode = buf[4 + 13];
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;
72 static float appa_55ii_temp(const uint8_t *buf, int ch)
78 ptr = buf + 4 + 14 + 3 * ch;
85 return (float)temp / 10;
90 static void appa_55ii_live_data(struct sr_dev_inst *sdi, const uint8_t *buf)
92 struct dev_context *devc;
93 struct sr_datafeed_packet packet;
94 struct sr_datafeed_analog analog;
95 struct sr_channel *ch;
96 float values[APPA_55II_NUM_CHANNELS], *val_ptr;
101 if (devc->data_source != DATA_SOURCE_LIVE)
105 memset(&analog, 0, sizeof(analog));
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;
112 for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) {
113 ch = g_slist_nth_data(sdi->channels, i);
116 analog.channels = g_slist_append(analog.channels, ch);
117 *val_ptr++ = appa_55ii_temp(buf, i);
120 packet.type = SR_DF_ANALOG;
121 packet.payload = &analog;
122 sr_session_send(devc->session_cb_data, &packet);
123 g_slist_free(analog.channels);
128 static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
130 struct dev_context *devc;
133 devc->num_log_records = (buf[5] << 8) + buf[4];
136 static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
138 struct dev_context *devc;
139 struct sr_datafeed_packet packet;
140 struct sr_datafeed_analog analog;
141 struct sr_channel *ch;
142 float values[APPA_55II_NUM_CHANNELS], *val_ptr;
150 while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
151 buf = devc->log_buf + offset;
154 /* FIXME: Timestamp should be sent in the packet. */
155 sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
157 memset(&analog, 0, sizeof(analog));
158 analog.num_samples = 1;
159 analog.mq = SR_MQ_TEMPERATURE;
160 analog.unit = SR_UNIT_CELSIUS;
161 analog.data = values;
163 for (i = 0; i < APPA_55II_NUM_CHANNELS; i++) {
164 temp = RL16(buf + 12 + 2 * i);
165 ch = g_slist_nth_data(sdi->channels, i);
168 analog.channels = g_slist_append(analog.channels, ch);
169 *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10;
172 packet.type = SR_DF_ANALOG;
173 packet.payload = &analog;
174 sr_session_send(devc->session_cb_data, &packet);
175 g_slist_free(analog.channels);
178 devc->log_buf_len -= 20;
180 devc->num_log_records--;
183 memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len);
186 static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
188 struct dev_context *devc;
194 if (devc->data_source != DATA_SOURCE_MEMORY)
200 s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
201 memcpy(devc->log_buf + devc->log_buf_len, ptr, s);
202 devc->log_buf_len += s;
206 appa_55ii_log_data_parse(sdi);
210 static void appa_55ii_log_end(struct sr_dev_inst *sdi)
212 struct dev_context *devc;
215 if (devc->data_source != DATA_SOURCE_MEMORY)
218 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
221 static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
222 const uint8_t *buf, int len)
225 /* Need more data. */
228 if (buf[0] != 0x55 || buf[1] != 0x55)
229 /* Try to re-synchronize on a packet start. */
232 if (len < 5 + buf[3])
233 /* Need more data. */
236 if (!appa_55ii_checksum(buf))
237 /* Skip broken packet. */
238 return buf + 4 + buf[3] + 1;
240 switch ((packet_type)buf[2]) {
242 appa_55ii_live_data(sdi, buf);
245 appa_55ii_log_metadata(sdi, buf);
248 appa_55ii_log_data(sdi, buf);
253 appa_55ii_log_end(sdi);
256 sr_warn("Invalid packet type: 0x%02x.", buf[2]);
260 return buf + 4 + buf[3] + 1;
263 SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
265 struct sr_dev_inst *sdi;
266 struct dev_context *devc;
267 struct sr_serial_dev_inst *serial;
269 const uint8_t *ptr, *next_ptr, *end_ptr;
274 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
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);
282 sr_err("Serial port read error: %d.", len);
285 devc->buf_len += len;
287 /* Now look for packets in that data. */
289 end_ptr = ptr + devc->buf_len;
290 while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr)))
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;
297 /* If buffer is full and no valid packet was found, wipe buffer. */
298 if (devc->buf_len >= sizeof(devc->buf)) {
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);
309 if (devc->limit_msec) {
310 time = (g_get_monotonic_time() - devc->start_time) / 1000;
311 if (time > (int64_t)devc->limit_msec) {
312 sr_info("Requested time limit reached.");
313 sdi->driver->dev_acquisition_stop(sdi,
314 devc->session_cb_data);