blob: 7478aabb852398bd9763c13bb50a034d996ea2ee [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";
Pavithra Barithaya47180ac2020-10-28 02:12:05 -0500124constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
Sampa Misraa2fa0702019-05-31 01:28:55 -0500125
George Liu1e44c732020-02-28 20:20:06 +0800126struct DBusMapping
127{
128 std::string objectPath; //!< D-Bus object path
129 std::string interface; //!< D-Bus interface
130 std::string propertyName; //!< D-Bus property name
131 std::string propertyType; //!< D-Bus property type
132};
133
134using PropertyValue =
135 std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t,
136 uint64_t, double, std::string>;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500137using DbusProp = std::string;
138using DbusChangedProps = std::map<DbusProp, PropertyValue>;
George Liu1e44c732020-02-28 20:20:06 +0800139
140/**
141 * @brief The interface for DBusHandler
142 */
143class DBusHandlerInterface
144{
145 public:
146 virtual ~DBusHandlerInterface() = default;
147
George Liu36e81352020-07-01 14:40:30 +0800148 virtual std::string getService(const char* path,
149 const char* interface) const = 0;
150
George Liu1e44c732020-02-28 20:20:06 +0800151 virtual void setDbusProperty(const DBusMapping& dBusMap,
152 const PropertyValue& value) const = 0;
John Wang9e242422020-03-05 08:37:50 +0800153
154 virtual PropertyValue
155 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
156 const char* dbusInterface) const = 0;
George Liu1e44c732020-02-28 20:20:06 +0800157};
158
Sampa Misraa2fa0702019-05-31 01:28:55 -0500159/**
160 * @class DBusHandler
161 *
162 * Wrapper class to handle the D-Bus calls
163 *
164 * This class contains the APIs to handle the D-Bus calls
165 * to cater the request from pldm requester.
166 * A class is created to mock the apis in the test cases
167 */
George Liu1e44c732020-02-28 20:20:06 +0800168class DBusHandler : public DBusHandlerInterface
Sampa Misraa2fa0702019-05-31 01:28:55 -0500169{
170 public:
George Liu0e02c322020-01-01 09:41:51 +0800171 /** @brief Get the bus connection. */
172 static auto& getBus()
173 {
174 static auto bus = sdbusplus::bus::new_default();
175 return bus;
176 }
177
178 /**
179 * @brief Get the DBUS Service name for the input dbus path
John Wang9e242422020-03-05 08:37:50 +0800180 *
George Liu0e02c322020-01-01 09:41:51 +0800181 * @param[in] path - DBUS object path
182 * @param[in] interface - DBUS Interface
John Wang9e242422020-03-05 08:37:50 +0800183 *
George Liu0e02c322020-01-01 09:41:51 +0800184 * @return std::string - the dbus service name
John Wang9e242422020-03-05 08:37:50 +0800185 *
186 * @throw sdbusplus::exception::SdBusError when it fails
George Liu0e02c322020-01-01 09:41:51 +0800187 */
George Liu36e81352020-07-01 14:40:30 +0800188 std::string getService(const char* path,
189 const char* interface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800190
John Wang9e242422020-03-05 08:37:50 +0800191 /** @brief Get property(type: variant) from the requested dbus
192 *
193 * @param[in] objPath - The Dbus object path
194 * @param[in] dbusProp - The property name to get
195 * @param[in] dbusInterface - The Dbus interface
196 *
197 * @return The value of the property(type: variant)
198 *
199 * @throw sdbusplus::exception::SdBusError when it fails
200 */
201 PropertyValue
202 getDbusPropertyVariant(const char* objPath, const char* dbusProp,
203 const char* dbusInterface) const override;
George Liu0e02c322020-01-01 09:41:51 +0800204
John Wang9e242422020-03-05 08:37:50 +0800205 /** @brief The template function to get property from the requested dbus
206 * path
207 *
208 * @tparam Property - Excepted type of the property on dbus
209 *
210 * @param[in] objPath - The Dbus object path
211 * @param[in] dbusProp - The property name to get
212 * @param[in] dbusInterface - The Dbus interface
213 *
214 * @return The value of the property
215 *
216 * @throw sdbusplus::exception::SdBusError when dbus request fails
217 * std::bad_variant_access when \p Property and property on dbus do
218 * not match
219 */
John Wang92b3c972019-10-17 11:06:41 +0800220 template <typename Property>
221 auto getDbusProperty(const char* objPath, const char* dbusProp,
222 const char* dbusInterface)
223 {
John Wang9e242422020-03-05 08:37:50 +0800224 auto VariantValue =
225 getDbusPropertyVariant(objPath, dbusProp, dbusInterface);
John Wang92b3c972019-10-17 11:06:41 +0800226 return std::get<Property>(VariantValue);
227 }
George Liu1e44c732020-02-28 20:20:06 +0800228
229 /** @brief Set Dbus property
230 *
231 * @param[in] dBusMap - Object path, property name, interface and property
232 * type for the D-Bus object
233 * @param[in] value - The value to be set
John Wang9e242422020-03-05 08:37:50 +0800234 *
235 * @throw sdbusplus::exception::SdBusError when it fails
George Liu1e44c732020-02-28 20:20:06 +0800236 */
237 void setDbusProperty(const DBusMapping& dBusMap,
238 const PropertyValue& value) const override;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500239};
240
Deepak Kodihalli3cd61812020-03-10 06:38:45 -0500241/** @brief Fetch parent D-Bus object based on pathname
242 *
243 * @param[in] dbusObj - child D-Bus object
244 *
245 * @return std::string - the parent D-Bus object path
246 */
247inline std::string findParent(const std::string& dbusObj)
248{
249 fs::path p(dbusObj);
250 return p.parent_path().string();
251}
252
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500253/** @brief Read (static) MCTP EID of host firmware from a file
254 *
255 * @return uint8_t - MCTP EID
256 */
257uint8_t readHostEID();
Tom Joseph250c4752020-04-15 10:32:45 +0530258
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530259/** @brief Convert a value in the JSON to a D-Bus property value
260 *
261 * @param[in] type - type of the D-Bus property
262 * @param[in] value - value in the JSON file
263 *
264 * @return PropertyValue - the D-Bus property value
265 */
266PropertyValue jsonEntryToDbusVal(std::string_view type,
267 const nlohmann::json& value);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500268
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500269/** @brief Find State Effecter PDR
270 * @param[in] tid - PLDM terminus ID.
271 * @param[in] entityID - entity that can be associated with PLDM State set.
272 * @param[in] stateSetId - value that identifies PLDM State set.
273 * @param[in] repo - pointer to BMC's primary PDR repo.
274 * @return array[array[uint8_t]] - StateEffecterPDRs
275 */
276std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid,
277 uint16_t entityID,
278 uint16_t stateSetId,
279 const pldm_pdr* repo);
Chicago Duan738e4d82020-05-28 16:39:19 +0800280/** @brief Find State Sensor PDR
281 * @param[in] tid - PLDM terminus ID.
282 * @param[in] entityID - entity that can be associated with PLDM State set.
283 * @param[in] stateSetId - value that identifies PLDM State set.
284 * @param[in] repo - pointer to BMC's primary PDR repo.
285 * @return array[array[uint8_t]] - StateSensorPDRs
286 */
287std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid,
288 uint16_t entityID,
289 uint16_t stateSetId,
290 const pldm_pdr* repo);
Pavithra Barithaya0f74c982020-04-27 02:17:10 -0500291
Tom Joseph250c4752020-04-15 10:32:45 +0530292/** @brief Find effecter id from a state effecter pdr
293 * @param[in] pdrRepo - PDR repository
294 * @param[in] entityType - entity type
295 * @param[in] entityInstance - entity instance number
296 * @param[in] containerId - container id
297 * @param[in] stateSetId - state set id
Sampa Misraa4a96162020-07-14 05:33:46 -0500298 * @param[in] localOrRemote - true for checking local repo and false for remote
299 * repo
Tom Joseph250c4752020-04-15 10:32:45 +0530300 *
301 * @return uint16_t - the effecter id
302 */
303uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType,
304 uint16_t entityInstance, uint16_t containerId,
Sampa Misraa4a96162020-07-14 05:33:46 -0500305 uint16_t stateSetId, bool localOrRemote);
Tom Joseph250c4752020-04-15 10:32:45 +0530306
Chicago Duanfe4d88b2020-06-12 16:44:13 +0800307/** @brief Emit the sensor event signal
308 *
309 * @param[in] tid - the terminus id
310 * @param[in] sensorId - sensorID value of the sensor
311 * @param[in] sensorOffset - Identifies which state sensor within a
312 * composite state sensor the event is being returned for
313 * @param[in] eventState - The event state value from the state change that
314 * triggered the event message
315 * @param[in] previousEventState - The event state value for the state from
316 * which the present event state was entered.
317 * @return PLDM completion code
318 */
319int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId,
320 uint8_t sensorOffset, uint8_t eventState,
321 uint8_t previousEventState);
322
George Liu83409572019-12-24 18:42:54 +0800323} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -0600324} // namespace pldm