test-field.sh: make sure bt_run_in_py_env() and bt_cli() succeed
[babeltrace.git] / src / clock-correlation-validator / clock-correlation-validator.cpp
CommitLineData
e0f8968a
SM
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2024 EfficiOS, Inc.
5 */
6
7#include "cpp-common/bt2/clock-class.hpp"
8#include "cpp-common/bt2/message.hpp"
9#include "cpp-common/bt2/wrap.hpp"
10
11#include "clock-correlation-validator.h"
12#include "clock-correlation-validator.hpp"
13
14namespace bt2ccv {
15
16void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg)
17{
18 bt2::OptionalBorrowedObject<bt2::ConstClockClass> clockCls;
19 bt2::OptionalBorrowedObject<bt2::ConstStreamClass> streamCls;
20
21 switch (msg.type()) {
1c5ea5eb 22 case bt2::MessageType::StreamBeginning:
e0f8968a
SM
23 streamCls = msg.asStreamBeginning().stream().cls();
24 clockCls = streamCls->defaultClockClass();
25 break;
26
1c5ea5eb 27 case bt2::MessageType::MessageIteratorInactivity:
e0f8968a
SM
28 clockCls = msg.asMessageIteratorInactivity().clockSnapshot().clockClass();
29 break;
30
31 default:
32 bt_common_abort();
33 }
34
35 switch (_mExpectation) {
1c5ea5eb 36 case PropsExpectation::Unset:
e0f8968a
SM
37 /*
38 * This is the first analysis of a message with a clock
39 * snapshot: record the properties of that clock, against which
40 * we'll compare the clock properties of the following messages.
41 */
42 if (!clockCls) {
1c5ea5eb 43 _mExpectation = PropsExpectation::None;
e0f8968a 44 } else if (clockCls->originIsUnixEpoch()) {
1c5ea5eb 45 _mExpectation = PropsExpectation::OriginUnix;
e0f8968a 46 } else if (const auto uuid = clockCls->uuid()) {
1c5ea5eb 47 _mExpectation = PropsExpectation::OriginOtherUuid;
e0f8968a
SM
48 _mUuid = *uuid;
49 } else {
1c5ea5eb 50 _mExpectation = PropsExpectation::OriginOtherNoUuid;
e0f8968a
SM
51 _mClockClass = clockCls->shared();
52 }
53
54 break;
55
1c5ea5eb 56 case PropsExpectation::None:
e0f8968a 57 if (clockCls) {
1c5ea5eb
SM
58 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingNoClockClassGotOne,
59 bt2s::nullopt,
60 *clockCls,
61 {},
62 streamCls};
e0f8968a
SM
63 }
64
65 break;
66
1c5ea5eb 67 case PropsExpectation::OriginUnix:
e0f8968a 68 if (!clockCls) {
1c5ea5eb
SM
69 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUnixGotNone,
70 bt2s::nullopt,
71 {},
72 {},
73 streamCls};
e0f8968a
SM
74 }
75
76 if (!clockCls->originIsUnixEpoch()) {
1c5ea5eb
SM
77 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUnixGotOther,
78 bt2s::nullopt,
79 *clockCls,
80 {},
81 streamCls};
e0f8968a
SM
82 }
83
84 break;
85
1c5ea5eb 86 case PropsExpectation::OriginOtherUuid:
e0f8968a
SM
87 {
88 if (!clockCls) {
1c5ea5eb
SM
89 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotNone,
90 bt2s::nullopt,
91 {},
92 {},
93 streamCls};
e0f8968a
SM
94 }
95
96 if (clockCls->originIsUnixEpoch()) {
1c5ea5eb
SM
97 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotUnix,
98 bt2s::nullopt,
99 *clockCls,
100 {},
101 streamCls};
e0f8968a
SM
102 }
103
104 const auto uuid = clockCls->uuid();
105
106 if (!uuid) {
1c5ea5eb
SM
107 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotNoUuid,
108 bt2s::nullopt,
109 *clockCls,
110 {},
111 streamCls};
e0f8968a
SM
112 }
113
114 if (*uuid != _mUuid) {
115 throw ClockCorrelationError {
1c5ea5eb 116 ClockCorrelationError::Type::ExpectingOriginUuidGotOtherUuid,
e0f8968a
SM
117 _mUuid,
118 *clockCls,
119 {},
120 streamCls};
121 }
122
123 break;
124 }
125
1c5ea5eb 126 case PropsExpectation::OriginOtherNoUuid:
e0f8968a 127 if (!clockCls) {
1c5ea5eb
SM
128 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginNoUuidGotNone,
129 bt2s::nullopt,
130 {},
131 {},
132 streamCls};
e0f8968a
SM
133 }
134
135 if (clockCls->libObjPtr() != _mClockClass->libObjPtr()) {
1c5ea5eb
SM
136 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginNoUuidGotOther,
137 bt2s::nullopt, *clockCls, *_mClockClass, streamCls};
e0f8968a
SM
138 }
139
140 break;
141
142 default:
143 bt_common_abort();
144 }
145}
146
147} /* namespace bt2ccv */
148
149bt_clock_correlation_validator *bt_clock_correlation_validator_create() noexcept
150{
151 try {
152 return reinterpret_cast<bt_clock_correlation_validator *>(
153 new bt2ccv::ClockCorrelationValidator);
154 } catch (const std::bad_alloc&) {
155 return nullptr;
156 }
157}
158
159bool bt_clock_correlation_validator_validate_message(
160 bt_clock_correlation_validator * const validator, const bt_message * const msg,
161 bt_clock_correlation_validator_error_type * const type, bt_uuid * const expectedUuidOut,
162 const bt_clock_class ** const actualClockClsOut,
163 const bt_clock_class ** const expectedClockClsOut) noexcept
164{
165 try {
166 reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator)->validate(bt2::wrap(msg));
167 return true;
168 } catch (const bt2ccv::ClockCorrelationError& error) {
169 *type = static_cast<bt_clock_correlation_validator_error_type>(error.type());
170
171 if (error.expectedUuid()) {
172 *expectedUuidOut = error.expectedUuid()->data();
173 } else {
174 *expectedUuidOut = nullptr;
175 }
176
177 if (error.actualClockCls()) {
178 *actualClockClsOut = error.actualClockCls()->libObjPtr();
179 } else {
180 *actualClockClsOut = nullptr;
181 }
182
183 if (error.expectedClockCls()) {
184 *expectedClockClsOut = error.expectedClockCls()->libObjPtr();
185 } else {
186 *expectedClockClsOut = nullptr;
187 }
188
189 return false;
190 }
191}
192
193void bt_clock_correlation_validator_destroy(
194 bt_clock_correlation_validator * const validator) noexcept
195{
196 delete reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator);
197}
This page took 0.033524 seconds and 4 git commands to generate.