]> sigrok.org Git - libsigrok.git/blame - device.c
Add sr_ prefix to list_hwplugins().
[libsigrok.git] / device.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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
20#include <stdio.h>
21#include <glib.h>
1b452b85 22#include <sigrok.h>
a1bb33af 23
a00ba012 24extern struct sr_global *global;
a1bb33af
UH
25
26GSList *devices = NULL;
27
2bf4aca6 28void sr_device_scan(void)
a1bb33af
UH
29{
30 GSList *plugins, *l;
5c2d46d1 31 struct sr_device_plugin *plugin;
a1bb33af 32
ee4b6342 33 plugins = sr_list_hwplugins();
a1bb33af 34
1b452b85
UH
35 /*
36 * Initialize all plugins first. Since the init() call may involve
a1bb33af
UH
37 * a firmware upload and associated delay, we may as well get all
38 * of these out of the way first.
39 */
1b452b85 40 for (l = plugins; l; l = l->next) {
a1bb33af 41 plugin = l->data;
2bf4aca6 42 sr_device_plugin_init(plugin);
a1bb33af 43 }
e54bcdc5
BV
44
45}
46
2bf4aca6 47int sr_device_plugin_init(struct sr_device_plugin *plugin)
e54bcdc5
BV
48{
49 int num_devices, num_probes, i;
50
51 g_message("initializing %s plugin", plugin->name);
52 num_devices = plugin->init(NULL);
53 for (i = 0; i < num_devices; i++) {
5a2326a7 54 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
2bf4aca6 55 sr_device_new(plugin, i, num_probes);
e54bcdc5
BV
56 }
57
58 return num_devices;
a1bb33af
UH
59}
60
2bf4aca6 61void sr_device_close_all(void)
a1bb33af 62{
5c2d46d1 63 struct sr_device *device;
a1bb33af 64
1b452b85 65 while (devices) {
a1bb33af 66 device = devices->data;
b8c2f85f 67 if (device->plugin && device->plugin->close)
873080cc 68 device->plugin->close(device->plugin_index);
2bf4aca6 69 sr_device_destroy(device);
a1bb33af 70 }
a1bb33af
UH
71}
72
2bf4aca6 73GSList *sr_device_list(void)
a1bb33af 74{
e54bcdc5
BV
75
76 if (!devices)
2bf4aca6 77 sr_device_scan();
e54bcdc5 78
a1bb33af
UH
79 return devices;
80}
81
2bf4aca6 82struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
5c2d46d1 83 int num_probes)
a1bb33af 84{
5c2d46d1 85 struct sr_device *device;
873080cc 86 int i;
a1bb33af 87
5c2d46d1 88 device = g_malloc0(sizeof(struct sr_device));
a1bb33af
UH
89 device->plugin = plugin;
90 device->plugin_index = plugin_index;
91 devices = g_slist_append(devices, device);
92
7d658874 93 for (i = 0; i < num_probes; i++)
2bf4aca6 94 sr_device_probe_add(device, NULL);
a1bb33af
UH
95
96 return device;
97}
98
2bf4aca6 99void sr_device_clear(struct sr_device *device)
a1bb33af 100{
1b452b85 101 unsigned int pnum;
a1bb33af 102
1b452b85 103 /* TODO: Plugin-specific clear call? */
a1bb33af 104
1b452b85
UH
105 if (!device->probes)
106 return;
a1bb33af 107
1b452b85 108 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 109 sr_device_probe_clear(device, pnum);
a1bb33af
UH
110}
111
2bf4aca6 112void sr_device_destroy(struct sr_device *device)
a1bb33af 113{
1b452b85 114 unsigned int pnum;
a1bb33af 115
1b452b85
UH
116 /*
117 * TODO: Plugin-specific destroy call, need to decrease refcount
118 * in plugin.
119 */
a1bb33af
UH
120
121 devices = g_slist_remove(devices, device);
1b452b85
UH
122 if (device->probes) {
123 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 124 sr_device_probe_clear(device, pnum);
a1bb33af
UH
125 g_slist_free(device->probes);
126 }
127 g_free(device);
a1bb33af
UH
128}
129
2bf4aca6 130void sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 131{
1afe8989 132 struct sr_probe *p;
a1bb33af 133
03dbc020 134 p = sr_device_probe_find(device, probenum);
1b452b85 135 if (!p)
a1bb33af
UH
136 return;
137
1b452b85 138 if (p->name) {
a1bb33af
UH
139 g_free(p->name);
140 p->name = NULL;
141 }
142
1b452b85 143 if (p->trigger) {
a1bb33af
UH
144 g_free(p->trigger);
145 p->trigger = NULL;
146 }
a1bb33af
UH
147}
148
2bf4aca6 149void sr_device_probe_add(struct sr_device *device, char *name)
a1bb33af 150{
1afe8989 151 struct sr_probe *p;
7d658874
BV
152 char probename[16];
153 int probenum;
a1bb33af 154
7d658874 155 probenum = g_slist_length(device->probes) + 1;
1afe8989 156 p = g_malloc0(sizeof(struct sr_probe));
7d658874 157 p->index = probenum;
a1bb33af 158 p->enabled = TRUE;
7d658874
BV
159 if (name) {
160 p->name = g_strdup(name);
161 } else {
162 snprintf(probename, 16, "%d", probenum);
163 p->name = g_strdup(probename);
164 }
a1bb33af
UH
165 p->trigger = NULL;
166 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
167}
168
03dbc020 169struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
a1bb33af
UH
170{
171 GSList *l;
1afe8989 172 struct sr_probe *p, *found_probe;
a1bb33af
UH
173
174 found_probe = NULL;
1b452b85 175 for (l = device->probes; l; l = l->next) {
a1bb33af 176 p = l->data;
1b452b85 177 if (p->index == probenum) {
a1bb33af
UH
178 found_probe = p;
179 break;
180 }
181 }
182
183 return found_probe;
184}
185
7d658874 186/* TODO: return SIGROK_ERR if probenum not found */
2bf4aca6 187void sr_device_probe_name(struct sr_device *device, int probenum, char *name)
a1bb33af 188{
1afe8989 189 struct sr_probe *p;
a1bb33af 190
03dbc020 191 p = sr_device_probe_find(device, probenum);
1b452b85 192 if (!p)
a1bb33af
UH
193 return;
194
1b452b85 195 if (p->name)
a1bb33af
UH
196 g_free(p->name);
197 p->name = g_strdup(name);
a1bb33af
UH
198}
199
7d658874 200/* TODO: return SIGROK_ERR if probenum not found */
2bf4aca6 201void sr_device_trigger_clear(struct sr_device *device)
a1bb33af 202{
1afe8989 203 struct sr_probe *p;
1b452b85 204 unsigned int pnum;
a1bb33af 205
1b452b85
UH
206 if (!device->probes)
207 return;
a1bb33af 208
1b452b85 209 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 210 p = sr_device_probe_find(device, pnum);
1b452b85
UH
211 if (p && p->trigger) {
212 g_free(p->trigger);
213 p->trigger = NULL;
214 }
215 }
216}
a1bb33af 217
7d658874 218/* TODO: return SIGROK_ERR if probenum not found */
2bf4aca6 219void sr_device_trigger_set(struct sr_device *device, int probenum, char *trigger)
a1bb33af 220{
1afe8989 221 struct sr_probe *p;
a1bb33af 222
03dbc020 223 p = sr_device_probe_find(device, probenum);
1b452b85 224 if (!p)
a1bb33af
UH
225 return;
226
1b452b85 227 if (p->trigger)
a1bb33af
UH
228 g_free(p->trigger);
229
230 p->trigger = g_strdup(trigger);
7d658874
BV
231
232}
233
2bf4aca6 234gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
7d658874
BV
235{
236 int *capabilities, i;
237
218557b8
UH
238 if (!device || !device->plugin)
239 return;
240
7d658874
BV
241 if ((capabilities = device->plugin->get_capabilities()))
242 for (i = 0; capabilities[i]; i++)
243 if (capabilities[i] == hwcap)
244 return TRUE;
245
246 return FALSE;
a1bb33af 247}