/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
1
#define _POSIX_C_SOURCE 199309L
2
#include <stdio.h>
3
#include <string.h>
4
#include <strings.h>
5
#include <stdlib.h>
6
#include <stdint.h>
7
#include <stdarg.h>
8
#include <fcntl.h>
9
#include <unistd.h>
10
#include <sys/ioctl.h>
11
#include <sys/mman.h>
12
#include <arpa/inet.h>
13
#include <errno.h>
14
#include <assert.h>
86 by Suren A. Chilingaryan
Change timeout definition in Events API from struct timespec to pcilib_timeout_t
15
#include <time.h>
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
16
17
#include "pci.h"
18
19
#include "tools.h"
20
#include "error.h"
21
86 by Suren A. Chilingaryan
Change timeout definition in Events API from struct timespec to pcilib_timeout_t
22
#ifndef __timespec_defined
23
struct timespec {
24
    time_t tv_sec;
25
    long tv_nsec;
26
};
27
#endif /* __timespec_defined */
28
29
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
30
pcilib_event_t pcilib_find_event(pcilib_t *ctx, const char *event) {
31
    int i;
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
32
236 by Suren A. Chilingaryan
Big redign of model structures
33
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
34
    const pcilib_event_description_t *events = model_info->events;
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
35
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
36
    for (i = 0; events[i].name; i++) {
117 by Suren A. Chilingaryan
new event architecture, first trial
37
	if (!strcasecmp(events[i].name, event)) return events[i].evid;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
38
    }
39
40
    return (pcilib_event_t)-1;
41
}
42
117 by Suren A. Chilingaryan
new event architecture, first trial
43
pcilib_event_data_type_t pcilib_find_event_data_type(pcilib_t *ctx, pcilib_event_t event, const char *data_type) {
44
    int i;
45
    
236 by Suren A. Chilingaryan
Big redign of model structures
46
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47
    const pcilib_event_data_type_description_t *data_types = model_info->data_types;
117 by Suren A. Chilingaryan
new event architecture, first trial
48
    
49
    for (i = 0; data_types[i].name; i++) {
50
	if ((data_types[i].evid&event)&&(!strcasecmp(data_types[i].name, data_type))) return data_types[i].data_type;
51
    }
52
53
    return (pcilib_event_data_type_t)-1;
54
}
55
56
int pcilib_init_event_engine(pcilib_t *ctx) {
236 by Suren A. Chilingaryan
Big redign of model structures
57
    const pcilib_event_api_description_t *api;
58
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
59
60
    api = model_info->api;
61
117 by Suren A. Chilingaryan
new event architecture, first trial
62
    if ((api)&&(api->init)) {
63
	ctx->event_ctx = api->init(ctx);
64
	if (ctx->event_ctx) {
65
	    ctx->event_ctx->pcilib = ctx;
66
	}
67
    }
68
    
69
    return 0;
70
}
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
71
247 by Suren A. Chilingaryan
New error reporting public interface
72
pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx) {
73
    return ctx->event_ctx;
74
}
75
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
76
int pcilib_reset(pcilib_t *ctx) {
236 by Suren A. Chilingaryan
Big redign of model structures
77
    const pcilib_event_api_description_t *api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
78
    
236 by Suren A. Chilingaryan
Big redign of model structures
79
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
80
236 by Suren A. Chilingaryan
Big redign of model structures
81
    api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
82
    if (!api) {
83
	pcilib_error("Event API is not supported by the selected model");
84
	return PCILIB_ERROR_NOTSUPPORTED;
85
    }
86
    
87
    if (api->reset) 
88
	return api->reset(ctx->event_ctx);
89
	
90
    return 0;
91
}
92
117 by Suren A. Chilingaryan
new event architecture, first trial
93
int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callback_t callback, void *user) {
236 by Suren A. Chilingaryan
Big redign of model structures
94
    const pcilib_event_api_description_t *api;
95
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
117 by Suren A. Chilingaryan
new event architecture, first trial
96
236 by Suren A. Chilingaryan
Big redign of model structures
97
    api = model_info->api;
117 by Suren A. Chilingaryan
new event architecture, first trial
98
    if (!api) {
99
	pcilib_error("Event API is not supported by the selected model");
100
	return PCILIB_ERROR_NOTSUPPORTED;
101
    }
102
103
    ctx->event_ctx->params.rawdata.callback = callback;
104
    ctx->event_ctx->params.rawdata.user = user;
105
    
106
    return 0;    
107
}
108
109
int pcilib_configure_autostop(pcilib_t *ctx, size_t max_events, pcilib_timeout_t duration) {
236 by Suren A. Chilingaryan
Big redign of model structures
110
    const pcilib_event_api_description_t *api;
111
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
117 by Suren A. Chilingaryan
new event architecture, first trial
112
236 by Suren A. Chilingaryan
Big redign of model structures
113
    api = model_info->api;
117 by Suren A. Chilingaryan
new event architecture, first trial
114
    if (!api) {
115
	pcilib_error("Event API is not supported by the selected model");
116
	return PCILIB_ERROR_NOTSUPPORTED;
117
    }
118
119
    ctx->event_ctx->params.autostop.max_events = max_events;
120
    ctx->event_ctx->params.autostop.duration = duration;
121
    
122
    return 0;    
123
}
124
151 by Suren A. Chilingaryan
small cleanup
125
int pcilib_configure_autotrigger(pcilib_t *ctx, pcilib_timeout_t interval, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
126
	/* To support hardware without autotriggering, we need to provide in event.c a code 
127
	to generate multiple triggers in a thread (available in cli). The function should 
128
	be re-enabled afterwards: just set parameters and let implementation decide if it 
129
	can make triggering in hardware or will use our emulation */
130
	
131
    return PCILIB_ERROR_NOTSUPPORTED;
132
}
133
135 by Suren A. Chilingaryan
Allow to configure the number of preprocessing threads
134
int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads) {
236 by Suren A. Chilingaryan
Big redign of model structures
135
    const pcilib_event_api_description_t *api;
136
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
135 by Suren A. Chilingaryan
Allow to configure the number of preprocessing threads
137
236 by Suren A. Chilingaryan
Big redign of model structures
138
    api = model_info->api;
135 by Suren A. Chilingaryan
Allow to configure the number of preprocessing threads
139
    if (!api) {
140
	pcilib_error("Event API is not supported by the selected model");
141
	return PCILIB_ERROR_NOTSUPPORTED;
142
    }
143
144
    ctx->event_ctx->params.parallel.max_threads = max_threads;
145
146
    return 0;
147
}
148
117 by Suren A. Chilingaryan
new event architecture, first trial
149
int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
324 by Suren A. Chilingaryan
Documentation update
150
    int err;
151
    pcilib_register_value_t max_threads;
152
236 by Suren A. Chilingaryan
Big redign of model structures
153
    const pcilib_event_api_description_t *api;
154
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
155
236 by Suren A. Chilingaryan
Big redign of model structures
156
    api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
157
    if (!api) {
158
	pcilib_error("Event API is not supported by the selected model");
159
	return PCILIB_ERROR_NOTSUPPORTED;
160
    }
161
324 by Suren A. Chilingaryan
Documentation update
162
    err = pcilib_read_register(ctx, "conf", "max_threads", &max_threads);
163
    if (!err) {
164
	err = pcilib_configure_preprocessing_threads(ctx, max_threads);
165
	if (err) pcilib_warning("Error (%i) configuring number of preprocessing threads", err);
166
    }
167
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
168
    if (api->start) 
117 by Suren A. Chilingaryan
new event architecture, first trial
169
	return api->start(ctx->event_ctx, event_mask, flags);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
170
171
    return 0;
172
}
173
117 by Suren A. Chilingaryan
new event architecture, first trial
174
int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags) {
236 by Suren A. Chilingaryan
Big redign of model structures
175
    const pcilib_event_api_description_t *api;
176
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
177
236 by Suren A. Chilingaryan
Big redign of model structures
178
    api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
179
    if (!api) {
180
	pcilib_error("Event API is not supported by the selected model");
181
	return PCILIB_ERROR_NOTSUPPORTED;
182
    }
183
184
    if (api->stop) 
117 by Suren A. Chilingaryan
new event architecture, first trial
185
	return api->stop(ctx->event_ctx, flags);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
186
187
    return 0;
188
}
189
117 by Suren A. Chilingaryan
new event architecture, first trial
190
int pcilib_stream(pcilib_t *ctx, pcilib_event_callback_t callback, void *user) {
236 by Suren A. Chilingaryan
Big redign of model structures
191
    const pcilib_event_api_description_t *api;
192
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
117 by Suren A. Chilingaryan
new event architecture, first trial
193
236 by Suren A. Chilingaryan
Big redign of model structures
194
    api = model_info->api;
117 by Suren A. Chilingaryan
new event architecture, first trial
195
    if (!api) {
196
	pcilib_error("Event API is not supported by the selected model");
197
	return PCILIB_ERROR_NOTSUPPORTED;
198
    }
199
200
    if (api->stream)
201
	return api->stream(ctx->event_ctx, callback, user);
202
203
    if (api->next_event) {
204
	pcilib_error("Streaming using next_event API is not implemented yet");
205
    }
206
207
    pcilib_error("Event enumeration is not suppored by API");
208
    return PCILIB_ERROR_NOTSUPPORTED;
209
}
210
/*
211
typedef struct {
212
    pcilib_event_id_t event_id;
213
    pcilib_event_info_t *info;
214
} pcilib_return_event_callback_context_t;
215
216
static int pcilib_return_event_callback(pcilib_event_id_t event_id, pcilib_event_info_t *info, void *user) {
217
    pcilib_return_event_callback_context_t *ctx = (pcilib_return_event_callback_context_t*)user;
218
    ctx->event_id = event_id;
219
    ctx->info = info;
220
}
221
*/
222
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
223
int pcilib_get_next_event(pcilib_t *ctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
236 by Suren A. Chilingaryan
Big redign of model structures
224
    const pcilib_event_api_description_t *api;
117 by Suren A. Chilingaryan
new event architecture, first trial
225
//    pcilib_return_event_callback_context_t user;
236 by Suren A. Chilingaryan
Big redign of model structures
226
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
227
236 by Suren A. Chilingaryan
Big redign of model structures
228
    api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
229
    if (!api) {
230
	pcilib_error("Event API is not supported by the selected model");
231
	return PCILIB_ERROR_NOTSUPPORTED;
232
    }
233
234
    if (api->next_event) 
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
235
	return api->next_event(ctx->event_ctx, timeout, evid, info_size, info);
117 by Suren A. Chilingaryan
new event architecture, first trial
236
237
/*	
238
    if (api->stream) {
239
	err = api->stream(ctx->event_ctx, 1, timeout, pcilib_return_event_callback, &user);
240
	if (err) return err;
241
	
242
	if (evid) *evid = user->event_id;
243
	if (info) *info = user->info;
244
245
	return 0;
246
    }
247
*/
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
248
249
    pcilib_error("Event enumeration is not suppored by API");
117 by Suren A. Chilingaryan
new event architecture, first trial
250
    return PCILIB_ERROR_NOTSUPPORTED;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
251
}
252
253
int pcilib_trigger(pcilib_t *ctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
236 by Suren A. Chilingaryan
Big redign of model structures
254
    const pcilib_event_api_description_t *api;
255
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
256
236 by Suren A. Chilingaryan
Big redign of model structures
257
    api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
258
    if (!api) {
259
	pcilib_error("Event API is not supported by the selected model");
260
	return PCILIB_ERROR_NOTSUPPORTED;
261
    }
262
263
    if (api->trigger) 
264
	return api->trigger(ctx->event_ctx, event, trigger_size, trigger_data);
265
266
    pcilib_error("Self triggering is not supported by the selected model");
267
    return PCILIB_ERROR_NOTSUPPORTED;
268
}
269
270
271
void *pcilib_get_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t *size) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
272
    int err;
273
    void *res = NULL;
236 by Suren A. Chilingaryan
Big redign of model structures
274
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
275
    const pcilib_event_api_description_t *api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
276
    if (!api) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
277
	if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
278
	pcilib_error("Event API is not supported by the selected model");
279
	return NULL;
280
    }
281
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
282
    if (api->get_data) {
283
	err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, size, &res);
284
	if (err) {
285
	    if (size) *size = (size_t)err;
286
	    return NULL;
287
	}
288
	return res;
289
    }
290
    
291
    if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
292
    return NULL;
293
}
294
117 by Suren A. Chilingaryan
new event architecture, first trial
295
int pcilib_copy_data_with_argument(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t arg_size, void *arg, size_t size, void *buf, size_t *retsize) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
296
    int err;
297
    void *res = buf;
236 by Suren A. Chilingaryan
Big redign of model structures
298
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
299
    const pcilib_event_api_description_t *api = model_info->api;
117 by Suren A. Chilingaryan
new event architecture, first trial
300
    if (!api) {
301
	pcilib_error("Event API is not supported by the selected model");
302
	return PCILIB_ERROR_NOTSUPPORTED;
303
    }
304
305
    if (api->get_data) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
306
	err = api->get_data(ctx->event_ctx, event_id, data_type, arg_size, arg, &size, &res);
307
	if (err) return err;
308
	
309
	if (buf != res) memcpy(buf, res, size);
117 by Suren A. Chilingaryan
new event architecture, first trial
310
	
311
	if (retsize) *retsize = size;
312
	return 0;
313
    }	
314
315
    return PCILIB_ERROR_NOTSUPPORTED;
316
}
317
318
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
319
void *pcilib_get_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t *size) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
320
    int err;
321
    void *res = NULL;
236 by Suren A. Chilingaryan
Big redign of model structures
322
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
323
    const pcilib_event_api_description_t *api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
324
    if (!api) {
325
	pcilib_error("Event API is not supported by the selected model");
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
326
	if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
327
	return NULL;
328
    }
329
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
330
    if (api->get_data) {
331
	err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, size, &res);
332
	if (err) {
333
	    if (size) *size = (size_t)err;
334
	    return NULL;
335
	}
336
	return res;
337
    }
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
338
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
339
    if (size) *size = (size_t)PCILIB_ERROR_NOTSUPPORTED;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
340
    return NULL;
341
}
342
117 by Suren A. Chilingaryan
new event architecture, first trial
343
int pcilib_copy_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, size_t size, void *buf, size_t *ret_size) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
344
    int err;
345
    void *res = buf;
236 by Suren A. Chilingaryan
Big redign of model structures
346
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
347
    const pcilib_event_api_description_t *api = model_info->api;
117 by Suren A. Chilingaryan
new event architecture, first trial
348
    if (!api) {
349
	pcilib_error("Event API is not supported by the selected model");
350
	return PCILIB_ERROR_NOTSUPPORTED;
351
    }
352
353
    if (api->get_data) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
354
	err = api->get_data(ctx->event_ctx, event_id, data_type, 0, NULL, &size, &res);
355
	if (err) return err;
117 by Suren A. Chilingaryan
new event architecture, first trial
356
	
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
357
	if (buf != res) memcpy(buf, res, size);
358
117 by Suren A. Chilingaryan
new event architecture, first trial
359
	if (ret_size) *ret_size = size;
360
	return 0;
361
    }
362
363
    return PCILIB_ERROR_NOTSUPPORTED;
364
}
365
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
366
int pcilib_return_data(pcilib_t *ctx, pcilib_event_id_t event_id, pcilib_event_data_type_t data_type, void *data) {
236 by Suren A. Chilingaryan
Big redign of model structures
367
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
368
    const pcilib_event_api_description_t *api = model_info->api;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
369
    if (!api) {
370
	pcilib_error("Event API is not supported by the selected model");
371
	return PCILIB_ERROR_NOTSUPPORTED;
372
    }
373
374
    if (api->return_data) 
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
375
	return api->return_data(ctx->event_ctx, event_id, data_type, data);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
376
377
    return 0;
378
}
379
380
381
typedef struct {
382
    pcilib_t *ctx;
383
    
384
    size_t *size;
385
    void **data;
386
} pcilib_grab_callback_user_data_t;
387
117 by Suren A. Chilingaryan
new event architecture, first trial
388
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
389
static int pcilib_grab_callback(pcilib_event_t event, pcilib_event_id_t event_id, void *vuser) {
390
    int err;
391
    void *data;
392
    size_t size;
393
    int allocated = 0;
394
395
    pcilib_grab_callback_user_data_t *user = (pcilib_grab_callback_user_data_t*)vuser;
396
397
    data = pcilib_get_data(user->ctx, event_id, PCILIB_EVENT_DATA, &size);
398
    if (!data) {
399
	pcilib_error("Error getting event data");
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
400
	return -(int)size;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
401
    }
402
    
403
    if (*(user->data)) {
404
	if ((user->size)&&(*(user->size) < size)) {
405
	    pcilib_error("The supplied buffer does not have enough space to hold the event data. Buffer size is %z, but %z is required", user->size, size);
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
406
	    return -PCILIB_ERROR_MEMORY;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
407
	}
408
409
	*(user->size) = size;
410
    } else {
411
	*(user->data) = malloc(size);
412
	if (!*(user->data)) {
413
	    pcilib_error("Memory allocation (%i bytes) for event data is failed");
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
414
	    return -PCILIB_ERROR_MEMORY;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
415
	}
416
	if (*(user->size)) *(user->size) = size;
417
	allocated = 1;
418
    }
419
    
420
    memcpy(*(user->data), data, size);
421
    
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
422
    err = pcilib_return_data(user->ctx, event_id, PCILIB_EVENT_DATA, data);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
423
    if (err) {
424
	if (allocated) {
425
	    free(*(user->data));
426
	    *(user->data) = NULL;
427
	}
428
	pcilib_error("The event data had been overwritten before it was returned, data corruption may occur");
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
429
	return -err;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
430
    }
431
    
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
432
    return PCILIB_STREAMING_CONTINUE;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
433
}
434
86 by Suren A. Chilingaryan
Change timeout definition in Events API from struct timespec to pcilib_timeout_t
435
int pcilib_grab(pcilib_t *ctx, pcilib_event_t event_mask, size_t *size, void **data, pcilib_timeout_t timeout) {
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
436
    int err;
117 by Suren A. Chilingaryan
new event architecture, first trial
437
    pcilib_event_id_t eid;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
438
    
439
    pcilib_grab_callback_user_data_t user = {ctx, size, data};
440
    
117 by Suren A. Chilingaryan
new event architecture, first trial
441
    err = pcilib_start(ctx, event_mask, PCILIB_EVENT_FLAGS_DEFAULT);
324 by Suren A. Chilingaryan
Documentation update
442
    if (!err) {
443
	if (timeout == PCILIB_TIMEOUT_IMMEDIATE) {
444
	     err = pcilib_trigger(ctx, event_mask, 0, NULL);
445
	     timeout = PCILIB_EVENT_TIMEOUT;
446
	}
447
    }
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
448
    if (!err) {
126 by Suren A. Chilingaryan
multithread preprocessing of ipecamera frames and code reorganization
449
	err = pcilib_get_next_event(ctx, timeout, &eid, 0, NULL);
117 by Suren A. Chilingaryan
new event architecture, first trial
450
	if (!err) pcilib_grab_callback(event_mask, eid, &user);
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
451
    }
117 by Suren A. Chilingaryan
new event architecture, first trial
452
    pcilib_stop(ctx, PCILIB_EVENT_FLAGS_DEFAULT);
92 by root
Report errors during event grabbing
453
    return err;
47 by Suren A. Chilingaryan
Support FIFO reading/writting, code restructurization, few fixes
454
}