2013-01-30 15:58:01 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the libsigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "protocol.h"
|
|
|
|
|
2013-01-30 17:21:07 +00:00
|
|
|
static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
|
2013-01-30 15:58:01 +00:00
|
|
|
{
|
2013-01-30 17:21:07 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = serial_write(serial, cmd, strlen(cmd))) < 0) {
|
|
|
|
sr_err("Error sending '%s' command: %d.", cmd, ret);
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
|
|
|
|
{
|
|
|
|
return mic_send(serial, "I\r");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mic_cmd_set_realtime_mode(struct sr_serial_dev_inst *serial)
|
|
|
|
{
|
|
|
|
return mic_send(serial, "S 1 M 2 32 3\r");
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:13:03 +00:00
|
|
|
SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
|
2013-01-30 17:21:07 +00:00
|
|
|
{
|
|
|
|
if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:13:03 +00:00
|
|
|
static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
|
2013-01-30 17:21:07 +00:00
|
|
|
{
|
|
|
|
char tmp[4];
|
|
|
|
|
2013-02-09 15:13:03 +00:00
|
|
|
/* Packet format MIC98581: "v ttt\r". */
|
|
|
|
/* Packet format MIC98583: "v ttt hhh\r". */
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
/* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
|
|
|
|
|
|
|
|
tmp[3] = '\0';
|
|
|
|
|
|
|
|
strncpy((char *)&tmp, &buf[2], 3);
|
|
|
|
*temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
|
|
|
|
|
2013-02-09 15:13:03 +00:00
|
|
|
if (mic_devs[idx].has_humidity) {
|
|
|
|
strncpy((char *)&tmp, &buf[6], 3);
|
|
|
|
*humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
|
|
|
|
}
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
|
|
|
|
{
|
|
|
|
float temperature, humidity;
|
|
|
|
struct sr_datafeed_packet packet;
|
|
|
|
struct sr_datafeed_analog analog;
|
|
|
|
struct dev_context *devc;
|
|
|
|
GSList *l;
|
|
|
|
int ret;
|
2013-01-30 15:58:01 +00:00
|
|
|
|
2013-01-30 17:21:07 +00:00
|
|
|
(void)idx;
|
|
|
|
|
|
|
|
devc = sdi->priv;
|
|
|
|
|
2013-02-09 15:13:03 +00:00
|
|
|
ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
|
2013-01-30 17:21:07 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
sr_err("Failed to parse packet.");
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
2013-02-11 17:23:17 +00:00
|
|
|
/* Clear 'analog', otherwise it'll contain random garbage. */
|
|
|
|
memset(&analog, 0, sizeof(struct sr_datafeed_analog));
|
|
|
|
|
2013-01-30 17:21:07 +00:00
|
|
|
/* Common values for both probes. */
|
|
|
|
packet.type = SR_DF_ANALOG;
|
|
|
|
packet.payload = &analog;
|
|
|
|
analog.num_samples = 1;
|
|
|
|
|
|
|
|
/* Temperature. */
|
|
|
|
l = g_slist_copy(sdi->probes);
|
|
|
|
l = g_slist_remove_link(l, g_slist_nth(l, 1));
|
|
|
|
analog.probes = l;
|
|
|
|
analog.mq = SR_MQ_TEMPERATURE;
|
|
|
|
analog.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
|
|
|
|
analog.data = &temperature;
|
|
|
|
sr_session_send(devc->cb_data, &packet);
|
|
|
|
g_slist_free(l);
|
|
|
|
|
|
|
|
/* Humidity. */
|
2013-02-09 15:13:03 +00:00
|
|
|
if (mic_devs[idx].has_humidity) {
|
|
|
|
l = g_slist_copy(sdi->probes);
|
|
|
|
l = g_slist_remove_link(l, g_slist_nth(l, 0));
|
|
|
|
analog.probes = l;
|
|
|
|
analog.mq = SR_MQ_RELATIVE_HUMIDITY;
|
|
|
|
analog.unit = SR_UNIT_PERCENTAGE;
|
|
|
|
analog.data = &humidity;
|
|
|
|
sr_session_send(devc->cb_data, &packet);
|
|
|
|
g_slist_free(l);
|
|
|
|
}
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
devc->num_samples++;
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_new_data(struct sr_dev_inst *sdi, int idx)
|
|
|
|
{
|
2013-01-30 15:58:01 +00:00
|
|
|
struct dev_context *devc;
|
2013-04-23 21:06:02 +00:00
|
|
|
struct sr_serial_dev_inst *serial;
|
2013-01-30 17:21:07 +00:00
|
|
|
int len, i, offset = 0;
|
|
|
|
|
|
|
|
devc = sdi->priv;
|
2013-04-23 21:06:02 +00:00
|
|
|
serial = sdi->conn;
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
/* Try to get as much data as the buffer can hold. */
|
|
|
|
len = SERIAL_BUFSIZE - devc->buflen;
|
2013-04-23 21:06:02 +00:00
|
|
|
len = serial_read(serial, devc->buf + devc->buflen, len);
|
2013-01-30 17:21:07 +00:00
|
|
|
if (len < 1) {
|
|
|
|
sr_err("Serial port read error: %d.", len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
devc->buflen += len;
|
|
|
|
|
|
|
|
/* Now look for packets in that data. */
|
2013-02-09 15:13:03 +00:00
|
|
|
while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
|
|
|
|
if (mic_devs[idx].packet_valid(devc->buf + offset)) {
|
2013-01-30 17:21:07 +00:00
|
|
|
handle_packet(devc->buf + offset, sdi, idx);
|
2013-02-09 15:13:03 +00:00
|
|
|
offset += mic_devs[idx].packet_size;
|
2013-01-30 17:21:07 +00:00
|
|
|
} else {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have any data left, move it to the beginning of our buffer. */
|
|
|
|
for (i = 0; i < devc->buflen - offset; i++)
|
|
|
|
devc->buf[i] = devc->buf[offset + i];
|
|
|
|
devc->buflen -= offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int receive_data(int fd, int revents, int idx, void *cb_data)
|
|
|
|
{
|
|
|
|
struct sr_dev_inst *sdi;
|
|
|
|
struct dev_context *devc;
|
|
|
|
int64_t t;
|
|
|
|
static gboolean first_time = TRUE;
|
2013-04-23 21:06:02 +00:00
|
|
|
struct sr_serial_dev_inst *serial;
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
(void)fd;
|
2013-01-30 15:58:01 +00:00
|
|
|
|
|
|
|
if (!(sdi = cb_data))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!(devc = sdi->priv))
|
|
|
|
return TRUE;
|
|
|
|
|
2013-04-23 21:06:02 +00:00
|
|
|
serial = sdi->conn;
|
|
|
|
|
2013-01-30 15:58:01 +00:00
|
|
|
if (revents == G_IO_IN) {
|
2013-01-30 17:21:07 +00:00
|
|
|
/* New data arrived. */
|
|
|
|
handle_new_data(sdi, idx);
|
|
|
|
} else {
|
|
|
|
/* Timeout. */
|
|
|
|
if (first_time) {
|
2013-04-23 21:06:02 +00:00
|
|
|
mic_cmd_set_realtime_mode(serial);
|
2013-01-30 17:21:07 +00:00
|
|
|
first_time = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
|
|
|
|
sr_info("Requested number of samples reached.");
|
|
|
|
sdi->driver->dev_acquisition_stop(sdi, cb_data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devc->limit_msec) {
|
|
|
|
t = (g_get_monotonic_time() - devc->starttime) / 1000;
|
|
|
|
if (t > (int64_t)devc->limit_msec) {
|
|
|
|
sr_info("Requested time limit reached.");
|
|
|
|
sdi->driver->dev_acquisition_stop(sdi, cb_data);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-30 15:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-01-30 17:21:07 +00:00
|
|
|
|
|
|
|
#define RECEIVE_DATA(ID_UPPER) \
|
|
|
|
SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
|
|
|
|
return receive_data(fd, revents, ID_UPPER, cb_data); }
|
|
|
|
|
|
|
|
/* Driver-specific receive_data() wrappers */
|
2013-02-09 15:13:03 +00:00
|
|
|
RECEIVE_DATA(MIC_98581)
|
2013-01-30 17:21:07 +00:00
|
|
|
RECEIVE_DATA(MIC_98583)
|