- int ret = 0;
- struct bt_stream_class *stream_class = NULL;
- struct bt_field_type *context_field_type = NULL;
- struct bt_field_type *expected_context_field_type = NULL;
-
- if (!packet) {
- BT_LOGW_STR("Invalid parameter: packet is NULL.");
- ret = -1;
- goto end;
- }
-
- if (packet->frozen) {
- BT_LOGW("Invalid parameter: packet is frozen: addr=%p",
- packet);
- ret = -1;
- goto end;
- }
-
- stream_class = bt_stream_get_class(packet->stream);
- assert(stream_class);
- expected_context_field_type =
- bt_stream_class_get_packet_context_type(stream_class);
-
- if (!context) {
- if (expected_context_field_type) {
- BT_LOGW("Invalid parameter: setting no packet context but packet context field type is not NULL: "
- "packet-addr=%p, packet-context-ft-addr=%p",
- packet, expected_context_field_type);
- ret = -1;
- goto end;
- }
-
- goto skip_validation;
- }
-
- context_field_type = bt_field_get_type(context);
- assert(context_field_type);
-
- if (bt_field_type_compare(context_field_type,
- expected_context_field_type)) {
- BT_LOGW("Invalid parameter: packet context's field type is different from the stream class's packet context field type: "
- "packet-addr=%p, packet-context-addr=%p",
- packet, context);
- ret = -1;
- goto end;
- }
-
-skip_validation:
+ BT_ASSERT_PRE_NON_NULL(packet, "Packet");
+ BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
+ BT_ASSERT_PRE(validate_field_to_set(context,
+ BT_FROM_COMMON(packet->stream->common.stream_class->packet_context_field_type)),
+ "Invalid packet context field: "
+ "%![packet-]+a, %![field-]+f", packet, context);