Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Snehalatha Venkatesh | 745164c | 2021-06-25 10:02:25 +0000 | [diff] [blame] | 3 | #include <openssl/crypto.h> |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | |
| 6 | #include <map> |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 7 | #include <sdbusplus/server.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 8 | #include <string> |
Vernon Mauery | 16b8693 | 2019-05-01 08:36:11 -0700 | [diff] [blame] | 9 | #include <variant> |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 10 | |
| 11 | namespace ipmi |
| 12 | { |
Ratan Gupta | dcb1067 | 2017-07-10 10:33:50 +0530 | [diff] [blame] | 13 | |
| 14 | using DbusObjectPath = std::string; |
| 15 | using DbusService = std::string; |
| 16 | using DbusInterface = std::string; |
| 17 | using DbusObjectInfo = std::pair<DbusObjectPath, DbusService>; |
| 18 | using DbusProperty = std::string; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 19 | |
Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 20 | using Association = std::tuple<std::string, std::string, std::string>; |
| 21 | |
Jian Zhang | 3345710 | 2022-04-26 09:46:32 +0000 | [diff] [blame] | 22 | using 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 Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 27 | |
Ratan Gupta | dcb1067 | 2017-07-10 10:33:50 +0530 | [diff] [blame] | 28 | using PropertyMap = std::map<DbusProperty, Value>; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 29 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 30 | using ObjectTree = |
| 31 | std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; |
Ratan Gupta | 8c31d23 | 2017-08-13 05:49:43 +0530 | [diff] [blame] | 32 | |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 33 | using InterfaceList = std::vector<std::string>; |
| 34 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 35 | using DbusInterfaceMap = std::map<DbusInterface, PropertyMap>; |
| 36 | |
| 37 | using ObjectValueTree = |
| 38 | std::map<sdbusplus::message::object_path, DbusInterfaceMap>; |
| 39 | |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 40 | namespace sensor |
| 41 | { |
| 42 | |
| 43 | using Offset = uint8_t; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 44 | |
Dhruvaraj Subhashchandran | e84841c | 2017-08-22 07:40:27 -0500 | [diff] [blame] | 45 | /** |
| 46 | * @enum SkipAssertion |
| 47 | * Matching value for skipping the update |
| 48 | */ |
| 49 | enum class SkipAssertion |
| 50 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 51 | NONE, // No skip defined |
| 52 | ASSERT, // Skip on Assert |
| 53 | DEASSERT, // Skip on Deassert |
Dhruvaraj Subhashchandran | e84841c | 2017-08-22 07:40:27 -0500 | [diff] [blame] | 54 | }; |
| 55 | |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 56 | struct Values |
| 57 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 58 | SkipAssertion skip; |
| 59 | Value assert; |
| 60 | Value deassert; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 61 | }; |
| 62 | |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 63 | /** |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 64 | * @enum PreReqValues |
| 65 | * Pre-req conditions for a property. |
| 66 | */ |
| 67 | struct PreReqValues |
| 68 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 69 | Value assert; // Value in case of assert. |
| 70 | Value deassert; // Value in case of deassert. |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | using PreReqOffsetValueMap = std::map<Offset, PreReqValues>; |
| 74 | |
| 75 | /** |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 76 | * @struct SetSensorReadingReq |
| 77 | * |
| 78 | * IPMI Request data for Set Sensor Reading and Event Status Command |
| 79 | */ |
| 80 | struct 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 | */ |
| 99 | struct GetReadingResponse |
| 100 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 101 | 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 Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 105 | } __attribute__((packed)); |
| 106 | |
| 107 | constexpr auto inventoryRoot = "/xyz/openbmc_project/inventory"; |
| 108 | |
Sui Chen | 4cc4255 | 2019-09-11 10:28:35 -0700 | [diff] [blame] | 109 | struct 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 Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 118 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 119 | using OffsetValueMap = std::map<Offset, Values>; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 120 | |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 121 | using DbusPropertyValues = std::pair<PreReqOffsetValueMap, OffsetValueMap>; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 122 | |
Dhruvaraj Subhashchandran | e245e4e | 2017-10-03 03:58:05 -0500 | [diff] [blame] | 123 | using DbusPropertyMap = std::map<DbusProperty, DbusPropertyValues>; |
| 124 | |
| 125 | using DbusInterfaceMap = std::map<DbusInterface, DbusPropertyMap>; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 126 | |
| 127 | using InstancePath = std::string; |
| 128 | using Type = uint8_t; |
| 129 | using ReadingType = uint8_t; |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 130 | using Multiplier = uint16_t; |
Tom Joseph | 0a1301c | 2018-02-16 08:27:00 +0530 | [diff] [blame] | 131 | using OffsetB = int16_t; |
| 132 | using Exponent = int8_t; |
Emily Shaffer | 4001191 | 2018-10-09 12:06:04 -0700 | [diff] [blame] | 133 | using ScaledOffset = double; |
Emily Shaffer | 6223561 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 134 | using Scale = int16_t; |
| 135 | using Unit = std::string; |
Tom Joseph | b0adbcd | 2018-01-24 11:51:29 +0530 | [diff] [blame] | 136 | using EntityType = uint8_t; |
| 137 | using EntityInst = uint8_t; |
| 138 | using SensorName = std::string; |
Tony Lee | c532425 | 2019-10-31 17:24:16 +0800 | [diff] [blame] | 139 | using SensorUnits1 = uint8_t; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 140 | |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 141 | enum class Mutability |
| 142 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 143 | Read = 1 << 0, |
| 144 | Write = 1 << 1, |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | inline Mutability operator|(Mutability lhs, Mutability rhs) |
| 148 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 149 | return static_cast<Mutability>(static_cast<uint8_t>(lhs) | |
| 150 | static_cast<uint8_t>(rhs)); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | inline Mutability operator&(Mutability lhs, Mutability rhs) |
| 154 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 155 | return static_cast<Mutability>(static_cast<uint8_t>(lhs) & |
| 156 | static_cast<uint8_t>(rhs)); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 159 | struct Info |
| 160 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 161 | 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 Lee | c532425 | 2019-10-31 17:24:16 +0800 | [diff] [blame] | 174 | SensorUnits1 sensorUnits1; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 175 | Unit unit; |
| 176 | std::function<uint8_t(SetSensorReadingReq&, const Info&)> updateFunc; |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 177 | #ifndef FEATURE_SENSORS_CACHE |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 178 | std::function<GetSensorResponse(const Info&)> getFunc; |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 179 | #else |
Lei YU | 8e8152c | 2021-12-06 20:11:08 +0800 | [diff] [blame] | 180 | std::function<std::optional<GetSensorResponse>(uint8_t, const Info&, |
| 181 | const ipmi::PropertyMap&)> |
Lei YU | 8c2c048 | 2021-09-16 17:28:28 +0800 | [diff] [blame] | 182 | getFunc; |
| 183 | #endif |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 184 | Mutability mutability; |
Jeremy Kerr | be4ffa8 | 2020-08-10 16:17:37 +0800 | [diff] [blame] | 185 | SensorName sensorName; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 186 | std::function<SensorName(const Info&)> sensorNameFunc; |
| 187 | DbusInterfaceMap propertyInterfaces; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | using Id = uint8_t; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 191 | using IdInfoMap = std::map<Id, Info>; |
Ratan Gupta | 0065905 | 2017-02-23 17:29:08 +0530 | [diff] [blame] | 192 | |
Ratan Gupta | dcb1067 | 2017-07-10 10:33:50 +0530 | [diff] [blame] | 193 | using PropertyMap = ipmi::PropertyMap; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 194 | |
| 195 | using InterfaceMap = std::map<DbusInterface, PropertyMap>; |
| 196 | |
| 197 | using Object = sdbusplus::message::object_path; |
| 198 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 199 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 200 | using IpmiUpdateData = sdbusplus::message_t; |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 201 | |
Tom Joseph | dd78a1d | 2017-05-05 11:04:29 +0530 | [diff] [blame] | 202 | struct SelData |
| 203 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 204 | Id sensorID; |
| 205 | Type sensorType; |
| 206 | ReadingType eventReadingType; |
| 207 | Offset eventOffset; |
Tom Joseph | dd78a1d | 2017-05-05 11:04:29 +0530 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | using InventoryPath = std::string; |
| 211 | |
| 212 | using InvObjectIDMap = std::map<InventoryPath, SelData>; |
| 213 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 214 | enum 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 Natarajan | 9c11894 | 2019-02-12 13:22:55 -0800 | [diff] [blame] | 222 | static constexpr uint8_t maxContainedEntities = 4; |
| 223 | using ContainedEntitiesArray = |
| 224 | std::array<std::pair<uint8_t, uint8_t>, maxContainedEntities>; |
| 225 | |
| 226 | struct EntityInfo |
| 227 | { |
| 228 | uint8_t containerEntityId; |
| 229 | uint8_t containerEntityInstance; |
| 230 | bool isList; |
| 231 | bool isLinked; |
| 232 | ContainedEntitiesArray containedEntities; |
| 233 | }; |
| 234 | |
| 235 | using EntityInfoMap = std::map<Id, EntityInfo>; |
| 236 | |
Lei YU | 9714050 | 2021-09-17 13:49:43 +0800 | [diff] [blame] | 237 | #ifdef FEATURE_SENSORS_CACHE |
| 238 | /** |
| 239 | * @struct SensorData |
| 240 | * |
| 241 | * The data to cache for sensors |
| 242 | */ |
| 243 | struct SensorData |
| 244 | { |
| 245 | double value; |
| 246 | bool available; |
| 247 | bool functional; |
| 248 | GetSensorResponse response; |
| 249 | }; |
| 250 | |
| 251 | using SensorCacheMap = std::map<uint8_t, std::optional<SensorData>>; |
| 252 | #endif |
| 253 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 254 | } // namespace sensor |
Ratan Gupta | 8c31d23 | 2017-08-13 05:49:43 +0530 | [diff] [blame] | 255 | |
| 256 | namespace network |
| 257 | { |
Ratan Gupta | 8c31d23 | 2017-08-13 05:49:43 +0530 | [diff] [blame] | 258 | constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"; |
Ratan Gupta | 8c31d23 | 2017-08-13 05:49:43 +0530 | [diff] [blame] | 259 | |
| 260 | constexpr auto IPV4_ADDRESS_SIZE_BYTE = 4; |
| 261 | constexpr auto IPV6_ADDRESS_SIZE_BYTE = 16; |
| 262 | |
| 263 | constexpr auto DEFAULT_MAC_ADDRESS = "00:00:00:00:00:00"; |
| 264 | constexpr auto DEFAULT_ADDRESS = "0.0.0.0"; |
| 265 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 266 | } // namespace network |
William A. Kennington III | c514d87 | 2019-04-06 18:19:38 -0700 | [diff] [blame] | 267 | |
Snehalatha Venkatesh | 745164c | 2021-06-25 10:02:25 +0000 | [diff] [blame] | 268 | template <typename T> |
| 269 | class SecureAllocator : public std::allocator<T> |
| 270 | { |
| 271 | public: |
Snehalatha Venkatesh | 745164c | 2021-06-25 10:02:25 +0000 | [diff] [blame] | 272 | void deallocate(T* p, size_t n) |
| 273 | { |
| 274 | OPENSSL_cleanse(p, n); |
| 275 | return std::allocator<T>::deallocate(p, n); |
| 276 | } |
| 277 | }; |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 278 | |
| 279 | using SecureStringBase = |
Snehalatha Venkatesh | 745164c | 2021-06-25 10:02:25 +0000 | [diff] [blame] | 280 | std::basic_string<char, std::char_traits<char>, SecureAllocator<char>>; |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 281 | class SecureString : public SecureStringBase |
| 282 | { |
| 283 | public: |
| 284 | using SecureStringBase::basic_string; |
| 285 | SecureString(const SecureStringBase& other) : SecureStringBase(other){}; |
| 286 | SecureString(SecureString&) = default; |
| 287 | SecureString(const SecureString&) = default; |
| 288 | SecureString(SecureString&&) = default; |
| 289 | SecureString& operator=(SecureString&&) = default; |
| 290 | SecureString& operator=(const SecureString&) = default; |
Snehalatha Venkatesh | 745164c | 2021-06-25 10:02:25 +0000 | [diff] [blame] | 291 | |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 292 | ~SecureString() |
| 293 | { |
Willy Tu | 45d4193 | 2022-10-17 19:49:32 +0000 | [diff] [blame] | 294 | OPENSSL_cleanse(this->data(), this->size()); |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 295 | } |
| 296 | }; |
Vernon Mauery | 997952a | 2021-07-30 14:06:14 -0700 | [diff] [blame] | 297 | |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 298 | using SecureBufferBase = std::vector<uint8_t, SecureAllocator<uint8_t>>; |
| 299 | |
| 300 | class SecureBuffer : public SecureBufferBase |
| 301 | { |
| 302 | public: |
| 303 | using SecureBufferBase::vector; |
| 304 | SecureBuffer(const SecureBufferBase& other) : SecureBufferBase(other){}; |
| 305 | SecureBuffer(SecureBuffer&) = default; |
| 306 | SecureBuffer(const SecureBuffer&) = default; |
| 307 | SecureBuffer(SecureBuffer&&) = default; |
| 308 | SecureBuffer& operator=(SecureBuffer&&) = default; |
| 309 | SecureBuffer& operator=(const SecureBuffer&) = default; |
| 310 | |
| 311 | ~SecureBuffer() |
| 312 | { |
Willy Tu | 45d4193 | 2022-10-17 19:49:32 +0000 | [diff] [blame] | 313 | OPENSSL_cleanse(this->data(), this->size()); |
Willy Tu | 7688909 | 2022-10-17 05:33:54 +0000 | [diff] [blame] | 314 | } |
| 315 | }; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 316 | } // namespace ipmi |