cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[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::StreamBeginning:
23 streamCls = msg.asStreamBeginning().stream().cls();
24 clockCls = streamCls->defaultClockClass();
25 break;
26
27 case bt2::MessageType::MessageIteratorInactivity:
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::OriginUnix;
46 } else if (const auto uuid = clockCls->uuid()) {
47 _mExpectation = PropsExpectation::OriginOtherUuid;
48 _mUuid = *uuid;
49 } else {
50 _mExpectation = PropsExpectation::OriginOtherNoUuid;
51 _mClockClass = clockCls->shared();
52 }
53
54 break;
55
56 case PropsExpectation::None:
57 if (clockCls) {
58 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingNoClockClassGotOne,
59 bt2s::nullopt,
60 *clockCls,
61 {},
62 streamCls};
63 }
64
65 break;
66
67 case PropsExpectation::OriginUnix:
68 if (!clockCls) {
69 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUnixGotNone,
70 bt2s::nullopt,
71 {},
72 {},
73 streamCls};
74 }
75
76 if (!clockCls->originIsUnixEpoch()) {
77 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUnixGotOther,
78 bt2s::nullopt,
79 *clockCls,
80 {},
81 streamCls};
82 }
83
84 break;
85
86 case PropsExpectation::OriginOtherUuid:
87 {
88 if (!clockCls) {
89 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotNone,
90 bt2s::nullopt,
91 {},
92 {},
93 streamCls};
94 }
95
96 if (clockCls->originIsUnixEpoch()) {
97 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotUnix,
98 bt2s::nullopt,
99 *clockCls,
100 {},
101 streamCls};
102 }
103
104 const auto uuid = clockCls->uuid();
105
106 if (!uuid) {
107 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginUuidGotNoUuid,
108 bt2s::nullopt,
109 *clockCls,
110 {},
111 streamCls};
112 }
113
114 if (*uuid != _mUuid) {
115 throw ClockCorrelationError {
116 ClockCorrelationError::Type::ExpectingOriginUuidGotOtherUuid,
117 _mUuid,
118 *clockCls,
119 {},
120 streamCls};
121 }
122
123 break;
124 }
125
126 case PropsExpectation::OriginOtherNoUuid:
127 if (!clockCls) {
128 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginNoUuidGotNone,
129 bt2s::nullopt,
130 {},
131 {},
132 streamCls};
133 }
134
135 if (clockCls->libObjPtr() != _mClockClass->libObjPtr()) {
136 throw ClockCorrelationError {ClockCorrelationError::Type::ExpectingOriginNoUuidGotOther,
137 bt2s::nullopt, *clockCls, *_mClockClass, streamCls};
138 }
139
140 break;
141
142 default:
143 bt_common_abort();
144 }
145 }
146
147 } /* namespace bt2ccv */
148
149 bt_clock_correlation_validator *bt_clock_correlation_validator_create() noexcept
150 {
151 try {
152 return reinterpret_cast<bt_clock_correlation_validator *>(
153 new bt2ccv::ClockCorrelationValidator);
154 } catch (const std::bad_alloc&) {
155 return nullptr;
156 }
157 }
158
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
164 {
165 try {
166 reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator)->validate(bt2::wrap(msg));
167 return true;
168 } catch (const bt2ccv::ClockCorrelationError& error) {
169 *type = static_cast<bt_clock_correlation_validator_error_type>(error.type());
170
171 if (error.expectedUuid()) {
172 *expectedUuidOut = error.expectedUuid()->data();
173 } else {
174 *expectedUuidOut = nullptr;
175 }
176
177 if (error.actualClockCls()) {
178 *actualClockClsOut = error.actualClockCls()->libObjPtr();
179 } else {
180 *actualClockClsOut = nullptr;
181 }
182
183 if (error.expectedClockCls()) {
184 *expectedClockClsOut = error.expectedClockCls()->libObjPtr();
185 } else {
186 *expectedClockClsOut = nullptr;
187 }
188
189 return false;
190 }
191 }
192
193 void bt_clock_correlation_validator_destroy(
194 bt_clock_correlation_validator * const validator) noexcept
195 {
196 delete reinterpret_cast<bt2ccv::ClockCorrelationValidator *>(validator);
197 }
This page took 0.033315 seconds and 4 git commands to generate.