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