]> sigrok.org Git - libsigrok.git/blob - hardware/serial-dmm/protocol.c
serial-dmm: Drop unneeded g_try_malloc0().
[libsigrok.git] / hardware / serial-dmm / protocol.c
1 /*
2  * This file is part of the sigrok 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
30 static 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
38 SR_PRIV void dmm_details_dt4000zc(struct sr_datafeed_analog *analog, void *info)
39 {
40         dmm_details_tp4000zc(analog, info); /* Same as TP4000ZC. */
41 }
42
43 SR_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;
52                 analog->unit = SR_UNIT_CELSIUS;
53         }
54 }
55
56 SR_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;
65                 analog->unit = SR_UNIT_CELSIUS;
66         }
67 }
68
69 SR_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
88 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
89                           int dmm, void *info)
90 {
91         float floatval;
92         struct sr_datafeed_packet packet;
93         struct sr_datafeed_analog analog;
94         struct dev_context *devc;
95
96         log_dmm_packet(buf);
97         devc = sdi->priv;
98
99         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
100
101         analog.probes = sdi->probes;
102         analog.num_samples = 1;
103         analog.mq = -1;
104
105         dmms[dmm].packet_parse(buf, &floatval, &analog, info);
106         analog.data = &floatval;
107
108         /* If this DMM needs additional handling, call the resp. function. */
109         if (dmms[dmm].dmm_details)
110                 dmms[dmm].dmm_details(&analog, info);
111
112         if (analog.mq != -1) {
113                 /* Got a measurement. */
114                 packet.type = SR_DF_ANALOG;
115                 packet.payload = &analog;
116                 sr_session_send(devc->cb_data, &packet);
117                 devc->num_samples++;
118         }
119 }
120
121 static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info)
122 {
123         struct dev_context *devc;
124         int len, i, offset = 0;
125
126         devc = sdi->priv;
127
128         /* Try to get as much data as the buffer can hold. */
129         len = DMM_BUFSIZE - devc->buflen;
130         len = serial_read(devc->serial, devc->buf + devc->buflen, len);
131         if (len < 1) {
132                 sr_err("Serial port read error: %d.", len);
133                 return;
134         }
135         devc->buflen += len;
136
137         /* Now look for packets in that data. */
138         while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
139                 if (dmms[dmm].packet_valid(devc->buf + offset)) {
140                         handle_packet(devc->buf + offset, sdi, dmm, info);
141                         offset += dmms[dmm].packet_size;
142                 } else {
143                         offset++;
144                 }
145         }
146
147         /* If we have any data left, move it to the beginning of our buffer. */
148         for (i = 0; i < devc->buflen - offset; i++)
149                 devc->buf[i] = devc->buf[offset + i];
150         devc->buflen -= offset;
151 }
152
153 static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
154 {
155         struct sr_dev_inst *sdi;
156         struct dev_context *devc;
157         int64_t time;
158         int ret;
159
160         (void)fd;
161
162         if (!(sdi = cb_data))
163                 return TRUE;
164
165         if (!(devc = sdi->priv))
166                 return TRUE;
167
168         if (revents == G_IO_IN) {
169                 /* Serial data arrived. */
170                 handle_new_data(sdi, dmm, info);
171         } else {
172                 /* Timeout, send another packet request (if DMM needs it). */
173                 if (dmms[dmm].packet_request) {
174                         ret = dmms[dmm].packet_request(devc->serial);
175                         if (ret < 0) {
176                                 sr_err("Failed to request packet: %d.", ret);
177                                 return FALSE;
178                         }
179                 }
180         }
181
182         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
183                 sr_info("Requested number of samples reached.");
184                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
185                 return TRUE;
186         }
187
188         if (devc->limit_msec) {
189                 time = (g_get_monotonic_time() - devc->starttime) / 1000;
190                 if (time > (int64_t)devc->limit_msec) {
191                         sr_info("Requested time limit reached.");
192                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
193                         return TRUE;
194                 }
195         }
196
197         return TRUE;
198 }
199
200 #define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \
201 SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
202         struct DMM_DRIVER##_info info; \
203         return receive_data(fd, revents, ID_UPPER, &info, cb_data); }
204
205 /* Driver-specific receive_data() wrappers */
206 RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721)
207 RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721)
208 RECEIVE_DATA(METEX_ME31, metex14)
209 RECEIVE_DATA(PEAKTECH_3410, metex14)
210 RECEIVE_DATA(MASTECH_MAS345, metex14)
211 RECEIVE_DATA(VA_VA18B, fs9721)
212 RECEIVE_DATA(METEX_M3640D, metex14)
213 RECEIVE_DATA(PEAKTECH_4370, metex14)
214 RECEIVE_DATA(PCE_PCE_DM32, fs9721)
215 RECEIVE_DATA(RADIOSHACK_22_168, metex14)
216 RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd)
217 RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721)
218 RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721)
219 RECEIVE_DATA(UNI_T_UT61E_SER, es51922)