2 * SPDX-License-Identifier: MIT
4 * Copyright 2024 EfficiOS, Inc.
7 #ifndef CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP
8 #define CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP
10 #include "cpp-common/bt2/message.hpp"
12 #include "clock-correlation-validator/clock-correlation-validator.h"
16 class ClockCorrelationError final : public std::runtime_error
21 EXPECTING_NO_CLOCK_CLASS_GOT_ONE =
22 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_NO_CLOCK_CLASS_GOT_ONE,
23 EXPECTING_ORIGIN_UNIX_GOT_NONE =
24 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UNIX_GOT_NONE,
25 EXPECTING_ORIGIN_UNIX_GOT_OTHER =
26 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UNIX_GOT_OTHER,
27 EXPECTING_ORIGIN_UUID_GOT_NONE =
28 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_NONE,
29 EXPECTING_ORIGIN_UUID_GOT_UNIX =
30 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_UNIX,
31 EXPECTING_ORIGIN_UUID_GOT_NO_UUID =
32 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_NO_UUID,
33 EXPECTING_ORIGIN_UUID_GOT_OTHER_UUID =
34 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_UUID_GOT_OTHER_UUID,
35 EXPECTING_ORIGIN_NO_UUID_GOT_NONE =
36 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_NO_UUID_GOT_NONE,
37 EXPECTING_ORIGIN_NO_UUID_GOT_OTHER =
38 BT_CLOCK_CORRELATION_VALIDATOR_ERROR_TYPE_EXPECTING_ORIGIN_NO_UUID_GOT_OTHER,
41 explicit ClockCorrelationError(
42 Type type, const bt2s::optional<bt2c::UuidView> expectedUuid,
43 const bt2::OptionalBorrowedObject<bt2::ConstClockClass> actualClockCls,
44 const bt2::OptionalBorrowedObject<bt2::ConstClockClass> expectedClockCls,
45 const bt2::OptionalBorrowedObject<bt2::ConstStreamClass> streamCls) noexcept :
46 std::runtime_error {"Clock classes are not correlatable"},
47 _mType {type}, _mExpectedUuid {expectedUuid}, _mActualClockCls {actualClockCls},
48 _mExpectedClockCls {expectedClockCls}, _mStreamCls {streamCls}
53 Type type() const noexcept
58 bt2s::optional<bt2c::UuidView> expectedUuid() const noexcept
60 return _mExpectedUuid;
63 bt2::OptionalBorrowedObject<bt2::ConstClockClass> actualClockCls() const noexcept
65 return _mActualClockCls;
68 bt2::OptionalBorrowedObject<bt2::ConstClockClass> expectedClockCls() const noexcept
70 return _mExpectedClockCls;
73 bt2::OptionalBorrowedObject<bt2::ConstStreamClass> streamCls() const noexcept
80 bt2s::optional<bt2c::UuidView> _mExpectedUuid;
81 bt2::OptionalBorrowedObject<bt2::ConstClockClass> _mActualClockCls;
82 bt2::OptionalBorrowedObject<bt2::ConstClockClass> _mExpectedClockCls;
83 bt2::OptionalBorrowedObject<bt2::ConstStreamClass> _mStreamCls;
86 class ClockCorrelationValidator final
89 enum class PropsExpectation
91 /* We haven't recorded clock properties yet. */
94 /* Expect to have no clock. */
97 /* Expect a clock with a Unix epoch origin. */
100 /* Expect a clock without a Unix epoch origin, but with a UUID. */
103 /* Expect a clock without a Unix epoch origin and without a UUID. */
104 ORIGIN_OTHER_NO_UUID,
108 void validate(const bt2::ConstMessage msg)
110 if (!msg.isStreamBeginning() && !msg.isMessageIteratorInactivity()) {
114 this->_validate(msg);
118 void _validate(const bt2::ConstMessage msg);
120 PropsExpectation _mExpectation = PropsExpectation::UNSET;
123 * Expected UUID of the clock, if `_mExpectation` is
124 * `PropsExpectation::ORIGIN_OTHER_UUID`.
126 * If the origin of the clock is the Unix epoch, then the UUID is
127 * irrelevant because the clock will have a correlation with other
128 * clocks having the same origin.
133 * Expected clock class, if `_mExpectation` is
134 * `ClockExpectation::ORIGIN_OTHER_NO_UUID`.
136 * If the first analyzed clock class has an unknown origin and no
137 * UUID, then all subsequent analyzed clock classes must be the same
140 * To make sure that the clock class pointed to by this member
141 * doesn't get freed and another one reallocated at the same
142 * address, which could potentially bypass the clock expectation
143 * check, we keep a strong reference, ensuring that the clock class
144 * lives at least as long as the owner of this validator.
146 bt2::ConstClockClass::Shared _mClockClass;
149 } /* namespace bt2ccv */
151 #endif /* CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP */