blob: b2b5c3c0ea368eacf5fc3b5e7ba3f3928b474e1f [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";
Unive Tienc40d4a62025-03-12 11:36:07 +080075
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
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530122 * @return true if decode success, false if decode failed
George Liu83409572019-12-24 18:42:54 +0800123 */
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 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400135std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
136 const std::vector<uint8_t>& effecterData, uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -0600137
138/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600139 * @brief creates an error log
140 * @param[in] errorMsg - the error message
141 */
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530142void reportError(const char* errorMsg);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600143
Sampa Misra032bd502019-03-06 05:03:22 -0600144/** @brief Convert any Decimal number to BCD
145 *
146 * @tparam[in] decimal - Decimal number
147 * @return Corresponding BCD number
148 */
149template <typename T>
150T decimalToBcd(T decimal)
151{
152 T bcd = 0;
153 T rem = 0;
154 auto cnt = 0;
155
156 while (decimal)
157 {
158 rem = decimal % 10;
159 bcd = bcd + (rem << cnt);
160 decimal = decimal / 10;
161 cnt += 4;
162 }
163
164 return bcd;
165}
166
George Liu1e44c732020-02-28 20:20:06 +0800167struct DBusMapping
168{
169 std::string objectPath; //!< D-Bus object path
170 std::string interface; //!< D-Bus interface
171 std::string propertyName; //!< D-Bus property name
172 std::string propertyType; //!< D-Bus property type
173};
174
175using PropertyValue =
176 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Riya Dixit754041d2024-02-20 06:15:49 -0600177 uint64_t, double, std::string, std::vector<uint8_t>,
Unive Tienc40d4a62025-03-12 11:36:07 +0800178 std::vector<uint64_t>, std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500179using DbusProp = std::string;
180using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500181using DBusInterfaceAdded = std::vector<
182 std::pair<pldm::dbus::Interface,
183 std::vector<std::pair<pldm::dbus::Property,
184 std::variant<pldm::dbus::Property>>>>>;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500185
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530186using ObjectPath = std::string;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500187using EntityName = std::string;
188using Entities = std::vector<pldm_entity_node*>;
189using EntityAssociations = std::vector<Entities>;
190using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
191using EntityMaps = std::map<pldm::pdr::EntityType, EntityName>;
192
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530193using ServiceName = std::string;
194using Interfaces = std::vector<std::string>;
195using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
196using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500197using GetSubTreePathsResponse = std::vector<std::string>;
Unive Tienc40d4a62025-03-12 11:36:07 +0800198using GetAssociatedSubTreeResponse =
199 std::map<std::string, std::map<std::string, std::vector<std::string>>>;
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800200using GetAncestorsResponse =
201 std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500202using PropertyMap = std::map<std::string, PropertyValue>;
203using InterfaceMap = std::map<std::string, PropertyMap>;
Riya Dixit754041d2024-02-20 06:15:49 -0600204using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
George Liu1e44c732020-02-28 20:20:06 +0800205
Pavithra Barithaya36ac5592025-06-16 11:41:37 +0530206using SensorPDR = std::vector<uint8_t>;
207using SensorPDRs = std::vector<SensorPDR>;
208
George Liu1e44c732020-02-28 20:20:06 +0800209/**
210 * @brief The interface for DBusHandler
211 */
212class DBusHandlerInterface
213{
214 public:
215 virtual ~DBusHandlerInterface() = default;
216
George Liu36e81352020-07-01 14:40:30 +0800217 virtual std::string getService(const char* path,
218 const char* interface) const = 0;
Patrick Williams366507c2025-02-03 14:28:01 -0500219 virtual GetSubTreeResponse getSubtree(
220 const std::string& path, int depth,
221 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800222
Patrick Williams366507c2025-02-03 14:28:01 -0500223 virtual GetSubTreePathsResponse getSubTreePaths(
224 const std::string& objectPath, int depth,
225 const std::vector<std::string>& ifaceList) const = 0;
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500226
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800227 virtual GetAncestorsResponse getAncestors(
228 const std::string& path,
229 const std::vector<std::string>& ifaceList) const = 0;
230
George Liu1e44c732020-02-28 20:20:06 +0800231 virtual void setDbusProperty(const DBusMapping& dBusMap,
232 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800233
Patrick Williams366507c2025-02-03 14:28:01 -0500234 virtual PropertyValue getDbusPropertyVariant(
235 const char* objPath, const char* dbusProp,
236 const char* dbusInterface) const = 0;
Gilbert Chen44524a52022-02-14 12:12:25 +0000237
Patrick Williams366507c2025-02-03 14:28:01 -0500238 virtual PropertyMap getDbusPropertiesVariant(
239 const char* serviceName, const char* objPath,
240 const char* dbusInterface) const = 0;
Unive Tienc40d4a62025-03-12 11:36:07 +0800241
242 virtual GetAssociatedSubTreeResponse getAssociatedSubTree(
243 const sdbusplus::message::object_path& objectPath,
244 const sdbusplus::message::object_path& subtree, int depth,
245 const std::vector<std::string>& ifaceList) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800246};
247
Sampa Misraa2fa0702019-05-31 01:28:55 -0500248/**
249 * @class DBusHandler
250 *
251 * Wrapper class to handle the D-Bus calls
252 *
253 * This class contains the APIs to handle the D-Bus calls
254 * to cater the request from pldm requester.
255 * A class is created to mock the apis in the test cases
256 */
George Liu1e44c732020-02-28 20:20:06 +0800257class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500258{
259 public:
George Liu0e02c322020-01-01 09:41:51 +0800260 /** @brief Get the bus connection. */
261 static auto& getBus()
262 {
263 static auto bus = sdbusplus::bus::new_default();
264 return bus;
265 }
266
267 /**
268 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800269 *
George Liu0e02c322020-01-01 09:41:51 +0800270 * @param[in] path - DBUS object path
271 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800272 *
George Liu0e02c322020-01-01 09:41:51 +0800273 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800274 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500275 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800276 */
George Liu36e81352020-07-01 14:40:30 +0800277 std::string getService(const char* path,
278 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800279
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530280 /**
281 * @brief Get the Subtree response from the mapper
282 *
283 * @param[in] path - DBUS object path
284 * @param[in] depth - Search depth
285 * @param[in] ifaceList - list of the interface that are being
286 * queried from the mapper
287 *
288 * @return GetSubTreeResponse - the mapper subtree response
289 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500290 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530291 */
Patrick Williams366507c2025-02-03 14:28:01 -0500292 GetSubTreeResponse getSubtree(
293 const std::string& path, int depth,
294 const std::vector<std::string>& ifaceList) const override;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530295
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500296 /** @brief Get Subtree path response from the mapper
297 *
298 * @param[in] path - DBUS object path
299 * @param[in] depth - Search depth
300 * @param[in] ifaceList - list of the interface that are being
301 * queried from the mapper
302 *
303 * @return std::vector<std::string> vector of subtree paths
304 */
305 GetSubTreePathsResponse getSubTreePaths(
306 const std::string& objectPath, int depth,
307 const std::vector<std::string>& ifaceList) const override;
308
Delphine CC Chiu549e4bc2024-03-06 11:24:05 +0800309 /**
310 * @brief Get the Ancestors response from the mapper
311 *
312 * @param[in] path - D-Bus object path
313 * @param[in] ifaceList - an optional list of interfaces to constrain the
314 * search to queried from the mapper
315 *
316 * @return GetAncestorsResponse - the mapper GetAncestors response
317 *
318 * @throw sdbusplus::exception_t when it fails
319 */
320 GetAncestorsResponse getAncestors(
321 const std::string& path,
322 const std::vector<std::string>& ifaceList) const override;
323
John Wang9e242422020-03-05 08:37:50 +0800324 /** @brief Get property(type: variant) from the requested dbus
325 *
326 * @param[in] objPath - The Dbus object path
327 * @param[in] dbusProp - The property name to get
328 * @param[in] dbusInterface - The Dbus interface
329 *
330 * @return The value of the property(type: variant)
331 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500332 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800333 */
Patrick Williams366507c2025-02-03 14:28:01 -0500334 PropertyValue getDbusPropertyVariant(
335 const char* objPath, const char* dbusProp,
336 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800337
Gilbert Chen44524a52022-02-14 12:12:25 +0000338 /** @brief Get All properties(type: variant) from the requested dbus
339 *
340 * @param[in] serviceName - The Dbus service name
341 * @param[in] objPath - The Dbus object path
342 * @param[in] dbusInterface - The Dbus interface
343 *
344 * @return The values of the properties(type: variant)
345 *
346 * @throw sdbusplus::exception_t when it fails
347 */
Patrick Williams366507c2025-02-03 14:28:01 -0500348 PropertyMap getDbusPropertiesVariant(
349 const char* serviceName, const char* objPath,
350 const char* dbusInterface) const override;
Gilbert Chen44524a52022-02-14 12:12:25 +0000351
John Wang9e242422020-03-05 08:37:50 +0800352 /** @brief The template function to get property from the requested dbus
353 * path
354 *
355 * @tparam Property - Excepted type of the property on dbus
356 *
357 * @param[in] objPath - The Dbus object path
358 * @param[in] dbusProp - The property name to get
359 * @param[in] dbusInterface - The Dbus interface
360 *
361 * @return The value of the property
362 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500363 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800364 * std::bad_variant_access when \p Property and property on dbus do
365 * not match
366 */
John Wang92b3c972019-10-17 11:06:41 +0800367 template <typename Property>
368 auto getDbusProperty(const char* objPath, const char* dbusProp,
369 const char* dbusInterface)
370 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400371 auto VariantValue =
372 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800373 return std::get<Property>(VariantValue);
374 }
George Liu1e44c732020-02-28 20:20:06 +0800375
Unive Tienc40d4a62025-03-12 11:36:07 +0800376 /** @brief Get the associated subtree from the mapper
377 *
378 * @param[in] path - The D-Bus object path
379 *
380 * @param[in] interface - The D-Bus interface
381 *
382 * @return GetAssociatedSubtreeResponse - The associated subtree
383 */
384 GetAssociatedSubTreeResponse getAssociatedSubTree(
385 const sdbusplus::message::object_path& objectPath,
386 const sdbusplus::message::object_path& subtree, int depth,
387 const std::vector<std::string>& ifaceList) const override;
388
George Liu1e44c732020-02-28 20:20:06 +0800389 /** @brief Set Dbus property
390 *
391 * @param[in] dBusMap - Object path, property name, interface and property
392 * type for the D-Bus object
393 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800394 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500395 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800396 */
397 void setDbusProperty(const DBusMapping& dBusMap,
398 const PropertyValue& value) const override;
Riya Dixit754041d2024-02-20 06:15:49 -0600399
400 /** @brief This function retrieves the properties of an object managed
401 * by the specified D-Bus service located at the given object path.
402 *
403 * @param[in] service - The D-Bus service providing the managed object
404 * @param[in] value - The object path of the managed object
405 *
406 * @return A hierarchical structure representing the properties of the
407 * managed object.
Patrick Williams897b0f82024-04-14 02:31:48 -0500408 * @throw sdbusplus::exception_t when it fails
Riya Dixit754041d2024-02-20 06:15:49 -0600409 */
410 static ObjectValueTree getManagedObj(const char* service, const char* path);
411
412 /** @brief Retrieve the inventory objects managed by a specified class.
413 * The retrieved inventory objects are cached statically
414 * and returned upon subsequent calls to this function.
415 *
416 * @tparam ClassType - The class type that manages the inventory objects.
417 *
418 * @return A reference to the cached inventory objects.
419 */
420 template <typename ClassType>
421 static auto& getInventoryObjects()
422 {
423 static ObjectValueTree object = ClassType::getManagedObj(
424 inventoryManager::interface, inventoryPath);
425 return object;
426 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500427};
428
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500429/** @brief Fetch parent D-Bus object based on pathname
430 *
431 * @param[in] dbusObj - child D-Bus object
432 *
433 * @return std::string - the parent D-Bus object path
434 */
435inline std::string findParent(const std::string& dbusObj)
436{
437 fs::path p(dbusObj);
438 return p.parent_path().string();
439}
440
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500441/** @brief Read (static) MCTP EID of host firmware from a file
442 *
443 * @return uint8_t - MCTP EID
444 */
445uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530446
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000447/** @brief Validate the MCTP EID of MCTP endpoint
448 * In `Table 2 - Special endpoint IDs` of DSP0236. EID 0 is NULL_EID.
449 * EID from 1 to 7 is reserved EID. EID 0xFF is broadcast EID.
450 * Those are invalid EID of one MCTP Endpoint.
451 *
452 * @param[in] eid - MCTP EID
453 *
454 * @return true if the MCTP EID is valid otherwise return false.
455 */
456bool isValidEID(eid mctpEid);
457
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530458/** @brief Convert a value in the JSON to a D-Bus property value
459 *
460 * @param[in] type - type of the D-Bus property
461 * @param[in] value - value in the JSON file
462 *
463 * @return PropertyValue - the D-Bus property value
464 */
465PropertyValue jsonEntryToDbusVal(std::string_view type,
466 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500467
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500468/** @brief Find State Effecter PDR
469 * @param[in] tid - PLDM terminus ID.
470 * @param[in] entityID - entity that can be associated with PLDM State set.
471 * @param[in] stateSetId - value that identifies PLDM State set.
472 * @param[in] repo - pointer to BMC's primary PDR repo.
473 * @return array[array[uint8_t]] - StateEffecterPDRs
474 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400475std::vector<std::vector<uint8_t>> findStateEffecterPDR(
476 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800477/** @brief Find State Sensor PDR
478 * @param[in] tid - PLDM terminus ID.
479 * @param[in] entityID - entity that can be associated with PLDM State set.
480 * @param[in] stateSetId - value that identifies PLDM State set.
481 * @param[in] repo - pointer to BMC's primary PDR repo.
482 * @return array[array[uint8_t]] - StateSensorPDRs
483 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400484std::vector<std::vector<uint8_t>> findStateSensorPDR(
485 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500486
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500487/** @brief Find sensor id from a state sensor PDR
488 *
489 * @param[in] pdrRepo - PDR repository
490 * @param[in] tid - terminus id
491 * @param[in] entityType - entity type
492 * @param[in] entityInstance - entity instance number
493 * @param[in] containerId - container id
494 * @param[in] stateSetId - state set id
495 *
496 * @return uint16_t - the sensor id
497 */
498uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
499 uint16_t entityType, uint16_t entityInstance,
500 uint16_t containerId, uint16_t stateSetId);
501
Tom Joseph250c4752020-04-15 10:32:45 +0530502/** @brief Find effecter id from a state effecter pdr
503 * @param[in] pdrRepo - PDR repository
504 * @param[in] entityType - entity type
505 * @param[in] entityInstance - entity instance number
506 * @param[in] containerId - container id
507 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500508 * @param[in] localOrRemote - true for checking local repo and false for remote
509 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530510 *
511 * @return uint16_t - the effecter id
512 */
513uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
514 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500515 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530516
Pavithra Barithaya36ac5592025-06-16 11:41:37 +0530517/** @brief Method to find all state sensor PDRs by type
518 *
519 * @param[in] entityType - the entity type
520 * @param[in] repo - opaque pointer acting as a PDR repo handle
521 *
522 * @return vector of vector of all state sensor PDRs
523 */
524SensorPDRs getStateSensorPDRsByType(uint16_t entityType, const pldm_pdr* repo);
525
526/** @brief method to find sensor IDs based on the pldm_entity
527 *
528 * @param[in] pdrRepo - opaque pointer acting as a PDR repo handle
529 * @param[in] entityType - the entity type
530 * @param[in] entityInstance - the entity instance number
531 * @param[in] containerId - the container ID
532 *
533 * @return vector of all sensor IDs
534 */
535std::vector<pldm::pdr::SensorID> findSensorIds(
536 const pldm_pdr* pdrRepo, uint16_t entityType, uint16_t entityInstance,
537 uint16_t containerId);
538
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800539/** @brief Emit the sensor event signal
540 *
541 * @param[in] tid - the terminus id
542 * @param[in] sensorId - sensorID value of the sensor
543 * @param[in] sensorOffset - Identifies which state sensor within a
544 * composite state sensor the event is being returned for
545 * @param[in] eventState - The event state value from the state change that
546 * triggered the event message
547 * @param[in] previousEventState - The event state value for the state from
548 * which the present event state was entered.
549 * @return PLDM completion code
550 */
551int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
552 uint8_t sensorOffset, uint8_t eventState,
553 uint8_t previousEventState);
554
Chau Ly8fa40db2024-04-02 09:32:01 +0000555/**
556 * @brief call Recover() method to recover an MCTP Endpoint
557 * @param[in] MCTP Endpoint's object path
558 */
559void recoverMctpEndpoint(const std::string& endpointObjPath);
560
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600561/** @brief Print the buffer
562 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530563 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
564 the buffer is an incoming PLDM message
565 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600566 *
567 * @return - None
568 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530569void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600570
Tom Joseph54922072021-06-19 02:45:46 -0700571/** @brief Convert the buffer to std::string
572 *
573 * If there are characters that are not printable characters, it is replaced
574 * with space(0x20).
575 *
576 * @param[in] var - pointer to data and length of the data
577 *
578 * @return std::string equivalent of variable field
579 */
580std::string toString(const struct variable_field& var);
581
George Liu872f0f62021-11-25 16:26:16 +0800582/** @brief Split strings according to special identifiers
583 *
584 * We can split the string according to the custom identifier(';', ',', '&' or
585 * others) and store it to vector.
586 *
587 * @param[in] srcStr - The string to be split
588 * @param[in] delim - The custom identifier
589 * @param[in] trimStr - The first and last string to be trimmed
590 *
591 * @return std::vector<std::string> Vectors are used to store strings
592 */
593std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
594 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530595/** @brief Get the current system time in readable format
596 *
597 * @return - std::string equivalent of the system time
598 */
599std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800600
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500601/** @brief checks if the FRU is actually present.
602 * @param[in] objPath - FRU object path.
603 *
604 * @return bool to indicate presence or absence of FRU.
605 */
606bool checkForFruPresence(const std::string& objPath);
607
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600608/** @brief Method to check if the logical bit is set
609 *
610 * @param[containerId] - container id of the entity
611 *
612 * @return true or false based on the logic bit set
613 */
614bool checkIfLogicalBitSet(const uint16_t& containerId);
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500615
616/** @brief setting the present property
617 *
618 * @param[in] objPath - the object path of the fru
619 * @param[in] present - status to set either true/false
620 */
621void setFruPresence(const std::string& fruObjPath, bool present);
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000622
623/** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus
624 * object path
625 *
626 * @param[in] name - the input string
627 *
628 * @return the result string
629 */
630std::string_view trimNameForDbus(std::string& name);
631
Thu Nguyena34a64b2022-03-31 08:56:39 +0700632/** @brief Convert the number type D-Bus Value to the double
633 *
634 * @param[in] type - string type should in dbusValueNumericTypeNames list
635 * @param[in] value - DBus PropertyValue variant
636 * @param[out] doubleValue - response value
637 *
638 * @return true if data type is corrected and converting is successful
639 * otherwise return false.
640 */
641bool dbusPropValuesToDouble(const std::string_view& type,
642 const pldm::utils::PropertyValue& value,
643 double* doubleValue);
644
Dung Caob6d39432024-06-05 03:46:47 +0000645/** @brief Convert the Fru String bytes from PLDM Fru to std::string
646 *
647 * @param[in] value - the Fru String bytes
648 * @param[in] length - Number of bytes
649 *
650 * @return Fru string or nullopt.
651 */
Patrick Williams366507c2025-02-03 14:28:01 -0500652std::optional<std::string> fruFieldValuestring(const uint8_t* value,
653 const uint8_t& length);
Dung Caob6d39432024-06-05 03:46:47 +0000654
655/** @brief Convert the Fru Uint32 raw data from PLDM Fru to uint32_t
656 *
657 * @param[in] value - the Fru uint32 raw data
658 * @param[in] length - Number of bytes
659 *
660 * @return Fru uint32_t or nullopt.
661 */
662std::optional<uint32_t> fruFieldParserU32(const uint8_t* value,
663 const uint8_t& length);
664
George Liu83409572019-12-24 18:42:54 +0800665} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600666} // namespace pldm