add generic serial_device_instance and helper, add trigger DI
[sigrok:sigrok.git] / backend / hwplugin.c
1 /*
2  *   sigrok - hwplugin.c
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
26 #include <glib.h>
27 #include <gmodule.h>
28
29 #include "sigrok.h"
30 #include "hwplugin.h"
31
32
33 GSList *plugins;
34
35 struct hwcap_option hwcap_options[] = {
36         { HWCAP_SAMPLERATE, "Sample rate", "samplerate" },
37         { 0, NULL, NULL }
38 };
39
40
41 int load_hwplugins(void)
42 {
43         struct device_plugin *plugin;
44         GModule *module;
45         DIR *plugindir;
46         struct dirent *de;
47         int l;
48         gchar *module_path;
49
50         if(!g_module_supported())
51                 return SIGROK_NOK;
52
53         plugindir = opendir(HWPLUGIN_DIR);
54         if(plugindir == NULL)
55         {
56                 g_warning("Hardware plugin directory %s not found.", HWPLUGIN_DIR);
57                 return SIGROK_NOK;
58         }
59
60         while( (de = readdir(plugindir)) )
61         {
62                 l = strlen(de->d_name);
63                 if(l > 3 && !strncmp(de->d_name + l - 3, ".la", 3))
64                 {
65                         /* it's a libtool archive */
66                         module_path = g_malloc(strlen(HWPLUGIN_DIR) + strlen(de->d_name) + 1);
67                         sprintf(module_path, "%s/%s", HWPLUGIN_DIR, de->d_name);
68                         g_debug("loading %s", module_path);
69                         module = g_module_open(module_path, G_MODULE_BIND_LOCAL);
70                         if(module)
71                         {
72                                 if(g_module_symbol(module, "plugin_info", (gpointer *) &plugin))
73                                         plugins = g_slist_append(plugins, plugin);
74                                 else
75                                 {
76                                         g_warning("failed to find plugin info: %s", g_module_error());
77                                         g_module_close(module);
78                                 }
79                         }
80                         else
81                         {
82                                 g_warning("failed to load module: %s", g_module_error());
83                         }
84                         g_free(module_path);
85                 }
86         }
87
88         return SIGROK_OK;
89 }
90
91
92 GSList *list_hwplugins(void)
93 {
94
95         return plugins;
96 }
97
98
99 struct usb_device_instance *usb_device_instance_new(int index, int status, uint8_t bus,
100                 uint8_t address, struct libusb_device_handle *hdl)
101 {
102         struct usb_device_instance *udi;
103
104         udi = g_malloc(sizeof(struct usb_device_instance));
105         udi->index = index;
106         udi->status = status;
107         udi->bus = bus;
108         udi->address = address;
109         udi->devhdl = hdl;
110
111         return udi;
112 }
113
114
115 struct usb_device_instance *get_usb_device_instance(GSList *usb_devices, int device_index)
116 {
117         struct usb_device_instance *udi;
118         GSList *l;
119
120         udi = NULL;
121         for(l = usb_devices; l; l = l->next)
122         {
123                 udi = (struct usb_device_instance *) (l->data);
124                 if(udi->index == device_index)
125                         return udi;
126         }
127         g_warning("could not find device index %d instance", device_index);
128
129         return NULL;
130 }
131
132
133 struct serial_device_instance *get_serial_device_instance(GSList *serial_devices, int device_index)
134 {
135         struct serial_device_instance *sdi;
136         GSList *l;
137
138         sdi = NULL;
139         for(l = serial_devices; l; l = l->next)
140         {
141                 sdi = (struct serial_device_instance *) (l->data);
142                 if(sdi->index == device_index)
143                         return sdi;
144         }
145         g_warning("could not find device index %d instance", device_index);
146
147         return NULL;
148 }
149
150
151 struct hwcap_option *find_hwcap_option(int hwcap)
152 {
153         struct hwcap_option *hwo;
154         int i;
155
156         hwo = NULL;
157         for(i = 0; hwcap_options[i].capability; i++)
158         {
159                 if(hwcap_options[i].capability == hwcap)
160                 {
161                         hwo = &hwcap_options[i];
162                         break;
163                 }
164         }
165
166         return hwo;
167 }
168
169
170
171