1 #pragma once
2
3 #include <openssl/crypto.h>
4 #include <stdint.h>
5
6 #include <sdbusplus/server.hpp>
7
8 #include <map>
9 #include <string>
10 #include <variant>
11
12 namespace ipmi
13 {
14
15 using DbusObjectPath = std::string;
16 using DbusService = std::string;
17 using DbusInterface = std::string;
18 using DbusObjectInfo = std::pair<DbusObjectPath, DbusService>;
19 using DbusProperty = std::string;
20
21 using Association = std::tuple<std::string, std::string, std::string>;
22 using BootProgressCode = std::tuple<std::vector<uint8_t>, std::vector<uint8_t>>;
23
24 using Value = std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t,
25 int64_t, uint64_t, double, std::string,
26 std::vector<uint8_t>, std::vector<uint16_t>,
27 std::vector<uint32_t>, std::vector<std::string>,
28 std::vector<Association>, BootProgressCode>;
29
30 using PropertyMap = std::map<DbusProperty, Value>;
31
32 using ObjectTree =
33 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
34
35 using InterfaceList = std::vector<std::string>;
36
37 using DbusInterfaceMap = std::map<DbusInterface, PropertyMap>;
38
39 using ObjectValueTree =
40 std::map<sdbusplus::message::object_path, DbusInterfaceMap>;
41
42 namespace sensor
43 {
44
45 using Offset = uint8_t;
46
47 /**
48 * @enum SkipAssertion
49 * Matching value for skipping the update
50 */
51 enum class SkipAssertion
52 {
53 NONE, // No skip defined
54 ASSERT, // Skip on Assert
55 DEASSERT, // Skip on Deassert
56 };
57
58 struct Values
59 {
60 SkipAssertion skip;
61 Value assert;
62 Value deassert;
63 };
64
65 /**
66 * @enum PreReqValues
67 * Pre-req conditions for a property.
68 */
69 struct PreReqValues
70 {
71 Value assert; // Value in case of assert.
72 Value deassert; // Value in case of deassert.
73 };
74
75 using PreReqOffsetValueMap = std::map<Offset, PreReqValues>;
76
77 /**
78 * @struct SetSensorReadingReq
79 *
80 * IPMI Request data for Set Sensor Reading and Event Status Command
81 */
82 struct SetSensorReadingReq
83 {
84 uint8_t number;
85 uint8_t operation;
86 uint8_t reading;
87 uint8_t assertOffset0_7;
88 uint8_t assertOffset8_14;
89 uint8_t deassertOffset0_7;
90 uint8_t deassertOffset8_14;
91 uint8_t eventData1;
92 uint8_t eventData2;
93 uint8_t eventData3;
94 } __attribute__((packed));
95
96 /**
97 * @struct GetReadingResponse
98 *
99 * IPMI response data for Get Sensor Reading command.
100 */
101 struct GetReadingResponse
102 {
103 uint8_t reading; //!< Sensor reading.
104 uint8_t operation; //!< Sensor scanning status / reading state.
105 uint8_t assertOffset0_7; //!< Discrete assertion states(0-7).
106 uint8_t assertOffset8_14; //!< Discrete assertion states(8-14).
107 } __attribute__((packed));
108
109 constexpr auto inventoryRoot = "/xyz/openbmc_project/inventory";
110
111 struct GetSensorResponse
112 {
113 uint8_t reading; // sensor reading
114 bool readingOrStateUnavailable; // 1 = reading/state unavailable
115 bool scanningEnabled; // 0 = sensor scanning disabled
116 bool allEventMessagesEnabled; // 0 = All Event Messages disabled
117 uint8_t thresholdLevelsStates; // threshold/discrete sensor states
118 uint8_t discreteReadingSensorStates; // discrete-only, optional states
119 };
120
121 using OffsetValueMap = std::map<Offset, Values>;
122
123 using DbusPropertyValues = std::pair<PreReqOffsetValueMap, OffsetValueMap>;
124
125 using DbusPropertyMap = std::map<DbusProperty, DbusPropertyValues>;
126
127 using DbusInterfaceMap = std::map<DbusInterface, DbusPropertyMap>;
128
129 using InstancePath = std::string;
130 using Type = uint8_t;
131 using ReadingType = uint8_t;
132 using Multiplier = uint16_t;
133 using OffsetB = int16_t;
134 using Exponent = int8_t;
135 using ScaledOffset = double;
136 using Scale = int16_t;
137 using Unit = std::string;
138 using EntityType = uint8_t;
139 using EntityInst = uint8_t;
140 using SensorName = std::string;
141 using SensorUnits1 = uint8_t;
142
143 enum class Mutability
144 {
145 Read = 1 << 0,
146 Write = 1 << 1,
147 };
148
operator |(Mutability lhs,Mutability rhs)149 inline Mutability operator|(Mutability lhs, Mutability rhs)
150 {
151 return static_cast<Mutability>(
152 static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
153 }
154
operator &(Mutability lhs,Mutability rhs)155 inline Mutability operator&(Mutability lhs, Mutability rhs)
156 {
157 return static_cast<Mutability>(
158 static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
159 }
160
161 struct Info
162 {
163 EntityType entityType;
164 EntityInst instance;
165 Type sensorType;
166 InstancePath sensorPath;
167 DbusInterface sensorInterface;
168 ReadingType sensorReadingType;
169 Multiplier coefficientM;
170 OffsetB coefficientB;
171 Exponent exponentB;
172 ScaledOffset scaledOffset;
173 Exponent exponentR;
174 bool hasScale;
175 Scale scale;
176 SensorUnits1 sensorUnits1;
177 Unit unit;
178 std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
179 #ifndef FEATURE_SENSORS_CACHE
180 std::function<GetSensorResponse(const Info&)> getFunc;
181 #else
182 std::function<std::optional<GetSensorResponse>(uint8_t, const Info&,
183 const ipmi::PropertyMap&)>
184 getFunc;
185 #endif
186 Mutability mutability;
187 SensorName sensorName;
188 std::function<SensorName(const Info&)> sensorNameFunc;
189 DbusInterfaceMap propertyInterfaces;
190 };
191
192 using Id = uint8_t;
193 using IdInfoMap = std::map<Id, Info>;
194
195 using PropertyMap = ipmi::PropertyMap;
196
197 using InterfaceMap = std::map<DbusInterface, PropertyMap>;
198
199 using Object = sdbusplus::message::object_path;
200 using ObjectMap = std::map<Object, InterfaceMap>;
201
202 using IpmiUpdateData = sdbusplus::message_t;
203
204 struct SelData
205 {
206 Id sensorID;
207 Type sensorType;
208 ReadingType eventReadingType;
209 Offset eventOffset;
210 };
211
212 using InventoryPath = std::string;
213
214 using InvObjectIDMap = std::map<InventoryPath, SelData>;
215
216 enum class ThresholdMask
217 {
218 NON_CRITICAL_LOW_MASK = 0x01,
219 CRITICAL_LOW_MASK = 0x02,
220 NON_RECOVERABLE_LOW_MASK = 0x4,
221 NON_CRITICAL_HIGH_MASK = 0x08,
222 CRITICAL_HIGH_MASK = 0x10,
223 NON_RECOVERABLE_HIGH_MASK = 0x20,
224 };
225
226 static constexpr uint8_t maxContainedEntities = 4;
227 using ContainedEntitiesArray =
228 std::array<std::pair<uint8_t, uint8_t>, maxContainedEntities>;
229
230 struct EntityInfo
231 {
232 uint8_t containerEntityId;
233 uint8_t containerEntityInstance;
234 bool isList;
235 bool isLinked;
236 ContainedEntitiesArray containedEntities;
237 };
238
239 using EntityInfoMap = std::map<Id, EntityInfo>;
240
241 #ifdef FEATURE_SENSORS_CACHE
242 /**
243 * @struct SensorData
244 *
245 * The data to cache for sensors
246 */
247 struct SensorData
248 {
249 double value;
250 bool available;
251 bool functional;
252 GetSensorResponse response;
253 };
254
255 using SensorCacheMap = std::map<uint8_t, std::optional<SensorData>>;
256 #endif
257
258 } // namespace sensor
259
260 namespace network
261 {
262 constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
263
264 constexpr auto IPV4_ADDRESS_SIZE_BYTE = 4;
265 constexpr auto IPV6_ADDRESS_SIZE_BYTE = 16;
266
267 constexpr auto DEFAULT_MAC_ADDRESS = "00:00:00:00:00:00";
268 constexpr auto DEFAULT_ADDRESS = "0.0.0.0";
269
270 } // namespace network
271
272 template <typename T>
273 class SecureAllocator : public std::allocator<T>
274 {
275 public:
276 template <typename U>
277 struct rebind
278 {
279 typedef SecureAllocator<U> other;
280 };
281
deallocate(T * p,size_t n)282 void deallocate(T* p, size_t n)
283 {
284 OPENSSL_cleanse(p, n);
285 return std::allocator<T>::deallocate(p, n);
286 }
287 };
288
289 using SecureStringBase =
290 std::basic_string<char, std::char_traits<char>, SecureAllocator<char>>;
291 class SecureString : public SecureStringBase
292 {
293 public:
294 using SecureStringBase::basic_string;
SecureString(const SecureStringBase & other)295 SecureString(const SecureStringBase& other) : SecureStringBase(other) {};
296 SecureString(SecureString&) = default;
297 SecureString(const SecureString&) = default;
298 SecureString(SecureString&&) = default;
299 SecureString& operator=(SecureString&&) = default;
300 SecureString& operator=(const SecureString&) = default;
301
~SecureString()302 ~SecureString()
303 {
304 OPENSSL_cleanse(this->data(), this->size());
305 }
306 };
307
308 using SecureBufferBase = std::vector<uint8_t, SecureAllocator<uint8_t>>;
309
310 class SecureBuffer : public SecureBufferBase
311 {
312 public:
313 using SecureBufferBase::vector;
SecureBuffer(const SecureBufferBase & other)314 SecureBuffer(const SecureBufferBase& other) : SecureBufferBase(other) {};
315 SecureBuffer(SecureBuffer&) = default;
316 SecureBuffer(const SecureBuffer&) = default;
317 SecureBuffer(SecureBuffer&&) = default;
318 SecureBuffer& operator=(SecureBuffer&&) = default;
319 SecureBuffer& operator=(const SecureBuffer&) = default;
320
~SecureBuffer()321 ~SecureBuffer()
322 {
323 OPENSSL_cleanse(this->data(), this->size());
324 }
325 };
326 } // namespace ipmi
327