]> sigrok.org Git - libsigrok.git/blame_incremental - hwplugin.c
probe names: Fix cosmetics, add docs, fix off-by-one.
[libsigrok.git] / hwplugin.c
... / ...
CommitLineData
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 "config.h"
21#include <stdlib.h>
22#include <stdio.h>
23#include <sys/types.h>
24#include <dirent.h>
25#include <string.h>
26#include <glib.h>
27#include "sigrok.h"
28#include "sigrok-internal.h"
29
30/* The list of loaded plugins lives here. */
31static GSList *plugins;
32
33/*
34 * This enumerates which plugin capabilities correspond to user-settable
35 * options.
36 */
37/* TODO: This shouldn't be a global. */
38struct sr_hwcap_option sr_hwcap_options[] = {
39 {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
40 {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
41 {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "patternmode"},
42 {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
43 {0, 0, NULL, NULL},
44};
45
46#ifdef HAVE_LA_DEMO
47extern struct sr_device_plugin demo_plugin_info;
48#endif
49#ifdef HAVE_LA_SALEAE_LOGIC
50extern struct sr_device_plugin saleae_logic_plugin_info;
51#endif
52#ifdef HAVE_LA_OLS
53extern struct sr_device_plugin ols_plugin_info;
54#endif
55#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
56extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
57#endif
58#ifdef HAVE_LA_ASIX_SIGMA
59extern struct sr_device_plugin asix_sigma_plugin_info;
60#endif
61#ifdef HAVE_LA_CHRONOVU_LA8
62extern struct device_plugin chronovu_la8_plugin_info;
63#endif
64#ifdef HAVE_LA_LINK_MSO19
65extern struct sr_device_plugin link_mso19_plugin_info;
66#endif
67#ifdef HAVE_LA_ALSA
68extern struct sr_device_plugin alsa_plugin_info;
69#endif
70
71/* TODO: No linked list needed, this can be a simple array. */
72int load_hwplugins(void)
73{
74#ifdef HAVE_LA_DEMO
75 plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
76#endif
77#ifdef HAVE_LA_SALEAE_LOGIC
78 plugins =
79 g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
80#endif
81#ifdef HAVE_LA_OLS
82 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
83#endif
84#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
85 plugins = g_slist_append(plugins,
86 (gpointer *)&zeroplus_logic_cube_plugin_info);
87#endif
88#ifdef HAVE_LA_ASIX_SIGMA
89 plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
90#endif
91#ifdef HAVE_LA_CHRONOVU_LA8
92 plugins = g_slist_append(plugins, (gpointer *)&chronovu_la8_plugin_info);
93#endif
94#ifdef HAVE_LA_LINK_MSO19
95 plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
96#endif
97#ifdef HAVE_LA_ALSA
98 plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
99#endif
100
101 return SR_OK;
102}
103
104GSList *sr_list_hwplugins(void)
105{
106 return plugins;
107}
108
109int sr_init_hwplugins(struct sr_device_plugin *plugin)
110{
111 int num_devices, num_probes, i, j;
112 int num_initialized_devices = 0;
113 struct sr_device *device;
114 char **probe_names;
115
116 g_message("initializing %s plugin", plugin->name);
117 num_devices = plugin->init(NULL);
118 for (i = 0; i < num_devices; i++) {
119 num_probes = GPOINTER_TO_INT(
120 plugin->get_device_info(i, SR_DI_NUM_PROBES));
121 probe_names = (char **)plugin->get_device_info(i,
122 SR_DI_PROBE_NAMES);
123
124 if (!probe_names) {
125 sr_warn("hwplugin: %s: plugin %s does not return a "
126 "list of probe names", __func__, plugin->name);
127 continue;
128 }
129
130 device = sr_device_new(plugin, i);
131 for (j = 0; j < num_probes; j++)
132 sr_device_probe_add(device, probe_names[j]);
133 num_initialized_devices++;
134 }
135
136 return num_initialized_devices;
137}
138
139void sr_cleanup_hwplugins(void)
140{
141 struct sr_device_plugin *plugin;
142 GSList *l;
143
144 for (l = plugins; l; l = l->next) {
145 plugin = l->data;
146 if (plugin->cleanup)
147 plugin->cleanup();
148 }
149}
150
151struct sr_device_instance *sr_device_instance_new(int index, int status,
152 const char *vendor, const char *model, const char *version)
153{
154 struct sr_device_instance *sdi;
155
156 if (!(sdi = g_malloc(sizeof(struct sr_device_instance))))
157 return NULL;
158
159 sdi->index = index;
160 sdi->status = status;
161 sdi->instance_type = -1;
162 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
163 sdi->model = model ? g_strdup(model) : NULL;
164 sdi->version = version ? g_strdup(version) : NULL;
165 sdi->priv = NULL;
166 sdi->usb = NULL;
167
168 return sdi;
169}
170
171struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
172 int device_index)
173{
174 struct sr_device_instance *sdi;
175 GSList *l;
176
177 for (l = device_instances; l; l = l->next) {
178 sdi = (struct sr_device_instance *)(l->data);
179 if (sdi->index == device_index)
180 return sdi;
181 }
182 sr_warn("could not find device index %d instance", device_index);
183
184 return NULL;
185}
186
187void sr_device_instance_free(struct sr_device_instance *sdi)
188{
189 switch (sdi->instance_type) {
190#ifdef HAVE_LIBUSB_1_0
191 case SR_USB_INSTANCE:
192 sr_usb_device_instance_free(sdi->usb);
193 break;
194#endif
195 case SR_SERIAL_INSTANCE:
196 sr_serial_device_instance_free(sdi->serial);
197 break;
198 default:
199 /* No specific type, nothing extra to free. */
200 break;
201 }
202
203 if (sdi->priv)
204 g_free(sdi->priv);
205
206 g_free(sdi->vendor);
207 g_free(sdi->model);
208 g_free(sdi->version);
209 g_free(sdi);
210}
211
212#ifdef HAVE_LIBUSB_1_0
213
214struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
215 uint8_t address, struct libusb_device_handle *hdl)
216{
217 struct sr_usb_device_instance *udi;
218
219 if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
220 return NULL;
221
222 udi->bus = bus;
223 udi->address = address;
224 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
225
226 return udi;
227}
228
229void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
230{
231 /* Avoid compiler warnings. */
232 (void)usb;
233
234 /* Nothing to do for this device instance type. */
235}
236
237#endif
238
239struct sr_serial_device_instance *sr_serial_device_instance_new(
240 const char *port, int fd)
241{
242 struct sr_serial_device_instance *serial;
243
244 if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
245 return NULL;
246
247 serial->port = strdup(port);
248 serial->fd = fd;
249
250 return serial;
251}
252
253void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
254{
255 free(serial->port);
256}
257
258int sr_find_hwcap(int *capabilities, int hwcap)
259{
260 int i;
261
262 for (i = 0; capabilities[i]; i++) {
263 if (capabilities[i] == hwcap)
264 return TRUE;
265 }
266
267 return FALSE;
268}
269
270struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
271{
272 int i;
273
274 for (i = 0; sr_hwcap_options[i].capability; i++) {
275 if (sr_hwcap_options[i].capability == hwcap)
276 return &sr_hwcap_options[i];
277 }
278
279 return NULL;
280}
281
282/* unnecessary level of indirection follows. */
283
284void sr_source_remove(int fd)
285{
286 sr_session_source_remove(fd);
287}
288
289void sr_source_add(int fd, int events, int timeout,
290 sr_receive_data_callback rcv_cb, void *user_data)
291{
292 sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
293}