]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/api.c
fluke-dmm: fix discovery
[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
883a2e9e
BV
114static GSList *hw_scan(GSList *options)
115{
4f958423 116 struct sr_dev_inst *sdi;
883a2e9e 117 struct drv_context *drvc;
4f958423
BV
118 struct dev_context *devc;
119 struct sr_hwopt *opt;
120 struct sr_probe *probe;
121 GSList *l, *devices;
fb480d57 122 int retry, len, fd, i, s;
4f958423 123 const char *conn, *serialcomm;
fb480d57 124 char buf[128], *b, **tokens;
883a2e9e 125
883a2e9e
BV
126 drvc = di->priv;
127 drvc->instances = NULL;
128
4f958423
BV
129 devices = NULL;
130 conn = serialcomm = NULL;
131 for (l = options; l; l = l->next) {
132 opt = l->data;
133 switch (opt->hwopt) {
134 case SR_HWOPT_CONN:
135 conn = opt->value;
136 break;
137 case SR_HWOPT_SERIALCOMM:
138 serialcomm = opt->value;
139 break;
140 }
141 }
142 if (!conn) {
143 sr_dbg("fluke-dmm: no serial port provided");
144 return NULL;
145 }
146 if (!serialcomm) {
147 sr_dbg("fluke-dmm: no serial communication parameters provided");
148 return NULL;
149 }
150
151 if ((fd = serial_open(conn, O_RDWR|O_NONBLOCK)) == -1) {
152 sr_err("fluke-dmm: unable to open %s: %s", conn, strerror(errno));
153 return NULL;
154 }
155
156 if (serial_set_paramstr(fd, serialcomm) != SR_OK) {
157 sr_err("fluke-dmm: unable to set serial parameters: %s",
158 strerror(errno));
159 return NULL;
160 }
161
fb480d57
BV
162 b = buf;
163 retry = 0;
164 /* We'll try the discovery sequence three times in case the device
165 * is not in an idle state when we send ID. */
166 while (!devices && retry < 3) {
167 retry++;
168 serial_flush(fd);
169 if (serial_write(fd, "ID\r", 3) == -1) {
170 sr_err("fluke-dmm: unable to send ID string: %s",
171 strerror(errno));
172 continue;
173 }
4f958423 174
fb480d57
BV
175 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
176 * or '1' to signify an error. */
177 len = 128;
178 serial_readline(fd, &b, &len, 150);
179 if (len != 1)
180 continue;
181 if (buf[0] != '0') {
182 sr_dbg("fluke-dmm: got ID response %.2x", buf[0]);
183 continue;
184 }
4f958423 185
fb480d57
BV
186 /* If CMD_ACK was OK, ID string follows. */
187 len = 128;
188 serial_readline(fd, &b, &len, 150);
189 if (len < 10)
190 continue;
191 tokens = g_strsplit(buf, ",", 3);
192 if (!strncmp("FLUKE", tokens[0], 5)
193 && tokens[1] && tokens[2]) {
194 for (i = 0; supported_flukedmm[i].model; i++) {
195 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
196 continue;
197 /* Skip leading spaces in version number. */
198 for (s = 0; tokens[1][s] == ' '; s++);
199 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
200 tokens[0] + 6, tokens[1] + s)))
201 return NULL;
202 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
203 sr_dbg("fluke-dmm: failed to malloc devc");
204 return NULL;
205 }
206 devc->profile = &supported_flukedmm[i];
207 devc->serial = sr_serial_dev_inst_new(conn, -1);
208 sdi->priv = devc;
209 sdi->driver = di;
210 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
211 return NULL;
212 sdi->probes = g_slist_append(sdi->probes, probe);
213 drvc->instances = g_slist_append(drvc->instances, sdi);
214 devices = g_slist_append(devices, sdi);
215 break;
4f958423 216 }
4f958423 217 }
fb480d57 218 g_strfreev(tokens);
4f958423 219 }
4f958423
BV
220
221 serial_close(fd);
883a2e9e
BV
222
223 return devices;
224}
225
226static GSList *hw_dev_list(void)
227{
228 struct drv_context *drvc;
229
230 drvc = di->priv;
231
232 return drvc->instances;
233}
234
235static int hw_dev_open(struct sr_dev_inst *sdi)
236{
237
238 /* TODO */
239
240 return SR_OK;
241}
242
243static int hw_dev_close(struct sr_dev_inst *sdi)
244{
245
246 /* TODO */
247
248 return SR_OK;
249}
250
251static int hw_cleanup(void)
252{
253
254 clear_instances();
255
256 /* TODO */
257
258 return SR_OK;
259}
260
261static int hw_info_get(int info_id, const void **data,
262 const struct sr_dev_inst *sdi)
263{
264
265
266 switch (info_id) {
267 /* TODO */
268 default:
269 return SR_ERR_ARG;
270 }
271
272 return SR_OK;
273}
274
275static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
276 const void *value)
277{
278 int ret;
279
280 if (sdi->status != SR_ST_ACTIVE)
281 return SR_ERR;
282
283 ret = SR_OK;
284 switch (hwcap) {
285 /* TODO */
286 default:
287 ret = SR_ERR_ARG;
288 }
289
290 return ret;
291}
292
293static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
294 void *cb_data)
295{
296
297 /* TODO */
298
299 return SR_OK;
300}
301
302static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
303 void *cb_data)
304{
305
306 (void)cb_data;
307
308 if (sdi->status != SR_ST_ACTIVE)
309 return SR_ERR;
310
311 /* TODO */
312
313 return SR_OK;
314}
315
e7edd64f 316SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
883a2e9e
BV
317 .name = "fluke-dmm",
318 .longname = "Fluke 18x/28x series DMMs",
319 .api_version = 1,
320 .init = hw_init,
321 .cleanup = hw_cleanup,
322 .scan = hw_scan,
323 .dev_list = hw_dev_list,
324 .dev_clear = clear_instances,
325 .dev_open = hw_dev_open,
326 .dev_close = hw_dev_close,
327 .info_get = hw_info_get,
328 .dev_config_set = hw_dev_config_set,
329 .dev_acquisition_start = hw_dev_acquisition_start,
330 .dev_acquisition_stop = hw_dev_acquisition_stop,
331 .priv = NULL,
332};