]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/fluke-dmm/api.c
std: Standardize function name.
[libsigrok.git] / hardware / fluke-dmm / api.c
... / ...
CommitLineData
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
30static const int32_t hwopts[] = {
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
33};
34
35static const int32_t hwcaps[] = {
36 SR_CONF_MULTIMETER,
37 SR_CONF_LIMIT_SAMPLES,
38 SR_CONF_LIMIT_MSEC,
39 SR_CONF_CONTINUOUS,
40};
41
42SR_PRIV struct sr_dev_driver flukedmm_driver_info;
43static struct sr_dev_driver *di = &flukedmm_driver_info;
44
45static 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
55static 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
61static int dev_clear(void)
62{
63 return std_dev_clear(di, NULL);
64}
65
66static int init(struct sr_context *sr_ctx)
67{
68 return std_init(sr_ctx, di, LOG_PREFIX);
69}
70
71static 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
162static 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
200static GSList *dev_list(void)
201{
202 return ((struct drv_context *)(di->priv))->instances;
203}
204
205static int dev_open(struct sr_dev_inst *sdi)
206{
207 struct sr_serial_dev_inst *serial;
208
209 serial = sdi->conn;
210 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
211 return SR_ERR;
212
213 sdi->status = SR_ST_ACTIVE;
214
215 return SR_OK;
216}
217
218static int cleanup(void)
219{
220 return dev_clear();
221}
222
223static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
224 const struct sr_probe_group *probe_group)
225{
226 struct dev_context *devc;
227
228 (void)probe_group;
229
230 if (sdi->status != SR_ST_ACTIVE)
231 return SR_ERR_DEV_CLOSED;
232
233 if (!(devc = sdi->priv)) {
234 sr_err("sdi->priv was NULL.");
235 return SR_ERR_BUG;
236 }
237
238 switch (id) {
239 case SR_CONF_LIMIT_MSEC:
240 /* TODO: not yet implemented */
241 if (g_variant_get_uint64(data) == 0) {
242 sr_err("LIMIT_MSEC can't be 0.");
243 return SR_ERR;
244 }
245 devc->limit_msec = g_variant_get_uint64(data);
246 sr_dbg("Setting time limit to %" PRIu64 "ms.",
247 devc->limit_msec);
248 break;
249 case SR_CONF_LIMIT_SAMPLES:
250 devc->limit_samples = g_variant_get_uint64(data);
251 sr_dbg("Setting sample limit to %" PRIu64 ".",
252 devc->limit_samples);
253 break;
254 default:
255 return SR_ERR_NA;
256 }
257
258 return SR_OK;
259}
260
261static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
262 const struct sr_probe_group *probe_group)
263{
264 (void)sdi;
265 (void)probe_group;
266
267 switch (key) {
268 case SR_CONF_SCAN_OPTIONS:
269 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
270 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
271 break;
272 case SR_CONF_DEVICE_OPTIONS:
273 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
274 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
275 break;
276 default:
277 return SR_ERR_NA;
278 }
279
280 return SR_OK;
281}
282
283static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
284{
285 struct dev_context *devc;
286 struct sr_serial_dev_inst *serial;
287
288 if (sdi->status != SR_ST_ACTIVE)
289 return SR_ERR_DEV_CLOSED;
290
291 if (!(devc = sdi->priv)) {
292 sr_err("sdi->priv was NULL.");
293 return SR_ERR_BUG;
294 }
295
296 devc->cb_data = cb_data;
297
298 /* Send header packet to the session bus. */
299 std_session_send_df_header(cb_data, LOG_PREFIX);
300
301 /* Poll every 100ms, or whenever some data comes in. */
302 serial = sdi->conn;
303 serial_source_add(serial, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
304
305 if (serial_write(serial, "QM\r", 3) == -1) {
306 sr_err("Unable to send QM: %s.", strerror(errno));
307 return SR_ERR;
308 }
309 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
310 devc->expect_response = TRUE;
311
312 return SR_OK;
313}
314
315static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
316{
317 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
318 sdi->conn, LOG_PREFIX);
319}
320
321SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
322 .name = "fluke-dmm",
323 .longname = "Fluke 18x/28x series DMMs",
324 .api_version = 1,
325 .init = init,
326 .cleanup = cleanup,
327 .scan = scan,
328 .dev_list = dev_list,
329 .dev_clear = dev_clear,
330 .config_get = NULL,
331 .config_set = config_set,
332 .config_list = config_list,
333 .dev_open = dev_open,
334 .dev_close = std_serial_dev_close,
335 .dev_acquisition_start = dev_acquisition_start,
336 .dev_acquisition_stop = dev_acquisition_stop,
337 .priv = NULL,
338};