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