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