add debug printout
[babeltrace.git] / plugins / ctf / common / notif-iter / notif-iter.h
CommitLineData
e98a2d6e
PP
1#ifndef CTF_NOTIF_ITER_H
2#define CTF_NOTIF_ITER_H
3
4/*
5 * Babeltrace - CTF notification iterator
6 * ¯¯¯¯¯ ¯¯¯¯
7 * Copyright (c) 2015-2016 EfficiOS Inc. and Linux Foundation
8 * Copyright (c) 2015-2016 Philippe Proulx <pproulx@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <stdint.h>
30#include <stdio.h>
31#include <stddef.h>
32#include <babeltrace/ctf-ir/trace.h>
33#include <babeltrace/ctf-ir/fields.h>
34#include <babeltrace/ctf-ir/event.h>
35#include <babeltrace/babeltrace-internal.h>
36
37/**
38 * @file ctf-notif-iter.h
39 *
40 * CTF notification iterator
41 * ¯¯¯¯¯ ¯¯¯¯
42 * This is a common internal API used by CTF source plugins. It allows
43 * one to get notifications from a user-provided medium.
44 */
45
46/**
47 * Medium operations status codes.
48 */
49enum bt_ctf_notif_iter_medium_status {
50 /**
51 * End of file.
52 *
53 * The medium function called by the notification iterator
54 * function reached the end of the file.
55 */
56 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF = -4,
57
58 /**
59 * There is no data available right now, try again later.
60 */
61 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN = -3,
62
63 /** Invalid argument. */
64 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL = -2,
65
66 /** General error. */
67 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR = -1,
68
69 /** Everything okay. */
70 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK = 0,
71};
72
73/**
74 * CTF notification iterator API status code.
75 */
76enum bt_ctf_notif_iter_status {
77 /**
78 * End of file.
79 *
80 * The medium function called by the notification iterator
81 * function reached the end of the file.
82 */
83 BT_CTF_NOTIF_ITER_STATUS_EOF = -4,
84
85 /**
86 * There is no data available right now, try again later.
87 *
88 * Some condition resulted in the
89 * bt_ctf_notif_iter_medium_ops::request_bytes() user function not
90 * having access to any data now. You should retry calling the
91 * last called notification iterator function once the situation
92 * is resolved.
93 */
94 BT_CTF_NOTIF_ITER_STATUS_AGAIN = -3,
95
96 /** Invalid argument. */
97 BT_CTF_NOTIF_ITER_STATUS_INVAL = -2,
98
99 /** General error. */
100 BT_CTF_NOTIF_ITER_STATUS_ERROR = -1,
101
102 /** Everything okay. */
103 BT_CTF_NOTIF_ITER_STATUS_OK = 0,
104};
105
106/**
107 * Medium operations.
108 *
109 * Those user functions are called by the notification iterator
110 * functions to request medium actions.
111 */
112struct bt_ctf_notif_iter_medium_ops {
113 /**
114 * Returns the next byte buffer to be used by the binary file
115 * reader to deserialize binary data.
116 *
117 * This function \em must be defined.
118 *
119 * The purpose of this function is to return a buffer of bytes
120 * to the notification iterator, of a maximum of \p request_sz
121 * bytes. If this function cannot return a buffer of at least
122 * \p request_sz bytes, it may return a smaller buffer. In
123 * either cases, \p buffer_sz must be set to the returned buffer
124 * size (in bytes).
125 *
126 * The returned buffer's ownership remains the medium, in that
127 * it won't be freed by the notification iterator functions. The
128 * returned buffer won't be modified by the notification
129 * iterator functions either.
130 *
131 * When this function is called for the first time for a given
132 * file, the offset within the file is considered to be 0. The
133 * next times this function is called, the returned buffer's
134 * byte offset within the complete file must be the previous
135 * offset plus the last returned value of \p buffer_sz by this
136 * medium.
137 *
138 * This function must return one of the following statuses:
139 *
140 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK</b>: Everything
141 * is okay, i.e. \p buffer_sz is set to a positive value
142 * reflecting the number of available bytes in the buffer
143 * starting at the address written in \p buffer_addr.
144 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN</b>: No data is
145 * available right now. In this case, the notification
146 * iterator function called by the user returns
147 * #BT_CTF_NOTIF_ITER_STATUS_AGAIN, and it is the user's
148 * responsibility to make sure enough data becomes available
149 * before calling the \em same notification iterator
150 * function again to continue the decoding process.
151 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF</b>: The end of
152 * the file was reached, and no more data will ever be
153 * available for this file. In this case, the notification
154 * iterator function called by the user returns
155 * #BT_CTF_NOTIF_ITER_STATUS_EOF. This must \em not be
156 * returned when returning at least one byte of data to the
157 * caller, i.e. this must be returned when there's
158 * absolutely nothing left; should the request size be
159 * larger than what's left in the file, this function must
160 * return what's left, setting \p buffer_sz to the number of
161 * remaining bytes, and return
162 * #BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF on the \em following
163 * call.
164 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR</b>: A fatal
165 * error occured during this operation. In this case, the
166 * notification iterator function called by the user returns
167 * #BT_CTF_NOTIF_ITER_STATUS_ERROR.
168 *
169 * If #BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK is not returned, the
170 * values of \p buffer_sz and \p buffer_addr are \em ignored by
171 * the caller.
172 *
173 * @param request_sz Requested buffer size (bytes)
174 * @param buffer_addr Returned buffer address
175 * @param buffer_sz Returned buffer's size (bytes)
176 * @param data User data
177 * @returns Status code (see description above)
178 */
179 enum bt_ctf_notif_iter_medium_status (* request_bytes)(
180 size_t request_sz, uint8_t **buffer_addr,
181 size_t *buffer_sz, void *data);
182
183 /**
184 * Returns a stream instance (weak reference) for the given
185 * stream class.
186 *
187 * This is called after a packet header is read, and the
188 * corresponding stream class is found by the notification
189 * iterator.
190 *
191 * @param stream_class Stream class associated to the stream
192 * @param data User data
193 * @returns Stream instance (weak reference) or
194 * \c NULL on error
195 */
196 struct bt_ctf_stream * (* get_stream)(
197 struct bt_ctf_stream_class *stream_class, void *data);
198};
199
200/** CTF notification iterator. */
201struct bt_ctf_notif_iter;
202
203// TODO: Replace by the real thing
204enum bt_ctf_notif_iter_notif_type {
205 BT_CTF_NOTIF_ITER_NOTIF_NEW_PACKET,
206 BT_CTF_NOTIF_ITER_NOTIF_END_OF_PACKET,
207 BT_CTF_NOTIF_ITER_NOTIF_EVENT,
208};
209
210struct bt_ctf_notif_iter_notif {
211 enum bt_ctf_notif_iter_notif_type type;
212};
213
214struct bt_ctf_notif_iter_notif_new_packet {
215 struct bt_ctf_notif_iter_notif base;
216 struct bt_ctf_packet *packet;
217};
218
219struct bt_ctf_notif_iter_notif_end_of_packet {
220 struct bt_ctf_notif_iter_notif base;
221 struct bt_ctf_packet *packet;
222};
223
224struct bt_ctf_notif_iter_notif_event {
225 struct bt_ctf_notif_iter_notif base;
226 struct bt_ctf_event *event;
227};
228
229void bt_ctf_notif_iter_notif_destroy(void *notif);
230
231/**
232 * Creates a CTF notification iterator.
233 *
234 * Upon successful completion, the reference count of \p trace is
235 * incremented.
236 *
237 * @param trace Trace to read
238 * @param max_request_sz Maximum buffer size, in bytes, to
239 * request to
240 * bt_ctf_notif_iter_medium_ops::request_bytes()
241 * at a time
242 * @param medops Medium operations
243 * @param medops_data User data (passed to medium operations)
244 * @param err_stream Error stream (can be \c NULL to disable)
245 * @returns New CTF notification iterator on
246 * success, or \c NULL on error
247 */
248struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
249 size_t max_request_sz, struct bt_ctf_notif_iter_medium_ops medops,
250 void *medops_data, FILE *err_stream);
251
252/**
253 * Destroys a CTF notification iterator, freeing all internal resources.
254 *
255 * The registered trace's reference count is decremented.
256 *
257 * @param notif_iter CTF notification iterator
258 */
259void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notif_iter);
260
261/**
262 * Resets the internal state of a CTF notification iterator.
263 *
264 * This function can be used when it is desired to seek to the beginning
265 * of another packet. It is expected that the next call to
266 * bt_ctf_notif_iter_medium_ops::request_bytes() made by this
267 * notification iterator will return the \em first bytes of a \em
268 * packet.
269 *
270 * @param notif_iter CTF notification iterator
271 */
272void bt_ctf_notif_iter_reset(struct bt_ctf_notif_iter *notif_iter);
273
274/**
275 * Returns the next notification from a CTF notification iterator.
276 *
277 * Upon successful completion, #BT_CTF_NOTIF_ITER_STATUS_OK is
278 * returned, and the next notification is written to \p notif.
279 * In this case, the caller is responsible for calling
280 * bt_notification_put() on the returned notification.
281 *
282 * If this function returns #BT_CTF_NOTIF_ITER_STATUS_AGAIN, the caller
283 * should make sure that data becomes available to its medium, and
284 * call this function again, until another status is returned.
285 *
286 * @param notif_iter CTF notification iterator
287 * @param notification Returned notification if the function's
288 * return value is #BT_CTF_NOTIF_ITER_STATUS_OK
289 * @returns One of #bt_ctf_notif_iter_status values
290 */
291enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
292 struct bt_ctf_notif_iter *notif_iter,
293 struct bt_ctf_notif_iter_notif **notification);
294
295#endif /* CTF_NOTIF_ITER_H */
This page took 0.032807 seconds and 4 git commands to generate.