]> sigrok.org Git - libsigrok.git/blob - hardware/fluke-dmm/api.c
Use std_serial_dev_open().
[libsigrok.git] / hardware / fluke-dmm / api.c
1 /*
2  * This file is part of the libsigrok 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 "fluke-dmm.h"
29
30 static const int32_t hwopts[] = {
31         SR_CONF_CONN,
32         SR_CONF_SERIALCOMM,
33 };
34
35 static const int32_t hwcaps[] = {
36         SR_CONF_MULTIMETER,
37         SR_CONF_LIMIT_SAMPLES,
38         SR_CONF_LIMIT_MSEC,
39         SR_CONF_CONTINUOUS,
40 };
41
42 SR_PRIV struct sr_dev_driver flukedmm_driver_info;
43 static struct sr_dev_driver *di = &flukedmm_driver_info;
44
45 static char *scan_conn[] = {
46         /* 287/289 */
47         "115200/8n1",
48         /* 187/189 */
49         "9600/8n1",
50         /* Scopemeter 190 series */
51         "1200/8n1",
52         NULL
53 };
54
55 static const struct flukedmm_profile supported_flukedmm[] = {
56         { FLUKE_187, "187", 100, 1000 },
57         { FLUKE_287, "287", 100, 1000 },
58         { FLUKE_190, "199B", 1000, 3500 },
59 };
60
61 static int dev_clear(void)
62 {
63         return std_dev_clear(di, NULL);
64 }
65
66 static int init(struct sr_context *sr_ctx)
67 {
68         return std_init(sr_ctx, di, LOG_PREFIX);
69 }
70
71 static GSList *fluke_scan(const char *conn, const char *serialcomm)
72 {
73         struct sr_dev_inst *sdi;
74         struct drv_context *drvc;
75         struct dev_context *devc;
76         struct sr_probe *probe;
77         struct sr_serial_dev_inst *serial;
78         GSList *devices;
79         int retry, len, i, s;
80         char buf[128], *b, **tokens;
81
82         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
83                 return NULL;
84
85         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
86                 return NULL;
87
88         drvc = di->priv;
89         b = buf;
90         retry = 0;
91         devices = NULL;
92         /* We'll try the discovery sequence three times in case the device
93          * is not in an idle state when we send ID. */
94         while (!devices && retry < 3) {
95                 retry++;
96                 serial_flush(serial);
97                 if (serial_write(serial, "ID\r", 3) == -1) {
98                         sr_err("Unable to send ID string: %s.",
99                                strerror(errno));
100                         continue;
101                 }
102
103                 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
104                  * or '1' to signify an error. */
105                 len = 128;
106                 serial_readline(serial, &b, &len, 150);
107                 if (len != 1)
108                         continue;
109                 if (buf[0] != '0')
110                         continue;
111
112                 /* If CMD_ACK was OK, ID string follows. */
113                 len = 128;
114                 serial_readline(serial, &b, &len, 850);
115                 if (len < 10)
116                         continue;
117                 if (strcspn(buf, ",") < 15)
118                         /* Looks like it's comma-separated. */
119                         tokens = g_strsplit(buf, ",", 3);
120                 else
121                         /* Fluke 199B, at least, uses semicolon. */
122                         tokens = g_strsplit(buf, ";", 3);
123                 if (!strncmp("FLUKE", tokens[0], 5)
124                                 && tokens[1] && tokens[2]) {
125                         for (i = 0; supported_flukedmm[i].model; i++) {
126                                 if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
127                                         continue;
128                                 /* Skip leading spaces in version number. */
129                                 for (s = 0; tokens[1][s] == ' '; s++);
130                                 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
131                                                 tokens[0] + 6, tokens[1] + s)))
132                                         return NULL;
133                                 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
134                                         sr_err("Device context malloc failed.");
135                                         return NULL;
136                                 }
137                                 devc->profile = &supported_flukedmm[i];
138                                 sdi->inst_type = SR_INST_SERIAL;
139                                 sdi->conn = serial;
140                                 sdi->priv = devc;
141                                 sdi->driver = di;
142                                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
143                                         return NULL;
144                                 sdi->probes = g_slist_append(sdi->probes, probe);
145                                 drvc->instances = g_slist_append(drvc->instances, sdi);
146                                 devices = g_slist_append(devices, sdi);
147                                 break;
148                         }
149                 }
150                 g_strfreev(tokens);
151                 if (devices)
152                         /* Found one. */
153                         break;
154         }
155         serial_close(serial);
156         if (!devices)
157                 sr_serial_dev_inst_free(serial);
158
159         return devices;
160 }
161
162 static GSList *scan(GSList *options)
163 {
164         struct sr_config *src;
165         GSList *l, *devices;
166         int i;
167         const char *conn, *serialcomm;
168
169         conn = serialcomm = NULL;
170         for (l = options; l; l = l->next) {
171                 src = l->data;
172                 switch (src->key) {
173                 case SR_CONF_CONN:
174                         conn = g_variant_get_string(src->data, NULL);
175                         break;
176                 case SR_CONF_SERIALCOMM:
177                         serialcomm = g_variant_get_string(src->data, NULL);
178                         break;
179                 }
180         }
181         if (!conn)
182                 return NULL;
183
184         if (serialcomm) {
185                 /* Use the provided comm specs. */
186                 devices = fluke_scan(conn, serialcomm);
187         } else {
188                 for (i = 0; scan_conn[i]; i++) {
189                         if ((devices = fluke_scan(conn, scan_conn[i])))
190                                 break;
191                         /* The Scopemeter 199B, at least, requires this
192                          * after all the 115k/9.6k confusion. */
193                         g_usleep(5000);
194                 }
195         }
196
197         return devices;
198 }
199
200 static GSList *dev_list(void)
201 {
202         return ((struct drv_context *)(di->priv))->instances;
203 }
204
205 static int cleanup(void)
206 {
207         return dev_clear();
208 }
209
210 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
211                 const struct sr_probe_group *probe_group)
212 {
213         struct dev_context *devc;
214
215         (void)probe_group;
216
217         if (sdi->status != SR_ST_ACTIVE)
218                 return SR_ERR_DEV_CLOSED;
219
220         if (!(devc = sdi->priv)) {
221                 sr_err("sdi->priv was NULL.");
222                 return SR_ERR_BUG;
223         }
224
225         switch (id) {
226         case SR_CONF_LIMIT_MSEC:
227                 /* TODO: not yet implemented */
228                 if (g_variant_get_uint64(data) == 0) {
229                         sr_err("LIMIT_MSEC can't be 0.");
230                         return SR_ERR;
231                 }
232                 devc->limit_msec = g_variant_get_uint64(data);
233                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
234                        devc->limit_msec);
235                 break;
236         case SR_CONF_LIMIT_SAMPLES:
237                 devc->limit_samples = g_variant_get_uint64(data);
238                 sr_dbg("Setting sample limit to %" PRIu64 ".",
239                        devc->limit_samples);
240                 break;
241         default:
242                 return SR_ERR_NA;
243         }
244
245         return SR_OK;
246 }
247
248 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
249                 const struct sr_probe_group *probe_group)
250 {
251         (void)sdi;
252         (void)probe_group;
253
254         switch (key) {
255         case SR_CONF_SCAN_OPTIONS:
256                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
257                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
258                 break;
259         case SR_CONF_DEVICE_OPTIONS:
260                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
261                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
262                 break;
263         default:
264                 return SR_ERR_NA;
265         }
266
267         return SR_OK;
268 }
269
270 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
271 {
272         struct dev_context *devc;
273         struct sr_serial_dev_inst *serial;
274
275         if (sdi->status != SR_ST_ACTIVE)
276                 return SR_ERR_DEV_CLOSED;
277
278         if (!(devc = sdi->priv)) {
279                 sr_err("sdi->priv was NULL.");
280                 return SR_ERR_BUG;
281         }
282
283         devc->cb_data = cb_data;
284
285         /* Send header packet to the session bus. */
286         std_session_send_df_header(cb_data, LOG_PREFIX);
287
288         /* Poll every 100ms, or whenever some data comes in. */
289         serial = sdi->conn;
290         serial_source_add(serial, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
291
292         if (serial_write(serial, "QM\r", 3) == -1) {
293                 sr_err("Unable to send QM: %s.", strerror(errno));
294                 return SR_ERR;
295         }
296         devc->cmd_sent_at = g_get_monotonic_time() / 1000;
297         devc->expect_response = TRUE;
298
299         return SR_OK;
300 }
301
302 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
303 {
304         return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
305                         sdi->conn, LOG_PREFIX);
306 }
307
308 SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
309         .name = "fluke-dmm",
310         .longname = "Fluke 18x/28x series DMMs",
311         .api_version = 1,
312         .init = init,
313         .cleanup = cleanup,
314         .scan = scan,
315         .dev_list = dev_list,
316         .dev_clear = dev_clear,
317         .config_get = NULL,
318         .config_set = config_set,
319         .config_list = config_list,
320         .dev_open = std_serial_dev_open,
321         .dev_close = std_serial_dev_close,
322         .dev_acquisition_start = dev_acquisition_start,
323         .dev_acquisition_stop = dev_acquisition_stop,
324         .priv = NULL,
325 };