2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #include <sigrok-internal.h>
25 extern struct sr_global *global;
27 GSList *devices = NULL;
29 void sr_device_scan(void)
32 struct sr_device_plugin *plugin;
34 plugins = sr_list_hwplugins();
37 * Initialize all plugins first. Since the init() call may involve
38 * a firmware upload and associated delay, we may as well get all
39 * of these out of the way first.
41 for (l = plugins; l; l = l->next) {
43 sr_device_plugin_init(plugin);
48 int sr_device_plugin_init(struct sr_device_plugin *plugin)
50 int num_devices, num_probes, i;
52 sr_info("initializing %s plugin", plugin->name);
53 num_devices = plugin->init(NULL);
54 for (i = 0; i < num_devices; i++) {
55 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
56 sr_device_new(plugin, i, num_probes);
62 void sr_device_close_all(void)
64 struct sr_device *device;
67 device = devices->data;
68 if (device->plugin && device->plugin->close)
69 device->plugin->close(device->plugin_index);
70 sr_device_destroy(device);
74 GSList *sr_device_list(void)
83 struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
86 struct sr_device *device;
89 if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
90 sr_err("dev: %s: device malloc failed", __func__);
94 device->plugin = plugin;
95 device->plugin_index = plugin_index;
96 devices = g_slist_append(devices, device);
98 for (i = 0; i < num_probes; i++)
99 sr_device_probe_add(device, NULL);
104 void sr_device_clear(struct sr_device *device)
108 /* TODO: Plugin-specific clear call? */
113 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
114 sr_device_probe_clear(device, pnum);
117 void sr_device_destroy(struct sr_device *device)
122 * TODO: Plugin-specific destroy call, need to decrease refcount
126 devices = g_slist_remove(devices, device);
127 if (device->probes) {
128 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
129 sr_device_probe_clear(device, pnum);
130 g_slist_free(device->probes);
135 void sr_device_probe_clear(struct sr_device *device, int probenum)
139 p = sr_device_probe_find(device, probenum);
154 void sr_device_probe_add(struct sr_device *device, const char *name)
160 probenum = g_slist_length(device->probes) + 1;
162 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
163 sr_err("dev: %s: p malloc failed", __func__);
164 // return SR_ERR_MALLOC;
165 return; /* FIXME: should return int. */
171 p->name = g_strdup(name);
173 snprintf(probename, 16, "%d", probenum);
174 p->name = g_strdup(probename);
177 device->probes = g_slist_append(device->probes, p);
180 struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
183 struct sr_probe *p, *found_probe;
186 for (l = device->probes; l; l = l->next) {
188 if (p->index == probenum) {
197 /* TODO: return SIGROK_ERR if probenum not found */
198 void sr_device_probe_name(struct sr_device *device, int probenum,
203 p = sr_device_probe_find(device, probenum);
209 p->name = g_strdup(name);
212 /* TODO: return SIGROK_ERR if probenum not found */
213 void sr_device_trigger_clear(struct sr_device *device)
221 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
222 p = sr_device_probe_find(device, pnum);
223 if (p && p->trigger) {
230 /* TODO: return SIGROK_ERR if probenum not found */
231 void sr_device_trigger_set(struct sr_device *device, int probenum,
236 p = sr_device_probe_find(device, probenum);
243 p->trigger = g_strdup(trigger);
247 gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
249 int *capabilities, i;
251 if (!device || !device->plugin)
254 if ((capabilities = device->plugin->get_capabilities()))
255 for (i = 0; capabilities[i]; i++)
256 if (capabilities[i] == hwcap)