]>
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 | 39 | {HWCAP_SAMPLERATE, T_UINT64, "Sample rate", "samplerate"}, |
49d0ce50 | 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; | |
5b907f9b | 46 | #ifdef HAVE_LA_ASIX_SIGMA |
28a35d8a | 47 | extern struct device_plugin asix_sigma_plugin_info; |
5b907f9b | 48 | #endif |
a1bb33af | 49 | |
49d0ce50 | 50 | /* TODO: No linked list needed, this can be a simple array. */ |
a1bb33af UH |
51 | int load_hwplugins(void) |
52 | { | |
62c82025 | 53 | plugins = |
49d0ce50 UH |
54 | g_slist_append(plugins, (gpointer *)&saleae_logic_plugin_info); |
55 | plugins = g_slist_append(plugins, (gpointer *)&ols_plugin_info); | |
62c82025 | 56 | plugins = g_slist_append(plugins, |
49d0ce50 | 57 | (gpointer *)&zeroplus_logic_cube_plugin_info); |
5b907f9b | 58 | #ifdef HAVE_LA_ASIX_SIGMA |
28a35d8a | 59 | plugins = g_slist_append(plugins, (gpointer *)&asix_sigma_plugin_info); |
5b907f9b | 60 | #endif |
a1bb33af UH |
61 | |
62 | return SIGROK_OK; | |
63 | } | |
64 | ||
a1bb33af UH |
65 | GSList *list_hwplugins(void) |
66 | { | |
a1bb33af UH |
67 | return plugins; |
68 | } | |
69 | ||
a1bb33af | 70 | struct sigrok_device_instance *sigrok_device_instance_new(int index, int status, |
49d0ce50 | 71 | const char *vendor, const char *model, const char *version) |
a1bb33af UH |
72 | { |
73 | struct sigrok_device_instance *sdi; | |
74 | ||
49d0ce50 | 75 | if (!(sdi = malloc(sizeof(struct sigrok_device_instance)))) |
a1bb33af UH |
76 | return NULL; |
77 | ||
78 | sdi->index = index; | |
79 | sdi->status = status; | |
80 | sdi->instance_type = -1; | |
81 | sdi->vendor = strdup(vendor); | |
82 | sdi->model = strdup(model); | |
83 | sdi->version = strdup(version); | |
84 | sdi->usb = NULL; | |
85 | ||
86 | return sdi; | |
87 | } | |
88 | ||
62c82025 UH |
89 | struct sigrok_device_instance *get_sigrok_device_instance( |
90 | GSList *device_instances, int device_index) | |
a1bb33af UH |
91 | { |
92 | struct sigrok_device_instance *sdi; | |
93 | GSList *l; | |
94 | ||
62c82025 UH |
95 | for (l = device_instances; l; l = l->next) { |
96 | sdi = (struct sigrok_device_instance *)(l->data); | |
97 | if (sdi->index == device_index) | |
a1bb33af UH |
98 | return sdi; |
99 | } | |
100 | g_warning("could not find device index %d instance", device_index); | |
101 | ||
102 | return NULL; | |
103 | } | |
104 | ||
a1bb33af UH |
105 | void sigrok_device_instance_free(struct sigrok_device_instance *sdi) |
106 | { | |
62c82025 | 107 | switch (sdi->instance_type) { |
a1bb33af UH |
108 | case USB_INSTANCE: |
109 | usb_device_instance_free(sdi->usb); | |
110 | break; | |
111 | case SERIAL_INSTANCE: | |
112 | serial_device_instance_free(sdi->serial); | |
113 | break; | |
49d0ce50 | 114 | default: |
62c82025 | 115 | /* No specific type, nothing extra to free. */ |
49d0ce50 | 116 | break; |
a1bb33af UH |
117 | } |
118 | ||
119 | free(sdi->vendor); | |
120 | free(sdi->model); | |
121 | free(sdi->version); | |
122 | free(sdi); | |
a1bb33af UH |
123 | } |
124 | ||
62c82025 UH |
125 | struct usb_device_instance *usb_device_instance_new(uint8_t bus, |
126 | uint8_t address, struct libusb_device_handle *hdl) | |
a1bb33af UH |
127 | { |
128 | struct usb_device_instance *udi; | |
129 | ||
49d0ce50 | 130 | if (!(udi = malloc(sizeof(struct usb_device_instance)))) |
a1bb33af UH |
131 | return NULL; |
132 | ||
133 | udi->bus = bus; | |
134 | udi->address = address; | |
49d0ce50 | 135 | udi->devhdl = hdl; /* TODO: Check if this is NULL? */ |
a1bb33af UH |
136 | |
137 | return udi; | |
138 | } | |
139 | ||
a1bb33af UH |
140 | void usb_device_instance_free(struct usb_device_instance *usb) |
141 | { | |
afc8e4de UH |
142 | /* QUICK HACK */ |
143 | usb = usb; | |
a1bb33af | 144 | |
62c82025 | 145 | /* Nothing to do for this device instance type. */ |
a1bb33af UH |
146 | } |
147 | ||
49d0ce50 UH |
148 | struct serial_device_instance *serial_device_instance_new( |
149 | const char *port, int fd) | |
a1bb33af UH |
150 | { |
151 | struct serial_device_instance *serial; | |
152 | ||
49d0ce50 | 153 | if (!(serial = malloc(sizeof(struct serial_device_instance)))) |
a1bb33af UH |
154 | return NULL; |
155 | ||
156 | serial->port = strdup(port); | |
157 | serial->fd = fd; | |
158 | ||
159 | return serial; | |
160 | } | |
161 | ||
a1bb33af UH |
162 | void serial_device_instance_free(struct serial_device_instance *serial) |
163 | { | |
a1bb33af | 164 | free(serial->port); |
a1bb33af UH |
165 | } |
166 | ||
a1bb33af UH |
167 | int find_hwcap(int *capabilities, int hwcap) |
168 | { | |
169 | int i; | |
170 | ||
49d0ce50 | 171 | for (i = 0; capabilities[i]; i++) { |
62c82025 | 172 | if (capabilities[i] == hwcap) |
a1bb33af | 173 | return TRUE; |
49d0ce50 | 174 | } |
a1bb33af UH |
175 | |
176 | return FALSE; | |
177 | } | |
178 | ||
a1bb33af UH |
179 | struct hwcap_option *find_hwcap_option(int hwcap) |
180 | { | |
a1bb33af UH |
181 | int i; |
182 | ||
62c82025 | 183 | for (i = 0; hwcap_options[i].capability; i++) { |
49d0ce50 UH |
184 | if (hwcap_options[i].capability == hwcap) |
185 | return &hwcap_options[i]; | |
a1bb33af UH |
186 | } |
187 | ||
49d0ce50 | 188 | return NULL; |
a1bb33af UH |
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 | } |