2013-12-01 00:13:44 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the libsigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Aurelien Jacobs <aurel@gnuage.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2013-12-01 00:13:44 +00:00
|
|
|
#include "protocol.h"
|
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
typedef enum {
|
|
|
|
LIVE_DATA = 0x00,
|
|
|
|
LOG_METADATA = 0x11,
|
|
|
|
LOG_DATA = 0x14,
|
|
|
|
LOG_START = 0x18,
|
|
|
|
LOG_END = 0x19,
|
2013-12-27 16:27:52 +00:00
|
|
|
} packet_type;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
|
|
|
static gboolean appa_55ii_checksum(const uint8_t *buf)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
int i, size, checksum;
|
|
|
|
|
|
|
|
size = buf[3] + 4;
|
|
|
|
checksum = 0;
|
|
|
|
for (i = 0; i < size; i++)
|
2013-12-11 23:22:32 +00:00
|
|
|
checksum += buf[i];
|
2013-12-23 00:53:30 +00:00
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
return buf[size] == (checksum & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV gboolean appa_55ii_packet_valid(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
if (buf[0] == 0x55 && buf[1] == 0x55 && buf[3] <= 32
|
2013-12-23 00:53:30 +00:00
|
|
|
&& appa_55ii_checksum(buf))
|
2013-12-11 23:22:32 +00:00
|
|
|
return TRUE;
|
2013-12-23 00:53:30 +00:00
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t appa_55ii_flags(const uint8_t *buf)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
uint8_t disp_mode;
|
|
|
|
uint64_t flags;
|
|
|
|
|
|
|
|
disp_mode = buf[4 + 13];
|
|
|
|
flags = 0;
|
|
|
|
if ((disp_mode & 0xF0) == 0x20)
|
|
|
|
flags |= SR_MQFLAG_HOLD;
|
|
|
|
if ((disp_mode & 0x0C) == 0x04)
|
|
|
|
flags |= SR_MQFLAG_MAX;
|
|
|
|
if ((disp_mode & 0x0C) == 0x08)
|
|
|
|
flags |= SR_MQFLAG_MIN;
|
|
|
|
if ((disp_mode & 0x0C) == 0x0C)
|
|
|
|
flags |= SR_MQFLAG_AVG;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float appa_55ii_temp(const uint8_t *buf, int probe)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
const uint8_t *ptr;
|
|
|
|
int16_t temp;
|
|
|
|
uint8_t flags;
|
|
|
|
|
2013-12-26 23:22:19 +00:00
|
|
|
ptr = buf + 4 + 14 + 3 * probe;
|
2013-12-23 00:53:30 +00:00
|
|
|
temp = RL16(ptr);
|
|
|
|
flags = ptr[2];
|
|
|
|
|
|
|
|
if (flags & 0x60)
|
|
|
|
return INFINITY;
|
|
|
|
else if (flags & 1)
|
|
|
|
return (float)temp / 10;
|
|
|
|
else
|
|
|
|
return (float)temp;
|
2013-12-11 23:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void appa_55ii_live_data(struct sr_dev_inst *sdi, const uint8_t *buf)
|
|
|
|
{
|
2013-12-26 23:22:19 +00:00
|
|
|
struct dev_context *devc;
|
2013-12-11 23:22:32 +00:00
|
|
|
struct sr_datafeed_packet packet;
|
2013-12-23 00:53:30 +00:00
|
|
|
struct sr_datafeed_analog analog;
|
|
|
|
struct sr_probe *probe;
|
|
|
|
float values[APPA_55II_NUM_PROBES], *val_ptr;
|
2013-12-11 23:22:32 +00:00
|
|
|
int i;
|
|
|
|
|
2013-12-26 23:22:19 +00:00
|
|
|
devc = sdi->priv;
|
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
if (devc->data_source != DATA_SOURCE_LIVE)
|
|
|
|
return;
|
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
val_ptr = values;
|
|
|
|
memset(&analog, 0, sizeof(analog));
|
2013-12-11 23:22:32 +00:00
|
|
|
analog.num_samples = 1;
|
|
|
|
analog.mq = SR_MQ_TEMPERATURE;
|
|
|
|
analog.unit = SR_UNIT_CELSIUS;
|
|
|
|
analog.mqflags = appa_55ii_flags(buf);
|
|
|
|
analog.data = values;
|
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
|
|
|
|
probe = g_slist_nth_data(sdi->probes, i);
|
2013-12-11 23:22:32 +00:00
|
|
|
if (!probe->enabled)
|
|
|
|
continue;
|
|
|
|
analog.probes = g_slist_append(analog.probes, probe);
|
|
|
|
*val_ptr++ = appa_55ii_temp(buf, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.type = SR_DF_ANALOG;
|
|
|
|
packet.payload = &analog;
|
|
|
|
sr_session_send(devc->session_cb_data, &packet);
|
|
|
|
g_slist_free(analog.probes);
|
|
|
|
|
|
|
|
devc->num_samples++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void appa_55ii_log_metadata(struct sr_dev_inst *sdi, const uint8_t *buf)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
struct dev_context *devc;
|
|
|
|
|
|
|
|
devc = sdi->priv;
|
2013-12-11 23:22:32 +00:00
|
|
|
devc->num_log_records = (buf[5] << 8) + buf[4];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void appa_55ii_log_data_parse(struct sr_dev_inst *sdi)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
struct dev_context *devc;
|
|
|
|
struct sr_datafeed_packet packet;
|
2013-12-26 23:22:19 +00:00
|
|
|
struct sr_datafeed_analog analog;
|
2013-12-23 00:53:30 +00:00
|
|
|
struct sr_probe *probe;
|
|
|
|
float values[APPA_55II_NUM_PROBES], *val_ptr;
|
|
|
|
const uint8_t *buf;
|
|
|
|
int16_t temp;
|
|
|
|
int offset, i;
|
|
|
|
|
|
|
|
devc = sdi->priv;
|
|
|
|
offset = 0;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
|
|
|
while (devc->log_buf_len >= 20 && devc->num_log_records > 0) {
|
2013-12-23 00:53:30 +00:00
|
|
|
buf = devc->log_buf + offset;
|
|
|
|
val_ptr = values;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
2013-12-26 23:22:19 +00:00
|
|
|
/* FIXME: Timestamp should be sent in the packet. */
|
2013-12-11 23:22:32 +00:00
|
|
|
sr_dbg("Timestamp: %02d:%02d:%02d", buf[2], buf[3], buf[4]);
|
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
memset(&analog, 0, sizeof(analog));
|
2013-12-11 23:22:32 +00:00
|
|
|
analog.num_samples = 1;
|
|
|
|
analog.mq = SR_MQ_TEMPERATURE;
|
|
|
|
analog.unit = SR_UNIT_CELSIUS;
|
|
|
|
analog.data = values;
|
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
for (i = 0; i < APPA_55II_NUM_PROBES; i++) {
|
|
|
|
temp = RL16(buf + 12 + 2 * i);
|
|
|
|
probe = g_slist_nth_data(sdi->probes, i);
|
2013-12-11 23:22:32 +00:00
|
|
|
if (!probe->enabled)
|
|
|
|
continue;
|
|
|
|
analog.probes = g_slist_append(analog.probes, probe);
|
|
|
|
*val_ptr++ = temp == 0x7FFF ? INFINITY : (float)temp / 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.type = SR_DF_ANALOG;
|
|
|
|
packet.payload = &analog;
|
|
|
|
sr_session_send(devc->session_cb_data, &packet);
|
|
|
|
g_slist_free(analog.probes);
|
|
|
|
|
|
|
|
devc->num_samples++;
|
|
|
|
devc->log_buf_len -= 20;
|
|
|
|
offset += 20;
|
|
|
|
devc->num_log_records--;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(devc->log_buf, devc->log_buf + offset, devc->log_buf_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void appa_55ii_log_data(struct sr_dev_inst *sdi, const uint8_t *buf)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
struct dev_context *devc;
|
|
|
|
const uint8_t *ptr;
|
|
|
|
unsigned int size;
|
|
|
|
int s;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
devc = sdi->priv;
|
2013-12-11 23:22:32 +00:00
|
|
|
if (devc->data_source != DATA_SOURCE_MEMORY)
|
|
|
|
return;
|
|
|
|
|
2013-12-26 23:22:19 +00:00
|
|
|
ptr = buf + 4;
|
2013-12-23 00:53:30 +00:00
|
|
|
size = buf[3];
|
2013-12-11 23:22:32 +00:00
|
|
|
while (size > 0) {
|
2013-12-23 00:53:30 +00:00
|
|
|
s = MIN(size, sizeof(devc->log_buf) - devc->log_buf_len);
|
2013-12-11 23:22:32 +00:00
|
|
|
memcpy(devc->log_buf + devc->log_buf_len, ptr, s);
|
|
|
|
devc->log_buf_len += s;
|
|
|
|
size -= s;
|
|
|
|
ptr += s;
|
|
|
|
|
|
|
|
appa_55ii_log_data_parse(sdi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void appa_55ii_log_end(struct sr_dev_inst *sdi)
|
|
|
|
{
|
2013-12-23 00:53:30 +00:00
|
|
|
struct dev_context *devc;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
devc = sdi->priv;
|
2013-12-11 23:22:32 +00:00
|
|
|
if (devc->data_source != DATA_SOURCE_MEMORY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t *appa_55ii_parse_data(struct sr_dev_inst *sdi,
|
2013-12-23 00:53:30 +00:00
|
|
|
const uint8_t *buf, int len)
|
2013-12-11 23:22:32 +00:00
|
|
|
{
|
|
|
|
if (len < 5)
|
2013-12-23 00:53:30 +00:00
|
|
|
/* Need more data. */
|
|
|
|
return NULL;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
|
|
|
if (buf[0] != 0x55 || buf[1] != 0x55)
|
2013-12-23 00:53:30 +00:00
|
|
|
/* Try to re-synchronize on a packet start. */
|
|
|
|
return buf + 1;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
2013-12-23 00:53:30 +00:00
|
|
|
if (len < 5 + buf[3])
|
|
|
|
/* Need more data. */
|
|
|
|
return NULL;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
|
|
|
if (!appa_55ii_checksum(buf))
|
2013-12-23 00:53:30 +00:00
|
|
|
/* Skip broken packet. */
|
|
|
|
return buf + 4 + buf[3] + 1;
|
2013-12-11 23:22:32 +00:00
|
|
|
|
2013-12-27 16:27:52 +00:00
|
|
|
switch ((packet_type)buf[2]) {
|
2013-12-23 00:53:30 +00:00
|
|
|
case LIVE_DATA:
|
|
|
|
appa_55ii_live_data(sdi, buf);
|
|
|
|
break;
|
|
|
|
case LOG_METADATA:
|
|
|
|
appa_55ii_log_metadata(sdi, buf);
|
|
|
|
break;
|
|
|
|
case LOG_DATA:
|
|
|
|
appa_55ii_log_data(sdi, buf);
|
|
|
|
break;
|
|
|
|
case LOG_START:
|
|
|
|
break;
|
|
|
|
case LOG_END:
|
|
|
|
appa_55ii_log_end(sdi);
|
|
|
|
break;
|
2013-12-26 23:22:19 +00:00
|
|
|
default:
|
|
|
|
sr_warn("Invalid packet type: 0x%02x.", buf[2]);
|
|
|
|
break;
|
2013-12-11 23:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf + 4 + buf[3] + 1;
|
|
|
|
}
|
|
|
|
|
2013-12-01 00:13:44 +00:00
|
|
|
SR_PRIV int appa_55ii_receive_data(int fd, int revents, void *cb_data)
|
|
|
|
{
|
2013-12-11 23:22:32 +00:00
|
|
|
struct sr_dev_inst *sdi;
|
2013-12-01 00:13:44 +00:00
|
|
|
struct dev_context *devc;
|
2013-12-11 23:22:32 +00:00
|
|
|
struct sr_serial_dev_inst *serial;
|
2013-12-23 00:53:30 +00:00
|
|
|
int64_t time;
|
2013-12-11 23:22:32 +00:00
|
|
|
const uint8_t *ptr, *next_ptr, *end_ptr;
|
|
|
|
int len;
|
2013-12-01 00:13:44 +00:00
|
|
|
|
|
|
|
(void)fd;
|
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
|
2013-12-01 00:13:44 +00:00
|
|
|
return TRUE;
|
2013-12-11 23:22:32 +00:00
|
|
|
serial = sdi->conn;
|
|
|
|
|
|
|
|
/* Try to get as much data as the buffer can hold. */
|
|
|
|
len = sizeof(devc->buf) - devc->buf_len;
|
|
|
|
len = serial_read(serial, devc->buf + devc->buf_len, len);
|
|
|
|
if (len < 1) {
|
|
|
|
sr_err("Serial port read error: %d.", len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
devc->buf_len += len;
|
|
|
|
|
|
|
|
/* Now look for packets in that data. */
|
|
|
|
ptr = devc->buf;
|
|
|
|
end_ptr = ptr + devc->buf_len;
|
|
|
|
while ((next_ptr = appa_55ii_parse_data(sdi, ptr, end_ptr - ptr)))
|
|
|
|
ptr = next_ptr;
|
2013-12-01 00:13:44 +00:00
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
/* If we have any data left, move it to the beginning of our buffer. */
|
|
|
|
memmove(devc->buf, ptr, end_ptr - ptr);
|
|
|
|
devc->buf_len -= ptr - devc->buf;
|
|
|
|
|
|
|
|
/* If buffer is full and no valid packet was found, wipe buffer. */
|
|
|
|
if (devc->buf_len >= sizeof(devc->buf)) {
|
|
|
|
devc->buf_len = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
|
|
|
|
sr_info("Requested number of samples reached.");
|
|
|
|
sdi->driver->dev_acquisition_stop(sdi, devc->session_cb_data);
|
2013-12-01 00:13:44 +00:00
|
|
|
return TRUE;
|
2013-12-11 23:22:32 +00:00
|
|
|
}
|
2013-12-01 00:13:44 +00:00
|
|
|
|
2013-12-11 23:22:32 +00:00
|
|
|
if (devc->limit_msec) {
|
2013-12-23 00:53:30 +00:00
|
|
|
time = (g_get_monotonic_time() - devc->start_time) / 1000;
|
2013-12-11 23:22:32 +00:00
|
|
|
if (time > (int64_t)devc->limit_msec) {
|
|
|
|
sr_info("Requested time limit reached.");
|
2013-12-26 23:22:19 +00:00
|
|
|
sdi->driver->dev_acquisition_stop(sdi,
|
|
|
|
devc->session_cb_data);
|
2013-12-11 23:22:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2013-12-01 00:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|