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