blob: 98d028874aa3c3f7af7fa0914a80598c0d520427 [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
Ratan Gupta00659052017-02-23 17:29:08 +05306#include <sdbusplus/server.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05007
8#include <map>
Patrick Venture0b02be92018-08-31 11:55:55 -07009#include <string>
Vernon Mauery16b86932019-05-01 08:36:11 -070010#include <variant>
Ratan Gupta00659052017-02-23 17:29:08 +053011
12namespace ipmi
13{
Ratan Guptadcb10672017-07-10 10:33:50 +053014
15using DbusObjectPath = std::string;
16using DbusService = std::string;
17using DbusInterface = std::string;
18using DbusObjectInfo = std::pair<DbusObjectPath, DbusService>;
19using DbusProperty = std::string;
Ratan Guptacc8feb42017-07-25 21:52:10 +053020
Willy Tude54f482021-01-26 15:59:09 -080021using Association = std::tuple<std::string, std::string, std::string>;
22
Jian Zhang33457102022-04-26 09:46:32 +000023using Value =
24 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
25 uint64_t, double, std::string, std::vector<uint8_t>,
26 std::vector<uint16_t>, std::vector<uint32_t>,
27 std::vector<std::string>, std::vector<Association>>;
Ratan Guptacc8feb42017-07-25 21:52:10 +053028
Ratan Guptadcb10672017-07-10 10:33:50 +053029using PropertyMap = std::map<DbusProperty, Value>;
Ratan Guptab8e99552017-07-27 07:07:48 +053030
Patrick Venture0b02be92018-08-31 11:55:55 -070031using ObjectTree =
32 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Gupta8c31d232017-08-13 05:49:43 +053033
Ratan Guptacc6cdbf2017-09-01 23:06:25 +053034using InterfaceList = std::vector<std::string>;
35
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -060036using DbusInterfaceMap = std::map<DbusInterface, PropertyMap>;
37
38using ObjectValueTree =
39 std::map<sdbusplus::message::object_path, DbusInterfaceMap>;
40
Ratan Gupta00659052017-02-23 17:29:08 +053041namespace sensor
42{
43
44using Offset = uint8_t;
Ratan Gupta00659052017-02-23 17:29:08 +053045
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -050046/**
47 * @enum SkipAssertion
48 * Matching value for skipping the update
49 */
50enum class SkipAssertion
51{
Patrick Venture0b02be92018-08-31 11:55:55 -070052 NONE, // No skip defined
53 ASSERT, // Skip on Assert
54 DEASSERT, // Skip on Deassert
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -050055};
56
Ratan Gupta00659052017-02-23 17:29:08 +053057struct Values
58{
Patrick Venture0b02be92018-08-31 11:55:55 -070059 SkipAssertion skip;
60 Value assert;
61 Value deassert;
Ratan Gupta00659052017-02-23 17:29:08 +053062};
63
Tom Joseph816e92b2017-09-06 19:23:00 +053064/**
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -050065 * @enum PreReqValues
66 * Pre-req conditions for a property.
67 */
68struct PreReqValues
69{
Patrick Venture0b02be92018-08-31 11:55:55 -070070 Value assert; // Value in case of assert.
71 Value deassert; // Value in case of deassert.
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -050072};
73
74using PreReqOffsetValueMap = std::map<Offset, PreReqValues>;
75
76/**
Tom Joseph816e92b2017-09-06 19:23:00 +053077 * @struct SetSensorReadingReq
78 *
79 * IPMI Request data for Set Sensor Reading and Event Status Command
80 */
81struct SetSensorReadingReq
82{
83 uint8_t number;
84 uint8_t operation;
85 uint8_t reading;
86 uint8_t assertOffset0_7;
87 uint8_t assertOffset8_14;
88 uint8_t deassertOffset0_7;
89 uint8_t deassertOffset8_14;
90 uint8_t eventData1;
91 uint8_t eventData2;
92 uint8_t eventData3;
93} __attribute__((packed));
94
95/**
96 * @struct GetReadingResponse
97 *
98 * IPMI response data for Get Sensor Reading command.
99 */
100struct GetReadingResponse
101{
Patrick Venture0b02be92018-08-31 11:55:55 -0700102 uint8_t reading; //!< Sensor reading.
103 uint8_t operation; //!< Sensor scanning status / reading state.
104 uint8_t assertOffset0_7; //!< Discrete assertion states(0-7).
105 uint8_t assertOffset8_14; //!< Discrete assertion states(8-14).
Tom Joseph816e92b2017-09-06 19:23:00 +0530106} __attribute__((packed));
107
108constexpr auto inventoryRoot = "/xyz/openbmc_project/inventory";
109
Sui Chen4cc42552019-09-11 10:28:35 -0700110struct GetSensorResponse
111{
112 uint8_t reading; // sensor reading
113 bool readingOrStateUnavailable; // 1 = reading/state unavailable
114 bool scanningEnabled; // 0 = sensor scanning disabled
115 bool allEventMessagesEnabled; // 0 = All Event Messages disabled
116 uint8_t thresholdLevelsStates; // threshold/discrete sensor states
117 uint8_t discreteReadingSensorStates; // discrete-only, optional states
118};
Tom Joseph816e92b2017-09-06 19:23:00 +0530119
Patrick Venture0b02be92018-08-31 11:55:55 -0700120using OffsetValueMap = std::map<Offset, Values>;
Ratan Gupta00659052017-02-23 17:29:08 +0530121
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500122using DbusPropertyValues = std::pair<PreReqOffsetValueMap, OffsetValueMap>;
Ratan Gupta00659052017-02-23 17:29:08 +0530123
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500124using DbusPropertyMap = std::map<DbusProperty, DbusPropertyValues>;
125
126using DbusInterfaceMap = std::map<DbusInterface, DbusPropertyMap>;
Ratan Gupta00659052017-02-23 17:29:08 +0530127
128using InstancePath = std::string;
129using Type = uint8_t;
130using ReadingType = uint8_t;
Emily Shaffer10f49592017-05-10 12:01:10 -0700131using Multiplier = uint16_t;
Tom Joseph0a1301c2018-02-16 08:27:00 +0530132using OffsetB = int16_t;
133using Exponent = int8_t;
Emily Shaffer40011912018-10-09 12:06:04 -0700134using ScaledOffset = double;
Emily Shaffer62235612017-06-14 13:06:26 -0700135using Scale = int16_t;
136using Unit = std::string;
Tom Josephb0adbcd2018-01-24 11:51:29 +0530137using EntityType = uint8_t;
138using EntityInst = uint8_t;
139using SensorName = std::string;
Tony Leec5324252019-10-31 17:24:16 +0800140using SensorUnits1 = uint8_t;
Ratan Gupta00659052017-02-23 17:29:08 +0530141
Emily Shaffercc941e12017-06-14 13:06:26 -0700142enum class Mutability
143{
Patrick Venture0b02be92018-08-31 11:55:55 -0700144 Read = 1 << 0,
145 Write = 1 << 1,
Emily Shaffercc941e12017-06-14 13:06:26 -0700146};
147
148inline Mutability operator|(Mutability lhs, Mutability rhs)
149{
Patrick Venture0b02be92018-08-31 11:55:55 -0700150 return static_cast<Mutability>(static_cast<uint8_t>(lhs) |
151 static_cast<uint8_t>(rhs));
Emily Shaffercc941e12017-06-14 13:06:26 -0700152}
153
154inline Mutability operator&(Mutability lhs, Mutability rhs)
155{
Patrick Venture0b02be92018-08-31 11:55:55 -0700156 return static_cast<Mutability>(static_cast<uint8_t>(lhs) &
157 static_cast<uint8_t>(rhs));
Emily Shaffercc941e12017-06-14 13:06:26 -0700158}
159
Ratan Gupta00659052017-02-23 17:29:08 +0530160struct Info
161{
Patrick Venture0b02be92018-08-31 11:55:55 -0700162 EntityType entityType;
163 EntityInst instance;
164 Type sensorType;
165 InstancePath sensorPath;
166 DbusInterface sensorInterface;
167 ReadingType sensorReadingType;
168 Multiplier coefficientM;
169 OffsetB coefficientB;
170 Exponent exponentB;
171 ScaledOffset scaledOffset;
172 Exponent exponentR;
173 bool hasScale;
174 Scale scale;
Tony Leec5324252019-10-31 17:24:16 +0800175 SensorUnits1 sensorUnits1;
Patrick Venture0b02be92018-08-31 11:55:55 -0700176 Unit unit;
177 std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc;
Lei YU8c2c0482021-09-16 17:28:28 +0800178#ifndef FEATURE_SENSORS_CACHE
Patrick Venture0b02be92018-08-31 11:55:55 -0700179 std::function<GetSensorResponse(const Info&)> getFunc;
Lei YU8c2c0482021-09-16 17:28:28 +0800180#else
Lei YU8e8152c2021-12-06 20:11:08 +0800181 std::function<std::optional<GetSensorResponse>(uint8_t, const Info&,
182 const ipmi::PropertyMap&)>
Lei YU8c2c0482021-09-16 17:28:28 +0800183 getFunc;
184#endif
Patrick Venture0b02be92018-08-31 11:55:55 -0700185 Mutability mutability;
Jeremy Kerrbe4ffa82020-08-10 16:17:37 +0800186 SensorName sensorName;
Patrick Venture0b02be92018-08-31 11:55:55 -0700187 std::function<SensorName(const Info&)> sensorNameFunc;
188 DbusInterfaceMap propertyInterfaces;
Ratan Gupta00659052017-02-23 17:29:08 +0530189};
190
191using Id = uint8_t;
Patrick Venture0b02be92018-08-31 11:55:55 -0700192using IdInfoMap = std::map<Id, Info>;
Ratan Gupta00659052017-02-23 17:29:08 +0530193
Ratan Guptadcb10672017-07-10 10:33:50 +0530194using PropertyMap = ipmi::PropertyMap;
Tom Josephbe703f72017-03-09 12:34:35 +0530195
196using InterfaceMap = std::map<DbusInterface, PropertyMap>;
197
198using Object = sdbusplus::message::object_path;
199using ObjectMap = std::map<Object, InterfaceMap>;
200
Patrick Williams5d82f472022-07-22 19:26:53 -0500201using IpmiUpdateData = sdbusplus::message_t;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500202
Tom Josephdd78a1d2017-05-05 11:04:29 +0530203struct SelData
204{
Patrick Venture0b02be92018-08-31 11:55:55 -0700205 Id sensorID;
206 Type sensorType;
207 ReadingType eventReadingType;
208 Offset eventOffset;
Tom Josephdd78a1d2017-05-05 11:04:29 +0530209};
210
211using InventoryPath = std::string;
212
213using InvObjectIDMap = std::map<InventoryPath, SelData>;
214
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600215enum class ThresholdMask
216{
217 NON_CRITICAL_LOW_MASK = 0x01,
218 CRITICAL_LOW_MASK = 0x02,
219 NON_CRITICAL_HIGH_MASK = 0x08,
220 CRITICAL_HIGH_MASK = 0x10,
221};
222
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800223static constexpr uint8_t maxContainedEntities = 4;
224using ContainedEntitiesArray =
225 std::array<std::pair<uint8_t, uint8_t>, maxContainedEntities>;
226
227struct EntityInfo
228{
229 uint8_t containerEntityId;
230 uint8_t containerEntityInstance;
231 bool isList;
232 bool isLinked;
233 ContainedEntitiesArray containedEntities;
234};
235
236using EntityInfoMap = std::map<Id, EntityInfo>;
237
Lei YU97140502021-09-17 13:49:43 +0800238#ifdef FEATURE_SENSORS_CACHE
239/**
240 * @struct SensorData
241 *
242 * The data to cache for sensors
243 */
244struct SensorData
245{
246 double value;
247 bool available;
248 bool functional;
249 GetSensorResponse response;
250};
251
252using SensorCacheMap = std::map<uint8_t, std::optional<SensorData>>;
253#endif
254
Patrick Venture0b02be92018-08-31 11:55:55 -0700255} // namespace sensor
Ratan Gupta8c31d232017-08-13 05:49:43 +0530256
257namespace network
258{
Ratan Gupta8c31d232017-08-13 05:49:43 +0530259constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
Ratan Gupta8c31d232017-08-13 05:49:43 +0530260
261constexpr auto IPV4_ADDRESS_SIZE_BYTE = 4;
262constexpr auto IPV6_ADDRESS_SIZE_BYTE = 16;
263
264constexpr auto DEFAULT_MAC_ADDRESS = "00:00:00:00:00:00";
265constexpr auto DEFAULT_ADDRESS = "0.0.0.0";
266
Patrick Venture0b02be92018-08-31 11:55:55 -0700267} // namespace network
William A. Kennington IIIc514d872019-04-06 18:19:38 -0700268
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000269template <typename T>
270class SecureAllocator : public std::allocator<T>
271{
272 public:
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000273 void deallocate(T* p, size_t n)
274 {
275 OPENSSL_cleanse(p, n);
276 return std::allocator<T>::deallocate(p, n);
277 }
278};
Willy Tu76889092022-10-17 05:33:54 +0000279
280using SecureStringBase =
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000281 std::basic_string<char, std::char_traits<char>, SecureAllocator<char>>;
Willy Tu76889092022-10-17 05:33:54 +0000282class SecureString : public SecureStringBase
283{
284 public:
285 using SecureStringBase::basic_string;
286 SecureString(const SecureStringBase& other) : SecureStringBase(other){};
287 SecureString(SecureString&) = default;
288 SecureString(const SecureString&) = default;
289 SecureString(SecureString&&) = default;
290 SecureString& operator=(SecureString&&) = default;
291 SecureString& operator=(const SecureString&) = default;
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +0000292
Willy Tu76889092022-10-17 05:33:54 +0000293 ~SecureString()
294 {
Willy Tu45d41932022-10-17 19:49:32 +0000295 OPENSSL_cleanse(this->data(), this->size());
Willy Tu76889092022-10-17 05:33:54 +0000296 }
297};
Vernon Mauery997952a2021-07-30 14:06:14 -0700298
Willy Tu76889092022-10-17 05:33:54 +0000299using SecureBufferBase = std::vector<uint8_t, SecureAllocator<uint8_t>>;
300
301class SecureBuffer : public SecureBufferBase
302{
303 public:
304 using SecureBufferBase::vector;
305 SecureBuffer(const SecureBufferBase& other) : SecureBufferBase(other){};
306 SecureBuffer(SecureBuffer&) = default;
307 SecureBuffer(const SecureBuffer&) = default;
308 SecureBuffer(SecureBuffer&&) = default;
309 SecureBuffer& operator=(SecureBuffer&&) = default;
310 SecureBuffer& operator=(const SecureBuffer&) = default;
311
312 ~SecureBuffer()
313 {
Willy Tu45d41932022-10-17 19:49:32 +0000314 OPENSSL_cleanse(this->data(), this->size());
Willy Tu76889092022-10-17 05:33:54 +0000315 }
316};
Patrick Venture0b02be92018-08-31 11:55:55 -0700317} // namespace ipmi