.clang-tidy: enable cppcoreguidelines-avoid-const-or-ref-data-members
[babeltrace.git] / src / clock-correlation-validator / clock-correlation-validator.cpp
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
14 namespace bt2ccv {
15
16 void ClockCorrelationValidator::_validate(const bt2::ConstMessage msg)
17 {
18 bt2::OptionalBorrowedObject<bt2::ConstClockClass> clockCls;
19 bt2::OptionalBorrowedObject<bt2::ConstStreamClass> streamCls;
20
21 switch (msg.type()) {
22 case bt2::MessageType::STREAM_BEGINNING:
23 streamCls = msg.asStreamBeginning().stream().cls();
24 clockCls = streamCls->defaultClockClass();
25 break;
26
27 case bt2::MessageType::MESSAGE_ITERATOR_INACTIVITY:
28 clockCls = msg.asMessageIteratorInactivity().clockSnapshot().clockClass();
29 break;
30
31 default:
32 bt_common_abort();
33 }
34
35 switch (_mExpectation) {
36 case PropsExpectation::UNSET:
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) {
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;
48 _mUuid = *uuid;
49 } else {
50 _mExpectation = PropsExpectation::ORIGIN_OTHER_NO_UUID;
51 _mClockClass = clockCls->shared();
52 }
53
54 break;
55
56 case PropsExpectation::NONE:
57 if (clockCls) {
58 throw ClockCorrelationError {
59 ClockCorrelationError::Type::EXPECTING_NO_CLOCK_CLASS_GOT_ONE,
60 bt2s::nullopt,
61 *clockCls,
62 {},
63 streamCls};
64 }
65
66 break;
67
68 case PropsExpectation::ORIGIN_UNIX:
69 if (!clockCls) {
70 throw ClockCorrelationError {
71 ClockCorrelationError::Type::EXPECTING_ORIGIN_UNIX_GOT_NONE,
72 bt2s::nullopt,
73 {},
74 {},
75 streamCls};
76 }
77
78 if (!clockCls->originIsUnixEpoch()) {
79 throw ClockCorrelationError {
80 ClockCorrelationError::Type::EXPECTING_ORIGIN_UNIX_GOT_OTHER,
81 bt2s::nullopt,
82 *clockCls,
83 {},
84 streamCls};
85 }
86
87 break;
88
89 case PropsExpectation::ORIGIN_OTHER_UUID:
90 {
91 if (!clockCls) {
92 throw ClockCorrelationError {
93 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_NONE,
94 bt2s::nullopt,
95 {},
96 {},
97 streamCls};
98 }
99
100 if (clockCls->originIsUnixEpoch()) {
101 throw ClockCorrelationError {
102 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_UNIX,
103 bt2s::nullopt,
104 *clockCls,
105 {},
106 streamCls};
107 }
108
109 const auto uuid = clockCls->uuid();
110
111 if (!uuid) {
112 throw ClockCorrelationError {
113 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_NO_UUID,
114 bt2s::nullopt,
115 *clockCls,
116 {},
117 streamCls};
118 }
119
120 if (*uuid != _mUuid) {
121 throw ClockCorrelationError {
122 ClockCorrelationError::Type::EXPECTING_ORIGIN_UUID_GOT_OTHER_UUID,
123 _mUuid,
124 *clockCls,
125 {},
126 streamCls};
127 }
128
129 break;
130 }
131
132 case PropsExpectation::ORIGIN_OTHER_NO_UUID:
133 if (!clockCls) {
134 throw ClockCorrelationError {
135 ClockCorrelationError::Type::EXPECTING_ORIGIN_NO_UUID_GOT_NONE,
136 bt2s::nullopt,
137 {},
138 {},
139 streamCls};
140 }
141
142 if (clockCls->libObjPtr() != _mClockClass->libObjPtr()) {
143 throw ClockCorrelationError {
144 ClockCorrelationError::Type::EXPECTING_ORIGIN_NO_UUID_GOT_OTHER, bt2s::nullopt,
145 *clockCls, *_mClockClass, streamCls};
146 }
147
148 break;
149
150 default:
151 bt_common_abort();
152 }
153 }
154
155 } /* namespace bt2ccv */
156
157 bt_clock_correlation_validator *bt_clock_correlation_validator_create() noexcept
158 {
159 try {
160 return reinterpret_cast<bt_clock_correlation_validator *>(
161 new bt2ccv::ClockCorrelationValidator);
162 } catch (const std::bad_alloc&) {
163 return nullptr;
164 }
165 }
166
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
172 {
173 try {
174 reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator)->validate(bt2::wrap(msg));
175 return true;
176 } catch (const bt2ccv::ClockCorrelationError& error) {
177 *type = static_cast<bt_clock_correlation_validator_error_type>(error.type());
178
179 if (error.expectedUuid()) {
180 *expectedUuidOut = error.expectedUuid()->data();
181 } else {
182 *expectedUuidOut = nullptr;
183 }
184
185 if (error.actualClockCls()) {
186 *actualClockClsOut = error.actualClockCls()->libObjPtr();
187 } else {
188 *actualClockClsOut = nullptr;
189 }
190
191 if (error.expectedClockCls()) {
192 *expectedClockClsOut = error.expectedClockCls()->libObjPtr();
193 } else {
194 *expectedClockClsOut = nullptr;
195 }
196
197 return false;
198 }
199 }
200
201 void bt_clock_correlation_validator_destroy(
202 bt_clock_correlation_validator * const validator) noexcept
203 {
204 delete reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator);
205 }
This page took 0.034318 seconds and 4 git commands to generate.