Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the sigrok-cli project. |
| 3 | * |
| 4 | * Copyright (C) 2013 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 | |
Uwe Hermann | 20fb52e | 2013-11-19 11:48:06 +0100 | [diff] [blame] | 20 | #include "sigrok-cli.h" |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include <glib.h> |
Uwe Hermann | 55de58a | 2014-01-28 15:55:24 +0100 | [diff] [blame] | 23 | #include <string.h> |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 24 | |
| 25 | extern struct sr_context *sr_ctx; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 26 | |
| 27 | /* Convert driver options hash to GSList of struct sr_config. */ |
| 28 | static GSList *hash_to_hwopt(GHashTable *hash) |
| 29 | { |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 30 | struct sr_config *src; |
| 31 | GList *gl, *keys; |
| 32 | GSList *opts; |
Bert Vermeulen | ad54a1a | 2013-12-26 12:08:34 +0100 | [diff] [blame] | 33 | char *key; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 34 | |
| 35 | keys = g_hash_table_get_keys(hash); |
| 36 | opts = NULL; |
| 37 | for (gl = keys; gl; gl = gl->next) { |
| 38 | key = gl->data; |
Bert Vermeulen | ad54a1a | 2013-12-26 12:08:34 +0100 | [diff] [blame] | 39 | src = g_malloc(sizeof(struct sr_config)); |
| 40 | if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 41 | return NULL; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 42 | opts = g_slist_append(opts, src); |
| 43 | } |
| 44 | g_list_free(keys); |
| 45 | |
| 46 | return opts; |
| 47 | } |
| 48 | |
| 49 | static void free_drvopts(struct sr_config *src) |
| 50 | { |
| 51 | g_variant_unref(src->data); |
| 52 | g_free(src); |
| 53 | } |
| 54 | |
| 55 | GSList *device_scan(void) |
| 56 | { |
| 57 | struct sr_dev_driver **drivers, *driver; |
| 58 | GHashTable *drvargs; |
| 59 | GSList *drvopts, *devices, *tmpdevs, *l; |
| 60 | int i; |
| 61 | char *drvname; |
| 62 | |
| 63 | if (opt_drv) { |
| 64 | drvargs = parse_generic_arg(opt_drv, TRUE); |
| 65 | drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); |
| 66 | g_hash_table_remove(drvargs, "sigrok_key"); |
| 67 | driver = NULL; |
| 68 | drivers = sr_driver_list(); |
| 69 | for (i = 0; drivers[i]; i++) { |
| 70 | if (strcmp(drivers[i]->name, drvname)) |
| 71 | continue; |
| 72 | driver = drivers[i]; |
| 73 | } |
| 74 | if (!driver) { |
| 75 | g_critical("Driver %s not found.", drvname); |
| 76 | g_hash_table_destroy(drvargs); |
| 77 | g_free(drvname); |
| 78 | return NULL; |
| 79 | } |
| 80 | g_free(drvname); |
| 81 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
| 82 | g_critical("Failed to initialize driver."); |
| 83 | g_hash_table_destroy(drvargs); |
| 84 | return NULL; |
| 85 | } |
| 86 | drvopts = NULL; |
| 87 | if (g_hash_table_size(drvargs) > 0) { |
| 88 | if (!(drvopts = hash_to_hwopt(drvargs))) { |
| 89 | /* Unknown options, already logged. */ |
| 90 | g_hash_table_destroy(drvargs); |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | g_hash_table_destroy(drvargs); |
| 95 | devices = sr_driver_scan(driver, drvopts); |
| 96 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); |
| 97 | } else { |
| 98 | /* No driver specified, let them all scan on their own. */ |
| 99 | devices = NULL; |
| 100 | drivers = sr_driver_list(); |
| 101 | for (i = 0; drivers[i]; i++) { |
| 102 | driver = drivers[i]; |
| 103 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
| 104 | g_critical("Failed to initialize driver."); |
| 105 | return NULL; |
| 106 | } |
| 107 | tmpdevs = sr_driver_scan(driver, NULL); |
| 108 | for (l = tmpdevs; l; l = l->next) |
| 109 | devices = g_slist_append(devices, l->data); |
| 110 | g_slist_free(tmpdevs); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return devices; |
| 115 | } |
| 116 | |
Uwe Hermann | ca50f4b | 2014-03-14 21:46:10 +0100 | [diff] [blame] | 117 | struct sr_channel_group *select_channel_group(struct sr_dev_inst *sdi) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 118 | { |
Uwe Hermann | ca50f4b | 2014-03-14 21:46:10 +0100 | [diff] [blame] | 119 | struct sr_channel_group *cg; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 120 | GSList *l; |
| 121 | |
Uwe Hermann | ca50f4b | 2014-03-14 21:46:10 +0100 | [diff] [blame] | 122 | if (!opt_channel_group) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 123 | return NULL; |
| 124 | |
Uwe Hermann | ca50f4b | 2014-03-14 21:46:10 +0100 | [diff] [blame] | 125 | if (!sdi->channel_groups) { |
| 126 | g_critical("This device does not have any channel groups."); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
| 129 | |
Uwe Hermann | ca50f4b | 2014-03-14 21:46:10 +0100 | [diff] [blame] | 130 | for (l = sdi->channel_groups; l; l = l->next) { |
| 131 | cg = l->data; |
| 132 | if (!strcasecmp(opt_channel_group, cg->name)) { |
| 133 | return cg; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
Bert Vermeulen | ac1e997 | 2014-09-09 19:54:36 +0200 | [diff] [blame^] | 136 | g_critical("Invalid channel group '%s'", opt_channel_group); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 137 | |
| 138 | return NULL; |
| 139 | } |
| 140 | |