]> sigrok.org Git - libsigrok.git/blob - src/hardware/uni-t-dmm/api.c
Consistently don't check sdi->priv in dev_acquisition_start().
[libsigrok.git] / src / hardware / uni-t-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26 #include "protocol.h"
27
28 #define UNI_T_UT_D04_NEW "1a86.e008"
29
30 static const uint32_t scanopts[] = {
31         SR_CONF_CONN,
32 };
33
34 static const uint32_t devopts[] = {
35         SR_CONF_MULTIMETER,
36         SR_CONF_CONTINUOUS,
37         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
38         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
39 };
40
41 /*
42  * Note 1: The actual baudrate of the Cyrustek ES519xx chip used in this DMM
43  * is 19230. However, the WCH CH9325 chip (UART to USB/HID) used in (some
44  * versions of) the UNI-T UT-D04 cable doesn't support 19230 baud. It only
45  * supports 19200, and setting an unsupported baudrate will result in the
46  * default of 2400 being used (which will not work with this DMM, of course).
47  */
48
49 static int dev_clear(const struct sr_dev_driver *di)
50 {
51         return std_dev_clear(di, NULL);
52 }
53
54 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
55 {
56         return std_init(sr_ctx, di, LOG_PREFIX);
57 }
58
59 static GSList *scan(struct sr_dev_driver *di, GSList *options)
60 {
61         GSList *usb_devices, *devices, *l;
62         struct sr_dev_inst *sdi;
63         struct dev_context *devc;
64         struct drv_context *drvc;
65         struct dmm_info *dmm;
66         struct sr_usb_dev_inst *usb;
67         struct sr_config *src;
68         const char *conn;
69
70         drvc = di->context;
71         dmm = (struct dmm_info *)di;
72
73         conn = NULL;
74         for (l = options; l; l = l->next) {
75                 src = l->data;
76                 switch (src->key) {
77                 case SR_CONF_CONN:
78                         conn = g_variant_get_string(src->data, NULL);
79                         break;
80                 }
81         }
82         if (!conn)
83                 return NULL;
84
85         devices = NULL;
86         if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
87                 g_slist_free_full(usb_devices, g_free);
88                 return NULL;
89         }
90
91         for (l = usb_devices; l; l = l->next) {
92                 usb = l->data;
93                 devc = g_malloc0(sizeof(struct dev_context));
94                 devc->first_run = TRUE;
95                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
96                 sdi->status = SR_ST_INACTIVE;
97                 sdi->vendor = g_strdup(dmm->vendor);
98                 sdi->model = g_strdup(dmm->device);
99                 sdi->priv = devc;
100                 sdi->driver = di;
101                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
102                 sdi->inst_type = SR_INST_USB;
103                 sdi->conn = usb;
104                 drvc->instances = g_slist_append(drvc->instances, sdi);
105                 devices = g_slist_append(devices, sdi);
106         }
107
108         return devices;
109 }
110
111 static GSList *dev_list(const struct sr_dev_driver *di)
112 {
113         return ((struct drv_context *)(di->context))->instances;
114 }
115
116 static int dev_open(struct sr_dev_inst *sdi)
117 {
118         struct sr_dev_driver *di;
119         struct drv_context *drvc;
120         struct sr_usb_dev_inst *usb;
121         int ret;
122
123         di = sdi->driver;
124         drvc = di->context;
125         usb = sdi->conn;
126
127         if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
128                 sdi->status = SR_ST_ACTIVE;
129
130         return ret;
131 }
132
133 static int dev_close(struct sr_dev_inst *sdi)
134 {
135         /* TODO */
136
137         sdi->status = SR_ST_INACTIVE;
138
139         return SR_OK;
140 }
141
142 static int cleanup(const struct sr_dev_driver *di)
143 {
144         return dev_clear(di);
145 }
146
147 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
148                 const struct sr_channel_group *cg)
149 {
150         struct dev_context *devc;
151
152         (void)cg;
153
154         devc = sdi->priv;
155
156         switch (key) {
157         case SR_CONF_LIMIT_MSEC:
158                 devc->limit_msec = g_variant_get_uint64(data);
159                 break;
160         case SR_CONF_LIMIT_SAMPLES:
161                 devc->limit_samples = g_variant_get_uint64(data);
162                 break;
163         default:
164                 return SR_ERR_NA;
165         }
166
167         return SR_OK;
168 }
169
170 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
171                 const struct sr_channel_group *cg)
172 {
173         (void)sdi;
174         (void)cg;
175
176         switch (key) {
177         case SR_CONF_SCAN_OPTIONS:
178                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
179                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
180                 break;
181         case SR_CONF_DEVICE_OPTIONS:
182                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
183                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
184                 break;
185         default:
186                 return SR_ERR_NA;
187         }
188
189         return SR_OK;
190 }
191
192 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
193 {
194         struct dev_context *devc;
195
196         devc = sdi->priv;
197         devc->cb_data = cb_data;
198         devc->starttime = g_get_monotonic_time();
199
200         std_session_send_df_header(sdi, LOG_PREFIX);
201
202         sr_session_source_add(sdi->session, -1, 0, 10 /* poll_timeout */,
203                       uni_t_dmm_receive_data, (void *)sdi);
204
205         return SR_OK;
206 }
207
208 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
209 {
210         (void)cb_data;
211
212         sr_dbg("Stopping acquisition.");
213         std_session_send_df_end(sdi, LOG_PREFIX);
214         sr_session_source_remove(sdi->session, -1);
215
216         return SR_OK;
217 }
218
219 #define DMM(ID, CHIPSET, VENDOR, MODEL, BAUDRATE, PACKETSIZE, \
220                         VALID, PARSE, DETAILS) \
221     &(struct dmm_info) { \
222                 { \
223                         .name = ID, \
224                         .longname = VENDOR " " MODEL, \
225                         .api_version = 1, \
226                         .init = init, \
227                         .cleanup = cleanup, \
228                         .scan = scan, \
229                         .dev_list = dev_list, \
230                         .dev_clear = dev_clear, \
231                         .config_get = NULL, \
232                         .config_set = config_set, \
233                         .config_list = config_list, \
234                         .dev_open = dev_open, \
235                         .dev_close = dev_close, \
236                         .dev_acquisition_start = dev_acquisition_start, \
237                         .dev_acquisition_stop = dev_acquisition_stop, \
238                         .context = NULL, \
239                 }, \
240                 VENDOR, MODEL, BAUDRATE, PACKETSIZE, \
241                 VALID, PARSE, DETAILS, sizeof(struct CHIPSET##_info) \
242         }
243
244 SR_PRIV const struct dmm_info *uni_t_dmm_drivers[] = {
245         DMM(
246                 "tecpel-dmm-8061", fs9721,
247                 "Tecpel", "DMM-8061", 2400,
248                 FS9721_PACKET_SIZE,
249                 sr_fs9721_packet_valid, sr_fs9721_parse,
250                 sr_fs9721_00_temp_c
251         ),
252         DMM(
253                 "uni-t-ut372", ut372,
254                 "UNI-T", "UT372", 2400,
255                 UT372_PACKET_SIZE,
256                 sr_ut372_packet_valid, sr_ut372_parse,
257                 NULL
258         ),
259         DMM(
260                 "uni-t-ut60a", fs9721,
261                 "UNI-T", "UT60A", 2400,
262                 FS9721_PACKET_SIZE,
263                 sr_fs9721_packet_valid, sr_fs9721_parse,
264                 NULL
265         ),
266         DMM(
267                 "uni-t-ut60e", fs9721,
268                 "UNI-T", "UT60E", 2400,
269                 FS9721_PACKET_SIZE,
270                 sr_fs9721_packet_valid, sr_fs9721_parse,
271                 sr_fs9721_00_temp_c
272         ),
273         DMM(
274                 "uni-t-ut60g", es519xx,
275                 /* The baudrate is actually 19230, see "Note 1" below. */
276                 "UNI-T", "UT60G", 19200,
277                 ES519XX_11B_PACKET_SIZE,
278                 sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse,
279                 NULL
280         ),
281         DMM(
282                 "uni-t-ut61b", fs9922,
283                 "UNI-T", "UT61B", 2400,
284                 FS9922_PACKET_SIZE,
285                 sr_fs9922_packet_valid, sr_fs9922_parse,
286                 NULL
287         ),
288         DMM(
289                 "uni-t-ut61c", fs9922,
290                 "UNI-T", "UT61C", 2400,
291                 FS9922_PACKET_SIZE,
292                 sr_fs9922_packet_valid, sr_fs9922_parse,
293                 NULL
294         ),
295         DMM(
296                 "uni-t-ut61d", fs9922,
297                 "UNI-T", "UT61D", 2400,
298                 FS9922_PACKET_SIZE,
299                 sr_fs9922_packet_valid, sr_fs9922_parse,
300                 NULL
301         ),
302         DMM(
303                 "uni-t-ut61e", es519xx,
304                 /* The baudrate is actually 19230, see "Note 1" below. */
305                 "UNI-T", "UT61E", 19200,
306                 ES519XX_14B_PACKET_SIZE,
307                 sr_es519xx_19200_14b_packet_valid, sr_es519xx_19200_14b_parse,
308                 NULL
309         ),
310         DMM(
311                 "uni-t-ut71a", ut71x,
312                 "UNI-T", "UT71A", 2400, UT71X_PACKET_SIZE,
313                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
314         ),
315         DMM(
316                 "uni-t-ut71b", ut71x,
317                 "UNI-T", "UT71B", 2400, UT71X_PACKET_SIZE,
318                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
319         ),
320         DMM(
321                 "uni-t-ut71c", ut71x,
322                 "UNI-T", "UT71C", 2400, UT71X_PACKET_SIZE,
323                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
324         ),
325         DMM(
326                 "uni-t-ut71d", ut71x,
327                 "UNI-T", "UT71D", 2400, UT71X_PACKET_SIZE,
328                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
329         ),
330         DMM(
331                 "uni-t-ut71e", ut71x,
332                 "UNI-T", "UT71E", 2400, UT71X_PACKET_SIZE,
333                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
334         ),
335         DMM(
336                 "voltcraft-vc820", fs9721,
337                 "Voltcraft", "VC-820", 2400,
338                 FS9721_PACKET_SIZE,
339                 sr_fs9721_packet_valid, sr_fs9721_parse,
340                 NULL
341         ),
342         DMM(
343                 "voltcraft-vc830", fs9922,
344                 /*
345                  * Note: The VC830 doesn't set the 'volt' and 'diode' bits of
346                  * the FS9922 protocol. Instead, it only sets the user-defined
347                  * bit "z1" to indicate "diode mode" and "voltage".
348                  */
349                 "Voltcraft", "VC-830", 2400,
350                 FS9922_PACKET_SIZE,
351                 sr_fs9922_packet_valid, sr_fs9922_parse,
352                 &sr_fs9922_z1_diode
353         ),
354         DMM(
355                 "voltcraft-vc840", fs9721,
356                 "Voltcraft", "VC-840", 2400,
357                 FS9721_PACKET_SIZE,
358                 sr_fs9721_packet_valid, sr_fs9721_parse,
359                 sr_fs9721_00_temp_c
360         ),
361         DMM(
362                 "voltcraft-vc870", vc870,
363                 "Voltcraft", "VC-870", 9600, VC870_PACKET_SIZE,
364                 sr_vc870_packet_valid, sr_vc870_parse, NULL
365         ),
366         DMM(
367                 "voltcraft-vc920", ut71x,
368                 "Voltcraft", "VC-920", 2400, UT71X_PACKET_SIZE,
369                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
370         ),
371         DMM(
372                 "voltcraft-vc940", ut71x,
373                 "Voltcraft", "VC-940", 2400, UT71X_PACKET_SIZE,
374                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
375         ),
376         DMM(
377                 "voltcraft-vc960", ut71x,
378                 "Voltcraft", "VC-960", 2400, UT71X_PACKET_SIZE,
379                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
380         ),
381         DMM(
382                 "tenma-72-7730", ut71x,
383                 "Tenma", "72-7730", 2400,
384                 UT71X_PACKET_SIZE,
385                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
386         ),
387         DMM(
388                 "tenma-72-7732", ut71x,
389                 "Tenma", "72-7732", 2400,
390                 UT71X_PACKET_SIZE,
391                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
392         ),
393         DMM(
394                 "tenma-72-9380a", ut71x,
395                 "Tenma", "72-9380A", 2400,
396                 UT71X_PACKET_SIZE,
397                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
398         ),
399         DMM(
400                 "tenma-72-7745", es519xx,
401                 "Tenma", "72-7745", 2400,
402                 FS9721_PACKET_SIZE,
403                 sr_fs9721_packet_valid, sr_fs9721_parse,
404                 sr_fs9721_00_temp_c
405         ),
406         DMM(
407                 "tenma-72-7750", es519xx,
408                 /* The baudrate is actually 19230, see "Note 1" below. */
409                 "Tenma", "72-7750", 19200,
410                 ES519XX_11B_PACKET_SIZE,
411                 sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse,
412                 NULL
413         ),
414         NULL
415 };