1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 Nicolas Pennequin
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include <sys/types.h>
42 EVENT_BUFFER_LOW
= (EVENT_CLASS_BUFFERING
|1),
43 EVENT_HANDLE_REBUFFER
,
46 EVENT_HANDLE_FINISHED
,
49 /* Error return values */
50 #define ERR_HANDLE_NOT_FOUND -1
51 #define ERR_BUFFER_FULL -2
52 #define ERR_INVALID_VALUE -3
53 #define ERR_FILE_ERROR -4
54 #define ERR_HANDLE_NOT_DONE -5
57 /* Initialise the buffering subsystem */
58 void buffering_init(void);
60 /* Reset the buffering system */
61 bool buffering_reset(char *buf
, size_t buflen
);
64 /***************************************************************************
65 * MAIN BUFFERING API CALLS
66 * ========================
68 * bufopen : Reserve space in the buffer for a given file
69 * bufalloc : Open a new handle from data that needs to be copied from memory
70 * bufclose : Close an open handle
71 * bufseek : Set handle reading index, relatively to the start of the file
72 * bufadvance: Move handle reading index, relatively to current position
73 * bufread : Copy data from a handle to a buffer
74 * bufgetdata: Obtain a pointer for linear access to a "size" amount of data
75 * bufgettail: Out-of-band get the last size bytes of a handle.
76 * bufcuttail: Out-of-band remove the trailing 'size' bytes of a handle.
78 * NOTE: bufread and bufgetdata will block the caller until the requested
79 * amount of data is ready (unless EOF is reached).
80 * NOTE: Tail operations are only legal when the end of the file is buffered.
81 ****************************************************************************/
83 #define BUF_MAX_HANDLES 256
85 int bufopen(const char *file
, size_t offset
, enum data_type type
);
86 int bufalloc(const void *src
, size_t size
, enum data_type type
);
87 bool bufclose(int handle_id
);
88 int bufseek(int handle_id
, size_t newpos
);
89 int bufadvance(int handle_id
, off_t offset
);
90 ssize_t
bufread(int handle_id
, size_t size
, void *dest
);
91 ssize_t
bufgetdata(int handle_id
, size_t size
, void **data
);
92 ssize_t
bufgettail(int handle_id
, size_t size
, void **data
);
93 ssize_t
bufcuttail(int handle_id
, size_t size
);
96 /***************************************************************************
100 * buf_get_offset: Get a handle offset from a pointer
101 * buf_handle_offset: Get the offset of the first buffered byte from the file
102 * buf_request_buffer_handle: Request buffering of a handle
103 * buf_set_base_handle: Tell the buffering thread which handle is currently read
104 * buf_used: Total amount of buffer space used (including allocated space)
105 ****************************************************************************/
107 ssize_t
buf_get_offset(int handle_id
, void *ptr
);
108 ssize_t
buf_handle_offset(int handle_id
);
109 void buf_request_buffer_handle(int handle_id
);
110 void buf_set_base_handle(int handle_id
);
111 size_t buf_used(void);
117 BUFFERING_SET_WATERMARK
= 1,
118 BUFFERING_SET_CHUNKSIZE
,
120 void buf_set_watermark(size_t bytes
);
124 struct buffering_debug
{
126 size_t buffered_data
;
131 void buffering_get_debugdata(struct buffering_debug
*dbgdata
);