]> sigrok.org Git - libsigrok.git/blame - device.c
Introduce proper libtool versioning for the libs.
[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
UH
23
24extern struct sigrok_global *global;
25
26GSList *devices = NULL;
27
a1bb33af
UH
28void device_scan(void)
29{
30 GSList *plugins, *l;
31 struct device_plugin *plugin;
6ea7e235 32 int num_devices, num_probes, i;
a1bb33af
UH
33
34 plugins = list_hwplugins();
35
1b452b85
UH
36 /*
37 * Initialize all plugins first. Since the init() call may involve
a1bb33af
UH
38 * a firmware upload and associated delay, we may as well get all
39 * of these out of the way first.
40 */
1b452b85 41 for (l = plugins; l; l = l->next) {
a1bb33af
UH
42 plugin = l->data;
43 g_message("initializing %s plugin", plugin->name);
44 num_devices = plugin->init(NULL);
873080cc 45 for (i = 0; i < num_devices; i++) {
5e2ddeb0
UH
46 num_probes
47 = (int)(unsigned long)plugin->get_device_info(i,
48 DI_NUM_PROBES);
6ea7e235 49 device_new(plugin, i, num_probes);
873080cc 50 }
a1bb33af 51 }
a1bb33af
UH
52}
53
a1bb33af
UH
54void device_close_all(void)
55{
56 struct device *device;
57
1b452b85 58 while (devices) {
a1bb33af 59 device = devices->data;
873080cc
BV
60 if (device->plugin)
61 device->plugin->close(device->plugin_index);
a1bb33af
UH
62 device_destroy(device);
63 }
a1bb33af
UH
64}
65
a1bb33af
UH
66GSList *device_list(void)
67{
a1bb33af
UH
68 return devices;
69}
70
757b8c62 71struct device *device_new(struct device_plugin *plugin, int plugin_index,
6ea7e235 72 int num_probes)
a1bb33af
UH
73{
74 struct device *device;
873080cc 75 int i;
a1bb33af
UH
76 char probename[16];
77
78 device = g_malloc0(sizeof(struct device));
79 device->plugin = plugin;
80 device->plugin_index = plugin_index;
81 devices = g_slist_append(devices, device);
82
1b452b85
UH
83 for (i = 0; i < num_probes; i++) {
84 snprintf(probename, 16, "%d", i + 1);
a1bb33af
UH
85 device_probe_add(device, probename);
86 }
87
88 return device;
89}
90
a1bb33af
UH
91void device_clear(struct device *device)
92{
1b452b85 93 unsigned int pnum;
a1bb33af 94
1b452b85 95 /* TODO: Plugin-specific clear call? */
a1bb33af 96
1b452b85
UH
97 if (!device->probes)
98 return;
a1bb33af 99
1b452b85
UH
100 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
101 device_probe_clear(device, pnum);
a1bb33af
UH
102}
103
a1bb33af
UH
104void device_destroy(struct device *device)
105{
1b452b85 106 unsigned int pnum;
a1bb33af 107
1b452b85
UH
108 /*
109 * TODO: Plugin-specific destroy call, need to decrease refcount
110 * in plugin.
111 */
a1bb33af
UH
112
113 devices = g_slist_remove(devices, device);
1b452b85
UH
114 if (device->probes) {
115 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
116 device_probe_clear(device, pnum);
a1bb33af
UH
117 g_slist_free(device->probes);
118 }
119 g_free(device);
a1bb33af
UH
120}
121
a1bb33af
UH
122void device_probe_clear(struct device *device, int probenum)
123{
124 struct probe *p;
125
126 p = probe_find(device, probenum);
1b452b85 127 if (!p)
a1bb33af
UH
128 return;
129
1b452b85 130 if (p->name) {
a1bb33af
UH
131 g_free(p->name);
132 p->name = NULL;
133 }
134
1b452b85 135 if (p->trigger) {
a1bb33af
UH
136 g_free(p->trigger);
137 p->trigger = NULL;
138 }
a1bb33af
UH
139}
140
a1bb33af
UH
141void device_probe_add(struct device *device, char *name)
142{
143 struct probe *p;
144
145 p = g_malloc0(sizeof(struct probe));
146 p->index = g_slist_length(device->probes) + 1;
147 p->enabled = TRUE;
148 p->name = g_strdup(name);
149 p->trigger = NULL;
150 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
151}
152
a1bb33af
UH
153struct probe *probe_find(struct device *device, int probenum)
154{
155 GSList *l;
156 struct probe *p, *found_probe;
157
158 found_probe = NULL;
1b452b85 159 for (l = device->probes; l; l = l->next) {
a1bb33af 160 p = l->data;
1b452b85 161 if (p->index == probenum) {
a1bb33af
UH
162 found_probe = p;
163 break;
164 }
165 }
166
167 return found_probe;
168}
169
a1bb33af
UH
170void device_probe_name(struct device *device, int probenum, char *name)
171{
172 struct probe *p;
173
174 p = probe_find(device, probenum);
1b452b85 175 if (!p)
a1bb33af
UH
176 return;
177
1b452b85 178 if (p->name)
a1bb33af
UH
179 g_free(p->name);
180 p->name = g_strdup(name);
a1bb33af
UH
181}
182
a1bb33af
UH
183void device_trigger_clear(struct device *device)
184{
185 struct probe *p;
1b452b85 186 unsigned int pnum;
a1bb33af 187
1b452b85
UH
188 if (!device->probes)
189 return;
a1bb33af 190
1b452b85
UH
191 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
192 p = probe_find(device, pnum);
193 if (p && p->trigger) {
194 g_free(p->trigger);
195 p->trigger = NULL;
196 }
197 }
198}
a1bb33af
UH
199
200void device_trigger_set(struct device *device, int probenum, char *trigger)
201{
202 struct probe *p;
203
204 p = probe_find(device, probenum);
1b452b85 205 if (!p)
a1bb33af
UH
206 return;
207
1b452b85 208 if (p->trigger)
a1bb33af
UH
209 g_free(p->trigger);
210
211 p->trigger = g_strdup(trigger);
a1bb33af 212}