blob: 72170fb4149c545e35b649d8acc43011f2dce027 [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"
Tom Joseph54922072021-06-19 02:45:46 -07006#include "libpldm/utils.h"
George Liu6492f522020-06-16 10:34:05 +08007
Sampa Misraaea5dde2020-08-31 08:33:47 -05008#include "types.hpp"
9
Sampa Misra032bd502019-03-06 05:03:22 -060010#include <stdint.h>
11#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050012#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -060013
George Liu6492f522020-06-16 10:34:05 +080014#include <nlohmann/json.hpp>
15#include <sdbusplus/server.hpp>
16#include <xyz/openbmc_project/Logging/Entry/server.hpp>
17
Sampa Misraa2fa0702019-05-31 01:28:55 -050018#include <exception>
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050019#include <filesystem>
Sampa Misraaa8ae722019-12-12 03:20:40 -060020#include <iostream>
Sampa Misra032bd502019-03-06 05:03:22 -060021#include <string>
Sampa Misraa2fa0702019-05-31 01:28:55 -050022#include <variant>
23#include <vector>
Sampa Misra032bd502019-03-06 05:03:22 -060024
25namespace pldm
26{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050027namespace utils
28{
29
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050030namespace fs = std::filesystem;
Tom Joseph250c4752020-04-15 10:32:45 +053031using Json = nlohmann::json;
Tom Josephe5268cd2021-09-07 13:04:03 +053032constexpr bool Tx = true;
33constexpr bool Rx = false;
Deepak Kodihalli3cd61812020-03-10 06:38:45 -050034
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050035/** @struct CustomFD
36 *
37 * RAII wrapper for file descriptor.
38 */
39struct CustomFD
40{
41 CustomFD(const CustomFD&) = delete;
42 CustomFD& operator=(const CustomFD&) = delete;
43 CustomFD(CustomFD&&) = delete;
44 CustomFD& operator=(CustomFD&&) = delete;
45
46 CustomFD(int fd) : fd(fd)
George Liu6492f522020-06-16 10:34:05 +080047 {}
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050048
49 ~CustomFD()
50 {
51 if (fd >= 0)
52 {
53 close(fd);
54 }
55 }
56
57 int operator()() const
58 {
59 return fd;
60 }
61
62 private:
63 int fd = -1;
64};
65
Sampa Misrab37be312019-07-03 02:26:41 -050066/** @brief Calculate the pad for PLDM data
67 *
68 * @param[in] data - Length of the data
69 * @return - uint8_t - number of pad bytes
70 */
71uint8_t getNumPadBytes(uint32_t data);
72
George Liu83409572019-12-24 18:42:54 +080073/** @brief Convert uint64 to date
74 *
75 * @param[in] data - time date of uint64
76 * @param[out] year - year number in dec
77 * @param[out] month - month number in dec
78 * @param[out] day - day of the month in dec
79 * @param[out] hour - number of hours in dec
80 * @param[out] min - number of minutes in dec
81 * @param[out] sec - number of seconds in dec
82 * @return true if decode success, false if decode faild
83 */
84bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day,
85 uint8_t* hour, uint8_t* min, uint8_t* sec);
86
87/** @brief Convert effecter data to structure of set_effecter_state_field
88 *
89 * @param[in] effecterData - the date of effecter
George Liuba4c1fb2020-02-05 14:13:30 +080090 * @param[in] effecterCount - the number of individual sets of effecter
91 * information
92 * @return[out] parse success and get a valid set_effecter_state_field
93 * structure, return nullopt means parse failed
George Liu83409572019-12-24 18:42:54 +080094 */
George Liuba4c1fb2020-02-05 14:13:30 +080095std::optional<std::vector<set_effecter_state_field>>
96 parseEffecterData(const std::vector<uint8_t>& effecterData,
97 uint8_t effecterCount);
Sampa Misra032bd502019-03-06 05:03:22 -060098
99/**
Sampa Misraaa8ae722019-12-12 03:20:40 -0600100 * @brief creates an error log
101 * @param[in] errorMsg - the error message
102 */
103void reportError(const char* errorMsg);
104
Sampa Misra032bd502019-03-06 05:03:22 -0600105/** @brief Convert any Decimal number to BCD
106 *
107 * @tparam[in] decimal - Decimal number
108 * @return Corresponding BCD number
109 */
110template <typename T>
111T decimalToBcd(T decimal)
112{
113 T bcd = 0;
114 T rem = 0;
115 auto cnt = 0;
116
117 while (decimal)
118 {
119 rem = decimal % 10;
120 bcd = bcd + (rem << cnt);
121 decimal = decimal / 10;
122 cnt += 4;
123 }
124
125 return bcd;
126}
127
Sampa Misraa2fa0702019-05-31 01:28:55 -0500128constexpr auto dbusProperties = "org.freedesktop.DBus.Properties";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500129constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500130
George Liu1e44c732020-02-28 20:20:06 +0800131struct DBusMapping
132{
133 std::string objectPath; //!< D-Bus object path
134 std::string interface; //!< D-Bus interface
135 std::string propertyName; //!< D-Bus property name
136 std::string propertyType; //!< D-Bus property type
137};
138
139using PropertyValue =
140 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
141 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500142using DbusProp = std::string;
143using DbusChangedProps = std::map<DbusProp, PropertyValue>;
Sampa Misraaea5dde2020-08-31 08:33:47 -0500144using DBusInterfaceAdded = std::vector<
145 std::pair<pldm::dbus::Interface,
146 std::vector<std::pair<pldm::dbus::Property,
147 std::variant<pldm::dbus::Property>>>>>;
George Liu1e44c732020-02-28 20:20:06 +0800148
149/**
150 * @brief The interface for DBusHandler
151 */
152class DBusHandlerInterface
153{
154 public:
155 virtual ~DBusHandlerInterface() = default;
156
George Liu36e81352020-07-01 14:40:30 +0800157 virtual std::string getService(const char* path,
158 const char* interface) const = 0;
159
George Liu1e44c732020-02-28 20:20:06 +0800160 virtual void setDbusProperty(const DBusMapping& dBusMap,
161 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800162
163 virtual PropertyValue
164 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
165 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800166};
167
Sampa Misraa2fa0702019-05-31 01:28:55 -0500168/**
169 * @class DBusHandler
170 *
171 * Wrapper class to handle the D-Bus calls
172 *
173 * This class contains the APIs to handle the D-Bus calls
174 * to cater the request from pldm requester.
175 * A class is created to mock the apis in the test cases
176 */
George Liu1e44c732020-02-28 20:20:06 +0800177class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500178{
179 public:
George Liu0e02c322020-01-01 09:41:51 +0800180 /** @brief Get the bus connection. */
181 static auto& getBus()
182 {
183 static auto bus = sdbusplus::bus::new_default();
184 return bus;
185 }
186
187 /**
188 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800189 *
George Liu0e02c322020-01-01 09:41:51 +0800190 * @param[in] path - DBUS object path
191 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800192 *
George Liu0e02c322020-01-01 09:41:51 +0800193 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800194 *
Patrick Williams4fea7a22021-09-02 09:54:12 -0500195 * @throw sdbusplus::exception::exception when it fails
George Liu0e02c322020-01-01 09:41:51 +0800196 */
George Liu36e81352020-07-01 14:40:30 +0800197 std::string getService(const char* path,
198 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800199
John Wang9e242422020-03-05 08:37:50 +0800200 /** @brief Get property(type: variant) from the requested dbus
201 *
202 * @param[in] objPath - The Dbus object path
203 * @param[in] dbusProp - The property name to get
204 * @param[in] dbusInterface - The Dbus interface
205 *
206 * @return The value of the property(type: variant)
207 *
Patrick Williams4fea7a22021-09-02 09:54:12 -0500208 * @throw sdbusplus::exception::exception when it fails
John Wang9e242422020-03-05 08:37:50 +0800209 */
210 PropertyValue
211 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
212 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800213
John Wang9e242422020-03-05 08:37:50 +0800214 /** @brief The template function to get property from the requested dbus
215 * path
216 *
217 * @tparam Property - Excepted type of the property on dbus
218 *
219 * @param[in] objPath - The Dbus object path
220 * @param[in] dbusProp - The property name to get
221 * @param[in] dbusInterface - The Dbus interface
222 *
223 * @return The value of the property
224 *
Patrick Williams4fea7a22021-09-02 09:54:12 -0500225 * @throw sdbusplus::exception::exception when dbus request fails
John Wang9e242422020-03-05 08:37:50 +0800226 * std::bad_variant_access when \p Property and property on dbus do
227 * not match
228 */
John Wang92b3c972019-10-17 11:06:41 +0800229 template <typename Property>
230 auto getDbusProperty(const char* objPath, const char* dbusProp,
231 const char* dbusInterface)
232 {
John Wang9e242422020-03-05 08:37:50 +0800233 auto VariantValue =
234 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800235 return std::get<Property>(VariantValue);
236 }
George Liu1e44c732020-02-28 20:20:06 +0800237
238 /** @brief Set Dbus property
239 *
240 * @param[in] dBusMap - Object path, property name, interface and property
241 * type for the D-Bus object
242 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800243 *
Patrick Williams4fea7a22021-09-02 09:54:12 -0500244 * @throw sdbusplus::exception::exception when it fails
George Liu1e44c732020-02-28 20:20:06 +0800245 */
246 void setDbusProperty(const DBusMapping& dBusMap,
247 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500248};
249
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500250/** @brief Fetch parent D-Bus object based on pathname
251 *
252 * @param[in] dbusObj - child D-Bus object
253 *
254 * @return std::string - the parent D-Bus object path
255 */
256inline std::string findParent(const std::string& dbusObj)
257{
258 fs::path p(dbusObj);
259 return p.parent_path().string();
260}
261
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500262/** @brief Read (static) MCTP EID of host firmware from a file
263 *
264 * @return uint8_t - MCTP EID
265 */
266uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530267
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530268/** @brief Convert a value in the JSON to a D-Bus property value
269 *
270 * @param[in] type - type of the D-Bus property
271 * @param[in] value - value in the JSON file
272 *
273 * @return PropertyValue - the D-Bus property value
274 */
275PropertyValue jsonEntryToDbusVal(std::string_view type,
276 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500277
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500278/** @brief Find State Effecter PDR
279 * @param[in] tid - PLDM terminus ID.
280 * @param[in] entityID - entity that can be associated with PLDM State set.
281 * @param[in] stateSetId - value that identifies PLDM State set.
282 * @param[in] repo - pointer to BMC's primary PDR repo.
283 * @return array[array[uint8_t]] - StateEffecterPDRs
284 */
285std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
286 uint16_t entityID,
287 uint16_t stateSetId,
288 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800289/** @brief Find State Sensor PDR
290 * @param[in] tid - PLDM terminus ID.
291 * @param[in] entityID - entity that can be associated with PLDM State set.
292 * @param[in] stateSetId - value that identifies PLDM State set.
293 * @param[in] repo - pointer to BMC's primary PDR repo.
294 * @return array[array[uint8_t]] - StateSensorPDRs
295 */
296std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
297 uint16_t entityID,
298 uint16_t stateSetId,
299 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500300
Sampa Misra3a0e3b92020-10-21 05:58:00 -0500301/** @brief Find sensor id from a state sensor PDR
302 *
303 * @param[in] pdrRepo - PDR repository
304 * @param[in] tid - terminus id
305 * @param[in] entityType - entity type
306 * @param[in] entityInstance - entity instance number
307 * @param[in] containerId - container id
308 * @param[in] stateSetId - state set id
309 *
310 * @return uint16_t - the sensor id
311 */
312uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid,
313 uint16_t entityType, uint16_t entityInstance,
314 uint16_t containerId, uint16_t stateSetId);
315
Tom Joseph250c4752020-04-15 10:32:45 +0530316/** @brief Find effecter id from a state effecter pdr
317 * @param[in] pdrRepo - PDR repository
318 * @param[in] entityType - entity type
319 * @param[in] entityInstance - entity instance number
320 * @param[in] containerId - container id
321 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500322 * @param[in] localOrRemote - true for checking local repo and false for remote
323 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530324 *
325 * @return uint16_t - the effecter id
326 */
327uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
328 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500329 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530330
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800331/** @brief Emit the sensor event signal
332 *
333 * @param[in] tid - the terminus id
334 * @param[in] sensorId - sensorID value of the sensor
335 * @param[in] sensorOffset - Identifies which state sensor within a
336 * composite state sensor the event is being returned for
337 * @param[in] eventState - The event state value from the state change that
338 * triggered the event message
339 * @param[in] previousEventState - The event state value for the state from
340 * which the present event state was entered.
341 * @return PLDM completion code
342 */
343int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
344 uint8_t sensorOffset, uint8_t eventState,
345 uint8_t previousEventState);
346
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600347/** @brief Print the buffer
348 *
Tom Josephe5268cd2021-09-07 13:04:03 +0530349 * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if
350 the buffer is an incoming PLDM message
351 * @param[in] buffer - Buffer to print
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600352 *
353 * @return - None
354 */
Tom Josephe5268cd2021-09-07 13:04:03 +0530355void printBuffer(bool isTx, const std::vector<uint8_t>& buffer);
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600356
Tom Joseph54922072021-06-19 02:45:46 -0700357/** @brief Convert the buffer to std::string
358 *
359 * If there are characters that are not printable characters, it is replaced
360 * with space(0x20).
361 *
362 * @param[in] var - pointer to data and length of the data
363 *
364 * @return std::string equivalent of variable field
365 */
366std::string toString(const struct variable_field& var);
367
George Liu83409572019-12-24 18:42:54 +0800368} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600369} // namespace pldm