blob: 8d65b0f8f11b36f6e3e25ead3424ac2dc44ca663 [file] [log] [blame]
Ratan Gupta00659052017-02-23 17:29:08 +05301#pragma once
2
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +00003#include <openssl/crypto.h>
Ratan Gupta00659052017-02-23 17:29:08 +05304#include <stdint.h>
5
6#include <map>
Ratan Gupta00659052017-02-23 17:29:08 +05307#include <sdbusplus/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -07009#include <variant>
Ratan Gupta00659052017-02-23 17:29:08 +053010
11namespace ipmi
12{
Ratan Guptadcb10672017-07-10 10:33:50 +053013
14using DbusObjectPath = std::string;
15using DbusService = std::string;
16using DbusInterface = std::string;
17using DbusObjectInfo = std::pair<DbusObjectPath, DbusService>;
18using DbusProperty = std::string;
Ratan Guptacc8feb42017-07-25 21:52:10 +053019
Willy Tude54f482021-01-26 15:59:09 -080020using Association = std::tuple<std::string, std::string, std::string>;
21
Jian Zhang33457102022-04-26 09:46:32 +000022using Value =
23 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
24 uint64_t, double, std::string, std::vector<uint8_t>,
25 std::vector<uint16_t>, std::vector<uint32_t>,
26 std::vector<std::string>, std::vector<Association>>;
Ratan Guptacc8feb42017-07-25 21:52:10 +053027
Ratan Guptadcb10672017-07-10 10:33:50 +053028using PropertyMap = std::map<DbusProperty, Value>;
Ratan Guptab8e99552017-07-27 07:07:48 +053029
Patrick Venture0b02be92018-08-31 11:55:55 -070030using ObjectTree =
31 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Gupta8c31d232017-08-13 05:49:43 +053032
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053033using InterfaceList = std::vector<std::string>;
34
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -060035using DbusInterfaceMap = std::map<DbusInterface, PropertyMap>;
36
37using ObjectValueTree =
38 std::map<sdbusplus::message::object_path, DbusInterfaceMap>;
39
Ratan Gupta00659052017-02-23 17:29:08 +053040namespace sensor
41{
42
43using Offset = uint8_t;
Ratan Gupta00659052017-02-23 17:29:08 +053044
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -050045/**
46 * @enum SkipAssertion
47 * Matching value for skipping the update
48 */
49enum class SkipAssertion
50{
Patrick Venture0b02be92018-08-31 11:55:55 -070051 NONE, // No skip defined
52 ASSERT, // Skip on Assert
53 DEASSERT, // Skip on Deassert
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -050054};
55
Ratan Gupta00659052017-02-23 17:29:08 +053056struct Values
57{
Patrick Venture0b02be92018-08-31 11:55:55 -070058 SkipAssertion skip;
59 Value assert;
60 Value deassert;
Ratan Gupta00659052017-02-23 17:29:08 +053061};
62
Tom Joseph816e92b2017-09-06 19:23:00 +053063/**
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -050064 * @enum PreReqValues
65 * Pre-req conditions for a property.
66 */
67struct PreReqValues
68{
Patrick Venture0b02be92018-08-31 11:55:55 -070069 Value assert; // Value in case of assert.
70 Value deassert; // Value in case of deassert.
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -050071};
72
73using PreReqOffsetValueMap = std::map<Offset, PreReqValues>;
74
75/**
Tom Joseph816e92b2017-09-06 19:23:00 +053076 * @struct SetSensorReadingReq
77 *
78 * IPMI Request data for Set Sensor Reading and Event Status Command
79 */
80struct SetSensorReadingReq
81{
82 uint8_t number;
83 uint8_t operation;
84 uint8_t reading;
85 uint8_t assertOffset0_7;
86 uint8_t assertOffset8_14;
87 uint8_t deassertOffset0_7;
88 uint8_t deassertOffset8_14;
89 uint8_t eventData1;
90 uint8_t eventData2;
91 uint8_t eventData3;
92} __attribute__((packed));
93
94/**
95 * @struct GetReadingResponse
96 *
97 * IPMI response data for Get Sensor Reading command.
98 */
99struct GetReadingResponse
100{
Patrick Venture0b02be92018-08-31 11:55:55 -0700101 uint8_t reading; //!< Sensor reading.
102 uint8_t operation; //!< Sensor scanning status / reading state.
103 uint8_t assertOffset0_7; //!< Discrete assertion states(0-7).
104 uint8_t assertOffset8_14; //!< Discrete assertion states(8-14).
Tom Joseph816e92b2017-09-06 19:23:00 +0530105} __attribute__((packed));
106
107constexpr auto inventoryRoot = "/xyz/openbmc_project/inventory";
108
Sui Chen4cc42552019-09-11 10:28:35 -0700109struct GetSensorResponse
110{
111 uint8_t reading; // sensor reading
112 bool readingOrStateUnavailable; // 1 = reading/state unavailable
113 bool scanningEnabled; // 0 = sensor scanning disabled
114 bool allEventMessagesEnabled; // 0 = All Event Messages disabled
115 uint8_t thresholdLevelsStates; // threshold/discrete sensor states
116 uint8_t discreteReadingSensorStates; // discrete-only, optional states
117};
Tom Joseph816e92b2017-09-06 19:23:00 +0530118
Patrick Venture0b02be92018-08-31 11:55:55 -0700119using OffsetValueMap = std::map<Offset, Values>;
Ratan Gupta00659052017-02-23 17:29:08 +0530120
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500121using DbusPropertyValues = std::pair<PreReqOffsetValueMap, OffsetValueMap>;
Ratan Gupta00659052017-02-23 17:29:08 +0530122
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500123using DbusPropertyMap = std::map<DbusProperty, DbusPropertyValues>;
124
125using DbusInterfaceMap = std::map<DbusInterface, DbusPropertyMap>;
Ratan Gupta00659052017-02-23 17:29:08 +0530126
127using InstancePath = std::string;
128using Type = uint8_t;
129using ReadingType = uint8_t;
Emily Shaffer10f49592017-05-10 12:01:10 -0700130using Multiplier = uint16_t;
Tom Joseph0a1301c2018-02-16 08:27:00 +0530131using OffsetB = int16_t;
132using Exponent = int8_t;
Emily Shaffer40011912018-10-09 12:06:04 -0700133using ScaledOffset = double;
Emily Shaffer62235612017-06-14 13:06:26 -0700134using Scale = int16_t;
135using Unit = std::string;
Tom Josephb0adbcd2018-01-24 11:51:29 +0530136using EntityType = uint8_t;
137using EntityInst = uint8_t;
138using SensorName = std::string;
Tony Leec5324252019-10-31 17:24:16 +0800139using SensorUnits1 = uint8_t;
Ratan Gupta00659052017-02-23 17:29:08 +0530140
Emily Shaffercc941e12017-06-14 13:06:26 -0700141enum class Mutability
142{
Patrick Venture0b02be92018-08-31 11:55:55 -0700143 Read = 1 << 0,
144 Write = 1 << 1,
Emily Shaffercc941e12017-06-14 13:06:26 -0700145};
146
147inline Mutability operator|(Mutability lhs, Mutability rhs)
148{
Patrick Venture0b02be92018-08-31 11:55:55 -0700149 return static_cast<Mutability>(static_cast<uint8_t>(lhs) |
150 static_cast<uint8_t>(rhs));
Emily Shaffercc941e12017-06-14 13:06:26 -0700151}
152
153inline Mutability operator&(Mutability lhs, Mutability rhs)
154{
Patrick Venture0b02be92018-08-31 11:55:55 -0700155 return static_cast<Mutability>(static_cast<uint8_t>(lhs) &
156 static_cast<uint8_t>(rhs));
Emily Shaffercc941e12017-06-14 13:06:26 -0700157}
158
Ratan Gupta00659052017-02-23 17:29:08 +0530159struct Info
160{
Patrick Venture0b02be92018-08-31 11:55:55 -0700161 EntityType entityType;
162 EntityInst instance;
163 Type sensorType;
164 InstancePath sensorPath;
165 DbusInterface sensorInterface;
166 ReadingType sensorReadingType;
167 Multiplier coefficientM;
168 OffsetB coefficientB;
169 Exponent exponentB;
170 ScaledOffset scaledOffset;
171 Exponent exponentR;
172 bool hasScale;
173 Scale scale;
Tony Leec5324252019-10-31 17:24:16 +0800174 SensorUnits1 sensorUnits1;
Patrick Venture0b02be92018-08-31 11:55:55 -0700175 Unit unit;
176 std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
Lei YU8c2c0482021-09-16 17:28:28 +0800177#ifndef FEATURE_SENSORS_CACHE
Patrick Venture0b02be92018-08-31 11:55:55 -0700178 std::function<GetSensorResponse(const Info&)> getFunc;
Lei YU8c2c0482021-09-16 17:28:28 +0800179#else
Lei YU8e8152c2021-12-06 20:11:08 +0800180 std::function<std::optional<GetSensorResponse>(uint8_t, const Info&,
181 const ipmi::PropertyMap&)>
Lei YU8c2c0482021-09-16 17:28:28 +0800182 getFunc;
183#endif
Patrick Venture0b02be92018-08-31 11:55:55 -0700184 Mutability mutability;
Jeremy Kerrbe4ffa82020-08-10 16:17:37 +0800185 SensorName sensorName;
Patrick Venture0b02be92018-08-31 11:55:55 -0700186 std::function<SensorName(const Info&)> sensorNameFunc;
187 DbusInterfaceMap propertyInterfaces;
Ratan Gupta00659052017-02-23 17:29:08 +0530188};
189
190using Id = uint8_t;
Patrick Venture0b02be92018-08-31 11:55:55 -0700191using IdInfoMap = std::map<Id, Info>;
Ratan Gupta00659052017-02-23 17:29:08 +0530192
Ratan Guptadcb10672017-07-10 10:33:50 +0530193using PropertyMap = ipmi::PropertyMap;
Tom Josephbe703f72017-03-09 12:34:35 +0530194
195using InterfaceMap = std::map<DbusInterface, PropertyMap>;
196
197using Object = sdbusplus::message::object_path;
198using ObjectMap = std::map<Object, InterfaceMap>;
199
Patrick Williams5d82f472022-07-22 19:26:53 -0500200using IpmiUpdateData = sdbusplus::message_t;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500201
Tom Josephdd78a1d2017-05-05 11:04:29 +0530202struct SelData
203{
Patrick Venture0b02be92018-08-31 11:55:55 -0700204 Id sensorID;
205 Type sensorType;
206 ReadingType eventReadingType;
207 Offset eventOffset;
Tom Josephdd78a1d2017-05-05 11:04:29 +0530208};
209
210using InventoryPath = std::string;
211
212using InvObjectIDMap = std::map<InventoryPath, SelData>;
213
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600214enum class ThresholdMask
215{
216 NON_CRITICAL_LOW_MASK = 0x01,
217 CRITICAL_LOW_MASK = 0x02,
218 NON_CRITICAL_HIGH_MASK = 0x08,
219 CRITICAL_HIGH_MASK = 0x10,
220};
221
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800222static constexpr uint8_t maxContainedEntities = 4;
223using ContainedEntitiesArray =
224 std::array<std::pair<uint8_t, uint8_t>, maxContainedEntities>;
225
226struct EntityInfo
227{
228 uint8_t containerEntityId;
229 uint8_t containerEntityInstance;
230 bool isList;
231 bool isLinked;
232 ContainedEntitiesArray containedEntities;
233};
234
235using EntityInfoMap = std::map<Id, EntityInfo>;
236
Lei YU97140502021-09-17 13:49:43 +0800237#ifdef FEATURE_SENSORS_CACHE
238/**
239 * @struct SensorData
240 *
241 * The data to cache for sensors
242 */
243struct SensorData
244{
245 double value;
246 bool available;
247 bool functional;
248 GetSensorResponse response;
249};
250
251using SensorCacheMap = std::map<uint8_t, std::optional<SensorData>>;
252#endif
253
Patrick Venture0b02be92018-08-31 11:55:55 -0700254} // namespace sensor
Ratan Gupta8c31d232017-08-13 05:49:43 +0530255
256namespace network
257{
Ratan Gupta8c31d232017-08-13 05:49:43 +0530258constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Gupta8c31d232017-08-13 05:49:43 +0530259
260constexpr auto IPV4_ADDRESS_SIZE_BYTE = 4;
261constexpr auto IPV6_ADDRESS_SIZE_BYTE = 16;
262
263constexpr auto DEFAULT_MAC_ADDRESS = "00:00:00:00:00:00";
264constexpr auto DEFAULT_ADDRESS = "0.0.0.0";
265
Patrick Venture0b02be92018-08-31 11:55:55 -0700266} // namespace network
William A. Kennington IIIc514d872019-04-06 18:19:38 -0700267
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000268template <typename T>
269class SecureAllocator : public std::allocator<T>
270{
271 public:
272 template <typename U>
273 struct rebind
274 {
275 typedef SecureAllocator<U> other;
276 };
277
278 void deallocate(T* p, size_t n)
279 {
280 OPENSSL_cleanse(p, n);
281 return std::allocator<T>::deallocate(p, n);
282 }
283};
Willy Tu76889092022-10-17 05:33:54 +0000284
285using SecureStringBase =
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000286 std::basic_string<char, std::char_traits<char>, SecureAllocator<char>>;
Willy Tu76889092022-10-17 05:33:54 +0000287class SecureString : public SecureStringBase
288{
289 public:
290 using SecureStringBase::basic_string;
291 SecureString(const SecureStringBase& other) : SecureStringBase(other){};
292 SecureString(SecureString&) = default;
293 SecureString(const SecureString&) = default;
294 SecureString(SecureString&&) = default;
295 SecureString& operator=(SecureString&&) = default;
296 SecureString& operator=(const SecureString&) = default;
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000297
Willy Tu76889092022-10-17 05:33:54 +0000298 ~SecureString()
299 {
300 OPENSSL_cleanse(&((*this)[0]), this->size());
301 }
302};
Vernon Mauery997952a2021-07-30 14:06:14 -0700303
Willy Tu76889092022-10-17 05:33:54 +0000304using SecureBufferBase = std::vector<uint8_t, SecureAllocator<uint8_t>>;
305
306class SecureBuffer : public SecureBufferBase
307{
308 public:
309 using SecureBufferBase::vector;
310 SecureBuffer(const SecureBufferBase& other) : SecureBufferBase(other){};
311 SecureBuffer(SecureBuffer&) = default;
312 SecureBuffer(const SecureBuffer&) = default;
313 SecureBuffer(SecureBuffer&&) = default;
314 SecureBuffer& operator=(SecureBuffer&&) = default;
315 SecureBuffer& operator=(const SecureBuffer&) = default;
316
317 ~SecureBuffer()
318 {
319 OPENSSL_cleanse(&((*this)[0]), this->size());
320 }
321};
Patrick Venture0b02be92018-08-31 11:55:55 -0700322} // namespace ipmi