blob: d78d172c23f618dd7746ff1765962dafe668f449 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
Sampa Misraaea5dde2020-08-31 08:33:47 -05003#include "types.hpp"
4
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/base.h>
6#include <libpldm/bios.h>
George Liudf9a6d32020-12-22 16:27:16 +08007#include <libpldm/entity.h>
8#include <libpldm/pdr.h>
George Liuc453e162022-12-21 17:16:23 +08009#include <libpldm/platform.h>
10#include <libpldm/utils.h>
Sampa Misra032bd502019-03-06 05:03:22 -060011#include <stdint.h>
12#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050013#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060014
George Liu6492f522020-06-16 10:34:05 +080015#include <nlohmann/json.hpp>
16#include <sdbusplus/server.hpp>
17#include <xyz/openbmc_project/Logging/Entry/server.hpp>
18
George Liudf9a6d32020-12-22 16:27:16 +080019#include <deque>
Sampa Misraa2fa0702019-05-31 01:28:55 -050020#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050021#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060022#include <iostream>
George Liudf9a6d32020-12-22 16:27:16 +080023#include <map>
Sampa Misra032bd502019-03-06 05:03:22 -060024#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050025#include <variant>
26#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060027
vkaverap@in.ibm.com9138c202023-05-19 07:50:47 -050028using microsec = std::chrono::microseconds;
29using sec = std::chrono::seconds;
30
Sampa Misra032bd502019-03-06 05:03:22 -060031namespace pldm
32{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033namespace utils
34{
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050035namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053036using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053037constexpr bool Tx = true;
38constexpr bool Rx = false;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050039
George Liudf9a6d32020-12-22 16:27:16 +080040using EntityName = std::string;
41using EntityType = uint16_t;
42
43using Entities = std::vector<pldm_entity_node*>;
44using EntityAssociations = std::vector<Entities>;
45using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
46
47const std::map<EntityType, EntityName> entityMaps = {
48 {PLDM_ENTITY_SYSTEM_CHASSIS, "chassis"},
49 {PLDM_ENTITY_BOARD, "io_board"},
50 {PLDM_ENTITY_SYS_BOARD, "motherboard"},
51 {PLDM_ENTITY_POWER_SUPPLY, "powersupply"},
52 {PLDM_ENTITY_PROC, "cpu"},
53 {PLDM_ENTITY_SYSTEM_CHASSIS | 0x8000, "system"},
54 {PLDM_ENTITY_PROC_MODULE, "dcm"},
55 {PLDM_ENTITY_PROC | 0x8000, "core"},
56 {PLDM_ENTITY_IO_MODULE, "io_module"},
57 {PLDM_ENTITY_FAN, "fan"},
58 {PLDM_ENTITY_SYS_MGMT_MODULE, "system_management_module"},
59 {PLDM_ENTITY_POWER_CONVERTER, "power_converter"},
60 {PLDM_ENTITY_SLOT, "slot"},
61 {PLDM_ENTITY_CONNECTOR, "connector"}};
62
63/** @brief Vector a entity name to pldm_entity from entity association tree
64 * @param[in] entityAssoc - Vector of associated pldm entities
65 * @param[in] entityTree - entity association tree
66 * @param[out] objPathMap - maps an object path to pldm_entity from the
67 * BMC's entity association tree
68 * @return
69 */
70void updateEntityAssociation(const EntityAssociations& entityAssoc,
71 pldm_entity_association_tree* entityTree,
72 ObjectPathMaps& objPathMap);
73
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050074/** @struct CustomFD
75 *
76 * RAII wrapper for file descriptor.
77 */
78struct CustomFD
79{
80 CustomFD(const CustomFD&) = delete;
81 CustomFD& operator=(const CustomFD&) = delete;
82 CustomFD(CustomFD&&) = delete;
83 CustomFD& operator=(CustomFD&&) = delete;
84
Patrick Williams6da4f912023-05-10 07:50:53 -050085 CustomFD(int fd) : fd(fd) {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050086
87 ~CustomFD()
88 {
89 if (fd >= 0)
90 {
91 close(fd);
92 }
93 }
94
95 int operator()() const
96 {
97 return fd;
98 }
99
100 private:
101 int fd = -1;
102};
103
Sampa Misrab37be312019-07-03 02:26:41 -0500104/** @brief Calculate the pad for PLDM data
105 *
106 * @param[in] data - Length of the data
107 * @return - uint8_t - number of pad bytes
108 */
109uint8_t getNumPadBytes(uint32_t data);
110
George Liu83409572019-12-24 18:42:54 +0800111/** @brief Convert uint64 to date
112 *
113 * @param[in] data - time date of uint64
114 * @param[out] year - year number in dec
115 * @param[out] month - month number in dec
116 * @param[out] day - day of the month in dec
117 * @param[out] hour - number of hours in dec
118 * @param[out] min - number of minutes in dec
119 * @param[out] sec - number of seconds in dec
120 * @return true if decode success, false if decode faild
121 */
122bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
123 uint8_t* hour, uint8_t* min, uint8_t* sec);
124
125/** @brief Convert effecter data to structure of set_effecter_state_field
126 *
127 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +0800128 * @param[in] effecterCount - the number of individual sets of effecter
129 * information
130 * @return[out] parse success and get a valid set_effecter_state_field
131 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +0800132 */
George Liuba4c1fb2020-02-05 14:13:30 +0800133std::optional<std::vector<set_effecter_state_field>>
134 parseEffecterData(const std::vector<uint8_t>& effecterData,
135 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 */
141void reportError(const char* errorMsg);
142
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
Sampa Misraa2fa0702019-05-31 01:28:55 -0500166constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500167constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500168
George Liu1e44c732020-02-28 20:20:06 +0800169struct DBusMapping
170{
171 std::string objectPath; //!< D-Bus object path
172 std::string interface; //!< D-Bus interface
173 std::string propertyName; //!< D-Bus property name
174 std::string propertyType; //!< D-Bus property type
175};
176
177using PropertyValue =
178 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500179 uint64_t, double, std::string, std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500180using DbusProp = std::string;
181using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500182using DBusInterfaceAdded = std::vector<
183 std::pair<pldm::dbus::Interface,
184 std::vector<std::pair<pldm::dbus::Property,
185 std::variant<pldm::dbus::Property>>>>>;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530186using ObjectPath = std::string;
187using ServiceName = std::string;
188using Interfaces = std::vector<std::string>;
189using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
190using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500191using PropertyMap = std::map<std::string, PropertyValue>;
192using InterfaceMap = std::map<std::string, PropertyMap>;
George Liu1e44c732020-02-28 20:20:06 +0800193
194/**
195 * @brief The interface for DBusHandler
196 */
197class DBusHandlerInterface
198{
199 public:
200 virtual ~DBusHandlerInterface() = default;
201
George Liu36e81352020-07-01 14:40:30 +0800202 virtual std::string getService(const char* path,
203 const char* interface) const = 0;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530204 virtual GetSubTreeResponse
205 getSubtree(const std::string& path, int depth,
206 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800207
George Liu1e44c732020-02-28 20:20:06 +0800208 virtual void setDbusProperty(const DBusMapping& dBusMap,
209 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800210
211 virtual PropertyValue
212 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
213 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800214};
215
Sampa Misraa2fa0702019-05-31 01:28:55 -0500216/**
217 * @class DBusHandler
218 *
219 * Wrapper class to handle the D-Bus calls
220 *
221 * This class contains the APIs to handle the D-Bus calls
222 * to cater the request from pldm requester.
223 * A class is created to mock the apis in the test cases
224 */
George Liu1e44c732020-02-28 20:20:06 +0800225class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500226{
227 public:
George Liu0e02c322020-01-01 09:41:51 +0800228 /** @brief Get the bus connection. */
229 static auto& getBus()
230 {
231 static auto bus = sdbusplus::bus::new_default();
232 return bus;
233 }
234
235 /**
236 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800237 *
George Liu0e02c322020-01-01 09:41:51 +0800238 * @param[in] path - DBUS object path
239 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800240 *
George Liu0e02c322020-01-01 09:41:51 +0800241 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800242 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500243 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800244 */
George Liu36e81352020-07-01 14:40:30 +0800245 std::string getService(const char* path,
246 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800247
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530248 /**
249 * @brief Get the Subtree response from the mapper
250 *
251 * @param[in] path - DBUS object path
252 * @param[in] depth - Search depth
253 * @param[in] ifaceList - list of the interface that are being
254 * queried from the mapper
255 *
256 * @return GetSubTreeResponse - the mapper subtree response
257 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500258 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530259 */
260 GetSubTreeResponse
261 getSubtree(const std::string& path, int depth,
262 const std::vector<std::string>& ifaceList) const override;
263
John Wang9e242422020-03-05 08:37:50 +0800264 /** @brief Get property(type: variant) from the requested dbus
265 *
266 * @param[in] objPath - The Dbus object path
267 * @param[in] dbusProp - The property name to get
268 * @param[in] dbusInterface - The Dbus interface
269 *
270 * @return The value of the property(type: variant)
271 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500272 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800273 */
274 PropertyValue
275 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
276 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800277
John Wang9e242422020-03-05 08:37:50 +0800278 /** @brief The template function to get property from the requested dbus
279 * path
280 *
281 * @tparam Property - Excepted type of the property on dbus
282 *
283 * @param[in] objPath - The Dbus object path
284 * @param[in] dbusProp - The property name to get
285 * @param[in] dbusInterface - The Dbus interface
286 *
287 * @return The value of the property
288 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500289 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800290 * std::bad_variant_access when \p Property and property on dbus do
291 * not match
292 */
John Wang92b3c972019-10-17 11:06:41 +0800293 template <typename Property>
294 auto getDbusProperty(const char* objPath, const char* dbusProp,
295 const char* dbusInterface)
296 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500297 auto VariantValue = getDbusPropertyVariant(objPath, dbusProp,
298 dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800299 return std::get<Property>(VariantValue);
300 }
George Liu1e44c732020-02-28 20:20:06 +0800301
302 /** @brief Set Dbus property
303 *
304 * @param[in] dBusMap - Object path, property name, interface and property
305 * type for the D-Bus object
306 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800307 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500308 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800309 */
310 void setDbusProperty(const DBusMapping& dBusMap,
311 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500312};
313
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500314/** @brief Fetch parent D-Bus object based on pathname
315 *
316 * @param[in] dbusObj - child D-Bus object
317 *
318 * @return std::string - the parent D-Bus object path
319 */
320inline std::string findParent(const std::string& dbusObj)
321{
322 fs::path p(dbusObj);
323 return p.parent_path().string();
324}
325
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500326/** @brief Read (static) MCTP EID of host firmware from a file
327 *
328 * @return uint8_t - MCTP EID
329 */
330uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530331
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530332/** @brief Convert a value in the JSON to a D-Bus property value
333 *
334 * @param[in] type - type of the D-Bus property
335 * @param[in] value - value in the JSON file
336 *
337 * @return PropertyValue - the D-Bus property value
338 */
339PropertyValue jsonEntryToDbusVal(std::string_view type,
340 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500341
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500342/** @brief Find State Effecter PDR
343 * @param[in] tid - PLDM terminus ID.
344 * @param[in] entityID - entity that can be associated with PLDM State set.
345 * @param[in] stateSetId - value that identifies PLDM State set.
346 * @param[in] repo - pointer to BMC's primary PDR repo.
347 * @return array[array[uint8_t]] - StateEffecterPDRs
348 */
349std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
350 uint16_t entityID,
351 uint16_t stateSetId,
352 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800353/** @brief Find State Sensor PDR
354 * @param[in] tid - PLDM terminus ID.
355 * @param[in] entityID - entity that can be associated with PLDM State set.
356 * @param[in] stateSetId - value that identifies PLDM State set.
357 * @param[in] repo - pointer to BMC's primary PDR repo.
358 * @return array[array[uint8_t]] - StateSensorPDRs
359 */
360std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
361 uint16_t entityID,
362 uint16_t stateSetId,
363 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500364
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500365/** @brief Find sensor id from a state sensor PDR
366 *
367 * @param[in] pdrRepo - PDR repository
368 * @param[in] tid - terminus id
369 * @param[in] entityType - entity type
370 * @param[in] entityInstance - entity instance number
371 * @param[in] containerId - container id
372 * @param[in] stateSetId - state set id
373 *
374 * @return uint16_t - the sensor id
375 */
376uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
377 uint16_t entityType, uint16_t entityInstance,
378 uint16_t containerId, uint16_t stateSetId);
379
Tom Joseph250c4752020-04-15 10:32:45 +0530380/** @brief Find effecter id from a state effecter pdr
381 * @param[in] pdrRepo - PDR repository
382 * @param[in] entityType - entity type
383 * @param[in] entityInstance - entity instance number
384 * @param[in] containerId - container id
385 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500386 * @param[in] localOrRemote - true for checking local repo and false for remote
387 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530388 *
389 * @return uint16_t - the effecter id
390 */
391uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
392 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500393 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530394
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800395/** @brief Emit the sensor event signal
396 *
397 * @param[in] tid - the terminus id
398 * @param[in] sensorId - sensorID value of the sensor
399 * @param[in] sensorOffset - Identifies which state sensor within a
400 * composite state sensor the event is being returned for
401 * @param[in] eventState - The event state value from the state change that
402 * triggered the event message
403 * @param[in] previousEventState - The event state value for the state from
404 * which the present event state was entered.
405 * @return PLDM completion code
406 */
407int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
408 uint8_t sensorOffset, uint8_t eventState,
409 uint8_t previousEventState);
410
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600411/** @brief Print the buffer
412 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530413 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
414 the buffer is an incoming PLDM message
415 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600416 *
417 * @return - None
418 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530419void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600420
Tom Joseph54922072021-06-19 02:45:46 -0700421/** @brief Convert the buffer to std::string
422 *
423 * If there are characters that are not printable characters, it is replaced
424 * with space(0x20).
425 *
426 * @param[in] var - pointer to data and length of the data
427 *
428 * @return std::string equivalent of variable field
429 */
430std::string toString(const struct variable_field& var);
431
George Liu872f0f62021-11-25 16:26:16 +0800432/** @brief Split strings according to special identifiers
433 *
434 * We can split the string according to the custom identifier(';', ',', '&' or
435 * others) and store it to vector.
436 *
437 * @param[in] srcStr - The string to be split
438 * @param[in] delim - The custom identifier
439 * @param[in] trimStr - The first and last string to be trimmed
440 *
441 * @return std::vector<std::string> Vectors are used to store strings
442 */
443std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
444 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530445/** @brief Get the current system time in readable format
446 *
447 * @return - std::string equivalent of the system time
448 */
449std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800450
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500451/** @brief checks if the FRU is actually present.
452 * @param[in] objPath - FRU object path.
453 *
454 * @return bool to indicate presence or absence of FRU.
455 */
456bool checkForFruPresence(const std::string& objPath);
457
George Liu83409572019-12-24 18:42:54 +0800458} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600459} // namespace pldm