blob: cdc539b8fe6a4d5e7e18a1521a9c23fde8acc195 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
Sampa Misraaea5dde2020-08-31 08:33:47 -05003#include "types.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/base.h>
6#include <libpldm/bios.h>
George Liudf9a6d32020-12-22 16:27:16 +08007#include <libpldm/entity.h>
8#include <libpldm/pdr.h>
George Liuc453e162022-12-21 17:16:23 +08009#include <libpldm/platform.h>
10#include <libpldm/utils.h>
Sampa Misra032bd502019-03-06 05:03:22 -060011#include <stdint.h>
12#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060014
George Liu6492f522020-06-16 10:34:05 +080015#include <nlohmann/json.hpp>
16#include <sdbusplus/server.hpp>
17#include <xyz/openbmc_project/Logging/Entry/server.hpp>
18
George Liudf9a6d32020-12-22 16:27:16 +080019#include <deque>
Sampa Misraa2fa0702019-05-31 01:28:55 -050020#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050021#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060022#include <iostream>
George Liudf9a6d32020-12-22 16:27:16 +080023#include <map>
Sampa Misra032bd502019-03-06 05:03:22 -060024#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050025#include <variant>
26#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060027
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +000028constexpr uint64_t dbusTimeout =
29 std::chrono::duration_cast<std::chrono::microseconds>(
30 std::chrono::seconds(DBUS_TIMEOUT))
31 .count();
32
Sampa Misra032bd502019-03-06 05:03:22 -060033namespace pldm
34{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035namespace utils
36{
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050037namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053038using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053039constexpr bool Tx = true;
40constexpr bool Rx = false;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050041
George Liudf9a6d32020-12-22 16:27:16 +080042using EntityName = std::string;
43using EntityType = uint16_t;
44
45using Entities = std::vector<pldm_entity_node*>;
46using EntityAssociations = std::vector<Entities>;
47using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
48
49const std::map<EntityType, EntityName> entityMaps = {
50 {PLDM_ENTITY_SYSTEM_CHASSIS, "chassis"},
51 {PLDM_ENTITY_BOARD, "io_board"},
52 {PLDM_ENTITY_SYS_BOARD, "motherboard"},
53 {PLDM_ENTITY_POWER_SUPPLY, "powersupply"},
54 {PLDM_ENTITY_PROC, "cpu"},
55 {PLDM_ENTITY_SYSTEM_CHASSIS | 0x8000, "system"},
56 {PLDM_ENTITY_PROC_MODULE, "dcm"},
57 {PLDM_ENTITY_PROC | 0x8000, "core"},
58 {PLDM_ENTITY_IO_MODULE, "io_module"},
59 {PLDM_ENTITY_FAN, "fan"},
60 {PLDM_ENTITY_SYS_MGMT_MODULE, "system_management_module"},
61 {PLDM_ENTITY_POWER_CONVERTER, "power_converter"},
62 {PLDM_ENTITY_SLOT, "slot"},
63 {PLDM_ENTITY_CONNECTOR, "connector"}};
64
65/** @brief Vector a entity name to pldm_entity from entity association tree
66 * @param[in] entityAssoc - Vector of associated pldm entities
67 * @param[in] entityTree - entity association tree
68 * @param[out] objPathMap - maps an object path to pldm_entity from the
69 * BMC's entity association tree
70 * @return
71 */
72void updateEntityAssociation(const EntityAssociations& entityAssoc,
73 pldm_entity_association_tree* entityTree,
74 ObjectPathMaps& objPathMap);
75
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050076/** @struct CustomFD
77 *
78 * RAII wrapper for file descriptor.
79 */
80struct CustomFD
81{
82 CustomFD(const CustomFD&) = delete;
83 CustomFD& operator=(const CustomFD&) = delete;
84 CustomFD(CustomFD&&) = delete;
85 CustomFD& operator=(CustomFD&&) = delete;
86
Patrick Williams6da4f912023-05-10 07:50:53 -050087 CustomFD(int fd) : fd(fd) {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050088
89 ~CustomFD()
90 {
91 if (fd >= 0)
92 {
93 close(fd);
94 }
95 }
96
97 int operator()() const
98 {
99 return fd;
100 }
101
102 private:
103 int fd = -1;
104};
105
Sampa Misrab37be312019-07-03 02:26:41 -0500106/** @brief Calculate the pad for PLDM data
107 *
108 * @param[in] data - Length of the data
109 * @return - uint8_t - number of pad bytes
110 */
111uint8_t getNumPadBytes(uint32_t data);
112
George Liu83409572019-12-24 18:42:54 +0800113/** @brief Convert uint64 to date
114 *
115 * @param[in] data - time date of uint64
116 * @param[out] year - year number in dec
117 * @param[out] month - month number in dec
118 * @param[out] day - day of the month in dec
119 * @param[out] hour - number of hours in dec
120 * @param[out] min - number of minutes in dec
121 * @param[out] sec - number of seconds in dec
122 * @return true if decode success, false if decode faild
123 */
124bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
125 uint8_t* hour, uint8_t* min, uint8_t* sec);
126
127/** @brief Convert effecter data to structure of set_effecter_state_field
128 *
129 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +0800130 * @param[in] effecterCount - the number of individual sets of effecter
131 * information
132 * @return[out] parse success and get a valid set_effecter_state_field
133 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +0800134 */
George Liuba4c1fb2020-02-05 14:13:30 +0800135std::optional<std::vector<set_effecter_state_field>>
136 parseEffecterData(const std::vector<uint8_t>& effecterData,
137 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -0600138
139/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600140 * @brief creates an error log
141 * @param[in] errorMsg - the error message
142 */
143void reportError(const char* errorMsg);
144
Sampa Misra032bd502019-03-06 05:03:22 -0600145/** @brief Convert any Decimal number to BCD
146 *
147 * @tparam[in] decimal - Decimal number
148 * @return Corresponding BCD number
149 */
150template <typename T>
151T decimalToBcd(T decimal)
152{
153 T bcd = 0;
154 T rem = 0;
155 auto cnt = 0;
156
157 while (decimal)
158 {
159 rem = decimal % 10;
160 bcd = bcd + (rem << cnt);
161 decimal = decimal / 10;
162 cnt += 4;
163 }
164
165 return bcd;
166}
167
Sampa Misraa2fa0702019-05-31 01:28:55 -0500168constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500169constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500170
George Liu1e44c732020-02-28 20:20:06 +0800171struct DBusMapping
172{
173 std::string objectPath; //!< D-Bus object path
174 std::string interface; //!< D-Bus interface
175 std::string propertyName; //!< D-Bus property name
176 std::string propertyType; //!< D-Bus property type
177};
178
179using PropertyValue =
180 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500181 uint64_t, double, std::string, std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500182using DbusProp = std::string;
183using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500184using DBusInterfaceAdded = std::vector<
185 std::pair<pldm::dbus::Interface,
186 std::vector<std::pair<pldm::dbus::Property,
187 std::variant<pldm::dbus::Property>>>>>;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530188using ObjectPath = std::string;
189using ServiceName = std::string;
190using Interfaces = std::vector<std::string>;
191using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
192using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500193using PropertyMap = std::map<std::string, PropertyValue>;
194using InterfaceMap = std::map<std::string, PropertyMap>;
George Liu1e44c732020-02-28 20:20:06 +0800195
196/**
197 * @brief The interface for DBusHandler
198 */
199class DBusHandlerInterface
200{
201 public:
202 virtual ~DBusHandlerInterface() = default;
203
George Liu36e81352020-07-01 14:40:30 +0800204 virtual std::string getService(const char* path,
205 const char* interface) const = 0;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530206 virtual GetSubTreeResponse
207 getSubtree(const std::string& path, int depth,
208 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800209
George Liu1e44c732020-02-28 20:20:06 +0800210 virtual void setDbusProperty(const DBusMapping& dBusMap,
211 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800212
213 virtual PropertyValue
214 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
215 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800216};
217
Sampa Misraa2fa0702019-05-31 01:28:55 -0500218/**
219 * @class DBusHandler
220 *
221 * Wrapper class to handle the D-Bus calls
222 *
223 * This class contains the APIs to handle the D-Bus calls
224 * to cater the request from pldm requester.
225 * A class is created to mock the apis in the test cases
226 */
George Liu1e44c732020-02-28 20:20:06 +0800227class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500228{
229 public:
George Liu0e02c322020-01-01 09:41:51 +0800230 /** @brief Get the bus connection. */
231 static auto& getBus()
232 {
233 static auto bus = sdbusplus::bus::new_default();
234 return bus;
235 }
236
237 /**
238 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800239 *
George Liu0e02c322020-01-01 09:41:51 +0800240 * @param[in] path - DBUS object path
241 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800242 *
George Liu0e02c322020-01-01 09:41:51 +0800243 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800244 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500245 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800246 */
George Liu36e81352020-07-01 14:40:30 +0800247 std::string getService(const char* path,
248 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800249
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530250 /**
251 * @brief Get the Subtree response from the mapper
252 *
253 * @param[in] path - DBUS object path
254 * @param[in] depth - Search depth
255 * @param[in] ifaceList - list of the interface that are being
256 * queried from the mapper
257 *
258 * @return GetSubTreeResponse - the mapper subtree response
259 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500260 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530261 */
262 GetSubTreeResponse
263 getSubtree(const std::string& path, int depth,
264 const std::vector<std::string>& ifaceList) const override;
265
John Wang9e242422020-03-05 08:37:50 +0800266 /** @brief Get property(type: variant) from the requested dbus
267 *
268 * @param[in] objPath - The Dbus object path
269 * @param[in] dbusProp - The property name to get
270 * @param[in] dbusInterface - The Dbus interface
271 *
272 * @return The value of the property(type: variant)
273 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500274 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800275 */
276 PropertyValue
277 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
278 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800279
John Wang9e242422020-03-05 08:37:50 +0800280 /** @brief The template function to get property from the requested dbus
281 * path
282 *
283 * @tparam Property - Excepted type of the property on dbus
284 *
285 * @param[in] objPath - The Dbus object path
286 * @param[in] dbusProp - The property name to get
287 * @param[in] dbusInterface - The Dbus interface
288 *
289 * @return The value of the property
290 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500291 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800292 * std::bad_variant_access when \p Property and property on dbus do
293 * not match
294 */
John Wang92b3c972019-10-17 11:06:41 +0800295 template <typename Property>
296 auto getDbusProperty(const char* objPath, const char* dbusProp,
297 const char* dbusInterface)
298 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500299 auto VariantValue = getDbusPropertyVariant(objPath, dbusProp,
300 dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800301 return std::get<Property>(VariantValue);
302 }
George Liu1e44c732020-02-28 20:20:06 +0800303
304 /** @brief Set Dbus property
305 *
306 * @param[in] dBusMap - Object path, property name, interface and property
307 * type for the D-Bus object
308 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800309 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500310 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800311 */
312 void setDbusProperty(const DBusMapping& dBusMap,
313 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500314};
315
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500316/** @brief Fetch parent D-Bus object based on pathname
317 *
318 * @param[in] dbusObj - child D-Bus object
319 *
320 * @return std::string - the parent D-Bus object path
321 */
322inline std::string findParent(const std::string& dbusObj)
323{
324 fs::path p(dbusObj);
325 return p.parent_path().string();
326}
327
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500328/** @brief Read (static) MCTP EID of host firmware from a file
329 *
330 * @return uint8_t - MCTP EID
331 */
332uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530333
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530334/** @brief Convert a value in the JSON to a D-Bus property value
335 *
336 * @param[in] type - type of the D-Bus property
337 * @param[in] value - value in the JSON file
338 *
339 * @return PropertyValue - the D-Bus property value
340 */
341PropertyValue jsonEntryToDbusVal(std::string_view type,
342 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500343
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500344/** @brief Find State Effecter PDR
345 * @param[in] tid - PLDM terminus ID.
346 * @param[in] entityID - entity that can be associated with PLDM State set.
347 * @param[in] stateSetId - value that identifies PLDM State set.
348 * @param[in] repo - pointer to BMC's primary PDR repo.
349 * @return array[array[uint8_t]] - StateEffecterPDRs
350 */
351std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
352 uint16_t entityID,
353 uint16_t stateSetId,
354 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800355/** @brief Find State Sensor PDR
356 * @param[in] tid - PLDM terminus ID.
357 * @param[in] entityID - entity that can be associated with PLDM State set.
358 * @param[in] stateSetId - value that identifies PLDM State set.
359 * @param[in] repo - pointer to BMC's primary PDR repo.
360 * @return array[array[uint8_t]] - StateSensorPDRs
361 */
362std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
363 uint16_t entityID,
364 uint16_t stateSetId,
365 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500366
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500367/** @brief Find sensor id from a state sensor PDR
368 *
369 * @param[in] pdrRepo - PDR repository
370 * @param[in] tid - terminus id
371 * @param[in] entityType - entity type
372 * @param[in] entityInstance - entity instance number
373 * @param[in] containerId - container id
374 * @param[in] stateSetId - state set id
375 *
376 * @return uint16_t - the sensor id
377 */
378uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
379 uint16_t entityType, uint16_t entityInstance,
380 uint16_t containerId, uint16_t stateSetId);
381
Tom Joseph250c4752020-04-15 10:32:45 +0530382/** @brief Find effecter id from a state effecter pdr
383 * @param[in] pdrRepo - PDR repository
384 * @param[in] entityType - entity type
385 * @param[in] entityInstance - entity instance number
386 * @param[in] containerId - container id
387 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500388 * @param[in] localOrRemote - true for checking local repo and false for remote
389 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530390 *
391 * @return uint16_t - the effecter id
392 */
393uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
394 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500395 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530396
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800397/** @brief Emit the sensor event signal
398 *
399 * @param[in] tid - the terminus id
400 * @param[in] sensorId - sensorID value of the sensor
401 * @param[in] sensorOffset - Identifies which state sensor within a
402 * composite state sensor the event is being returned for
403 * @param[in] eventState - The event state value from the state change that
404 * triggered the event message
405 * @param[in] previousEventState - The event state value for the state from
406 * which the present event state was entered.
407 * @return PLDM completion code
408 */
409int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
410 uint8_t sensorOffset, uint8_t eventState,
411 uint8_t previousEventState);
412
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600413/** @brief Print the buffer
414 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530415 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
416 the buffer is an incoming PLDM message
417 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600418 *
419 * @return - None
420 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530421void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600422
Tom Joseph54922072021-06-19 02:45:46 -0700423/** @brief Convert the buffer to std::string
424 *
425 * If there are characters that are not printable characters, it is replaced
426 * with space(0x20).
427 *
428 * @param[in] var - pointer to data and length of the data
429 *
430 * @return std::string equivalent of variable field
431 */
432std::string toString(const struct variable_field& var);
433
George Liu872f0f62021-11-25 16:26:16 +0800434/** @brief Split strings according to special identifiers
435 *
436 * We can split the string according to the custom identifier(';', ',', '&' or
437 * others) and store it to vector.
438 *
439 * @param[in] srcStr - The string to be split
440 * @param[in] delim - The custom identifier
441 * @param[in] trimStr - The first and last string to be trimmed
442 *
443 * @return std::vector<std::string> Vectors are used to store strings
444 */
445std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
446 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530447/** @brief Get the current system time in readable format
448 *
449 * @return - std::string equivalent of the system time
450 */
451std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800452
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500453/** @brief checks if the FRU is actually present.
454 * @param[in] objPath - FRU object path.
455 *
456 * @return bool to indicate presence or absence of FRU.
457 */
458bool checkForFruPresence(const std::string& objPath);
459
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600460/** @brief Method to check if the logical bit is set
461 *
462 * @param[containerId] - container id of the entity
463 *
464 * @return true or false based on the logic bit set
465 */
466bool checkIfLogicalBitSet(const uint16_t& containerId);
George Liu83409572019-12-24 18:42:54 +0800467} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600468} // namespace pldm