]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
412cf2d68ab5b34e22f70a36543b61d75afd13a6
[libsigrok.git] / hwplugin.c
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. */
31 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. */
38 struct 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         {0, 0, NULL, NULL},
43 };
44
45 #ifdef HAVE_LA_DEMO
46 extern struct sr_device_plugin demo_plugin_info;
47 #endif
48 #ifdef HAVE_LA_SALEAE_LOGIC
49 extern struct sr_device_plugin saleae_logic_plugin_info;
50 #endif
51 #ifdef HAVE_LA_OLS
52 extern struct sr_device_plugin ols_plugin_info;
53 #endif
54 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
55 extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
56 #endif
57 #ifdef HAVE_LA_ASIX_SIGMA
58 extern struct sr_device_plugin asix_sigma_plugin_info;
59 #endif
60 #ifdef HAVE_LA_CHRONOVU_LA8
61 extern struct device_plugin chronovu_la8_plugin_info;
62 #endif
63 #ifdef HAVE_LA_LINK_MSO19
64 extern struct sr_device_plugin link_mso19_plugin_info;
65 #endif
66 #ifdef HAVE_LA_ALSA
67 extern struct sr_device_plugin alsa_plugin_info;
68 #endif
69
70
71 /* TODO: No linked list needed, this can be a simple array. */
72 int 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
102         return SR_OK;
103 }
104
105 GSList *sr_list_hwplugins(void)
106 {
107         return plugins;
108 }
109
110 struct sr_device_instance *sr_device_instance_new(int index, int status,
111                 const char *vendor, const char *model, const char *version)
112 {
113         struct sr_device_instance *sdi;
114
115         if (!(sdi = malloc(sizeof(struct sr_device_instance))))
116                 return NULL;
117
118         sdi->index = index;
119         sdi->status = status;
120         sdi->instance_type = -1;
121         sdi->vendor = vendor ? strdup(vendor) : NULL;
122         sdi->model = model ? strdup(model) : strdup("(unknown)");
123         sdi->version = version ? strdup(version) : NULL;
124         sdi->priv = NULL;
125         sdi->usb = NULL;
126
127         return sdi;
128 }
129
130 struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
131                                                   int device_index)
132 {
133         struct sr_device_instance *sdi;
134         GSList *l;
135
136         for (l = device_instances; l; l = l->next) {
137                 sdi = (struct sr_device_instance *)(l->data);
138                 if (sdi->index == device_index)
139                         return sdi;
140         }
141         sr_warn("could not find device index %d instance", device_index);
142
143         return NULL;
144 }
145
146 void sr_device_instance_free(struct sr_device_instance *sdi)
147 {
148         switch (sdi->instance_type) {
149 #ifdef HAVE_LIBUSB_1_0
150         case SR_USB_INSTANCE:
151                 sr_usb_device_instance_free(sdi->usb);
152                 break;
153 #endif
154         case SR_SERIAL_INSTANCE:
155                 sr_serial_device_instance_free(sdi->serial);
156                 break;
157         default:
158                 /* No specific type, nothing extra to free. */
159                 break;
160         }
161
162         free(sdi->vendor);
163         free(sdi->model);
164         free(sdi->version);
165         free(sdi);
166 }
167
168 #ifdef HAVE_LIBUSB_1_0
169
170 struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
171                         uint8_t address, struct libusb_device_handle *hdl)
172 {
173         struct sr_usb_device_instance *udi;
174
175         if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
176                 return NULL;
177
178         udi->bus = bus;
179         udi->address = address;
180         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
181
182         return udi;
183 }
184
185 void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
186 {
187         /* Avoid compiler warnings. */
188         usb = usb;
189
190         /* Nothing to do for this device instance type. */
191 }
192
193 #endif
194
195 struct sr_serial_device_instance *sr_serial_device_instance_new(
196                                                 const char *port, int fd)
197 {
198         struct sr_serial_device_instance *serial;
199
200         if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
201                 return NULL;
202
203         serial->port = strdup(port);
204         serial->fd = fd;
205
206         return serial;
207 }
208
209 void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
210 {
211         free(serial->port);
212 }
213
214 int sr_find_hwcap(int *capabilities, int hwcap)
215 {
216         int i;
217
218         for (i = 0; capabilities[i]; i++) {
219                 if (capabilities[i] == hwcap)
220                         return TRUE;
221         }
222
223         return FALSE;
224 }
225
226 struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
227 {
228         int i;
229
230         for (i = 0; sr_hwcap_options[i].capability; i++) {
231                 if (sr_hwcap_options[i].capability == hwcap)
232                         return &sr_hwcap_options[i];
233         }
234
235         return NULL;
236 }
237
238 /* unnecessary level of indirection follows. */
239
240 void sr_source_remove(int fd)
241 {
242         sr_session_source_remove(fd);
243 }
244
245 void sr_source_add(int fd, int events, int timeout,
246                    sr_receive_data_callback rcv_cb, void *user_data)
247 {
248         sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
249 }