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 probe)
78 ptr = buf + 4 + 14 + 3 * probe;
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 = sdi->priv;
93 struct sr_datafeed_packet packet;
94 struct sr_datafeed_analog analog;
95 struct sr_probe *probe;
96 float values[APPA_55II_NUM_PROBES], *val_ptr;
99 if (devc->data_source != DATA_SOURCE_LIVE)
103 memset(&analog, 0, sizeof(analog));
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;
110 for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
111 probe = g_slist_nth_data(sdi->probes, i);
114 analog.probes = g_slist_append(analog.probes, probe);
115 *val_ptr++ = appa_55ii_temp(buf, i);
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);
126 static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
128 struct dev_context *devc;
131 devc->num_log_records = (buf[5] << 8) + buf[4];
134 static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
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;
148 while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
149 buf = devc->log_buf + offset;
152 /* FIXME: timestamp should be sent in the packet */
153 sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
155 memset(&analog, 0, sizeof(analog));
156 analog.num_samples = 1;
157 analog.mq = SR_MQ_TEMPERATURE;
158 analog.unit = SR_UNIT_CELSIUS;
159 analog.data = values;
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);
166 analog.probes = g_slist_append(analog.probes, probe);
167 *val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10;
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);
176 devc->log_buf_len -= 20;
178 devc->num_log_records--;
181 memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len);
184 static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
186 struct dev_context *devc;
192 if (devc->data_source != DATA_SOURCE_MEMORY)
198 s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
199 memcpy(devc->log_buf + devc->log_buf_len, ptr, s);
200 devc->log_buf_len += s;
204 appa_55ii_log_data_parse(sdi);
208 static void appa_55ii_log_end(struct sr_dev_inst *sdi)
210 struct dev_context *devc;
213 if (devc->data_source != DATA_SOURCE_MEMORY)
216 sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
219 static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
220 const uint8_t *buf, int len)
223 /* Need more data. */
226 if (buf[0] != 0x55 || buf[1] != 0x55)
227 /* Try to re-synchronize on a packet start. */
230 if (len < 5 + buf[3])
231 /* Need more data. */
234 if (!appa_55ii_checksum(buf))
235 /* Skip broken packet. */
236 return buf + 4 + buf[3] + 1;
238 switch ((PacketType) buf[2]) {
240 appa_55ii_live_data(sdi, buf);
243 appa_55ii_log_metadata(sdi, buf);
246 appa_55ii_log_data(sdi, buf);
251 appa_55ii_log_end(sdi);
255 return buf + 4 + buf[3] + 1;
258 SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
260 struct sr_dev_inst *sdi;
261 struct dev_context *devc;
262 struct sr_serial_dev_inst *serial;
264 const uint8_t *ptr, *next_ptr, *end_ptr;
269 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
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);
277 sr_err("Serial port read error: %d.", len);
280 devc->buf_len += len;
282 /* Now look for packets in that data. */
284 end_ptr = ptr + devc->buf_len;
285 while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr)))
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;
292 /* If buffer is full and no valid packet was found, wipe buffer. */
293 if (devc->buf_len >= sizeof(devc->buf)) {
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);
304 if (devc->limit_msec) {
305 time = (g_get_monotonic_time() - devc->start_time) / 1000;
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);