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