2 * SPDX-License-Identifier: MIT
4 * Copyright 2024 EfficiOS, Inc.
7 #include "cpp-common/bt2/clock-class.hpp"
8 #include "cpp-common/bt2/message.hpp"
9 #include "cpp-common/bt2/wrap.hpp"
11 #include "clock-correlation-validator.h"
12 #include "clock-correlation-validator.hpp"
16 void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg
)
18 bt2::OptionalBorrowedObject
<bt2::ConstClockClass
> clockCls
;
19 bt2::OptionalBorrowedObject
<bt2::ConstStreamClass
> streamCls
;
22 case bt2::MessageType::StreamBeginning
:
23 streamCls
= msg
.asStreamBeginning().stream().cls();
24 clockCls
= streamCls
->defaultClockClass();
27 case bt2::MessageType::MessageIteratorInactivity
:
28 clockCls
= msg
.asMessageIteratorInactivity().clockSnapshot().clockClass();
35 switch (_mExpectation
) {
36 case PropsExpectation::Unset
:
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.
43 _mExpectation
= PropsExpectation::None
;
44 } else if (clockCls
->originIsUnixEpoch()) {
45 _mExpectation
= PropsExpectation::OriginUnix
;
46 } else if (const auto uuid
= clockCls
->uuid()) {
47 _mExpectation
= PropsExpectation::OriginOtherUuid
;
50 _mExpectation
= PropsExpectation::OriginOtherNoUuid
;
51 _mClockClass
= clockCls
->shared();
56 case PropsExpectation::None
:
58 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingNoClockClassGotOne
,
67 case PropsExpectation::OriginUnix
:
69 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginUnixGotNone
,
76 if (!clockCls
->originIsUnixEpoch()) {
77 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginUnixGotOther
,
86 case PropsExpectation::OriginOtherUuid
:
89 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginUuidGotNone
,
96 if (clockCls
->originIsUnixEpoch()) {
97 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginUuidGotUnix
,
104 const auto uuid
= clockCls
->uuid();
107 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginUuidGotNoUuid
,
114 if (*uuid
!= _mUuid
) {
115 throw ClockCorrelationError
{
116 ClockCorrelationError::Type::ExpectingOriginUuidGotOtherUuid
,
126 case PropsExpectation::OriginOtherNoUuid
:
128 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginNoUuidGotNone
,
135 if (clockCls
->libObjPtr() != _mClockClass
->libObjPtr()) {
136 throw ClockCorrelationError
{ClockCorrelationError::Type::ExpectingOriginNoUuidGotOther
,
137 bt2s::nullopt
, *clockCls
, *_mClockClass
, streamCls
};
147 } /* namespace bt2ccv */
149 bt_clock_correlation_validator
*bt_clock_correlation_validator_create() noexcept
152 return reinterpret_cast<bt_clock_correlation_validator
*>(
153 new bt2ccv::ClockCorrelationValidator
);
154 } catch (const std::bad_alloc
&) {
159 bool 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
166 reinterpret_cast<bt2ccv::ClockCorrelationValidator
*>(validator
)->validate(bt2::wrap(msg
));
168 } catch (const bt2ccv::ClockCorrelationError
& error
) {
169 *type
= static_cast<bt_clock_correlation_validator_error_type
>(error
.type());
171 if (error
.expectedUuid()) {
172 *expectedUuidOut
= error
.expectedUuid()->data();
174 *expectedUuidOut
= nullptr;
177 if (error
.actualClockCls()) {
178 *actualClockClsOut
= error
.actualClockCls()->libObjPtr();
180 *actualClockClsOut
= nullptr;
183 if (error
.expectedClockCls()) {
184 *expectedClockClsOut
= error
.expectedClockCls()->libObjPtr();
186 *expectedClockClsOut
= nullptr;
193 void bt_clock_correlation_validator_destroy(
194 bt_clock_correlation_validator
* const validator
) noexcept
196 delete reinterpret_cast<bt2ccv::ClockCorrelationValidator
*>(validator
);