summaryrefslogtreecommitdiffstats
path: root/events.c
blob: e719e2dff5e85b5da052778676865015713be8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#define _BSD_SOURCE
#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <pthread.h>
#include <assert.h>

#include <ufodecode.h>

#include <pcilib.h>
#include <pcilib/tools.h>
#include <pcilib/error.h>

#include "ipecamera.h"
#include "private.h"
#include "events.h"

#define LOCK(lock_name) \
    err = pcilib_lock(ctx->lock_name##_lock); \
    if (err) { \
	pcilib_error("IPECamera is busy"); \
	return PCILIB_ERROR_BUSY; \
    } \
    ctx->lock_name##_locked = 1;

#define UNLOCK(lock_name) \
    if (ctx->lock_name##_locked) { \
	pcilib_unlock(ctx->lock_name##_lock); \
	ctx->lock_name##_locked = 0; \
    }

int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, void *user) {
    int run_flag = 1;
    int res, err = 0;
    int do_stop = 0;
    
    ipecamera_event_info_t info;
    ipecamera_t *ctx = (ipecamera_t*)vctx;

    if (!ctx) {
	pcilib_error("IPECamera imaging is not initialized");
	return PCILIB_ERROR_NOTINITIALIZED;
    }

    ipecamera_debug(API, "ipecamera: start streaming");

    LOCK(stream);

    ctx->streaming = 1;
    ctx->run_streamer = 1;

    if (!ctx->started) {
	err = ipecamera_start(vctx, PCILIB_EVENTS_ALL, PCILIB_EVENT_FLAGS_DEFAULT);
	if (err) {
	    ctx->streaming = 0;
	    return err;
	}
	
	do_stop = 1;
    }
    
    if (ctx->parse_data) {
	    // This loop iterates while the generation
	while ((run_flag)&&((ctx->run_streamer)||(ctx->reported_id != ctx->event_id))) {
#ifdef IPECAMERA_ANNOUNCE_READY
	    while (((!ctx->preproc)&&(ctx->reported_id != ctx->event_id))||((ctx->preproc)&&(ctx->reported_id != ctx->preproc_id))) {
#else /* IPECAMERA_ANNOUNCE_READY */
	    while (ctx->reported_id != ctx->event_id) {
#endif /* IPECAMERA_ANNOUNCE_READY */
		if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) {
		    ipecamera_debug(HARDWARE, "Skipping events %zu to %zu as preprocessing is too slow. We are currently %zu buffers beyond, but only %zu buffers are available and safety limit is %zu",
			ctx->reported_id, ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS), ctx->event_id - ctx->reported_id, ctx->buffer_size, IPECAMERA_RESERVE_BUFFERS);
		    ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
		} else ++ctx->reported_id;

		memcpy(&info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));

		if ((ctx->event_id - ctx->reported_id) < ctx->buffer_size) {
		    res = callback(ctx->reported_id, (pcilib_event_info_t*)&info, user);
		    if (res <= 0) {
			if (res < 0) err = -res;
			run_flag = 0;
			break;
		    }
		}
	    }
	    usleep(IPECAMERA_NOFRAME_SLEEP);
	}
    } else {
	while ((run_flag)&&(ctx->run_streamer)) {
	    usleep(IPECAMERA_NOFRAME_SLEEP);
	}
    }

    ctx->streaming = 0;

    UNLOCK(stream);

    ipecamera_debug(API, "ipecamera: streaming finished");

    if (do_stop) {
	ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
    }

    return err;
}

int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
    int err;
    struct timeval tv;
    ipecamera_t *ctx = (ipecamera_t*)vctx;

    if (!ctx) {
	pcilib_error("IPECamera imaging is not initialized");
	return PCILIB_ERROR_NOTINITIALIZED;
    }

    if (!ctx->started) {
	pcilib_error("IPECamera is not in grabbing mode");
	return PCILIB_ERROR_INVALID_REQUEST;
    }
    
    if (!ctx->parse_data) {
	pcilib_error("RAWData only mode is requested");
	return PCILIB_ERROR_INVALID_REQUEST;
    }

    ipecamera_debug(API, "ipecamera: next_event");

    LOCK(stream);

#ifdef IPECAMERA_ANNOUNCE_READY
    if (((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id))) {
#else /* IPECAMERA_ANNOUNCE_READY */
    if (ctx->reported_id == ctx->event_id) {
#endif /* IPECAMERA_ANNOUNCE_READY */

	if (timeout) {
	    if (timeout == PCILIB_TIMEOUT_INFINITE) {
#ifdef IPECAMERA_ANNOUNCE_READY
		while ((ctx->started)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
#else /* IPECAMERA_ANNOUNCE_READY */
		while ((ctx->started)&&(ctx->reported_id == ctx->event_id)) {
#endif /* IPECAMERA_ANNOUNCE_READY */
		usleep(IPECAMERA_NOFRAME_SLEEP);
		}
	    } else {
		pcilib_calc_deadline(&tv, timeout);

#ifdef IPECAMERA_ANNOUNCE_READY
		while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id)))) {
#else /* IPECAMERA_ANNOUNCE_READY */
		while ((ctx->started)&&(pcilib_calc_time_to_deadline(&tv) > 0)&&(ctx->reported_id == ctx->event_id)) {
#endif /* IPECAMERA_ANNOUNCE_READY */
		    usleep(IPECAMERA_NOFRAME_SLEEP);
		}
	    }
	}
	
	if (ctx->reported_id == ctx->event_id) {
	    UNLOCK(stream);
	    ipecamera_debug(API, "ipecamera: next_event timed out");
	    return PCILIB_ERROR_TIMEOUT;
	}
	
    }

retry:
    if ((ctx->event_id - ctx->reported_id) > (ctx->buffer_size - IPECAMERA_RESERVE_BUFFERS)) {
	ipecamera_debug(HARDWARE, "Skipping events %zu to %zu as preprocessing is too slow. We are currently %zu buffers beyond, but only %zu buffers are available and safety limit is %zu",
	    ctx->reported_id, ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS), ctx->event_id - ctx->reported_id, ctx->buffer_size, IPECAMERA_RESERVE_BUFFERS);
	ctx->reported_id = ctx->event_id - (ctx->buffer_size - 1 - IPECAMERA_RESERVE_BUFFERS);
    } else ++ctx->reported_id;

    if (evid) *evid = ctx->reported_id;

    if (info) {
	if (info_size >= sizeof(ipecamera_event_info_t))
	    memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(ipecamera_event_info_t));
	else if (info_size >= sizeof(pcilib_event_info_t))
	    memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
	else {
	    UNLOCK(stream);
	    ipecamera_debug(API, "ipecamera: next_event returned a error");
	    return PCILIB_ERROR_INVALID_ARGUMENT;
	}
    }

    if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;

    UNLOCK(stream);

    ipecamera_debug(API, "ipecamera: next_event returned");

    return 0;
}