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