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