]>
Commit | Line | Data |
---|---|---|
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 | 30 | static const int32_t hwopts[] = { |
1953564a BV |
31 | SR_CONF_CONN, |
32 | SR_CONF_SERIALCOMM, | |
d3f8f141 BV |
33 | }; |
34 | ||
70424328 | 35 | static 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 |
42 | SR_PRIV struct sr_dev_driver flukedmm_driver_info; |
43 | static struct sr_dev_driver *di = &flukedmm_driver_info; | |
883a2e9e | 44 | |
9fa09680 BV |
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 | ||
4f958423 | 55 | static 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 | 61 | static int dev_clear(void) |
883a2e9e | 62 | { |
771bd216 | 63 | return std_dev_clear(di, NULL); |
883a2e9e BV |
64 | } |
65 | ||
6078d2c9 | 66 | static int init(struct sr_context *sr_ctx) |
883a2e9e | 67 | { |
f6beaac5 | 68 | return std_init(sr_ctx, di, LOG_PREFIX); |
883a2e9e BV |
69 | } |
70 | ||
41298320 | 71 | static 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 | 162 | static 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 | 200 | static GSList *dev_list(void) |
883a2e9e | 201 | { |
0e94d524 | 202 | return ((struct drv_context *)(di->priv))->instances; |
883a2e9e BV |
203 | } |
204 | ||
6078d2c9 | 205 | static int dev_open(struct sr_dev_inst *sdi) |
883a2e9e | 206 | { |
919681f0 | 207 | struct sr_serial_dev_inst *serial; |
41298320 | 208 | |
919681f0 BV |
209 | serial = sdi->conn; |
210 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
41298320 | 211 | return SR_ERR; |
58d03f03 | 212 | |
41298320 | 213 | sdi->status = SR_ST_ACTIVE; |
883a2e9e BV |
214 | |
215 | return SR_OK; | |
216 | } | |
217 | ||
6078d2c9 | 218 | static int dev_close(struct sr_dev_inst *sdi) |
883a2e9e | 219 | { |
919681f0 | 220 | struct sr_serial_dev_inst *serial; |
d3f8f141 | 221 | |
919681f0 BV |
222 | serial = sdi->conn; |
223 | if (serial && serial->fd != -1) { | |
224 | serial_close(serial); | |
d3f8f141 BV |
225 | sdi->status = SR_ST_INACTIVE; |
226 | } | |
883a2e9e BV |
227 | |
228 | return SR_OK; | |
229 | } | |
230 | ||
6078d2c9 | 231 | static int cleanup(void) |
883a2e9e | 232 | { |
3b412e3a | 233 | return dev_clear(); |
883a2e9e BV |
234 | } |
235 | ||
8f996b89 ML |
236 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
237 | const struct sr_probe_group *probe_group) | |
883a2e9e | 238 | { |
d3f8f141 | 239 | struct dev_context *devc; |
883a2e9e | 240 | |
8f996b89 ML |
241 | (void)probe_group; |
242 | ||
883a2e9e | 243 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 244 | return SR_ERR_DEV_CLOSED; |
883a2e9e | 245 | |
d3f8f141 | 246 | if (!(devc = sdi->priv)) { |
31d84da3 | 247 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
248 | return SR_ERR_BUG; |
249 | } | |
250 | ||
035a1078 | 251 | switch (id) { |
1953564a | 252 | case SR_CONF_LIMIT_MSEC: |
d3f8f141 | 253 | /* TODO: not yet implemented */ |
70424328 | 254 | if (g_variant_get_uint64(data) == 0) { |
31d84da3 | 255 | sr_err("LIMIT_MSEC can't be 0."); |
d3f8f141 BV |
256 | return SR_ERR; |
257 | } | |
70424328 | 258 | devc->limit_msec = g_variant_get_uint64(data); |
31d84da3 | 259 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
d3f8f141 BV |
260 | devc->limit_msec); |
261 | break; | |
1953564a | 262 | case SR_CONF_LIMIT_SAMPLES: |
70424328 | 263 | devc->limit_samples = g_variant_get_uint64(data); |
31d84da3 | 264 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
d3f8f141 BV |
265 | devc->limit_samples); |
266 | break; | |
883a2e9e | 267 | default: |
bd6fbf62 | 268 | return SR_ERR_NA; |
883a2e9e BV |
269 | } |
270 | ||
d3f8f141 | 271 | return SR_OK; |
883a2e9e BV |
272 | } |
273 | ||
8f996b89 ML |
274 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
275 | const struct sr_probe_group *probe_group) | |
a1c743fc | 276 | { |
a1c743fc | 277 | (void)sdi; |
8f996b89 | 278 | (void)probe_group; |
a1c743fc BV |
279 | |
280 | switch (key) { | |
0d485e30 | 281 | case SR_CONF_SCAN_OPTIONS: |
70424328 BV |
282 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
283 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 284 | break; |
9a6517d1 | 285 | case SR_CONF_DEVICE_OPTIONS: |
70424328 BV |
286 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
287 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 288 | break; |
a1c743fc | 289 | default: |
bd6fbf62 | 290 | return SR_ERR_NA; |
a1c743fc BV |
291 | } |
292 | ||
293 | return SR_OK; | |
294 | } | |
295 | ||
6078d2c9 | 296 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
883a2e9e | 297 | { |
d3f8f141 | 298 | struct dev_context *devc; |
919681f0 | 299 | struct sr_serial_dev_inst *serial; |
d3f8f141 | 300 | |
e73ffd42 BV |
301 | if (sdi->status != SR_ST_ACTIVE) |
302 | return SR_ERR_DEV_CLOSED; | |
303 | ||
d3f8f141 | 304 | if (!(devc = sdi->priv)) { |
31d84da3 | 305 | sr_err("sdi->priv was NULL."); |
d3f8f141 BV |
306 | return SR_ERR_BUG; |
307 | } | |
308 | ||
d3f8f141 BV |
309 | devc->cb_data = cb_data; |
310 | ||
311 | /* Send header packet to the session bus. */ | |
29a27196 | 312 | std_session_send_df_header(cb_data, LOG_PREFIX); |
883a2e9e | 313 | |
d3f8f141 | 314 | /* Poll every 100ms, or whenever some data comes in. */ |
919681f0 BV |
315 | serial = sdi->conn; |
316 | sr_source_add(serial->fd, G_IO_IN, 50, fluke_receive_data, (void *)sdi); | |
d38d2ef0 | 317 | |
919681f0 | 318 | if (serial_write(serial, "QM\r", 3) == -1) { |
31d84da3 | 319 | sr_err("Unable to send QM: %s.", strerror(errno)); |
d38d2ef0 BV |
320 | return SR_ERR; |
321 | } | |
322 | devc->cmd_sent_at = g_get_monotonic_time() / 1000; | |
323 | devc->expect_response = TRUE; | |
883a2e9e BV |
324 | |
325 | return SR_OK; | |
326 | } | |
327 | ||
6078d2c9 | 328 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
883a2e9e | 329 | { |
f6beaac5 UH |
330 | return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close, |
331 | sdi->conn, LOG_PREFIX); | |
883a2e9e BV |
332 | } |
333 | ||
e7edd64f | 334 | SR_PRIV struct sr_dev_driver flukedmm_driver_info = { |
883a2e9e BV |
335 | .name = "fluke-dmm", |
336 | .longname = "Fluke 18x/28x series DMMs", | |
337 | .api_version = 1, | |
6078d2c9 UH |
338 | .init = init, |
339 | .cleanup = cleanup, | |
340 | .scan = scan, | |
341 | .dev_list = dev_list, | |
3b412e3a | 342 | .dev_clear = dev_clear, |
6fab7b8f | 343 | .config_get = NULL, |
035a1078 | 344 | .config_set = config_set, |
a1c743fc | 345 | .config_list = config_list, |
6078d2c9 UH |
346 | .dev_open = dev_open, |
347 | .dev_close = dev_close, | |
348 | .dev_acquisition_start = dev_acquisition_start, | |
349 | .dev_acquisition_stop = dev_acquisition_stop, | |
883a2e9e BV |
350 | .priv = NULL, |
351 | }; |