]>
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 | ||
22b02383 | 20 | #include "config.h" |
a1bb33af UH |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <string.h> | |
544a4582 | 25 | #include <glib.h> |
62c82025 | 26 | #include <sigrok.h> |
aa4b1107 | 27 | |
9f4bc44e UH |
28 | /* demo.c */ |
29 | extern GIOChannel channels[2]; | |
a1bb33af | 30 | |
544a4582 BV |
31 | struct source { |
32 | int fd; | |
33 | int events; | |
34 | int timeout; | |
a887e3da | 35 | sr_receive_data_callback cb; |
544a4582 BV |
36 | void *user_data; |
37 | }; | |
38 | ||
7d658874 | 39 | /* There can only be one session at a time. */ |
2872d21e | 40 | struct sr_session *session; |
7d658874 BV |
41 | int num_sources = 0; |
42 | ||
544a4582 BV |
43 | struct source *sources = NULL; |
44 | int source_timeout = -1; | |
45 | ||
46 | ||
8a2efef2 | 47 | struct sr_session *sr_session_new(void) |
a1bb33af | 48 | { |
2872d21e | 49 | session = calloc(1, sizeof(struct sr_session)); |
a1bb33af UH |
50 | |
51 | return session; | |
52 | } | |
53 | ||
8a2efef2 | 54 | void sr_session_destroy(void) |
a1bb33af | 55 | { |
a1bb33af UH |
56 | g_slist_free(session->devices); |
57 | ||
aa4b1107 | 58 | /* TODO: Loop over protocol decoders and free them. */ |
a1bb33af UH |
59 | |
60 | g_free(session); | |
a1bb33af UH |
61 | } |
62 | ||
8a2efef2 | 63 | void sr_session_device_clear(void) |
a1bb33af | 64 | { |
a1bb33af UH |
65 | g_slist_free(session->devices); |
66 | session->devices = NULL; | |
a1bb33af UH |
67 | } |
68 | ||
8a2efef2 | 69 | int sr_session_device_add(struct sr_device *device) |
a1bb33af UH |
70 | { |
71 | int ret; | |
72 | ||
aa4b1107 BV |
73 | if (device->plugin && device->plugin->open) { |
74 | ret = device->plugin->open(device->plugin_index); | |
e46b8fb1 | 75 | if (ret != SR_OK) |
aa4b1107 BV |
76 | return ret; |
77 | } | |
a1bb33af | 78 | |
aa4b1107 BV |
79 | session->devices = g_slist_append(session->devices, device); |
80 | ||
e46b8fb1 | 81 | return SR_OK; |
a1bb33af UH |
82 | } |
83 | ||
8bb416be | 84 | #if 0 |
8a2efef2 | 85 | void sr_session_pa_clear(void) |
a1bb33af | 86 | { |
62c82025 UH |
87 | /* |
88 | * The protocols are pointers to the global set of PA plugins, | |
89 | * so don't free them. | |
90 | */ | |
a1bb33af UH |
91 | g_slist_free(session->analyzers); |
92 | session->analyzers = NULL; | |
a1bb33af UH |
93 | } |
94 | ||
809c5f20 | 95 | void sr_session_pa_add(struct sr_analyzer *an) |
a1bb33af | 96 | { |
a1bb33af | 97 | session->analyzers = g_slist_append(session->analyzers, an); |
a1bb33af | 98 | } |
8bb416be | 99 | #endif |
a1bb33af | 100 | |
8a2efef2 | 101 | void sr_session_datafeed_callback_clear(void) |
a1bb33af | 102 | { |
a1bb33af UH |
103 | g_slist_free(session->datafeed_callbacks); |
104 | session->datafeed_callbacks = NULL; | |
a1bb33af UH |
105 | } |
106 | ||
13b05733 | 107 | void sr_session_datafeed_callback_add(sr_datafeed_callback callback) |
a1bb33af | 108 | { |
62c82025 UH |
109 | session->datafeed_callbacks = |
110 | g_slist_append(session->datafeed_callbacks, callback); | |
a1bb33af UH |
111 | } |
112 | ||
8a2efef2 | 113 | static void sr_session_run_poll() |
544a4582 BV |
114 | { |
115 | GPollFD *fds, my_gpollfd; | |
116 | int ret, i; | |
117 | ||
544a4582 BV |
118 | fds = NULL; |
119 | while (session->running) { | |
120 | if (fds) | |
121 | free(fds); | |
122 | ||
123 | /* Construct g_poll()'s array. */ | |
124 | fds = malloc(sizeof(GPollFD) * num_sources); | |
125 | for (i = 0; i < num_sources; i++) { | |
126 | #ifdef _WIN32 | |
127 | g_io_channel_win32_make_pollfd(&channels[0], | |
128 | sources[i].events, &my_gpollfd); | |
129 | #else | |
130 | my_gpollfd.fd = sources[i].fd; | |
131 | my_gpollfd.events = sources[i].events; | |
132 | fds[i] = my_gpollfd; | |
133 | #endif | |
134 | } | |
135 | ||
136 | ret = g_poll(fds, num_sources, source_timeout); | |
137 | ||
138 | for (i = 0; i < num_sources; i++) { | |
139 | if (fds[i].revents > 0 || (ret == 0 | |
140 | && source_timeout == sources[i].timeout)) { | |
141 | /* | |
142 | * Invoke the source's callback on an event, | |
143 | * or if the poll timeout out and this source | |
144 | * asked for that timeout. | |
145 | */ | |
146 | sources[i].cb(fds[i].fd, fds[i].revents, | |
7d658874 | 147 | sources[i].user_data); |
544a4582 BV |
148 | } |
149 | } | |
150 | } | |
151 | free(fds); | |
152 | ||
153 | } | |
154 | ||
8a2efef2 | 155 | int sr_session_start(void) |
7d658874 BV |
156 | { |
157 | struct sr_device *device; | |
158 | GSList *l; | |
159 | int ret; | |
160 | ||
161 | g_message("session: starting"); | |
162 | for (l = session->devices; l; l = l->next) { | |
163 | device = l->data; | |
164 | if ((ret = device->plugin->start_acquisition( | |
165 | device->plugin_index, device)) != SR_OK) | |
166 | break; | |
167 | } | |
168 | ||
169 | return ret; | |
170 | } | |
171 | ||
8a2efef2 | 172 | void sr_session_run(void) |
7d658874 BV |
173 | { |
174 | ||
175 | g_message("session: running"); | |
176 | session->running = TRUE; | |
177 | ||
178 | /* do we have real sources? */ | |
179 | if (num_sources == 1 && sources[0].fd == -1) | |
180 | /* dummy source, freewheel over it */ | |
181 | while (session->running) | |
182 | sources[0].cb(-1, 0, sources[0].user_data); | |
183 | else | |
184 | /* real sources, use g_poll() main loop */ | |
8a2efef2 | 185 | sr_session_run_poll(); |
7d658874 BV |
186 | |
187 | } | |
188 | ||
8a2efef2 | 189 | void sr_session_halt(void) |
544a4582 BV |
190 | { |
191 | ||
7d658874 | 192 | g_message("session: halting"); |
544a4582 BV |
193 | session->running = FALSE; |
194 | ||
195 | } | |
196 | ||
8a2efef2 | 197 | void sr_session_stop(void) |
a1bb33af | 198 | { |
5c2d46d1 | 199 | struct sr_device *device; |
a1bb33af UH |
200 | GSList *l; |
201 | ||
7d658874 | 202 | g_message("session: stopping"); |
544a4582 | 203 | session->running = FALSE; |
62c82025 | 204 | for (l = session->devices; l; l = l->next) { |
a1bb33af | 205 | device = l->data; |
b8c2f85f | 206 | if (device->plugin && device->plugin->stop_acquisition) |
aa4b1107 | 207 | device->plugin->stop_acquisition(device->plugin_index, device); |
a1bb33af | 208 | } |
544a4582 | 209 | |
a1bb33af UH |
210 | } |
211 | ||
8a2efef2 | 212 | void sr_session_bus(struct sr_device *device, struct sr_datafeed_packet *packet) |
a1bb33af UH |
213 | { |
214 | GSList *l; | |
13b05733 | 215 | sr_datafeed_callback cb; |
a1bb33af | 216 | |
62c82025 | 217 | /* |
aa4b1107 | 218 | * TODO: Send packet through PD pipe, and send the output of that to |
62c82025 | 219 | * the callbacks as well. |
a1bb33af | 220 | */ |
62c82025 | 221 | for (l = session->datafeed_callbacks; l; l = l->next) { |
a1bb33af UH |
222 | cb = l->data; |
223 | cb(device, packet); | |
224 | } | |
a1bb33af UH |
225 | } |
226 | ||
8a2efef2 | 227 | void sr_session_source_add(int fd, int events, int timeout, |
a887e3da | 228 | sr_receive_data_callback callback, void *user_data) |
544a4582 BV |
229 | { |
230 | struct source *new_sources, *s; | |
231 | ||
232 | new_sources = calloc(1, sizeof(struct source) * (num_sources + 1)); | |
233 | ||
234 | if (sources) { | |
235 | memcpy(new_sources, sources, | |
236 | sizeof(struct source) * num_sources); | |
237 | free(sources); | |
238 | } | |
239 | ||
240 | s = &new_sources[num_sources++]; | |
241 | s->fd = fd; | |
242 | s->events = events; | |
243 | s->timeout = timeout; | |
244 | s->cb = callback; | |
245 | s->user_data = user_data; | |
246 | sources = new_sources; | |
247 | ||
248 | if (timeout != source_timeout && timeout > 0 | |
249 | && (source_timeout == -1 || timeout < source_timeout)) | |
250 | source_timeout = timeout; | |
251 | } | |
252 | ||
8a2efef2 | 253 | void sr_session_source_remove(int fd) |
544a4582 BV |
254 | { |
255 | struct source *new_sources; | |
256 | int old, new; | |
257 | ||
258 | if (!sources) | |
259 | return; | |
260 | ||
261 | new_sources = calloc(1, sizeof(struct source) * num_sources); | |
262 | for (old = 0; old < num_sources; old++) | |
263 | if (sources[old].fd != fd) | |
264 | memcpy(&new_sources[new++], &sources[old], | |
265 | sizeof(struct source)); | |
266 | ||
267 | if (old != new) { | |
268 | free(sources); | |
269 | sources = new_sources; | |
270 | num_sources--; | |
271 | } else { | |
272 | /* Target fd was not found. */ | |
273 | free(new_sources); | |
274 | } | |
275 | } | |
276 |