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/>.
24 extern struct sigrok_global *global;
26 GSList *devices = NULL;
28 void device_scan(void)
31 struct device_plugin *plugin;
32 int num_devices, num_probes, i;
34 plugins = 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 g_message("initializing %s plugin", plugin->name);
44 num_devices = plugin->init(NULL);
45 for (i = 0; i < num_devices; i++) {
46 num_probes = (int)plugin->get_device_info(i,
48 device_new(plugin, i, num_probes);
53 void device_close_all(void)
55 struct device *device;
58 device = devices->data;
60 device->plugin->close(device->plugin_index);
61 device_destroy(device);
65 GSList *device_list(void)
70 struct device *device_new(struct device_plugin *plugin, int plugin_index,
73 struct device *device;
77 device = g_malloc0(sizeof(struct device));
78 device->plugin = plugin;
79 device->plugin_index = plugin_index;
80 devices = g_slist_append(devices, device);
82 for (i = 0; i < num_probes; i++) {
83 snprintf(probename, 16, "%d", i + 1);
84 device_probe_add(device, probename);
90 void device_clear(struct device *device)
94 /* TODO: Plugin-specific clear call? */
99 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
100 device_probe_clear(device, pnum);
103 void device_destroy(struct device *device)
108 * TODO: Plugin-specific destroy call, need to decrease refcount
112 devices = g_slist_remove(devices, device);
113 if (device->probes) {
114 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
115 device_probe_clear(device, pnum);
116 g_slist_free(device->probes);
121 void device_probe_clear(struct device *device, int probenum)
125 p = probe_find(device, probenum);
140 void device_probe_add(struct device *device, char *name)
144 p = g_malloc0(sizeof(struct probe));
145 p->index = g_slist_length(device->probes) + 1;
147 p->name = g_strdup(name);
149 device->probes = g_slist_append(device->probes, p);
152 struct probe *probe_find(struct device *device, int probenum)
155 struct probe *p, *found_probe;
158 for (l = device->probes; l; l = l->next) {
160 if (p->index == probenum) {
169 void device_probe_name(struct device *device, int probenum, char *name)
173 p = probe_find(device, probenum);
179 p->name = g_strdup(name);
182 void device_trigger_clear(struct device *device)
190 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
191 p = probe_find(device, pnum);
192 if (p && p->trigger) {
199 void device_trigger_set(struct device *device, int probenum, char *trigger)
203 p = probe_find(device, probenum);
210 p->trigger = g_strdup(trigger);