xref: /openbmc/openpower-occ-control/pldm.hpp (revision 815f9f55)
1 #pragma once
2 
3 #include "occ_status.hpp"
4 
5 #include <sdbusplus/bus/match.hpp>
6 
7 namespace pldm
8 {
9 
10 namespace MatchRules = sdbusplus::bus::match::rules;
11 
12 using EntityType = uint16_t;
13 using EntityInstance = uint16_t;
14 using EventState = uint8_t;
15 using PdrList = std::vector<std::vector<uint8_t>>;
16 using SensorID = uint16_t;
17 using SensorOffset = uint8_t;
18 using SensorToOCCInstance = std::map<SensorID, open_power::occ::instanceID>;
19 using TerminusID = uint8_t;
20 
21 /** @brief OCC instance starts with 0 for example "occ0" */
22 constexpr open_power::occ::instanceID start = 0;
23 
24 /** @brief Hardcoded TID */
25 constexpr TerminusID tid = 0;
26 
27 /** @class Interface
28  *
29  *  @brief Abstracts the PLDM details related to the OCC
30  */
31 class Interface
32 {
33   public:
34     Interface() = delete;
35     ~Interface() = default;
36     Interface(const Interface&) = delete;
37     Interface& operator=(const Interface&) = delete;
38     Interface(Interface&&) = delete;
39     Interface& operator=(Interface&&) = delete;
40 
41     /** @brief Constructs the PLDM Interface object for OCC functions
42      *
43      *  @param[in] bus      - reference to systemd bus
44      *  @param[in] callBack - callBack handler to invoke when the OCC state
45      *                        changes.
46      */
47     explicit Interface(
48         sdbusplus::bus::bus& bus,
49         std::function<bool(open_power::occ::instanceID, bool)> callBack) :
50         bus(bus),
51         callBack(callBack),
52         pldmEventSignal(
53             bus,
54             MatchRules::type::signal() +
55                 MatchRules::member("StateSensorEvent") +
56                 MatchRules::path("/xyz/openbmc_project/pldm") +
57                 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
58             std::bind(std::mem_fn(&Interface::sensorEvent), this,
59                       std::placeholders::_1)),
60         hostStateSignal(
61             bus,
62             MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
63                                           "xyz.openbmc_project.State.Host"),
64             std::bind(std::mem_fn(&Interface::hostStateEvent), this,
65                       std::placeholders::_1))
66     {
67     }
68 
69     /** @brief Fetch the OCC state sensor PDRs and populate the cache with
70      *         sensorId to OCC instance mapping information and the sensor
71      *         offset for Operational Running Status.
72      *
73      *  @param[in] pdrs - OCC state sensor PDRs
74      *  @param[out] sensorInstanceMap - map of sensorID to OCC instance
75      *  @param[out] sensorOffset - sensor offset of interested state set ID
76      */
77     void fetchOCCSensorInfo(const PdrList& pdrs,
78                             SensorToOCCInstance& sensorInstanceMap,
79                             SensorOffset& sensorOffset);
80 
81   private:
82     /** @brief reference to the systemd bus*/
83     sdbusplus::bus::bus& bus;
84 
85     /** @brief Callback handler to be invoked when the state of the OCC
86      *         changes
87      */
88     std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
89 
90     /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
91      *         processes if the event corresponds to OCC state change.
92      */
93     sdbusplus::bus::match_t pldmEventSignal;
94 
95     /** @brief Used to subscribe for host state change signal */
96     sdbusplus::bus::match_t hostStateSignal;
97 
98     /** @brief PLDM Sensor ID to OCC Instance mapping
99      */
100     SensorToOCCInstance sensorToOCCInstance;
101 
102     /** @brief Sensor offset of state set ID
103      * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
104      */
105     SensorOffset sensorOffset;
106 
107     /** @brief When the OCC state changes host sends PlatformEventMessage
108      *         StateSensorEvent, this function processes the D-Bus signal
109      *         with the sensor event information and invokes the callback
110      *         to change the OCC state.
111      *
112      *  @param[in] msg - data associated with the subscribed signal
113      */
114     void sensorEvent(sdbusplus::message::message& msg);
115 
116     /** @brief When the host state changes and if the CurrentHostState is
117      *         xyz.openbmc_project.State.Host.HostState.Off then
118      *         the cache of OCC sensors and effecters mapping is cleared.
119      *
120      *  @param[in] msg - data associated with the subscribed signal
121      */
122     void hostStateEvent(sdbusplus::message::message& msg);
123 
124     /** @brief Check if the PDR cache for PLDM OCC sensors is valid
125      *
126      *  @return true if cache is populated and false if the cache is not
127      *          populated.
128      */
129     auto isOCCSensorCacheValid()
130     {
131         return (sensorToOCCInstance.empty() ? false : true);
132     }
133 };
134 
135 } // namespace pldm
136