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