ir: add bt_ctf_event_set_packet()
[babeltrace.git] / formats / ctf / ir / packet.c
1 /*
2 * packet.c
3 *
4 * Babeltrace CTF IR - Stream packet
5 *
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <babeltrace/ctf-ir/fields-internal.h>
28 #include <babeltrace/ctf-ir/packet.h>
29 #include <babeltrace/ctf-ir/packet-internal.h>
30 #include <babeltrace/ctf-ir/trace.h>
31 #include <babeltrace/ctf-ir/stream-class-internal.h>
32 #include <babeltrace/ctf-ir/stream-class.h>
33 #include <babeltrace/ctf-ir/stream.h>
34 #include <babeltrace/ctf-ir/stream-internal.h>
35 #include <babeltrace/ctf-ir/trace-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.h>
38
39 struct bt_ctf_stream *bt_ctf_packet_get_stream(struct bt_ctf_packet *packet)
40 {
41 return packet ? bt_get(packet->stream) : NULL;
42 }
43
44 struct bt_ctf_field *bt_ctf_packet_get_header(
45 struct bt_ctf_packet *packet)
46 {
47 return packet ? bt_get(packet->header) : NULL;
48 }
49
50 int bt_ctf_packet_set_header(struct bt_ctf_packet *packet,
51 struct bt_ctf_field *header)
52 {
53 int ret = 0;
54 struct bt_ctf_trace *trace = NULL;
55 struct bt_ctf_stream_class *stream_class = NULL;
56 struct bt_ctf_field_type *header_field_type = NULL;
57 struct bt_ctf_field_type *expected_header_field_type = NULL;
58
59 if (!packet || !header || packet->frozen) {
60 ret = -1;
61 goto end;
62 }
63
64 stream_class = bt_ctf_stream_get_class(packet->stream);
65 assert(stream_class);
66 trace = bt_ctf_stream_class_get_trace(stream_class);
67 assert(trace);
68 header_field_type = bt_ctf_field_get_type(header);
69 assert(header_field_type);
70 expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace);
71
72 if (bt_ctf_field_type_compare(header_field_type,
73 expected_header_field_type)) {
74 ret = -1;
75 goto end;
76 }
77
78 bt_put(packet->header);
79 packet->header = bt_get(header);
80
81 end:
82 BT_PUT(trace);
83 BT_PUT(stream_class);
84 BT_PUT(header_field_type);
85 BT_PUT(expected_header_field_type);
86
87 return ret;
88 }
89
90 struct bt_ctf_field *bt_ctf_packet_get_context(
91 struct bt_ctf_packet *packet)
92 {
93 return packet ? bt_get(packet->context) : NULL;
94 }
95
96 int bt_ctf_packet_set_context(struct bt_ctf_packet *packet,
97 struct bt_ctf_field *context)
98 {
99 int ret = 0;
100 struct bt_ctf_stream_class *stream_class = NULL;
101 struct bt_ctf_field_type *context_field_type = NULL;
102 struct bt_ctf_field_type *expected_context_field_type = NULL;
103
104 if (!packet || !context || packet->frozen) {
105 ret = -1;
106 goto end;
107 }
108
109 stream_class = bt_ctf_stream_get_class(packet->stream);
110 assert(stream_class);
111 context_field_type = bt_ctf_field_get_type(context);
112 assert(context_field_type);
113 expected_context_field_type =
114 bt_ctf_stream_class_get_packet_context_type(stream_class);
115
116 if (bt_ctf_field_type_compare(context_field_type,
117 expected_context_field_type)) {
118 ret = -1;
119 goto end;
120 }
121
122 bt_put(packet->context);
123 packet->context = bt_get(context);
124
125 end:
126 BT_PUT(stream_class);
127 BT_PUT(context_field_type);
128 BT_PUT(expected_context_field_type);
129
130 return ret;
131 }
132
133 BT_HIDDEN
134 void bt_ctf_packet_freeze(struct bt_ctf_packet *packet)
135 {
136 if (!packet) {
137 return;
138 }
139
140 packet->frozen = 1;
141 }
142
143 static
144 void bt_ctf_packet_destroy(struct bt_object *obj)
145 {
146 struct bt_ctf_packet *packet;
147
148 packet = container_of(obj, struct bt_ctf_packet, base);
149 bt_put(packet->header);
150 bt_put(packet->context);
151 bt_put(packet->stream);
152 g_free(packet);
153 }
154
155 struct bt_ctf_packet *bt_ctf_packet_create(
156 struct bt_ctf_stream *stream)
157 {
158 struct bt_ctf_packet *packet = NULL;
159 struct bt_ctf_stream_class *stream_class = NULL;
160 struct bt_ctf_trace *trace = NULL;
161
162 if (!stream || stream->pos.fd >= 0) {
163 goto end;
164 }
165
166 stream_class = bt_ctf_stream_get_class(stream);
167 assert(stream_class);
168 trace = bt_ctf_stream_class_get_trace(stream_class);
169 assert(trace);
170 packet = g_new0(struct bt_ctf_packet, 1);
171 if (!packet) {
172 goto end;
173 }
174
175 bt_object_init(packet, bt_ctf_packet_destroy);
176 packet->stream = bt_get(stream);
177 packet->header = bt_ctf_field_create(trace->packet_header_type);
178 if (!packet->header) {
179 BT_PUT(packet);
180 goto end;
181 }
182
183 packet->context = bt_ctf_field_create(
184 stream->stream_class->packet_context_type);
185 if (!packet->context) {
186 BT_PUT(packet);
187 goto end;
188 }
189
190 end:
191 BT_PUT(trace);
192 BT_PUT(stream_class);
193
194 return packet;
195 }
This page took 0.034951 seconds and 5 git commands to generate.