]> sigrok.org Git - libsigrok.git/blame - hardware/agilent-dmm/api.c
serial-dmm: Drop unneeded g_try_malloc0().
[libsigrok.git] / hardware / agilent-dmm / api.c
CommitLineData
e93cdf42
BV
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>
e93cdf42
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <string.h>
25#include <errno.h>
6f22a8ef
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "agilent-dmm.h"
e93cdf42
BV
29
30static const int hwopts[] = {
1953564a
BV
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
e93cdf42
BV
33 0,
34};
35
36static const int hwcaps[] = {
1953564a
BV
37 SR_CONF_MULTIMETER,
38 SR_CONF_LIMIT_SAMPLES,
39 SR_CONF_LIMIT_MSEC,
40 SR_CONF_CONTINUOUS,
e93cdf42
BV
41 0,
42};
43
8c0152f2
BV
44extern const struct agdmm_job agdmm_jobs_u123x[];
45extern const struct agdmm_recv agdmm_recvs_u123x[];
46extern const struct agdmm_job agdmm_jobs_u125x[];
47extern const struct agdmm_recv agdmm_recvs_u125x[];
e93cdf42 48
74ac7d7f
BV
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
e93cdf42 53static const struct agdmm_profile supported_agdmm[] = {
74ac7d7f
BV
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 }
e93cdf42
BV
61};
62
63SR_PRIV struct sr_dev_driver agdmm_driver_info;
64static struct sr_dev_driver *di = &agdmm_driver_info;
65
66/* Properly close and free all devices. */
67static 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
34f06b90 92static int hw_init(struct sr_context *sr_ctx)
e93cdf42 93{
063e7aef 94 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
e93cdf42
BV
95}
96
e93cdf42
BV
97static GSList *hw_scan(GSList *options)
98{
99 struct sr_dev_inst *sdi;
100 struct drv_context *drvc;
101 struct dev_context *devc;
1987b8d6 102 struct sr_config *src;
e93cdf42 103 struct sr_probe *probe;
109a3ba4 104 struct sr_serial_dev_inst *serial;
e93cdf42 105 GSList *l, *devices;
109a3ba4 106 int len, i;
e93cdf42
BV
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) {
1987b8d6
BV
116 src = l->data;
117 switch (src->key) {
1953564a 118 case SR_CONF_CONN:
1987b8d6 119 conn = src->value;
e93cdf42 120 break;
1953564a 121 case SR_CONF_SERIALCOMM:
1987b8d6 122 serialcomm = src->value;
e93cdf42
BV
123 break;
124 }
125 }
74ac7d7f 126 if (!conn)
e93cdf42 127 return NULL;
109a3ba4
BV
128 if (!serialcomm)
129 serialcomm = SERIALCOMM;
e93cdf42 130
109a3ba4 131 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
e93cdf42 132 return NULL;
e93cdf42 133
a54dd31e 134 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
e93cdf42 135 return NULL;
e93cdf42 136
109a3ba4
BV
137 serial_flush(serial);
138 if (serial_write(serial, "*IDN?\r\n", 7) == -1) {
38d326e8
UH
139 sr_err("Unable to send identification string: %s.",
140 strerror(errno));
e93cdf42
BV
141 return NULL;
142 }
143
144 len = 128;
886a52b6
UH
145 if (!(buf = g_try_malloc(len))) {
146 sr_err("Serial buffer malloc failed.");
147 return NULL;
148 }
109a3ba4 149 serial_readline(serial, &buf, &len, 150);
e93cdf42
BV
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)))) {
886a52b6 163 sr_err("Device context malloc failed.");
e93cdf42
BV
164 return NULL;
165 }
166 devc->profile = &supported_agdmm[i];
109a3ba4 167 devc->serial = serial;
e93cdf42
BV
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
109a3ba4
BV
182 serial_close(serial);
183 if (!devices)
184 sr_serial_dev_inst_free(serial);
e93cdf42
BV
185
186 return devices;
187}
188
189static GSList *hw_dev_list(void)
190{
191 struct drv_context *drvc;
192
193 drvc = di->priv;
194
195 return drvc->instances;
196}
197
198static int hw_dev_open(struct sr_dev_inst *sdi)
199{
200 struct dev_context *devc;
201
202 if (!(devc = sdi->priv)) {
38d326e8 203 sr_err("sdi->priv was NULL.");
e93cdf42
BV
204 return SR_ERR_BUG;
205 }
206
a54dd31e 207 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
e93cdf42 208 return SR_ERR;
109a3ba4 209
e93cdf42
BV
210 sdi->status = SR_ST_ACTIVE;
211
212 return SR_OK;
213}
214
215static int hw_dev_close(struct sr_dev_inst *sdi)
216{
217 struct dev_context *devc;
218
219 if (!(devc = sdi->priv)) {
38d326e8 220 sr_err("sdi->priv was NULL.");
e93cdf42
BV
221 return SR_ERR_BUG;
222 }
223
224 if (devc->serial && devc->serial->fd != -1) {
109a3ba4 225 serial_close(devc->serial);
e93cdf42
BV
226 sdi->status = SR_ST_INACTIVE;
227 }
228
229 return SR_OK;
230}
231
232static int hw_cleanup(void)
233{
234
235 clear_instances();
236
237 return SR_OK;
238}
239
035a1078 240static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
e93cdf42
BV
241{
242 struct dev_context *devc;
243
244 if (sdi->status != SR_ST_ACTIVE)
245 return SR_ERR;
246
247 if (!(devc = sdi->priv)) {
38d326e8 248 sr_err("sdi->priv was NULL.");
e93cdf42
BV
249 return SR_ERR_BUG;
250 }
251
035a1078 252 switch (id) {
1953564a 253 case SR_CONF_LIMIT_MSEC:
e93cdf42
BV
254 /* TODO: not yet implemented */
255 if (*(const uint64_t *)value == 0) {
38d326e8 256 sr_err("LIMIT_MSEC can't be 0.");
e93cdf42
BV
257 return SR_ERR;
258 }
259 devc->limit_msec = *(const uint64_t *)value;
38d326e8 260 sr_dbg("Setting time limit to %" PRIu64 "ms.",
e93cdf42
BV
261 devc->limit_msec);
262 break;
1953564a 263 case SR_CONF_LIMIT_SAMPLES:
e93cdf42 264 devc->limit_samples = *(const uint64_t *)value;
38d326e8 265 sr_dbg("Setting sample limit to %" PRIu64 ".",
e93cdf42
BV
266 devc->limit_samples);
267 break;
268 default:
035a1078 269 sr_err("Unknown capability: %d.", id);
e93cdf42
BV
270 return SR_ERR;
271 break;
272 }
273
274 return SR_OK;
275}
276
a1c743fc
BV
277static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
278{
279
280 (void)sdi;
281
282 switch (key) {
0d485e30
BV
283 case SR_CONF_SCAN_OPTIONS:
284 *data = hwopts;
285 break;
9a6517d1
BV
286 case SR_CONF_DEVICE_OPTIONS:
287 *data = hwcaps;
288 break;
a1c743fc
BV
289 default:
290 return SR_ERR_ARG;
291 }
292
293 return SR_OK;
294}
295
e93cdf42
BV
296static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
297 void *cb_data)
298{
299 struct sr_datafeed_packet packet;
300 struct sr_datafeed_header header;
e93cdf42
BV
301 struct dev_context *devc;
302
303 if (!(devc = sdi->priv)) {
38d326e8 304 sr_err("sdi->priv was NULL.");
e93cdf42
BV
305 return SR_ERR_BUG;
306 }
307
38d326e8 308 sr_dbg("Starting acquisition.");
e93cdf42
BV
309
310 devc->cb_data = cb_data;
311
312 /* Send header packet to the session bus. */
38d326e8 313 sr_dbg("Sending SR_DF_HEADER.");
e93cdf42
BV
314 packet.type = SR_DF_HEADER;
315 packet.payload = (uint8_t *)&header;
316 header.feed_version = 1;
317 gettimeofday(&header.starttime, NULL);
318 sr_session_send(devc->cb_data, &packet);
319
e93cdf42
BV
320 /* Poll every 100ms, or whenever some data comes in. */
321 sr_source_add(devc->serial->fd, G_IO_IN, 100, agdmm_receive_data, (void *)sdi);
322
323 return SR_OK;
324}
325
69b07d14 326static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
e93cdf42
BV
327{
328 struct sr_datafeed_packet packet;
329 struct dev_context *devc;
330
331 if (sdi->status != SR_ST_ACTIVE)
332 return SR_ERR;
333
334 if (!(devc = sdi->priv)) {
38d326e8 335 sr_err("sdi->priv was NULL.");
e93cdf42
BV
336 return SR_ERR_BUG;
337 }
338
38d326e8 339 sr_dbg("Stopping acquisition.");
e93cdf42
BV
340
341 sr_source_remove(devc->serial->fd);
342 hw_dev_close((struct sr_dev_inst *)sdi);
343
344 /* Send end packet to the session bus. */
38d326e8 345 sr_dbg("Sending SR_DF_END.");
e93cdf42
BV
346 packet.type = SR_DF_END;
347 sr_session_send(cb_data, &packet);
348
349
350 return SR_OK;
351}
352
353SR_PRIV struct sr_dev_driver agdmm_driver_info = {
354 .name = "agilent-dmm",
355 .longname = "Agilent U12xx series DMMs",
356 .api_version = 1,
357 .init = hw_init,
358 .cleanup = hw_cleanup,
359 .scan = hw_scan,
360 .dev_list = hw_dev_list,
361 .dev_clear = clear_instances,
035a1078 362 .config_set = config_set,
a1c743fc 363 .config_list = config_list,
e93cdf42
BV
364 .dev_open = hw_dev_open,
365 .dev_close = hw_dev_close,
e93cdf42
BV
366 .dev_acquisition_start = hw_dev_acquisition_start,
367 .dev_acquisition_stop = hw_dev_acquisition_stop,
368 .priv = NULL,
369};