]>
Commit | Line | Data |
---|---|---|
06a3e78a DJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.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 | ||
13f2b9d7 DJ |
20 | #include <stdlib.h> |
21 | #include <glib/gstdio.h> | |
06a3e78a DJ |
22 | #include "protocol.h" |
23 | ||
13f2b9d7 DJ |
24 | #define SERIALCOMM "115200/8n1/flow=1" |
25 | ||
26 | static const int32_t hwopts[] = { | |
27 | SR_CONF_CONN, | |
28 | SR_CONF_SERIALCOMM, | |
29 | }; | |
30 | ||
31 | struct usb_id_info { | |
32 | uint16_t vendor_id; | |
33 | uint16_t product_id; | |
89280b1a | 34 | }; |
13f2b9d7 DJ |
35 | |
36 | static struct usb_id_info ho_models[] = { | |
89280b1a UH |
37 | { 0x0403, 0xed72 }, /* HO720 */ |
38 | { 0x0403, 0xed73 }, /* HO730 */ | |
13f2b9d7 | 39 | }; |
06a3e78a | 40 | |
719eff68 UH |
41 | enum { |
42 | PG_INVALID = -1, | |
43 | PG_NONE, | |
44 | PG_ANALOG, | |
45 | PG_DIGITAL, | |
46 | }; | |
47 | ||
06a3e78a DJ |
48 | static int init(struct sr_context *sr_ctx) |
49 | { | |
50 | return std_init(sr_ctx, di, LOG_PREFIX); | |
51 | } | |
52 | ||
13f2b9d7 DJ |
53 | /** |
54 | * Find USB serial devices via the USB vendor ID and product ID. | |
55 | * | |
89280b1a UH |
56 | * @param vendor_id Vendor ID of the USB device. |
57 | * @param product_id Product ID of the USB device. | |
13f2b9d7 | 58 | * |
89280b1a UH |
59 | * @return A GSList of strings containing the path of the serial device or |
60 | * NULL if no serial device is found. The returned list must be freed | |
61 | * by the caller. | |
13f2b9d7 | 62 | */ |
89280b1a | 63 | static GSList *auto_find_usb(uint16_t vendor_id, uint16_t product_id) |
13f2b9d7 DJ |
64 | { |
65 | #ifdef __linux__ | |
66 | const gchar *usb_dev; | |
67 | const char device_tree[] = "/sys/bus/usb/devices/"; | |
89280b1a | 68 | GDir *devices_dir, *device_dir; |
13f2b9d7 | 69 | GSList *l = NULL; |
89280b1a | 70 | GSList *tty_devs; |
13f2b9d7 | 71 | GSList *matched_paths; |
89280b1a UH |
72 | FILE *fd; |
73 | char tmp[5]; | |
74 | gchar *vendor_path, *product_path, *path_copy; | |
75 | gchar *prefix, *subdir_path, *device_path, *tty_path; | |
76 | unsigned long read_vendor_id, read_product_id; | |
77 | const char *file; | |
13f2b9d7 DJ |
78 | |
79 | l = NULL; | |
89280b1a | 80 | tty_devs = NULL; |
13f2b9d7 DJ |
81 | matched_paths = NULL; |
82 | ||
83 | if (!(devices_dir = g_dir_open(device_tree, 0, NULL))) | |
84 | return NULL; | |
85 | ||
86 | /* | |
87 | * Find potential candidates using the vendor ID and product ID | |
89280b1a | 88 | * and store them in matched_paths. |
13f2b9d7 DJ |
89 | */ |
90 | while ((usb_dev = g_dir_read_name(devices_dir))) { | |
13f2b9d7 DJ |
91 | vendor_path = g_strconcat(device_tree, |
92 | usb_dev, "/idVendor", NULL); | |
93 | product_path = g_strconcat(device_tree, | |
94 | usb_dev, "/idProduct", NULL); | |
95 | ||
96 | if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) || | |
97 | !g_file_test(product_path, G_FILE_TEST_EXISTS)) | |
98 | goto skip_device; | |
99 | ||
100 | if ((fd = g_fopen(vendor_path, "r")) == NULL) | |
101 | goto skip_device; | |
102 | ||
103 | if (fgets(tmp, sizeof(tmp), fd) == NULL) { | |
104 | fclose(fd); | |
105 | goto skip_device; | |
106 | } | |
107 | read_vendor_id = strtoul(tmp, NULL, 16); | |
108 | ||
109 | fclose(fd); | |
110 | ||
111 | if ((fd = g_fopen(product_path, "r")) == NULL) | |
112 | goto skip_device; | |
113 | ||
114 | if (fgets(tmp, sizeof(tmp), fd) == NULL) { | |
115 | fclose(fd); | |
116 | goto skip_device; | |
117 | } | |
118 | read_product_id = strtoul(tmp, NULL, 16); | |
119 | ||
120 | fclose(fd); | |
121 | ||
122 | if (vendor_id == read_vendor_id && | |
123 | product_id == read_product_id) { | |
13f2b9d7 DJ |
124 | path_copy = g_strdup(usb_dev); |
125 | matched_paths = g_slist_prepend(matched_paths, | |
126 | path_copy); | |
127 | } | |
128 | ||
89280b1a | 129 | skip_device: |
13f2b9d7 DJ |
130 | g_free(vendor_path); |
131 | g_free(product_path); | |
132 | } | |
133 | g_dir_close(devices_dir); | |
134 | ||
89280b1a | 135 | /* For every matched device try to find a ttyUSBX subfolder. */ |
13f2b9d7 | 136 | for (l = matched_paths; l; l = l->next) { |
13f2b9d7 DJ |
137 | subdir_path = NULL; |
138 | ||
139 | device_path = g_strconcat(device_tree, l->data, NULL); | |
140 | ||
141 | if (!(device_dir = g_dir_open(device_path, 0, NULL))) { | |
142 | g_free(device_path); | |
143 | continue; | |
144 | } | |
145 | ||
146 | prefix = g_strconcat(l->data, ":", NULL); | |
147 | ||
148 | while ((file = g_dir_read_name(device_dir))) { | |
149 | if (g_str_has_prefix(file, prefix)) { | |
150 | subdir_path = g_strconcat(device_path, | |
89280b1a | 151 | "/", file, NULL); |
13f2b9d7 DJ |
152 | break; |
153 | } | |
154 | } | |
155 | g_dir_close(device_dir); | |
156 | ||
157 | g_free(prefix); | |
158 | g_free(device_path); | |
159 | ||
160 | if (subdir_path) { | |
161 | if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) { | |
162 | g_free(subdir_path); | |
163 | continue; | |
164 | } | |
165 | g_free(subdir_path); | |
166 | ||
167 | while ((file = g_dir_read_name(device_dir))) { | |
168 | if (g_str_has_prefix(file, "ttyUSB")) { | |
13f2b9d7 DJ |
169 | tty_path = g_strconcat("/dev/", |
170 | file, NULL); | |
89280b1a | 171 | sr_dbg("Found USB device %04x:%04x attached to %s.", |
13f2b9d7 | 172 | vendor_id, product_id, tty_path); |
89280b1a UH |
173 | tty_devs = g_slist_prepend(tty_devs, |
174 | tty_path); | |
13f2b9d7 DJ |
175 | break; |
176 | } | |
177 | } | |
178 | g_dir_close(device_dir); | |
179 | } | |
180 | } | |
181 | g_slist_free_full(matched_paths, g_free); | |
182 | ||
89280b1a | 183 | return tty_devs; |
13f2b9d7 DJ |
184 | #else |
185 | return NULL; | |
186 | #endif | |
187 | } | |
188 | ||
06a3e78a DJ |
189 | static GSList *scan(GSList *options) |
190 | { | |
06a3e78a | 191 | GSList *devices; |
13f2b9d7 DJ |
192 | struct drv_context *drvc; |
193 | struct sr_dev_inst *sdi; | |
89280b1a UH |
194 | const char *serial_device, *serial_options; |
195 | GSList *l, *tty_devs; | |
196 | unsigned int i; | |
06a3e78a | 197 | |
13f2b9d7 DJ |
198 | serial_device = NULL; |
199 | serial_options = SERIALCOMM; | |
13f2b9d7 | 200 | sdi = NULL; |
06a3e78a DJ |
201 | devices = NULL; |
202 | drvc = di->priv; | |
203 | drvc->instances = NULL; | |
204 | ||
13f2b9d7 DJ |
205 | if (sr_serial_extract_options(options, &serial_device, |
206 | &serial_options) == SR_OK) { | |
719eff68 | 207 | sdi = hmo_probe_serial_device(serial_device, serial_options); |
13f2b9d7 DJ |
208 | if (sdi != NULL) { |
209 | devices = g_slist_append(devices, sdi); | |
210 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
211 | } | |
13f2b9d7 | 212 | } else { |
89280b1a | 213 | tty_devs = NULL; |
13f2b9d7 DJ |
214 | |
215 | for (i = 0; i < ARRAY_SIZE(ho_models); i++) { | |
216 | if ((l = auto_find_usb(ho_models[i].vendor_id, | |
217 | ho_models[i].product_id)) == NULL) | |
218 | continue; | |
89280b1a | 219 | tty_devs = g_slist_concat(tty_devs, l); |
13f2b9d7 DJ |
220 | } |
221 | ||
89280b1a | 222 | for (l = tty_devs; l; l = l->next) { |
719eff68 | 223 | sdi = hmo_probe_serial_device(l->data, serial_options); |
13f2b9d7 DJ |
224 | if (sdi != NULL) { |
225 | devices = g_slist_append(devices, sdi); | |
226 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
227 | } | |
228 | } | |
229 | ||
89280b1a | 230 | g_slist_free_full(tty_devs, g_free); |
13f2b9d7 | 231 | } |
06a3e78a DJ |
232 | |
233 | return devices; | |
234 | } | |
235 | ||
236 | static GSList *dev_list(void) | |
237 | { | |
238 | return ((struct drv_context *)(di->priv))->instances; | |
239 | } | |
240 | ||
13f2b9d7 DJ |
241 | static void clear_helper(void *priv) |
242 | { | |
243 | unsigned int i; | |
13f2b9d7 DJ |
244 | struct dev_context *devc; |
245 | struct scope_config *model; | |
246 | ||
247 | devc = priv; | |
248 | model = devc->model_config; | |
249 | ||
719eff68 | 250 | hmo_scope_state_free(devc->model_state); |
13f2b9d7 | 251 | |
89280b1a | 252 | for (i = 0; i < model->analog_channels; ++i) |
13f2b9d7 | 253 | g_slist_free(devc->analog_groups[i].probes); |
13f2b9d7 DJ |
254 | |
255 | for (i = 0; i < model->digital_pods; ++i) { | |
256 | g_slist_free(devc->digital_groups[i].probes); | |
257 | g_free(devc->digital_groups[i].name); | |
258 | } | |
259 | ||
260 | g_free(devc->analog_groups); | |
261 | g_free(devc->digital_groups); | |
262 | ||
263 | g_free(devc); | |
264 | } | |
265 | ||
06a3e78a DJ |
266 | static int dev_clear(void) |
267 | { | |
13f2b9d7 | 268 | return std_dev_clear(di, clear_helper); |
06a3e78a DJ |
269 | } |
270 | ||
271 | static int dev_open(struct sr_dev_inst *sdi) | |
272 | { | |
13f2b9d7 DJ |
273 | if (sdi->status != SR_ST_ACTIVE && |
274 | serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
275 | return SR_ERR; | |
06a3e78a | 276 | |
719eff68 | 277 | if (hmo_scope_state_get(sdi) != SR_OK) |
13f2b9d7 | 278 | return SR_ERR; |
06a3e78a DJ |
279 | |
280 | sdi->status = SR_ST_ACTIVE; | |
281 | ||
282 | return SR_OK; | |
283 | } | |
284 | ||
285 | static int dev_close(struct sr_dev_inst *sdi) | |
286 | { | |
13f2b9d7 DJ |
287 | if (sdi->status == SR_ST_INACTIVE) |
288 | return SR_OK; | |
06a3e78a | 289 | |
13f2b9d7 | 290 | serial_close(sdi->conn); |
06a3e78a DJ |
291 | |
292 | sdi->status = SR_ST_INACTIVE; | |
293 | ||
294 | return SR_OK; | |
295 | } | |
296 | ||
297 | static int cleanup(void) | |
298 | { | |
299 | dev_clear(); | |
300 | ||
06a3e78a DJ |
301 | return SR_OK; |
302 | } | |
303 | ||
13f2b9d7 DJ |
304 | static int check_probe_group(struct dev_context *devc, |
305 | const struct sr_probe_group *probe_group) | |
306 | { | |
307 | unsigned int i; | |
308 | struct scope_config *model; | |
309 | ||
310 | model = devc->model_config; | |
311 | ||
312 | if (!probe_group) | |
313 | return PG_NONE; | |
314 | ||
315 | for (i = 0; i < model->analog_channels; ++i) | |
316 | if (probe_group == &devc->analog_groups[i]) | |
317 | return PG_ANALOG; | |
318 | ||
319 | for (i = 0; i < model->digital_pods; ++i) | |
320 | if (probe_group == &devc->digital_groups[i]) | |
321 | return PG_DIGITAL; | |
322 | ||
323 | sr_err("Invalid probe group specified."); | |
89280b1a | 324 | |
13f2b9d7 DJ |
325 | return PG_INVALID; |
326 | } | |
327 | ||
328 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
329 | const struct sr_probe_group *probe_group) | |
06a3e78a | 330 | { |
89280b1a | 331 | int ret, pg_type; |
13f2b9d7 | 332 | unsigned int i; |
13f2b9d7 DJ |
333 | struct dev_context *devc; |
334 | struct scope_config *model; | |
335 | ||
336 | if (!sdi || !(devc = sdi->priv)) | |
337 | return SR_ERR_ARG; | |
338 | ||
339 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) | |
340 | return SR_ERR; | |
06a3e78a | 341 | |
13f2b9d7 DJ |
342 | ret = SR_ERR_NA; |
343 | model = devc->model_config; | |
06a3e78a | 344 | |
06a3e78a | 345 | switch (key) { |
13f2b9d7 DJ |
346 | case SR_CONF_NUM_TIMEBASE: |
347 | *data = g_variant_new_int32(model->num_xdivs); | |
348 | ret = SR_OK; | |
349 | break; | |
13f2b9d7 DJ |
350 | case SR_CONF_NUM_VDIV: |
351 | if (pg_type == PG_NONE) { | |
352 | sr_err("No probe group specified."); | |
353 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 DJ |
354 | } else if (pg_type == PG_ANALOG) { |
355 | for (i = 0; i < model->analog_channels; ++i) { | |
356 | if (probe_group == &devc->analog_groups[i]) { | |
357 | *data = g_variant_new_int32(model->num_ydivs); | |
358 | ret = SR_OK; | |
359 | break; | |
360 | } | |
361 | } | |
362 | ||
363 | } else { | |
364 | ret = SR_ERR_NA; | |
365 | } | |
366 | break; | |
06a3e78a | 367 | default: |
13f2b9d7 | 368 | ret = SR_ERR_NA; |
06a3e78a DJ |
369 | } |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
13f2b9d7 DJ |
374 | static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n) |
375 | { | |
376 | unsigned int i; | |
13f2b9d7 DJ |
377 | GVariant *rational[2]; |
378 | GVariantBuilder gvb; | |
379 | ||
380 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
381 | ||
382 | for (i = 0; i < n; i++) { | |
383 | rational[0] = g_variant_new_uint64((*array)[i][0]); | |
384 | rational[1] = g_variant_new_uint64((*array)[i][1]); | |
385 | ||
89280b1a | 386 | /* FIXME: Valgrind reports a memory leak here. */ |
13f2b9d7 DJ |
387 | g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); |
388 | } | |
389 | ||
390 | return g_variant_builder_end(&gvb); | |
391 | } | |
392 | ||
393 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
394 | const struct sr_probe_group *probe_group) | |
06a3e78a | 395 | { |
89280b1a UH |
396 | int ret, pg_type; |
397 | unsigned int i, j; | |
13f2b9d7 | 398 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 DJ |
399 | struct dev_context *devc; |
400 | struct scope_config *model; | |
401 | struct scope_state *state; | |
89280b1a UH |
402 | const char *tmp; |
403 | uint64_t p, q, tmp_u64; | |
404 | double tmp_d; | |
06a3e78a | 405 | |
13f2b9d7 DJ |
406 | if (!sdi || !(devc = sdi->priv)) |
407 | return SR_ERR_ARG; | |
408 | ||
409 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) | |
410 | return SR_ERR; | |
411 | ||
412 | model = devc->model_config; | |
413 | state = devc->model_state; | |
414 | ||
415 | ret = SR_ERR_NA; | |
06a3e78a | 416 | |
06a3e78a | 417 | switch (key) { |
13f2b9d7 DJ |
418 | case SR_CONF_LIMIT_FRAMES: |
419 | devc->frame_limit = g_variant_get_uint64(data); | |
420 | ret = SR_OK; | |
421 | break; | |
13f2b9d7 | 422 | case SR_CONF_TRIGGER_SOURCE: |
13f2b9d7 | 423 | tmp = g_variant_get_string(data, NULL); |
13f2b9d7 DJ |
424 | for (i = 0; (*model->trigger_sources)[i]; i++) { |
425 | if (!g_strcmp0(tmp, (*model->trigger_sources)[i])) { | |
426 | state->trigger_source = i; | |
13f2b9d7 DJ |
427 | g_snprintf(command, sizeof(command), |
428 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE], | |
429 | (*model->trigger_sources)[i]); | |
430 | ||
431 | ret = sr_scpi_send(sdi->conn, command); | |
432 | break; | |
433 | } | |
434 | } | |
89280b1a | 435 | break; |
13f2b9d7 | 436 | case SR_CONF_VDIV: |
13f2b9d7 DJ |
437 | if (pg_type == PG_NONE) { |
438 | sr_err("No probe group specified."); | |
439 | return SR_ERR_PROBE_GROUP; | |
440 | } | |
441 | ||
442 | g_variant_get(data, "(tt)", &p, &q); | |
443 | ||
444 | for (i = 0; i < model->num_vdivs; i++) { | |
445 | if (p == (*model->vdivs)[i][0] && | |
89280b1a | 446 | q == (*model->vdivs)[i][1]) { |
13f2b9d7 DJ |
447 | for (j = 1; j <= model->analog_channels; ++j) { |
448 | if (probe_group == &devc->analog_groups[j - 1]) { | |
89280b1a | 449 | state->analog_channels[j - 1].vdiv = (float) p / q; |
13f2b9d7 DJ |
450 | g_snprintf(command, sizeof(command), |
451 | (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV], | |
452 | j, state->analog_channels[j-1].vdiv); | |
453 | ||
454 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
455 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
456 | return SR_ERR; | |
457 | ||
458 | break; | |
459 | } | |
460 | } | |
461 | ||
462 | ret = SR_OK; | |
463 | break; | |
464 | } | |
465 | } | |
89280b1a | 466 | break; |
13f2b9d7 | 467 | case SR_CONF_TIMEBASE: |
13f2b9d7 DJ |
468 | g_variant_get(data, "(tt)", &p, &q); |
469 | ||
470 | for (i = 0; i < model->num_timebases; i++) { | |
471 | if (p == (*model->timebases)[i][0] && | |
89280b1a | 472 | q == (*model->timebases)[i][1]) { |
13f2b9d7 DJ |
473 | state->timebase = (float) p / q; |
474 | g_snprintf(command, sizeof(command), | |
475 | (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE], | |
476 | state->timebase); | |
477 | ||
478 | ret = sr_scpi_send(sdi->conn, command); | |
479 | break; | |
480 | } | |
481 | } | |
89280b1a | 482 | break; |
13f2b9d7 | 483 | case SR_CONF_HORIZ_TRIGGERPOS: |
89280b1a | 484 | tmp_d = g_variant_get_double(data); |
13f2b9d7 | 485 | |
89280b1a | 486 | if (tmp_d < 0.0 || tmp_d > 1.0) |
13f2b9d7 DJ |
487 | return SR_ERR; |
488 | ||
89280b1a | 489 | state->horiz_triggerpos = -(tmp_d - 0.5) * state->timebase * model->num_xdivs; |
13f2b9d7 DJ |
490 | g_snprintf(command, sizeof(command), |
491 | (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS], | |
492 | state->horiz_triggerpos); | |
493 | ||
494 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 495 | break; |
13f2b9d7 | 496 | case SR_CONF_TRIGGER_SLOPE: |
89280b1a | 497 | tmp_u64 = g_variant_get_uint64(data); |
13f2b9d7 | 498 | |
89280b1a | 499 | if (tmp_u64 != 0 && tmp_u64 != 1) |
13f2b9d7 DJ |
500 | return SR_ERR; |
501 | ||
89280b1a | 502 | state->trigger_slope = tmp_u64; |
13f2b9d7 DJ |
503 | |
504 | g_snprintf(command, sizeof(command), | |
505 | (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE], | |
89280b1a | 506 | tmp_u64 ? "POS" : "NEG"); |
13f2b9d7 DJ |
507 | |
508 | ret = sr_scpi_send(sdi->conn, command); | |
89280b1a | 509 | break; |
13f2b9d7 | 510 | case SR_CONF_COUPLING: |
13f2b9d7 DJ |
511 | if (pg_type == PG_NONE) { |
512 | sr_err("No probe group specified."); | |
513 | return SR_ERR_PROBE_GROUP; | |
514 | } | |
515 | ||
516 | tmp = g_variant_get_string(data, NULL); | |
517 | ||
518 | for (i = 0; (*model->coupling_options)[i]; i++) { | |
519 | if (!strcmp(tmp, (*model->coupling_options)[i])) { | |
520 | for (j = 1; j <= model->analog_channels; ++j) { | |
521 | if (probe_group == &devc->analog_groups[j - 1]) { | |
522 | state->analog_channels[j-1].coupling = i; | |
523 | ||
524 | g_snprintf(command, sizeof(command), | |
525 | (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING], | |
526 | j, tmp); | |
527 | ||
528 | if (sr_scpi_send(sdi->conn, command) != SR_OK || | |
529 | sr_scpi_get_opc(sdi->conn) != SR_OK) | |
530 | return SR_ERR; | |
531 | break; | |
532 | } | |
533 | } | |
534 | ||
535 | ret = SR_OK; | |
536 | break; | |
537 | } | |
538 | } | |
89280b1a | 539 | break; |
06a3e78a DJ |
540 | default: |
541 | ret = SR_ERR_NA; | |
13f2b9d7 | 542 | break; |
06a3e78a DJ |
543 | } |
544 | ||
13f2b9d7 DJ |
545 | if (ret == SR_OK) |
546 | ret = sr_scpi_get_opc(sdi->conn); | |
547 | ||
06a3e78a DJ |
548 | return ret; |
549 | } | |
550 | ||
13f2b9d7 | 551 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
89280b1a | 552 | const struct sr_probe_group *probe_group) |
06a3e78a | 553 | { |
13f2b9d7 | 554 | int pg_type; |
13f2b9d7 DJ |
555 | struct dev_context *devc; |
556 | struct scope_config *model; | |
557 | ||
558 | if (!sdi || !(devc = sdi->priv)) | |
559 | return SR_ERR_ARG; | |
06a3e78a | 560 | |
13f2b9d7 DJ |
561 | if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID) |
562 | return SR_ERR; | |
563 | ||
564 | model = devc->model_config; | |
06a3e78a | 565 | |
06a3e78a | 566 | switch (key) { |
13f2b9d7 DJ |
567 | case SR_CONF_DEVICE_OPTIONS: |
568 | if (pg_type == PG_NONE) { | |
569 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a UH |
570 | model->hw_caps, model->num_hwcaps, |
571 | sizeof(int32_t)); | |
13f2b9d7 DJ |
572 | } else if (pg_type == PG_ANALOG) { |
573 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a UH |
574 | model->analog_hwcaps, model->num_analog_hwcaps, |
575 | sizeof(int32_t)); | |
13f2b9d7 DJ |
576 | } else { |
577 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
89280b1a | 578 | NULL, 0, sizeof(int32_t)); |
13f2b9d7 DJ |
579 | } |
580 | break; | |
13f2b9d7 DJ |
581 | case SR_CONF_COUPLING: |
582 | if (pg_type == PG_NONE) | |
583 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 | 584 | *data = g_variant_new_strv(*model->coupling_options, |
89280b1a | 585 | g_strv_length((char **)*model->coupling_options)); |
13f2b9d7 | 586 | break; |
13f2b9d7 DJ |
587 | case SR_CONF_TRIGGER_SOURCE: |
588 | *data = g_variant_new_strv(*model->trigger_sources, | |
89280b1a | 589 | g_strv_length((char **)*model->trigger_sources)); |
13f2b9d7 | 590 | break; |
13f2b9d7 DJ |
591 | case SR_CONF_TIMEBASE: |
592 | *data = build_tuples(model->timebases, model->num_timebases); | |
593 | break; | |
13f2b9d7 DJ |
594 | case SR_CONF_VDIV: |
595 | if (pg_type == PG_NONE) | |
596 | return SR_ERR_PROBE_GROUP; | |
13f2b9d7 DJ |
597 | *data = build_tuples(model->vdivs, model->num_vdivs); |
598 | break; | |
06a3e78a DJ |
599 | default: |
600 | return SR_ERR_NA; | |
601 | } | |
602 | ||
13f2b9d7 | 603 | return SR_OK; |
06a3e78a DJ |
604 | } |
605 | ||
13f2b9d7 | 606 | SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi) |
06a3e78a | 607 | { |
13f2b9d7 | 608 | char command[MAX_COMMAND_SIZE]; |
13f2b9d7 DJ |
609 | struct sr_probe *probe; |
610 | struct dev_context *devc; | |
611 | struct scope_config *model; | |
612 | ||
613 | devc = sdi->priv; | |
614 | model = devc->model_config; | |
615 | ||
616 | probe = devc->current_probe->data; | |
617 | ||
618 | switch (probe->type) { | |
619 | case SR_PROBE_ANALOG: | |
620 | g_snprintf(command, sizeof(command), | |
621 | (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA], | |
622 | probe->index + 1); | |
623 | break; | |
624 | case SR_PROBE_LOGIC: | |
625 | g_snprintf(command, sizeof(command), | |
626 | (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA], | |
627 | probe->index < 8 ? 1 : 2); | |
628 | break; | |
629 | default: | |
89280b1a | 630 | sr_err("Invalid probe type."); |
13f2b9d7 DJ |
631 | break; |
632 | } | |
633 | ||
634 | return sr_scpi_send(sdi->conn, command); | |
635 | } | |
636 | ||
637 | static int hmo_check_probes(GSList *probes) | |
638 | { | |
639 | GSList *l; | |
13f2b9d7 | 640 | struct sr_probe *probe; |
89280b1a | 641 | gboolean enabled_pod1, enabled_pod2, enabled_chan3, enabled_chan4; |
13f2b9d7 | 642 | |
89280b1a | 643 | enabled_pod1 = enabled_pod2 = enabled_chan3 = enabled_chan4 = FALSE; |
13f2b9d7 DJ |
644 | |
645 | for (l = probes; l; l = l->next) { | |
646 | probe = l->data; | |
13f2b9d7 DJ |
647 | switch (probe->type) { |
648 | case SR_PROBE_ANALOG: | |
649 | if (probe->index == 2) | |
650 | enabled_chan3 = TRUE; | |
651 | else if (probe->index == 3) | |
652 | enabled_chan4 = TRUE; | |
653 | break; | |
13f2b9d7 DJ |
654 | case SR_PROBE_LOGIC: |
655 | if (probe->index < 8) | |
656 | enabled_pod1 = TRUE; | |
657 | else | |
658 | enabled_pod2 = TRUE; | |
659 | break; | |
13f2b9d7 DJ |
660 | default: |
661 | return SR_ERR; | |
662 | } | |
663 | } | |
664 | ||
665 | if ((enabled_pod1 && enabled_chan3) || | |
666 | (enabled_pod2 && enabled_chan4)) | |
667 | return SR_ERR; | |
668 | ||
669 | return SR_OK; | |
670 | } | |
671 | ||
672 | static int hmo_setup_probes(const struct sr_dev_inst *sdi) | |
673 | { | |
674 | GSList *l; | |
675 | unsigned int i; | |
676 | gboolean *pod_enabled; | |
677 | char command[MAX_COMMAND_SIZE]; | |
13f2b9d7 DJ |
678 | struct scope_state *state; |
679 | struct scope_config *model; | |
13f2b9d7 DJ |
680 | struct sr_probe *probe; |
681 | struct dev_context *devc; | |
682 | struct sr_serial_dev_inst *serial; | |
683 | ||
684 | devc = sdi->priv; | |
685 | serial = sdi->conn; | |
686 | state = devc->model_state; | |
687 | model = devc->model_config; | |
688 | ||
689 | pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods); | |
690 | ||
691 | for (l = sdi->probes; l; l = l->next) { | |
692 | probe = l->data; | |
13f2b9d7 DJ |
693 | switch (probe->type) { |
694 | case SR_PROBE_ANALOG: | |
13f2b9d7 DJ |
695 | if (probe->enabled != state->analog_channels[probe->index].state) { |
696 | g_snprintf(command, sizeof(command), | |
697 | (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE], | |
698 | probe->index + 1, probe->enabled); | |
699 | ||
700 | if (sr_scpi_send(serial, command) != SR_OK) | |
701 | return SR_ERR; | |
702 | state->analog_channels[probe->index].state = probe->enabled; | |
703 | } | |
89280b1a | 704 | break; |
13f2b9d7 | 705 | case SR_PROBE_LOGIC: |
13f2b9d7 DJ |
706 | /* |
707 | * A digital POD needs to be enabled for every group of | |
708 | * 8 probes. | |
709 | */ | |
710 | if (probe->enabled) | |
711 | pod_enabled[probe->index < 8 ? 0 : 1] = TRUE; | |
712 | ||
713 | if (probe->enabled != state->digital_channels[probe->index]) { | |
714 | g_snprintf(command, sizeof(command), | |
715 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE], | |
716 | probe->index, probe->enabled); | |
717 | ||
718 | if (sr_scpi_send(serial, command) != SR_OK) | |
719 | return SR_ERR; | |
720 | ||
721 | state->digital_channels[probe->index] = probe->enabled; | |
722 | } | |
89280b1a | 723 | break; |
13f2b9d7 DJ |
724 | default: |
725 | return SR_ERR; | |
726 | } | |
727 | } | |
728 | ||
729 | for (i = 1; i <= model->digital_pods; ++i) { | |
89280b1a | 730 | if (state->digital_pods[i - 1] != pod_enabled[i - 1]) { |
13f2b9d7 DJ |
731 | g_snprintf(command, sizeof(command), |
732 | (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE], | |
89280b1a | 733 | i, pod_enabled[i - 1]); |
13f2b9d7 DJ |
734 | |
735 | if (sr_scpi_send(serial, command) != SR_OK) | |
736 | return SR_ERR; | |
737 | ||
89280b1a | 738 | state->digital_pods[i - 1] = pod_enabled[i - 1]; |
13f2b9d7 DJ |
739 | } |
740 | } | |
741 | ||
742 | g_free(pod_enabled); | |
743 | ||
744 | return SR_OK; | |
745 | } | |
746 | ||
747 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
748 | { | |
749 | GSList *l; | |
750 | gboolean digital_added; | |
13f2b9d7 DJ |
751 | struct sr_probe *probe; |
752 | struct dev_context *devc; | |
753 | struct sr_serial_dev_inst *serial; | |
06a3e78a DJ |
754 | |
755 | if (sdi->status != SR_ST_ACTIVE) | |
756 | return SR_ERR_DEV_CLOSED; | |
757 | ||
13f2b9d7 DJ |
758 | serial = sdi->conn; |
759 | devc = sdi->priv; | |
760 | digital_added = FALSE; | |
761 | ||
762 | for (l = sdi->probes; l; l = l->next) { | |
763 | probe = l->data; | |
13f2b9d7 | 764 | if (probe->enabled) { |
89280b1a UH |
765 | /* Only add a single digital probe. */ |
766 | if (probe->type != SR_PROBE_LOGIC || !digital_added) { | |
767 | devc->enabled_probes = g_slist_append( | |
768 | devc->enabled_probes, probe); | |
13f2b9d7 DJ |
769 | if (probe->type == SR_PROBE_LOGIC) |
770 | digital_added = TRUE; | |
771 | } | |
772 | } | |
773 | } | |
06a3e78a | 774 | |
13f2b9d7 DJ |
775 | if (!devc->enabled_probes) |
776 | return SR_ERR; | |
777 | ||
778 | if (hmo_check_probes(devc->enabled_probes) != SR_OK) { | |
779 | sr_err("Invalid probe configuration specified!"); | |
780 | return SR_ERR_NA; | |
781 | } | |
782 | ||
783 | if (hmo_setup_probes(sdi) != SR_OK) { | |
784 | sr_err("Failed to setup probe configuration!"); | |
785 | return SR_ERR; | |
786 | } | |
787 | ||
719eff68 | 788 | sr_source_add(serial->fd, G_IO_IN, 50, hmo_receive_data, (void *)sdi); |
13f2b9d7 DJ |
789 | |
790 | /* Send header packet to the session bus. */ | |
791 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
792 | ||
793 | devc->current_probe = devc->enabled_probes; | |
794 | ||
795 | return hmo_request_data(sdi); | |
06a3e78a DJ |
796 | } |
797 | ||
798 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
799 | { | |
13f2b9d7 DJ |
800 | struct dev_context *devc; |
801 | struct sr_serial_dev_inst *serial; | |
802 | ||
06a3e78a DJ |
803 | (void)cb_data; |
804 | ||
805 | if (sdi->status != SR_ST_ACTIVE) | |
806 | return SR_ERR_DEV_CLOSED; | |
807 | ||
13f2b9d7 DJ |
808 | devc = sdi->priv; |
809 | ||
810 | g_slist_free(devc->enabled_probes); | |
811 | devc->enabled_probes = NULL; | |
812 | serial = sdi->conn; | |
813 | sr_source_remove(serial->fd); | |
06a3e78a DJ |
814 | |
815 | return SR_OK; | |
816 | } | |
817 | ||
818 | SR_PRIV struct sr_dev_driver hameg_hmo_driver_info = { | |
819 | .name = "hameg-hmo", | |
89280b1a | 820 | .longname = "Hameg HMO", |
06a3e78a DJ |
821 | .api_version = 1, |
822 | .init = init, | |
823 | .cleanup = cleanup, | |
824 | .scan = scan, | |
825 | .dev_list = dev_list, | |
826 | .dev_clear = dev_clear, | |
827 | .config_get = config_get, | |
828 | .config_set = config_set, | |
829 | .config_list = config_list, | |
830 | .dev_open = dev_open, | |
831 | .dev_close = dev_close, | |
832 | .dev_acquisition_start = dev_acquisition_start, | |
833 | .dev_acquisition_stop = dev_acquisition_stop, | |
834 | .priv = NULL, | |
835 | }; |