]> sigrok.org Git - libsigrok.git/blame - device.c
make time/duration work, at least when loading from a session file
[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
8722c31e 29
2bf4aca6 30void sr_device_scan(void)
a1bb33af
UH
31{
32 GSList *plugins, *l;
5c2d46d1 33 struct sr_device_plugin *plugin;
a1bb33af 34
ee4b6342 35 plugins = sr_list_hwplugins();
a1bb33af 36
1b452b85
UH
37 /*
38 * Initialize all plugins first. Since the init() call may involve
a1bb33af
UH
39 * a firmware upload and associated delay, we may as well get all
40 * of these out of the way first.
41 */
1b452b85 42 for (l = plugins; l; l = l->next) {
a1bb33af 43 plugin = l->data;
8722c31e 44 sr_init_hwplugins(plugin);
e54bcdc5
BV
45 }
46
a1bb33af
UH
47}
48
2bf4aca6 49GSList *sr_device_list(void)
a1bb33af 50{
e54bcdc5
BV
51
52 if (!devices)
2bf4aca6 53 sr_device_scan();
e54bcdc5 54
a1bb33af
UH
55 return devices;
56}
57
2bf4aca6 58struct sr_device *sr_device_new(struct sr_device_plugin *plugin, int plugin_index,
5c2d46d1 59 int num_probes)
a1bb33af 60{
5c2d46d1 61 struct sr_device *device;
873080cc 62 int i;
a1bb33af 63
b53738ba
UH
64 if (!(device = g_try_malloc0(sizeof(struct sr_device)))) {
65 sr_err("dev: %s: device malloc failed", __func__);
66 return NULL;
67 }
68
a1bb33af
UH
69 device->plugin = plugin;
70 device->plugin_index = plugin_index;
71 devices = g_slist_append(devices, device);
72
7d658874 73 for (i = 0; i < num_probes; i++)
2bf4aca6 74 sr_device_probe_add(device, NULL);
a1bb33af
UH
75
76 return device;
77}
78
2bf4aca6 79void sr_device_clear(struct sr_device *device)
a1bb33af 80{
1b452b85 81 unsigned int pnum;
a1bb33af 82
1b452b85
UH
83 if (!device->probes)
84 return;
a1bb33af 85
1b452b85 86 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++)
2bf4aca6 87 sr_device_probe_clear(device, pnum);
a1bb33af
UH
88}
89
2bf4aca6 90void sr_device_probe_clear(struct sr_device *device, int probenum)
a1bb33af 91{
1afe8989 92 struct sr_probe *p;
a1bb33af 93
03dbc020 94 p = sr_device_probe_find(device, probenum);
1b452b85 95 if (!p)
a1bb33af
UH
96 return;
97
1b452b85 98 if (p->name) {
a1bb33af
UH
99 g_free(p->name);
100 p->name = NULL;
101 }
102
1b452b85 103 if (p->trigger) {
a1bb33af
UH
104 g_free(p->trigger);
105 p->trigger = NULL;
106 }
a1bb33af
UH
107}
108
8225e921 109void sr_device_probe_add(struct sr_device *device, const char *name)
a1bb33af 110{
1afe8989 111 struct sr_probe *p;
7d658874
BV
112 char probename[16];
113 int probenum;
a1bb33af 114
7d658874 115 probenum = g_slist_length(device->probes) + 1;
b53738ba
UH
116
117 if (!(p = g_try_malloc0(sizeof(struct sr_probe)))) {
118 sr_err("dev: %s: p malloc failed", __func__);
119 // return SR_ERR_MALLOC;
120 return; /* FIXME: should return int. */
121 }
122
7d658874 123 p->index = probenum;
a1bb33af 124 p->enabled = TRUE;
7d658874
BV
125 if (name) {
126 p->name = g_strdup(name);
127 } else {
128 snprintf(probename, 16, "%d", probenum);
129 p->name = g_strdup(probename);
130 }
a1bb33af
UH
131 p->trigger = NULL;
132 device->probes = g_slist_append(device->probes, p);
a1bb33af
UH
133}
134
03dbc020 135struct sr_probe *sr_device_probe_find(struct sr_device *device, int probenum)
a1bb33af
UH
136{
137 GSList *l;
1afe8989 138 struct sr_probe *p, *found_probe;
a1bb33af
UH
139
140 found_probe = NULL;
1b452b85 141 for (l = device->probes; l; l = l->next) {
a1bb33af 142 p = l->data;
1b452b85 143 if (p->index == probenum) {
a1bb33af
UH
144 found_probe = p;
145 break;
146 }
147 }
148
149 return found_probe;
150}
151
f38bdf56 152/* TODO: return SR_ERR if probenum not found */
8225e921
UH
153void sr_device_probe_name(struct sr_device *device, int probenum,
154 const char *name)
a1bb33af 155{
1afe8989 156 struct sr_probe *p;
a1bb33af 157
03dbc020 158 p = sr_device_probe_find(device, probenum);
1b452b85 159 if (!p)
a1bb33af
UH
160 return;
161
1b452b85 162 if (p->name)
a1bb33af
UH
163 g_free(p->name);
164 p->name = g_strdup(name);
a1bb33af
UH
165}
166
f38bdf56 167/* TODO: return SR_ERR if probenum not found */
2bf4aca6 168void sr_device_trigger_clear(struct sr_device *device)
a1bb33af 169{
1afe8989 170 struct sr_probe *p;
1b452b85 171 unsigned int pnum;
a1bb33af 172
1b452b85
UH
173 if (!device->probes)
174 return;
a1bb33af 175
1b452b85 176 for (pnum = 1; pnum <= g_slist_length(device->probes); pnum++) {
03dbc020 177 p = sr_device_probe_find(device, pnum);
1b452b85
UH
178 if (p && p->trigger) {
179 g_free(p->trigger);
180 p->trigger = NULL;
181 }
182 }
183}
a1bb33af 184
f38bdf56 185/* TODO: return SR_ERR if probenum not found */
8225e921
UH
186void sr_device_trigger_set(struct sr_device *device, int probenum,
187 const char *trigger)
a1bb33af 188{
1afe8989 189 struct sr_probe *p;
a1bb33af 190
03dbc020 191 p = sr_device_probe_find(device, probenum);
1b452b85 192 if (!p)
a1bb33af
UH
193 return;
194
1b452b85 195 if (p->trigger)
a1bb33af
UH
196 g_free(p->trigger);
197
198 p->trigger = g_strdup(trigger);
7d658874
BV
199
200}
201
2bf4aca6 202gboolean sr_device_has_hwcap(struct sr_device *device, int hwcap)
7d658874
BV
203{
204 int *capabilities, i;
205
218557b8 206 if (!device || !device->plugin)
f437ea3f 207 return FALSE;
218557b8 208
7d658874
BV
209 if ((capabilities = device->plugin->get_capabilities()))
210 for (i = 0; capabilities[i]; i++)
211 if (capabilities[i] == hwcap)
212 return TRUE;
213
214 return FALSE;
a1bb33af 215}