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