blob: 4e702d8dbe30a21b35c99e6d69e6acf2b7d8252b [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
George Liu36e81352020-07-01 14:40:30 +0800147 virtual std::string getService(const char* path,
148 const char* interface) const = 0;
149
George Liu1e44c732020-02-28 20:20:06 +0800150 virtual void setDbusProperty(const DBusMapping& dBusMap,
151 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800152
153 virtual PropertyValue
154 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
155 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800156};
157
Sampa Misraa2fa0702019-05-31 01:28:55 -0500158/**
159 * @class DBusHandler
160 *
161 * Wrapper class to handle the D-Bus calls
162 *
163 * This class contains the APIs to handle the D-Bus calls
164 * to cater the request from pldm requester.
165 * A class is created to mock the apis in the test cases
166 */
George Liu1e44c732020-02-28 20:20:06 +0800167class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500168{
169 public:
George Liu0e02c322020-01-01 09:41:51 +0800170 /** @brief Get the bus connection. */
171 static auto& getBus()
172 {
173 static auto bus = sdbusplus::bus::new_default();
174 return bus;
175 }
176
177 /**
178 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800179 *
George Liu0e02c322020-01-01 09:41:51 +0800180 * @param[in] path - DBUS object path
181 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800182 *
George Liu0e02c322020-01-01 09:41:51 +0800183 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800184 *
185 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800186 */
George Liu36e81352020-07-01 14:40:30 +0800187 std::string getService(const char* path,
188 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800189
John Wang9e242422020-03-05 08:37:50 +0800190 /** @brief Get property(type: variant) from the requested dbus
191 *
192 * @param[in] objPath - The Dbus object path
193 * @param[in] dbusProp - The property name to get
194 * @param[in] dbusInterface - The Dbus interface
195 *
196 * @return The value of the property(type: variant)
197 *
198 * @throw sdbusplus::exception::SdBusError when it fails
199 */
200 PropertyValue
201 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
202 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800203
John Wang9e242422020-03-05 08:37:50 +0800204 /** @brief The template function to get property from the requested dbus
205 * path
206 *
207 * @tparam Property - Excepted type of the property on dbus
208 *
209 * @param[in] objPath - The Dbus object path
210 * @param[in] dbusProp - The property name to get
211 * @param[in] dbusInterface - The Dbus interface
212 *
213 * @return The value of the property
214 *
215 * @throw sdbusplus::exception::SdBusError when dbus request fails
216 * std::bad_variant_access when \p Property and property on dbus do
217 * not match
218 */
John Wang92b3c972019-10-17 11:06:41 +0800219 template <typename Property>
220 auto getDbusProperty(const char* objPath, const char* dbusProp,
221 const char* dbusInterface)
222 {
John Wang9e242422020-03-05 08:37:50 +0800223 auto VariantValue =
224 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800225 return std::get<Property>(VariantValue);
226 }
George Liu1e44c732020-02-28 20:20:06 +0800227
228 /** @brief Set Dbus property
229 *
230 * @param[in] dBusMap - Object path, property name, interface and property
231 * type for the D-Bus object
232 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800233 *
234 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800235 */
236 void setDbusProperty(const DBusMapping& dBusMap,
237 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500238};
239
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500240/** @brief Fetch parent D-Bus object based on pathname
241 *
242 * @param[in] dbusObj - child D-Bus object
243 *
244 * @return std::string - the parent D-Bus object path
245 */
246inline std::string findParent(const std::string& dbusObj)
247{
248 fs::path p(dbusObj);
249 return p.parent_path().string();
250}
251
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500252/** @brief Read (static) MCTP EID of host firmware from a file
253 *
254 * @return uint8_t - MCTP EID
255 */
256uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530257
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530258/** @brief Convert a value in the JSON to a D-Bus property value
259 *
260 * @param[in] type - type of the D-Bus property
261 * @param[in] value - value in the JSON file
262 *
263 * @return PropertyValue - the D-Bus property value
264 */
265PropertyValue jsonEntryToDbusVal(std::string_view type,
266 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500267
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500268/** @brief Find State Effecter PDR
269 * @param[in] tid - PLDM terminus ID.
270 * @param[in] entityID - entity that can be associated with PLDM State set.
271 * @param[in] stateSetId - value that identifies PLDM State set.
272 * @param[in] repo - pointer to BMC's primary PDR repo.
273 * @return array[array[uint8_t]] - StateEffecterPDRs
274 */
275std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
276 uint16_t entityID,
277 uint16_t stateSetId,
278 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800279/** @brief Find State Sensor PDR
280 * @param[in] tid - PLDM terminus ID.
281 * @param[in] entityID - entity that can be associated with PLDM State set.
282 * @param[in] stateSetId - value that identifies PLDM State set.
283 * @param[in] repo - pointer to BMC's primary PDR repo.
284 * @return array[array[uint8_t]] - StateSensorPDRs
285 */
286std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
287 uint16_t entityID,
288 uint16_t stateSetId,
289 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500290
Tom Joseph250c4752020-04-15 10:32:45 +0530291/** @brief Find effecter id from a state effecter pdr
292 * @param[in] pdrRepo - PDR repository
293 * @param[in] entityType - entity type
294 * @param[in] entityInstance - entity instance number
295 * @param[in] containerId - container id
296 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500297 * @param[in] localOrRemote - true for checking local repo and false for remote
298 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530299 *
300 * @return uint16_t - the effecter id
301 */
302uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
303 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500304 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530305
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800306/** @brief Emit the sensor event signal
307 *
308 * @param[in] tid - the terminus id
309 * @param[in] sensorId - sensorID value of the sensor
310 * @param[in] sensorOffset - Identifies which state sensor within a
311 * composite state sensor the event is being returned for
312 * @param[in] eventState - The event state value from the state change that
313 * triggered the event message
314 * @param[in] previousEventState - The event state value for the state from
315 * which the present event state was entered.
316 * @return PLDM completion code
317 */
318int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
319 uint8_t sensorOffset, uint8_t eventState,
320 uint8_t previousEventState);
321
George Liu83409572019-12-24 18:42:54 +0800322} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600323} // namespace pldm