Commit | Line | Data |
---|---|---|
e0f8968a SM |
1 | /* |
2 | * SPDX-License-Identifier: MIT | |
3 | * | |
4 | * Copyright 2024 EfficiOS, Inc. | |
5 | */ | |
6 | ||
7 | #ifndef CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP | |
8 | #define CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP | |
9 | ||
10 | #include "cpp-common/bt2/message.hpp" | |
11 | ||
12 | #include "clock-correlation-validator/clock-correlation-validator.h" | |
13 | ||
14 | namespace bt2ccv { | |
15 | ||
16 | class ClockCorrelationError final : public std::runtime_error | |
17 | { | |
18 | public: | |
19 | enum class Type | |
20 | { | |
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, | |
39 | }; | |
40 | ||
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} | |
49 | ||
50 | { | |
51 | } | |
52 | ||
53 | Type type() const noexcept | |
54 | { | |
55 | return _mType; | |
56 | } | |
57 | ||
58 | bt2s::optional<bt2c::UuidView> expectedUuid() const noexcept | |
59 | { | |
60 | return _mExpectedUuid; | |
61 | } | |
62 | ||
63 | bt2::OptionalBorrowedObject<bt2::ConstClockClass> actualClockCls() const noexcept | |
64 | { | |
65 | return _mActualClockCls; | |
66 | } | |
67 | ||
68 | bt2::OptionalBorrowedObject<bt2::ConstClockClass> expectedClockCls() const noexcept | |
69 | { | |
70 | return _mExpectedClockCls; | |
71 | } | |
72 | ||
73 | bt2::OptionalBorrowedObject<bt2::ConstStreamClass> streamCls() const noexcept | |
74 | { | |
75 | return _mStreamCls; | |
76 | } | |
77 | ||
78 | private: | |
79 | Type _mType; | |
80 | bt2s::optional<bt2c::UuidView> _mExpectedUuid; | |
81 | bt2::OptionalBorrowedObject<bt2::ConstClockClass> _mActualClockCls; | |
82 | bt2::OptionalBorrowedObject<bt2::ConstClockClass> _mExpectedClockCls; | |
83 | bt2::OptionalBorrowedObject<bt2::ConstStreamClass> _mStreamCls; | |
84 | }; | |
85 | ||
86 | class ClockCorrelationValidator final | |
87 | { | |
88 | private: | |
89 | enum class PropsExpectation | |
90 | { | |
91 | /* We haven't recorded clock properties yet. */ | |
92 | UNSET, | |
93 | ||
94 | /* Expect to have no clock. */ | |
95 | NONE, | |
96 | ||
97 | /* Expect a clock with a Unix epoch origin. */ | |
98 | ORIGIN_UNIX, | |
99 | ||
100 | /* Expect a clock without a Unix epoch origin, but with a UUID. */ | |
101 | ORIGIN_OTHER_UUID, | |
102 | ||
103 | /* Expect a clock without a Unix epoch origin and without a UUID. */ | |
104 | ORIGIN_OTHER_NO_UUID, | |
105 | }; | |
106 | ||
107 | public: | |
108 | void validate(const bt2::ConstMessage msg) | |
109 | { | |
110 | if (!msg.isStreamBeginning() && !msg.isMessageIteratorInactivity()) { | |
111 | return; | |
112 | } | |
113 | ||
114 | this->_validate(msg); | |
115 | } | |
116 | ||
117 | private: | |
118 | void _validate(const bt2::ConstMessage msg); | |
119 | ||
120 | PropsExpectation _mExpectation = PropsExpectation::UNSET; | |
121 | ||
122 | /* | |
123 | * Expected UUID of the clock, if `_mExpectation` is | |
124 | * `PropsExpectation::ORIGIN_OTHER_UUID`. | |
125 | * | |
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. | |
129 | */ | |
130 | bt2c::Uuid _mUuid; | |
131 | ||
132 | /* | |
133 | * Expected clock class, if `_mExpectation` is | |
134 | * `ClockExpectation::ORIGIN_OTHER_NO_UUID`. | |
135 | * | |
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 | |
138 | * instance. | |
139 | * | |
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. | |
145 | */ | |
146 | bt2::ConstClockClass::Shared _mClockClass; | |
147 | }; | |
148 | ||
149 | } /* namespace bt2ccv */ | |
150 | ||
151 | #endif /* CLOCK_CORRELATION_VALIDATOR_CLOCK_CORRELATION_VALIDATOR_HPP */ |