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