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