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::STREAM_BEGINNING
:
23 streamCls
= msg
.asStreamBeginning().stream().cls();
24 clockCls
= streamCls
->defaultClockClass();
27 case bt2::MessageType::MESSAGE_ITERATOR_INACTIVITY
:
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::ORIGIN_UNIX
;
46 } else if (const auto uuid
= clockCls
->uuid()) {
47 _mExpectation
= PropsExpectation::ORIGIN_OTHER_UUID
;
50 _mExpectation
= PropsExpectation::ORIGIN_OTHER_NO_UUID
;
51 _mClockClass
= clockCls
->shared();
56 case PropsExpectation::NONE
:
58 throw ClockCorrelationError
{
59 ClockCorrelationError::Type::EXPECTING_NO_CLOCK_CLASS_GOT_ONE
,
68 case PropsExpectation::ORIGIN_UNIX
:
70 throw ClockCorrelationError
{
71 ClockCorrelationError::Type::EXPECTING_ORIGIN_UNIX_GOT_NONE
,
78 if (!clockCls
->originIsUnixEpoch()) {
79 throw ClockCorrelationError
{
80 ClockCorrelationError::Type::EXPECTING_ORIGIN_UNIX_GOT_OTHER
,
89 case PropsExpectation::ORIGIN_OTHER_UUID
:
92 throw ClockCorrelationError
{
93 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_NONE
,
100 if (clockCls
->originIsUnixEpoch()) {
101 throw ClockCorrelationError
{
102 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_UNIX
,
109 const auto uuid
= clockCls
->uuid();
112 throw ClockCorrelationError
{
113 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_NO_UUID
,
120 if (*uuid
!= _mUuid
) {
121 throw ClockCorrelationError
{
122 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_OTHER_UUID
,
132 case PropsExpectation::ORIGIN_OTHER_NO_UUID
:
134 throw ClockCorrelationError
{
135 ClockCorrelationError::Type::EXPECTING_ORIGIN_NO_UUID_GOT_NONE
,
142 if (clockCls
->libObjPtr() != _mClockClass
->libObjPtr()) {
143 throw ClockCorrelationError
{
144 ClockCorrelationError::Type::EXPECTING_ORIGIN_NO_UUID_GOT_OTHER
, bt2s::nullopt
,
145 *clockCls
, *_mClockClass
, streamCls
};
155 } /* namespace bt2ccv */
157 bt_clock_correlation_validator
*bt_clock_correlation_validator_create() noexcept
160 return reinterpret_cast<bt_clock_correlation_validator
*>(
161 new bt2ccv::ClockCorrelationValidator
);
162 } catch (const std::bad_alloc
&) {
167 bool bt_clock_correlation_validator_validate_message(
168 bt_clock_correlation_validator
* const validator
, const bt_message
* const msg
,
169 bt_clock_correlation_validator_error_type
* const type
, bt_uuid
* const expectedUuidOut
,
170 const bt_clock_class
** const actualClockClsOut
,
171 const bt_clock_class
** const expectedClockClsOut
) noexcept
174 reinterpret_cast<bt2ccv::ClockCorrelationValidator
*>(validator
)->validate(bt2::wrap(msg
));
176 } catch (const bt2ccv::ClockCorrelationError
& error
) {
177 *type
= static_cast<bt_clock_correlation_validator_error_type
>(error
.type());
179 if (error
.expectedUuid()) {
180 *expectedUuidOut
= error
.expectedUuid()->data();
182 *expectedUuidOut
= nullptr;
185 if (error
.actualClockCls()) {
186 *actualClockClsOut
= error
.actualClockCls()->libObjPtr();
188 *actualClockClsOut
= nullptr;
191 if (error
.expectedClockCls()) {
192 *expectedClockClsOut
= error
.expectedClockCls()->libObjPtr();
194 *expectedClockClsOut
= nullptr;
201 void bt_clock_correlation_validator_destroy(
202 bt_clock_correlation_validator
* const validator
) noexcept
204 delete reinterpret_cast<bt2ccv::ClockCorrelationValidator
*>(validator
);