1 #pragma once
2 
3 #include "common/utils.hpp"
4 #include "libpldmresponder/pdr.hpp"
5 #include "pdr_utils.hpp"
6 #include "pldmd/handler.hpp"
7 
8 #include <libpldm/platform.h>
9 #include <libpldm/states.h>
10 
11 #include <phosphor-logging/lg2.hpp>
12 
13 #include <cstdint>
14 #include <map>
15 
16 PHOSPHOR_LOG2_USING;
17 
18 namespace pldm
19 {
20 namespace responder
21 {
22 namespace platform_state_effecter
23 {
24 /** @brief Function to set the effecter requested by pldm requester
25  *
26  *  @tparam[in] DBusInterface - DBus interface type
27  *  @tparam[in] Handler - pldm::responder::platform::Handler
28  *  @param[in] dBusIntf - The interface object of DBusInterface
29  *  @param[in] handler - The interface object of
30  *             pldm::responder::platform::Handler
31  *  @param[in] effecterId - Effecter ID sent by the requester to act on
32  *  @param[in] stateField - The state field data for each of the states,
33  * equal to composite effecter count in number
34  *  @return - Success or failure in setting the states. Returns failure in
35  * terms of PLDM completion codes if atleast one state fails to be set
36  */
37 template <class DBusInterface, class Handler>
38 int setStateEffecterStatesHandler(
39     const DBusInterface& dBusIntf, Handler& handler, uint16_t effecterId,
40     const std::vector<set_effecter_state_field>& stateField)
41 {
42     using namespace pldm::responder::pdr;
43     using namespace pldm::utils;
44     using StateSetNum = uint8_t;
45 
46     state_effecter_possible_states* states = nullptr;
47     pldm_state_effecter_pdr* pdr = nullptr;
48     uint8_t compEffecterCnt = stateField.size();
49 
50     std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> stateEffecterPdrRepo(
51         pldm_pdr_init(), pldm_pdr_destroy);
52     if (!stateEffecterPdrRepo)
53     {
54         throw std::runtime_error(
55             "Failed to instantiate state effecter PDR repository");
56     }
57     pldm::responder::pdr_utils::Repo stateEffecterPDRs(
58         stateEffecterPdrRepo.get());
59     getRepoByType(handler.getRepo(), stateEffecterPDRs,
60                   PLDM_STATE_EFFECTER_PDR);
61     if (stateEffecterPDRs.empty())
62     {
63         error("Failed to get record by PDR type");
64         return PLDM_PLATFORM_INVALID_EFFECTER_ID;
65     }
66 
67     pldm::responder::pdr_utils::PdrEntry pdrEntry{};
68     auto pdrRecord = stateEffecterPDRs.getFirstRecord(pdrEntry);
69     while (pdrRecord)
70     {
71         pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
72         if (pdr->effecter_id != effecterId)
73         {
74             pdr = nullptr;
75             pdrRecord = stateEffecterPDRs.getNextRecord(pdrRecord, pdrEntry);
76             continue;
77         }
78 
79         states = reinterpret_cast<state_effecter_possible_states*>(
80             pdr->possible_states);
81         if (compEffecterCnt > pdr->composite_effecter_count)
82         {
83             error(
84                 "The requester sent wrong composite effecter count for the effecter, EFFECTER_ID={EFFECTER_ID} COMP_EFF_CNT={COMP_EFF_CNT}",
85                 "EFFECTER_ID", effecterId, "COMP_EFF_CNT", compEffecterCnt);
86             return PLDM_ERROR_INVALID_DATA;
87         }
88         break;
89     }
90 
91     if (!pdr)
92     {
93         return PLDM_PLATFORM_INVALID_EFFECTER_ID;
94     }
95 
96     int rc = PLDM_SUCCESS;
97     try
98     {
99         const auto& [dbusMappings,
100                      dbusValMaps] = handler.getDbusObjMaps(effecterId);
101         for (uint8_t currState = 0; currState < compEffecterCnt; ++currState)
102         {
103             std::vector<StateSetNum> allowed{};
104             // computation is based on table 79 from DSP0248 v1.1.1
105             uint8_t bitfieldIndex = stateField[currState].effecter_state / 8;
106             uint8_t bit = stateField[currState].effecter_state -
107                           (8 * bitfieldIndex);
108             if (states->possible_states_size < bitfieldIndex ||
109                 !(states->states[bitfieldIndex].byte & (1 << bit)))
110             {
111                 error(
112                     "Invalid state set value, EFFECTER_ID={EFFECTER_ID} VALUE={EFFECTER_STATE} COMPOSITE_EFFECTER_ID={CURR_STATE} DBUS_PATH={DBUS_OBJ_PATH}",
113                     "EFFECTER_ID", effecterId, "EFFECTER_STATE",
114                     stateField[currState].effecter_state, "CURR_STATE",
115                     currState, "DBUS_OBJ_PATH",
116                     dbusMappings[currState].objectPath.c_str());
117                 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
118                 break;
119             }
120             const DBusMapping& dbusMapping = dbusMappings[currState];
121             const pldm::responder::pdr_utils::StatestoDbusVal& dbusValToMap =
122                 dbusValMaps[currState];
123 
124             if (stateField[currState].set_request == PLDM_REQUEST_SET)
125             {
126                 try
127                 {
128                     dBusIntf.setDbusProperty(
129                         dbusMapping,
130                         dbusValToMap.at(stateField[currState].effecter_state));
131                 }
132                 catch (const std::exception& e)
133                 {
134                     error(
135                         "Error setting property, ERROR={ERR_EXCEP} PROPERTY={DBUS_PROP} INTERFACE={DBUS_INTF} PATH={DBUS_OBJ_PATH}",
136                         "ERR_EXCEP", e.what(), "DBUS_PROP",
137                         dbusMapping.propertyName, "DBUS_INTF",
138                         dbusMapping.interface, "DBUS_OBJ_PATH",
139                         dbusMapping.objectPath.c_str());
140                     return PLDM_ERROR;
141                 }
142             }
143             uint8_t* nextState =
144                 reinterpret_cast<uint8_t*>(states) +
145                 sizeof(state_effecter_possible_states) -
146                 sizeof(states->states) +
147                 (states->possible_states_size * sizeof(states->states));
148             states =
149                 reinterpret_cast<state_effecter_possible_states*>(nextState);
150         }
151     }
152     catch (const std::out_of_range& e)
153     {
154         error("Unknown effecter ID : {EFFECTER_ID} {ERR_EXCEP}", "EFFECTER_ID",
155               effecterId, "ERR_EXCEP", e.what());
156         return PLDM_ERROR;
157     }
158 
159     return rc;
160 }
161 
162 } // namespace platform_state_effecter
163 } // namespace responder
164 } // namespace pldm
165