]> sigrok.org Git - libsigrok.git/blame - hardware/serial-dmm/protocol.c
serial-dmm: Use a macro for driver struct simplification.
[libsigrok.git] / hardware / serial-dmm / protocol.c
CommitLineData
7dc55d93
AG
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
729b01f9 5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
7dc55d93
AG
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 <glib.h>
7dc55d93
AG
22#include <stdlib.h>
23#include <math.h>
24#include <string.h>
25#include <errno.h>
bbabddbd
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "protocol.h"
7dc55d93 29
6bef68a7
UH
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",
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]);
36}
37
f086b830
UH
38SR_PRIV void dmm_details_dt4000zc(struct sr_datafeed_analog *analog, void *info)
39{
40 dmm_details_tp4000zc(analog, info); /* Same as TP4000ZC. */
41}
42
729b01f9
UH
43SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
44{
45 struct fs9721_info *info_local;
46
47 info_local = (struct fs9721_info *)info;
48
49 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
50 if (info_local->is_c2c1_10) {
51 analog->mq = SR_MQ_TEMPERATURE;
6dca2f16
UH
52 analog->unit = SR_UNIT_CELSIUS;
53 }
54}
55
56SR_PRIV void dmm_details_va18b(struct sr_datafeed_analog *analog, void *info)
57{
58 struct fs9721_info *info_local;
59
60 info_local = (struct fs9721_info *)info;
61
62 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature. */
63 if (info_local->is_c2c1_01) {
64 analog->mq = SR_MQ_TEMPERATURE;
729b01f9
UH
65 analog->unit = SR_UNIT_CELSIUS;
66 }
67}
68
d4bd66a0
UH
69SR_PRIV void dmm_details_pce_dm32(struct sr_datafeed_analog *analog, void *info)
70{
71 struct fs9721_info *info_local;
72
73 info_local = (struct fs9721_info *)info;
74
75 /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (F). */
76 if (info_local->is_c2c1_01) {
77 analog->mq = SR_MQ_TEMPERATURE;
78 analog->unit = SR_UNIT_FAHRENHEIT;
79 }
80
81 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */
82 if (info_local->is_c2c1_10) {
83 analog->mq = SR_MQ_TEMPERATURE;
84 analog->unit = SR_UNIT_CELSIUS;
85 }
86}
87
729b01f9
UH
88static void handle_packet(const uint8_t *buf, struct dev_context *devc,
89 int dmm, void *info)
7dc55d93 90{
8c1adf37 91 float floatval;
7dc55d93
AG
92 struct sr_datafeed_packet packet;
93 struct sr_datafeed_analog *analog;
94
6bef68a7
UH
95 log_dmm_packet(buf);
96
bbabddbd 97 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
8c1adf37 98 sr_err("Analog packet malloc failed.");
7dc55d93
AG
99 return;
100 }
bbabddbd 101
bbabddbd 102 analog->num_samples = 1;
7dc55d93
AG
103 analog->mq = -1;
104
729b01f9 105 dmms[dmm].packet_parse(buf, &floatval, analog, info);
d84fc9cb 106 analog->data = &floatval;
be5c1d3b 107
ce3777ad
UH
108 if (dmms[dmm].dmm_details)
109 dmms[dmm].dmm_details(analog, info);
7dc55d93 110
7dc55d93
AG
111 if (analog->mq != -1) {
112 /* Got a measurement. */
7dc55d93
AG
113 packet.type = SR_DF_ANALOG;
114 packet.payload = analog;
115 sr_session_send(devc->cb_data, &packet);
116 devc->num_samples++;
117 }
bbabddbd 118
7dc55d93
AG
119 g_free(analog);
120}
121
729b01f9 122static void handle_new_data(struct dev_context *devc, int dmm, void *info)
7dc55d93 123{
bbabddbd 124 int len, i, offset = 0;
bbabddbd
UH
125
126 /* Try to get as much data as the buffer can hold. */
7dc55d93 127 len = DMM_BUFSIZE - devc->buflen;
d35afa87 128 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
7dc55d93 129 if (len < 1) {
bbabddbd 130 sr_err("Serial port read error: %d.", len);
7dc55d93
AG
131 return;
132 }
133 devc->buflen += len;
134
bbabddbd 135 /* Now look for packets in that data. */
729b01f9
UH
136 while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
137 if (dmms[dmm].packet_valid(devc->buf + offset)) {
138 handle_packet(devc->buf + offset, devc, dmm, info);
139 offset += dmms[dmm].packet_size;
7dc55d93
AG
140 } else {
141 offset++;
142 }
143 }
144
bbabddbd
UH
145 /* If we have any data left, move it to the beginning of our buffer. */
146 for (i = 0; i < devc->buflen - offset; i++)
7dc55d93
AG
147 devc->buf[i] = devc->buf[offset + i];
148 devc->buflen -= offset;
149}
150
729b01f9 151static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
7dc55d93 152{
642e9d62 153 struct sr_dev_inst *sdi;
7dc55d93 154 struct dev_context *devc;
ce3777ad 155 int ret;
7dc55d93 156
d35afa87
BV
157 (void)fd;
158
7dc55d93
AG
159 if (!(sdi = cb_data))
160 return TRUE;
161
162 if (!(devc = sdi->priv))
163 return TRUE;
164
bbabddbd 165 if (revents == G_IO_IN) {
7dc55d93 166 /* Serial data arrived. */
729b01f9 167 handle_new_data(devc, dmm, info);
ce3777ad
UH
168 } else {
169 /* Timeout, send another packet request (if DMM needs it). */
170 if (dmms[dmm].packet_request) {
171 ret = dmms[dmm].packet_request(devc->serial);
172 if (ret < 0) {
173 sr_err("Failed to request packet: %d.", ret);
174 return FALSE;
175 }
176 }
7dc55d93
AG
177 }
178
179 if (devc->num_samples >= devc->limit_samples) {
6bef68a7 180 sr_info("Requested number of samples reached, stopping.");
7dc55d93
AG
181 sdi->driver->dev_acquisition_stop(sdi, cb_data);
182 return TRUE;
183 }
184
185 return TRUE;
186}
729b01f9 187
f086b830
UH
188SR_PRIV int digitek_dt4000zc_receive_data(int fd, int revents, void *cb_data)
189{
190 struct fs9721_info info;
f086b830
UH
191 return receive_data(fd, revents, DIGITEK_DT4000ZC, &info, cb_data);
192}
193
729b01f9
UH
194SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
195{
196 struct fs9721_info info;
729b01f9
UH
197 return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
198}
ce3777ad
UH
199
200SR_PRIV int metex_me31_receive_data(int fd, int revents, void *cb_data)
201{
202 struct metex14_info info;
ce3777ad
UH
203 return receive_data(fd, revents, METEX_ME31, &info, cb_data);
204}
f0ac4929
UH
205
206SR_PRIV int peaktech_3410_receive_data(int fd, int revents, void *cb_data)
207{
208 struct metex14_info info;
f0ac4929
UH
209 return receive_data(fd, revents, PEAKTECH_3410, &info, cb_data);
210}
5887c9cc
UH
211
212SR_PRIV int mastech_mas345_receive_data(int fd, int revents, void *cb_data)
213{
214 struct metex14_info info;
5887c9cc
UH
215 return receive_data(fd, revents, MASTECH_MAS345, &info, cb_data);
216}
6dca2f16
UH
217
218SR_PRIV int va_va18b_receive_data(int fd, int revents, void *cb_data)
219{
220 struct fs9721_info info;
6dca2f16
UH
221 return receive_data(fd, revents, VA_VA18B, &info, cb_data);
222}
a53da082
UH
223
224SR_PRIV int metex_m3640d_receive_data(int fd, int revents, void *cb_data)
225{
226 struct metex14_info info;
227 return receive_data(fd, revents, METEX_M3640D, &info, cb_data);
228}
a376ffea
UH
229
230SR_PRIV int peaktech_4370_receive_data(int fd, int revents, void *cb_data)
231{
232 struct metex14_info info;
233 return receive_data(fd, revents, PEAKTECH_4370, &info, cb_data);
234}
d4bd66a0
UH
235
236SR_PRIV int pce_pce_dm32_receive_data(int fd, int revents, void *cb_data)
237{
238 struct fs9721_info info;
239 return receive_data(fd, revents, PCE_PCE_DM32, &info, cb_data);
240}
d5ce233f
AG
241
242SR_PRIV int radioshack_22_168_receive_data(int fd, int revents, void *cb_data)
243{
244 struct metex14_info info;
245 return receive_data(fd, revents, RADIOSHACK_22_168, &info, cb_data);
246}
21829e67
AG
247
248SR_PRIV int radioshack_22_812_receive_data(int fd, int revents, void *cb_data)
249{
250 struct metex14_info info;
251 return receive_data(fd, revents, RADIOSHACK_22_812, &info, cb_data);
252}