]> sigrok.org Git - libsigrok.git/blame - hwplugin.c
libsigrok: Coding style fixes.
[libsigrok.git] / hwplugin.c
CommitLineData
a1bb33af
UH
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 <gmodule.h>
27#include "sigrok.h"
28
29source_callback_add source_cb_add = NULL;
30source_callback_remove source_cb_remove = NULL;
31
32/* the list of loaded plugins lives here */
33GSList *plugins;
34
35/* this enumerates which plugin capabilities correspond to user-settable options */
36struct hwcap_option hwcap_options[] = {
37 { HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate" },
38 { 0, 0, NULL, NULL }
39};
40
41extern struct device_plugin saleae_logic_plugin_info;
42extern struct device_plugin ols_plugin_info;
43extern struct device_plugin zeroplus_logic_cube_plugin_info;
44
45int load_hwplugins(void)
46{
47 plugins = g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info);
48 plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info);
49 plugins = g_slist_append(plugins, (gpointer *)&zeroplus_logic_cube_plugin_info);
50
51 return SIGROK_OK;
52}
53
54
55GSList *list_hwplugins(void)
56{
57
58 return plugins;
59}
60
61
62struct sigrok_device_instance *sigrok_device_instance_new(int index, int status,
63 char *vendor, char *model, char *version)
64{
65 struct sigrok_device_instance *sdi;
66
67 sdi = malloc(sizeof(struct sigrok_device_instance));
68 if(!sdi)
69 return NULL;
70
71 sdi->index = index;
72 sdi->status = status;
73 sdi->instance_type = -1;
74 sdi->vendor = strdup(vendor);
75 sdi->model = strdup(model);
76 sdi->version = strdup(version);
77 sdi->usb = NULL;
78
79 return sdi;
80}
81
82
83struct sigrok_device_instance *get_sigrok_device_instance(GSList *device_instances, int device_index)
84{
85 struct sigrok_device_instance *sdi;
86 GSList *l;
87
88 sdi = NULL;
89 for(l = device_instances; l; l = l->next) {
90 sdi = (struct sigrok_device_instance *) (l->data);
91 if(sdi->index == device_index)
92 return sdi;
93 }
94 g_warning("could not find device index %d instance", device_index);
95
96 return NULL;
97}
98
99
100void sigrok_device_instance_free(struct sigrok_device_instance *sdi)
101{
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 /* no specific type, nothing extra to free */
111 }
112
113 free(sdi->vendor);
114 free(sdi->model);
115 free(sdi->version);
116 free(sdi);
117
118}
119
120
121struct usb_device_instance *usb_device_instance_new(uint8_t bus, uint8_t address,
122 struct libusb_device_handle *hdl)
123{
124 struct usb_device_instance *udi;
125
126 udi = malloc(sizeof(struct usb_device_instance));
127 if(!udi)
128 return NULL;
129
130 udi->bus = bus;
131 udi->address = address;
132 udi->devhdl = hdl;
133
134 return udi;
135}
136
137
138void usb_device_instance_free(struct usb_device_instance *usb)
139{
afc8e4de
UH
140 /* QUICK HACK */
141 usb = usb;
a1bb33af
UH
142
143 /* nothing to do for this device instance type */
144
145}
146
147
148struct serial_device_instance *serial_device_instance_new(char *port, int fd)
149{
150 struct serial_device_instance *serial;
151
152 serial = malloc(sizeof(struct serial_device_instance));
153 if(!serial)
154 return NULL;
155
156 serial->port = strdup(port);
157 serial->fd = fd;
158
159 return serial;
160}
161
162
163void serial_device_instance_free(struct serial_device_instance *serial)
164{
165
166 free(serial->port);
167
168}
169
170
171int find_hwcap(int *capabilities, int hwcap)
172{
173 int i;
174
175 for(i = 0; capabilities[i]; i++)
176 if(capabilities[i] == hwcap)
177 return TRUE;
178
179 return FALSE;
180}
181
182
183struct hwcap_option *find_hwcap_option(int hwcap)
184{
185 struct hwcap_option *hwo;
186 int i;
187
188 hwo = NULL;
189 for(i = 0; hwcap_options[i].capability; i++)
190 {
191 if(hwcap_options[i].capability == hwcap)
192 {
193 hwo = &hwcap_options[i];
194 break;
195 }
196 }
197
198 return hwo;
199}
200
201
202void source_remove(int fd)
203{
204
205 if(source_cb_remove)
206 source_cb_remove(fd);
207
208}
209
210
211void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, void *user_data)
212{
213
214 if(source_cb_add)
215 source_cb_add(fd, events, timeout, rcv_cb, user_data);
216
217}
218
219
220
221