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 sr_global *global;
26 GSList *devices = NULL;
28 void device_scan(void)
31 struct sr_device_plugin *plugin;
33 plugins = list_hwplugins();
36 * Initialize all plugins first. Since the init() call may involve
37 * a firmware upload and associated delay, we may as well get all
38 * of these out of the way first.
40 for (l = plugins; l; l = l->next) {
42 device_plugin_init(plugin);
47 int device_plugin_init(struct sr_device_plugin *plugin)
49 int num_devices, num_probes, i;
51 g_message("initializing %s plugin", plugin->name);
52 num_devices = plugin->init(NULL);
53 for (i = 0; i < num_devices; i++) {
54 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
55 device_new(plugin, i, num_probes);
61 void device_close_all(void)
63 struct sr_device *device;
66 device = devices->data;
68 device->plugin->close(device->plugin_index);
69 device_destroy(device);
73 GSList *device_list(void)
82 struct sr_device *device_new(struct sr_device_plugin *plugin, int plugin_index,
85 struct sr_device *device;
88 device = g_malloc0(sizeof(struct sr_device));
89 device->plugin = plugin;
90 device->plugin_index = plugin_index;
91 devices = g_slist_append(devices, device);
93 for (i = 0; i < num_probes; i++)
94 device_probe_add(device, NULL);
99 void device_clear(struct sr_device *device)
103 /* TODO: Plugin-specific clear call? */
108 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
109 device_probe_clear(device, pnum);
112 void device_destroy(struct sr_device *device)
117 * TODO: Plugin-specific destroy call, need to decrease refcount
121 devices = g_slist_remove(devices, device);
122 if (device->probes) {
123 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
124 device_probe_clear(device, pnum);
125 g_slist_free(device->probes);
130 void device_probe_clear(struct sr_device *device, int probenum)
134 p = probe_find(device, probenum);
149 void device_probe_add(struct sr_device *device, char *name)
155 probenum = g_slist_length(device->probes) + 1;
156 p = g_malloc0(sizeof(struct probe));
160 p->name = g_strdup(name);
162 snprintf(probename, 16, "%d", probenum);
163 p->name = g_strdup(probename);
165 p->name = g_strdup(name);
167 device->probes = g_slist_append(device->probes, p);
170 struct probe *probe_find(struct sr_device *device, int probenum)
173 struct probe *p, *found_probe;
176 for (l = device->probes; l; l = l->next) {
178 if (p->index == probenum) {
187 /* TODO: return SIGROK_ERR if probenum not found */
188 void device_probe_name(struct sr_device *device, int probenum, char *name)
192 p = probe_find(device, probenum);
198 p->name = g_strdup(name);
201 /* TODO: return SIGROK_ERR if probenum not found */
202 void device_trigger_clear(struct sr_device *device)
210 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
211 p = probe_find(device, pnum);
212 if (p && p->trigger) {
219 /* TODO: return SIGROK_ERR if probenum not found */
220 void device_trigger_set(struct sr_device *device, int probenum, char *trigger)
224 p = probe_find(device, probenum);
231 p->trigger = g_strdup(trigger);
235 gboolean device_has_hwcap(struct sr_device *device, int hwcap)
237 int *capabilities, i;
239 if ((capabilities = device->plugin->get_capabilities()))
240 for (i = 0; capabilities[i]; i++)
241 if (capabilities[i] == hwcap)