blob: 0eb2b2d02e8252e9a6de07c9e1f6b19a2cffb373 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "libpldm/bios.h"
5#include "libpldm/platform.h"
6
Sampa Misra032bd502019-03-06 05:03:22 -06007#include <stdint.h>
8#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05009#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060010
George Liu6492f522020-06-16 10:34:05 +080011#include <nlohmann/json.hpp>
12#include <sdbusplus/server.hpp>
13#include <xyz/openbmc_project/Logging/Entry/server.hpp>
14
Sampa Misraa2fa0702019-05-31 01:28:55 -050015#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050016#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060017#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060018#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050019#include <variant>
20#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060021
22namespace pldm
23{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050024namespace utils
25{
26
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050027namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053028using Json = nlohmann::json;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050029
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050030/** @struct CustomFD
31 *
32 * RAII wrapper for file descriptor.
33 */
34struct CustomFD
35{
36 CustomFD(const CustomFD&) = delete;
37 CustomFD& operator=(const CustomFD&) = delete;
38 CustomFD(CustomFD&&) = delete;
39 CustomFD& operator=(CustomFD&&) = delete;
40
41 CustomFD(int fd) : fd(fd)
George Liu6492f522020-06-16 10:34:05 +080042 {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050043
44 ~CustomFD()
45 {
46 if (fd >= 0)
47 {
48 close(fd);
49 }
50 }
51
52 int operator()() const
53 {
54 return fd;
55 }
56
57 private:
58 int fd = -1;
59};
60
Sampa Misrab37be312019-07-03 02:26:41 -050061/** @brief Calculate the pad for PLDM data
62 *
63 * @param[in] data - Length of the data
64 * @return - uint8_t - number of pad bytes
65 */
66uint8_t getNumPadBytes(uint32_t data);
67
George Liu83409572019-12-24 18:42:54 +080068/** @brief Convert uint64 to date
69 *
70 * @param[in] data - time date of uint64
71 * @param[out] year - year number in dec
72 * @param[out] month - month number in dec
73 * @param[out] day - day of the month in dec
74 * @param[out] hour - number of hours in dec
75 * @param[out] min - number of minutes in dec
76 * @param[out] sec - number of seconds in dec
77 * @return true if decode success, false if decode faild
78 */
79bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
80 uint8_t* hour, uint8_t* min, uint8_t* sec);
81
82/** @brief Convert effecter data to structure of set_effecter_state_field
83 *
84 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080085 * @param[in] effecterCount - the number of individual sets of effecter
86 * information
87 * @return[out] parse success and get a valid set_effecter_state_field
88 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080089 */
George Liuba4c1fb2020-02-05 14:13:30 +080090std::optional<std::vector<set_effecter_state_field>>
91 parseEffecterData(const std::vector<uint8_t>& effecterData,
92 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060093
94/**
Sampa Misraaa8ae722019-12-12 03:20:40 -060095 * @brief creates an error log
96 * @param[in] errorMsg - the error message
97 */
98void reportError(const char* errorMsg);
99
Sampa Misra032bd502019-03-06 05:03:22 -0600100/** @brief Convert any Decimal number to BCD
101 *
102 * @tparam[in] decimal - Decimal number
103 * @return Corresponding BCD number
104 */
105template <typename T>
106T decimalToBcd(T decimal)
107{
108 T bcd = 0;
109 T rem = 0;
110 auto cnt = 0;
111
112 while (decimal)
113 {
114 rem = decimal % 10;
115 bcd = bcd + (rem << cnt);
116 decimal = decimal / 10;
117 cnt += 4;
118 }
119
120 return bcd;
121}
122
Sampa Misraa2fa0702019-05-31 01:28:55 -0500123constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
124
George Liu1e44c732020-02-28 20:20:06 +0800125struct DBusMapping
126{
127 std::string objectPath; //!< D-Bus object path
128 std::string interface; //!< D-Bus interface
129 std::string propertyName; //!< D-Bus property name
130 std::string propertyType; //!< D-Bus property type
131};
132
133using PropertyValue =
134 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
135 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500136using DbusProp = std::string;
137using DbusChangedProps = std::map<DbusProp, PropertyValue>;
George Liu1e44c732020-02-28 20:20:06 +0800138
139/**
140 * @brief The interface for DBusHandler
141 */
142class DBusHandlerInterface
143{
144 public:
145 virtual ~DBusHandlerInterface() = default;
146
147 virtual void setDbusProperty(const DBusMapping& dBusMap,
148 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800149
150 virtual PropertyValue
151 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
152 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800153};
154
Sampa Misraa2fa0702019-05-31 01:28:55 -0500155/**
156 * @class DBusHandler
157 *
158 * Wrapper class to handle the D-Bus calls
159 *
160 * This class contains the APIs to handle the D-Bus calls
161 * to cater the request from pldm requester.
162 * A class is created to mock the apis in the test cases
163 */
George Liu1e44c732020-02-28 20:20:06 +0800164class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500165{
166 public:
George Liu0e02c322020-01-01 09:41:51 +0800167 /** @brief Get the bus connection. */
168 static auto& getBus()
169 {
170 static auto bus = sdbusplus::bus::new_default();
171 return bus;
172 }
173
174 /**
175 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800176 *
George Liu0e02c322020-01-01 09:41:51 +0800177 * @param[in] path - DBUS object path
178 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800179 *
George Liu0e02c322020-01-01 09:41:51 +0800180 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800181 *
182 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800183 */
184 std::string getService(const char* path, const char* interface) const;
185
John Wang9e242422020-03-05 08:37:50 +0800186 /** @brief Get property(type: variant) from the requested dbus
187 *
188 * @param[in] objPath - The Dbus object path
189 * @param[in] dbusProp - The property name to get
190 * @param[in] dbusInterface - The Dbus interface
191 *
192 * @return The value of the property(type: variant)
193 *
194 * @throw sdbusplus::exception::SdBusError when it fails
195 */
196 PropertyValue
197 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
198 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800199
John Wang9e242422020-03-05 08:37:50 +0800200 /** @brief The template function to get property from the requested dbus
201 * path
202 *
203 * @tparam Property - Excepted type of the property on dbus
204 *
205 * @param[in] objPath - The Dbus object path
206 * @param[in] dbusProp - The property name to get
207 * @param[in] dbusInterface - The Dbus interface
208 *
209 * @return The value of the property
210 *
211 * @throw sdbusplus::exception::SdBusError when dbus request fails
212 * std::bad_variant_access when \p Property and property on dbus do
213 * not match
214 */
John Wang92b3c972019-10-17 11:06:41 +0800215 template <typename Property>
216 auto getDbusProperty(const char* objPath, const char* dbusProp,
217 const char* dbusInterface)
218 {
John Wang9e242422020-03-05 08:37:50 +0800219 auto VariantValue =
220 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800221 return std::get<Property>(VariantValue);
222 }
George Liu1e44c732020-02-28 20:20:06 +0800223
224 /** @brief Set Dbus property
225 *
226 * @param[in] dBusMap - Object path, property name, interface and property
227 * type for the D-Bus object
228 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800229 *
230 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800231 */
232 void setDbusProperty(const DBusMapping& dBusMap,
233 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500234};
235
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500236/** @brief Fetch parent D-Bus object based on pathname
237 *
238 * @param[in] dbusObj - child D-Bus object
239 *
240 * @return std::string - the parent D-Bus object path
241 */
242inline std::string findParent(const std::string& dbusObj)
243{
244 fs::path p(dbusObj);
245 return p.parent_path().string();
246}
247
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500248/** @brief Read (static) MCTP EID of host firmware from a file
249 *
250 * @return uint8_t - MCTP EID
251 */
252uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530253
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530254/** @brief Convert a value in the JSON to a D-Bus property value
255 *
256 * @param[in] type - type of the D-Bus property
257 * @param[in] value - value in the JSON file
258 *
259 * @return PropertyValue - the D-Bus property value
260 */
261PropertyValue jsonEntryToDbusVal(std::string_view type,
262 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500263
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500264/** @brief Find State Effecter PDR
265 * @param[in] tid - PLDM terminus ID.
266 * @param[in] entityID - entity that can be associated with PLDM State set.
267 * @param[in] stateSetId - value that identifies PLDM State set.
268 * @param[in] repo - pointer to BMC's primary PDR repo.
269 * @return array[array[uint8_t]] - StateEffecterPDRs
270 */
271std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
272 uint16_t entityID,
273 uint16_t stateSetId,
274 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800275/** @brief Find State Sensor PDR
276 * @param[in] tid - PLDM terminus ID.
277 * @param[in] entityID - entity that can be associated with PLDM State set.
278 * @param[in] stateSetId - value that identifies PLDM State set.
279 * @param[in] repo - pointer to BMC's primary PDR repo.
280 * @return array[array[uint8_t]] - StateSensorPDRs
281 */
282std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
283 uint16_t entityID,
284 uint16_t stateSetId,
285 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500286
Tom Joseph250c4752020-04-15 10:32:45 +0530287/** @brief Find effecter id from a state effecter pdr
288 * @param[in] pdrRepo - PDR repository
289 * @param[in] entityType - entity type
290 * @param[in] entityInstance - entity instance number
291 * @param[in] containerId - container id
292 * @param[in] stateSetId - state set id
293 *
294 * @return uint16_t - the effecter id
295 */
296uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
297 uint16_t entityInstance, uint16_t containerId,
298 uint16_t stateSetId);
299
George Liu83409572019-12-24 18:42:54 +0800300} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600301} // namespace pldm