blob: 654b0b49979a7519e2e7c5cbe280ce38154201b5 [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
vkaverap@in.ibm.com5b71b862023-08-21 05:19:04 +000031constexpr uint64_t dbusTimeout =
32 std::chrono::duration_cast<std::chrono::microseconds>(
33 std::chrono::seconds(DBUS_TIMEOUT))
34 .count();
35
Sampa Misra032bd502019-03-06 05:03:22 -060036namespace pldm
37{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050038namespace utils
39{
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050040namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053041using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053042constexpr bool Tx = true;
43constexpr bool Rx = false;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050044
George Liudf9a6d32020-12-22 16:27:16 +080045using EntityName = std::string;
46using EntityType = uint16_t;
47
48using Entities = std::vector<pldm_entity_node*>;
49using EntityAssociations = std::vector<Entities>;
50using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>;
51
52const std::map<EntityType, EntityName> entityMaps = {
53 {PLDM_ENTITY_SYSTEM_CHASSIS, "chassis"},
54 {PLDM_ENTITY_BOARD, "io_board"},
55 {PLDM_ENTITY_SYS_BOARD, "motherboard"},
56 {PLDM_ENTITY_POWER_SUPPLY, "powersupply"},
57 {PLDM_ENTITY_PROC, "cpu"},
58 {PLDM_ENTITY_SYSTEM_CHASSIS | 0x8000, "system"},
59 {PLDM_ENTITY_PROC_MODULE, "dcm"},
60 {PLDM_ENTITY_PROC | 0x8000, "core"},
61 {PLDM_ENTITY_IO_MODULE, "io_module"},
62 {PLDM_ENTITY_FAN, "fan"},
63 {PLDM_ENTITY_SYS_MGMT_MODULE, "system_management_module"},
64 {PLDM_ENTITY_POWER_CONVERTER, "power_converter"},
65 {PLDM_ENTITY_SLOT, "slot"},
66 {PLDM_ENTITY_CONNECTOR, "connector"}};
67
68/** @brief Vector a entity name to pldm_entity from entity association tree
69 * @param[in] entityAssoc - Vector of associated pldm entities
70 * @param[in] entityTree - entity association tree
71 * @param[out] objPathMap - maps an object path to pldm_entity from the
72 * BMC's entity association tree
73 * @return
74 */
75void updateEntityAssociation(const EntityAssociations& entityAssoc,
76 pldm_entity_association_tree* entityTree,
77 ObjectPathMaps& objPathMap);
78
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050079/** @struct CustomFD
80 *
81 * RAII wrapper for file descriptor.
82 */
83struct CustomFD
84{
85 CustomFD(const CustomFD&) = delete;
86 CustomFD& operator=(const CustomFD&) = delete;
87 CustomFD(CustomFD&&) = delete;
88 CustomFD& operator=(CustomFD&&) = delete;
89
Patrick Williams6da4f912023-05-10 07:50:53 -050090 CustomFD(int fd) : fd(fd) {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091
92 ~CustomFD()
93 {
94 if (fd >= 0)
95 {
96 close(fd);
97 }
98 }
99
100 int operator()() const
101 {
102 return fd;
103 }
104
105 private:
106 int fd = -1;
107};
108
Sampa Misrab37be312019-07-03 02:26:41 -0500109/** @brief Calculate the pad for PLDM data
110 *
111 * @param[in] data - Length of the data
112 * @return - uint8_t - number of pad bytes
113 */
114uint8_t getNumPadBytes(uint32_t data);
115
George Liu83409572019-12-24 18:42:54 +0800116/** @brief Convert uint64 to date
117 *
118 * @param[in] data - time date of uint64
119 * @param[out] year - year number in dec
120 * @param[out] month - month number in dec
121 * @param[out] day - day of the month in dec
122 * @param[out] hour - number of hours in dec
123 * @param[out] min - number of minutes in dec
124 * @param[out] sec - number of seconds in dec
125 * @return true if decode success, false if decode faild
126 */
127bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
128 uint8_t* hour, uint8_t* min, uint8_t* sec);
129
130/** @brief Convert effecter data to structure of set_effecter_state_field
131 *
132 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +0800133 * @param[in] effecterCount - the number of individual sets of effecter
134 * information
135 * @return[out] parse success and get a valid set_effecter_state_field
136 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +0800137 */
George Liuba4c1fb2020-02-05 14:13:30 +0800138std::optional<std::vector<set_effecter_state_field>>
139 parseEffecterData(const std::vector<uint8_t>& effecterData,
140 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -0600141
142/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600143 * @brief creates an error log
144 * @param[in] errorMsg - the error message
145 */
146void reportError(const char* errorMsg);
147
Sampa Misra032bd502019-03-06 05:03:22 -0600148/** @brief Convert any Decimal number to BCD
149 *
150 * @tparam[in] decimal - Decimal number
151 * @return Corresponding BCD number
152 */
153template <typename T>
154T decimalToBcd(T decimal)
155{
156 T bcd = 0;
157 T rem = 0;
158 auto cnt = 0;
159
160 while (decimal)
161 {
162 rem = decimal % 10;
163 bcd = bcd + (rem << cnt);
164 decimal = decimal / 10;
165 cnt += 4;
166 }
167
168 return bcd;
169}
170
Sampa Misraa2fa0702019-05-31 01:28:55 -0500171constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500172constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500173
George Liu1e44c732020-02-28 20:20:06 +0800174struct DBusMapping
175{
176 std::string objectPath; //!< D-Bus object path
177 std::string interface; //!< D-Bus interface
178 std::string propertyName; //!< D-Bus property name
179 std::string propertyType; //!< D-Bus property type
180};
181
182using PropertyValue =
183 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
Sagar Srinivas11ce8d22022-07-28 11:32:34 -0500184 uint64_t, double, std::string, std::vector<std::string>>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500185using DbusProp = std::string;
186using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500187using DBusInterfaceAdded = std::vector<
188 std::pair<pldm::dbus::Interface,
189 std::vector<std::pair<pldm::dbus::Property,
190 std::variant<pldm::dbus::Property>>>>>;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530191using ObjectPath = std::string;
192using 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>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500196using PropertyMap = std::map<std::string, PropertyValue>;
197using InterfaceMap = std::map<std::string, PropertyMap>;
George Liu1e44c732020-02-28 20:20:06 +0800198
199/**
200 * @brief The interface for DBusHandler
201 */
202class DBusHandlerInterface
203{
204 public:
205 virtual ~DBusHandlerInterface() = default;
206
George Liu36e81352020-07-01 14:40:30 +0800207 virtual std::string getService(const char* path,
208 const char* interface) const = 0;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530209 virtual GetSubTreeResponse
210 getSubtree(const std::string& path, int depth,
211 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800212
George Liu1e44c732020-02-28 20:20:06 +0800213 virtual void setDbusProperty(const DBusMapping& dBusMap,
214 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800215
216 virtual PropertyValue
217 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
218 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800219};
220
Sampa Misraa2fa0702019-05-31 01:28:55 -0500221/**
222 * @class DBusHandler
223 *
224 * Wrapper class to handle the D-Bus calls
225 *
226 * This class contains the APIs to handle the D-Bus calls
227 * to cater the request from pldm requester.
228 * A class is created to mock the apis in the test cases
229 */
George Liu1e44c732020-02-28 20:20:06 +0800230class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500231{
232 public:
George Liu0e02c322020-01-01 09:41:51 +0800233 /** @brief Get the bus connection. */
234 static auto& getBus()
235 {
236 static auto bus = sdbusplus::bus::new_default();
237 return bus;
238 }
239
240 /**
241 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800242 *
George Liu0e02c322020-01-01 09:41:51 +0800243 * @param[in] path - DBUS object path
244 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800245 *
George Liu0e02c322020-01-01 09:41:51 +0800246 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800247 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500248 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800249 */
George Liu36e81352020-07-01 14:40:30 +0800250 std::string getService(const char* path,
251 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800252
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530253 /**
254 * @brief Get the Subtree response from the mapper
255 *
256 * @param[in] path - DBUS object path
257 * @param[in] depth - Search depth
258 * @param[in] ifaceList - list of the interface that are being
259 * queried from the mapper
260 *
261 * @return GetSubTreeResponse - the mapper subtree response
262 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500263 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530264 */
265 GetSubTreeResponse
266 getSubtree(const std::string& path, int depth,
267 const std::vector<std::string>& ifaceList) const override;
268
John Wang9e242422020-03-05 08:37:50 +0800269 /** @brief Get property(type: variant) from the requested dbus
270 *
271 * @param[in] objPath - The Dbus object path
272 * @param[in] dbusProp - The property name to get
273 * @param[in] dbusInterface - The Dbus interface
274 *
275 * @return The value of the property(type: variant)
276 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500277 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800278 */
279 PropertyValue
280 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
281 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800282
John Wang9e242422020-03-05 08:37:50 +0800283 /** @brief The template function to get property from the requested dbus
284 * path
285 *
286 * @tparam Property - Excepted type of the property on dbus
287 *
288 * @param[in] objPath - The Dbus object path
289 * @param[in] dbusProp - The property name to get
290 * @param[in] dbusInterface - The Dbus interface
291 *
292 * @return The value of the property
293 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500294 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800295 * std::bad_variant_access when \p Property and property on dbus do
296 * not match
297 */
John Wang92b3c972019-10-17 11:06:41 +0800298 template <typename Property>
299 auto getDbusProperty(const char* objPath, const char* dbusProp,
300 const char* dbusInterface)
301 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500302 auto VariantValue = getDbusPropertyVariant(objPath, dbusProp,
303 dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800304 return std::get<Property>(VariantValue);
305 }
George Liu1e44c732020-02-28 20:20:06 +0800306
307 /** @brief Set Dbus property
308 *
309 * @param[in] dBusMap - Object path, property name, interface and property
310 * type for the D-Bus object
311 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800312 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500313 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800314 */
315 void setDbusProperty(const DBusMapping& dBusMap,
316 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500317};
318
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500319/** @brief Fetch parent D-Bus object based on pathname
320 *
321 * @param[in] dbusObj - child D-Bus object
322 *
323 * @return std::string - the parent D-Bus object path
324 */
325inline std::string findParent(const std::string& dbusObj)
326{
327 fs::path p(dbusObj);
328 return p.parent_path().string();
329}
330
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500331/** @brief Read (static) MCTP EID of host firmware from a file
332 *
333 * @return uint8_t - MCTP EID
334 */
335uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530336
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530337/** @brief Convert a value in the JSON to a D-Bus property value
338 *
339 * @param[in] type - type of the D-Bus property
340 * @param[in] value - value in the JSON file
341 *
342 * @return PropertyValue - the D-Bus property value
343 */
344PropertyValue jsonEntryToDbusVal(std::string_view type,
345 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500346
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500347/** @brief Find State Effecter PDR
348 * @param[in] tid - PLDM terminus ID.
349 * @param[in] entityID - entity that can be associated with PLDM State set.
350 * @param[in] stateSetId - value that identifies PLDM State set.
351 * @param[in] repo - pointer to BMC's primary PDR repo.
352 * @return array[array[uint8_t]] - StateEffecterPDRs
353 */
354std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
355 uint16_t entityID,
356 uint16_t stateSetId,
357 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800358/** @brief Find State Sensor PDR
359 * @param[in] tid - PLDM terminus ID.
360 * @param[in] entityID - entity that can be associated with PLDM State set.
361 * @param[in] stateSetId - value that identifies PLDM State set.
362 * @param[in] repo - pointer to BMC's primary PDR repo.
363 * @return array[array[uint8_t]] - StateSensorPDRs
364 */
365std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
366 uint16_t entityID,
367 uint16_t stateSetId,
368 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500369
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500370/** @brief Find sensor id from a state sensor PDR
371 *
372 * @param[in] pdrRepo - PDR repository
373 * @param[in] tid - terminus id
374 * @param[in] entityType - entity type
375 * @param[in] entityInstance - entity instance number
376 * @param[in] containerId - container id
377 * @param[in] stateSetId - state set id
378 *
379 * @return uint16_t - the sensor id
380 */
381uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
382 uint16_t entityType, uint16_t entityInstance,
383 uint16_t containerId, uint16_t stateSetId);
384
Tom Joseph250c4752020-04-15 10:32:45 +0530385/** @brief Find effecter id from a state effecter pdr
386 * @param[in] pdrRepo - PDR repository
387 * @param[in] entityType - entity type
388 * @param[in] entityInstance - entity instance number
389 * @param[in] containerId - container id
390 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500391 * @param[in] localOrRemote - true for checking local repo and false for remote
392 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530393 *
394 * @return uint16_t - the effecter id
395 */
396uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
397 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500398 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530399
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800400/** @brief Emit the sensor event signal
401 *
402 * @param[in] tid - the terminus id
403 * @param[in] sensorId - sensorID value of the sensor
404 * @param[in] sensorOffset - Identifies which state sensor within a
405 * composite state sensor the event is being returned for
406 * @param[in] eventState - The event state value from the state change that
407 * triggered the event message
408 * @param[in] previousEventState - The event state value for the state from
409 * which the present event state was entered.
410 * @return PLDM completion code
411 */
412int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
413 uint8_t sensorOffset, uint8_t eventState,
414 uint8_t previousEventState);
415
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600416/** @brief Print the buffer
417 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530418 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
419 the buffer is an incoming PLDM message
420 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600421 *
422 * @return - None
423 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530424void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600425
Tom Joseph54922072021-06-19 02:45:46 -0700426/** @brief Convert the buffer to std::string
427 *
428 * If there are characters that are not printable characters, it is replaced
429 * with space(0x20).
430 *
431 * @param[in] var - pointer to data and length of the data
432 *
433 * @return std::string equivalent of variable field
434 */
435std::string toString(const struct variable_field& var);
436
George Liu872f0f62021-11-25 16:26:16 +0800437/** @brief Split strings according to special identifiers
438 *
439 * We can split the string according to the custom identifier(';', ',', '&' or
440 * others) and store it to vector.
441 *
442 * @param[in] srcStr - The string to be split
443 * @param[in] delim - The custom identifier
444 * @param[in] trimStr - The first and last string to be trimmed
445 *
446 * @return std::vector<std::string> Vectors are used to store strings
447 */
448std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
449 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530450/** @brief Get the current system time in readable format
451 *
452 * @return - std::string equivalent of the system time
453 */
454std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800455
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500456/** @brief checks if the FRU is actually present.
457 * @param[in] objPath - FRU object path.
458 *
459 * @return bool to indicate presence or absence of FRU.
460 */
461bool checkForFruPresence(const std::string& objPath);
462
George Liu83409572019-12-24 18:42:54 +0800463} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600464} // namespace pldm