]>
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> | |
62c82025 | 26 | #include <sigrok.h> |
c4fffe1e | 27 | #include "config.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 | 40 | {HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate"}, |
3245dfcb | 41 | {HWCAP_CAPTURE_RATIO, T_UINT64, "Pre-trigger capture ratio", "captureratio"}, |
925dbf9f | 42 | {HWCAP_PATTERN_MODE, T_CHAR, "Pattern generator mode", "patternmode"}, |
49d0ce50 | 43 | {0, 0, NULL, NULL}, |
a1bb33af UH |
44 | }; |
45 | ||
a61b0e6a | 46 | #ifdef HAVE_LA_DEMO |
6239c175 | 47 | extern struct device_plugin demo_plugin_info; |
a61b0e6a | 48 | #endif |
960a75e4 | 49 | #ifdef HAVE_LA_SALEAE_LOGIC |
a1bb33af | 50 | extern struct device_plugin saleae_logic_plugin_info; |
960a75e4 UH |
51 | #endif |
52 | #ifdef HAVE_LA_OLS | |
a1bb33af | 53 | extern struct device_plugin ols_plugin_info; |
960a75e4 UH |
54 | #endif |
55 | #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE | |
a1bb33af | 56 | extern struct device_plugin zeroplus_logic_cube_plugin_info; |
960a75e4 | 57 | #endif |
5b907f9b | 58 | #ifdef HAVE_LA_ASIX_SIGMA |
28a35d8a | 59 | extern struct device_plugin asix_sigma_plugin_info; |
5b907f9b | 60 | #endif |
01cf8814 DR |
61 | #ifdef HAVE_LA_LINK_MSO19 |
62 | extern struct device_plugin link_mso19_plugin_info; | |
63 | #endif | |
64 | ||
a1bb33af | 65 | |
49d0ce50 | 66 | /* TODO: No linked list needed, this can be a simple array. */ |
a1bb33af UH |
67 | int load_hwplugins(void) |
68 | { | |
a61b0e6a | 69 | #ifdef HAVE_LA_DEMO |
6239c175 | 70 | plugins = g_slist_append(plugins, (gpointer *)&demo_plugin_info); |
a61b0e6a | 71 | #endif |
960a75e4 | 72 | #ifdef HAVE_LA_SALEAE_LOGIC |
62c82025 | 73 | plugins = |
49d0ce50 | 74 | g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info); |
960a75e4 UH |
75 | #endif |
76 | #ifdef HAVE_LA_OLS | |
49d0ce50 | 77 | plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info); |
960a75e4 UH |
78 | #endif |
79 | #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE | |
62c82025 | 80 | plugins = g_slist_append(plugins, |
49d0ce50 | 81 | (gpointer *)&zeroplus_logic_cube_plugin_info); |
960a75e4 | 82 | #endif |
5b907f9b | 83 | #ifdef HAVE_LA_ASIX_SIGMA |
28a35d8a | 84 | plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info); |
5b907f9b | 85 | #endif |
01cf8814 DR |
86 | #ifdef HAVE_LA_LINK_MSO19 |
87 | plugins = g_slist_append(plugins, (gpointer *)&link_mso19_plugin_info); | |
88 | #endif | |
a1bb33af UH |
89 | |
90 | return SIGROK_OK; | |
91 | } | |
92 | ||
a1bb33af UH |
93 | GSList *list_hwplugins(void) |
94 | { | |
a1bb33af UH |
95 | return plugins; |
96 | } | |
97 | ||
a1bb33af | 98 | struct sigrok_device_instance *sigrok_device_instance_new(int index, int status, |
49d0ce50 | 99 | const char *vendor, const char *model, const char *version) |
a1bb33af UH |
100 | { |
101 | struct sigrok_device_instance *sdi; | |
102 | ||
49d0ce50 | 103 | if (!(sdi = malloc(sizeof(struct sigrok_device_instance)))) |
a1bb33af UH |
104 | return NULL; |
105 | ||
106 | sdi->index = index; | |
107 | sdi->status = status; | |
108 | sdi->instance_type = -1; | |
925dbf9f BV |
109 | sdi->vendor = vendor ? strdup(vendor) : strdup("(unknown)"); |
110 | sdi->model = model ? strdup(model) : NULL; | |
111 | sdi->version = version ? strdup(version) : NULL; | |
01cf8814 | 112 | sdi->priv = NULL; |
a1bb33af UH |
113 | sdi->usb = NULL; |
114 | ||
115 | return sdi; | |
116 | } | |
117 | ||
62c82025 UH |
118 | struct sigrok_device_instance *get_sigrok_device_instance( |
119 | GSList *device_instances, int device_index) | |
a1bb33af UH |
120 | { |
121 | struct sigrok_device_instance *sdi; | |
122 | GSList *l; | |
123 | ||
62c82025 UH |
124 | for (l = device_instances; l; l = l->next) { |
125 | sdi = (struct sigrok_device_instance *)(l->data); | |
126 | if (sdi->index == device_index) | |
a1bb33af UH |
127 | return sdi; |
128 | } | |
129 | g_warning("could not find device index %d instance", device_index); | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
a1bb33af UH |
134 | void sigrok_device_instance_free(struct sigrok_device_instance *sdi) |
135 | { | |
62c82025 | 136 | switch (sdi->instance_type) { |
a1bb33af UH |
137 | case USB_INSTANCE: |
138 | usb_device_instance_free(sdi->usb); | |
139 | break; | |
140 | case SERIAL_INSTANCE: | |
141 | serial_device_instance_free(sdi->serial); | |
142 | break; | |
49d0ce50 | 143 | default: |
62c82025 | 144 | /* No specific type, nothing extra to free. */ |
49d0ce50 | 145 | break; |
a1bb33af UH |
146 | } |
147 | ||
148 | free(sdi->vendor); | |
149 | free(sdi->model); | |
150 | free(sdi->version); | |
151 | free(sdi); | |
a1bb33af UH |
152 | } |
153 | ||
62c82025 UH |
154 | struct usb_device_instance *usb_device_instance_new(uint8_t bus, |
155 | uint8_t address, struct libusb_device_handle *hdl) | |
a1bb33af UH |
156 | { |
157 | struct usb_device_instance *udi; | |
158 | ||
49d0ce50 | 159 | if (!(udi = malloc(sizeof(struct usb_device_instance)))) |
a1bb33af UH |
160 | return NULL; |
161 | ||
162 | udi->bus = bus; | |
163 | udi->address = address; | |
49d0ce50 | 164 | udi->devhdl = hdl; /* TODO: Check if this is NULL? */ |
a1bb33af UH |
165 | |
166 | return udi; | |
167 | } | |
168 | ||
a1bb33af UH |
169 | void usb_device_instance_free(struct usb_device_instance *usb) |
170 | { | |
17e1afcb | 171 | /* Avoid compiler warnings. */ |
afc8e4de | 172 | usb = usb; |
a1bb33af | 173 | |
62c82025 | 174 | /* Nothing to do for this device instance type. */ |
a1bb33af UH |
175 | } |
176 | ||
49d0ce50 UH |
177 | struct serial_device_instance *serial_device_instance_new( |
178 | const char *port, int fd) | |
a1bb33af UH |
179 | { |
180 | struct serial_device_instance *serial; | |
181 | ||
49d0ce50 | 182 | if (!(serial = malloc(sizeof(struct serial_device_instance)))) |
a1bb33af UH |
183 | return NULL; |
184 | ||
185 | serial->port = strdup(port); | |
186 | serial->fd = fd; | |
187 | ||
188 | return serial; | |
189 | } | |
190 | ||
a1bb33af UH |
191 | void serial_device_instance_free(struct serial_device_instance *serial) |
192 | { | |
a1bb33af | 193 | free(serial->port); |
a1bb33af UH |
194 | } |
195 | ||
a1bb33af UH |
196 | int find_hwcap(int *capabilities, int hwcap) |
197 | { | |
198 | int i; | |
199 | ||
49d0ce50 | 200 | for (i = 0; capabilities[i]; i++) { |
62c82025 | 201 | if (capabilities[i] == hwcap) |
a1bb33af | 202 | return TRUE; |
49d0ce50 | 203 | } |
a1bb33af UH |
204 | |
205 | return FALSE; | |
206 | } | |
207 | ||
a1bb33af UH |
208 | struct hwcap_option *find_hwcap_option(int hwcap) |
209 | { | |
a1bb33af UH |
210 | int i; |
211 | ||
62c82025 | 212 | for (i = 0; hwcap_options[i].capability; i++) { |
49d0ce50 UH |
213 | if (hwcap_options[i].capability == hwcap) |
214 | return &hwcap_options[i]; | |
a1bb33af UH |
215 | } |
216 | ||
49d0ce50 | 217 | return NULL; |
a1bb33af UH |
218 | } |
219 | ||
a1bb33af UH |
220 | void source_remove(int fd) |
221 | { | |
62c82025 | 222 | if (source_cb_remove) |
a1bb33af | 223 | source_cb_remove(fd); |
a1bb33af UH |
224 | } |
225 | ||
62c82025 UH |
226 | void source_add(int fd, int events, int timeout, receive_data_callback rcv_cb, |
227 | void *user_data) | |
a1bb33af | 228 | { |
62c82025 | 229 | if (source_cb_add) |
a1bb33af | 230 | source_cb_add(fd, events, timeout, rcv_cb, user_data); |
a1bb33af | 231 | } |