blob: 9964188c6ea3bdb3c3156135b0478e436810a338 [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 <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060013
George Liu6492f522020-06-16 10:34:05 +080014#include <nlohmann/json.hpp>
15#include <sdbusplus/server.hpp>
Riya Dixit754041d2024-02-20 06:15:49 -060016#include <xyz/openbmc_project/Inventory/Manager/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080017#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Gilbert Chen44524a52022-02-14 12:12:25 +000018#include <xyz/openbmc_project/ObjectMapper/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080019
Pavithra Barithayab3b84b42024-08-23 11:43:57 +053020#include <cstdint>
George Liudf9a6d32020-12-22 16:27:16 +080021#include <deque>
Sampa Misraa2fa0702019-05-31 01:28:55 -050022#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050023#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060024#include <iostream>
George Liudf9a6d32020-12-22 16:27:16 +080025#include <map>
Sampa Misra032bd502019-03-06 05:03:22 -060026#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050027#include <variant>
28#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060029
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +000030constexpr uint64_t dbusTimeout =
31 std::chrono::duration_cast<std::chrono::microseconds>(
32 std::chrono::seconds(DBUS_TIMEOUT))
33 .count();
34
Sampa Misra032bd502019-03-06 05:03:22 -060035namespace pldm
36{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050037namespace utils
38{
Thu Nguyena34a64b2022-03-31 08:56:39 +070039
Gilbert Chen77e6fe72024-08-06 09:23:30 +000040enum class Level
41{
42 WARNING,
43 CRITICAL,
44 PERFORMANCELOSS,
45 SOFTSHUTDOWN,
46 HARDSHUTDOWN,
47 ERROR
48};
49enum class Direction
50{
51 HIGH,
52 LOW,
53 ERROR
54};
55
Thu Nguyena34a64b2022-03-31 08:56:39 +070056const std::set<std::string_view> dbusValueTypeNames = {
57 "bool", "uint8_t", "int16_t", "uint16_t",
58 "int32_t", "uint32_t", "int64_t", "uint64_t",
59 "double", "string", "vector<uint8_t>", "vector<string>"};
60const std::set<std::string_view> dbusValueNumericTypeNames = {
61 "uint8_t", "int16_t", "uint16_t", "int32_t",
62 "uint32_t", "int64_t", "uint64_t", "double"};
63
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050064namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053065using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053066constexpr bool Tx = true;
67constexpr bool Rx = false;
Gilbert Chen44524a52022-02-14 12:12:25 +000068using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
Gilbert Chen44524a52022-02-14 12:12:25 +000069using inventoryManager =
70 sdbusplus::client::xyz::openbmc_project::inventory::Manager<>;
71
72constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
73constexpr auto mapperService = ObjectMapper::default_service;
74constexpr auto inventoryPath = "/xyz/openbmc_project/inventory";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050075/** @struct CustomFD
76 *
77 * RAII wrapper for file descriptor.
78 */
79struct CustomFD
80{
81 CustomFD(const CustomFD&) = delete;
82 CustomFD& operator=(const CustomFD&) = delete;
83 CustomFD(CustomFD&&) = delete;
84 CustomFD& operator=(CustomFD&&) = delete;
85
Patrick Williams6da4f912023-05-10 07:50:53 -050086 CustomFD(int fd) : fd(fd) {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050087
88 ~CustomFD()
89 {
90 if (fd >= 0)
91 {
92 close(fd);
93 }
94 }
95
96 int operator()() const
97 {
98 return fd;
99 }
100
101 private:
102 int fd = -1;
103};
104
Sampa Misrab37be312019-07-03 02:26:41 -0500105/** @brief Calculate the pad for PLDM data
106 *
107 * @param[in] data - Length of the data
108 * @return - uint8_t - number of pad bytes
109 */
110uint8_t getNumPadBytes(uint32_t data);
111
George Liu83409572019-12-24 18:42:54 +0800112/** @brief Convert uint64 to date
113 *
114 * @param[in] data - time date of uint64
115 * @param[out] year - year number in dec
116 * @param[out] month - month number in dec
117 * @param[out] day - day of the month in dec
118 * @param[out] hour - number of hours in dec
119 * @param[out] min - number of minutes in dec
120 * @param[out] sec - number of seconds in dec
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530121 * @return true if decode success, false if decode failed
George Liu83409572019-12-24 18:42:54 +0800122 */
123bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
124 uint8_t* hour, uint8_t* min, uint8_t* sec);
125
126/** @brief Convert effecter data to structure of set_effecter_state_field
127 *
128 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +0800129 * @param[in] effecterCount - the number of individual sets of effecter
130 * information
131 * @return[out] parse success and get a valid set_effecter_state_field
132 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +0800133 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400134std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
135 const std::vector<uint8_t>& effecterData, uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -0600136
137/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600138 * @brief creates an error log
139 * @param[in] errorMsg - the error message
140 */
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530141void reportError(const char* errorMsg);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600142
Sampa Misra032bd502019-03-06 05:03:22 -0600143/** @brief Convert any Decimal number to BCD
144 *
145 * @tparam[in] decimal - Decimal number
146 * @return Corresponding BCD number
147 */
148template <typename T>
149T decimalToBcd(T decimal)
150{
151 T bcd = 0;
152 T rem = 0;
153 auto cnt = 0;
154
155 while (decimal)
156 {
157 rem = decimal % 10;
158 bcd = bcd + (rem << cnt);
159 decimal = decimal / 10;
160 cnt += 4;
161 }
162
163 return bcd;
164}
165
George Liu1e44c732020-02-28 20:20:06 +0800166struct DBusMapping
167{
168 std::string objectPath; //!< D-Bus object path
169 std::string interface; //!< D-Bus interface
170 std::string propertyName; //!< D-Bus property name
171 std::string propertyType; //!< D-Bus property type
172};
173
174using PropertyValue =
175 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Riya Dixit754041d2024-02-20 06:15:49 -0600176 uint64_t, double, std::string, std::vector<uint8_t>,
177 std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500178using DbusProp = std::string;
179using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500180using DBusInterfaceAdded = std::vector<
181 std::pair<pldm::dbus::Interface,
182 std::vector<std::pair<pldm::dbus::Property,
183 std::variant<pldm::dbus::Property>>>>>;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500184
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530185using ObjectPath = std::string;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500186using EntityName = std::string;
187using Entities = std::vector<pldm_entity_node*>;
188using EntityAssociations = std::vector<Entities>;
189using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
190using EntityMaps = std::map<pldm::pdr::EntityType, EntityName>;
191
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530192using ServiceName = std::string;
193using Interfaces = std::vector<std::string>;
194using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
195using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500196using GetSubTreePathsResponse = std::vector<std::string>;
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800197using GetAncestorsResponse =
198 std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500199using PropertyMap = std::map<std::string, PropertyValue>;
200using InterfaceMap = std::map<std::string, PropertyMap>;
Riya Dixit754041d2024-02-20 06:15:49 -0600201using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
George Liu1e44c732020-02-28 20:20:06 +0800202
203/**
204 * @brief The interface for DBusHandler
205 */
206class DBusHandlerInterface
207{
208 public:
209 virtual ~DBusHandlerInterface() = default;
210
George Liu36e81352020-07-01 14:40:30 +0800211 virtual std::string getService(const char* path,
212 const char* interface) const = 0;
Patrick Williams366507c2025-02-03 14:28:01 -0500213 virtual GetSubTreeResponse getSubtree(
214 const std::string& path, int depth,
215 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800216
Patrick Williams366507c2025-02-03 14:28:01 -0500217 virtual GetSubTreePathsResponse getSubTreePaths(
218 const std::string& objectPath, int depth,
219 const std::vector<std::string>& ifaceList) const = 0;
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500220
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800221 virtual GetAncestorsResponse getAncestors(
222 const std::string& path,
223 const std::vector<std::string>& ifaceList) const = 0;
224
George Liu1e44c732020-02-28 20:20:06 +0800225 virtual void setDbusProperty(const DBusMapping& dBusMap,
226 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800227
Patrick Williams366507c2025-02-03 14:28:01 -0500228 virtual PropertyValue getDbusPropertyVariant(
229 const char* objPath, const char* dbusProp,
230 const char* dbusInterface) const = 0;
Gilbert Chen44524a52022-02-14 12:12:25 +0000231
Patrick Williams366507c2025-02-03 14:28:01 -0500232 virtual PropertyMap getDbusPropertiesVariant(
233 const char* serviceName, const char* objPath,
234 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800235};
236
Sampa Misraa2fa0702019-05-31 01:28:55 -0500237/**
238 * @class DBusHandler
239 *
240 * Wrapper class to handle the D-Bus calls
241 *
242 * This class contains the APIs to handle the D-Bus calls
243 * to cater the request from pldm requester.
244 * A class is created to mock the apis in the test cases
245 */
George Liu1e44c732020-02-28 20:20:06 +0800246class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247{
248 public:
George Liu0e02c322020-01-01 09:41:51 +0800249 /** @brief Get the bus connection. */
250 static auto& getBus()
251 {
252 static auto bus = sdbusplus::bus::new_default();
253 return bus;
254 }
255
256 /**
257 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800258 *
George Liu0e02c322020-01-01 09:41:51 +0800259 * @param[in] path - DBUS object path
260 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800261 *
George Liu0e02c322020-01-01 09:41:51 +0800262 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800263 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500264 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800265 */
George Liu36e81352020-07-01 14:40:30 +0800266 std::string getService(const char* path,
267 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800268
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530269 /**
270 * @brief Get the Subtree response from the mapper
271 *
272 * @param[in] path - DBUS object path
273 * @param[in] depth - Search depth
274 * @param[in] ifaceList - list of the interface that are being
275 * queried from the mapper
276 *
277 * @return GetSubTreeResponse - the mapper subtree response
278 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500279 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530280 */
Patrick Williams366507c2025-02-03 14:28:01 -0500281 GetSubTreeResponse getSubtree(
282 const std::string& path, int depth,
283 const std::vector<std::string>& ifaceList) const override;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530284
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500285 /** @brief Get Subtree path response from the mapper
286 *
287 * @param[in] path - DBUS object path
288 * @param[in] depth - Search depth
289 * @param[in] ifaceList - list of the interface that are being
290 * queried from the mapper
291 *
292 * @return std::vector<std::string> vector of subtree paths
293 */
294 GetSubTreePathsResponse getSubTreePaths(
295 const std::string& objectPath, int depth,
296 const std::vector<std::string>& ifaceList) const override;
297
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800298 /**
299 * @brief Get the Ancestors response from the mapper
300 *
301 * @param[in] path - D-Bus object path
302 * @param[in] ifaceList - an optional list of interfaces to constrain the
303 * search to queried from the mapper
304 *
305 * @return GetAncestorsResponse - the mapper GetAncestors response
306 *
307 * @throw sdbusplus::exception_t when it fails
308 */
309 GetAncestorsResponse getAncestors(
310 const std::string& path,
311 const std::vector<std::string>& ifaceList) const override;
312
John Wang9e242422020-03-05 08:37:50 +0800313 /** @brief Get property(type: variant) from the requested dbus
314 *
315 * @param[in] objPath - The Dbus object path
316 * @param[in] dbusProp - The property name to get
317 * @param[in] dbusInterface - The Dbus interface
318 *
319 * @return The value of the property(type: variant)
320 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500321 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800322 */
Patrick Williams366507c2025-02-03 14:28:01 -0500323 PropertyValue getDbusPropertyVariant(
324 const char* objPath, const char* dbusProp,
325 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800326
Gilbert Chen44524a52022-02-14 12:12:25 +0000327 /** @brief Get All properties(type: variant) from the requested dbus
328 *
329 * @param[in] serviceName - The Dbus service name
330 * @param[in] objPath - The Dbus object path
331 * @param[in] dbusInterface - The Dbus interface
332 *
333 * @return The values of the properties(type: variant)
334 *
335 * @throw sdbusplus::exception_t when it fails
336 */
Patrick Williams366507c2025-02-03 14:28:01 -0500337 PropertyMap getDbusPropertiesVariant(
338 const char* serviceName, const char* objPath,
339 const char* dbusInterface) const override;
Gilbert Chen44524a52022-02-14 12:12:25 +0000340
John Wang9e242422020-03-05 08:37:50 +0800341 /** @brief The template function to get property from the requested dbus
342 * path
343 *
344 * @tparam Property - Excepted type of the property on dbus
345 *
346 * @param[in] objPath - The Dbus object path
347 * @param[in] dbusProp - The property name to get
348 * @param[in] dbusInterface - The Dbus interface
349 *
350 * @return The value of the property
351 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500352 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800353 * std::bad_variant_access when \p Property and property on dbus do
354 * not match
355 */
John Wang92b3c972019-10-17 11:06:41 +0800356 template <typename Property>
357 auto getDbusProperty(const char* objPath, const char* dbusProp,
358 const char* dbusInterface)
359 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400360 auto VariantValue =
361 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800362 return std::get<Property>(VariantValue);
363 }
George Liu1e44c732020-02-28 20:20:06 +0800364
365 /** @brief Set Dbus property
366 *
367 * @param[in] dBusMap - Object path, property name, interface and property
368 * type for the D-Bus object
369 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800370 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500371 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800372 */
373 void setDbusProperty(const DBusMapping& dBusMap,
374 const PropertyValue& value) const override;
Riya Dixit754041d2024-02-20 06:15:49 -0600375
376 /** @brief This function retrieves the properties of an object managed
377 * by the specified D-Bus service located at the given object path.
378 *
379 * @param[in] service - The D-Bus service providing the managed object
380 * @param[in] value - The object path of the managed object
381 *
382 * @return A hierarchical structure representing the properties of the
383 * managed object.
Patrick Williams897b0f82024-04-14 02:31:48 -0500384 * @throw sdbusplus::exception_t when it fails
Riya Dixit754041d2024-02-20 06:15:49 -0600385 */
386 static ObjectValueTree getManagedObj(const char* service, const char* path);
387
388 /** @brief Retrieve the inventory objects managed by a specified class.
389 * The retrieved inventory objects are cached statically
390 * and returned upon subsequent calls to this function.
391 *
392 * @tparam ClassType - The class type that manages the inventory objects.
393 *
394 * @return A reference to the cached inventory objects.
395 */
396 template <typename ClassType>
397 static auto& getInventoryObjects()
398 {
399 static ObjectValueTree object = ClassType::getManagedObj(
400 inventoryManager::interface, inventoryPath);
401 return object;
402 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500403};
404
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500405/** @brief Fetch parent D-Bus object based on pathname
406 *
407 * @param[in] dbusObj - child D-Bus object
408 *
409 * @return std::string - the parent D-Bus object path
410 */
411inline std::string findParent(const std::string& dbusObj)
412{
413 fs::path p(dbusObj);
414 return p.parent_path().string();
415}
416
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500417/** @brief Read (static) MCTP EID of host firmware from a file
418 *
419 * @return uint8_t - MCTP EID
420 */
421uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530422
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000423/** @brief Validate the MCTP EID of MCTP endpoint
424 * In `Table 2 - Special endpoint IDs` of DSP0236. EID 0 is NULL_EID.
425 * EID from 1 to 7 is reserved EID. EID 0xFF is broadcast EID.
426 * Those are invalid EID of one MCTP Endpoint.
427 *
428 * @param[in] eid - MCTP EID
429 *
430 * @return true if the MCTP EID is valid otherwise return false.
431 */
432bool isValidEID(eid mctpEid);
433
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530434/** @brief Convert a value in the JSON to a D-Bus property value
435 *
436 * @param[in] type - type of the D-Bus property
437 * @param[in] value - value in the JSON file
438 *
439 * @return PropertyValue - the D-Bus property value
440 */
441PropertyValue jsonEntryToDbusVal(std::string_view type,
442 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500443
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500444/** @brief Find State Effecter PDR
445 * @param[in] tid - PLDM terminus ID.
446 * @param[in] entityID - entity that can be associated with PLDM State set.
447 * @param[in] stateSetId - value that identifies PLDM State set.
448 * @param[in] repo - pointer to BMC's primary PDR repo.
449 * @return array[array[uint8_t]] - StateEffecterPDRs
450 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400451std::vector<std::vector<uint8_t>> findStateEffecterPDR(
452 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800453/** @brief Find State Sensor PDR
454 * @param[in] tid - PLDM terminus ID.
455 * @param[in] entityID - entity that can be associated with PLDM State set.
456 * @param[in] stateSetId - value that identifies PLDM State set.
457 * @param[in] repo - pointer to BMC's primary PDR repo.
458 * @return array[array[uint8_t]] - StateSensorPDRs
459 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400460std::vector<std::vector<uint8_t>> findStateSensorPDR(
461 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500462
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500463/** @brief Find sensor id from a state sensor PDR
464 *
465 * @param[in] pdrRepo - PDR repository
466 * @param[in] tid - terminus id
467 * @param[in] entityType - entity type
468 * @param[in] entityInstance - entity instance number
469 * @param[in] containerId - container id
470 * @param[in] stateSetId - state set id
471 *
472 * @return uint16_t - the sensor id
473 */
474uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
475 uint16_t entityType, uint16_t entityInstance,
476 uint16_t containerId, uint16_t stateSetId);
477
Tom Joseph250c4752020-04-15 10:32:45 +0530478/** @brief Find effecter id from a state effecter pdr
479 * @param[in] pdrRepo - PDR repository
480 * @param[in] entityType - entity type
481 * @param[in] entityInstance - entity instance number
482 * @param[in] containerId - container id
483 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500484 * @param[in] localOrRemote - true for checking local repo and false for remote
485 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530486 *
487 * @return uint16_t - the effecter id
488 */
489uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
490 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500491 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530492
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800493/** @brief Emit the sensor event signal
494 *
495 * @param[in] tid - the terminus id
496 * @param[in] sensorId - sensorID value of the sensor
497 * @param[in] sensorOffset - Identifies which state sensor within a
498 * composite state sensor the event is being returned for
499 * @param[in] eventState - The event state value from the state change that
500 * triggered the event message
501 * @param[in] previousEventState - The event state value for the state from
502 * which the present event state was entered.
503 * @return PLDM completion code
504 */
505int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
506 uint8_t sensorOffset, uint8_t eventState,
507 uint8_t previousEventState);
508
Chau Ly8fa40db2024-04-02 09:32:01 +0000509/**
510 * @brief call Recover() method to recover an MCTP Endpoint
511 * @param[in] MCTP Endpoint's object path
512 */
513void recoverMctpEndpoint(const std::string& endpointObjPath);
514
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600515/** @brief Print the buffer
516 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530517 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
518 the buffer is an incoming PLDM message
519 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600520 *
521 * @return - None
522 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530523void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600524
Tom Joseph54922072021-06-19 02:45:46 -0700525/** @brief Convert the buffer to std::string
526 *
527 * If there are characters that are not printable characters, it is replaced
528 * with space(0x20).
529 *
530 * @param[in] var - pointer to data and length of the data
531 *
532 * @return std::string equivalent of variable field
533 */
534std::string toString(const struct variable_field& var);
535
George Liu872f0f62021-11-25 16:26:16 +0800536/** @brief Split strings according to special identifiers
537 *
538 * We can split the string according to the custom identifier(';', ',', '&' or
539 * others) and store it to vector.
540 *
541 * @param[in] srcStr - The string to be split
542 * @param[in] delim - The custom identifier
543 * @param[in] trimStr - The first and last string to be trimmed
544 *
545 * @return std::vector<std::string> Vectors are used to store strings
546 */
547std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
548 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530549/** @brief Get the current system time in readable format
550 *
551 * @return - std::string equivalent of the system time
552 */
553std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800554
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500555/** @brief checks if the FRU is actually present.
556 * @param[in] objPath - FRU object path.
557 *
558 * @return bool to indicate presence or absence of FRU.
559 */
560bool checkForFruPresence(const std::string& objPath);
561
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600562/** @brief Method to check if the logical bit is set
563 *
564 * @param[containerId] - container id of the entity
565 *
566 * @return true or false based on the logic bit set
567 */
568bool checkIfLogicalBitSet(const uint16_t& containerId);
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500569
570/** @brief setting the present property
571 *
572 * @param[in] objPath - the object path of the fru
573 * @param[in] present - status to set either true/false
574 */
575void setFruPresence(const std::string& fruObjPath, bool present);
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000576
577/** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus
578 * object path
579 *
580 * @param[in] name - the input string
581 *
582 * @return the result string
583 */
584std::string_view trimNameForDbus(std::string& name);
585
Thu Nguyena34a64b2022-03-31 08:56:39 +0700586/** @brief Convert the number type D-Bus Value to the double
587 *
588 * @param[in] type - string type should in dbusValueNumericTypeNames list
589 * @param[in] value - DBus PropertyValue variant
590 * @param[out] doubleValue - response value
591 *
592 * @return true if data type is corrected and converting is successful
593 * otherwise return false.
594 */
595bool dbusPropValuesToDouble(const std::string_view& type,
596 const pldm::utils::PropertyValue& value,
597 double* doubleValue);
598
Dung Caob6d39432024-06-05 03:46:47 +0000599/** @brief Convert the Fru String bytes from PLDM Fru to std::string
600 *
601 * @param[in] value - the Fru String bytes
602 * @param[in] length - Number of bytes
603 *
604 * @return Fru string or nullopt.
605 */
Patrick Williams366507c2025-02-03 14:28:01 -0500606std::optional<std::string> fruFieldValuestring(const uint8_t* value,
607 const uint8_t& length);
Dung Caob6d39432024-06-05 03:46:47 +0000608
609/** @brief Convert the Fru Uint32 raw data from PLDM Fru to uint32_t
610 *
611 * @param[in] value - the Fru uint32 raw data
612 * @param[in] length - Number of bytes
613 *
614 * @return Fru uint32_t or nullopt.
615 */
616std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
617 const uint8_t& length);
618
George Liu83409572019-12-24 18:42:54 +0800619} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600620} // namespace pldm