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