X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fhardware%2Fserial-dmm%2Fprotocol.c;h=483bb009b1295fac7464dd9c2c7ff31005c0f0c0;hb=9417f26fd47556c4b4f6ecaa08a2860672664eb9;hp=6128e493f66888bb97aab00e132095c441308c45;hpb=1a8639164e4e44a43fe1558e30823606f7b607b3;p=libsigrok.git
diff --git a/src/hardware/serial-dmm/protocol.c b/src/hardware/serial-dmm/protocol.c
index 6128e493..483bb009 100644
--- a/src/hardware/serial-dmm/protocol.c
+++ b/src/hardware/serial-dmm/protocol.c
@@ -18,59 +18,85 @@
* along with this program. If not, see .
*/
+#include
#include
#include
#include
-#include
#include
-#include "libsigrok.h"
+#include
#include "libsigrok-internal.h"
#include "protocol.h"
-static void log_dmm_packet(const uint8_t *buf)
+static void log_dmm_packet(const uint8_t *buf, size_t len)
{
- sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
- "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
- "%02x %02x %02x %02x %02x %02x %02x",
- buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
- buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
- buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
- buf[21], buf[22]);
+ GString *text;
+
+ if (sr_log_loglevel_get() < SR_LOG_DBG)
+ return;
+
+ text = sr_hexdump_new(buf, len);
+ sr_dbg("DMM packet: %s", text->str);
+ sr_hexdump_free(text);
}
-static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
- void *info)
+static void handle_packet(struct sr_dev_inst *sdi,
+ const uint8_t *buf, size_t len, void *info)
{
struct dmm_info *dmm;
+ struct dev_context *devc;
float floatval;
+ double doubleval;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog;
- struct dev_context *devc;
+ struct sr_analog_encoding encoding;
+ struct sr_analog_meaning meaning;
+ struct sr_analog_spec spec;
+ gboolean sent_sample;
+ struct sr_channel *channel;
+ size_t ch_idx;
dmm = (struct dmm_info *)sdi->driver;
- log_dmm_packet(buf);
+ log_dmm_packet(buf, len);
devc = sdi->priv;
- memset(&analog, 0, sizeof(struct sr_datafeed_analog));
-
- analog.channels = sdi->channels;
- analog.num_samples = 1;
- analog.mq = -1;
+ sent_sample = FALSE;
+ memset(info, 0, dmm->info_size);
+ for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) {
+ /* Note: digits/spec_digits will be overridden by the DMM parsers. */
+ sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
+ channel = g_slist_nth_data(sdi->channels, ch_idx);
+ analog.meaning->channels = g_slist_append(NULL, channel);
+ analog.num_samples = 1;
+ analog.meaning->mq = 0;
+
+ if (dmm->packet_parse) {
+ dmm->packet_parse(buf, &floatval, &analog, info);
+ analog.data = &floatval;
+ analog.encoding->unitsize = sizeof(floatval);
+ } else if (dmm->packet_parse_len) {
+ dmm->packet_parse_len(dmm->dmm_state, buf, len,
+ &doubleval, &analog, info);
+ analog.data = &doubleval;
+ analog.encoding->unitsize = sizeof(doubleval);
+ }
- dmm->packet_parse(buf, &floatval, &analog, info);
- analog.data = &floatval;
+ /* If this DMM needs additional handling, call the resp. function. */
+ if (dmm->dmm_details)
+ dmm->dmm_details(&analog, info);
- /* If this DMM needs additional handling, call the resp. function. */
- if (dmm->dmm_details)
- dmm->dmm_details(&analog, info);
+ if (analog.meaning->mq != 0 && channel->enabled) {
+ /* Got a measurement. */
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+ sr_session_send(sdi, &packet);
+ sent_sample = TRUE;
+ }
+ }
- if (analog.mq != -1) {
- /* Got a measurement. */
- packet.type = SR_DF_ANALOG;
- packet.payload = &analog;
- sr_session_send(devc->cb_data, &packet);
- devc->num_samples++;
+ if (sent_sample) {
+ sr_sw_limits_update_samples_read(&devc->limits, 1);
}
}
@@ -80,30 +106,34 @@ SR_PRIV int req_packet(struct sr_dev_inst *sdi)
struct dmm_info *dmm;
struct dev_context *devc;
struct sr_serial_dev_inst *serial;
+ uint64_t now, left, next;
int ret;
dmm = (struct dmm_info *)sdi->driver;
-
if (!dmm->packet_request)
return SR_OK;
devc = sdi->priv;
serial = sdi->conn;
- if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
- sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
- ((devc->req_next_at - g_get_monotonic_time()) / 1000));
+ now = g_get_monotonic_time();
+ if (devc->req_next_at && now < devc->req_next_at) {
+ left = (devc->req_next_at - now) / 1000;
+ sr_spew("Not re-requesting yet, %" PRIu64 "ms left.", left);
return SR_OK;
}
+ sr_spew("Requesting next packet.");
ret = dmm->packet_request(serial);
if (ret < 0) {
sr_err("Failed to request packet: %d.", ret);
return ret;
}
- if (dmm->req_timeout_ms)
- devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
+ if (dmm->req_timeout_ms) {
+ next = now + dmm->req_timeout_ms * 1000;
+ devc->req_next_at = next;
+ }
return SR_OK;
}
@@ -112,47 +142,96 @@ static void handle_new_data(struct sr_dev_inst *sdi, void *info)
{
struct dmm_info *dmm;
struct dev_context *devc;
- int len, i, offset = 0;
struct sr_serial_dev_inst *serial;
+ int ret;
+ size_t read_len, check_pos, check_len, pkt_size, copy_len;
+ uint8_t *check_ptr;
+ uint64_t deadline;
dmm = (struct dmm_info *)sdi->driver;
devc = sdi->priv;
serial = sdi->conn;
- /* Try to get as much data as the buffer can hold. */
- len = DMM_BUFSIZE - devc->buflen;
- len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
- if (len == 0)
+ /* Add the maximum available RX data we can get to the local buffer. */
+ read_len = DMM_BUFSIZE - devc->buflen;
+ ret = serial_read_nonblocking(serial, &devc->buf[devc->buflen], read_len);
+ if (ret == 0)
return; /* No new bytes, nothing to do. */
- if (len < 0) {
- sr_err("Serial port read error: %d.", len);
+ if (ret < 0) {
+ sr_err("Serial port read error: %d.", ret);
return;
}
- devc->buflen += len;
-
- /* Now look for packets in that data. */
- while ((devc->buflen - offset) >= dmm->packet_size) {
- if (dmm->packet_valid(devc->buf + offset)) {
- handle_packet(devc->buf + offset, sdi, info);
- offset += dmm->packet_size;
-
- /* Request next packet, if required. */
- if (!dmm->packet_request)
+ devc->buflen += ret;
+
+ /*
+ * Process packets when their reception has completed, or keep
+ * trying to synchronize to the stream of input data.
+ */
+ check_pos = 0;
+ while (check_pos < devc->buflen) {
+ /* Got the (minimum) amount of receive data for a packet? */
+ check_len = devc->buflen - check_pos;
+ if (check_len < dmm->packet_size)
+ break;
+ sr_dbg("Checking: pos %zu, len %zu.", check_pos, check_len);
+
+ /* Is it a valid packet? */
+ check_ptr = &devc->buf[check_pos];
+ if (dmm->packet_valid_len) {
+ ret = dmm->packet_valid_len(dmm->dmm_state,
+ check_ptr, check_len, &pkt_size);
+ if (ret == SR_PACKET_NEED_RX) {
+ sr_dbg("Need more RX data.");
break;
- if (dmm->req_timeout_ms || dmm->req_delay_ms)
- devc->req_next_at = g_get_monotonic_time() +
- dmm->req_delay_ms * 1000;
- req_packet(sdi);
- } else {
- offset++;
+ }
+ if (ret == SR_PACKET_INVALID) {
+ sr_dbg("Not a valid packet, searching.");
+ check_pos++;
+ continue;
+ }
+ } else if (dmm->packet_valid) {
+ if (!dmm->packet_valid(check_ptr)) {
+ sr_dbg("Not a valid packet, searching.");
+ check_pos++;
+ continue;
+ }
+ pkt_size = dmm->packet_size;
}
+
+ /* Process the package. */
+ sr_dbg("Valid packet, size %zu, processing", pkt_size);
+ handle_packet(sdi, check_ptr, pkt_size, info);
+ check_pos += pkt_size;
+
+ /* Arrange for the next packet request if needed. */
+ if (!dmm->packet_request)
+ continue;
+ if (dmm->req_timeout_ms || dmm->req_delay_ms) {
+ deadline = g_get_monotonic_time();
+ deadline += dmm->req_delay_ms * 1000;
+ devc->req_next_at = deadline;
+ }
+ req_packet(sdi);
+ continue;
}
/* 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;
+ if (devc->buflen > check_pos) {
+ copy_len = devc->buflen - check_pos;
+ memmove(&devc->buf[0], &devc->buf[check_pos], copy_len);
+ }
+ devc->buflen -= check_pos;
+
+ /*
+ * If the complete buffer filled up and none of it got processed,
+ * discard the unprocessed buffer, re-sync to the stream in later
+ * calls again.
+ */
+ if (devc->buflen == sizeof(devc->buf)) {
+ sr_info("Drop unprocessed RX data, try to re-sync to stream.");
+ devc->buflen = 0;
+ }
}
int receive_data(int fd, int revents, void *cb_data)
@@ -160,7 +239,6 @@ int receive_data(int fd, int revents, void *cb_data)
struct sr_dev_inst *sdi;
struct dev_context *devc;
struct dmm_info *dmm;
- int64_t time;
void *info;
(void)fd;
@@ -175,29 +253,17 @@ int receive_data(int fd, int revents, void *cb_data)
if (revents == G_IO_IN) {
/* Serial data arrived. */
- info = malloc(dmm->info_size);
+ info = g_malloc(dmm->info_size);
handle_new_data(sdi, info);
- free(info);
+ g_free(info);
} else {
/* Timeout; send another packet request if DMM needs it. */
if (dmm->packet_request && (req_packet(sdi) < 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, cb_data);
- return TRUE;
- }
-
- if (devc->limit_msec) {
- time = (g_get_monotonic_time() - devc->starttime) / 1000;
- if (time > (int64_t)devc->limit_msec) {
- sr_info("Requested time limit reached.");
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
- return TRUE;
- }
- }
+ if (sr_sw_limits_check(&devc->limits))
+ sr_dev_acquisition_stop(sdi);
return TRUE;
}