blob: 0efb89b19bafff6e644c1a7957ad3b3b336126cd [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>
7#include <libpldm/platform.h>
8#include <libpldm/utils.h>
Sampa Misra032bd502019-03-06 05:03:22 -06009#include <stdint.h>
10#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050011#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060012
George Liu6492f522020-06-16 10:34:05 +080013#include <nlohmann/json.hpp>
14#include <sdbusplus/server.hpp>
15#include <xyz/openbmc_project/Logging/Entry/server.hpp>
16
Sampa Misraa2fa0702019-05-31 01:28:55 -050017#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050018#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060019#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060020#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050021#include <variant>
22#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060023
24namespace pldm
25{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050026namespace utils
27{
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050028namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053029using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053030constexpr bool Tx = true;
31constexpr bool Rx = false;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050032
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050033/** @struct CustomFD
34 *
35 * RAII wrapper for file descriptor.
36 */
37struct CustomFD
38{
39 CustomFD(const CustomFD&) = delete;
40 CustomFD& operator=(const CustomFD&) = delete;
41 CustomFD(CustomFD&&) = delete;
42 CustomFD& operator=(CustomFD&&) = delete;
43
44 CustomFD(int fd) : fd(fd)
George Liu6492f522020-06-16 10:34:05 +080045 {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050046
47 ~CustomFD()
48 {
49 if (fd >= 0)
50 {
51 close(fd);
52 }
53 }
54
55 int operator()() const
56 {
57 return fd;
58 }
59
60 private:
61 int fd = -1;
62};
63
Sampa Misrab37be312019-07-03 02:26:41 -050064/** @brief Calculate the pad for PLDM data
65 *
66 * @param[in] data - Length of the data
67 * @return - uint8_t - number of pad bytes
68 */
69uint8_t getNumPadBytes(uint32_t data);
70
George Liu83409572019-12-24 18:42:54 +080071/** @brief Convert uint64 to date
72 *
73 * @param[in] data - time date of uint64
74 * @param[out] year - year number in dec
75 * @param[out] month - month number in dec
76 * @param[out] day - day of the month in dec
77 * @param[out] hour - number of hours in dec
78 * @param[out] min - number of minutes in dec
79 * @param[out] sec - number of seconds in dec
80 * @return true if decode success, false if decode faild
81 */
82bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
83 uint8_t* hour, uint8_t* min, uint8_t* sec);
84
85/** @brief Convert effecter data to structure of set_effecter_state_field
86 *
87 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080088 * @param[in] effecterCount - the number of individual sets of effecter
89 * information
90 * @return[out] parse success and get a valid set_effecter_state_field
91 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080092 */
George Liuba4c1fb2020-02-05 14:13:30 +080093std::optional<std::vector<set_effecter_state_field>>
94 parseEffecterData(const std::vector<uint8_t>& effecterData,
95 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060096
97/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060098 * @brief creates an error log
99 * @param[in] errorMsg - the error message
100 */
101void reportError(const char* errorMsg);
102
Sampa Misra032bd502019-03-06 05:03:22 -0600103/** @brief Convert any Decimal number to BCD
104 *
105 * @tparam[in] decimal - Decimal number
106 * @return Corresponding BCD number
107 */
108template <typename T>
109T decimalToBcd(T decimal)
110{
111 T bcd = 0;
112 T rem = 0;
113 auto cnt = 0;
114
115 while (decimal)
116 {
117 rem = decimal % 10;
118 bcd = bcd + (rem << cnt);
119 decimal = decimal / 10;
120 cnt += 4;
121 }
122
123 return bcd;
124}
125
Sampa Misraa2fa0702019-05-31 01:28:55 -0500126constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500127constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500128
George Liu1e44c732020-02-28 20:20:06 +0800129struct DBusMapping
130{
131 std::string objectPath; //!< D-Bus object path
132 std::string interface; //!< D-Bus interface
133 std::string propertyName; //!< D-Bus property name
134 std::string propertyType; //!< D-Bus property type
135};
136
137using PropertyValue =
138 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
139 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500140using DbusProp = std::string;
141using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500142using DBusInterfaceAdded = std::vector<
143 std::pair<pldm::dbus::Interface,
144 std::vector<std::pair<pldm::dbus::Property,
145 std::variant<pldm::dbus::Property>>>>>;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530146using ObjectPath = std::string;
147using ServiceName = std::string;
148using Interfaces = std::vector<std::string>;
149using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>;
150using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>;
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500151using PropertyMap = std::map<std::string, PropertyValue>;
152using InterfaceMap = std::map<std::string, PropertyMap>;
George Liu1e44c732020-02-28 20:20:06 +0800153
154/**
155 * @brief The interface for DBusHandler
156 */
157class DBusHandlerInterface
158{
159 public:
160 virtual ~DBusHandlerInterface() = default;
161
George Liu36e81352020-07-01 14:40:30 +0800162 virtual std::string getService(const char* path,
163 const char* interface) const = 0;
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530164 virtual GetSubTreeResponse
165 getSubtree(const std::string& path, int depth,
166 const std::vector<std::string>& ifaceList) const = 0;
George Liu36e81352020-07-01 14:40:30 +0800167
George Liu1e44c732020-02-28 20:20:06 +0800168 virtual void setDbusProperty(const DBusMapping& dBusMap,
169 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800170
171 virtual PropertyValue
172 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
173 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800174};
175
Sampa Misraa2fa0702019-05-31 01:28:55 -0500176/**
177 * @class DBusHandler
178 *
179 * Wrapper class to handle the D-Bus calls
180 *
181 * This class contains the APIs to handle the D-Bus calls
182 * to cater the request from pldm requester.
183 * A class is created to mock the apis in the test cases
184 */
George Liu1e44c732020-02-28 20:20:06 +0800185class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500186{
187 public:
George Liu0e02c322020-01-01 09:41:51 +0800188 /** @brief Get the bus connection. */
189 static auto& getBus()
190 {
191 static auto bus = sdbusplus::bus::new_default();
192 return bus;
193 }
194
195 /**
196 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800197 *
George Liu0e02c322020-01-01 09:41:51 +0800198 * @param[in] path - DBUS object path
199 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800200 *
George Liu0e02c322020-01-01 09:41:51 +0800201 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800202 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500203 * @throw sdbusplus::exception_t when it fails
George Liu0e02c322020-01-01 09:41:51 +0800204 */
George Liu36e81352020-07-01 14:40:30 +0800205 std::string getService(const char* path,
206 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800207
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530208 /**
209 * @brief Get the Subtree response from the mapper
210 *
211 * @param[in] path - DBUS object path
212 * @param[in] depth - Search depth
213 * @param[in] ifaceList - list of the interface that are being
214 * queried from the mapper
215 *
216 * @return GetSubTreeResponse - the mapper subtree response
217 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500218 * @throw sdbusplus::exception_t when it fails
Manojkiran Eda1ef62c32021-04-24 07:23:18 +0530219 */
220 GetSubTreeResponse
221 getSubtree(const std::string& path, int depth,
222 const std::vector<std::string>& ifaceList) const override;
223
John Wang9e242422020-03-05 08:37:50 +0800224 /** @brief Get property(type: variant) from the requested dbus
225 *
226 * @param[in] objPath - The Dbus object path
227 * @param[in] dbusProp - The property name to get
228 * @param[in] dbusInterface - The Dbus interface
229 *
230 * @return The value of the property(type: variant)
231 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500232 * @throw sdbusplus::exception_t when it fails
John Wang9e242422020-03-05 08:37:50 +0800233 */
234 PropertyValue
235 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
236 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800237
John Wang9e242422020-03-05 08:37:50 +0800238 /** @brief The template function to get property from the requested dbus
239 * path
240 *
241 * @tparam Property - Excepted type of the property on dbus
242 *
243 * @param[in] objPath - The Dbus object path
244 * @param[in] dbusProp - The property name to get
245 * @param[in] dbusInterface - The Dbus interface
246 *
247 * @return The value of the property
248 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500249 * @throw sdbusplus::exception_t when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800250 * std::bad_variant_access when \p Property and property on dbus do
251 * not match
252 */
John Wang92b3c972019-10-17 11:06:41 +0800253 template <typename Property>
254 auto getDbusProperty(const char* objPath, const char* dbusProp,
255 const char* dbusInterface)
256 {
John Wang9e242422020-03-05 08:37:50 +0800257 auto VariantValue =
258 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800259 return std::get<Property>(VariantValue);
260 }
George Liu1e44c732020-02-28 20:20:06 +0800261
262 /** @brief Set Dbus property
263 *
264 * @param[in] dBusMap - Object path, property name, interface and property
265 * type for the D-Bus object
266 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800267 *
Patrick Williams84b790c2022-07-22 19:26:56 -0500268 * @throw sdbusplus::exception_t when it fails
George Liu1e44c732020-02-28 20:20:06 +0800269 */
270 void setDbusProperty(const DBusMapping& dBusMap,
271 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500272};
273
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500274/** @brief Fetch parent D-Bus object based on pathname
275 *
276 * @param[in] dbusObj - child D-Bus object
277 *
278 * @return std::string - the parent D-Bus object path
279 */
280inline std::string findParent(const std::string& dbusObj)
281{
282 fs::path p(dbusObj);
283 return p.parent_path().string();
284}
285
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500286/** @brief Read (static) MCTP EID of host firmware from a file
287 *
288 * @return uint8_t - MCTP EID
289 */
290uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530291
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530292/** @brief Convert a value in the JSON to a D-Bus property value
293 *
294 * @param[in] type - type of the D-Bus property
295 * @param[in] value - value in the JSON file
296 *
297 * @return PropertyValue - the D-Bus property value
298 */
299PropertyValue jsonEntryToDbusVal(std::string_view type,
300 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500301
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500302/** @brief Find State Effecter PDR
303 * @param[in] tid - PLDM terminus ID.
304 * @param[in] entityID - entity that can be associated with PLDM State set.
305 * @param[in] stateSetId - value that identifies PLDM State set.
306 * @param[in] repo - pointer to BMC's primary PDR repo.
307 * @return array[array[uint8_t]] - StateEffecterPDRs
308 */
309std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
310 uint16_t entityID,
311 uint16_t stateSetId,
312 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800313/** @brief Find State Sensor PDR
314 * @param[in] tid - PLDM terminus ID.
315 * @param[in] entityID - entity that can be associated with PLDM State set.
316 * @param[in] stateSetId - value that identifies PLDM State set.
317 * @param[in] repo - pointer to BMC's primary PDR repo.
318 * @return array[array[uint8_t]] - StateSensorPDRs
319 */
320std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
321 uint16_t entityID,
322 uint16_t stateSetId,
323 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500324
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500325/** @brief Find sensor id from a state sensor PDR
326 *
327 * @param[in] pdrRepo - PDR repository
328 * @param[in] tid - terminus id
329 * @param[in] entityType - entity type
330 * @param[in] entityInstance - entity instance number
331 * @param[in] containerId - container id
332 * @param[in] stateSetId - state set id
333 *
334 * @return uint16_t - the sensor id
335 */
336uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
337 uint16_t entityType, uint16_t entityInstance,
338 uint16_t containerId, uint16_t stateSetId);
339
Tom Joseph250c4752020-04-15 10:32:45 +0530340/** @brief Find effecter id from a state effecter pdr
341 * @param[in] pdrRepo - PDR repository
342 * @param[in] entityType - entity type
343 * @param[in] entityInstance - entity instance number
344 * @param[in] containerId - container id
345 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500346 * @param[in] localOrRemote - true for checking local repo and false for remote
347 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530348 *
349 * @return uint16_t - the effecter id
350 */
351uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
352 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500353 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530354
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800355/** @brief Emit the sensor event signal
356 *
357 * @param[in] tid - the terminus id
358 * @param[in] sensorId - sensorID value of the sensor
359 * @param[in] sensorOffset - Identifies which state sensor within a
360 * composite state sensor the event is being returned for
361 * @param[in] eventState - The event state value from the state change that
362 * triggered the event message
363 * @param[in] previousEventState - The event state value for the state from
364 * which the present event state was entered.
365 * @return PLDM completion code
366 */
367int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
368 uint8_t sensorOffset, uint8_t eventState,
369 uint8_t previousEventState);
370
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600371/** @brief Print the buffer
372 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530373 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
374 the buffer is an incoming PLDM message
375 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600376 *
377 * @return - None
378 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530379void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600380
Tom Joseph54922072021-06-19 02:45:46 -0700381/** @brief Convert the buffer to std::string
382 *
383 * If there are characters that are not printable characters, it is replaced
384 * with space(0x20).
385 *
386 * @param[in] var - pointer to data and length of the data
387 *
388 * @return std::string equivalent of variable field
389 */
390std::string toString(const struct variable_field& var);
391
George Liu872f0f62021-11-25 16:26:16 +0800392/** @brief Split strings according to special identifiers
393 *
394 * We can split the string according to the custom identifier(';', ',', '&' or
395 * others) and store it to vector.
396 *
397 * @param[in] srcStr - The string to be split
398 * @param[in] delim - The custom identifier
399 * @param[in] trimStr - The first and last string to be trimmed
400 *
401 * @return std::vector<std::string> Vectors are used to store strings
402 */
403std::vector<std::string> split(std::string_view srcStr, std::string_view delim,
404 std::string_view trimStr = "");
Manojkiran Edaef773052021-07-29 09:29:28 +0530405/** @brief Get the current system time in readable format
406 *
407 * @return - std::string equivalent of the system time
408 */
409std::string getCurrentSystemTime();
George Liu872f0f62021-11-25 16:26:16 +0800410
Sridevi Ramesheefe49b2022-06-27 11:51:02 -0500411/** @brief checks if the FRU is actually present.
412 * @param[in] objPath - FRU object path.
413 *
414 * @return bool to indicate presence or absence of FRU.
415 */
416bool checkForFruPresence(const std::string& objPath);
417
George Liu83409572019-12-24 18:42:54 +0800418} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600419} // namespace pldm