1*a31ceb91SManojkiran Eda #pragma once
2*a31ceb91SManojkiran Eda 
3*a31ceb91SManojkiran Eda #include "libpldmresponder/platform.hpp"
4*a31ceb91SManojkiran Eda 
5*a31ceb91SManojkiran Eda #include <libpldm/pdr.h>
6*a31ceb91SManojkiran Eda #include <libpldm/platform.h>
7*a31ceb91SManojkiran Eda 
8*a31ceb91SManojkiran Eda #include <sdbusplus/bus/match.hpp>
9*a31ceb91SManojkiran Eda #include <sdeventplus/event.hpp>
10*a31ceb91SManojkiran Eda #include <sdeventplus/utility/timer.hpp>
11*a31ceb91SManojkiran Eda 
12*a31ceb91SManojkiran Eda #include <memory>
13*a31ceb91SManojkiran Eda 
14*a31ceb91SManojkiran Eda namespace pldm
15*a31ceb91SManojkiran Eda {
16*a31ceb91SManojkiran Eda 
17*a31ceb91SManojkiran Eda using namespace sdbusplus::bus::match::rules;
18*a31ceb91SManojkiran Eda 
19*a31ceb91SManojkiran Eda namespace responder
20*a31ceb91SManojkiran Eda {
21*a31ceb91SManojkiran Eda 
22*a31ceb91SManojkiran Eda using ObjectPath = std::string;
23*a31ceb91SManojkiran Eda using AssociatedEntityMap = std::map<ObjectPath, pldm_entity>;
24*a31ceb91SManojkiran Eda 
25*a31ceb91SManojkiran Eda /** @class SlotHandler
26*a31ceb91SManojkiran Eda  *
27*a31ceb91SManojkiran Eda  *  @brief This class performs the necessary operation in pldm for
28*a31ceb91SManojkiran Eda  *         Slot Enable operation. That includes taking actions on the
29*a31ceb91SManojkiran Eda  *         setStateEffecterStates calls from Host and also sending
30*a31ceb91SManojkiran Eda  *         notification to inventory manager application
31*a31ceb91SManojkiran Eda  */
32*a31ceb91SManojkiran Eda class SlotHandler
33*a31ceb91SManojkiran Eda {
34*a31ceb91SManojkiran Eda   public:
35*a31ceb91SManojkiran Eda     SlotHandler() = delete;
36*a31ceb91SManojkiran Eda     ~SlotHandler() = default;
37*a31ceb91SManojkiran Eda     SlotHandler(const SlotHandler&) = delete;
38*a31ceb91SManojkiran Eda     SlotHandler& operator=(const SlotHandler&) = delete;
39*a31ceb91SManojkiran Eda     SlotHandler(SlotHandler&&) = delete;
40*a31ceb91SManojkiran Eda     SlotHandler& operator=(SlotHandler&&) = delete;
41*a31ceb91SManojkiran Eda 
42*a31ceb91SManojkiran Eda     /** @brief SlotHandler Constructor
43*a31ceb91SManojkiran Eda      *
44*a31ceb91SManojkiran Eda      * @param[in] event - reference for the main event loop
45*a31ceb91SManojkiran Eda      * @param[in] repo - pointer to the BMC's Primary PDR repo
46*a31ceb91SManojkiran Eda      *
47*a31ceb91SManojkiran Eda      */
SlotHandler(const sdeventplus::Event & event,pldm_pdr * repo)48*a31ceb91SManojkiran Eda     SlotHandler(const sdeventplus::Event& event, pldm_pdr* repo) :
49*a31ceb91SManojkiran Eda         timer(event,
50*a31ceb91SManojkiran Eda               std::bind(std::mem_fn(&SlotHandler::timeOutHandler), this)),
51*a31ceb91SManojkiran Eda         pdrRepo(repo)
52*a31ceb91SManojkiran Eda     {
53*a31ceb91SManojkiran Eda         fruPresenceMatch = nullptr;
54*a31ceb91SManojkiran Eda         currentOnGoingSlotEntity.entity_type = 0;
55*a31ceb91SManojkiran Eda         currentOnGoingSlotEntity.entity_instance_num = 0;
56*a31ceb91SManojkiran Eda         currentOnGoingSlotEntity.entity_container_id = 0;
57*a31ceb91SManojkiran Eda     }
58*a31ceb91SManojkiran Eda 
59*a31ceb91SManojkiran Eda     /** @brief Method to be called when enabling a Slot for ADD/REMOVE/REPLACE
60*a31ceb91SManojkiran Eda      *  @param[in] effecterID - The effecter ID of the effecter that is set from
61*a31ceb91SManojkiran Eda      *                          host
62*a31ceb91SManojkiran Eda      *  @param[in] fruAssociationMap - the dbus path to pldm entity stored while
63*a31ceb91SManojkiran Eda      *                                 creating the pldm fru records
64*a31ceb91SManojkiran Eda      *  @param[in] stateFieldValue - the current Effecter stateFieldValue
65*a31ceb91SManojkiran Eda      */
66*a31ceb91SManojkiran Eda     void enableSlot(uint16_t effecterId,
67*a31ceb91SManojkiran Eda                     const AssociatedEntityMap& fruAssociationMap,
68*a31ceb91SManojkiran Eda                     uint8_t stateFieldValue);
69*a31ceb91SManojkiran Eda 
70*a31ceb91SManojkiran Eda     /** @brief Method to be used for fetching the slot sensor value
71*a31ceb91SManojkiran Eda      *  @param[in] slotObjectPath - the dbus object path for slot object
72*a31ceb91SManojkiran Eda      *  @return - returns the sensor state for the respective slot sensor
73*a31ceb91SManojkiran Eda      */
74*a31ceb91SManojkiran Eda     uint8_t fetchSlotSensorState(const std::string& slotObjectPath);
75*a31ceb91SManojkiran Eda 
76*a31ceb91SManojkiran Eda     /** @brief Method to set the oem platform handler in SlotHandler class
77*a31ceb91SManojkiran Eda      *  @param[in] handler - pointer to the oem platform handler
78*a31ceb91SManojkiran Eda      */
79*a31ceb91SManojkiran Eda     void setOemPlatformHandler(pldm::responder::oem_platform::Handler* handler);
80*a31ceb91SManojkiran Eda 
81*a31ceb91SManojkiran Eda   private:
82*a31ceb91SManojkiran Eda     /** @brief call back method called when the timer is expired. This handler
83*a31ceb91SManojkiran Eda      *  is called when the change of state cannot be made for a slot.
84*a31ceb91SManojkiran Eda      */
85*a31ceb91SManojkiran Eda     void timeOutHandler();
86*a31ceb91SManojkiran Eda 
87*a31ceb91SManojkiran Eda     /** @brief Abstracted method for obtaining the entityID from effecterID
88*a31ceb91SManojkiran Eda      *  @param[in]  effecterID - The effecterID of the BMC effecter
89*a31ceb91SManojkiran Eda      *  @return - pldm entity ID for the given effecter ID
90*a31ceb91SManojkiran Eda      */
91*a31ceb91SManojkiran Eda     pldm_entity getEntityIDfromEffecterID(uint16_t effecterID);
92*a31ceb91SManojkiran Eda 
93*a31ceb91SManojkiran Eda     /** @brief Abstracted method for processing the Slot Operations
94*a31ceb91SManojkiran Eda      *  @param[in] slotObjPath - The slot dbus object path
95*a31ceb91SManojkiran Eda      *  @param[in] entity - the current slot pldm entity under operation
96*a31ceb91SManojkiran Eda      *  @param[in] stateFieldValue - the current Effecter stateFieldValue
97*a31ceb91SManojkiran Eda      */
98*a31ceb91SManojkiran Eda     void processSlotOperations(const std::string& slotObjectPath,
99*a31ceb91SManojkiran Eda                                const pldm_entity& entity,
100*a31ceb91SManojkiran Eda                                uint8_t stateFieldValue);
101*a31ceb91SManojkiran Eda 
102*a31ceb91SManojkiran Eda     /** @brief Method to obtain the Adapter dbus Object Path from Slot Adapter
103*a31ceb91SManojkiran Eda      * path
104*a31ceb91SManojkiran Eda      *  @param[in] slotObjPath - The slot dbus object path
105*a31ceb91SManojkiran Eda      *  @return - if Successfull, returns the adapter dbus object path
106*a31ceb91SManojkiran Eda      */
107*a31ceb91SManojkiran Eda     std::optional<std::string>
108*a31ceb91SManojkiran Eda         getAdapterObjPath(const std::string& slotObjPath);
109*a31ceb91SManojkiran Eda 
110*a31ceb91SManojkiran Eda     /** @brief Method to call VPD collection & VPD removal API's
111*a31ceb91SManojkiran Eda      *  @param[in] adapterObjectPath - The adapter D-Bus object path
112*a31ceb91SManojkiran Eda      *  @param[in] stateFieldvalue - The current stateField value from set
113*a31ceb91SManojkiran Eda      *                               effecter call
114*a31ceb91SManojkiran Eda      */
115*a31ceb91SManojkiran Eda     void callVPDManager(const std::string& adapterObjPath,
116*a31ceb91SManojkiran Eda                         uint8_t stateFieldValue);
117*a31ceb91SManojkiran Eda 
118*a31ceb91SManojkiran Eda     /** @brief Method to create a matcher to catch the property change signal
119*a31ceb91SManojkiran Eda      *  @param[in] adapterObjectPath - The adapter D-Bus object path
120*a31ceb91SManojkiran Eda      *  @param[in] stateFieldvalue - The current stateField value from set
121*a31ceb91SManojkiran Eda      *                               effecter call
122*a31ceb91SManojkiran Eda      *  @param[in] entity - the current slot pldm entity under operation
123*a31ceb91SManojkiran Eda      */
124*a31ceb91SManojkiran Eda     void createPresenceMatch(const std::string& adapterObjectPath,
125*a31ceb91SManojkiran Eda                              const pldm_entity& entity,
126*a31ceb91SManojkiran Eda                              uint8_t stateFieldValue);
127*a31ceb91SManojkiran Eda 
128*a31ceb91SManojkiran Eda     /** @brief Method to process the Property change signal from Preset Property
129*a31ceb91SManojkiran Eda      *  @param[in] presentValue - The current value of present Value
130*a31ceb91SManojkiran Eda      *  @param[in] stateFieldvalue - The current stateField value from set
131*a31ceb91SManojkiran Eda      *                               effecter call
132*a31ceb91SManojkiran Eda      *  @param[in] entity - the current slot pldm entity under operation
133*a31ceb91SManojkiran Eda      */
134*a31ceb91SManojkiran Eda     void processPresentPropertyChange(
135*a31ceb91SManojkiran Eda         bool presentValue, uint8_t stateFieldvalue, const pldm_entity& entity);
136*a31ceb91SManojkiran Eda 
137*a31ceb91SManojkiran Eda     /** @brief Get the sensor state from D-Bus
138*a31ceb91SManojkiran Eda      *  @param[in] adapterObjectPath - reference of the Adapter dbus object path
139*a31ceb91SManojkiran Eda      *  @return - Boolean value of sensor state
140*a31ceb91SManojkiran Eda      */
141*a31ceb91SManojkiran Eda 
142*a31ceb91SManojkiran Eda     bool fetchSensorStateFromDbus(const std::string& adapterObjectPath);
143*a31ceb91SManojkiran Eda 
144*a31ceb91SManojkiran Eda     /* @brief Method to send a state sensor event to Host from SlotHandler class
145*a31ceb91SManojkiran Eda      * @param[in] sensorId - sensor id for the event
146*a31ceb91SManojkiran Eda      * @param[in] sensorEventClass - sensor event class wrt DSP0248
147*a31ceb91SManojkiran Eda      * @param[in] sensorOffset - sensor offset
148*a31ceb91SManojkiran Eda      * @param[in] eventState - new event state
149*a31ceb91SManojkiran Eda      * @param[in] prevEventState - previous state
150*a31ceb91SManojkiran Eda      */
151*a31ceb91SManojkiran Eda     void sendStateSensorEvent(uint16_t sensorId,
152*a31ceb91SManojkiran Eda                               enum sensor_event_class_states sensorEventClass,
153*a31ceb91SManojkiran Eda                               uint8_t sensorOffset, uint8_t eventState,
154*a31ceb91SManojkiran Eda                               uint8_t prevEventState);
155*a31ceb91SManojkiran Eda 
156*a31ceb91SManojkiran Eda     pldm::responder::oem_platform::Handler* oemPlatformHandler =
157*a31ceb91SManojkiran Eda         nullptr; //!< oem platform handler
158*a31ceb91SManojkiran Eda 
159*a31ceb91SManojkiran Eda     /** @brief pointer to tha matcher for Present State for adapter object*/
160*a31ceb91SManojkiran Eda     std::unique_ptr<sdbusplus::bus::match_t> fruPresenceMatch;
161*a31ceb91SManojkiran Eda 
162*a31ceb91SManojkiran Eda     /** @brief Timer used for Slot VPD Collection operation */
163*a31ceb91SManojkiran Eda     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
164*a31ceb91SManojkiran Eda 
165*a31ceb91SManojkiran Eda     /** @brief pointer to BMC's primary PDR repo */
166*a31ceb91SManojkiran Eda     const pldm_pdr* pdrRepo;
167*a31ceb91SManojkiran Eda 
168*a31ceb91SManojkiran Eda     /** @brief variable to store the current slot entity under operation */
169*a31ceb91SManojkiran Eda     pldm_entity currentOnGoingSlotEntity;
170*a31ceb91SManojkiran Eda };
171*a31ceb91SManojkiran Eda 
172*a31ceb91SManojkiran Eda } // namespace responder
173*a31ceb91SManojkiran Eda } // namespace pldm
174