+ /* Packet header field type, if it exists, must be a structure */
+ if (!bt_field_type_is_structure(packet_header_type)) {
+ BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: "
+ "ft-addr=%p, ft-id=%s",
+ packet_header_type,
+ bt_field_type_id_string(packet_header_type->id));
+ goto invalid;
+ }
+
+ /*
+ * If there's a `magic` field, it must be a 32-bit unsigned
+ * integer field type. Also it must be the first field of the
+ * packet header field type.
+ */
+ field_type = bt_field_type_structure_get_field_type_by_name(
+ packet_header_type, "magic");
+ if (field_type) {
+ const char *field_name;
+
+ if (!bt_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: "
+ "magic-ft-addr=%p, magic-ft-id=%s",
+ field_type,
+ bt_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: "
+ "magic-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_get_size(field_type) != 32) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: "
+ "magic-ft-addr=%p, magic-ft-size=%u",
+ field_type,
+ bt_field_type_integer_get_size(field_type));
+ goto invalid;
+ }
+
+ ret = bt_field_type_structure_get_field_by_index(
+ packet_header_type, &field_name, NULL, 0);
+ BT_ASSERT(ret == 0);
+
+ if (strcmp(field_name, "magic") != 0) {
+ BT_LOGW("Invalid packet header field type: `magic` field must be the first field: "
+ "magic-ft-addr=%p, first-field-name=\"%s\"",
+ field_type, field_name);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `uuid` field, it must be an array field type of
+ * length 16 with an 8-bit unsigned integer element field type.
+ */
+ field_type = bt_field_type_structure_get_field_type_by_name(
+ packet_header_type, "uuid");
+ if (field_type) {
+ struct bt_field_type *elem_ft;
+
+ if (!bt_field_type_is_array(field_type)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: "
+ "uuid-ft-addr=%p, uuid-ft-id=%s",
+ field_type,
+ bt_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_field_type_array_get_length(field_type) != 16) {
+ BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: "
+ "uuid-ft-addr=%p, uuid-ft-length=%" PRId64,
+ field_type,
+ bt_field_type_array_get_length(field_type));
+ goto invalid;
+ }
+
+ elem_ft = bt_field_type_array_get_element_type(field_type);
+ BT_ASSERT(elem_ft);
+
+ if (!bt_field_type_is_integer(elem_ft)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: "
+ "elem-ft-addr=%p, elem-ft-id=%s",
+ elem_ft,
+ bt_field_type_id_string(elem_ft->id));
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_is_signed(elem_ft)) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: "
+ "elem-ft-addr=%p", elem_ft);
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_get_size(elem_ft) != 8) {
+ BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: "
+ "elem-ft-addr=%p, elem-ft-size=%u",
+ elem_ft,
+ bt_field_type_integer_get_size(elem_ft));
+ bt_put(elem_ft);
+ goto invalid;
+ }
+
+ bt_put(elem_ft);
+ BT_PUT(field_type);
+ }
+
+ /*
+ * The `stream_id` field must exist if there's more than one
+ * stream classes in the trace.
+ */
+ field_type = bt_field_type_structure_get_field_type_by_name(
+ packet_header_type, "stream_id");
+
+ if (!field_type && trace->stream_classes->len >= 1) {
+ BT_LOGW_STR("Invalid packet header field type: "
+ "`stream_id` field does not exist but there's more than one stream class in the trace.");
+ goto invalid;
+ }
+
+ /*
+ * If there's a `stream_id` field, it must be an unsigned
+ * integer field type.
+ */
+ if (field_type) {
+ if (!bt_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: "
+ "stream-id-ft-addr=%p, stream-id-ft-id=%s",
+ field_type,
+ bt_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: "
+ "stream-id-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ /*
+ * If there's a `packet_seq_num` field, it must be an unsigned
+ * integer field type.
+ */
+ field_type = bt_field_type_structure_get_field_type_by_name(
+ packet_header_type, "packet_seq_num");
+ if (field_type) {
+ if (!bt_field_type_is_integer(field_type)) {
+ BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an integer field type: "
+ "stream-id-ft-addr=%p, packet-seq-num-ft-id=%s",
+ field_type,
+ bt_field_type_id_string(field_type->id));
+ goto invalid;
+ }
+
+ if (bt_field_type_integer_is_signed(field_type)) {
+ BT_LOGW("Invalid packet header field type: `packet_seq_num` field must be an unsigned integer field type: "
+ "packet-seq-num-ft-addr=%p", field_type);
+ goto invalid;
+ }
+
+ BT_PUT(field_type);
+ }
+
+ goto end;
+
+invalid:
+ is_valid = false;