| 1 |
/* |
| 2 |
* packet.c |
| 3 |
* |
| 4 |
* Babeltrace CTF IR - Stream packet |
| 5 |
* |
| 6 |
* Copyright 2016 Philippe Proulx <[email protected]> |
| 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/field-types-internal.h> |
| 29 |
#include <babeltrace/ctf-ir/packet.h> |
| 30 |
#include <babeltrace/ctf-ir/packet-internal.h> |
| 31 |
#include <babeltrace/ctf-ir/trace.h> |
| 32 |
#include <babeltrace/ctf-ir/stream-class-internal.h> |
| 33 |
#include <babeltrace/ctf-ir/stream-class.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 |
BT_HIDDEN |
| 40 |
struct bt_ctf_stream *bt_ctf_packet_get_stream(struct bt_ctf_packet *packet) |
| 41 |
{ |
| 42 |
return packet ? bt_get(packet->stream) : NULL; |
| 43 |
} |
| 44 |
|
| 45 |
BT_HIDDEN |
| 46 |
struct bt_ctf_field *bt_ctf_packet_get_header( |
| 47 |
struct bt_ctf_packet *packet) |
| 48 |
{ |
| 49 |
return packet ? bt_get(packet->header) : NULL; |
| 50 |
} |
| 51 |
|
| 52 |
BT_HIDDEN |
| 53 |
int bt_ctf_packet_set_header(struct bt_ctf_packet *packet, |
| 54 |
struct bt_ctf_field *header) |
| 55 |
{ |
| 56 |
int ret = 0; |
| 57 |
struct bt_ctf_trace *trace = NULL; |
| 58 |
struct bt_ctf_stream_class *stream_class = NULL; |
| 59 |
struct bt_ctf_field_type *header_field_type = NULL; |
| 60 |
struct bt_ctf_field_type *expected_header_field_type = NULL; |
| 61 |
|
| 62 |
if (!packet || !header || packet->frozen) { |
| 63 |
ret = -1; |
| 64 |
goto end; |
| 65 |
} |
| 66 |
|
| 67 |
stream_class = bt_ctf_stream_get_class(packet->stream); |
| 68 |
assert(stream_class); |
| 69 |
trace = bt_ctf_stream_class_get_trace(stream_class); |
| 70 |
assert(trace); |
| 71 |
header_field_type = bt_ctf_field_get_type(header); |
| 72 |
assert(header_field_type); |
| 73 |
expected_header_field_type = bt_ctf_trace_get_packet_header_type(trace); |
| 74 |
|
| 75 |
if (bt_ctf_field_type_compare(header_field_type, |
| 76 |
expected_header_field_type)) { |
| 77 |
ret = -1; |
| 78 |
goto end; |
| 79 |
} |
| 80 |
|
| 81 |
bt_put(packet->header); |
| 82 |
packet->header = bt_get(header); |
| 83 |
|
| 84 |
end: |
| 85 |
BT_PUT(trace); |
| 86 |
BT_PUT(stream_class); |
| 87 |
BT_PUT(header_field_type); |
| 88 |
BT_PUT(expected_header_field_type); |
| 89 |
|
| 90 |
return ret; |
| 91 |
} |
| 92 |
|
| 93 |
BT_HIDDEN |
| 94 |
struct bt_ctf_field *bt_ctf_packet_get_context( |
| 95 |
struct bt_ctf_packet *packet) |
| 96 |
{ |
| 97 |
return packet ? bt_get(packet->context) : NULL; |
| 98 |
} |
| 99 |
|
| 100 |
BT_HIDDEN |
| 101 |
int bt_ctf_packet_set_context(struct bt_ctf_packet *packet, |
| 102 |
struct bt_ctf_field *context) |
| 103 |
{ |
| 104 |
int ret = 0; |
| 105 |
struct bt_ctf_stream_class *stream_class = NULL; |
| 106 |
struct bt_ctf_field_type *context_field_type = NULL; |
| 107 |
struct bt_ctf_field_type *expected_context_field_type = NULL; |
| 108 |
|
| 109 |
if (!packet || !context || packet->frozen) { |
| 110 |
ret = -1; |
| 111 |
goto end; |
| 112 |
} |
| 113 |
|
| 114 |
stream_class = bt_ctf_stream_get_class(packet->stream); |
| 115 |
assert(stream_class); |
| 116 |
context_field_type = bt_ctf_field_get_type(context); |
| 117 |
assert(context_field_type); |
| 118 |
expected_context_field_type = |
| 119 |
bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 120 |
|
| 121 |
if (bt_ctf_field_type_compare(context_field_type, |
| 122 |
expected_context_field_type)) { |
| 123 |
ret = -1; |
| 124 |
goto end; |
| 125 |
} |
| 126 |
|
| 127 |
bt_put(packet->context); |
| 128 |
packet->context = bt_get(context); |
| 129 |
|
| 130 |
end: |
| 131 |
BT_PUT(stream_class); |
| 132 |
BT_PUT(context_field_type); |
| 133 |
BT_PUT(expected_context_field_type); |
| 134 |
|
| 135 |
return ret; |
| 136 |
} |
| 137 |
|
| 138 |
BT_HIDDEN |
| 139 |
void bt_ctf_packet_freeze(struct bt_ctf_packet *packet) |
| 140 |
{ |
| 141 |
if (!packet) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
bt_ctf_field_freeze(packet->header); |
| 146 |
bt_ctf_field_freeze(packet->context); |
| 147 |
packet->frozen = 1; |
| 148 |
} |
| 149 |
|
| 150 |
static |
| 151 |
void bt_ctf_packet_destroy(struct bt_object *obj) |
| 152 |
{ |
| 153 |
struct bt_ctf_packet *packet; |
| 154 |
|
| 155 |
packet = container_of(obj, struct bt_ctf_packet, base); |
| 156 |
bt_put(packet->header); |
| 157 |
bt_put(packet->context); |
| 158 |
bt_put(packet->stream); |
| 159 |
g_free(packet); |
| 160 |
} |
| 161 |
|
| 162 |
BT_HIDDEN |
| 163 |
struct bt_ctf_packet *bt_ctf_packet_create( |
| 164 |
struct bt_ctf_stream *stream) |
| 165 |
{ |
| 166 |
struct bt_ctf_packet *packet = NULL; |
| 167 |
struct bt_ctf_stream_class *stream_class = NULL; |
| 168 |
struct bt_ctf_trace *trace = NULL; |
| 169 |
|
| 170 |
if (!stream || stream->pos.fd >= 0) { |
| 171 |
goto end; |
| 172 |
} |
| 173 |
|
| 174 |
stream_class = bt_ctf_stream_get_class(stream); |
| 175 |
assert(stream_class); |
| 176 |
trace = bt_ctf_stream_class_get_trace(stream_class); |
| 177 |
assert(trace); |
| 178 |
packet = g_new0(struct bt_ctf_packet, 1); |
| 179 |
if (!packet) { |
| 180 |
goto end; |
| 181 |
} |
| 182 |
|
| 183 |
bt_object_init(packet, bt_ctf_packet_destroy); |
| 184 |
packet->stream = bt_get(stream); |
| 185 |
packet->header = bt_ctf_field_create(trace->packet_header_type); |
| 186 |
if (!packet->header) { |
| 187 |
BT_PUT(packet); |
| 188 |
goto end; |
| 189 |
} |
| 190 |
|
| 191 |
packet->context = bt_ctf_field_create( |
| 192 |
stream->stream_class->packet_context_type); |
| 193 |
if (!packet->context) { |
| 194 |
BT_PUT(packet); |
| 195 |
goto end; |
| 196 |
} |
| 197 |
|
| 198 |
end: |
| 199 |
BT_PUT(trace); |
| 200 |
BT_PUT(stream_class); |
| 201 |
|
| 202 |
return packet; |
| 203 |
} |