src.ctf.fs: move internal util to ctf/common/utils
[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>
599faa1c 35#include <babeltrace/graph/clock-class-priority-map.h>
e98a2d6e
PP
36#include <babeltrace/babeltrace-internal.h>
37
38/**
39 * @file ctf-notif-iter.h
40 *
41 * CTF notification iterator
42 * ¯¯¯¯¯ ¯¯¯¯
43 * This is a common internal API used by CTF source plugins. It allows
44 * one to get notifications from a user-provided medium.
45 */
46
47/**
48 * Medium operations status codes.
49 */
50enum bt_ctf_notif_iter_medium_status {
51 /**
52 * End of file.
53 *
54 * The medium function called by the notification iterator
55 * function reached the end of the file.
56 */
fdf0e7a0 57 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF = 1,
e98a2d6e
PP
58
59 /**
60 * There is no data available right now, try again later.
61 */
fdf0e7a0 62 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN = 11,
e98a2d6e
PP
63
64 /** Invalid argument. */
65 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL = -2,
66
67 /** General error. */
68 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR = -1,
69
70 /** Everything okay. */
dc77b521 71 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK = 0,
e98a2d6e
PP
72};
73
74/**
75 * CTF notification iterator API status code.
76 */
77enum bt_ctf_notif_iter_status {
78 /**
79 * End of file.
80 *
81 * The medium function called by the notification iterator
82 * function reached the end of the file.
83 */
dc77b521 84 BT_CTF_NOTIF_ITER_STATUS_EOF = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF,
e98a2d6e
PP
85
86 /**
87 * There is no data available right now, try again later.
88 *
89 * Some condition resulted in the
90 * bt_ctf_notif_iter_medium_ops::request_bytes() user function not
91 * having access to any data now. You should retry calling the
92 * last called notification iterator function once the situation
93 * is resolved.
94 */
dc77b521 95 BT_CTF_NOTIF_ITER_STATUS_AGAIN = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN,
e98a2d6e
PP
96
97 /** Invalid argument. */
dc77b521 98 BT_CTF_NOTIF_ITER_STATUS_INVAL = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL,
e98a2d6e
PP
99
100 /** General error. */
dc77b521 101 BT_CTF_NOTIF_ITER_STATUS_ERROR = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR,
e98a2d6e
PP
102
103 /** Everything okay. */
104 BT_CTF_NOTIF_ITER_STATUS_OK = 0,
105};
106
107/**
108 * Medium operations.
109 *
110 * Those user functions are called by the notification iterator
111 * functions to request medium actions.
112 */
113struct bt_ctf_notif_iter_medium_ops {
114 /**
115 * Returns the next byte buffer to be used by the binary file
116 * reader to deserialize binary data.
117 *
118 * This function \em must be defined.
119 *
120 * The purpose of this function is to return a buffer of bytes
121 * to the notification iterator, of a maximum of \p request_sz
122 * bytes. If this function cannot return a buffer of at least
123 * \p request_sz bytes, it may return a smaller buffer. In
124 * either cases, \p buffer_sz must be set to the returned buffer
125 * size (in bytes).
126 *
127 * The returned buffer's ownership remains the medium, in that
128 * it won't be freed by the notification iterator functions. The
129 * returned buffer won't be modified by the notification
130 * iterator functions either.
131 *
132 * When this function is called for the first time for a given
133 * file, the offset within the file is considered to be 0. The
134 * next times this function is called, the returned buffer's
135 * byte offset within the complete file must be the previous
136 * offset plus the last returned value of \p buffer_sz by this
137 * medium.
138 *
139 * This function must return one of the following statuses:
140 *
141 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK</b>: Everything
142 * is okay, i.e. \p buffer_sz is set to a positive value
143 * reflecting the number of available bytes in the buffer
144 * starting at the address written in \p buffer_addr.
145 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN</b>: No data is
146 * available right now. In this case, the notification
147 * iterator function called by the user returns
148 * #BT_CTF_NOTIF_ITER_STATUS_AGAIN, and it is the user's
149 * responsibility to make sure enough data becomes available
150 * before calling the \em same notification iterator
151 * function again to continue the decoding process.
152 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF</b>: The end of
153 * the file was reached, and no more data will ever be
154 * available for this file. In this case, the notification
155 * iterator function called by the user returns
156 * #BT_CTF_NOTIF_ITER_STATUS_EOF. This must \em not be
157 * returned when returning at least one byte of data to the
158 * caller, i.e. this must be returned when there's
159 * absolutely nothing left; should the request size be
160 * larger than what's left in the file, this function must
161 * return what's left, setting \p buffer_sz to the number of
162 * remaining bytes, and return
163 * #BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF on the \em following
164 * call.
165 * - <b>#BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR</b>: A fatal
166 * error occured during this operation. In this case, the
167 * notification iterator function called by the user returns
168 * #BT_CTF_NOTIF_ITER_STATUS_ERROR.
169 *
170 * If #BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK is not returned, the
171 * values of \p buffer_sz and \p buffer_addr are \em ignored by
172 * the caller.
173 *
174 * @param request_sz Requested buffer size (bytes)
175 * @param buffer_addr Returned buffer address
176 * @param buffer_sz Returned buffer's size (bytes)
177 * @param data User data
178 * @returns Status code (see description above)
179 */
180 enum bt_ctf_notif_iter_medium_status (* request_bytes)(
181 size_t request_sz, uint8_t **buffer_addr,
182 size_t *buffer_sz, void *data);
183
184 /**
185 * Returns a stream instance (weak reference) for the given
186 * stream class.
187 *
188 * This is called after a packet header is read, and the
189 * corresponding stream class is found by the notification
190 * iterator.
191 *
b92735af
PP
192 * @param stream_class Stream class of the stream to get
193 * @param stream_id Stream (instance) ID of the stream
194 * to get (-1ULL if not available)
e98a2d6e
PP
195 * @param data User data
196 * @returns Stream instance (weak reference) or
197 * \c NULL on error
198 */
199 struct bt_ctf_stream * (* get_stream)(
b92735af
PP
200 struct bt_ctf_stream_class *stream_class,
201 uint64_t stream_id, void *data);
e98a2d6e
PP
202};
203
204/** CTF notification iterator. */
205struct bt_ctf_notif_iter;
206
207// TODO: Replace by the real thing
208enum bt_ctf_notif_iter_notif_type {
209 BT_CTF_NOTIF_ITER_NOTIF_NEW_PACKET,
210 BT_CTF_NOTIF_ITER_NOTIF_END_OF_PACKET,
211 BT_CTF_NOTIF_ITER_NOTIF_EVENT,
212};
213
214struct bt_ctf_notif_iter_notif {
215 enum bt_ctf_notif_iter_notif_type type;
216};
217
218struct bt_ctf_notif_iter_notif_new_packet {
219 struct bt_ctf_notif_iter_notif base;
220 struct bt_ctf_packet *packet;
221};
222
223struct bt_ctf_notif_iter_notif_end_of_packet {
224 struct bt_ctf_notif_iter_notif base;
225 struct bt_ctf_packet *packet;
226};
227
228struct bt_ctf_notif_iter_notif_event {
229 struct bt_ctf_notif_iter_notif base;
230 struct bt_ctf_event *event;
231};
232
e98a2d6e
PP
233/**
234 * Creates a CTF notification iterator.
235 *
236 * Upon successful completion, the reference count of \p trace is
237 * incremented.
238 *
239 * @param trace Trace to read
240 * @param max_request_sz Maximum buffer size, in bytes, to
241 * request to
242 * bt_ctf_notif_iter_medium_ops::request_bytes()
243 * at a time
244 * @param medops Medium operations
245 * @param medops_data User data (passed to medium operations)
e98a2d6e
PP
246 * @returns New CTF notification iterator on
247 * success, or \c NULL on error
248 */
2cf1d51e 249BT_HIDDEN
e98a2d6e
PP
250struct bt_ctf_notif_iter *bt_ctf_notif_iter_create(struct bt_ctf_trace *trace,
251 size_t max_request_sz, struct bt_ctf_notif_iter_medium_ops medops,
55314f2a 252 void *medops_data);
e98a2d6e
PP
253
254/**
255 * Destroys a CTF notification iterator, freeing all internal resources.
256 *
257 * The registered trace's reference count is decremented.
258 *
259 * @param notif_iter CTF notification iterator
260 */
2cf1d51e 261BT_HIDDEN
e98a2d6e
PP
262void bt_ctf_notif_iter_destroy(struct bt_ctf_notif_iter *notif_iter);
263
e98a2d6e
PP
264/**
265 * Returns the next notification from a CTF notification iterator.
266 *
267 * Upon successful completion, #BT_CTF_NOTIF_ITER_STATUS_OK is
268 * returned, and the next notification is written to \p notif.
269 * In this case, the caller is responsible for calling
270 * bt_notification_put() on the returned notification.
271 *
272 * If this function returns #BT_CTF_NOTIF_ITER_STATUS_AGAIN, the caller
273 * should make sure that data becomes available to its medium, and
274 * call this function again, until another status is returned.
275 *
276 * @param notif_iter CTF notification iterator
0982a26d
PP
277 * @param cc_prio_map Clock class priority map to use when
278 * creating an event notification
e98a2d6e
PP
279 * @param notification Returned notification if the function's
280 * return value is #BT_CTF_NOTIF_ITER_STATUS_OK
281 * @returns One of #bt_ctf_notif_iter_status values
282 */
2cf1d51e 283BT_HIDDEN
e98a2d6e 284enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_next_notification(
78586d8a 285 struct bt_ctf_notif_iter *notit,
0982a26d 286 struct bt_clock_class_priority_map *cc_prio_map,
78586d8a 287 struct bt_notification **notification);
e98a2d6e 288
87187cbf
PP
289/**
290 * Returns the first packet header and context fields. This function
291 * never needs to call the `get_stream()` medium operation because
292 * it does not create packet or event objects.
293 *
294 * @param notif_iter CTF notification iterator
295 * @param packet_header_field Packet header field (\c NULL if there's
296 * no packet header field)
297 * @param packet_context_field Packet context field (\c NULL if there's
298 * no packet context field)
299 * @returns One of #bt_ctf_notif_iter_status values
300 */
301BT_HIDDEN
302enum bt_ctf_notif_iter_status bt_ctf_notif_iter_get_packet_header_context_fields(
303 struct bt_ctf_notif_iter *notit,
304 struct bt_ctf_field **packet_header_field,
305 struct bt_ctf_field **packet_context_field);
306
6de92955
PP
307BT_HIDDEN
308void bt_ctf_notif_iter_set_medops_data(struct bt_ctf_notif_iter *notit,
309 void *medops_data);
310
fdf0e7a0
PP
311static inline
312const char *bt_ctf_notif_iter_medium_status_string(
313 enum bt_ctf_notif_iter_medium_status status)
314{
315 switch (status) {
316 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
317 return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF";
318 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN:
319 return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN";
320 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL:
321 return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL";
322 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR:
323 return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR";
324 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
325 return "BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK";
326 default:
327 return "(unknown)";
328 }
329}
330
331static inline
332const char *bt_ctf_notif_iter_status_string(
333 enum bt_ctf_notif_iter_status status)
334{
335 switch (status) {
336 case BT_CTF_NOTIF_ITER_STATUS_EOF:
337 return "BT_CTF_NOTIF_ITER_STATUS_EOF";
338 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
339 return "BT_CTF_NOTIF_ITER_STATUS_AGAIN";
340 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
341 return "BT_CTF_NOTIF_ITER_STATUS_INVAL";
342 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
343 return "BT_CTF_NOTIF_ITER_STATUS_ERROR";
344 case BT_CTF_NOTIF_ITER_STATUS_OK:
345 return "BT_CTF_NOTIF_ITER_STATUS_OK";
346 default:
347 return "(unknown)";
348 }
349}
350
e98a2d6e 351#endif /* CTF_NOTIF_ITER_H */
This page took 0.045384 seconds and 4 git commands to generate.