1 #pragma once
2 
3 #include "types/sensor_types.hpp"
4 #include "utils/conversion.hpp"
5 #include "utils/labeled_tuple.hpp"
6 #include "utils/tstring.hpp"
7 #include "utils/variant_utils.hpp"
8 
9 #include <string>
10 #include <tuple>
11 #include <utility>
12 #include <variant>
13 #include <vector>
14 
15 enum class TriggerAction
16 {
17     LogToJournal = 0,
18     LogToRedfishEventLog,
19     UpdateReport,
20 };
21 
22 namespace details
23 {
24 constexpr std::array<std::pair<std::string_view, TriggerAction>, 3>
25     convDataTriggerAction = {
26         std::make_pair(
27             "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal",
28             TriggerAction::LogToJournal),
29         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.TriggerAction."
30                        "LogToRedfishEventLog",
31                        TriggerAction::LogToRedfishEventLog),
32         std::make_pair(
33             "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport",
34             TriggerAction::UpdateReport)};
35 }
36 
toTriggerAction(const std::string & str)37 inline TriggerAction toTriggerAction(const std::string& str)
38 {
39     return utils::toEnum(details::convDataTriggerAction, str);
40 }
41 
actionToString(TriggerAction v)42 inline std::string actionToString(TriggerAction v)
43 {
44     return std::string(utils::enumToString(details::convDataTriggerAction, v));
45 }
46 
47 namespace discrete
48 {
49 
50 enum class Severity
51 {
52     ok = 0,
53     warning,
54     critical
55 };
56 
57 using ThresholdParam =
58     std::tuple<std::string, std::string, uint64_t, std::string>;
59 
60 using LabeledThresholdParam = utils::LabeledTuple<
61     std::tuple<std::string, Severity, uint64_t, std::string>,
62     utils::tstring::UserId, utils::tstring::Severity, utils::tstring::DwellTime,
63     utils::tstring::ThresholdValue>;
64 } // namespace discrete
65 
66 namespace numeric
67 {
68 
69 enum class Type
70 {
71     lowerCritical = 0,
72     lowerWarning,
73     upperWarning,
74     upperCritical
75 };
76 
77 enum class Direction
78 {
79     either = 0,
80     decreasing,
81     increasing
82 };
83 
84 namespace details
85 {
86 
87 constexpr std::array<std::pair<std::string_view, Type>, 4> convDataType = {
88     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical",
89                    Type::lowerCritical),
90     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning",
91                    Type::lowerWarning),
92     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning",
93                    Type::upperWarning),
94     std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical",
95                    Type::upperCritical)};
96 
97 constexpr std::array<std::pair<std::string_view, Direction>, 3>
98     convDataDirection = {
99         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Direction.Either",
100                        Direction::either),
101         std::make_pair(
102             "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing",
103             Direction::decreasing),
104         std::make_pair(
105             "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing",
106             Direction::increasing)};
107 
108 } // namespace details
109 
toType(const std::string & str)110 inline Type toType(const std::string& str)
111 {
112     return utils::toEnum(details::convDataType, str);
113 }
114 
typeToString(Type v)115 inline std::string typeToString(Type v)
116 {
117     return std::string(utils::enumToString(details::convDataType, v));
118 }
119 
toDirection(const std::string & str)120 inline Direction toDirection(const std::string& str)
121 {
122     return utils::toEnum(details::convDataDirection, str);
123 }
124 
directionToString(Direction v)125 inline std::string directionToString(Direction v)
126 {
127     return std::string(utils::enumToString(details::convDataDirection, v));
128 }
129 
130 using ThresholdParam = std::tuple<std::string, uint64_t, std::string, double>;
131 
132 using LabeledThresholdParam =
133     utils::LabeledTuple<std::tuple<Type, uint64_t, Direction, double>,
134                         utils::tstring::Type, utils::tstring::DwellTime,
135                         utils::tstring::Direction,
136                         utils::tstring::ThresholdValue>;
137 } // namespace numeric
138 
139 using TriggerThresholdParamsExt =
140     std::variant<std::monostate, std::vector<numeric::ThresholdParam>,
141                  std::vector<discrete::ThresholdParam>>;
142 
143 using TriggerThresholdParams =
144     utils::WithoutMonostate<TriggerThresholdParamsExt>;
145 
146 using LabeledTriggerThresholdParams =
147     std::variant<std::vector<numeric::LabeledThresholdParam>,
148                  std::vector<discrete::LabeledThresholdParam>>;
149 
150 using LabeledThresholdParam =
151     std::variant<std::monostate, numeric::LabeledThresholdParam,
152                  discrete::LabeledThresholdParam>;
153 
154 inline bool
isTriggerThresholdDiscrete(const LabeledTriggerThresholdParams & params)155     isTriggerThresholdDiscrete(const LabeledTriggerThresholdParams& params)
156 {
157     return std::holds_alternative<std::vector<discrete::LabeledThresholdParam>>(
158         params);
159 }
160 
161 using TriggerId = std::unique_ptr<const std::string>;
162 using TriggerValue = std::variant<double, std::string>;
163 using ThresholdName = std::optional<std::reference_wrapper<const std::string>>;
164 
triggerValueToString(TriggerValue val)165 inline std::string triggerValueToString(TriggerValue val)
166 {
167     if (auto* doubleVal = std::get_if<double>(&val); doubleVal != nullptr)
168     {
169         return std::to_string(*doubleVal);
170     }
171     else
172     {
173         return std::get<std::string>(val);
174     }
175 }
176 
177 namespace utils
178 {
179 
180 constexpr std::array<std::pair<std::string_view, discrete::Severity>, 3>
181     convDataSeverity = {
182         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Severity.OK",
183                        discrete::Severity::ok),
184         std::make_pair("xyz.openbmc_project.Telemetry.Trigger.Severity.Warning",
185                        discrete::Severity::warning),
186         std::make_pair(
187             "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical",
188             discrete::Severity::critical)};
189 
toSeverity(const std::string & str)190 inline discrete::Severity toSeverity(const std::string& str)
191 {
192     return utils::toEnum(convDataSeverity, str);
193 }
194 
enumToString(discrete::Severity v)195 inline std::string enumToString(discrete::Severity v)
196 {
197     return std::string(enumToString(convDataSeverity, v));
198 }
199 
200 template <>
201 struct EnumTraits<TriggerAction>
202 {
203     static constexpr auto propertyName = ConstexprString{"TriggerAction"};
204 };
205 
206 template <>
207 struct EnumTraits<discrete::Severity>
208 {
209     static constexpr auto propertyName = ConstexprString{"discrete::Severity"};
210 };
211 
212 template <>
213 struct EnumTraits<numeric::Type>
214 {
215     static constexpr auto propertyName = ConstexprString{"numeric::Type"};
216 };
217 
218 template <>
219 struct EnumTraits<numeric::Direction>
220 {
221     static constexpr auto propertyName = ConstexprString{"numeric::Direction"};
222 };
223 
224 } // namespace utils
225