]> sigrok.org Git - libsigrok.git/blame - device.c
demo: Always use glib's memory allocation functions.
[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>
b08024a8 23#include <sigrok-internal.h>
a1bb33af 24
a00ba012 25extern struct sr_global *global;
a1bb33af
UH
26
27GSList *devices = NULL;
28
2bf4aca6 29void sr_device_scan(void)
a1bb33af
UH
30{
31 GSList *plugins, *l;
5c2d46d1 32 struct sr_device_plugin *plugin;
a1bb33af 33
ee4b6342 34 plugins = sr_list_hwplugins();
a1bb33af 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 42 plugin = l->data;
2bf4aca6 43 sr_device_plugin_init(plugin);
a1bb33af 44 }
e54bcdc5
BV
45
46}
47
2bf4aca6 48int sr_device_plugin_init(struct sr_device_plugin *plugin)
e54bcdc5
BV
49{
50 int num_devices, num_probes, i;
51
b08024a8 52 sr_info("initializing %s plugin", plugin->name);
e54bcdc5
BV
53 num_devices = plugin->init(NULL);
54 for (i = 0; i < num_devices; i++) {
5a2326a7 55 num_probes = (int)plugin->get_device_info(i, SR_DI_NUM_PROBES);
2bf4aca6 56 sr_device_new(plugin, i, num_probes);
e54bcdc5
BV
57 }
58
59 return num_devices;
a1bb33af
UH
60}
61
2bf4aca6 62void sr_device_close_all(void)
a1bb33af 63{
5c2d46d1 64 struct sr_device *device;
a1bb33af 65
1b452b85 66 while (devices) {
a1bb33af 67 device = devices->data;
b8c2f85f 68 if (device->plugin && device->plugin->close)
873080cc 69 device->plugin->close(device->plugin_index);
2bf4aca6 70 sr_device_destroy(device);
a1bb33af 71 }
a1bb33af
UH
72}
73
2bf4aca6 74GSList *sr_device_list(void)
a1bb33af 75{
e54bcdc5
BV
76
77 if (!devices)
2bf4aca6 78 sr_device_scan();
e54bcdc5 79
a1bb33af
UH
80 return devices;
81}
82
2bf4aca6 83struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
5c2d46d1 84 int num_probes)
a1bb33af 85{
5c2d46d1 86 struct sr_device *device;
873080cc 87 int i;
a1bb33af 88
b53738ba
UH
89 if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
90 sr_err("dev: %s: device malloc failed", __func__);
91 return NULL;
92 }
93
a1bb33af
UH
94 device->plugin = plugin;
95 device->plugin_index = plugin_index;
96 devices = g_slist_append(devices, device);
97
7d658874 98 for (i = 0; i < num_probes; i++)
2bf4aca6 99 sr_device_probe_add(device, NULL);
a1bb33af
UH
100
101 return device;
102}
103
2bf4aca6 104void sr_device_clear(struct sr_device *device)
a1bb33af 105{
1b452b85 106 unsigned int pnum;
a1bb33af 107
1b452b85 108 /* TODO: Plugin-specific clear call? */
a1bb33af 109
1b452b85
UH
110 if (!device->probes)
111 return;
a1bb33af 112
1b452b85 113 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 114 sr_device_probe_clear(device, pnum);
a1bb33af
UH
115}
116
2bf4aca6 117void sr_device_destroy(struct sr_device *device)
a1bb33af 118{
1b452b85 119 unsigned int pnum;
a1bb33af 120
1b452b85
UH
121 /*
122 * TODO: Plugin-specific destroy call, need to decrease refcount
123 * in plugin.
124 */
a1bb33af
UH
125
126 devices = g_slist_remove(devices, device);
1b452b85
UH
127 if (device->probes) {
128 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 129 sr_device_probe_clear(device, pnum);
a1bb33af
UH
130 g_slist_free(device->probes);
131 }
132 g_free(device);
a1bb33af
UH
133}
134
2bf4aca6 135void sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 136{
1afe8989 137 struct sr_probe *p;
a1bb33af 138
03dbc020 139 p = sr_device_probe_find(device, probenum);
1b452b85 140 if (!p)
a1bb33af
UH
141 return;
142
1b452b85 143 if (p->name) {
a1bb33af
UH
144 g_free(p->name);
145 p->name = NULL;
146 }
147
1b452b85 148 if (p->trigger) {
a1bb33af
UH
149 g_free(p->trigger);
150 p->trigger = NULL;
151 }
a1bb33af
UH
152}
153
8225e921 154void sr_device_probe_add(struct sr_device *device, const char *name)
a1bb33af 155{
1afe8989 156 struct sr_probe *p;
7d658874
BV
157 char probename[16];
158 int probenum;
a1bb33af 159
7d658874 160 probenum = g_slist_length(device->probes) + 1;
b53738ba
UH
161
162 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
163 sr_err("dev: %s: p malloc failed", __func__);
164 // return SR_ERR_MALLOC;
165 return; /* FIXME: should return int. */
166 }
167
7d658874 168 p->index = probenum;
a1bb33af 169 p->enabled = TRUE;
7d658874
BV
170 if (name) {
171 p->name = g_strdup(name);
172 } else {
173 snprintf(probename, 16, "%d", probenum);
174 p->name = g_strdup(probename);
175 }
a1bb33af
UH
176 p->trigger = NULL;
177 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
178}
179
03dbc020 180struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
a1bb33af
UH
181{
182 GSList *l;
1afe8989 183 struct sr_probe *p, *found_probe;
a1bb33af
UH
184
185 found_probe = NULL;
1b452b85 186 for (l = device->probes; l; l = l->next) {
a1bb33af 187 p = l->data;
1b452b85 188 if (p->index == probenum) {
a1bb33af
UH
189 found_probe = p;
190 break;
191 }
192 }
193
194 return found_probe;
195}
196
7d658874 197/* TODO: return SIGROK_ERR if probenum not found */
8225e921
UH
198void sr_device_probe_name(struct sr_device *device, int probenum,
199 const char *name)
a1bb33af 200{
1afe8989 201 struct sr_probe *p;
a1bb33af 202
03dbc020 203 p = sr_device_probe_find(device, probenum);
1b452b85 204 if (!p)
a1bb33af
UH
205 return;
206
1b452b85 207 if (p->name)
a1bb33af
UH
208 g_free(p->name);
209 p->name = g_strdup(name);
a1bb33af
UH
210}
211
7d658874 212/* TODO: return SIGROK_ERR if probenum not found */
2bf4aca6 213void sr_device_trigger_clear(struct sr_device *device)
a1bb33af 214{
1afe8989 215 struct sr_probe *p;
1b452b85 216 unsigned int pnum;
a1bb33af 217
1b452b85
UH
218 if (!device->probes)
219 return;
a1bb33af 220
1b452b85 221 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 222 p = sr_device_probe_find(device, pnum);
1b452b85
UH
223 if (p && p->trigger) {
224 g_free(p->trigger);
225 p->trigger = NULL;
226 }
227 }
228}
a1bb33af 229
7d658874 230/* TODO: return SIGROK_ERR if probenum not found */
8225e921
UH
231void sr_device_trigger_set(struct sr_device *device, int probenum,
232 const char *trigger)
a1bb33af 233{
1afe8989 234 struct sr_probe *p;
a1bb33af 235
03dbc020 236 p = sr_device_probe_find(device, probenum);
1b452b85 237 if (!p)
a1bb33af
UH
238 return;
239
1b452b85 240 if (p->trigger)
a1bb33af
UH
241 g_free(p->trigger);
242
243 p->trigger = g_strdup(trigger);
7d658874
BV
244
245}
246
2bf4aca6 247gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
7d658874
BV
248{
249 int *capabilities, i;
250
218557b8 251 if (!device || !device->plugin)
f437ea3f 252 return FALSE;
218557b8 253
7d658874
BV
254 if ((capabilities = device->plugin->get_capabilities()))
255 for (i = 0; capabilities[i]; i++)
256 if (capabilities[i] == hwcap)
257 return TRUE;
258
259 return FALSE;
a1bb33af 260}