]> sigrok.org Git - libsigrok.git/blob - hardware/agilent-dmm/api.c
Add and use std_session_send_df_header().
[libsigrok.git] / hardware / agilent-dmm / api.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <glib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28 #include "agilent-dmm.h"
29
30 static const int hwopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33         0,
34 };
35
36 static const int hwcaps[] = {
37         SR_CONF_MULTIMETER,
38         SR_CONF_LIMIT_SAMPLES,
39         SR_CONF_LIMIT_MSEC,
40         SR_CONF_CONTINUOUS,
41         0,
42 };
43
44 extern const struct agdmm_job agdmm_jobs_u123x[];
45 extern const struct agdmm_recv agdmm_recvs_u123x[];
46 extern const struct agdmm_job agdmm_jobs_u125x[];
47 extern const struct agdmm_recv agdmm_recvs_u125x[];
48
49 /* This works on all the Agilent U12xxA series, although the
50  * U127xA can apparently also run at 19200/8n1. */
51 #define SERIALCOMM "9600/8n1"
52
53 static const struct agdmm_profile supported_agdmm[] = {
54         { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x },
55         { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x },
56         { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x },
57         { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x },
58         { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x },
59         { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x },
60         { 0, NULL, NULL, NULL }
61 };
62
63 SR_PRIV struct sr_dev_driver agdmm_driver_info;
64 static struct sr_dev_driver *di = &agdmm_driver_info;
65
66 /* Properly close and free all devices. */
67 static int clear_instances(void)
68 {
69         struct sr_dev_inst *sdi;
70         struct drv_context *drvc;
71         struct dev_context *devc;
72         GSList *l;
73
74         if (!(drvc = di->priv))
75                 return SR_OK;
76
77         drvc = di->priv;
78         for (l = drvc->instances; l; l = l->next) {
79                 if (!(sdi = l->data))
80                         continue;
81                 if (!(devc = sdi->priv))
82                         continue;
83                 sr_serial_dev_inst_free(devc->serial);
84                 sr_dev_inst_free(sdi);
85         }
86         g_slist_free(drvc->instances);
87         drvc->instances = NULL;
88
89         return SR_OK;
90 }
91
92 static int hw_init(struct sr_context *sr_ctx)
93 {
94         return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
95 }
96
97 static GSList *hw_scan(GSList *options)
98 {
99         struct sr_dev_inst *sdi;
100         struct drv_context *drvc;
101         struct dev_context *devc;
102         struct sr_config *src;
103         struct sr_probe *probe;
104         struct sr_serial_dev_inst *serial;
105         GSList *l, *devices;
106         int len, i;
107         const char *conn, *serialcomm;
108         char *buf, **tokens;
109
110         drvc = di->priv;
111         drvc->instances = NULL;
112
113         devices = NULL;
114         conn = serialcomm = NULL;
115         for (l = options; l; l = l->next) {
116                 src = l->data;
117                 switch (src->key) {
118                 case SR_CONF_CONN:
119                         conn = src->value;
120                         break;
121                 case SR_CONF_SERIALCOMM:
122                         serialcomm = src->value;
123                         break;
124                 }
125         }
126         if (!conn)
127                 return NULL;
128         if (!serialcomm)
129                 serialcomm = SERIALCOMM;
130
131         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
132                 return NULL;
133
134         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
135                 return NULL;
136
137         serial_flush(serial);
138         if (serial_write(serial, "*IDN?\r\n", 7) == -1) {
139                 sr_err("Unable to send identification string: %s.",
140                        strerror(errno));
141                 return NULL;
142         }
143
144         len = 128;
145         if (!(buf = g_try_malloc(len))) {
146                 sr_err("Serial buffer malloc failed.");
147                 return NULL;
148         }
149         serial_readline(serial, &buf, &len, 150);
150         if (!len)
151                 return NULL;
152
153         tokens = g_strsplit(buf, ",", 4);
154         if (!strcmp("Agilent Technologies", tokens[0])
155                         && tokens[2] && tokens[3]) {
156                 for (i = 0; supported_agdmm[i].model; i++) {
157                         if (strcmp(supported_agdmm[i].modelname, tokens[1]))
158                                 continue;
159                         if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, tokens[0],
160                                         tokens[1], tokens[3])))
161                                 return NULL;
162                         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
163                                 sr_err("Device context malloc failed.");
164                                 return NULL;
165                         }
166                         devc->profile = &supported_agdmm[i];
167                         devc->serial = serial;
168                         devc->cur_mq = -1;
169                         sdi->priv = devc;
170                         sdi->driver = di;
171                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
172                                 return NULL;
173                         sdi->probes = g_slist_append(sdi->probes, probe);
174                         drvc->instances = g_slist_append(drvc->instances, sdi);
175                         devices = g_slist_append(devices, sdi);
176                         break;
177                 }
178         }
179         g_strfreev(tokens);
180         g_free(buf);
181
182         serial_close(serial);
183         if (!devices)
184                 sr_serial_dev_inst_free(serial);
185
186         return devices;
187 }
188
189 static GSList *hw_dev_list(void)
190 {
191         return ((struct drv_context *)(di->priv))->instances;
192 }
193
194 static int hw_dev_open(struct sr_dev_inst *sdi)
195 {
196         struct dev_context *devc;
197
198         if (!(devc = sdi->priv)) {
199                 sr_err("sdi->priv was NULL.");
200                 return SR_ERR_BUG;
201         }
202
203         if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
204                 return SR_ERR;
205
206         sdi->status = SR_ST_ACTIVE;
207
208         return SR_OK;
209 }
210
211 static int hw_dev_close(struct sr_dev_inst *sdi)
212 {
213         struct dev_context *devc;
214
215         devc = sdi->priv;
216
217         if (devc->serial && devc->serial->fd != -1) {
218                 serial_close(devc->serial);
219                 sdi->status = SR_ST_INACTIVE;
220         }
221
222         return SR_OK;
223 }
224
225 static int hw_cleanup(void)
226 {
227
228         clear_instances();
229
230         return SR_OK;
231 }
232
233 static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
234 {
235         struct dev_context *devc;
236
237         if (sdi->status != SR_ST_ACTIVE)
238                 return SR_ERR;
239
240         if (!(devc = sdi->priv)) {
241                 sr_err("sdi->priv was NULL.");
242                 return SR_ERR_BUG;
243         }
244
245         switch (id) {
246         case SR_CONF_LIMIT_MSEC:
247                 /* TODO: not yet implemented */
248                 if (*(const uint64_t *)value == 0) {
249                         sr_err("LIMIT_MSEC can't be 0.");
250                         return SR_ERR;
251                 }
252                 devc->limit_msec = *(const uint64_t *)value;
253                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
254                        devc->limit_msec);
255                 break;
256         case SR_CONF_LIMIT_SAMPLES:
257                 devc->limit_samples = *(const uint64_t *)value;
258                 sr_dbg("Setting sample limit to %" PRIu64 ".",
259                        devc->limit_samples);
260                 break;
261         default:
262                 sr_err("Unknown capability: %d.", id);
263                 return SR_ERR;
264                 break;
265         }
266
267         return SR_OK;
268 }
269
270 static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
271 {
272
273         (void)sdi;
274
275         switch (key) {
276         case SR_CONF_SCAN_OPTIONS:
277                 *data = hwopts;
278                 break;
279         case SR_CONF_DEVICE_OPTIONS:
280                 *data = hwcaps;
281                 break;
282         default:
283                 return SR_ERR_ARG;
284         }
285
286         return SR_OK;
287 }
288
289 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
290                 void *cb_data)
291 {
292         struct dev_context *devc;
293
294         if (!(devc = sdi->priv)) {
295                 sr_err("sdi->priv was NULL.");
296                 return SR_ERR_BUG;
297         }
298
299         devc->cb_data = cb_data;
300
301         /* Send header packet to the session bus. */
302         std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
303
304         /* Poll every 100ms, or whenever some data comes in. */
305         sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
306
307         return SR_OK;
308 }
309
310 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
311 {
312         struct sr_datafeed_packet packet;
313         struct dev_context *devc;
314
315         if (sdi->status != SR_ST_ACTIVE)
316                 return SR_ERR;
317
318         if (!(devc = sdi->priv)) {
319                 sr_err("sdi->priv was NULL.");
320                 return SR_ERR_BUG;
321         }
322
323         sr_dbg("Stopping acquisition.");
324
325         sr_source_remove(devc->serial->fd);
326         hw_dev_close((struct sr_dev_inst *)sdi);
327
328         /* Send end packet to the session bus. */
329         sr_dbg("Sending SR_DF_END.");
330         packet.type = SR_DF_END;
331         sr_session_send(cb_data, &packet);
332
333
334         return SR_OK;
335 }
336
337 SR_PRIV struct sr_dev_driver agdmm_driver_info = {
338         .name = "agilent-dmm",
339         .longname = "Agilent U12xx series DMMs",
340         .api_version = 1,
341         .init = hw_init,
342         .cleanup = hw_cleanup,
343         .scan = hw_scan,
344         .dev_list = hw_dev_list,
345         .dev_clear = clear_instances,
346         .config_set = config_set,
347         .config_list = config_list,
348         .dev_open = hw_dev_open,
349         .dev_close = hw_dev_close,
350         .dev_acquisition_start = hw_dev_acquisition_start,
351         .dev_acquisition_stop = hw_dev_acquisition_stop,
352         .priv = NULL,
353 };