]> sigrok.org Git - libsigrok.git/blob - device.c
Start of code base layout restructuring.
[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
24 extern struct sigrok_global *global;
25
26 GSList *devices = NULL;
27
28
29 void device_scan(void)
30 {
31         GSList *plugins, *l;
32         struct device_plugin *plugin;
33         int num_devices, i;
34
35         plugins = list_hwplugins();
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         {
43                 plugin = l->data;
44                 g_message("initializing %s plugin", plugin->name);
45                 num_devices = plugin->init(NULL);
46                 for(i = 0; i < num_devices; i++)
47                 {
48                         device_new(plugin, i);
49                 }
50         }
51
52 }
53
54
55 void device_close_all(void)
56 {
57         struct device *device;
58
59         while(devices)
60         {
61                 device = devices->data;
62                 device->plugin->close(device->plugin_index);
63                 device_destroy(device);
64         }
65
66 }
67
68
69 GSList *device_list(void)
70 {
71
72         return devices;
73 }
74
75
76 struct device *device_new(struct device_plugin *plugin, int plugin_index)
77 {
78         struct device *device;
79         int num_probes, i;
80         char probename[16];
81
82         device = g_malloc0(sizeof(struct device));
83         device->plugin = plugin;
84         device->plugin_index = plugin_index;
85         devices = g_slist_append(devices, device);
86
87         num_probes = (int) device->plugin->get_device_info(device->plugin_index, DI_NUM_PROBES);
88         for(i = 0; i < num_probes; i++)
89         {
90                 snprintf(probename, 16, "%d", i+1);
91                 device_probe_add(device, probename);
92         }
93
94         return device;
95 }
96
97
98 void device_clear(struct device *device)
99 {
100         int probenum;
101
102         /* TODO: plugin-specific clear call? */
103
104         if(device->probes)
105                 for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
106                         device_probe_clear(device, probenum);
107
108 }
109
110
111 void device_destroy(struct device *device)
112 {
113         int probenum;
114
115         /* TODO: plugin-specific destroy call, need to decrease refcount in plugin */
116
117         devices = g_slist_remove(devices, device);
118         if(device->probes)
119         {
120                 for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
121                         device_probe_clear(device, probenum);
122                 g_slist_free(device->probes);
123         }
124         g_free(device);
125
126 }
127
128
129 void device_probe_clear(struct device *device, int probenum)
130 {
131         struct probe *p;
132
133         p = probe_find(device, probenum);
134         if(!p)
135                 return;
136
137         if(p->name)
138         {
139                 g_free(p->name);
140                 p->name = NULL;
141         }
142
143         if(p->trigger)
144         {
145                 g_free(p->trigger);
146                 p->trigger = NULL;
147         }
148
149 }
150
151
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);
162
163 }
164
165
166 struct probe *probe_find(struct device *device, int probenum)
167 {
168         GSList *l;
169         struct probe *p, *found_probe;
170
171         found_probe = NULL;
172         for(l = device->probes; l; l = l->next)
173         {
174                 p = l->data;
175                 if(p->index == probenum)
176                 {
177                         found_probe = p;
178                         break;
179                 }
180         }
181
182         return found_probe;
183 }
184
185
186 void device_probe_name(struct device *device, int probenum, char *name)
187 {
188         struct probe *p;
189
190         p = probe_find(device, probenum);
191         if(!p)
192                 return;
193
194         if(p->name)
195                 g_free(p->name);
196         p->name = g_strdup(name);
197
198 }
199
200
201 void device_trigger_clear(struct device *device)
202 {
203         struct probe *p;
204         int probenum;
205
206         if(device->probes)
207                 for(probenum = 1; probenum <= g_slist_length(device->probes); probenum++)
208                 {
209                         p = probe_find(device, probenum);
210                         if(p && p->trigger)
211                         {
212                                 g_free(p->trigger);
213                                 p->trigger = NULL;
214                         }
215                 }
216
217 }
218
219
220 void device_trigger_set(struct device *device, int probenum, char *trigger)
221 {
222         struct probe *p;
223
224         p = probe_find(device, probenum);
225         if(!p)
226                 return;
227
228         if(p->trigger)
229                 g_free(p->trigger);
230
231         p->trigger = g_strdup(trigger);
232
233 }
234
235