]> sigrok.org Git - libsigrok.git/blob - hardware/uni-t-dmm/api.c
GPL headers: Use correct project name.
[libsigrok.git] / hardware / uni-t-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 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 <stdlib.h>
22 #include <string.h>
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 #define UNI_T_UT_D04_NEW "1a86.e008"
28
29 static const int32_t hwopts[] = {
30         SR_CONF_CONN,
31 };
32
33 static const int32_t hwcaps[] = {
34         SR_CONF_MULTIMETER,
35         SR_CONF_LIMIT_SAMPLES,
36         SR_CONF_LIMIT_MSEC,
37         SR_CONF_CONTINUOUS,
38 };
39
40 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
41 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
42
43 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
44 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
45
46 /* After hw_init() this will point to a device-specific entry (see above). */
47 static struct sr_dev_driver *di = NULL;
48
49 static int clear_instances(void)
50 {
51         /* TODO: Use common code later. */
52
53         return SR_OK;
54 }
55
56 static int hw_init(struct sr_context *sr_ctx, int dmm)
57 {
58         if (dmm == UNI_T_UT61D)
59                 di = di_ut61d;
60         else if (dmm == VOLTCRAFT_VC820)
61                 di = di_vc820;
62         sr_dbg("Selected '%s' subdriver.", di->name);
63
64         return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
65 }
66
67 static int hw_init_ut61d(struct sr_context *sr_ctx)
68 {
69         return hw_init(sr_ctx, UNI_T_UT61D);
70 }
71
72 static int hw_init_vc820(struct sr_context *sr_ctx)
73 {
74         return hw_init(sr_ctx, VOLTCRAFT_VC820);
75 }
76
77 static GSList *hw_scan(GSList *options)
78 {
79         GSList *usb_devices, *devices, *l;
80         struct sr_dev_inst *sdi;
81         struct dev_context *devc;
82         struct drv_context *drvc;
83         struct sr_usb_dev_inst *usb;
84         struct sr_config *src;
85         struct sr_probe *probe;
86         const char *conn;
87
88         (void)options;
89
90         drvc = di->priv;
91
92         /* USB scan is always authoritative. */
93         clear_instances();
94
95         conn = NULL;
96         for (l = options; l; l = l->next) {
97                 src = l->data;
98                 switch (src->key) {
99                 case SR_CONF_CONN:
100                         conn = g_variant_get_string(src->data, NULL);
101                         break;
102                 }
103         }
104         if (!conn)
105                 conn = UNI_T_UT_D04_NEW;
106
107         devices = NULL;
108         if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
109                 g_slist_free_full(usb_devices, g_free);
110                 return NULL;
111         }
112
113         for (l = usb_devices; l; l = l->next) {
114                 usb = l->data;
115
116                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117                         sr_err("Device context malloc failed.");
118                         return NULL;
119                 }
120
121                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
122                                 di->longname, NULL, NULL))) {
123                         sr_err("sr_dev_inst_new returned NULL.");
124                         return NULL;
125                 }
126                 sdi->priv = devc;
127                 sdi->driver = di;
128                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
129                         return NULL;
130                 sdi->probes = g_slist_append(sdi->probes, probe);
131
132                 devc->usb = usb;
133
134                 drvc->instances = g_slist_append(drvc->instances, sdi);
135                 devices = g_slist_append(devices, sdi);
136         }
137
138         return devices;
139 }
140
141 static GSList *hw_dev_list(void)
142 {
143         return ((struct drv_context *)(di->priv))->instances;
144 }
145
146 static int hw_dev_open(struct sr_dev_inst *sdi)
147 {
148         struct drv_context *drvc;
149         struct dev_context *devc;
150
151         drvc = di->priv;
152         devc = sdi->priv;
153
154         return sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb);
155 }
156
157 static int hw_dev_close(struct sr_dev_inst *sdi)
158 {
159         (void)sdi;
160
161         /* TODO */
162
163         return SR_OK;
164 }
165
166 static int hw_cleanup(void)
167 {
168         clear_instances();
169
170         return SR_OK;
171 }
172
173 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
174 {
175         struct dev_context *devc;
176
177         devc = sdi->priv;
178
179         switch (id) {
180         case SR_CONF_LIMIT_MSEC:
181                 /* TODO: Not yet implemented. */
182                 if (g_variant_get_uint64(data) == 0) {
183                         sr_err("Time limit cannot be 0.");
184                         return SR_ERR;
185                 }
186                 devc->limit_msec = g_variant_get_uint64(data);
187                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
188                        devc->limit_msec);
189                 break;
190         case SR_CONF_LIMIT_SAMPLES:
191                 if (g_variant_get_uint64(data) == 0) {
192                         sr_err("Sample limit cannot be 0.");
193                         return SR_ERR;
194                 }
195                 devc->limit_samples = g_variant_get_uint64(data);
196                 sr_dbg("Setting sample limit to %" PRIu64 ".",
197                        devc->limit_samples);
198                 break;
199         default:
200                 return SR_ERR_NA;
201         }
202
203         return SR_OK;
204 }
205
206 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
207 {
208
209         (void)sdi;
210
211         switch (key) {
212         case SR_CONF_SCAN_OPTIONS:
213                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
214                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
215                 break;
216         case SR_CONF_DEVICE_OPTIONS:
217                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
218                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
219                 break;
220         default:
221                 return SR_ERR_NA;
222         }
223
224         return SR_OK;
225 }
226
227 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
228                                     void *cb_data)
229 {
230         struct dev_context *devc;
231
232         devc = sdi->priv;
233
234         devc->cb_data = cb_data;
235
236         /* Send header packet to the session bus. */
237         std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
238
239         if (!strcmp(di->name, "uni-t-ut61d")) {
240                 sr_source_add(0, 0, 10 /* poll_timeout */,
241                               uni_t_ut61d_receive_data, (void *)sdi);
242         } else if (!strcmp(di->name, "voltcraft-vc820")) {
243                 sr_source_add(0, 0, 10 /* poll_timeout */,
244                               voltcraft_vc820_receive_data, (void *)sdi);
245         }
246
247         return SR_OK;
248 }
249
250 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
251 {
252         struct sr_datafeed_packet packet;
253
254         (void)sdi;
255
256         sr_dbg("Stopping acquisition.");
257
258         /* Send end packet to the session bus. */
259         sr_dbg("Sending SR_DF_END.");
260         packet.type = SR_DF_END;
261         sr_session_send(cb_data, &packet);
262
263         /* TODO? */
264         sr_source_remove(0);
265
266         return SR_OK;
267 }
268
269 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
270         .name = "uni-t-ut61d",
271         .longname = "UNI-T UT61D",
272         .api_version = 1,
273         .init = hw_init_ut61d,
274         .cleanup = hw_cleanup,
275         .scan = hw_scan,
276         .dev_list = hw_dev_list,
277         .dev_clear = clear_instances,
278         .config_get = NULL,
279         .config_set = config_set,
280         .config_list = config_list,
281         .dev_open = hw_dev_open,
282         .dev_close = hw_dev_close,
283         .dev_acquisition_start = hw_dev_acquisition_start,
284         .dev_acquisition_stop = hw_dev_acquisition_stop,
285         .priv = NULL,
286 };
287
288 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
289         .name = "voltcraft-vc820",
290         .longname = "Voltcraft VC-820",
291         .api_version = 1,
292         .init = hw_init_vc820,
293         .cleanup = hw_cleanup,
294         .scan = hw_scan,
295         .dev_list = hw_dev_list,
296         .dev_clear = clear_instances,
297         .config_get = NULL,
298         .config_set = config_set,
299         .config_list = config_list,
300         .dev_open = hw_dev_open,
301         .dev_close = hw_dev_close,
302         .dev_acquisition_start = hw_dev_acquisition_start,
303         .dev_acquisition_stop = hw_dev_acquisition_stop,
304         .priv = NULL,
305 };