]> sigrok.org Git - libsigrok.git/blame - hardware/fluke-dmm/api.c
session: Implement dev_clear().
[libsigrok.git] / hardware / fluke-dmm / api.c
CommitLineData
883a2e9e 1/*
50985c20 2 * This file is part of the libsigrok project.
883a2e9e
BV
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>
4f958423
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <string.h>
25#include <errno.h>
6f22a8ef
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "fluke-dmm.h"
883a2e9e 29
70424328 30static const int32_t hwopts[] = {
1953564a
BV
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
d3f8f141
BV
33};
34
70424328 35static const int32_t hwcaps[] = {
1953564a
BV
36 SR_CONF_MULTIMETER,
37 SR_CONF_LIMIT_SAMPLES,
38 SR_CONF_LIMIT_MSEC,
39 SR_CONF_CONTINUOUS,
d3f8f141
BV
40};
41
e7edd64f
BV
42SR_PRIV struct sr_dev_driver flukedmm_driver_info;
43static struct sr_dev_driver *di = &flukedmm_driver_info;
883a2e9e 44
9fa09680
BV
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
4f958423 55static const struct flukedmm_profile supported_flukedmm[] = {
d4b11de0
BV
56 { FLUKE_187, "187", 100, 1000 },
57 { FLUKE_287, "287", 100, 1000 },
58 { FLUKE_190, "199B", 1000, 3500 },
4f958423
BV
59};
60
3b412e3a 61static int dev_clear(void)
883a2e9e 62{
771bd216 63 return std_dev_clear(di, NULL);
883a2e9e
BV
64}
65
6078d2c9 66static int init(struct sr_context *sr_ctx)
883a2e9e 67{
f6beaac5 68 return std_init(sr_ctx, di, LOG_PREFIX);
883a2e9e
BV
69}
70
41298320 71static GSList *fluke_scan(const char *conn, const char *serialcomm)
883a2e9e 72{
4f958423 73 struct sr_dev_inst *sdi;
883a2e9e 74 struct drv_context *drvc;
4f958423 75 struct dev_context *devc;
4f958423 76 struct sr_probe *probe;
58d03f03 77 struct sr_serial_dev_inst *serial;
41298320 78 GSList *devices;
58d03f03 79 int retry, len, i, s;
fb480d57 80 char buf[128], *b, **tokens;
883a2e9e 81
58d03f03 82 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
4f958423 83 return NULL;
58d03f03 84
a54dd31e 85 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
4f958423 86 return NULL;
4f958423 87
41298320 88 drvc = di->priv;
fb480d57
BV
89 b = buf;
90 retry = 0;
41298320 91 devices = NULL;
fb480d57
BV
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++;
58d03f03
BV
96 serial_flush(serial);
97 if (serial_write(serial, "ID\r", 3) == -1) {
31d84da3
UH
98 sr_err("Unable to send ID string: %s.",
99 strerror(errno));
fb480d57
BV
100 continue;
101 }
4f958423 102
fb480d57
BV
103 /* Response is first a CMD_ACK byte (ASCII '0' for OK,
104 * or '1' to signify an error. */
105 len = 128;
58d03f03 106 serial_readline(serial, &b, &len, 150);
fb480d57
BV
107 if (len != 1)
108 continue;
41298320 109 if (buf[0] != '0')
fb480d57 110 continue;
4f958423 111
fb480d57
BV
112 /* If CMD_ACK was OK, ID string follows. */
113 len = 128;
9fa09680 114 serial_readline(serial, &b, &len, 850);
fb480d57
BV
115 if (len < 10)
116 continue;
9fa09680
BV
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);
fb480d57
BV
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)))) {
31d84da3 134 sr_err("Device context malloc failed.");
fb480d57
BV
135 return NULL;
136 }
137 devc->profile = &supported_flukedmm[i];
771bd216 138 sdi->inst_type = SR_INST_SERIAL;
919681f0 139 sdi->conn = serial;
fb480d57
BV
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;
4f958423 148 }
4f958423 149 }
fb480d57 150 g_strfreev(tokens);
9fa09680
BV
151 if (devices)
152 /* Found one. */
153 break;
4f958423 154 }
58d03f03
BV
155 serial_close(serial);
156 if (!devices)
157 sr_serial_dev_inst_free(serial);
883a2e9e
BV
158
159 return devices;
160}
161
6078d2c9 162static GSList *scan(GSList *options)
41298320 163{
1987b8d6 164 struct sr_config *src;
41298320 165 GSList *l, *devices;
9fa09680 166 int i;
41298320
BV
167 const char *conn, *serialcomm;
168
169 conn = serialcomm = NULL;
170 for (l = options; l; l = l->next) {
1987b8d6
BV
171 src = l->data;
172 switch (src->key) {
1953564a 173 case SR_CONF_CONN:
70424328 174 conn = g_variant_get_string(src->data, NULL);
41298320 175 break;
1953564a 176 case SR_CONF_SERIALCOMM:
70424328 177 serialcomm = g_variant_get_string(src->data, NULL);
41298320
BV
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 {
9fa09680
BV
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 }
41298320
BV
195 }
196
197 return devices;
198}
199
6078d2c9 200static GSList *dev_list(void)
883a2e9e 201{
0e94d524 202 return ((struct drv_context *)(di->priv))->instances;
883a2e9e
BV
203}
204
6078d2c9 205static int cleanup(void)
883a2e9e 206{
3b412e3a 207 return dev_clear();
883a2e9e
BV
208}
209
8f996b89
ML
210static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
211 const struct sr_probe_group *probe_group)
883a2e9e 212{
d3f8f141 213 struct dev_context *devc;
883a2e9e 214
8f996b89
ML
215 (void)probe_group;
216
883a2e9e 217 if (sdi->status != SR_ST_ACTIVE)
e73ffd42 218 return SR_ERR_DEV_CLOSED;
883a2e9e 219
d3f8f141 220 if (!(devc = sdi->priv)) {
31d84da3 221 sr_err("sdi->priv was NULL.");
d3f8f141
BV
222 return SR_ERR_BUG;
223 }
224
035a1078 225 switch (id) {
1953564a 226 case SR_CONF_LIMIT_MSEC:
d3f8f141 227 /* TODO: not yet implemented */
70424328 228 if (g_variant_get_uint64(data) == 0) {
31d84da3 229 sr_err("LIMIT_MSEC can't be 0.");
d3f8f141
BV
230 return SR_ERR;
231 }
70424328 232 devc->limit_msec = g_variant_get_uint64(data);
31d84da3 233 sr_dbg("Setting time limit to %" PRIu64 "ms.",
d3f8f141
BV
234 devc->limit_msec);
235 break;
1953564a 236 case SR_CONF_LIMIT_SAMPLES:
70424328 237 devc->limit_samples = g_variant_get_uint64(data);
31d84da3 238 sr_dbg("Setting sample limit to %" PRIu64 ".",
d3f8f141
BV
239 devc->limit_samples);
240 break;
883a2e9e 241 default:
bd6fbf62 242 return SR_ERR_NA;
883a2e9e
BV
243 }
244
d3f8f141 245 return SR_OK;
883a2e9e
BV
246}
247
8f996b89
ML
248static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
249 const struct sr_probe_group *probe_group)
a1c743fc 250{
a1c743fc 251 (void)sdi;
8f996b89 252 (void)probe_group;
a1c743fc
BV
253
254 switch (key) {
0d485e30 255 case SR_CONF_SCAN_OPTIONS:
70424328
BV
256 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
257 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
0d485e30 258 break;
9a6517d1 259 case SR_CONF_DEVICE_OPTIONS:
70424328
BV
260 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
261 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
9a6517d1 262 break;
a1c743fc 263 default:
bd6fbf62 264 return SR_ERR_NA;
a1c743fc
BV
265 }
266
267 return SR_OK;
268}
269
6078d2c9 270static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
883a2e9e 271{
d3f8f141 272 struct dev_context *devc;
919681f0 273 struct sr_serial_dev_inst *serial;
d3f8f141 274
e73ffd42
BV
275 if (sdi->status != SR_ST_ACTIVE)
276 return SR_ERR_DEV_CLOSED;
277
d3f8f141 278 if (!(devc = sdi->priv)) {
31d84da3 279 sr_err("sdi->priv was NULL.");
d3f8f141
BV
280 return SR_ERR_BUG;
281 }
282
d3f8f141
BV
283 devc->cb_data = cb_data;
284
285 /* Send header packet to the session bus. */
29a27196 286 std_session_send_df_header(cb_data, LOG_PREFIX);
883a2e9e 287
d3f8f141 288 /* Poll every 100ms, or whenever some data comes in. */
919681f0 289 serial = sdi->conn;
abc4b335 290 serial_source_add(serial, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
d38d2ef0 291
919681f0 292 if (serial_write(serial, "QM\r", 3) == -1) {
31d84da3 293 sr_err("Unable to send QM: %s.", strerror(errno));
d38d2ef0
BV
294 return SR_ERR;
295 }
296 devc->cmd_sent_at = g_get_monotonic_time() / 1000;
297 devc->expect_response = TRUE;
883a2e9e
BV
298
299 return SR_OK;
300}
301
6078d2c9 302static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
883a2e9e 303{
d43b0908 304 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
f6beaac5 305 sdi->conn, LOG_PREFIX);
883a2e9e
BV
306}
307
e7edd64f 308SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
883a2e9e
BV
309 .name = "fluke-dmm",
310 .longname = "Fluke 18x/28x series DMMs",
311 .api_version = 1,
6078d2c9
UH
312 .init = init,
313 .cleanup = cleanup,
314 .scan = scan,
315 .dev_list = dev_list,
3b412e3a 316 .dev_clear = dev_clear,
6fab7b8f 317 .config_get = NULL,
035a1078 318 .config_set = config_set,
a1c743fc 319 .config_list = config_list,
854434de 320 .dev_open = std_serial_dev_open,
bf2c987f 321 .dev_close = std_serial_dev_close,
6078d2c9
UH
322 .dev_acquisition_start = dev_acquisition_start,
323 .dev_acquisition_stop = dev_acquisition_stop,
883a2e9e
BV
324 .priv = NULL,
325};