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