blob: 6c17191f6dbc381610eb3a8754f73b496b98db14 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
Sampa Misraaea5dde2020-08-31 08:33:47 -05003#include "types.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/base.h>
6#include <libpldm/bios.h>
George Liudf9a6d32020-12-22 16:27:16 +08007#include <libpldm/entity.h>
8#include <libpldm/pdr.h>
George Liuc453e162022-12-21 17:16:23 +08009#include <libpldm/platform.h>
10#include <libpldm/utils.h>
Sampa Misra032bd502019-03-06 05:03:22 -060011#include <stdint.h>
12#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060014
George Liu6492f522020-06-16 10:34:05 +080015#include <nlohmann/json.hpp>
16#include <sdbusplus/server.hpp>
Riya Dixit754041d2024-02-20 06:15:49 -060017#include <xyz/openbmc_project/Inventory/Manager/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080018#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Gilbert Chen44524a52022-02-14 12:12:25 +000019#include <xyz/openbmc_project/ObjectMapper/client.hpp>
George Liu6492f522020-06-16 10:34:05 +080020
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
40const std::set<std::string_view> dbusValueTypeNames = {
41 "bool", "uint8_t", "int16_t", "uint16_t",
42 "int32_t", "uint32_t", "int64_t", "uint64_t",
43 "double", "string", "vector<uint8_t>", "vector<string>"};
44const std::set<std::string_view> dbusValueNumericTypeNames = {
45 "uint8_t", "int16_t", "uint16_t", "int32_t",
46 "uint32_t", "int64_t", "uint64_t", "double"};
47
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050048namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053049using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053050constexpr bool Tx = true;
51constexpr bool Rx = false;
Gilbert Chen44524a52022-02-14 12:12:25 +000052using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
Gilbert Chen44524a52022-02-14 12:12:25 +000053using inventoryManager =
54 sdbusplus::client::xyz::openbmc_project::inventory::Manager<>;
55
56constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
57constexpr auto mapperService = ObjectMapper::default_service;
58constexpr auto inventoryPath = "/xyz/openbmc_project/inventory";
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050059/** @struct CustomFD
60 *
61 * RAII wrapper for file descriptor.
62 */
63struct CustomFD
64{
65 CustomFD(const CustomFD&) = delete;
66 CustomFD& operator=(const CustomFD&) = delete;
67 CustomFD(CustomFD&&) = delete;
68 CustomFD& operator=(CustomFD&&) = delete;
69
Patrick Williams6da4f912023-05-10 07:50:53 -050070 CustomFD(int fd) : fd(fd) {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050071
72 ~CustomFD()
73 {
74 if (fd >= 0)
75 {
76 close(fd);
77 }
78 }
79
80 int operator()() const
81 {
82 return fd;
83 }
84
85 private:
86 int fd = -1;
87};
88
Sampa Misrab37be312019-07-03 02:26:41 -050089/** @brief Calculate the pad for PLDM data
90 *
91 * @param[in] data - Length of the data
92 * @return - uint8_t - number of pad bytes
93 */
94uint8_t getNumPadBytes(uint32_t data);
95
George Liu83409572019-12-24 18:42:54 +080096/** @brief Convert uint64 to date
97 *
98 * @param[in] data - time date of uint64
99 * @param[out] year - year number in dec
100 * @param[out] month - month number in dec
101 * @param[out] day - day of the month in dec
102 * @param[out] hour - number of hours in dec
103 * @param[out] min - number of minutes in dec
104 * @param[out] sec - number of seconds in dec
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530105 * @return true if decode success, false if decode failed
George Liu83409572019-12-24 18:42:54 +0800106 */
107bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
108 uint8_t* hour, uint8_t* min, uint8_t* sec);
109
110/** @brief Convert effecter data to structure of set_effecter_state_field
111 *
112 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +0800113 * @param[in] effecterCount - the number of individual sets of effecter
114 * information
115 * @return[out] parse success and get a valid set_effecter_state_field
116 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +0800117 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400118std::optional<std::vector<set_effecter_state_field>> parseEffecterData(
119 const std::vector<uint8_t>& effecterData, uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -0600120
121/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600122 * @brief creates an error log
123 * @param[in] errorMsg - the error message
124 */
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530125void reportError(const char* errorMsg);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600126
Sampa Misra032bd502019-03-06 05:03:22 -0600127/** @brief Convert any Decimal number to BCD
128 *
129 * @tparam[in] decimal - Decimal number
130 * @return Corresponding BCD number
131 */
132template <typename T>
133T decimalToBcd(T decimal)
134{
135 T bcd = 0;
136 T rem = 0;
137 auto cnt = 0;
138
139 while (decimal)
140 {
141 rem = decimal % 10;
142 bcd = bcd + (rem << cnt);
143 decimal = decimal / 10;
144 cnt += 4;
145 }
146
147 return bcd;
148}
149
George Liu1e44c732020-02-28 20:20:06 +0800150struct DBusMapping
151{
152 std::string objectPath; //!< D-Bus object path
153 std::string interface; //!< D-Bus interface
154 std::string propertyName; //!< D-Bus property name
155 std::string propertyType; //!< D-Bus property type
156};
157
158using PropertyValue =
159 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Riya Dixit754041d2024-02-20 06:15:49 -0600160 uint64_t, double, std::string, std::vector<uint8_t>,
161 std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500162using DbusProp = std::string;
163using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500164using DBusInterfaceAdded = std::vector<
165 std::pair<pldm::dbus::Interface,
166 std::vector<std::pair<pldm::dbus::Property,
167 std::variant<pldm::dbus::Property>>>>>;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500168
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530169using ObjectPath = std::string;
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500170using EntityName = std::string;
171using Entities = std::vector<pldm_entity_node*>;
172using EntityAssociations = std::vector<Entities>;
173using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
174using EntityMaps = std::map<pldm::pdr::EntityType, EntityName>;
175
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530176using ServiceName = std::string;
177using Interfaces = std::vector<std::string>;
178using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
179using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500180using GetSubTreePathsResponse = std::vector<std::string>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500181using PropertyMap = std::map<std::string, PropertyValue>;
182using InterfaceMap = std::map<std::string, PropertyMap>;
Riya Dixit754041d2024-02-20 06:15:49 -0600183using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>;
George Liu1e44c732020-02-28 20:20:06 +0800184
185/**
186 * @brief The interface for DBusHandler
187 */
188class DBusHandlerInterface
189{
190 public:
191 virtual ~DBusHandlerInterface() = default;
192
George Liu36e81352020-07-01 14:40:30 +0800193 virtual std::string getService(const char* path,
194 const char* interface) const = 0;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530195 virtual GetSubTreeResponse
196 getSubtree(const std::string& path, int depth,
197 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800198
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500199 virtual GetSubTreePathsResponse
200 getSubTreePaths(const std::string& objectPath, int depth,
201 const std::vector<std::string>& ifaceList) const = 0;
202
George Liu1e44c732020-02-28 20:20:06 +0800203 virtual void setDbusProperty(const DBusMapping& dBusMap,
204 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800205
206 virtual PropertyValue
207 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
208 const char* dbusInterface) const = 0;
Gilbert Chen44524a52022-02-14 12:12:25 +0000209
210 virtual PropertyMap
211 getDbusPropertiesVariant(const char* serviceName, const char* objPath,
212 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800213};
214
Sampa Misraa2fa0702019-05-31 01:28:55 -0500215/**
216 * @class DBusHandler
217 *
218 * Wrapper class to handle the D-Bus calls
219 *
220 * This class contains the APIs to handle the D-Bus calls
221 * to cater the request from pldm requester.
222 * A class is created to mock the apis in the test cases
223 */
George Liu1e44c732020-02-28 20:20:06 +0800224class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500225{
226 public:
George Liu0e02c322020-01-01 09:41:51 +0800227 /** @brief Get the bus connection. */
228 static auto& getBus()
229 {
230 static auto bus = sdbusplus::bus::new_default();
231 return bus;
232 }
233
234 /**
235 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800236 *
George Liu0e02c322020-01-01 09:41:51 +0800237 * @param[in] path - DBUS object path
238 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800239 *
George Liu0e02c322020-01-01 09:41:51 +0800240 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800241 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500242 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800243 */
George Liu36e81352020-07-01 14:40:30 +0800244 std::string getService(const char* path,
245 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800246
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530247 /**
248 * @brief Get the Subtree response from the mapper
249 *
250 * @param[in] path - DBUS object path
251 * @param[in] depth - Search depth
252 * @param[in] ifaceList - list of the interface that are being
253 * queried from the mapper
254 *
255 * @return GetSubTreeResponse - the mapper subtree response
256 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500257 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530258 */
259 GetSubTreeResponse
260 getSubtree(const std::string& path, int depth,
261 const std::vector<std::string>& ifaceList) const override;
262
Pavithra Barithaya2ec82692024-04-29 06:31:10 -0500263 /** @brief Get Subtree path response from the mapper
264 *
265 * @param[in] path - DBUS object path
266 * @param[in] depth - Search depth
267 * @param[in] ifaceList - list of the interface that are being
268 * queried from the mapper
269 *
270 * @return std::vector<std::string> vector of subtree paths
271 */
272 GetSubTreePathsResponse getSubTreePaths(
273 const std::string& objectPath, int depth,
274 const std::vector<std::string>& ifaceList) const override;
275
John Wang9e242422020-03-05 08:37:50 +0800276 /** @brief Get property(type: variant) from the requested dbus
277 *
278 * @param[in] objPath - The Dbus object path
279 * @param[in] dbusProp - The property name to get
280 * @param[in] dbusInterface - The Dbus interface
281 *
282 * @return The value of the property(type: variant)
283 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500284 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800285 */
286 PropertyValue
287 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
288 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800289
Gilbert Chen44524a52022-02-14 12:12:25 +0000290 /** @brief Get All properties(type: variant) from the requested dbus
291 *
292 * @param[in] serviceName - The Dbus service name
293 * @param[in] objPath - The Dbus object path
294 * @param[in] dbusInterface - The Dbus interface
295 *
296 * @return The values of the properties(type: variant)
297 *
298 * @throw sdbusplus::exception_t when it fails
299 */
300 PropertyMap
301 getDbusPropertiesVariant(const char* serviceName, const char* objPath,
302 const char* dbusInterface) const override;
303
John Wang9e242422020-03-05 08:37:50 +0800304 /** @brief The template function to get property from the requested dbus
305 * path
306 *
307 * @tparam Property - Excepted type of the property on dbus
308 *
309 * @param[in] objPath - The Dbus object path
310 * @param[in] dbusProp - The property name to get
311 * @param[in] dbusInterface - The Dbus interface
312 *
313 * @return The value of the property
314 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500315 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800316 * std::bad_variant_access when \p Property and property on dbus do
317 * not match
318 */
John Wang92b3c972019-10-17 11:06:41 +0800319 template <typename Property>
320 auto getDbusProperty(const char* objPath, const char* dbusProp,
321 const char* dbusInterface)
322 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400323 auto VariantValue =
324 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800325 return std::get<Property>(VariantValue);
326 }
George Liu1e44c732020-02-28 20:20:06 +0800327
328 /** @brief Set Dbus property
329 *
330 * @param[in] dBusMap - Object path, property name, interface and property
331 * type for the D-Bus object
332 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800333 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500334 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800335 */
336 void setDbusProperty(const DBusMapping& dBusMap,
337 const PropertyValue& value) const override;
Riya Dixit754041d2024-02-20 06:15:49 -0600338
339 /** @brief This function retrieves the properties of an object managed
340 * by the specified D-Bus service located at the given object path.
341 *
342 * @param[in] service - The D-Bus service providing the managed object
343 * @param[in] value - The object path of the managed object
344 *
345 * @return A hierarchical structure representing the properties of the
346 * managed object.
Patrick Williams897b0f82024-04-14 02:31:48 -0500347 * @throw sdbusplus::exception_t when it fails
Riya Dixit754041d2024-02-20 06:15:49 -0600348 */
349 static ObjectValueTree getManagedObj(const char* service, const char* path);
350
351 /** @brief Retrieve the inventory objects managed by a specified class.
352 * The retrieved inventory objects are cached statically
353 * and returned upon subsequent calls to this function.
354 *
355 * @tparam ClassType - The class type that manages the inventory objects.
356 *
357 * @return A reference to the cached inventory objects.
358 */
359 template <typename ClassType>
360 static auto& getInventoryObjects()
361 {
362 static ObjectValueTree object = ClassType::getManagedObj(
363 inventoryManager::interface, inventoryPath);
364 return object;
365 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500366};
367
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500368/** @brief Fetch parent D-Bus object based on pathname
369 *
370 * @param[in] dbusObj - child D-Bus object
371 *
372 * @return std::string - the parent D-Bus object path
373 */
374inline std::string findParent(const std::string& dbusObj)
375{
376 fs::path p(dbusObj);
377 return p.parent_path().string();
378}
379
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500380/** @brief Read (static) MCTP EID of host firmware from a file
381 *
382 * @return uint8_t - MCTP EID
383 */
384uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530385
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000386/** @brief Validate the MCTP EID of MCTP endpoint
387 * In `Table 2 - Special endpoint IDs` of DSP0236. EID 0 is NULL_EID.
388 * EID from 1 to 7 is reserved EID. EID 0xFF is broadcast EID.
389 * Those are invalid EID of one MCTP Endpoint.
390 *
391 * @param[in] eid - MCTP EID
392 *
393 * @return true if the MCTP EID is valid otherwise return false.
394 */
395bool isValidEID(eid mctpEid);
396
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530397/** @brief Convert a value in the JSON to a D-Bus property value
398 *
399 * @param[in] type - type of the D-Bus property
400 * @param[in] value - value in the JSON file
401 *
402 * @return PropertyValue - the D-Bus property value
403 */
404PropertyValue jsonEntryToDbusVal(std::string_view type,
405 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500406
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500407/** @brief Find State Effecter PDR
408 * @param[in] tid - PLDM terminus ID.
409 * @param[in] entityID - entity that can be associated with PLDM State set.
410 * @param[in] stateSetId - value that identifies PLDM State set.
411 * @param[in] repo - pointer to BMC's primary PDR repo.
412 * @return array[array[uint8_t]] - StateEffecterPDRs
413 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400414std::vector<std::vector<uint8_t>> findStateEffecterPDR(
415 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800416/** @brief Find State Sensor PDR
417 * @param[in] tid - PLDM terminus ID.
418 * @param[in] entityID - entity that can be associated with PLDM State set.
419 * @param[in] stateSetId - value that identifies PLDM State set.
420 * @param[in] repo - pointer to BMC's primary PDR repo.
421 * @return array[array[uint8_t]] - StateSensorPDRs
422 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400423std::vector<std::vector<uint8_t>> findStateSensorPDR(
424 uint8_t tid, uint16_t entityID, uint16_t stateSetId, const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500425
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500426/** @brief Find sensor id from a state sensor PDR
427 *
428 * @param[in] pdrRepo - PDR repository
429 * @param[in] tid - terminus id
430 * @param[in] entityType - entity type
431 * @param[in] entityInstance - entity instance number
432 * @param[in] containerId - container id
433 * @param[in] stateSetId - state set id
434 *
435 * @return uint16_t - the sensor id
436 */
437uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
438 uint16_t entityType, uint16_t entityInstance,
439 uint16_t containerId, uint16_t stateSetId);
440
Tom Joseph250c4752020-04-15 10:32:45 +0530441/** @brief Find effecter id from a state effecter pdr
442 * @param[in] pdrRepo - PDR repository
443 * @param[in] entityType - entity type
444 * @param[in] entityInstance - entity instance number
445 * @param[in] containerId - container id
446 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500447 * @param[in] localOrRemote - true for checking local repo and false for remote
448 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530449 *
450 * @return uint16_t - the effecter id
451 */
452uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
453 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500454 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530455
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800456/** @brief Emit the sensor event signal
457 *
458 * @param[in] tid - the terminus id
459 * @param[in] sensorId - sensorID value of the sensor
460 * @param[in] sensorOffset - Identifies which state sensor within a
461 * composite state sensor the event is being returned for
462 * @param[in] eventState - The event state value from the state change that
463 * triggered the event message
464 * @param[in] previousEventState - The event state value for the state from
465 * which the present event state was entered.
466 * @return PLDM completion code
467 */
468int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
469 uint8_t sensorOffset, uint8_t eventState,
470 uint8_t previousEventState);
471
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600472/** @brief Print the buffer
473 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530474 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
475 the buffer is an incoming PLDM message
476 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600477 *
478 * @return - None
479 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530480void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600481
Tom Joseph54922072021-06-19 02:45:46 -0700482/** @brief Convert the buffer to std::string
483 *
484 * If there are characters that are not printable characters, it is replaced
485 * with space(0x20).
486 *
487 * @param[in] var - pointer to data and length of the data
488 *
489 * @return std::string equivalent of variable field
490 */
491std::string toString(const struct variable_field& var);
492
George Liu872f0f62021-11-25 16:26:16 +0800493/** @brief Split strings according to special identifiers
494 *
495 * We can split the string according to the custom identifier(';', ',', '&' or
496 * others) and store it to vector.
497 *
498 * @param[in] srcStr - The string to be split
499 * @param[in] delim - The custom identifier
500 * @param[in] trimStr - The first and last string to be trimmed
501 *
502 * @return std::vector<std::string> Vectors are used to store strings
503 */
504std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
505 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530506/** @brief Get the current system time in readable format
507 *
508 * @return - std::string equivalent of the system time
509 */
510std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800511
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500512/** @brief checks if the FRU is actually present.
513 * @param[in] objPath - FRU object path.
514 *
515 * @return bool to indicate presence or absence of FRU.
516 */
517bool checkForFruPresence(const std::string& objPath);
518
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600519/** @brief Method to check if the logical bit is set
520 *
521 * @param[containerId] - container id of the entity
522 *
523 * @return true or false based on the logic bit set
524 */
525bool checkIfLogicalBitSet(const uint16_t& containerId);
Pavithra Barithaya5e542be2021-08-13 00:33:31 -0500526
527/** @brief setting the present property
528 *
529 * @param[in] objPath - the object path of the fru
530 * @param[in] present - status to set either true/false
531 */
532void setFruPresence(const std::string& fruObjPath, bool present);
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000533
534/** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus
535 * object path
536 *
537 * @param[in] name - the input string
538 *
539 * @return the result string
540 */
541std::string_view trimNameForDbus(std::string& name);
542
Thu Nguyena34a64b2022-03-31 08:56:39 +0700543/** @brief Convert the number type D-Bus Value to the double
544 *
545 * @param[in] type - string type should in dbusValueNumericTypeNames list
546 * @param[in] value - DBus PropertyValue variant
547 * @param[out] doubleValue - response value
548 *
549 * @return true if data type is corrected and converting is successful
550 * otherwise return false.
551 */
552bool dbusPropValuesToDouble(const std::string_view& type,
553 const pldm::utils::PropertyValue& value,
554 double* doubleValue);
555
George Liu83409572019-12-24 18:42:54 +0800556} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600557} // namespace pldm