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