]> sigrok.org Git - libsigrok.git/blob - device.c
better cleanup of device/plugin resources
[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
30 void sr_device_scan(void)
31 {
32         GSList *plugins, *l;
33         struct sr_device_plugin *plugin;
34
35         plugins = sr_list_hwplugins();
36
37         /*
38          * Initialize all plugins first. Since the init() call may involve
39          * a firmware upload and associated delay, we may as well get all
40          * of these out of the way first.
41          */
42         for (l = plugins; l; l = l->next) {
43                 plugin = l->data;
44                 sr_init_hwplugins(plugin);
45         }
46
47 }
48
49 GSList *sr_device_list(void)
50 {
51
52         if (!devices)
53                 sr_device_scan();
54
55         return devices;
56 }
57
58 struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
59                              int num_probes)
60 {
61         struct sr_device *device;
62         int i;
63
64         if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
65                 sr_err("dev: %s: device malloc failed", __func__);
66                 return NULL;
67         }
68
69         device->plugin = plugin;
70         device->plugin_index = plugin_index;
71         devices = g_slist_append(devices, device);
72
73         for (i = 0; i < num_probes; i++)
74                 sr_device_probe_add(device, NULL);
75
76         return device;
77 }
78
79 void sr_device_clear(struct sr_device *device)
80 {
81         unsigned int pnum;
82
83         if (!device->probes)
84                 return;
85
86         for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
87                 sr_device_probe_clear(device, pnum);
88 }
89
90 void sr_device_probe_clear(struct sr_device *device, int probenum)
91 {
92         struct sr_probe *p;
93
94         p = sr_device_probe_find(device, probenum);
95         if (!p)
96                 return;
97
98         if (p->name) {
99                 g_free(p->name);
100                 p->name = NULL;
101         }
102
103         if (p->trigger) {
104                 g_free(p->trigger);
105                 p->trigger = NULL;
106         }
107 }
108
109 void sr_device_probe_add(struct sr_device *device, const char *name)
110 {
111         struct sr_probe *p;
112         char probename[16];
113         int probenum;
114
115         probenum = g_slist_length(device->probes) + 1;
116
117         if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
118                 sr_err("dev: %s: p malloc failed", __func__);
119                 // return SR_ERR_MALLOC;
120                 return; /* FIXME: should return int. */
121         }
122
123         p->index = probenum;
124         p->enabled = TRUE;
125         if (name) {
126                 p->name = g_strdup(name);
127         } else {
128                 snprintf(probename, 16, "%d", probenum);
129                 p->name = g_strdup(probename);
130         }
131         p->trigger = NULL;
132         device->probes = g_slist_append(device->probes, p);
133 }
134
135 struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
136 {
137         GSList *l;
138         struct sr_probe *p, *found_probe;
139
140         found_probe = NULL;
141         for (l = device->probes; l; l = l->next) {
142                 p = l->data;
143                 if (p->index == probenum) {
144                         found_probe = p;
145                         break;
146                 }
147         }
148
149         return found_probe;
150 }
151
152 /* TODO: return SR_ERR if probenum not found */
153 void sr_device_probe_name(struct sr_device *device, int probenum,
154                           const char *name)
155 {
156         struct sr_probe *p;
157
158         p = sr_device_probe_find(device, probenum);
159         if (!p)
160                 return;
161
162         if (p->name)
163                 g_free(p->name);
164         p->name = g_strdup(name);
165 }
166
167 /* TODO: return SR_ERR if probenum not found */
168 void sr_device_trigger_clear(struct sr_device *device)
169 {
170         struct sr_probe *p;
171         unsigned int pnum;
172
173         if (!device->probes)
174                 return;
175
176         for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
177                 p = sr_device_probe_find(device, pnum);
178                 if (p && p->trigger) {
179                         g_free(p->trigger);
180                         p->trigger = NULL;
181                 }
182         }
183 }
184
185 /* TODO: return SR_ERR if probenum not found */
186 void sr_device_trigger_set(struct sr_device *device, int probenum,
187                            const char *trigger)
188 {
189         struct sr_probe *p;
190
191         p = sr_device_probe_find(device, probenum);
192         if (!p)
193                 return;
194
195         if (p->trigger)
196                 g_free(p->trigger);
197
198         p->trigger = g_strdup(trigger);
199
200 }
201
202 gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
203 {
204         int *capabilities, i;
205
206         if (!device || !device->plugin)
207                 return FALSE;
208
209         if ((capabilities = device->plugin->get_capabilities()))
210                 for (i = 0; capabilities[i]; i++)
211                         if (capabilities[i] == hwcap)
212                                 return TRUE;
213
214         return FALSE;
215 }