Rename bt_ctf_X -> bt_X, maintain backward compat. for pre-2.0 CTF writer
[babeltrace.git] / lib / graph / notification / packet.c
1 /*
2 * Babeltrace Plug-in Packet-related Notifications
3 *
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Author: Jérémie Galarneau <jeremie.galarneau@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 #define BT_LOG_TAG "NOTIF-PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ctf-ir/packet.h>
32 #include <babeltrace/ctf-ir/packet-internal.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/stream.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/graph/notification-packet-internal.h>
37 #include <inttypes.h>
38
39 static
40 void bt_notification_packet_begin_destroy(struct bt_object *obj)
41 {
42 struct bt_notification_packet_begin *notification =
43 (struct bt_notification_packet_begin *) obj;
44
45 BT_LOGD("Destroying packet beginning notification: addr=%p",
46 notification);
47 BT_LOGD_STR("Putting packet.");
48 BT_PUT(notification->packet);
49 g_free(notification);
50 }
51
52 static
53 void bt_notification_packet_end_destroy(struct bt_object *obj)
54 {
55 struct bt_notification_packet_end *notification =
56 (struct bt_notification_packet_end *) obj;
57
58 BT_LOGD("Destroying packet end notification: addr=%p",
59 notification);
60 BT_LOGD_STR("Putting packet.");
61 BT_PUT(notification->packet);
62 g_free(notification);
63 }
64
65 struct bt_notification *bt_notification_packet_begin_create(
66 struct bt_packet *packet)
67 {
68 struct bt_notification_packet_begin *notification;
69 struct bt_stream *stream;
70 struct bt_stream_class *stream_class;
71
72 if (!packet) {
73 BT_LOGW_STR("Invalid parameter: packet is NULL.");
74 goto error;
75 }
76
77 stream = bt_packet_borrow_stream(packet);
78 assert(stream);
79 stream_class = bt_stream_borrow_stream_class(stream);
80 assert(stream_class);
81 BT_LOGD("Creating packet beginning notification object: "
82 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
83 "stream-class-addr=%p, stream-class-name=\"%s\", "
84 "stream-class-id=%" PRId64,
85 packet, stream, bt_stream_get_name(stream),
86 stream_class,
87 bt_stream_class_get_name(stream_class),
88 bt_stream_class_get_id(stream_class));
89 notification = g_new0(struct bt_notification_packet_begin, 1);
90 if (!notification) {
91 BT_LOGE_STR("Failed to allocate one packet beginning notification.");
92 goto error;
93 }
94
95 bt_notification_init(&notification->parent,
96 BT_NOTIFICATION_TYPE_PACKET_BEGIN,
97 bt_notification_packet_begin_destroy);
98 notification->packet = bt_get(packet);
99 BT_LOGD("Created packet beginning notification object: "
100 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
101 "stream-class-addr=%p, stream-class-name=\"%s\", "
102 "stream-class-id=%" PRId64 ", addr=%p",
103 packet, stream, bt_stream_get_name(stream),
104 stream_class,
105 bt_stream_class_get_name(stream_class),
106 bt_stream_class_get_id(stream_class), notification);
107 return &notification->parent;
108 error:
109 return NULL;
110 }
111
112 struct bt_packet *bt_notification_packet_begin_get_packet(
113 struct bt_notification *notification)
114 {
115 struct bt_packet *ret = NULL;
116 struct bt_notification_packet_begin *packet_begin;
117
118 if (!notification) {
119 BT_LOGW_STR("Invalid parameter: notification is NULL.");
120 goto end;
121 }
122
123 if (notification->type != BT_NOTIFICATION_TYPE_PACKET_BEGIN) {
124 BT_LOGW("Invalid parameter: notification is not a packet beginning notification: "
125 "addr%p, notif-type=%s",
126 notification, bt_notification_type_string(
127 bt_notification_get_type(notification)));
128 goto end;
129 }
130
131 packet_begin = container_of(notification,
132 struct bt_notification_packet_begin, parent);
133 ret = bt_get(packet_begin->packet);
134 end:
135 return ret;
136 }
137
138 struct bt_notification *bt_notification_packet_end_create(
139 struct bt_packet *packet)
140 {
141 struct bt_notification_packet_end *notification;
142 struct bt_stream *stream;
143 struct bt_stream_class *stream_class;
144
145 if (!packet) {
146 BT_LOGW_STR("Invalid parameter: packet is NULL.");
147 goto error;
148 }
149
150 stream = bt_packet_borrow_stream(packet);
151 assert(stream);
152 stream_class = bt_stream_borrow_stream_class(stream);
153 assert(stream_class);
154 BT_LOGD("Creating packet end notification object: "
155 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
156 "stream-class-addr=%p, stream-class-name=\"%s\", "
157 "stream-class-id=%" PRId64,
158 packet, stream, bt_stream_get_name(stream),
159 stream_class,
160 bt_stream_class_get_name(stream_class),
161 bt_stream_class_get_id(stream_class));
162 notification = g_new0(struct bt_notification_packet_end, 1);
163 if (!notification) {
164 BT_LOGE_STR("Failed to allocate one packet end notification.");
165 goto error;
166 }
167
168 bt_notification_init(&notification->parent,
169 BT_NOTIFICATION_TYPE_PACKET_END,
170 bt_notification_packet_end_destroy);
171 notification->packet = bt_get(packet);
172 BT_LOGD("Created packet end notification object: "
173 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
174 "stream-class-addr=%p, stream-class-name=\"%s\", "
175 "stream-class-id=%" PRId64 ", addr=%p",
176 packet, stream, bt_stream_get_name(stream),
177 stream_class,
178 bt_stream_class_get_name(stream_class),
179 bt_stream_class_get_id(stream_class), notification);
180 return &notification->parent;
181 error:
182 return NULL;
183 }
184
185 struct bt_packet *bt_notification_packet_end_get_packet(
186 struct bt_notification *notification)
187 {
188 struct bt_packet *ret = NULL;
189 struct bt_notification_packet_end *packet_end;
190
191 if (!notification) {
192 BT_LOGW_STR("Invalid parameter: notification is NULL.");
193 goto end;
194 }
195
196 if (notification->type != BT_NOTIFICATION_TYPE_PACKET_END) {
197 BT_LOGW("Invalid parameter: notification is not a packet end notification: "
198 "addr%p, notif-type=%s",
199 notification, bt_notification_type_string(
200 bt_notification_get_type(notification)));
201 goto end;
202 }
203
204 packet_end = container_of(notification,
205 struct bt_notification_packet_end, parent);
206 ret = bt_get(packet_end->packet);
207 end:
208 return ret;
209 }
This page took 0.032872 seconds and 4 git commands to generate.