]> sigrok.org Git - libsigrok.git/blob - hwplugin.c
aaf71b50ce94e840e8bddd5c0266ad58db5ba073
[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 extern struct device_plugin saleae_logic_plugin_info;
44 extern struct device_plugin ols_plugin_info;
45 extern struct device_plugin zeroplus_logic_cube_plugin_info;
46 #ifdef HAVE_LA_ASIX_SIGMA
47 extern struct device_plugin asix_sigma_plugin_info;
48 #endif
49
50 /* TODO: No linked list needed, this can be a simple array. */
51 int load_hwplugins(void)
52 {
53         plugins =
54             g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
55         plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
56         plugins = g_slist_append(plugins,
57                            (gpointer *)&zeroplus_logic_cube_plugin_info);
58 #ifdef HAVE_LA_ASIX_SIGMA
59         plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info);
60 #endif
61
62         return SIGROK_OK;
63 }
64
65 GSList *list_hwplugins(void)
66 {
67         return plugins;
68 }
69
70 struct sigrok_device_instance *sigrok_device_instance_new(int index, int status,
71                 const char *vendor, const char *model, const char *version)
72 {
73         struct sigrok_device_instance *sdi;
74
75         if (!(sdi = malloc(sizeof(struct sigrok_device_instance))))
76                 return NULL;
77
78         sdi->index = index;
79         sdi->status = status;
80         sdi->instance_type = -1;
81         sdi->vendor = strdup(vendor);
82         sdi->model = strdup(model);
83         sdi->version = strdup(version);
84         sdi->usb = NULL;
85
86         return sdi;
87 }
88
89 struct sigrok_device_instance *get_sigrok_device_instance(
90                                 GSList *device_instances, int device_index)
91 {
92         struct sigrok_device_instance *sdi;
93         GSList *l;
94
95         for (l = device_instances; l; l = l->next) {
96                 sdi = (struct sigrok_device_instance *)(l->data);
97                 if (sdi->index == device_index)
98                         return sdi;
99         }
100         g_warning("could not find device index %d instance", device_index);
101
102         return NULL;
103 }
104
105 void sigrok_device_instance_free(struct sigrok_device_instance *sdi)
106 {
107         switch (sdi->instance_type) {
108         case USB_INSTANCE:
109                 usb_device_instance_free(sdi->usb);
110                 break;
111         case SERIAL_INSTANCE:
112                 serial_device_instance_free(sdi->serial);
113                 break;
114         default:
115                 /* No specific type, nothing extra to free. */
116                 break;
117         }
118
119         free(sdi->vendor);
120         free(sdi->model);
121         free(sdi->version);
122         free(sdi);
123 }
124
125 struct usb_device_instance *usb_device_instance_new(uint8_t bus,
126                         uint8_t address, struct libusb_device_handle *hdl)
127 {
128         struct usb_device_instance *udi;
129
130         if (!(udi = malloc(sizeof(struct usb_device_instance))))
131                 return NULL;
132
133         udi->bus = bus;
134         udi->address = address;
135         udi->devhdl = hdl; /* TODO: Check if this is NULL? */
136
137         return udi;
138 }
139
140 void usb_device_instance_free(struct usb_device_instance *usb)
141 {
142         /* QUICK HACK */
143         usb = usb;
144
145         /* Nothing to do for this device instance type. */
146 }
147
148 struct serial_device_instance *serial_device_instance_new(
149                                                 const char *port, int fd)
150 {
151         struct serial_device_instance *serial;
152
153         if (!(serial = malloc(sizeof(struct serial_device_instance))))
154                 return NULL;
155
156         serial->port = strdup(port);
157         serial->fd = fd;
158
159         return serial;
160 }
161
162 void serial_device_instance_free(struct serial_device_instance *serial)
163 {
164         free(serial->port);
165 }
166
167 int find_hwcap(int *capabilities, int hwcap)
168 {
169         int i;
170
171         for (i = 0; capabilities[i]; i++) {
172                 if (capabilities[i] == hwcap)
173                         return TRUE;
174         }
175
176         return FALSE;
177 }
178
179 struct hwcap_option *find_hwcap_option(int hwcap)
180 {
181         int i;
182
183         for (i = 0; hwcap_options[i].capability; i++) {
184                 if (hwcap_options[i].capability == hwcap)
185                         return &hwcap_options[i];
186         }
187
188         return NULL;
189 }
190
191 void source_remove(int fd)
192 {
193         if (source_cb_remove)
194                 source_cb_remove(fd);
195 }
196
197 void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb,
198                 void *user_data)
199 {
200         if (source_cb_add)
201                 source_cb_add(fd, events, timeout, rcv_cb, user_data);
202 }