xref: /openbmc/pldm/common/utils.hpp (revision 8fa40dbeb861ba078f5839d65abb4c48c49ece64)
1d130e1a3SDeepak Kodihalli #pragma once
2d130e1a3SDeepak Kodihalli 
3aea5dde1SSampa Misra #include "types.hpp"
4aea5dde1SSampa Misra 
5c453e164SGeorge Liu #include <libpldm/base.h>
6c453e164SGeorge Liu #include <libpldm/bios.h>
7df9a6d34SGeorge Liu #include <libpldm/entity.h>
8df9a6d34SGeorge Liu #include <libpldm/pdr.h>
9c453e164SGeorge Liu #include <libpldm/platform.h>
10c453e164SGeorge Liu #include <libpldm/utils.h>
11d130e1a3SDeepak Kodihalli #include <systemd/sd-bus.h>
12d130e1a3SDeepak Kodihalli #include <unistd.h>
13d130e1a3SDeepak Kodihalli 
14d130e1a3SDeepak Kodihalli #include <nlohmann/json.hpp>
15d130e1a3SDeepak Kodihalli #include <sdbusplus/server.hpp>
16754041d0SRiya Dixit #include <xyz/openbmc_project/Inventory/Manager/client.hpp>
17d130e1a3SDeepak Kodihalli #include <xyz/openbmc_project/Logging/Entry/server.hpp>
1844524a5fSGilbert Chen #include <xyz/openbmc_project/ObjectMapper/client.hpp>
19d130e1a3SDeepak Kodihalli 
20b3b84b49SPavithra Barithaya #include <cstdint>
21df9a6d34SGeorge Liu #include <deque>
22d130e1a3SDeepak Kodihalli #include <exception>
23d130e1a3SDeepak Kodihalli #include <filesystem>
24d130e1a3SDeepak Kodihalli #include <iostream>
25df9a6d34SGeorge Liu #include <map>
26d130e1a3SDeepak Kodihalli #include <string>
27d130e1a3SDeepak Kodihalli #include <variant>
28d130e1a3SDeepak Kodihalli #include <vector>
29d130e1a3SDeepak Kodihalli 
305b71b86fSvkaverap@in.ibm.com constexpr uint64_t dbusTimeout =
315b71b86fSvkaverap@in.ibm.com     std::chrono::duration_cast<std::chrono::microseconds>(
325b71b86fSvkaverap@in.ibm.com         std::chrono::seconds(DBUS_TIMEOUT))
335b71b86fSvkaverap@in.ibm.com         .count();
345b71b86fSvkaverap@in.ibm.com 
35d130e1a3SDeepak Kodihalli namespace pldm
36d130e1a3SDeepak Kodihalli {
37d130e1a3SDeepak Kodihalli namespace utils
38d130e1a3SDeepak Kodihalli {
39a34a64bbSThu Nguyen 
4077e6fe7aSGilbert Chen enum class Level
4177e6fe7aSGilbert Chen {
4277e6fe7aSGilbert Chen     WARNING,
4377e6fe7aSGilbert Chen     CRITICAL,
4477e6fe7aSGilbert Chen     PERFORMANCELOSS,
4577e6fe7aSGilbert Chen     SOFTSHUTDOWN,
4677e6fe7aSGilbert Chen     HARDSHUTDOWN,
4777e6fe7aSGilbert Chen     ERROR
4877e6fe7aSGilbert Chen };
4977e6fe7aSGilbert Chen enum class Direction
5077e6fe7aSGilbert Chen {
5177e6fe7aSGilbert Chen     HIGH,
5277e6fe7aSGilbert Chen     LOW,
5377e6fe7aSGilbert Chen     ERROR
5477e6fe7aSGilbert Chen };
5577e6fe7aSGilbert Chen 
56a34a64bbSThu Nguyen const std::set<std::string_view> dbusValueTypeNames = {
57a34a64bbSThu Nguyen     "bool",    "uint8_t",  "int16_t",         "uint16_t",
58a34a64bbSThu Nguyen     "int32_t", "uint32_t", "int64_t",         "uint64_t",
59a34a64bbSThu Nguyen     "double",  "string",   "vector<uint8_t>", "vector<string>"};
60a34a64bbSThu Nguyen const std::set<std::string_view> dbusValueNumericTypeNames = {
61a34a64bbSThu Nguyen     "uint8_t",  "int16_t", "uint16_t", "int32_t",
62a34a64bbSThu Nguyen     "uint32_t", "int64_t", "uint64_t", "double"};
63a34a64bbSThu Nguyen 
64d130e1a3SDeepak Kodihalli namespace fs = std::filesystem;
65d130e1a3SDeepak Kodihalli using Json = nlohmann::json;
66e5268cdaSTom Joseph constexpr bool Tx = true;
67e5268cdaSTom Joseph constexpr bool Rx = false;
6844524a5fSGilbert Chen using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
6944524a5fSGilbert Chen using inventoryManager =
7044524a5fSGilbert Chen     sdbusplus::client::xyz::openbmc_project::inventory::Manager<>;
7144524a5fSGilbert Chen 
7244524a5fSGilbert Chen constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
7344524a5fSGilbert Chen constexpr auto mapperService = ObjectMapper::default_service;
7444524a5fSGilbert Chen constexpr auto inventoryPath = "/xyz/openbmc_project/inventory";
75d130e1a3SDeepak Kodihalli /** @struct CustomFD
76d130e1a3SDeepak Kodihalli  *
77d130e1a3SDeepak Kodihalli  *  RAII wrapper for file descriptor.
78d130e1a3SDeepak Kodihalli  */
79d130e1a3SDeepak Kodihalli struct CustomFD
80d130e1a3SDeepak Kodihalli {
81d130e1a3SDeepak Kodihalli     CustomFD(const CustomFD&) = delete;
82d130e1a3SDeepak Kodihalli     CustomFD& operator=(const CustomFD&) = delete;
83d130e1a3SDeepak Kodihalli     CustomFD(CustomFD&&) = delete;
84d130e1a3SDeepak Kodihalli     CustomFD& operator=(CustomFD&&) = delete;
85d130e1a3SDeepak Kodihalli 
CustomFDpldm::utils::CustomFD866da4f91bSPatrick Williams     CustomFD(int fd) : fd(fd) {}
87d130e1a3SDeepak Kodihalli 
~CustomFDpldm::utils::CustomFD88d130e1a3SDeepak Kodihalli     ~CustomFD()
89d130e1a3SDeepak Kodihalli     {
90d130e1a3SDeepak Kodihalli         if (fd >= 0)
91d130e1a3SDeepak Kodihalli         {
92d130e1a3SDeepak Kodihalli             close(fd);
93d130e1a3SDeepak Kodihalli         }
94d130e1a3SDeepak Kodihalli     }
95d130e1a3SDeepak Kodihalli 
operator ()pldm::utils::CustomFD96d130e1a3SDeepak Kodihalli     int operator()() const
97d130e1a3SDeepak Kodihalli     {
98d130e1a3SDeepak Kodihalli         return fd;
99d130e1a3SDeepak Kodihalli     }
100d130e1a3SDeepak Kodihalli 
101d130e1a3SDeepak Kodihalli   private:
102d130e1a3SDeepak Kodihalli     int fd = -1;
103d130e1a3SDeepak Kodihalli };
104d130e1a3SDeepak Kodihalli 
105d130e1a3SDeepak Kodihalli /** @brief Calculate the pad for PLDM data
106d130e1a3SDeepak Kodihalli  *
107d130e1a3SDeepak Kodihalli  *  @param[in] data - Length of the data
108d130e1a3SDeepak Kodihalli  *  @return - uint8_t - number of pad bytes
109d130e1a3SDeepak Kodihalli  */
110d130e1a3SDeepak Kodihalli uint8_t getNumPadBytes(uint32_t data);
111d130e1a3SDeepak Kodihalli 
112d130e1a3SDeepak Kodihalli /** @brief Convert uint64 to date
113d130e1a3SDeepak Kodihalli  *
114d130e1a3SDeepak Kodihalli  *  @param[in] data - time date of uint64
115d130e1a3SDeepak Kodihalli  *  @param[out] year - year number in dec
116d130e1a3SDeepak Kodihalli  *  @param[out] month - month number in dec
117d130e1a3SDeepak Kodihalli  *  @param[out] day - day of the month in dec
118d130e1a3SDeepak Kodihalli  *  @param[out] hour - number of hours in dec
119d130e1a3SDeepak Kodihalli  *  @param[out] min - number of minutes in dec
120d130e1a3SDeepak Kodihalli  *  @param[out] sec - number of seconds in dec
1212576aecdSManojkiran Eda  *  @return true if decode success, false if decode failed
122d130e1a3SDeepak Kodihalli  */
123d130e1a3SDeepak Kodihalli bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
124d130e1a3SDeepak Kodihalli                 uint8_t* hour, uint8_t* min, uint8_t* sec);
125d130e1a3SDeepak Kodihalli 
126d130e1a3SDeepak Kodihalli /** @brief Convert effecter data to structure of set_effecter_state_field
127d130e1a3SDeepak Kodihalli  *
128d130e1a3SDeepak Kodihalli  *  @param[in] effecterData - the date of effecter
129d130e1a3SDeepak Kodihalli  *  @param[in] effecterCount - the number of individual sets of effecter
130d130e1a3SDeepak Kodihalli  *                              information
131d130e1a3SDeepak Kodihalli  *  @return[out] parse success and get a valid set_effecter_state_field
132d130e1a3SDeepak Kodihalli  *               structure, return nullopt means parse failed
133d130e1a3SDeepak Kodihalli  */
13416c2a0a0SPatrick Williams std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
13516c2a0a0SPatrick Williams     const std::vector<uint8_t>& effecterData, uint8_t effecterCount);
136d130e1a3SDeepak Kodihalli 
137d130e1a3SDeepak Kodihalli /**
138d130e1a3SDeepak Kodihalli  *  @brief creates an error log
139d130e1a3SDeepak Kodihalli  *  @param[in] errorMsg - the error message
140d130e1a3SDeepak Kodihalli  */
14192fb0b55SManojkiran Eda void reportError(const char* errorMsg);
142d130e1a3SDeepak Kodihalli 
143d130e1a3SDeepak Kodihalli /** @brief Convert any Decimal number to BCD
144d130e1a3SDeepak Kodihalli  *
145d130e1a3SDeepak Kodihalli  *  @tparam[in] decimal - Decimal number
146d130e1a3SDeepak Kodihalli  *  @return Corresponding BCD number
147d130e1a3SDeepak Kodihalli  */
148d130e1a3SDeepak Kodihalli template <typename T>
decimalToBcd(T decimal)149d130e1a3SDeepak Kodihalli T decimalToBcd(T decimal)
150d130e1a3SDeepak Kodihalli {
151d130e1a3SDeepak Kodihalli     T bcd = 0;
152d130e1a3SDeepak Kodihalli     T rem = 0;
153d130e1a3SDeepak Kodihalli     auto cnt = 0;
154d130e1a3SDeepak Kodihalli 
155d130e1a3SDeepak Kodihalli     while (decimal)
156d130e1a3SDeepak Kodihalli     {
157d130e1a3SDeepak Kodihalli         rem = decimal % 10;
158d130e1a3SDeepak Kodihalli         bcd = bcd + (rem << cnt);
159d130e1a3SDeepak Kodihalli         decimal = decimal / 10;
160d130e1a3SDeepak Kodihalli         cnt += 4;
161d130e1a3SDeepak Kodihalli     }
162d130e1a3SDeepak Kodihalli 
163d130e1a3SDeepak Kodihalli     return bcd;
164d130e1a3SDeepak Kodihalli }
165d130e1a3SDeepak Kodihalli 
166d130e1a3SDeepak Kodihalli struct DBusMapping
167d130e1a3SDeepak Kodihalli {
168d130e1a3SDeepak Kodihalli     std::string objectPath;   //!< D-Bus object path
169d130e1a3SDeepak Kodihalli     std::string interface;    //!< D-Bus interface
170d130e1a3SDeepak Kodihalli     std::string propertyName; //!< D-Bus property name
171d130e1a3SDeepak Kodihalli     std::string propertyType; //!< D-Bus property type
172d130e1a3SDeepak Kodihalli };
173d130e1a3SDeepak Kodihalli 
174d130e1a3SDeepak Kodihalli using PropertyValue =
175d130e1a3SDeepak Kodihalli     std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
176754041d0SRiya Dixit                  uint64_t, double, std::string, std::vector<uint8_t>,
177754041d0SRiya Dixit                  std::vector<std::string>>;
178d130e1a3SDeepak Kodihalli using DbusProp = std::string;
179d130e1a3SDeepak Kodihalli using DbusChangedProps = std::map<DbusProp, PropertyValue>;
180aea5dde1SSampa Misra using DBusInterfaceAdded = std::vector<
181aea5dde1SSampa Misra     std::pair<pldm::dbus::Interface,
182aea5dde1SSampa Misra               std::vector<std::pair<pldm::dbus::Property,
183aea5dde1SSampa Misra                                     std::variant<pldm::dbus::Property>>>>>;
1847d427f16SKamalkumar Patel 
1851ef62c37SManojkiran Eda using ObjectPath = std::string;
1867d427f16SKamalkumar Patel using EntityName = std::string;
1877d427f16SKamalkumar Patel using Entities = std::vector<pldm_entity_node*>;
1887d427f16SKamalkumar Patel using EntityAssociations = std::vector<Entities>;
1897d427f16SKamalkumar Patel using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
1907d427f16SKamalkumar Patel using EntityMaps = std::map<pldm::pdr::EntityType, EntityName>;
1917d427f16SKamalkumar Patel 
1921ef62c37SManojkiran Eda using ServiceName = std::string;
1931ef62c37SManojkiran Eda using Interfaces = std::vector<std::string>;
1941ef62c37SManojkiran Eda using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
1951ef62c37SManojkiran Eda using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
1962ec8269fSPavithra Barithaya using GetSubTreePathsResponse = std::vector<std::string>;
197549e4bc8SDelphine CC Chiu using GetAncestorsResponse =
198549e4bc8SDelphine CC Chiu     std::vector<std::pair<ObjectPath, MapperServiceMap>>;
199eefe49bfSSridevi Ramesh using PropertyMap = std::map<std::string, PropertyValue>;
200eefe49bfSSridevi Ramesh using InterfaceMap = std::map<std::string, PropertyMap>;
201754041d0SRiya Dixit using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
202d130e1a3SDeepak Kodihalli 
203d130e1a3SDeepak Kodihalli /**
204d130e1a3SDeepak Kodihalli  * @brief The interface for DBusHandler
205d130e1a3SDeepak Kodihalli  */
206d130e1a3SDeepak Kodihalli class DBusHandlerInterface
207d130e1a3SDeepak Kodihalli {
208d130e1a3SDeepak Kodihalli   public:
209d130e1a3SDeepak Kodihalli     virtual ~DBusHandlerInterface() = default;
210d130e1a3SDeepak Kodihalli 
21136e81352SGeorge Liu     virtual std::string getService(const char* path,
21236e81352SGeorge Liu                                    const char* interface) const = 0;
213366507c8SPatrick Williams     virtual GetSubTreeResponse getSubtree(
214366507c8SPatrick Williams         const std::string& path, int depth,
2151ef62c37SManojkiran Eda         const std::vector<std::string>& ifaceList) const = 0;
21636e81352SGeorge Liu 
217366507c8SPatrick Williams     virtual GetSubTreePathsResponse getSubTreePaths(
218366507c8SPatrick Williams         const std::string& objectPath, int depth,
2192ec8269fSPavithra Barithaya         const std::vector<std::string>& ifaceList) const = 0;
2202ec8269fSPavithra Barithaya 
221549e4bc8SDelphine CC Chiu     virtual GetAncestorsResponse getAncestors(
222549e4bc8SDelphine CC Chiu         const std::string& path,
223549e4bc8SDelphine CC Chiu         const std::vector<std::string>& ifaceList) const = 0;
224549e4bc8SDelphine CC Chiu 
225d130e1a3SDeepak Kodihalli     virtual void setDbusProperty(const DBusMapping& dBusMap,
226d130e1a3SDeepak Kodihalli                                  const PropertyValue& value) const = 0;
227d130e1a3SDeepak Kodihalli 
228366507c8SPatrick Williams     virtual PropertyValue getDbusPropertyVariant(
229366507c8SPatrick Williams         const char* objPath, const char* dbusProp,
230d130e1a3SDeepak Kodihalli         const char* dbusInterface) const = 0;
23144524a5fSGilbert Chen 
232366507c8SPatrick Williams     virtual PropertyMap getDbusPropertiesVariant(
233366507c8SPatrick Williams         const char* serviceName, const char* objPath,
23444524a5fSGilbert Chen         const char* dbusInterface) const = 0;
235d130e1a3SDeepak Kodihalli };
236d130e1a3SDeepak Kodihalli 
237d130e1a3SDeepak Kodihalli /**
238d130e1a3SDeepak Kodihalli  *  @class DBusHandler
239d130e1a3SDeepak Kodihalli  *
240d130e1a3SDeepak Kodihalli  *  Wrapper class to handle the D-Bus calls
241d130e1a3SDeepak Kodihalli  *
242d130e1a3SDeepak Kodihalli  *  This class contains the APIs to handle the D-Bus calls
243d130e1a3SDeepak Kodihalli  *  to cater the request from pldm requester.
244d130e1a3SDeepak Kodihalli  *  A class is created to mock the apis in the test cases
245d130e1a3SDeepak Kodihalli  */
246d130e1a3SDeepak Kodihalli class DBusHandler : public DBusHandlerInterface
247d130e1a3SDeepak Kodihalli {
248d130e1a3SDeepak Kodihalli   public:
249d130e1a3SDeepak Kodihalli     /** @brief Get the bus connection. */
getBus()250d130e1a3SDeepak Kodihalli     static auto& getBus()
251d130e1a3SDeepak Kodihalli     {
252d130e1a3SDeepak Kodihalli         static auto bus = sdbusplus::bus::new_default();
253d130e1a3SDeepak Kodihalli         return bus;
254d130e1a3SDeepak Kodihalli     }
255d130e1a3SDeepak Kodihalli 
256d130e1a3SDeepak Kodihalli     /**
257d130e1a3SDeepak Kodihalli      *  @brief Get the DBUS Service name for the input dbus path
258d130e1a3SDeepak Kodihalli      *
259d130e1a3SDeepak Kodihalli      *  @param[in] path - DBUS object path
260d130e1a3SDeepak Kodihalli      *  @param[in] interface - DBUS Interface
261d130e1a3SDeepak Kodihalli      *
262d130e1a3SDeepak Kodihalli      *  @return std::string - the dbus service name
263d130e1a3SDeepak Kodihalli      *
26484b790cbSPatrick Williams      *  @throw sdbusplus::exception_t when it fails
265d130e1a3SDeepak Kodihalli      */
26636e81352SGeorge Liu     std::string getService(const char* path,
26736e81352SGeorge Liu                            const char* interface) const override;
268d130e1a3SDeepak Kodihalli 
2691ef62c37SManojkiran Eda     /**
2701ef62c37SManojkiran Eda      *  @brief Get the Subtree response from the mapper
2711ef62c37SManojkiran Eda      *
2721ef62c37SManojkiran Eda      *  @param[in] path - DBUS object path
2731ef62c37SManojkiran Eda      *  @param[in] depth - Search depth
2741ef62c37SManojkiran Eda      *  @param[in] ifaceList - list of the interface that are being
2751ef62c37SManojkiran Eda      *                         queried from the mapper
2761ef62c37SManojkiran Eda      *
2771ef62c37SManojkiran Eda      *  @return GetSubTreeResponse - the mapper subtree response
2781ef62c37SManojkiran Eda      *
27984b790cbSPatrick Williams      *  @throw sdbusplus::exception_t when it fails
2801ef62c37SManojkiran Eda      */
281366507c8SPatrick Williams     GetSubTreeResponse getSubtree(
282366507c8SPatrick Williams         const std::string& path, int depth,
2831ef62c37SManojkiran Eda         const std::vector<std::string>& ifaceList) const override;
2841ef62c37SManojkiran Eda 
2852ec8269fSPavithra Barithaya     /** @brief Get Subtree path response from the mapper
2862ec8269fSPavithra Barithaya      *
2872ec8269fSPavithra Barithaya      *  @param[in] path - DBUS object path
2882ec8269fSPavithra Barithaya      *  @param[in] depth - Search depth
2892ec8269fSPavithra Barithaya      *  @param[in] ifaceList - list of the interface that are being
2902ec8269fSPavithra Barithaya      *                         queried from the mapper
2912ec8269fSPavithra Barithaya      *
2922ec8269fSPavithra Barithaya      *  @return std::vector<std::string> vector of subtree paths
2932ec8269fSPavithra Barithaya      */
2942ec8269fSPavithra Barithaya     GetSubTreePathsResponse getSubTreePaths(
2952ec8269fSPavithra Barithaya         const std::string& objectPath, int depth,
2962ec8269fSPavithra Barithaya         const std::vector<std::string>& ifaceList) const override;
2972ec8269fSPavithra Barithaya 
298549e4bc8SDelphine CC Chiu     /**
299549e4bc8SDelphine CC Chiu      *  @brief Get the Ancestors response from the mapper
300549e4bc8SDelphine CC Chiu      *
301549e4bc8SDelphine CC Chiu      *  @param[in] path - D-Bus object path
302549e4bc8SDelphine CC Chiu      *  @param[in] ifaceList - an optional list of interfaces to constrain the
303549e4bc8SDelphine CC Chiu      *                         search to queried from the mapper
304549e4bc8SDelphine CC Chiu      *
305549e4bc8SDelphine CC Chiu      *  @return GetAncestorsResponse - the mapper GetAncestors response
306549e4bc8SDelphine CC Chiu      *
307549e4bc8SDelphine CC Chiu      *  @throw sdbusplus::exception_t when it fails
308549e4bc8SDelphine CC Chiu      */
309549e4bc8SDelphine CC Chiu     GetAncestorsResponse getAncestors(
310549e4bc8SDelphine CC Chiu         const std::string& path,
311549e4bc8SDelphine CC Chiu         const std::vector<std::string>& ifaceList) const override;
312549e4bc8SDelphine CC Chiu 
313d130e1a3SDeepak Kodihalli     /** @brief Get property(type: variant) from the requested dbus
314d130e1a3SDeepak Kodihalli      *
315d130e1a3SDeepak Kodihalli      *  @param[in] objPath - The Dbus object path
316d130e1a3SDeepak Kodihalli      *  @param[in] dbusProp - The property name to get
317d130e1a3SDeepak Kodihalli      *  @param[in] dbusInterface - The Dbus interface
318d130e1a3SDeepak Kodihalli      *
319d130e1a3SDeepak Kodihalli      *  @return The value of the property(type: variant)
320d130e1a3SDeepak Kodihalli      *
32184b790cbSPatrick Williams      *  @throw sdbusplus::exception_t when it fails
322d130e1a3SDeepak Kodihalli      */
323366507c8SPatrick Williams     PropertyValue getDbusPropertyVariant(
324366507c8SPatrick Williams         const char* objPath, const char* dbusProp,
325d130e1a3SDeepak Kodihalli         const char* dbusInterface) const override;
326d130e1a3SDeepak Kodihalli 
32744524a5fSGilbert Chen     /** @brief Get All properties(type: variant) from the requested dbus
32844524a5fSGilbert Chen      *
32944524a5fSGilbert Chen      *  @param[in] serviceName - The Dbus service name
33044524a5fSGilbert Chen      *  @param[in] objPath - The Dbus object path
33144524a5fSGilbert Chen      *  @param[in] dbusInterface - The Dbus interface
33244524a5fSGilbert Chen      *
33344524a5fSGilbert Chen      *  @return The values of the properties(type: variant)
33444524a5fSGilbert Chen      *
33544524a5fSGilbert Chen      *  @throw sdbusplus::exception_t when it fails
33644524a5fSGilbert Chen      */
337366507c8SPatrick Williams     PropertyMap getDbusPropertiesVariant(
338366507c8SPatrick Williams         const char* serviceName, const char* objPath,
33944524a5fSGilbert Chen         const char* dbusInterface) const override;
34044524a5fSGilbert Chen 
341d130e1a3SDeepak Kodihalli     /** @brief The template function to get property from the requested dbus
342d130e1a3SDeepak Kodihalli      *         path
343d130e1a3SDeepak Kodihalli      *
344d130e1a3SDeepak Kodihalli      *  @tparam Property - Excepted type of the property on dbus
345d130e1a3SDeepak Kodihalli      *
346d130e1a3SDeepak Kodihalli      *  @param[in] objPath - The Dbus object path
347d130e1a3SDeepak Kodihalli      *  @param[in] dbusProp - The property name to get
348d130e1a3SDeepak Kodihalli      *  @param[in] dbusInterface - The Dbus interface
349d130e1a3SDeepak Kodihalli      *
350d130e1a3SDeepak Kodihalli      *  @return The value of the property
351d130e1a3SDeepak Kodihalli      *
35284b790cbSPatrick Williams      *  @throw sdbusplus::exception_t when dbus request fails
353d130e1a3SDeepak Kodihalli      *         std::bad_variant_access when \p Property and property on dbus do
354d130e1a3SDeepak Kodihalli      *         not match
355d130e1a3SDeepak Kodihalli      */
356d130e1a3SDeepak Kodihalli     template <typename Property>
getDbusProperty(const char * objPath,const char * dbusProp,const char * dbusInterface)357d130e1a3SDeepak Kodihalli     auto getDbusProperty(const char* objPath, const char* dbusProp,
358d130e1a3SDeepak Kodihalli                          const char* dbusInterface)
359d130e1a3SDeepak Kodihalli     {
36016c2a0a0SPatrick Williams         auto VariantValue =
36116c2a0a0SPatrick Williams             getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
362d130e1a3SDeepak Kodihalli         return std::get<Property>(VariantValue);
363d130e1a3SDeepak Kodihalli     }
364d130e1a3SDeepak Kodihalli 
365d130e1a3SDeepak Kodihalli     /** @brief Set Dbus property
366d130e1a3SDeepak Kodihalli      *
367d130e1a3SDeepak Kodihalli      *  @param[in] dBusMap - Object path, property name, interface and property
368d130e1a3SDeepak Kodihalli      *                       type for the D-Bus object
369d130e1a3SDeepak Kodihalli      *  @param[in] value - The value to be set
370d130e1a3SDeepak Kodihalli      *
37184b790cbSPatrick Williams      *  @throw sdbusplus::exception_t when it fails
372d130e1a3SDeepak Kodihalli      */
373d130e1a3SDeepak Kodihalli     void setDbusProperty(const DBusMapping& dBusMap,
374d130e1a3SDeepak Kodihalli                          const PropertyValue& value) const override;
375754041d0SRiya Dixit 
376754041d0SRiya Dixit     /** @brief This function retrieves the properties of an object managed
377754041d0SRiya Dixit      *         by the specified D-Bus service located at the given object path.
378754041d0SRiya Dixit      *
379754041d0SRiya Dixit      *  @param[in] service - The D-Bus service providing the managed object
380754041d0SRiya Dixit      *  @param[in] value - The object path of the managed object
381754041d0SRiya Dixit      *
382754041d0SRiya Dixit      *  @return A hierarchical structure representing the properties of the
383754041d0SRiya Dixit      *          managed object.
384897b0f8bSPatrick Williams      *  @throw sdbusplus::exception_t when it fails
385754041d0SRiya Dixit      */
386754041d0SRiya Dixit     static ObjectValueTree getManagedObj(const char* service, const char* path);
387754041d0SRiya Dixit 
388754041d0SRiya Dixit     /** @brief Retrieve the inventory objects managed by a specified class.
389754041d0SRiya Dixit      *         The retrieved inventory objects are cached statically
390754041d0SRiya Dixit      *         and returned upon subsequent calls to this function.
391754041d0SRiya Dixit      *
392754041d0SRiya Dixit      *  @tparam ClassType - The class type that manages the inventory objects.
393754041d0SRiya Dixit      *
394754041d0SRiya Dixit      *  @return A reference to the cached inventory objects.
395754041d0SRiya Dixit      */
396754041d0SRiya Dixit     template <typename ClassType>
getInventoryObjects()397754041d0SRiya Dixit     static auto& getInventoryObjects()
398754041d0SRiya Dixit     {
399754041d0SRiya Dixit         static ObjectValueTree object = ClassType::getManagedObj(
400754041d0SRiya Dixit             inventoryManager::interface, inventoryPath);
401754041d0SRiya Dixit         return object;
402754041d0SRiya Dixit     }
403d130e1a3SDeepak Kodihalli };
404d130e1a3SDeepak Kodihalli 
405d130e1a3SDeepak Kodihalli /** @brief Fetch parent D-Bus object based on pathname
406d130e1a3SDeepak Kodihalli  *
407d130e1a3SDeepak Kodihalli  *  @param[in] dbusObj - child D-Bus object
408d130e1a3SDeepak Kodihalli  *
409d130e1a3SDeepak Kodihalli  *  @return std::string - the parent D-Bus object path
410d130e1a3SDeepak Kodihalli  */
findParent(const std::string & dbusObj)411d130e1a3SDeepak Kodihalli inline std::string findParent(const std::string& dbusObj)
412d130e1a3SDeepak Kodihalli {
413d130e1a3SDeepak Kodihalli     fs::path p(dbusObj);
414d130e1a3SDeepak Kodihalli     return p.parent_path().string();
415d130e1a3SDeepak Kodihalli }
416d130e1a3SDeepak Kodihalli 
417d130e1a3SDeepak Kodihalli /** @brief Read (static) MCTP EID of host firmware from a file
418d130e1a3SDeepak Kodihalli  *
419d130e1a3SDeepak Kodihalli  *  @return uint8_t - MCTP EID
420d130e1a3SDeepak Kodihalli  */
421d130e1a3SDeepak Kodihalli uint8_t readHostEID();
422d130e1a3SDeepak Kodihalli 
4236c7fed4cSGilbert Chen /** @brief Validate the MCTP EID of MCTP endpoint
4246c7fed4cSGilbert Chen  *         In `Table 2 - Special endpoint IDs` of DSP0236. EID 0 is NULL_EID.
4256c7fed4cSGilbert Chen  *         EID from 1 to 7 is reserved EID. EID 0xFF is broadcast EID.
4266c7fed4cSGilbert Chen  *         Those are invalid EID of one MCTP Endpoint.
4276c7fed4cSGilbert Chen  *
4286c7fed4cSGilbert Chen  * @param[in] eid - MCTP EID
4296c7fed4cSGilbert Chen  *
4306c7fed4cSGilbert Chen  * @return true if the MCTP EID is valid otherwise return false.
4316c7fed4cSGilbert Chen  */
4326c7fed4cSGilbert Chen bool isValidEID(eid mctpEid);
4336c7fed4cSGilbert Chen 
434d130e1a3SDeepak Kodihalli /** @brief Convert a value in the JSON to a D-Bus property value
435d130e1a3SDeepak Kodihalli  *
436d130e1a3SDeepak Kodihalli  *  @param[in] type - type of the D-Bus property
437d130e1a3SDeepak Kodihalli  *  @param[in] value - value in the JSON file
438d130e1a3SDeepak Kodihalli  *
439d130e1a3SDeepak Kodihalli  *  @return PropertyValue - the D-Bus property value
440d130e1a3SDeepak Kodihalli  */
441d130e1a3SDeepak Kodihalli PropertyValue jsonEntryToDbusVal(std::string_view type,
442d130e1a3SDeepak Kodihalli                                  const nlohmann::json& value);
443d130e1a3SDeepak Kodihalli 
444d130e1a3SDeepak Kodihalli /** @brief Find State Effecter PDR
445d130e1a3SDeepak Kodihalli  *  @param[in] tid - PLDM terminus ID.
446d130e1a3SDeepak Kodihalli  *  @param[in] entityID - entity that can be associated with PLDM State set.
447d130e1a3SDeepak Kodihalli  *  @param[in] stateSetId - value that identifies PLDM State set.
448d130e1a3SDeepak Kodihalli  *  @param[in] repo - pointer to BMC's primary PDR repo.
449d130e1a3SDeepak Kodihalli  *  @return array[array[uint8_t]] - StateEffecterPDRs
450d130e1a3SDeepak Kodihalli  */
45116c2a0a0SPatrick Williams std::vector<std::vector<uint8_t>> findStateEffecterPDR(
45216c2a0a0SPatrick Williams     uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
453d130e1a3SDeepak Kodihalli /** @brief Find State Sensor PDR
454d130e1a3SDeepak Kodihalli  *  @param[in] tid - PLDM terminus ID.
455d130e1a3SDeepak Kodihalli  *  @param[in] entityID - entity that can be associated with PLDM State set.
456d130e1a3SDeepak Kodihalli  *  @param[in] stateSetId - value that identifies PLDM State set.
457d130e1a3SDeepak Kodihalli  *  @param[in] repo - pointer to BMC's primary PDR repo.
458d130e1a3SDeepak Kodihalli  *  @return array[array[uint8_t]] - StateSensorPDRs
459d130e1a3SDeepak Kodihalli  */
46016c2a0a0SPatrick Williams std::vector<std::vector<uint8_t>> findStateSensorPDR(
46116c2a0a0SPatrick Williams     uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
462d130e1a3SDeepak Kodihalli 
4633a0e3b9bSSampa Misra /** @brief Find sensor id from a state sensor PDR
4643a0e3b9bSSampa Misra  *
4653a0e3b9bSSampa Misra  *  @param[in] pdrRepo - PDR repository
4663a0e3b9bSSampa Misra  *  @param[in] tid - terminus id
4673a0e3b9bSSampa Misra  *  @param[in] entityType - entity type
4683a0e3b9bSSampa Misra  *  @param[in] entityInstance - entity instance number
4693a0e3b9bSSampa Misra  *  @param[in] containerId - container id
4703a0e3b9bSSampa Misra  *  @param[in] stateSetId - state set id
4713a0e3b9bSSampa Misra  *
4723a0e3b9bSSampa Misra  *  @return uint16_t - the sensor id
4733a0e3b9bSSampa Misra  */
4743a0e3b9bSSampa Misra uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
4753a0e3b9bSSampa Misra                            uint16_t entityType, uint16_t entityInstance,
4763a0e3b9bSSampa Misra                            uint16_t containerId, uint16_t stateSetId);
4773a0e3b9bSSampa Misra 
478d130e1a3SDeepak Kodihalli /** @brief Find effecter id from a state effecter pdr
479d130e1a3SDeepak Kodihalli  *  @param[in] pdrRepo - PDR repository
480d130e1a3SDeepak Kodihalli  *  @param[in] entityType - entity type
481d130e1a3SDeepak Kodihalli  *  @param[in] entityInstance - entity instance number
482d130e1a3SDeepak Kodihalli  *  @param[in] containerId - container id
483d130e1a3SDeepak Kodihalli  *  @param[in] stateSetId - state set id
484a4a96162SSampa Misra  *  @param[in] localOrRemote - true for checking local repo and false for remote
485a4a96162SSampa Misra  *                             repo
486d130e1a3SDeepak Kodihalli  *
487d130e1a3SDeepak Kodihalli  *  @return uint16_t - the effecter id
488d130e1a3SDeepak Kodihalli  */
489d130e1a3SDeepak Kodihalli uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
490d130e1a3SDeepak Kodihalli                              uint16_t entityInstance, uint16_t containerId,
491a4a96162SSampa Misra                              uint16_t stateSetId, bool localOrRemote);
492d130e1a3SDeepak Kodihalli 
493fe4d88bbSChicago Duan /** @brief Emit the sensor event signal
494fe4d88bbSChicago Duan  *
495fe4d88bbSChicago Duan  *	@param[in] tid - the terminus id
496fe4d88bbSChicago Duan  *  @param[in] sensorId - sensorID value of the sensor
497fe4d88bbSChicago Duan  *  @param[in] sensorOffset - Identifies which state sensor within a
498fe4d88bbSChicago Duan  * composite state sensor the event is being returned for
499fe4d88bbSChicago Duan  *  @param[in] eventState - The event state value from the state change that
500fe4d88bbSChicago Duan  * triggered the event message
501fe4d88bbSChicago Duan  *  @param[in] previousEventState - The event state value for the state from
502fe4d88bbSChicago Duan  * which the present event state was entered.
503fe4d88bbSChicago Duan  *  @return PLDM completion code
504fe4d88bbSChicago Duan  */
505fe4d88bbSChicago Duan int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
506fe4d88bbSChicago Duan                                uint8_t sensorOffset, uint8_t eventState,
507fe4d88bbSChicago Duan                                uint8_t previousEventState);
508fe4d88bbSChicago Duan 
509*8fa40dbeSChau Ly /**
510*8fa40dbeSChau Ly  *  @brief call Recover() method to recover an MCTP Endpoint
511*8fa40dbeSChau Ly  *  @param[in] MCTP Endpoint's object path
512*8fa40dbeSChau Ly  */
513*8fa40dbeSChau Ly void recoverMctpEndpoint(const std::string& endpointObjPath);
514*8fa40dbeSChau Ly 
515ae28bc77SSridevi Ramesh /** @brief Print the buffer
516ae28bc77SSridevi Ramesh  *
517e5268cdaSTom Joseph  *  @param[in]  isTx - True if the buffer is an outgoing PLDM message, false if
518e5268cdaSTom Joseph                        the buffer is an incoming PLDM message
519ae28bc77SSridevi Ramesh  *  @param[in]  buffer - Buffer to print
520ae28bc77SSridevi Ramesh  *
521ae28bc77SSridevi Ramesh  *  @return - None
522ae28bc77SSridevi Ramesh  */
523e5268cdaSTom Joseph void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
524ae28bc77SSridevi Ramesh 
5255492207aSTom Joseph /** @brief Convert the buffer to std::string
5265492207aSTom Joseph  *
5275492207aSTom Joseph  *  If there are characters that are not printable characters, it is replaced
5285492207aSTom Joseph  *  with space(0x20).
5295492207aSTom Joseph  *
5305492207aSTom Joseph  *  @param[in] var - pointer to data and length of the data
5315492207aSTom Joseph  *
5325492207aSTom Joseph  *  @return std::string equivalent of variable field
5335492207aSTom Joseph  */
5345492207aSTom Joseph std::string toString(const struct variable_field& var);
5355492207aSTom Joseph 
536872f0f69SGeorge Liu /** @brief Split strings according to special identifiers
537872f0f69SGeorge Liu  *
538872f0f69SGeorge Liu  *  We can split the string according to the custom identifier(';', ',', '&' or
539872f0f69SGeorge Liu  *  others) and store it to vector.
540872f0f69SGeorge Liu  *
541872f0f69SGeorge Liu  *  @param[in] srcStr       - The string to be split
542872f0f69SGeorge Liu  *  @param[in] delim        - The custom identifier
543872f0f69SGeorge Liu  *  @param[in] trimStr      - The first and last string to be trimmed
544872f0f69SGeorge Liu  *
545872f0f69SGeorge Liu  *  @return std::vector<std::string> Vectors are used to store strings
546872f0f69SGeorge Liu  */
547872f0f69SGeorge Liu std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
548872f0f69SGeorge Liu                                std::string_view trimStr = "");
549ef773059SManojkiran Eda /** @brief Get the current system time in readable format
550ef773059SManojkiran Eda  *
551ef773059SManojkiran Eda  *  @return - std::string equivalent of the system time
552ef773059SManojkiran Eda  */
553ef773059SManojkiran Eda std::string getCurrentSystemTime();
554872f0f69SGeorge Liu 
555eefe49bfSSridevi Ramesh /** @brief checks if the FRU is actually present.
556eefe49bfSSridevi Ramesh  *  @param[in] objPath - FRU object path.
557eefe49bfSSridevi Ramesh  *
558eefe49bfSSridevi Ramesh  *  @return bool to indicate presence or absence of FRU.
559eefe49bfSSridevi Ramesh  */
560eefe49bfSSridevi Ramesh bool checkForFruPresence(const std::string& objPath);
561eefe49bfSSridevi Ramesh 
5625db6e872SSagar Srinivas /** @brief Method to check if the logical bit is set
5635db6e872SSagar Srinivas  *
5645db6e872SSagar Srinivas  *  @param[containerId] - container id of the entity
5655db6e872SSagar Srinivas  *
5665db6e872SSagar Srinivas  *  @return true or false based on the logic bit set
5675db6e872SSagar Srinivas  */
5685db6e872SSagar Srinivas bool checkIfLogicalBitSet(const uint16_t& containerId);
5695e542be2SPavithra Barithaya 
5705e542be2SPavithra Barithaya /** @brief setting the present property
5715e542be2SPavithra Barithaya  *
5725e542be2SPavithra Barithaya  *  @param[in] objPath - the object path of the fru
5735e542be2SPavithra Barithaya  *  @param[in] present - status to set either true/false
5745e542be2SPavithra Barithaya  */
5755e542be2SPavithra Barithaya void setFruPresence(const std::string& fruObjPath, bool present);
576b8cf46b8SThu Nguyen 
577b8cf46b8SThu Nguyen /** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus
578b8cf46b8SThu Nguyen  *         object path
579b8cf46b8SThu Nguyen  *
580b8cf46b8SThu Nguyen  *  @param[in] name - the input string
581b8cf46b8SThu Nguyen  *
582b8cf46b8SThu Nguyen  *  @return the result string
583b8cf46b8SThu Nguyen  */
584b8cf46b8SThu Nguyen std::string_view trimNameForDbus(std::string& name);
585b8cf46b8SThu Nguyen 
586a34a64bbSThu Nguyen /** @brief Convert the number type D-Bus Value to the double
587a34a64bbSThu Nguyen  *
588a34a64bbSThu Nguyen  *  @param[in] type - string type should in dbusValueNumericTypeNames list
589a34a64bbSThu Nguyen  *  @param[in] value - DBus PropertyValue variant
590a34a64bbSThu Nguyen  *  @param[out] doubleValue - response value
591a34a64bbSThu Nguyen  *
592a34a64bbSThu Nguyen  *  @return true if data type is corrected and converting is successful
593a34a64bbSThu Nguyen  *          otherwise return false.
594a34a64bbSThu Nguyen  */
595a34a64bbSThu Nguyen bool dbusPropValuesToDouble(const std::string_view& type,
596a34a64bbSThu Nguyen                             const pldm::utils::PropertyValue& value,
597a34a64bbSThu Nguyen                             double* doubleValue);
598a34a64bbSThu Nguyen 
599b6d3943dSDung Cao /** @brief Convert the Fru String bytes from PLDM Fru to std::string
600b6d3943dSDung Cao  *
601b6d3943dSDung Cao  *  @param[in] value - the Fru String bytes
602b6d3943dSDung Cao  *  @param[in] length - Number of bytes
603b6d3943dSDung Cao  *
604b6d3943dSDung Cao  *  @return Fru string or nullopt.
605b6d3943dSDung Cao  */
606366507c8SPatrick Williams std::optional<std::string> fruFieldValuestring(const uint8_t* value,
607366507c8SPatrick Williams                                                const uint8_t& length);
608b6d3943dSDung Cao 
609b6d3943dSDung Cao /** @brief Convert the Fru Uint32 raw data from PLDM Fru to uint32_t
610b6d3943dSDung Cao  *
611b6d3943dSDung Cao  *  @param[in] value - the Fru uint32 raw data
612b6d3943dSDung Cao  *  @param[in] length - Number of bytes
613b6d3943dSDung Cao  *
614b6d3943dSDung Cao  *  @return Fru uint32_t or nullopt.
615b6d3943dSDung Cao  */
616b6d3943dSDung Cao std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
617b6d3943dSDung Cao                                           const uint8_t& length);
618b6d3943dSDung Cao 
619d130e1a3SDeepak Kodihalli } // namespace utils
620d130e1a3SDeepak Kodihalli } // namespace pldm
621