blob: 8bcc5966cdb3c50e63e717f3ea4300e3e1f57278 [file] [log] [blame]
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05301#pragma once
2
Sampa Misraa2fa0702019-05-31 01:28:55 -05003#include "config.h"
4
Deepak Kodihallibc669f12019-11-28 08:52:07 -06005#include "handler.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05006#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08007#include "libpldmresponder/pdr_utils.hpp"
George Liu83409572019-12-24 18:42:54 +08008#include "utils.hpp"
Sampa Misraa2fa0702019-05-31 01:28:55 -05009
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053010#include <stdint.h>
11
Sampa Misraa2fa0702019-05-31 01:28:55 -050012#include <map>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053013
14#include "libpldm/platform.h"
Sampa Misraa2fa0702019-05-31 01:28:55 -050015#include "libpldm/states.h"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053016
17namespace pldm
18{
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053019namespace responder
20{
Sampa Misraa2fa0702019-05-31 01:28:55 -050021namespace platform
22{
23
Deepak Kodihallic682fe22020-03-04 00:42:54 -060024using DbusPath = std::string;
25using EffecterObjs = std::vector<DbusPath>;
26
Deepak Kodihallibc669f12019-11-28 08:52:07 -060027class Handler : public CmdHandler
Sampa Misraa2fa0702019-05-31 01:28:55 -050028{
Deepak Kodihallibc669f12019-11-28 08:52:07 -060029 public:
Deepak Kodihallic682fe22020-03-04 00:42:54 -060030 Handler(const std::string& dir, pldm_pdr* repo) : pdrRepo(repo)
Sampa Misraa2fa0702019-05-31 01:28:55 -050031 {
Deepak Kodihallic682fe22020-03-04 00:42:54 -060032 generate(dir, pdrRepo);
33
Deepak Kodihallibc669f12019-11-28 08:52:07 -060034 handlers.emplace(PLDM_GET_PDR,
35 [this](const pldm_msg* request, size_t payloadLength) {
36 return this->getPDR(request, payloadLength);
37 });
38 handlers.emplace(PLDM_SET_STATE_EFFECTER_STATES,
39 [this](const pldm_msg* request, size_t payloadLength) {
40 return this->setStateEffecterStates(request,
41 payloadLength);
42 });
Sampa Misraa2fa0702019-05-31 01:28:55 -050043 }
44
Deepak Kodihallic682fe22020-03-04 00:42:54 -060045 const EffecterObjs& getEffecterObjs(uint16_t effecterId) const
46 {
47 return effecterObjs.at(effecterId);
48 }
49
50 void addEffecterObjs(uint16_t effecterId, EffecterObjs&& paths)
51 {
52 effecterObjs.emplace(effecterId, std::move(paths));
53 }
54
55 uint16_t getNextEffecterId()
56 {
57 return ++nextEffecterId;
58 }
59
60 /** @brief Parse PDR JSONs and build PDR repository
61 *
62 * @param[in] dir - directory housing platform specific PDR JSON files
63 * @param[in] repo - instance of concrete implementation of Repo
64 */
65 void generate(const std::string& dir, Repo& repo);
66
67 /** @brief Parse PDR JSONs and build state effecter PDR repository
68 *
69 * @param[in] json - platform specific PDR JSON files
70 * @param[in] repo - instance of state effecter implementation of Repo
71 */
72 void generateStateEffecterRepo(const Json& json, Repo& repo);
73
Deepak Kodihallibc669f12019-11-28 08:52:07 -060074 /** @brief Handler for GetPDR
75 *
76 * @param[in] request - Request message payload
77 * @param[in] payloadLength - Request payload length
78 * @param[out] Response - Response message written here
79 */
80 Response getPDR(const pldm_msg* request, size_t payloadLength);
Sampa Misraa2fa0702019-05-31 01:28:55 -050081
Deepak Kodihallibc669f12019-11-28 08:52:07 -060082 /** @brief Handler for setStateEffecterStates
83 *
84 * @param[in] request - Request message
85 * @param[in] payloadLength - Request payload length
86 * @return Response - PLDM Response message
87 */
88 Response setStateEffecterStates(const pldm_msg* request,
89 size_t payloadLength);
90
91 /** @brief Function to set the effecter requested by pldm requester
92 * @param[in] dBusIntf - The interface object
93 * @param[in] effecterId - Effecter ID sent by the requester to act on
94 * @param[in] stateField - The state field data for each of the states,
95 * equal to composite effecter count in number
96 * @return - Success or failure in setting the states. Returns failure in
97 * terms of PLDM completion codes if atleast one state fails to be set
98 */
99 template <class DBusInterface>
100 int setStateEffecterStatesHandler(
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600101 const DBusInterface& dBusIntf, uint16_t effecterId,
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600102 const std::vector<set_effecter_state_field>& stateField)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500103 {
George Liue53193f2020-02-24 09:23:26 +0800104 using namespace pldm::responder::pdr;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600105 using namespace std::string_literals;
106 using DBusProperty = std::variant<std::string, bool>;
107 using StateSetId = uint16_t;
108 using StateSetNum = uint8_t;
109 using PropertyMap =
110 std::map<StateSetId, std::map<StateSetNum, DBusProperty>>;
111 static const PropertyMap stateNumToDbusProp = {
112 {PLDM_BOOT_PROGRESS_STATE,
113 {{PLDM_BOOT_NOT_ACTIVE,
114 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus."
115 "Standby"s},
116 {PLDM_BOOT_COMPLETED,
117 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus."
118 "BootComplete"s}}},
119 {PLDM_SYSTEM_POWER_STATE,
120 {{PLDM_OFF_SOFT_GRACEFUL,
121 "xyz.openbmc_project.State.Chassis.Transition.Off"s}}}};
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600122 using namespace pldm::responder::pdr;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600123
124 state_effecter_possible_states* states = nullptr;
125 pldm_state_effecter_pdr* pdr = nullptr;
126 uint8_t compEffecterCnt = stateField.size();
George Liue53193f2020-02-24 09:23:26 +0800127 PdrEntry pdrEntry{};
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600128 auto pdrRecord = pdrRepo.getFirstRecord(pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800129 while (pdrRecord)
130 {
131 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrEntry.data);
132 if (pdr->effecter_id != effecterId)
Sampa Misraa2fa0702019-05-31 01:28:55 -0500133 {
George Liue53193f2020-02-24 09:23:26 +0800134 pdr = nullptr;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600135 pdrRecord = pdrRepo.getNextRecord(pdrRecord, pdrEntry);
George Liue53193f2020-02-24 09:23:26 +0800136 continue;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600137 }
George Liue53193f2020-02-24 09:23:26 +0800138
139 states = reinterpret_cast<state_effecter_possible_states*>(
140 pdr->possible_states);
141 if (compEffecterCnt > pdr->composite_effecter_count)
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600142 {
George Liue53193f2020-02-24 09:23:26 +0800143 std::cerr << "The requester sent wrong composite effecter"
144 << " count for the effecter, EFFECTER_ID="
145 << effecterId << "COMP_EFF_CNT=" << compEffecterCnt
146 << "\n";
147 return PLDM_ERROR_INVALID_DATA;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500148 }
George Liue53193f2020-02-24 09:23:26 +0800149 break;
150 }
151
152 if (!pdr)
153 {
154 return PLDM_PLATFORM_INVALID_EFFECTER_ID;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500155 }
Sampa Misraa2fa0702019-05-31 01:28:55 -0500156
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600157 std::map<StateSetId, std::function<int(const std::string& objPath,
158 const uint8_t currState)>>
159 effecterToDbusEntries = {
160 {PLDM_BOOT_PROGRESS_STATE,
161 [&](const std::string& objPath, const uint8_t currState) {
162 auto stateSet =
163 stateNumToDbusProp.find(PLDM_BOOT_PROGRESS_STATE);
164 if (stateSet == stateNumToDbusProp.end())
165 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600166 std::cerr << "Couldn't find D-Bus mapping for "
167 << "PLDM_BOOT_PROGRESS_STATE, EFFECTER_ID="
168 << effecterId << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600169 return PLDM_ERROR;
170 }
171 auto iter = stateSet->second.find(
172 stateField[currState].effecter_state);
173 if (iter == stateSet->second.end())
174 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600175 std::cerr << "Invalid state field passed or field not "
176 << "found for PLDM_BOOT_PROGRESS_STATE, "
177 "EFFECTER_ID="
178 << effecterId << " FIELD="
179 << stateField[currState].effecter_state
180 << " OBJECT_PATH=" << objPath.c_str()
181 << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600182 return PLDM_ERROR_INVALID_DATA;
183 }
184 auto dbusProp = "OperatingSystemState";
185 std::variant<std::string> value{
186 std::get<std::string>(iter->second)};
187 auto dbusInterface =
188 "xyz.openbmc_project.State.OperatingSystem.Status";
189 try
190 {
191 dBusIntf.setDbusProperty(objPath.c_str(), dbusProp,
192 dbusInterface, value);
193 }
194 catch (const std::exception& e)
195 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600196 std::cerr
197 << "Error setting property, ERROR=" << e.what()
198 << " PROPERTY=" << dbusProp
199 << " INTERFACE=" << dbusInterface
200 << " PATH=" << objPath.c_str() << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600201 return PLDM_ERROR;
202 }
203 return PLDM_SUCCESS;
204 }},
205 {PLDM_SYSTEM_POWER_STATE,
206 [&](const std::string& objPath, const uint8_t currState) {
207 auto stateSet =
208 stateNumToDbusProp.find(PLDM_SYSTEM_POWER_STATE);
209 if (stateSet == stateNumToDbusProp.end())
210 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600211 std::cerr << "Couldn't find D-Bus mapping for "
212 << "PLDM_SYSTEM_POWER_STATE, EFFECTER_ID="
213 << effecterId << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600214 return PLDM_ERROR;
215 }
216 auto iter = stateSet->second.find(
217 stateField[currState].effecter_state);
218 if (iter == stateSet->second.end())
219 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600220 std::cerr << "Invalid state field passed or field not "
221 << "found for PLDM_SYSTEM_POWER_STATE, "
222 "EFFECTER_ID="
223 << effecterId << " FIELD="
224 << stateField[currState].effecter_state
225 << " OBJECT_PATH=" << objPath.c_str()
226 << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600227 return PLDM_ERROR_INVALID_DATA;
228 }
229 auto dbusProp = "RequestedPowerTransition";
230 std::variant<std::string> value{
231 std::get<std::string>(iter->second)};
232 auto dbusInterface = "xyz.openbmc_project.State.Chassis";
233 try
234 {
235 dBusIntf.setDbusProperty(objPath.c_str(), dbusProp,
236 dbusInterface, value);
237 }
238 catch (const std::exception& e)
239 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600240 std::cerr
241 << "Error setting property, ERROR=" << e.what()
242 << " PROPERTY=" << dbusProp
243 << " INTERFACE=" << dbusInterface
244 << " PATH=" << objPath.c_str() << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600245 return PLDM_ERROR;
246 }
247 return PLDM_SUCCESS;
248 }}};
249
250 int rc = PLDM_SUCCESS;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600251 const auto& paths = getEffecterObjs(effecterId);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600252 for (uint8_t currState = 0; currState < compEffecterCnt; ++currState)
253 {
254 std::vector<StateSetNum> allowed{};
255 // computation is based on table 79 from DSP0248 v1.1.1
256 uint8_t bitfieldIndex = stateField[currState].effecter_state / 8;
257 uint8_t bit =
258 stateField[currState].effecter_state - (8 * bitfieldIndex);
259 if (states->possible_states_size < bitfieldIndex ||
260 !(states->states[bitfieldIndex].byte & (1 << bit)))
261 {
Sampa Misraaa8ae722019-12-12 03:20:40 -0600262 std::cerr << "Invalid state set value, EFFECTER_ID="
263 << effecterId
264 << " VALUE=" << stateField[currState].effecter_state
265 << " COMPOSITE_EFFECTER_ID=" << currState
266 << " DBUS_PATH=" << paths[currState].c_str() << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600267 rc = PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE;
268 break;
269 }
270 auto iter = effecterToDbusEntries.find(states->state_set_id);
271 if (iter == effecterToDbusEntries.end())
272 {
273 uint16_t setId = states->state_set_id;
Sampa Misraaa8ae722019-12-12 03:20:40 -0600274 std::cerr << "Did not find the state set for the"
275 << " state effecter pdr, STATE=" << setId
276 << " EFFECTER_ID=" << effecterId << "\n";
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600277 rc = PLDM_PLATFORM_INVALID_STATE_VALUE;
278 break;
279 }
280 if (stateField[currState].set_request == PLDM_REQUEST_SET)
281 {
282 rc = iter->second(paths[currState], currState);
283 if (rc != PLDM_SUCCESS)
284 {
285 break;
286 }
287 }
288 uint8_t* nextState =
289 reinterpret_cast<uint8_t*>(states) +
290 sizeof(state_effecter_possible_states) -
291 sizeof(states->states) +
292 (states->possible_states_size * sizeof(states->states));
293 states =
294 reinterpret_cast<state_effecter_possible_states*>(nextState);
295 }
296 return rc;
297 }
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600298
299 private:
300 pdr_utils::Repo pdrRepo;
301 uint16_t nextEffecterId{};
302 std::map<uint16_t, EffecterObjs> effecterObjs{};
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600303};
304
305} // namespace platform
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530306} // namespace responder
307} // namespace pldm