]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
Add sr_ prefix to list_hwplugins().
[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
29 /* The list of loaded plugins lives here. */
30 GSList *plugins;
31
32 /*
33  * This enumerates which plugin capabilities correspond to user-settable
34  * options.
35  */
36 /* TODO: This shouldn't be a global. */
37 struct sr_hwcap_option sr_hwcap_options[] = {
38         {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
39         {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
40         {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "patternmode"},
41         {0, 0, NULL, NULL},
42 };
43
44 #ifdef HAVE_LA_DEMO
45 extern struct sr_device_plugin demo_plugin_info;
46 #endif
47 #ifdef HAVE_LA_SALEAE_LOGIC
48 extern struct sr_device_plugin saleae_logic_plugin_info;
49 #endif
50 #ifdef HAVE_LA_OLS
51 extern struct sr_device_plugin ols_plugin_info;
52 #endif
53 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
54 extern struct sr_device_plugin zeroplus_logic_cube_plugin_info;
55 #endif
56 #ifdef HAVE_LA_ASIX_SIGMA
57 extern struct sr_device_plugin asix_sigma_plugin_info;
58 #endif
59 #ifdef HAVE_LA_LINK_MSO19
60 extern struct sr_device_plugin link_mso19_plugin_info;
61 #endif
62 #ifdef HAVE_LA_ALSA
63 extern struct sr_device_plugin alsa_plugin_info;
64 #endif
65
66
67 /* TODO: No linked list needed, this can be a simple array. */
68 int load_hwplugins(void)
69 {
70 #ifdef HAVE_LA_DEMO
71         plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
72 #endif
73 #ifdef HAVE_LA_SALEAE_LOGIC
74         plugins =
75             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
76 #endif
77 #ifdef HAVE_LA_OLS
78         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
79 #endif
80 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
81         plugins = g_slist_append(plugins,
82                            (gpointer *)&zeroplus_logic_cube_plugin_info);
83 #endif
84 #ifdef HAVE_LA_ASIX_SIGMA
85         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
86 #endif
87 #ifdef HAVE_LA_LINK_MSO19
88         plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
89 #endif
90 #ifdef HAVE_LA_ALSA
91         plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
92 #endif
93
94
95         return SR_OK;
96 }
97
98 GSList *sr_list_hwplugins(void)
99 {
100         return plugins;
101 }
102
103 struct sr_device_instance *sr_device_instance_new(int index, int status,
104                 const char *vendor, const char *model, const char *version)
105 {
106         struct sr_device_instance *sdi;
107
108         if (!(sdi = malloc(sizeof(struct sr_device_instance))))
109                 return NULL;
110
111         sdi->index = index;
112         sdi->status = status;
113         sdi->instance_type = -1;
114         sdi->vendor = vendor ? strdup(vendor) : strdup("(unknown)");
115         sdi->model = model ? strdup(model) : NULL;
116         sdi->version = version ? strdup(version) : NULL;
117         sdi->priv = NULL;
118         sdi->usb = NULL;
119
120         return sdi;
121 }
122
123 struct sr_device_instance *sr_get_device_instance(GSList *device_instances,
124                                                   int device_index)
125 {
126         struct sr_device_instance *sdi;
127         GSList *l;
128
129         for (l = device_instances; l; l = l->next) {
130                 sdi = (struct sr_device_instance *)(l->data);
131                 if (sdi->index == device_index)
132                         return sdi;
133         }
134         g_warning("could not find device index %d instance", device_index);
135
136         return NULL;
137 }
138
139 void sr_device_instance_free(struct sr_device_instance *sdi)
140 {
141         switch (sdi->instance_type) {
142 #ifdef HAVE_LIBUSB_1_0
143         case SR_USB_INSTANCE:
144                 sr_usb_device_instance_free(sdi->usb);
145                 break;
146 #endif
147         case SR_SERIAL_INSTANCE:
148                 sr_serial_device_instance_free(sdi->serial);
149                 break;
150         default:
151                 /* No specific type, nothing extra to free. */
152                 break;
153         }
154
155         free(sdi->vendor);
156         free(sdi->model);
157         free(sdi->version);
158         free(sdi);
159 }
160
161 #ifdef HAVE_LIBUSB_1_0
162
163 struct sr_usb_device_instance *sr_usb_device_instance_new(uint8_t bus,
164                         uint8_t address, struct libusb_device_handle *hdl)
165 {
166         struct sr_usb_device_instance *udi;
167
168         if (!(udi = malloc(sizeof(struct sr_usb_device_instance))))
169                 return NULL;
170
171         udi->bus = bus;
172         udi->address = address;
173         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
174
175         return udi;
176 }
177
178 void sr_usb_device_instance_free(struct sr_usb_device_instance *usb)
179 {
180         /* Avoid compiler warnings. */
181         usb = usb;
182
183         /* Nothing to do for this device instance type. */
184 }
185
186 #endif
187
188 struct sr_serial_device_instance *sr_serial_device_instance_new(
189                                                 const char *port, int fd)
190 {
191         struct sr_serial_device_instance *serial;
192
193         if (!(serial = malloc(sizeof(struct sr_serial_device_instance))))
194                 return NULL;
195
196         serial->port = strdup(port);
197         serial->fd = fd;
198
199         return serial;
200 }
201
202 void sr_serial_device_instance_free(struct sr_serial_device_instance *serial)
203 {
204         free(serial->port);
205 }
206
207 int sr_find_hwcap(int *capabilities, int hwcap)
208 {
209         int i;
210
211         for (i = 0; capabilities[i]; i++) {
212                 if (capabilities[i] == hwcap)
213                         return TRUE;
214         }
215
216         return FALSE;
217 }
218
219 struct sr_hwcap_option *sr_find_hwcap_option(int hwcap)
220 {
221         int i;
222
223         for (i = 0; sr_hwcap_options[i].capability; i++) {
224                 if (sr_hwcap_options[i].capability == hwcap)
225                         return &sr_hwcap_options[i];
226         }
227
228         return NULL;
229 }
230
231 /* unnecessary level of indirection follows. */
232
233 void sr_source_remove(int fd)
234 {
235         sr_session_source_remove(fd);
236 }
237
238 void sr_source_add(int fd, int events, int timeout,
239                    receive_data_callback rcv_cb, void *user_data)
240 {
241         sr_session_source_add(fd, events, timeout, rcv_cb, user_data);
242 }