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