]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
Only build hardware plugins if requested by user.
[libsigrok.git] / hwplugin.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 <stdlib.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <string.h>
25#include <glib.h>
62c82025 26#include <sigrok.h>
a1bb33af
UH
27
28source_callback_add source_cb_add = NULL;
29source_callback_remove source_cb_remove = NULL;
30
62c82025 31/* The list of loaded plugins lives here. */
a1bb33af
UH
32GSList *plugins;
33
62c82025
UH
34/*
35 * This enumerates which plugin capabilities correspond to user-settable
36 * options.
37 */
a1bb33af 38struct hwcap_option hwcap_options[] = {
62c82025 39 {HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate"},
49d0ce50 40 {0, 0, NULL, NULL},
a1bb33af
UH
41};
42
960a75e4 43#ifdef HAVE_LA_SALEAE_LOGIC
a1bb33af 44extern struct device_plugin saleae_logic_plugin_info;
960a75e4
UH
45#endif
46#ifdef HAVE_LA_OLS
a1bb33af 47extern struct device_plugin ols_plugin_info;
960a75e4
UH
48#endif
49#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
a1bb33af 50extern struct device_plugin zeroplus_logic_cube_plugin_info;
960a75e4 51#endif
5b907f9b 52#ifdef HAVE_LA_ASIX_SIGMA
28a35d8a 53extern struct device_plugin asix_sigma_plugin_info;
5b907f9b 54#endif
a1bb33af 55
49d0ce50 56/* TODO: No linked list needed, this can be a simple array. */
a1bb33af
UH
57int load_hwplugins(void)
58{
960a75e4 59#ifdef HAVE_LA_SALEAE_LOGIC
62c82025 60 plugins =
49d0ce50 61 g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
960a75e4
UH
62#endif
63#ifdef HAVE_LA_OLS
49d0ce50 64 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
960a75e4
UH
65#endif
66#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
62c82025 67 plugins = g_slist_append(plugins,
49d0ce50 68 (gpointer *)&zeroplus_logic_cube_plugin_info);
960a75e4 69#endif
5b907f9b 70#ifdef HAVE_LA_ASIX_SIGMA
28a35d8a 71 plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
5b907f9b 72#endif
a1bb33af
UH
73
74 return SIGROK_OK;
75}
76
a1bb33af
UH
77GSList *list_hwplugins(void)
78{
a1bb33af
UH
79 return plugins;
80}
81
a1bb33af 82struct sigrok_device_instance *sigrok_device_instance_new(int index, int status,
49d0ce50 83 const char *vendor, const char *model, const char *version)
a1bb33af
UH
84{
85 struct sigrok_device_instance *sdi;
86
49d0ce50 87 if (!(sdi = malloc(sizeof(struct sigrok_device_instance))))
a1bb33af
UH
88 return NULL;
89
90 sdi->index = index;
91 sdi->status = status;
92 sdi->instance_type = -1;
93 sdi->vendor = strdup(vendor);
94 sdi->model = strdup(model);
95 sdi->version = strdup(version);
96 sdi->usb = NULL;
97
98 return sdi;
99}
100
62c82025
UH
101struct sigrok_device_instance *get_sigrok_device_instance(
102 GSList *device_instances, int device_index)
a1bb33af
UH
103{
104 struct sigrok_device_instance *sdi;
105 GSList *l;
106
62c82025
UH
107 for (l = device_instances; l; l = l->next) {
108 sdi = (struct sigrok_device_instance *)(l->data);
109 if (sdi->index == device_index)
a1bb33af
UH
110 return sdi;
111 }
112 g_warning("could not find device index %d instance", device_index);
113
114 return NULL;
115}
116
a1bb33af
UH
117void sigrok_device_instance_free(struct sigrok_device_instance *sdi)
118{
62c82025 119 switch (sdi->instance_type) {
a1bb33af
UH
120 case USB_INSTANCE:
121 usb_device_instance_free(sdi->usb);
122 break;
123 case SERIAL_INSTANCE:
124 serial_device_instance_free(sdi->serial);
125 break;
49d0ce50 126 default:
62c82025 127 /* No specific type, nothing extra to free. */
49d0ce50 128 break;
a1bb33af
UH
129 }
130
131 free(sdi->vendor);
132 free(sdi->model);
133 free(sdi->version);
134 free(sdi);
a1bb33af
UH
135}
136
62c82025
UH
137struct usb_device_instance *usb_device_instance_new(uint8_t bus,
138 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af
UH
139{
140 struct usb_device_instance *udi;
141
49d0ce50 142 if (!(udi = malloc(sizeof(struct usb_device_instance))))
a1bb33af
UH
143 return NULL;
144
145 udi->bus = bus;
146 udi->address = address;
49d0ce50 147 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
a1bb33af
UH
148
149 return udi;
150}
151
a1bb33af
UH
152void usb_device_instance_free(struct usb_device_instance *usb)
153{
afc8e4de
UH
154 /* QUICK HACK */
155 usb = usb;
a1bb33af 156
62c82025 157 /* Nothing to do for this device instance type. */
a1bb33af
UH
158}
159
49d0ce50
UH
160struct serial_device_instance *serial_device_instance_new(
161 const char *port, int fd)
a1bb33af
UH
162{
163 struct serial_device_instance *serial;
164
49d0ce50 165 if (!(serial = malloc(sizeof(struct serial_device_instance))))
a1bb33af
UH
166 return NULL;
167
168 serial->port = strdup(port);
169 serial->fd = fd;
170
171 return serial;
172}
173
a1bb33af
UH
174void serial_device_instance_free(struct serial_device_instance *serial)
175{
a1bb33af 176 free(serial->port);
a1bb33af
UH
177}
178
a1bb33af
UH
179int find_hwcap(int *capabilities, int hwcap)
180{
181 int i;
182
49d0ce50 183 for (i = 0; capabilities[i]; i++) {
62c82025 184 if (capabilities[i] == hwcap)
a1bb33af 185 return TRUE;
49d0ce50 186 }
a1bb33af
UH
187
188 return FALSE;
189}
190
a1bb33af
UH
191struct hwcap_option *find_hwcap_option(int hwcap)
192{
a1bb33af
UH
193 int i;
194
62c82025 195 for (i = 0; hwcap_options[i].capability; i++) {
49d0ce50
UH
196 if (hwcap_options[i].capability == hwcap)
197 return &hwcap_options[i];
a1bb33af
UH
198 }
199
49d0ce50 200 return NULL;
a1bb33af
UH
201}
202
a1bb33af
UH
203void source_remove(int fd)
204{
62c82025 205 if (source_cb_remove)
a1bb33af 206 source_cb_remove(fd);
a1bb33af
UH
207}
208
62c82025
UH
209void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
210 void *user_data)
a1bb33af 211{
62c82025 212 if (source_cb_add)
a1bb33af 213 source_cb_add(fd, events, timeout, rcv_cb, user_data);
a1bb33af 214}