]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
1f3a34ba3814ba7734f35dc2549db6862475f687
[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 <stdlib.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <dirent.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <sigrok.h>
27 #include "config.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 struct hwcap_option hwcap_options[] = {
37         {HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate"},
38         {HWCAP_CAPTURE_RATIO, T_UINT64, "Pre-trigger capture ratio", "captureratio"},
39         {HWCAP_PATTERN_MODE, T_CHAR, "Pattern generator mode", "patternmode"},
40         {0, 0, NULL, NULL},
41 };
42
43 #ifdef HAVE_LA_DEMO
44 extern struct device_plugin demo_plugin_info;
45 #endif
46 #ifdef HAVE_LA_SALEAE_LOGIC
47 extern struct device_plugin saleae_logic_plugin_info;
48 #endif
49 #ifdef HAVE_LA_OLS
50 extern struct device_plugin ols_plugin_info;
51 #endif
52 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
53 extern struct device_plugin zeroplus_logic_cube_plugin_info;
54 #endif
55 #ifdef HAVE_LA_ASIX_SIGMA
56 extern struct device_plugin asix_sigma_plugin_info;
57 #endif
58 #ifdef HAVE_LA_LINK_MSO19
59 extern struct device_plugin link_mso19_plugin_info;
60 #endif
61 #ifdef HAVE_LA_ALSA
62 extern struct device_plugin alsa_plugin_info;
63 #endif
64
65
66 /* TODO: No linked list needed, this can be a simple array. */
67 int load_hwplugins(void)
68 {
69 #ifdef HAVE_LA_DEMO
70         plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info);
71 #endif
72 #ifdef HAVE_LA_SALEAE_LOGIC
73         plugins =
74             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
75 #endif
76 #ifdef HAVE_LA_OLS
77         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
78 #endif
79 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
80         plugins = g_slist_append(plugins,
81                            (gpointer *)&zeroplus_logic_cube_plugin_info);
82 #endif
83 #ifdef HAVE_LA_ASIX_SIGMA
84         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
85 #endif
86 #ifdef HAVE_LA_LINK_MSO19
87         plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info);
88 #endif
89 #ifdef HAVE_LA_ALSA
90         plugins = g_slist_append(plugins, (gpointer *)&alsa_plugin_info);
91 #endif
92
93
94         return SIGROK_OK;
95 }
96
97 GSList *list_hwplugins(void)
98 {
99         return plugins;
100 }
101
102 struct sigrok_device_instance *sigrok_device_instance_new(int index, int status,
103                 const char *vendor, const char *model, const char *version)
104 {
105         struct sigrok_device_instance *sdi;
106
107         if (!(sdi = malloc(sizeof(struct sigrok_device_instance))))
108                 return NULL;
109
110         sdi->index = index;
111         sdi->status = status;
112         sdi->instance_type = -1;
113         sdi->vendor = vendor ? strdup(vendor) : strdup("(unknown)");
114         sdi->model = model ? strdup(model) : NULL;
115         sdi->version = version ? strdup(version) : NULL;
116         sdi->priv = NULL;
117         sdi->usb = NULL;
118
119         return sdi;
120 }
121
122 struct sigrok_device_instance *get_sigrok_device_instance(
123                                 GSList *device_instances, int device_index)
124 {
125         struct sigrok_device_instance *sdi;
126         GSList *l;
127
128         for (l = device_instances; l; l = l->next) {
129                 sdi = (struct sigrok_device_instance *)(l->data);
130                 if (sdi->index == device_index)
131                         return sdi;
132         }
133         g_warning("could not find device index %d instance", device_index);
134
135         return NULL;
136 }
137
138 void sigrok_device_instance_free(struct sigrok_device_instance *sdi)
139 {
140         switch (sdi->instance_type) {
141         case USB_INSTANCE:
142                 usb_device_instance_free(sdi->usb);
143                 break;
144         case SERIAL_INSTANCE:
145                 serial_device_instance_free(sdi->serial);
146                 break;
147         default:
148                 /* No specific type, nothing extra to free. */
149                 break;
150         }
151
152         free(sdi->vendor);
153         free(sdi->model);
154         free(sdi->version);
155         free(sdi);
156 }
157
158 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
159                         uint8_t address, struct libusb_device_handle *hdl)
160 {
161         struct usb_device_instance *udi;
162
163         if (!(udi = malloc(sizeof(struct usb_device_instance))))
164                 return NULL;
165
166         udi->bus = bus;
167         udi->address = address;
168         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
169
170         return udi;
171 }
172
173 void usb_device_instance_free(struct usb_device_instance *usb)
174 {
175         /* Avoid compiler warnings. */
176         usb = usb;
177
178         /* Nothing to do for this device instance type. */
179 }
180
181 struct serial_device_instance *serial_device_instance_new(
182                                                 const char *port, int fd)
183 {
184         struct serial_device_instance *serial;
185
186         if (!(serial = malloc(sizeof(struct serial_device_instance))))
187                 return NULL;
188
189         serial->port = strdup(port);
190         serial->fd = fd;
191
192         return serial;
193 }
194
195 void serial_device_instance_free(struct serial_device_instance *serial)
196 {
197         free(serial->port);
198 }
199
200 int find_hwcap(int *capabilities, int hwcap)
201 {
202         int i;
203
204         for (i = 0; capabilities[i]; i++) {
205                 if (capabilities[i] == hwcap)
206                         return TRUE;
207         }
208
209         return FALSE;
210 }
211
212 struct hwcap_option *find_hwcap_option(int hwcap)
213 {
214         int i;
215
216         for (i = 0; hwcap_options[i].capability; i++) {
217                 if (hwcap_options[i].capability == hwcap)
218                         return &hwcap_options[i];
219         }
220
221         return NULL;
222 }
223
224 /* unnecessary level of indirection follows. */
225
226 void source_remove(int fd)
227 {
228
229         session_source_remove(fd);
230
231 }
232
233 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
234                 void *user_data)
235 {
236
237         session_source_add(fd, events, timeout, rcv_cb, user_data);
238
239 }