summaryrefslogtreecommitdiffstats
path: root/carl9170fw/tools/include/frame.h
blob: f753fe3171a51d0b6fdbeb5d9e9a6ba79088eb19 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Most ideas and some code are copied from the linux' kernels
 *	include/linux/skbuff.h
 */

#ifndef __TOOLS_FRAME_H
#define __TOOLS_FRAME_H

#include "SDL.h"
#include "list.h"

/**
 * struct frame_queue - sk_buff_head like frame queue
 *
 * @list: pointer to head and tail
 * @lock: mutex lock for serialize access
 * @len: exact number of queued frames
 */

struct frame_queue {
	struct list_head list;
	SDL_mutex *lock;
	size_t len;
};

/**
 * struct frame - frame data structure (like sk_buff)
 *
 * @list: storage for double-linked &struct frame_queue list
 * @dev: pointer to private device/driver structure
 * @timestamp: space for the current timestamp
 * @cb: private driver data
 * @dcb: more reserved space for low-level / backend driver
 * @queue: selected frame queue / priority
 * @ref: reference counter
 * @alloced: maximum available space
 * @total_len: currently consumed and reserved memory
 * @len: current frame length
 * @head: points to the buffer head
 * @data: current frame data pointer
 * @tail: frame data tail pointer
 * @payload: frame data storage container
 */

struct frame {
	struct list_head list;
	void *dev;
	unsigned long timestamp;
	uint8_t cb[64];
	union {
		struct list_head list;
		uint8_t raw_data[32];
	} dcb;

	unsigned int queue;
	unsigned int ref;
	unsigned int alloced;
	unsigned int total_len;
	unsigned int len;
	uint8_t *head;
	uint8_t *data;
	uint8_t *tail;

	/* payload must be the last entry */
	uint8_t payload[0];
};

/**
 * frame_put - append more data to &struct frame
 *
 * Allocate @size bytes from &struct frame tail and
 * returns a pointer to the requested location.
 *
 * @frame: frame to alter
 * @size: extra size
 */
static inline void *frame_put(struct frame *frame, unsigned int size)
{
	void *tmp;

	BUG_ON(frame->total_len + size > frame->alloced);

	frame->len += size;
	frame->total_len += size;

	tmp = (void *) frame->tail;
	frame->tail += size;

	BUG_ON(frame->tail > (frame->payload + frame->alloced));

	return tmp;
}

/**
 * frame_push - allocate head
 *
 * returns a pointer to a newly allocate area at &struct frame head.
 *
 * @frame: frame to modify
 * @size: requested extra size
 */
static inline void *frame_push(struct frame *frame, unsigned int size)
{
	frame->len += size;
	frame->data -= size;

	BUG_ON(frame->data < frame->payload);
	return frame->data;
}

/**
 * frame_get - reference frame buffer
 *
 * grab a reference from the frame buffer, in order to
 * prevent it from being freed prematurely by a different user.
 *
 * @frame: frame pointer
 */
static inline struct frame *frame_get(struct frame *frame)
{
	frame->ref++;
	return frame;
}

/**
 * frame_pull - remove space from &struct frame head
 *
 * Does the opposite of frame_push() and removes freed-up
 * space at the frames's head.
 *
 * @frame: pointer to frame structure
 * @size: bytes to remove from head
 */
static inline void *frame_pull(struct frame *frame, unsigned int size)
{
	BUG_ON(frame->len < size);

	frame->len -= size;
	frame->total_len -= size;
	frame->data += size;

	return frame->data;
}

/**
 * frame_reserve - reserve frame headroom
 *
 * Reserve a certain amount of space to allow headroom manipulations
 * in the future.
 *
 * @frame: frame to adjust
 * @size: bytes to reserve
 */
static inline void frame_reserve(struct frame *frame, unsigned int size)
{
	BUG_ON(frame->total_len + size > frame->alloced);
	BUG_ON(frame->len != 0);

	frame->total_len += size;
	frame->data += size;
	frame->tail += size;
}

/**
 * frame_trim - set frame length
 *
 * cut the frame to @size length.
 *
 * @frame: frame to be trimmed
 * @size: new length
 */
static inline void frame_trim(struct frame *frame, unsigned int size)
{
	BUG_ON(size > frame->total_len);

	frame->len = size;
	frame->total_len = size;
	frame->data = frame->head;
	frame->tail = frame->head + size;
}

/**
 * frame_alloc - alloc and initialize new frame
 *
 * returns a newly created &struct frame object.
 *
 * @size: maximum frame size of the new frame
 */
static inline struct frame *frame_alloc(unsigned int size)
{
	struct frame *tmp;

	tmp = malloc(size + sizeof(*tmp));
	if (tmp != NULL) {
		memset(tmp, 0, sizeof(*tmp));
		init_list_head(&tmp->list);
		init_list_head(&tmp->dcb.list);
		tmp->len = 0;
		tmp->total_len = 0;
		tmp->alloced = size;

		tmp->head = tmp->payload;
		tmp->data = tmp->payload;
		tmp->tail = tmp->payload;
		tmp->ref = 1;
	}
	return tmp;
}

/**
 * frame_free - unref and free frame
 *
 * Unreference frame and free it up, if all users are gone.
 *
 * @frame: frame to be freed
 */
static inline void frame_free(struct frame *frame)
{
	if (!--frame->ref)
		free(frame);
}

/**
 * FRAME_WALK - MACRO walker
 *
 * Walks over all queued elements in &struct frame_queue
 *
 * NOTE: This function is vulnerable in concurrent access
 *	 scenarios without proper locking.
 *
 * @pos: current position inside the queue
 * @head: &struct frame_queue head
 */
#define FRAME_WALK(pos, head)					\
	list_for_each_entry((pos), &(head)->list, list)

static inline void __frame_queue_init(struct frame_queue *queue)
{
	queue->len = 0;
	init_list_head(&queue->list);
}

/**
 * frame_queue_init - initialize frame_queue
 *
 * Initialize the given &struct frame_queue object.
 *
 * @queue: frame_queue to be initialized
 */
static inline void frame_queue_init(struct frame_queue *queue)
{
	queue->lock = SDL_CreateMutex();
	__frame_queue_init(queue);
}

/**
 * frame_queue_len - returns number of queue elements
 *
 * @queue: frame_queue object
 */
static inline unsigned int frame_queue_len(struct frame_queue *queue)
{
	return queue->len;
}

/**
 * frame_queue_empty - returns %TRUE whenever queue is empty
 *
 * @queue: frame_queue object
 */
static inline bool frame_queue_empty(struct frame_queue *queue)
{
	return list_empty(&queue->list);
}

static inline void __frame_queue_head(struct frame_queue *queue, struct frame *frame)
{
	list_add_head(&frame->list, &queue->list);
	queue->len++;
}

/**
 * frame_queue_head - queue a frame at the queues head
 * @queue: queue to use
 */
static inline void frame_queue_head(struct frame_queue *queue, struct frame *frame)
{
	BUG_ON((SDL_mutexP(queue->lock) != 0));
	__frame_queue_head(queue, frame);
	SDL_mutexV(queue->lock);
}

static inline void __frame_queue_tail(struct frame_queue *queue, struct frame *frame)
{
	list_add_tail(&frame->list, &queue->list);
	queue->len++;
}

/**
 * frame_queue_head - queue a frame at the queues tail
 * @queue: queue to use
 */
static inline void frame_queue_tail(struct frame_queue *queue, struct frame *frame)
{
	BUG_ON((SDL_mutexP(queue->lock) != 0));
	__frame_queue_tail(queue, frame);
	SDL_mutexV(queue->lock);
}

static inline void __frame_unlink(struct frame_queue *queue, struct frame *frame)
{
	list_del(&frame->list);
	queue->len--;
}

/**
 * frame_queue_unlink - remove a frame from the queue
 * @queue: queue to use
 * @frame: frame to remove
 */
static inline void frame_unlink(struct frame_queue *queue, struct frame *frame)
{
	BUG_ON((SDL_mutexP(queue->lock) != 0));
	__frame_unlink(queue, frame);
	SDL_mutexV(queue->lock);
}


static inline struct frame *__frame_dequeue(struct frame_queue *queue)
{
	struct frame *tmp = NULL;

	if (!frame_queue_empty(queue)) {
		tmp = list_entry(queue->list.next, struct frame, list);
		__frame_unlink(queue, tmp);
	}

	return tmp;
}

/**
 * frame_dequeue - remove frame from the head of the queue
 *
 * @queue: queue to dequeue from
 */
static inline struct frame *frame_dequeue(struct frame_queue *queue)
{
	struct frame *tmp;

	BUG_ON((SDL_mutexP(queue->lock) != 0));

	tmp = __frame_dequeue(queue);
	SDL_mutexV(queue->lock);
	return tmp;
}

static inline void __frame_queue_purge(struct frame_queue *queue)
{
	while (list_empty(&queue->list) == false)
		frame_free(__frame_dequeue(queue));
}

/**
 * frame_queue_purge - frees all queued &struct frame objects
 *
 * @queue: queue to be freed
 */
static inline void frame_queue_purge(struct frame_queue *queue)
{
	BUG_ON((SDL_mutexP(queue->lock) != 0));
	__frame_queue_purge(queue);
	SDL_mutexV(queue->lock);
}

/**
 * frame_queue_kill - destroys frame_queue object
 *
 * Destroy object and frees up all remaining elements
 *
 * @queue: frame_queue victim
 */
static inline void frame_queue_kill(struct frame_queue *queue)
{
	SDL_DestroyMutex(queue->lock);
	__frame_queue_purge(queue);
}

#endif /* __TOOLS_FRAME_H */