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