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