2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 static void log_dmm_packet(const uint8_t *buf)
32 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
33 " %02x %02x %02x %02x %02x %02x %02x",
34 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
35 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
38 SR_PRIV void dmm_details_dt4000zc(struct sr_datafeed_analog *analog, void *info)
40 dmm_details_tp4000zc(analog, info); /* Same as TP4000ZC. */
43 SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
45 struct fs9721_info *info_local;
47 info_local = (struct fs9721_info *)info;
49 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
50 if (info_local->is_c2c1_10) {
51 analog->mq = SR_MQ_TEMPERATURE;
52 analog->unit = SR_UNIT_CELSIUS;
56 SR_PRIV void dmm_details_va18b(struct sr_datafeed_analog *analog, void *info)
58 struct fs9721_info *info_local;
60 info_local = (struct fs9721_info *)info;
62 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature. */
63 if (info_local->is_c2c1_01) {
64 analog->mq = SR_MQ_TEMPERATURE;
65 analog->unit = SR_UNIT_CELSIUS;
69 static void handle_packet(const uint8_t *buf, struct dev_context *devc,
73 struct sr_datafeed_packet packet;
74 struct sr_datafeed_analog *analog;
78 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
79 sr_err("Analog packet malloc failed.");
83 analog->num_samples = 1;
86 dmms[dmm].packet_parse(buf, &floatval, analog, info);
87 analog->data = &floatval;
89 if (dmms[dmm].dmm_details)
90 dmms[dmm].dmm_details(analog, info);
92 if (analog->mq != -1) {
93 /* Got a measurement. */
94 packet.type = SR_DF_ANALOG;
95 packet.payload = analog;
96 sr_session_send(devc->cb_data, &packet);
103 static void handle_new_data(struct dev_context *devc, int dmm, void *info)
105 int len, i, offset = 0;
107 /* Try to get as much data as the buffer can hold. */
108 len = DMM_BUFSIZE - devc->buflen;
109 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
111 sr_err("Serial port read error: %d.", len);
116 /* Now look for packets in that data. */
117 while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
118 if (dmms[dmm].packet_valid(devc->buf + offset)) {
119 handle_packet(devc->buf + offset, devc, dmm, info);
120 offset += dmms[dmm].packet_size;
126 /* If we have any data left, move it to the beginning of our buffer. */
127 for (i = 0; i < devc->buflen - offset; i++)
128 devc->buf[i] = devc->buf[offset + i];
129 devc->buflen -= offset;
132 static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
134 struct sr_dev_inst *sdi;
135 struct dev_context *devc;
140 if (!(sdi = cb_data))
143 if (!(devc = sdi->priv))
146 if (revents == G_IO_IN) {
147 /* Serial data arrived. */
148 handle_new_data(devc, dmm, info);
150 /* Timeout, send another packet request (if DMM needs it). */
151 if (dmms[dmm].packet_request) {
152 ret = dmms[dmm].packet_request(devc->serial);
154 sr_err("Failed to request packet: %d.", ret);
160 if (devc->num_samples >= devc->limit_samples) {
161 sr_info("Requested number of samples reached, stopping.");
162 sdi->driver->dev_acquisition_stop(sdi, cb_data);
169 SR_PRIV int digitek_dt4000zc_receive_data(int fd, int revents, void *cb_data)
171 struct fs9721_info info;
172 return receive_data(fd, revents, DIGITEK_DT4000ZC, &info, cb_data);
175 SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
177 struct fs9721_info info;
178 return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
181 SR_PRIV int metex_me31_receive_data(int fd, int revents, void *cb_data)
183 struct metex14_info info;
184 return receive_data(fd, revents, METEX_ME31, &info, cb_data);
187 SR_PRIV int peaktech_3410_receive_data(int fd, int revents, void *cb_data)
189 struct metex14_info info;
190 return receive_data(fd, revents, PEAKTECH_3410, &info, cb_data);
193 SR_PRIV int mastech_mas345_receive_data(int fd, int revents, void *cb_data)
195 struct metex14_info info;
196 return receive_data(fd, revents, MASTECH_MAS345, &info, cb_data);
199 SR_PRIV int va_va18b_receive_data(int fd, int revents, void *cb_data)
201 struct fs9721_info info;
202 return receive_data(fd, revents, VA_VA18B, &info, cb_data);
205 SR_PRIV int metex_m3640d_receive_data(int fd, int revents, void *cb_data)
207 struct metex14_info info;
208 return receive_data(fd, revents, METEX_M3640D, &info, cb_data);