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