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