]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/api.c
fluke-dmm: auto-discover serial bitrate if not provided
[libsigrok.git] / hardware / fluke-dmm / api.c
CommitLineData
883a2e9e
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>
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23#include "config.h"
4f958423
BV
24#include "fluke-dmm.h"
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <string.h>
29#include <errno.h>
883a2e9e
BV
30
31
e7edd64f
BV
32SR_PRIV struct sr_dev_driver flukedmm_driver_info;
33static struct sr_dev_driver *di = &flukedmm_driver_info;
883a2e9e 34
4f958423
BV
35static const struct flukedmm_profile supported_flukedmm[] = {
36 { FLUKE_187, "187" },
37};
38
883a2e9e
BV
39
40/* Properly close and free all devices. */
41static int clear_instances(void)
42{
43 struct sr_dev_inst *sdi;
44 struct drv_context *drvc;
45 struct dev_context *devc;
46 GSList *l;
47
4f958423
BV
48 if (!(drvc = di->priv))
49 return SR_OK;
50
883a2e9e
BV
51 drvc = di->priv;
52 for (l = drvc->instances; l; l = l->next) {
4f958423 53 if (!(sdi = l->data))
883a2e9e 54 continue;
4f958423 55 if (!(devc = sdi->priv))
883a2e9e 56 continue;
4f958423 57 sr_serial_dev_inst_free(devc->serial);
883a2e9e
BV
58 sr_dev_inst_free(sdi);
59 }
883a2e9e
BV
60 g_slist_free(drvc->instances);
61 drvc->instances = NULL;
62
63 return SR_OK;
64}
65
66static int hw_init(void)
67{
68 struct drv_context *drvc;
69
70 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
4f958423 71 sr_err("fluke-dmm: driver context malloc failed.");
883a2e9e
BV
72 return SR_ERR;
73 }
74
883a2e9e
BV
75 di->priv = drvc;
76
77 return SR_OK;
78}
79
4f958423
BV
80static int serial_readline(int fd, char **buf, int *buflen, uint64_t timeout_ms)
81{
82 uint64_t start;
83 int maxlen, len;
84
85 timeout_ms *= 1000;
86 start = g_get_monotonic_time();
87
88 maxlen = *buflen;
89 *buflen = len = 0;
90 while(1) {
91 len = maxlen - *buflen - 1;
92 if (len < 1)
93 break;
fb480d57 94 len = serial_read(fd, *buf + *buflen, 1);
4f958423
BV
95 if (len > 0) {
96 *buflen += len;
97 *(*buf + *buflen) = '\0';
fb480d57
BV
98 if (*buflen > 0 && *(*buf + *buflen - 1) == '\n') {
99 /* Strip LF and terminate. */
100 *(*buf + --*buflen) = '\0';
4f958423 101 break;
fb480d57 102 }
4f958423
BV
103 }
104 if (g_get_monotonic_time() - start > timeout_ms)
105 /* Timeout */
106 break;
107 g_usleep(2000);
108 }
fb480d57 109 sr_dbg("fluke-dmm: received %d: '%s'", *buflen, *buf);
4f958423
BV
110
111 return SR_OK;
112}
113
41298320 114static GSList *fluke_scan(const char *conn, const char *serialcomm)
883a2e9e 115{
4f958423 116 struct sr_dev_inst *sdi;
883a2e9e 117 struct drv_context *drvc;
4f958423 118 struct dev_context *devc;
4f958423 119 struct sr_probe *probe;
41298320
BV
120 GSList *devices;
121 int fd, retry, len, i, s;
fb480d57 122 char buf[128], *b, **tokens;
883a2e9e 123
4f958423
BV
124 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
125 sr_err("fluke-dmm: unable to open %s: %s", conn, strerror(errno));
126 return NULL;
127 }
4f958423 128 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
41298320 129 sr_err("fluke-dmm: unable to set serial parameters");
4f958423
BV
130 return NULL;
131 }
132
41298320 133 drvc = di->priv;
fb480d57
BV
134 b = buf;
135 retry = 0;
41298320 136 devices = NULL;
fb480d57
BV
137 /* We'll try the discovery sequence three times in case the device
138 * is not in an idle state when we send ID. */
139 while (!devices && retry < 3) {
140 retry++;
141 serial_flush(fd);
142 if (serial_write(fd, "ID\r", 3) == -1) {
143 sr_err("fluke-dmm: unable to send ID string: %s",
144 strerror(errno));
145 continue;
146 }
4f958423 147
fb480d57
BV
148 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
149 * or '1' to signify an error. */
150 len = 128;
151 serial_readline(fd, &b, &len, 150);
152 if (len != 1)
153 continue;
41298320 154 if (buf[0] != '0')
fb480d57 155 continue;
4f958423 156
fb480d57
BV
157 /* If CMD_ACK was OK, ID string follows. */
158 len = 128;
159 serial_readline(fd, &b, &len, 150);
160 if (len < 10)
161 continue;
162 tokens = g_strsplit(buf, ",", 3);
163 if (!strncmp("FLUKE", tokens[0], 5)
164 && tokens[1] && tokens[2]) {
165 for (i = 0; supported_flukedmm[i].model; i++) {
166 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
167 continue;
168 /* Skip leading spaces in version number. */
169 for (s = 0; tokens[1][s] == ' '; s++);
170 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
171 tokens[0] + 6, tokens[1] + s)))
172 return NULL;
173 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
174 sr_dbg("fluke-dmm: failed to malloc devc");
175 return NULL;
176 }
177 devc->profile = &supported_flukedmm[i];
178 devc->serial = sr_serial_dev_inst_new(conn, -1);
41298320 179 devc->serialcomm = g_strdup(serialcomm);
fb480d57
BV
180 sdi->priv = devc;
181 sdi->driver = di;
182 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
183 return NULL;
184 sdi->probes = g_slist_append(sdi->probes, probe);
185 drvc->instances = g_slist_append(drvc->instances, sdi);
186 devices = g_slist_append(devices, sdi);
187 break;
4f958423 188 }
4f958423 189 }
fb480d57 190 g_strfreev(tokens);
4f958423 191 }
4f958423 192 serial_close(fd);
883a2e9e
BV
193
194 return devices;
195}
196
41298320
BV
197static GSList *hw_scan(GSList *options)
198{
199 struct sr_dev_inst *sdi;
200 struct sr_hwopt *opt;
201 GSList *l, *devices;
202 const char *conn, *serialcomm;
203
204 conn = serialcomm = NULL;
205 for (l = options; l; l = l->next) {
206 opt = l->data;
207 switch (opt->hwopt) {
208 case SR_HWOPT_CONN:
209 conn = opt->value;
210 break;
211 case SR_HWOPT_SERIALCOMM:
212 serialcomm = opt->value;
213 break;
214 }
215 }
216 if (!conn)
217 return NULL;
218
219 if (serialcomm) {
220 /* Use the provided comm specs. */
221 devices = fluke_scan(conn, serialcomm);
222 } else {
223 /* Try 115200, as used on 287/289. */
224 devices = fluke_scan(conn, "115200/8n1");
225 if (!devices)
226 /* Fall back to 9600 for 187/189. */
227 devices = fluke_scan(conn, "9600/8n1");
228 }
229
230 return devices;
231}
232
883a2e9e
BV
233static GSList *hw_dev_list(void)
234{
235 struct drv_context *drvc;
236
237 drvc = di->priv;
238
239 return drvc->instances;
240}
241
242static int hw_dev_open(struct sr_dev_inst *sdi)
243{
41298320 244 struct dev_context *devc;
883a2e9e 245
41298320
BV
246 if (!(devc = sdi->priv)) {
247 sr_err("fluke-dmm: sdi->priv was NULL.");
248 return SR_ERR_BUG;
249 }
250
251 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
252 if (devc->serial->fd == -1) {
253 sr_err("fluke-dmm: Couldn't open serial port '%s'.",
254 devc->serial->port);
255 return SR_ERR;
256 }
257 if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
258 sr_err("fluke-dmm: unable to set serial parameters");
259 return SR_ERR;
260 }
261 sdi->status = SR_ST_ACTIVE;
883a2e9e
BV
262
263 return SR_OK;
264}
265
266static int hw_dev_close(struct sr_dev_inst *sdi)
267{
268
269 /* TODO */
270
271 return SR_OK;
272}
273
274static int hw_cleanup(void)
275{
276
277 clear_instances();
278
279 /* TODO */
280
281 return SR_OK;
282}
283
284static int hw_info_get(int info_id, const void **data,
285 const struct sr_dev_inst *sdi)
286{
287
288
289 switch (info_id) {
290 /* TODO */
291 default:
292 return SR_ERR_ARG;
293 }
294
295 return SR_OK;
296}
297
298static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
299 const void *value)
300{
301 int ret;
302
303 if (sdi->status != SR_ST_ACTIVE)
304 return SR_ERR;
305
306 ret = SR_OK;
307 switch (hwcap) {
308 /* TODO */
309 default:
310 ret = SR_ERR_ARG;
311 }
312
313 return ret;
314}
315
316static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
317 void *cb_data)
318{
319
320 /* TODO */
321
322 return SR_OK;
323}
324
325static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
326 void *cb_data)
327{
328
329 (void)cb_data;
330
331 if (sdi->status != SR_ST_ACTIVE)
332 return SR_ERR;
333
334 /* TODO */
335
336 return SR_OK;
337}
338
e7edd64f 339SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
883a2e9e
BV
340 .name = "fluke-dmm",
341 .longname = "Fluke 18x/28x series DMMs",
342 .api_version = 1,
343 .init = hw_init,
344 .cleanup = hw_cleanup,
345 .scan = hw_scan,
346 .dev_list = hw_dev_list,
347 .dev_clear = clear_instances,
348 .dev_open = hw_dev_open,
349 .dev_close = hw_dev_close,
350 .info_get = hw_info_get,
351 .dev_config_set = hw_dev_config_set,
352 .dev_acquisition_start = hw_dev_acquisition_start,
353 .dev_acquisition_stop = hw_dev_acquisition_stop,
354 .priv = NULL,
355};