]> sigrok.org Git - libsigrok.git/blame - device.c
implement session loading based on a virtual device driver
[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
a1bb33af
UH
28void device_scan(void)
29{
30 GSList *plugins, *l;
5c2d46d1 31 struct sr_device_plugin *plugin;
a1bb33af
UH
32
33 plugins = list_hwplugins();
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;
e54bcdc5 42 device_plugin_init(plugin);
a1bb33af 43 }
e54bcdc5
BV
44
45}
46
5c2d46d1 47int 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);
e54bcdc5
BV
55 device_new(plugin, i, num_probes);
56 }
57
58 return num_devices;
a1bb33af
UH
59}
60
a1bb33af
UH
61void device_close_all(void)
62{
5c2d46d1 63 struct sr_device *device;
a1bb33af 64
1b452b85 65 while (devices) {
a1bb33af 66 device = devices->data;
873080cc
BV
67 if (device->plugin)
68 device->plugin->close(device->plugin_index);
a1bb33af
UH
69 device_destroy(device);
70 }
a1bb33af
UH
71}
72
a1bb33af
UH
73GSList *device_list(void)
74{
e54bcdc5
BV
75
76 if (!devices)
77 device_scan();
78
a1bb33af
UH
79 return devices;
80}
81
5c2d46d1
UH
82struct sr_device *device_new(struct sr_device_plugin *plugin, int plugin_index,
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
BV
93 for (i = 0; i < num_probes; i++)
94 device_probe_add(device, NULL);
a1bb33af
UH
95
96 return device;
97}
98
5c2d46d1 99void 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
UH
108 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
109 device_probe_clear(device, pnum);
a1bb33af
UH
110}
111
5c2d46d1 112void 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++)
124 device_probe_clear(device, pnum);
a1bb33af
UH
125 g_slist_free(device->probes);
126 }
127 g_free(device);
a1bb33af
UH
128}
129
5c2d46d1 130void device_probe_clear(struct sr_device *device, int probenum)
a1bb33af
UH
131{
132 struct probe *p;
133
134 p = 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
5c2d46d1 149void device_probe_add(struct sr_device *device, char *name)
a1bb33af
UH
150{
151 struct probe *p;
7d658874
BV
152 char probename[16];
153 int probenum;
a1bb33af 154
7d658874 155 probenum = g_slist_length(device->probes) + 1;
a1bb33af 156 p = g_malloc0(sizeof(struct 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->name = g_strdup(name);
166 p->trigger = NULL;
167 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
168}
169
5c2d46d1 170struct probe *probe_find(struct sr_device *device, int probenum)
a1bb33af
UH
171{
172 GSList *l;
173 struct probe *p, *found_probe;
174
175 found_probe = NULL;
1b452b85 176 for (l = device->probes; l; l = l->next) {
a1bb33af 177 p = l->data;
1b452b85 178 if (p->index == probenum) {
a1bb33af
UH
179 found_probe = p;
180 break;
181 }
182 }
183
184 return found_probe;
185}
186
7d658874 187/* TODO: return SIGROK_ERR if probenum not found */
5c2d46d1 188void device_probe_name(struct sr_device *device, int probenum, char *name)
a1bb33af
UH
189{
190 struct probe *p;
191
192 p = probe_find(device, probenum);
1b452b85 193 if (!p)
a1bb33af
UH
194 return;
195
1b452b85 196 if (p->name)
a1bb33af
UH
197 g_free(p->name);
198 p->name = g_strdup(name);
a1bb33af
UH
199}
200
7d658874 201/* TODO: return SIGROK_ERR if probenum not found */
5c2d46d1 202void device_trigger_clear(struct sr_device *device)
a1bb33af
UH
203{
204 struct probe *p;
1b452b85 205 unsigned int pnum;
a1bb33af 206
1b452b85
UH
207 if (!device->probes)
208 return;
a1bb33af 209
1b452b85
UH
210 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
211 p = probe_find(device, pnum);
212 if (p && p->trigger) {
213 g_free(p->trigger);
214 p->trigger = NULL;
215 }
216 }
217}
a1bb33af 218
7d658874 219/* TODO: return SIGROK_ERR if probenum not found */
5c2d46d1 220void device_trigger_set(struct sr_device *device, int probenum, char *trigger)
a1bb33af
UH
221{
222 struct probe *p;
223
224 p = probe_find(device, probenum);
1b452b85 225 if (!p)
a1bb33af
UH
226 return;
227
1b452b85 228 if (p->trigger)
a1bb33af
UH
229 g_free(p->trigger);
230
231 p->trigger = g_strdup(trigger);
7d658874
BV
232
233}
234
235gboolean device_has_hwcap(struct sr_device *device, int hwcap)
236{
237 int *capabilities, i;
238
239 if ((capabilities = device->plugin->get_capabilities()))
240 for (i = 0; capabilities[i]; i++)
241 if (capabilities[i] == hwcap)
242 return TRUE;
243
244 return FALSE;
a1bb33af 245}