]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/serial-dmm/protocol.c
serial-dmm: Declare each meter type in only one place.
[libsigrok.git] / src / hardware / serial-dmm / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
24#include <errno.h>
25#include <glib.h>
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "protocol.h"
29
30static void log_dmm_packet(const uint8_t *buf)
31{
32 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
33 "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
34 "%02x %02x %02x %02x %02x %02x %02x",
35 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
36 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
37 buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
38 buf[21], buf[22]);
39}
40
41static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
42 void *info)
43{
44 struct dmm_info *dmm = (struct dmm_info *) sdi->driver;
45 float floatval;
46 struct sr_datafeed_packet packet;
47 struct sr_datafeed_analog analog;
48 struct dev_context *devc;
49
50 log_dmm_packet(buf);
51 devc = sdi->priv;
52
53 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
54
55 analog.channels = sdi->channels;
56 analog.num_samples = 1;
57 analog.mq = -1;
58
59 dmm->packet_parse(buf, &floatval, &analog, info);
60 analog.data = &floatval;
61
62 /* If this DMM needs additional handling, call the resp. function. */
63 if (dmm->dmm_details)
64 dmm->dmm_details(&analog, info);
65
66 if (analog.mq != -1) {
67 /* Got a measurement. */
68 packet.type = SR_DF_ANALOG;
69 packet.payload = &analog;
70 sr_session_send(devc->cb_data, &packet);
71 devc->num_samples++;
72 }
73}
74
75/** Request packet, if required. */
76SR_PRIV int req_packet(struct sr_dev_inst *sdi)
77{
78 struct dmm_info *dmm = (struct dmm_info *) sdi->driver;
79 struct dev_context *devc;
80 struct sr_serial_dev_inst *serial;
81 int ret;
82
83 if (!dmm->packet_request)
84 return SR_OK;
85
86 devc = sdi->priv;
87 serial = sdi->conn;
88
89 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
90 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
91 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
92 return SR_OK;
93 }
94
95 ret = dmm->packet_request(serial);
96 if (ret < 0) {
97 sr_err("Failed to request packet: %d.", ret);
98 return ret;
99 }
100
101 if (dmm->req_timeout_ms)
102 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
103
104 return SR_OK;
105}
106
107static void handle_new_data(struct sr_dev_inst *sdi, void *info)
108{
109 struct dmm_info *dmm = (struct dmm_info *) sdi->driver;
110 struct dev_context *devc;
111 int len, i, offset = 0;
112 struct sr_serial_dev_inst *serial;
113
114 devc = sdi->priv;
115 serial = sdi->conn;
116
117 /* Try to get as much data as the buffer can hold. */
118 len = DMM_BUFSIZE - devc->buflen;
119 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
120 if (len == 0)
121 return; /* No new bytes, nothing to do. */
122 if (len < 0) {
123 sr_err("Serial port read error: %d.", len);
124 return;
125 }
126 devc->buflen += len;
127
128 /* Now look for packets in that data. */
129 while ((devc->buflen - offset) >= dmm->packet_size) {
130 if (dmm->packet_valid(devc->buf + offset)) {
131 handle_packet(devc->buf + offset, sdi, info);
132 offset += dmm->packet_size;
133
134 /* Request next packet, if required. */
135 if (!dmm->packet_request)
136 break;
137 if (dmm->req_timeout_ms || dmm->req_delay_ms)
138 devc->req_next_at = g_get_monotonic_time() +
139 dmm->req_delay_ms * 1000;
140 req_packet(sdi);
141 } else {
142 offset++;
143 }
144 }
145
146 /* If we have any data left, move it to the beginning of our buffer. */
147 for (i = 0; i < devc->buflen - offset; i++)
148 devc->buf[i] = devc->buf[offset + i];
149 devc->buflen -= offset;
150}
151
152int receive_data(int fd, int revents, void *cb_data)
153{
154 struct sr_dev_inst *sdi;
155 struct dev_context *devc;
156 struct dmm_info *dmm;
157 int64_t time;
158 void *info;
159
160 (void)fd;
161
162 if (!(sdi = cb_data))
163 return TRUE;
164
165 if (!(devc = sdi->priv))
166 return TRUE;
167
168 dmm = (struct dmm_info *) sdi->driver;
169
170 if (revents == G_IO_IN) {
171 /* Serial data arrived. */
172 info = malloc(dmm->info_size);
173 handle_new_data(sdi, info);
174 free(info);
175 } else {
176 /* Timeout; send another packet request if DMM needs it. */
177 if (dmm->packet_request && (req_packet(sdi) < 0))
178 return FALSE;
179 }
180
181 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
182 sr_info("Requested number of samples reached.");
183 sdi->driver->dev_acquisition_stop(sdi, cb_data);
184 return TRUE;
185 }
186
187 if (devc->limit_msec) {
188 time = (g_get_monotonic_time() - devc->starttime) / 1000;
189 if (time > (int64_t)devc->limit_msec) {
190 sr_info("Requested time limit reached.");
191 sdi->driver->dev_acquisition_stop(sdi, cb_data);
192 return TRUE;
193 }
194 }
195
196 return TRUE;
197}