xref: /openbmc/bmcweb/features/redfish/lib/processor.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
1ac6a4445SGunnar Mills /*
2ac6a4445SGunnar Mills // Copyright (c) 2018 Intel Corporation
3ac6a4445SGunnar Mills //
4ac6a4445SGunnar Mills // Licensed under the Apache License, Version 2.0 (the "License");
5ac6a4445SGunnar Mills // you may not use this file except in compliance with the License.
6ac6a4445SGunnar Mills // You may obtain a copy of the License at
7ac6a4445SGunnar Mills //
8ac6a4445SGunnar Mills //      http://www.apache.org/licenses/LICENSE-2.0
9ac6a4445SGunnar Mills //
10ac6a4445SGunnar Mills // Unless required by applicable law or agreed to in writing, software
11ac6a4445SGunnar Mills // distributed under the License is distributed on an "AS IS" BASIS,
12ac6a4445SGunnar Mills // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ac6a4445SGunnar Mills // See the License for the specific language governing permissions and
14ac6a4445SGunnar Mills // limitations under the License.
15ac6a4445SGunnar Mills */
16ac6a4445SGunnar Mills #pragma once
17ac6a4445SGunnar Mills 
181e1e598dSJonathan Doman #include "dbus_singleton.hpp"
191e1e598dSJonathan Doman #include "error_messages.hpp"
20ac6a4445SGunnar Mills #include "health.hpp"
21ac6a4445SGunnar Mills 
227e860f15SJohn Edward Broadbent #include <app.hpp>
23ac6a4445SGunnar Mills #include <boost/container/flat_map.hpp>
24168e20c1SEd Tanous #include <dbus_utility.hpp>
2545ca1b86SEd Tanous #include <query.hpp>
26ed398213SEd Tanous #include <registries/privilege_registry.hpp>
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28dba0c291SJonathan Doman #include <sdbusplus/message/native_types.hpp>
29dba0c291SJonathan Doman #include <sdbusplus/utility/dedup_variant.hpp>
30ac6a4445SGunnar Mills #include <utils/collection.hpp>
31ac6a4445SGunnar Mills #include <utils/json_utils.hpp>
32ac6a4445SGunnar Mills 
33ac6a4445SGunnar Mills namespace redfish
34ac6a4445SGunnar Mills {
35ac6a4445SGunnar Mills 
36c951448aSJonathan Doman // Interfaces which imply a D-Bus object represents a Processor
37c951448aSJonathan Doman constexpr std::array<const char*, 2> processorInterfaces = {
38c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Cpu",
39c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Accelerator"};
402bab9831SJonathan Doman 
4171b82f26SSharad Yadav /**
4271b82f26SSharad Yadav  * @brief Fill out uuid info of a processor by
4371b82f26SSharad Yadav  * requesting data from the given D-Bus object.
4471b82f26SSharad Yadav  *
4571b82f26SSharad Yadav  * @param[in,out]   aResp       Async HTTP response.
4671b82f26SSharad Yadav  * @param[in]       service     D-Bus service to query.
4771b82f26SSharad Yadav  * @param[in]       objPath     D-Bus object to query.
4871b82f26SSharad Yadav  */
4971b82f26SSharad Yadav inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
5071b82f26SSharad Yadav                              const std::string& service,
5171b82f26SSharad Yadav                              const std::string& objPath)
5271b82f26SSharad Yadav {
5371b82f26SSharad Yadav     BMCWEB_LOG_DEBUG << "Get Processor UUID";
541e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
551e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
561e1e598dSJonathan Doman         "xyz.openbmc_project.Common.UUID", "UUID",
571e1e598dSJonathan Doman         [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
581e1e598dSJonathan Doman                                            const std::string& property) {
5971b82f26SSharad Yadav             if (ec)
6071b82f26SSharad Yadav             {
6171b82f26SSharad Yadav                 BMCWEB_LOG_DEBUG << "DBUS response error";
6271b82f26SSharad Yadav                 messages::internalError(aResp->res);
6371b82f26SSharad Yadav                 return;
6471b82f26SSharad Yadav             }
651e1e598dSJonathan Doman             aResp->res.jsonValue["UUID"] = property;
661e1e598dSJonathan Doman         });
6771b82f26SSharad Yadav }
6871b82f26SSharad Yadav 
69711ac7a9SEd Tanous inline void getCpuDataByInterface(
70711ac7a9SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
71711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
72ac6a4445SGunnar Mills {
73ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
74ac6a4445SGunnar Mills 
75a1649ec6SChicago Duan     // Set the default value of state
76a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["State"] = "Enabled";
77a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["Health"] = "OK";
78ac6a4445SGunnar Mills 
79ac6a4445SGunnar Mills     for (const auto& interface : cpuInterfacesProperties)
80ac6a4445SGunnar Mills     {
81ac6a4445SGunnar Mills         for (const auto& property : interface.second)
82ac6a4445SGunnar Mills         {
83a1649ec6SChicago Duan             if (property.first == "Present")
84ac6a4445SGunnar Mills             {
85a1649ec6SChicago Duan                 const bool* cpuPresent = std::get_if<bool>(&property.second);
86a1649ec6SChicago Duan                 if (cpuPresent == nullptr)
87ac6a4445SGunnar Mills                 {
88ac6a4445SGunnar Mills                     // Important property not in desired type
89ac6a4445SGunnar Mills                     messages::internalError(aResp->res);
90ac6a4445SGunnar Mills                     return;
91ac6a4445SGunnar Mills                 }
92e05aec50SEd Tanous                 if (!*cpuPresent)
93ac6a4445SGunnar Mills                 {
94a1649ec6SChicago Duan                     // Slot is not populated
95ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
96a1649ec6SChicago Duan                 }
97a1649ec6SChicago Duan             }
98a1649ec6SChicago Duan             else if (property.first == "Functional")
99a1649ec6SChicago Duan             {
100a1649ec6SChicago Duan                 const bool* cpuFunctional = std::get_if<bool>(&property.second);
101a1649ec6SChicago Duan                 if (cpuFunctional == nullptr)
102a1649ec6SChicago Duan                 {
103a1649ec6SChicago Duan                     messages::internalError(aResp->res);
104ac6a4445SGunnar Mills                     return;
105ac6a4445SGunnar Mills                 }
106e05aec50SEd Tanous                 if (!*cpuFunctional)
107a1649ec6SChicago Duan                 {
108a1649ec6SChicago Duan                     aResp->res.jsonValue["Status"]["Health"] = "Critical";
109a1649ec6SChicago Duan                 }
110a1649ec6SChicago Duan             }
111a1649ec6SChicago Duan             else if (property.first == "CoreCount")
112a1649ec6SChicago Duan             {
113a1649ec6SChicago Duan                 const uint16_t* coresCount =
114a1649ec6SChicago Duan                     std::get_if<uint16_t>(&property.second);
115a1649ec6SChicago Duan                 if (coresCount == nullptr)
116a1649ec6SChicago Duan                 {
117a1649ec6SChicago Duan                     messages::internalError(aResp->res);
118a1649ec6SChicago Duan                     return;
119a1649ec6SChicago Duan                 }
120ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = *coresCount;
121ac6a4445SGunnar Mills             }
122dc3fa667SJonathan Doman             else if (property.first == "MaxSpeedInMhz")
123dc3fa667SJonathan Doman             {
124dc3fa667SJonathan Doman                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
125dc3fa667SJonathan Doman                 if (value != nullptr)
126dc3fa667SJonathan Doman                 {
127dc3fa667SJonathan Doman                     aResp->res.jsonValue["MaxSpeedMHz"] = *value;
128dc3fa667SJonathan Doman                 }
129dc3fa667SJonathan Doman             }
130ac6a4445SGunnar Mills             else if (property.first == "Socket")
131ac6a4445SGunnar Mills             {
132ac6a4445SGunnar Mills                 const std::string* value =
133ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
134ac6a4445SGunnar Mills                 if (value != nullptr)
135ac6a4445SGunnar Mills                 {
136ac6a4445SGunnar Mills                     aResp->res.jsonValue["Socket"] = *value;
137ac6a4445SGunnar Mills                 }
138ac6a4445SGunnar Mills             }
139ac6a4445SGunnar Mills             else if (property.first == "ThreadCount")
140ac6a4445SGunnar Mills             {
141dc3fa667SJonathan Doman                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
142ac6a4445SGunnar Mills                 if (value != nullptr)
143ac6a4445SGunnar Mills                 {
144ac6a4445SGunnar Mills                     aResp->res.jsonValue["TotalThreads"] = *value;
145ac6a4445SGunnar Mills                 }
146ac6a4445SGunnar Mills             }
1471930fbd4SBrandon Kim             else if (property.first == "EffectiveFamily")
148ac6a4445SGunnar Mills             {
1491930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
150ac6a4445SGunnar Mills                 if (value != nullptr)
151ac6a4445SGunnar Mills                 {
152ac6a4445SGunnar Mills                     aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
153866e4862SEd Tanous                         "0x" + intToHexString(*value, 4);
154ac6a4445SGunnar Mills                 }
155ac6a4445SGunnar Mills             }
1561930fbd4SBrandon Kim             else if (property.first == "EffectiveModel")
1571930fbd4SBrandon Kim             {
1581930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
1591930fbd4SBrandon Kim                 if (value == nullptr)
1601930fbd4SBrandon Kim                 {
1611930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1621930fbd4SBrandon Kim                     return;
1631930fbd4SBrandon Kim                 }
1641930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
165866e4862SEd Tanous                     "0x" + intToHexString(*value, 4);
1661930fbd4SBrandon Kim             }
167ac6a4445SGunnar Mills             else if (property.first == "Id")
168ac6a4445SGunnar Mills             {
169ac6a4445SGunnar Mills                 const uint64_t* value = std::get_if<uint64_t>(&property.second);
170ac6a4445SGunnar Mills                 if (value != nullptr && *value != 0)
171ac6a4445SGunnar Mills                 {
172ac6a4445SGunnar Mills                     aResp->res
173ac6a4445SGunnar Mills                         .jsonValue["ProcessorId"]["IdentificationRegisters"] =
174866e4862SEd Tanous                         "0x" + intToHexString(*value, 16);
175ac6a4445SGunnar Mills                 }
176ac6a4445SGunnar Mills             }
1771930fbd4SBrandon Kim             else if (property.first == "Microcode")
1781930fbd4SBrandon Kim             {
1791930fbd4SBrandon Kim                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
1801930fbd4SBrandon Kim                 if (value == nullptr)
1811930fbd4SBrandon Kim                 {
1821930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1831930fbd4SBrandon Kim                     return;
1841930fbd4SBrandon Kim                 }
1851930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
186866e4862SEd Tanous                     "0x" + intToHexString(*value, 8);
1871930fbd4SBrandon Kim             }
1881930fbd4SBrandon Kim             else if (property.first == "Step")
1891930fbd4SBrandon Kim             {
1901930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
1911930fbd4SBrandon Kim                 if (value == nullptr)
1921930fbd4SBrandon Kim                 {
1931930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1941930fbd4SBrandon Kim                     return;
1951930fbd4SBrandon Kim                 }
1961930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["Step"] =
197866e4862SEd Tanous                     "0x" + intToHexString(*value, 4);
1981930fbd4SBrandon Kim             }
199ac6a4445SGunnar Mills         }
200ac6a4445SGunnar Mills     }
201ac6a4445SGunnar Mills }
202ac6a4445SGunnar Mills 
2038d1b46d7Szhanghch05 inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
204ac6a4445SGunnar Mills                                 const std::string& cpuId,
205ac6a4445SGunnar Mills                                 const std::string& service,
206ac6a4445SGunnar Mills                                 const std::string& objPath)
207ac6a4445SGunnar Mills {
208ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
209ac6a4445SGunnar Mills 
210ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
211ac6a4445SGunnar Mills         [cpuId, service, objPath, aResp{std::move(aResp)}](
212ac6a4445SGunnar Mills             const boost::system::error_code ec,
213ac6a4445SGunnar Mills             const dbus::utility::ManagedObjectType& dbusData) {
214ac6a4445SGunnar Mills             if (ec)
215ac6a4445SGunnar Mills             {
216ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
217ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
218ac6a4445SGunnar Mills                 return;
219ac6a4445SGunnar Mills             }
220ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = cpuId;
221ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
222ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "CPU";
223ac6a4445SGunnar Mills 
224ac6a4445SGunnar Mills             bool slotPresent = false;
225ac6a4445SGunnar Mills             std::string corePath = objPath + "/core";
226ac6a4445SGunnar Mills             size_t totalCores = 0;
227ac6a4445SGunnar Mills             for (const auto& object : dbusData)
228ac6a4445SGunnar Mills             {
229ac6a4445SGunnar Mills                 if (object.first.str == objPath)
230ac6a4445SGunnar Mills                 {
231ac6a4445SGunnar Mills                     getCpuDataByInterface(aResp, object.second);
232ac6a4445SGunnar Mills                 }
233ac6a4445SGunnar Mills                 else if (boost::starts_with(object.first.str, corePath))
234ac6a4445SGunnar Mills                 {
235ac6a4445SGunnar Mills                     for (const auto& interface : object.second)
236ac6a4445SGunnar Mills                     {
237ac6a4445SGunnar Mills                         if (interface.first ==
238ac6a4445SGunnar Mills                             "xyz.openbmc_project.Inventory.Item")
239ac6a4445SGunnar Mills                         {
240ac6a4445SGunnar Mills                             for (const auto& property : interface.second)
241ac6a4445SGunnar Mills                             {
242ac6a4445SGunnar Mills                                 if (property.first == "Present")
243ac6a4445SGunnar Mills                                 {
244ac6a4445SGunnar Mills                                     const bool* present =
245ac6a4445SGunnar Mills                                         std::get_if<bool>(&property.second);
246ac6a4445SGunnar Mills                                     if (present != nullptr)
247ac6a4445SGunnar Mills                                     {
248e05aec50SEd Tanous                                         if (*present)
249ac6a4445SGunnar Mills                                         {
250ac6a4445SGunnar Mills                                             slotPresent = true;
251ac6a4445SGunnar Mills                                             totalCores++;
252ac6a4445SGunnar Mills                                         }
253ac6a4445SGunnar Mills                                     }
254ac6a4445SGunnar Mills                                 }
255ac6a4445SGunnar Mills                             }
256ac6a4445SGunnar Mills                         }
257ac6a4445SGunnar Mills                     }
258ac6a4445SGunnar Mills                 }
259ac6a4445SGunnar Mills             }
260ac6a4445SGunnar Mills             // In getCpuDataByInterface(), state and health are set
261ac6a4445SGunnar Mills             // based on the present and functional status. If core
262ac6a4445SGunnar Mills             // count is zero, then it has a higher precedence.
263ac6a4445SGunnar Mills             if (slotPresent)
264ac6a4445SGunnar Mills             {
265ac6a4445SGunnar Mills                 if (totalCores == 0)
266ac6a4445SGunnar Mills                 {
267ac6a4445SGunnar Mills                     // Slot is not populated, set status end return
268ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
269ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["Health"] = "OK";
270ac6a4445SGunnar Mills                 }
271ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = totalCores;
272ac6a4445SGunnar Mills             }
273ac6a4445SGunnar Mills             return;
274ac6a4445SGunnar Mills         },
275ac6a4445SGunnar Mills         service, "/xyz/openbmc_project/inventory",
276ac6a4445SGunnar Mills         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
277ac6a4445SGunnar Mills }
278ac6a4445SGunnar Mills 
2798d1b46d7Szhanghch05 inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
280ac6a4445SGunnar Mills                             const std::string& service,
281ac6a4445SGunnar Mills                             const std::string& objPath)
282ac6a4445SGunnar Mills {
283ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
284ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
285ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
286ac6a4445SGunnar Mills             const boost::system::error_code ec,
287ac6a4445SGunnar Mills             const boost::container::flat_map<
288168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
289ac6a4445SGunnar Mills             if (ec)
290ac6a4445SGunnar Mills             {
291ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
292ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
293ac6a4445SGunnar Mills                 return;
294ac6a4445SGunnar Mills             }
295ac6a4445SGunnar Mills 
296ac6a4445SGunnar Mills             for (const auto& property : properties)
297ac6a4445SGunnar Mills             {
298ac6a4445SGunnar Mills                 if (property.first == "SerialNumber")
299ac6a4445SGunnar Mills                 {
300ac6a4445SGunnar Mills                     const std::string* sn =
301ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
302ac6a4445SGunnar Mills                     if (sn != nullptr && !sn->empty())
303ac6a4445SGunnar Mills                     {
304ac6a4445SGunnar Mills                         aResp->res.jsonValue["SerialNumber"] = *sn;
305ac6a4445SGunnar Mills                     }
306ac6a4445SGunnar Mills                 }
307ac6a4445SGunnar Mills                 else if (property.first == "Model")
308ac6a4445SGunnar Mills                 {
309ac6a4445SGunnar Mills                     const std::string* model =
310ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
311ac6a4445SGunnar Mills                     if (model != nullptr && !model->empty())
312ac6a4445SGunnar Mills                     {
313ac6a4445SGunnar Mills                         aResp->res.jsonValue["Model"] = *model;
314ac6a4445SGunnar Mills                     }
315ac6a4445SGunnar Mills                 }
316ac6a4445SGunnar Mills                 else if (property.first == "Manufacturer")
317ac6a4445SGunnar Mills                 {
318ac6a4445SGunnar Mills 
319ac6a4445SGunnar Mills                     const std::string* mfg =
320ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
321ac6a4445SGunnar Mills                     if (mfg != nullptr)
322ac6a4445SGunnar Mills                     {
323ac6a4445SGunnar Mills                         aResp->res.jsonValue["Manufacturer"] = *mfg;
324ac6a4445SGunnar Mills 
325ac6a4445SGunnar Mills                         // Otherwise would be unexpected.
326ac6a4445SGunnar Mills                         if (mfg->find("Intel") != std::string::npos)
327ac6a4445SGunnar Mills                         {
328ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
329ac6a4445SGunnar Mills                                 "x86";
330ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "x86-64";
331ac6a4445SGunnar Mills                         }
332ac6a4445SGunnar Mills                         else if (mfg->find("IBM") != std::string::npos)
333ac6a4445SGunnar Mills                         {
334ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
335ac6a4445SGunnar Mills                                 "Power";
336ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "PowerISA";
337ac6a4445SGunnar Mills                         }
338ac6a4445SGunnar Mills                     }
339ac6a4445SGunnar Mills                 }
340cba4f448SSunnySrivastava1984                 else if (property.first == "PartNumber")
341cba4f448SSunnySrivastava1984                 {
342cba4f448SSunnySrivastava1984                     const std::string* partNumber =
343cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
344cba4f448SSunnySrivastava1984 
345cba4f448SSunnySrivastava1984                     if (partNumber == nullptr)
346cba4f448SSunnySrivastava1984                     {
347cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
348cba4f448SSunnySrivastava1984                         return;
349cba4f448SSunnySrivastava1984                     }
350cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["PartNumber"] = *partNumber;
351cba4f448SSunnySrivastava1984                 }
352cba4f448SSunnySrivastava1984                 else if (property.first == "SparePartNumber")
353cba4f448SSunnySrivastava1984                 {
354cba4f448SSunnySrivastava1984                     const std::string* sparePartNumber =
355cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
356cba4f448SSunnySrivastava1984 
357cba4f448SSunnySrivastava1984                     if (sparePartNumber == nullptr)
358cba4f448SSunnySrivastava1984                     {
359cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
360cba4f448SSunnySrivastava1984                         return;
361cba4f448SSunnySrivastava1984                     }
362cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
363cba4f448SSunnySrivastava1984                 }
364ac6a4445SGunnar Mills             }
365ac6a4445SGunnar Mills         },
366ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
367ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Asset");
368ac6a4445SGunnar Mills }
369ac6a4445SGunnar Mills 
3708d1b46d7Szhanghch05 inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
371ac6a4445SGunnar Mills                                const std::string& service,
372ac6a4445SGunnar Mills                                const std::string& objPath)
373ac6a4445SGunnar Mills {
374ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
375ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
376ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
377ac6a4445SGunnar Mills             const boost::system::error_code ec,
378ac6a4445SGunnar Mills             const boost::container::flat_map<
379168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
380ac6a4445SGunnar Mills             if (ec)
381ac6a4445SGunnar Mills             {
382ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
383ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
384ac6a4445SGunnar Mills                 return;
385ac6a4445SGunnar Mills             }
386ac6a4445SGunnar Mills 
387ac6a4445SGunnar Mills             for (const auto& property : properties)
388ac6a4445SGunnar Mills             {
389ac6a4445SGunnar Mills                 if (property.first == "Version")
390ac6a4445SGunnar Mills                 {
391ac6a4445SGunnar Mills                     const std::string* ver =
392ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
393ac6a4445SGunnar Mills                     if (ver != nullptr)
394ac6a4445SGunnar Mills                     {
395ac6a4445SGunnar Mills                         aResp->res.jsonValue["Version"] = *ver;
396ac6a4445SGunnar Mills                     }
397ac6a4445SGunnar Mills                     break;
398ac6a4445SGunnar Mills                 }
399ac6a4445SGunnar Mills             }
400ac6a4445SGunnar Mills         },
401ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
402ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Revision");
403ac6a4445SGunnar Mills }
404ac6a4445SGunnar Mills 
4058d1b46d7Szhanghch05 inline void getAcceleratorDataByService(
4068d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
4078d1b46d7Szhanghch05     const std::string& service, const std::string& objPath)
408ac6a4445SGunnar Mills {
409ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG
410ac6a4445SGunnar Mills         << "Get available system Accelerator resources by service.";
411ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
412ac6a4445SGunnar Mills         [acclrtrId, aResp{std::move(aResp)}](
413ac6a4445SGunnar Mills             const boost::system::error_code ec,
414ac6a4445SGunnar Mills             const boost::container::flat_map<
415168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
416ac6a4445SGunnar Mills             if (ec)
417ac6a4445SGunnar Mills             {
418ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
419ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
420ac6a4445SGunnar Mills                 return;
421ac6a4445SGunnar Mills             }
422ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = acclrtrId;
423ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
424ac6a4445SGunnar Mills             const bool* accPresent = nullptr;
425ac6a4445SGunnar Mills             const bool* accFunctional = nullptr;
426ac6a4445SGunnar Mills 
427ac6a4445SGunnar Mills             for (const auto& property : properties)
428ac6a4445SGunnar Mills             {
429ac6a4445SGunnar Mills                 if (property.first == "Functional")
430ac6a4445SGunnar Mills                 {
431ac6a4445SGunnar Mills                     accFunctional = std::get_if<bool>(&property.second);
432ac6a4445SGunnar Mills                 }
433ac6a4445SGunnar Mills                 else if (property.first == "Present")
434ac6a4445SGunnar Mills                 {
435ac6a4445SGunnar Mills                     accPresent = std::get_if<bool>(&property.second);
436ac6a4445SGunnar Mills                 }
437ac6a4445SGunnar Mills             }
438ac6a4445SGunnar Mills 
439ac6a4445SGunnar Mills             std::string state = "Enabled";
440ac6a4445SGunnar Mills             std::string health = "OK";
441ac6a4445SGunnar Mills 
442e05aec50SEd Tanous             if (accPresent != nullptr && !*accPresent)
443ac6a4445SGunnar Mills             {
444ac6a4445SGunnar Mills                 state = "Absent";
445ac6a4445SGunnar Mills             }
446ac6a4445SGunnar Mills 
447e05aec50SEd Tanous             if ((accFunctional != nullptr) && !*accFunctional)
448ac6a4445SGunnar Mills             {
449ac6a4445SGunnar Mills                 if (state == "Enabled")
450ac6a4445SGunnar Mills                 {
451ac6a4445SGunnar Mills                     health = "Critical";
452ac6a4445SGunnar Mills                 }
453ac6a4445SGunnar Mills             }
454ac6a4445SGunnar Mills 
455ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = state;
456ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = health;
457ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "Accelerator";
458ac6a4445SGunnar Mills         },
459ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
460ac6a4445SGunnar Mills }
461ac6a4445SGunnar Mills 
462dba0c291SJonathan Doman // OperatingConfig D-Bus Types
463dba0c291SJonathan Doman using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
464dba0c291SJonathan Doman using BaseSpeedPrioritySettingsProperty =
465dba0c291SJonathan Doman     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
466dba0c291SJonathan Doman // uint32_t and size_t may or may not be the same type, requiring a dedup'd
467dba0c291SJonathan Doman // variant
468168e20c1SEd Tanous using OperatingConfigProperties =
469168e20c1SEd Tanous     std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
470dba0c291SJonathan Doman 
471dba0c291SJonathan Doman /**
472dba0c291SJonathan Doman  * Fill out the HighSpeedCoreIDs in a Processor resource from the given
473dba0c291SJonathan Doman  * OperatingConfig D-Bus property.
474dba0c291SJonathan Doman  *
475dba0c291SJonathan Doman  * @param[in,out]   aResp               Async HTTP response.
476dba0c291SJonathan Doman  * @param[in]       baseSpeedSettings   Full list of base speed priority groups,
477dba0c291SJonathan Doman  *                                      to use to determine the list of high
478dba0c291SJonathan Doman  *                                      speed cores.
479dba0c291SJonathan Doman  */
480dba0c291SJonathan Doman inline void highSpeedCoreIdsHandler(
4818d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
482dba0c291SJonathan Doman     const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
483dba0c291SJonathan Doman {
484dba0c291SJonathan Doman     // The D-Bus property does not indicate which bucket is the "high
485dba0c291SJonathan Doman     // priority" group, so let's discern that by looking for the one with
486dba0c291SJonathan Doman     // highest base frequency.
487dba0c291SJonathan Doman     auto highPriorityGroup = baseSpeedSettings.cend();
488dba0c291SJonathan Doman     uint32_t highestBaseSpeed = 0;
489dba0c291SJonathan Doman     for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
490dba0c291SJonathan Doman          ++it)
491dba0c291SJonathan Doman     {
492dba0c291SJonathan Doman         const uint32_t baseFreq = std::get<uint32_t>(*it);
493dba0c291SJonathan Doman         if (baseFreq > highestBaseSpeed)
494dba0c291SJonathan Doman         {
495dba0c291SJonathan Doman             highestBaseSpeed = baseFreq;
496dba0c291SJonathan Doman             highPriorityGroup = it;
497dba0c291SJonathan Doman         }
498dba0c291SJonathan Doman     }
499dba0c291SJonathan Doman 
500dba0c291SJonathan Doman     nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
501dba0c291SJonathan Doman     jsonCoreIds = nlohmann::json::array();
502dba0c291SJonathan Doman 
503dba0c291SJonathan Doman     // There may not be any entries in the D-Bus property, so only populate
504dba0c291SJonathan Doman     // if there was actually something there.
505dba0c291SJonathan Doman     if (highPriorityGroup != baseSpeedSettings.cend())
506dba0c291SJonathan Doman     {
507dba0c291SJonathan Doman         jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
508dba0c291SJonathan Doman     }
509dba0c291SJonathan Doman }
510dba0c291SJonathan Doman 
511dba0c291SJonathan Doman /**
512dba0c291SJonathan Doman  * Fill out OperatingConfig related items in a Processor resource by requesting
513dba0c291SJonathan Doman  * data from the given D-Bus object.
514dba0c291SJonathan Doman  *
515dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
516dba0c291SJonathan Doman  * @param[in]       cpuId       CPU D-Bus name.
517dba0c291SJonathan Doman  * @param[in]       service     D-Bus service to query.
518dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
519dba0c291SJonathan Doman  */
5208d1b46d7Szhanghch05 inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
521dba0c291SJonathan Doman                              const std::string& cpuId,
522dba0c291SJonathan Doman                              const std::string& service,
523dba0c291SJonathan Doman                              const std::string& objPath)
524dba0c291SJonathan Doman {
525dba0c291SJonathan Doman     BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
526dba0c291SJonathan Doman 
527dba0c291SJonathan Doman     // First, GetAll CurrentOperatingConfig properties on the object
528dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
529dba0c291SJonathan Doman         [aResp, cpuId, service](
530dba0c291SJonathan Doman             const boost::system::error_code ec,
531168e20c1SEd Tanous             const std::vector<std::pair<
532168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>>& properties) {
533dba0c291SJonathan Doman             if (ec)
534dba0c291SJonathan Doman             {
535dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
536dba0c291SJonathan Doman                                    << ec.message();
537dba0c291SJonathan Doman                 messages::internalError(aResp->res);
538dba0c291SJonathan Doman                 return;
539dba0c291SJonathan Doman             }
540dba0c291SJonathan Doman 
541dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
542dba0c291SJonathan Doman 
543dba0c291SJonathan Doman             for (const auto& [dbusPropName, variantVal] : properties)
544dba0c291SJonathan Doman             {
545dba0c291SJonathan Doman                 if (dbusPropName == "AppliedConfig")
546dba0c291SJonathan Doman                 {
547dba0c291SJonathan Doman                     const sdbusplus::message::object_path* dbusPathWrapper =
548dba0c291SJonathan Doman                         std::get_if<sdbusplus::message::object_path>(
549dba0c291SJonathan Doman                             &variantVal);
550dba0c291SJonathan Doman                     if (dbusPathWrapper == nullptr)
551dba0c291SJonathan Doman                     {
552dba0c291SJonathan Doman                         continue;
553dba0c291SJonathan Doman                     }
554dba0c291SJonathan Doman 
555dba0c291SJonathan Doman                     const std::string& dbusPath = dbusPathWrapper->str;
556dba0c291SJonathan Doman                     std::string uri = "/redfish/v1/Systems/system/Processors/" +
557dba0c291SJonathan Doman                                       cpuId + "/OperatingConfigs";
558*1476687dSEd Tanous                     nlohmann::json::object_t operatingConfig;
559*1476687dSEd Tanous                     operatingConfig["@odata.id"] = uri;
560*1476687dSEd Tanous                     json["OperatingConfigs"] = std::move(operatingConfig);
561dba0c291SJonathan Doman 
562dba0c291SJonathan Doman                     // Reuse the D-Bus config object name for the Redfish
563dba0c291SJonathan Doman                     // URI
564dba0c291SJonathan Doman                     size_t baseNamePos = dbusPath.rfind('/');
565dba0c291SJonathan Doman                     if (baseNamePos == std::string::npos ||
566dba0c291SJonathan Doman                         baseNamePos == (dbusPath.size() - 1))
567dba0c291SJonathan Doman                     {
568dba0c291SJonathan Doman                         // If the AppliedConfig was somehow not a valid path,
569dba0c291SJonathan Doman                         // skip adding any more properties, since everything
570dba0c291SJonathan Doman                         // else is tied to this applied config.
571dba0c291SJonathan Doman                         messages::internalError(aResp->res);
572dba0c291SJonathan Doman                         break;
573dba0c291SJonathan Doman                     }
574dba0c291SJonathan Doman                     uri += '/';
575dba0c291SJonathan Doman                     uri += dbusPath.substr(baseNamePos + 1);
576*1476687dSEd Tanous                     nlohmann::json::object_t appliedOperatingConfig;
577*1476687dSEd Tanous                     appliedOperatingConfig["@odata.id"] = uri;
578*1476687dSEd Tanous                     json["AppliedOperatingConfig"] =
579*1476687dSEd Tanous                         std::move(appliedOperatingConfig);
580dba0c291SJonathan Doman 
581dba0c291SJonathan Doman                     // Once we found the current applied config, queue another
582dba0c291SJonathan Doman                     // request to read the base freq core ids out of that
583dba0c291SJonathan Doman                     // config.
5841e1e598dSJonathan Doman                     sdbusplus::asio::getProperty<
5851e1e598dSJonathan Doman                         BaseSpeedPrioritySettingsProperty>(
5861e1e598dSJonathan Doman                         *crow::connections::systemBus, service, dbusPath,
5871e1e598dSJonathan Doman                         "xyz.openbmc_project.Inventory.Item.Cpu."
5881e1e598dSJonathan Doman                         "OperatingConfig",
5891e1e598dSJonathan Doman                         "BaseSpeedPrioritySettings",
5901e1e598dSJonathan Doman                         [aResp](const boost::system::error_code ec,
5911e1e598dSJonathan Doman                                 const BaseSpeedPrioritySettingsProperty&
5921e1e598dSJonathan Doman                                     baseSpeedList) {
593dba0c291SJonathan Doman                             if (ec)
594dba0c291SJonathan Doman                             {
595dba0c291SJonathan Doman                                 BMCWEB_LOG_WARNING
596dba0c291SJonathan Doman                                     << "D-Bus Property Get error: " << ec;
597dba0c291SJonathan Doman                                 messages::internalError(aResp->res);
598dba0c291SJonathan Doman                                 return;
599dba0c291SJonathan Doman                             }
6001e1e598dSJonathan Doman 
6011e1e598dSJonathan Doman                             highSpeedCoreIdsHandler(aResp, baseSpeedList);
6021e1e598dSJonathan Doman                         });
603dba0c291SJonathan Doman                 }
604dba0c291SJonathan Doman                 else if (dbusPropName == "BaseSpeedPriorityEnabled")
605dba0c291SJonathan Doman                 {
606dba0c291SJonathan Doman                     const bool* state = std::get_if<bool>(&variantVal);
607dba0c291SJonathan Doman                     if (state != nullptr)
608dba0c291SJonathan Doman                     {
609dba0c291SJonathan Doman                         json["BaseSpeedPriorityState"] =
610dba0c291SJonathan Doman                             *state ? "Enabled" : "Disabled";
611dba0c291SJonathan Doman                     }
612dba0c291SJonathan Doman                 }
613dba0c291SJonathan Doman             }
614dba0c291SJonathan Doman         },
615dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
616dba0c291SJonathan Doman         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig");
617dba0c291SJonathan Doman }
618dba0c291SJonathan Doman 
619cba4f448SSunnySrivastava1984 /**
620cba4f448SSunnySrivastava1984  * @brief Fill out location info of a processor by
621cba4f448SSunnySrivastava1984  * requesting data from the given D-Bus object.
622cba4f448SSunnySrivastava1984  *
623cba4f448SSunnySrivastava1984  * @param[in,out]   aResp       Async HTTP response.
624cba4f448SSunnySrivastava1984  * @param[in]       service     D-Bus service to query.
625cba4f448SSunnySrivastava1984  * @param[in]       objPath     D-Bus object to query.
626cba4f448SSunnySrivastava1984  */
6278d1b46d7Szhanghch05 inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
628cba4f448SSunnySrivastava1984                                const std::string& service,
629cba4f448SSunnySrivastava1984                                const std::string& objPath)
630cba4f448SSunnySrivastava1984 {
631cba4f448SSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
6321e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
6331e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
6341e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
6351e1e598dSJonathan Doman         [objPath, aResp{std::move(aResp)}](const boost::system::error_code ec,
6361e1e598dSJonathan Doman                                            const std::string& property) {
637cba4f448SSunnySrivastava1984             if (ec)
638cba4f448SSunnySrivastava1984             {
639cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error";
640cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
641cba4f448SSunnySrivastava1984                 return;
642cba4f448SSunnySrivastava1984             }
643cba4f448SSunnySrivastava1984 
644cba4f448SSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
6451e1e598dSJonathan Doman                 property;
6461e1e598dSJonathan Doman         });
647cba4f448SSunnySrivastava1984 }
648cba4f448SSunnySrivastava1984 
649c951448aSJonathan Doman /**
65049e429caSJonathan Doman  * Populate the unique identifier in a Processor resource by requesting data
65149e429caSJonathan Doman  * from the given D-Bus object.
65249e429caSJonathan Doman  *
65349e429caSJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
65449e429caSJonathan Doman  * @param[in]       service     D-Bus service to query.
65549e429caSJonathan Doman  * @param[in]       objPath     D-Bus object to query.
65649e429caSJonathan Doman  */
65749e429caSJonathan Doman inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
65849e429caSJonathan Doman                            const std::string& service,
65949e429caSJonathan Doman                            const std::string& objectPath)
66049e429caSJonathan Doman {
66149e429caSJonathan Doman     BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
6621e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
6631e1e598dSJonathan Doman         *crow::connections::systemBus, service, objectPath,
6641e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
6651e1e598dSJonathan Doman         "UniqueIdentifier",
6661e1e598dSJonathan Doman         [aResp](boost::system::error_code ec, const std::string& id) {
6671e1e598dSJonathan Doman             if (ec)
66849e429caSJonathan Doman             {
66949e429caSJonathan Doman                 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
67049e429caSJonathan Doman                 messages::internalError(aResp->res);
67149e429caSJonathan Doman                 return;
67249e429caSJonathan Doman             }
67349e429caSJonathan Doman             aResp->res
6741e1e598dSJonathan Doman                 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] = id;
6751e1e598dSJonathan Doman         });
67649e429caSJonathan Doman }
67749e429caSJonathan Doman 
67849e429caSJonathan Doman /**
679c951448aSJonathan Doman  * Find the D-Bus object representing the requested Processor, and call the
680c951448aSJonathan Doman  * handler with the results. If matching object is not found, add 404 error to
681c951448aSJonathan Doman  * response and don't call the handler.
682c951448aSJonathan Doman  *
683c951448aSJonathan Doman  * @param[in,out]   resp            Async HTTP response.
684c951448aSJonathan Doman  * @param[in]       processorId     Redfish Processor Id.
685c951448aSJonathan Doman  * @param[in]       handler         Callback to continue processing request upon
686c951448aSJonathan Doman  *                                  successfully finding object.
687c951448aSJonathan Doman  */
688c951448aSJonathan Doman template <typename Handler>
6898d1b46d7Szhanghch05 inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
690c951448aSJonathan Doman                                const std::string& processorId,
691c951448aSJonathan Doman                                Handler&& handler)
692ac6a4445SGunnar Mills {
693ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system processor resources.";
694ac6a4445SGunnar Mills 
695c951448aSJonathan Doman     // GetSubTree on all interfaces which provide info about a Processor
696ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
697c951448aSJonathan Doman         [resp, processorId, handler = std::forward<Handler>(handler)](
698c951448aSJonathan Doman             boost::system::error_code ec,
6995df6eda2SShantappa Teekappanavar             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
700ac6a4445SGunnar Mills             if (ec)
701ac6a4445SGunnar Mills             {
702c951448aSJonathan Doman                 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
703c951448aSJonathan Doman                 messages::internalError(resp->res);
704ac6a4445SGunnar Mills                 return;
705ac6a4445SGunnar Mills             }
7062bab9831SJonathan Doman             for (const auto& [objectPath, serviceMap] : subtree)
707ac6a4445SGunnar Mills             {
7082bab9831SJonathan Doman                 // Ignore any objects which don't end with our desired cpu name
7092bab9831SJonathan Doman                 if (!boost::ends_with(objectPath, processorId))
710ac6a4445SGunnar Mills                 {
7112bab9831SJonathan Doman                     continue;
712ac6a4445SGunnar Mills                 }
7132bab9831SJonathan Doman 
714c951448aSJonathan Doman                 bool found = false;
715c951448aSJonathan Doman                 // Filter out objects that don't have the CPU-specific
716c951448aSJonathan Doman                 // interfaces to make sure we can return 404 on non-CPUs
717c951448aSJonathan Doman                 // (e.g. /redfish/../Processors/dimm0)
7182bab9831SJonathan Doman                 for (const auto& [serviceName, interfaceList] : serviceMap)
719ac6a4445SGunnar Mills                 {
720c951448aSJonathan Doman                     if (std::find_first_of(
721c951448aSJonathan Doman                             interfaceList.begin(), interfaceList.end(),
722c951448aSJonathan Doman                             processorInterfaces.begin(),
723c951448aSJonathan Doman                             processorInterfaces.end()) != interfaceList.end())
7242bab9831SJonathan Doman                     {
725c951448aSJonathan Doman                         found = true;
726c951448aSJonathan Doman                         break;
727c951448aSJonathan Doman                     }
728c951448aSJonathan Doman                 }
729c951448aSJonathan Doman 
730c951448aSJonathan Doman                 if (!found)
7312bab9831SJonathan Doman                 {
732c951448aSJonathan Doman                     continue;
733ac6a4445SGunnar Mills                 }
734c951448aSJonathan Doman 
735c951448aSJonathan Doman                 // Process the first object which does match our cpu name and
736c951448aSJonathan Doman                 // required interfaces, and potentially ignore any other
737c951448aSJonathan Doman                 // matching objects. Assume all interfaces we want to process
738c951448aSJonathan Doman                 // must be on the same object path.
739c951448aSJonathan Doman 
740c951448aSJonathan Doman                 handler(resp, processorId, objectPath, serviceMap);
741ac6a4445SGunnar Mills                 return;
742ac6a4445SGunnar Mills             }
743c951448aSJonathan Doman             messages::resourceNotFound(resp->res, "Processor", processorId);
744ac6a4445SGunnar Mills         },
745ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper",
746ac6a4445SGunnar Mills         "/xyz/openbmc_project/object_mapper",
747ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7482bab9831SJonathan Doman         "/xyz/openbmc_project/inventory", 0,
74949e429caSJonathan Doman         std::array<const char*, 8>{
75071b82f26SSharad Yadav             "xyz.openbmc_project.Common.UUID",
7512bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Asset",
7522bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Revision",
7532bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Cpu",
754cba4f448SSunnySrivastava1984             "xyz.openbmc_project.Inventory.Decorator.LocationCode",
755dba0c291SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Accelerator",
75649e429caSJonathan Doman             "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
75749e429caSJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"});
758ac6a4445SGunnar Mills }
759ac6a4445SGunnar Mills 
7608d1b46d7Szhanghch05 inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
761c951448aSJonathan Doman                              const std::string& processorId,
762c951448aSJonathan Doman                              const std::string& objectPath,
7635df6eda2SShantappa Teekappanavar                              const dbus::utility::MapperServiceMap& serviceMap)
764c951448aSJonathan Doman {
765c951448aSJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
766c951448aSJonathan Doman     {
767c951448aSJonathan Doman         for (const auto& interface : interfaceList)
768c951448aSJonathan Doman         {
769c951448aSJonathan Doman             if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
770c951448aSJonathan Doman             {
771c951448aSJonathan Doman                 getCpuAssetData(aResp, serviceName, objectPath);
772c951448aSJonathan Doman             }
7730fda0f12SGeorge Liu             else if (interface ==
7740fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.Revision")
775c951448aSJonathan Doman             {
776c951448aSJonathan Doman                 getCpuRevisionData(aResp, serviceName, objectPath);
777c951448aSJonathan Doman             }
778c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
779c951448aSJonathan Doman             {
780c951448aSJonathan Doman                 getCpuDataByService(aResp, processorId, serviceName,
781c951448aSJonathan Doman                                     objectPath);
782c951448aSJonathan Doman             }
7830fda0f12SGeorge Liu             else if (interface ==
7840fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Item.Accelerator")
785c951448aSJonathan Doman             {
786c951448aSJonathan Doman                 getAcceleratorDataByService(aResp, processorId, serviceName,
787c951448aSJonathan Doman                                             objectPath);
788c951448aSJonathan Doman             }
7890fda0f12SGeorge Liu             else if (
7900fda0f12SGeorge Liu                 interface ==
7910fda0f12SGeorge Liu                 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
792c951448aSJonathan Doman             {
793c951448aSJonathan Doman                 getCpuConfigData(aResp, processorId, serviceName, objectPath);
794c951448aSJonathan Doman             }
7950fda0f12SGeorge Liu             else if (interface ==
7960fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.LocationCode")
797c951448aSJonathan Doman             {
798c951448aSJonathan Doman                 getCpuLocationCode(aResp, serviceName, objectPath);
799c951448aSJonathan Doman             }
80071b82f26SSharad Yadav             else if (interface == "xyz.openbmc_project.Common.UUID")
80171b82f26SSharad Yadav             {
80271b82f26SSharad Yadav                 getProcessorUUID(aResp, serviceName, objectPath);
80371b82f26SSharad Yadav             }
8040fda0f12SGeorge Liu             else if (interface ==
8050fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
80649e429caSJonathan Doman             {
80749e429caSJonathan Doman                 getCpuUniqueId(aResp, serviceName, objectPath);
80849e429caSJonathan Doman             }
809c951448aSJonathan Doman         }
810c951448aSJonathan Doman     }
811c951448aSJonathan Doman }
812c951448aSJonathan Doman 
813dba0c291SJonathan Doman /**
814dba0c291SJonathan Doman  * Request all the properties for the given D-Bus object and fill out the
815dba0c291SJonathan Doman  * related entries in the Redfish OperatingConfig response.
816dba0c291SJonathan Doman  *
817dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
818dba0c291SJonathan Doman  * @param[in]       service     D-Bus service name to query.
819dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
820dba0c291SJonathan Doman  */
8218d1b46d7Szhanghch05 inline void
8228d1b46d7Szhanghch05     getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
823dba0c291SJonathan Doman                            const std::string& service,
824dba0c291SJonathan Doman                            const std::string& objPath)
825dba0c291SJonathan Doman {
826dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
827914e2d5dSEd Tanous         [aResp](const boost::system::error_code ec,
828dba0c291SJonathan Doman                 const OperatingConfigProperties& properties) {
829dba0c291SJonathan Doman             if (ec)
830dba0c291SJonathan Doman             {
831dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
832dba0c291SJonathan Doman                                    << ec.message();
833dba0c291SJonathan Doman                 messages::internalError(aResp->res);
834dba0c291SJonathan Doman                 return;
835dba0c291SJonathan Doman             }
836dba0c291SJonathan Doman 
837dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
838dba0c291SJonathan Doman             for (const auto& [key, variant] : properties)
839dba0c291SJonathan Doman             {
840dba0c291SJonathan Doman                 if (key == "AvailableCoreCount")
841dba0c291SJonathan Doman                 {
842dba0c291SJonathan Doman                     const size_t* cores = std::get_if<size_t>(&variant);
843dba0c291SJonathan Doman                     if (cores != nullptr)
844dba0c291SJonathan Doman                     {
845dba0c291SJonathan Doman                         json["TotalAvailableCoreCount"] = *cores;
846dba0c291SJonathan Doman                     }
847dba0c291SJonathan Doman                 }
848dba0c291SJonathan Doman                 else if (key == "BaseSpeed")
849dba0c291SJonathan Doman                 {
850dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
851dba0c291SJonathan Doman                     if (speed != nullptr)
852dba0c291SJonathan Doman                     {
853dba0c291SJonathan Doman                         json["BaseSpeedMHz"] = *speed;
854dba0c291SJonathan Doman                     }
855dba0c291SJonathan Doman                 }
856dba0c291SJonathan Doman                 else if (key == "MaxJunctionTemperature")
857dba0c291SJonathan Doman                 {
858dba0c291SJonathan Doman                     const uint32_t* temp = std::get_if<uint32_t>(&variant);
859dba0c291SJonathan Doman                     if (temp != nullptr)
860dba0c291SJonathan Doman                     {
861dba0c291SJonathan Doman                         json["MaxJunctionTemperatureCelsius"] = *temp;
862dba0c291SJonathan Doman                     }
863dba0c291SJonathan Doman                 }
864dba0c291SJonathan Doman                 else if (key == "MaxSpeed")
865dba0c291SJonathan Doman                 {
866dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
867dba0c291SJonathan Doman                     if (speed != nullptr)
868dba0c291SJonathan Doman                     {
869dba0c291SJonathan Doman                         json["MaxSpeedMHz"] = *speed;
870dba0c291SJonathan Doman                     }
871dba0c291SJonathan Doman                 }
872dba0c291SJonathan Doman                 else if (key == "PowerLimit")
873dba0c291SJonathan Doman                 {
874dba0c291SJonathan Doman                     const uint32_t* tdp = std::get_if<uint32_t>(&variant);
875dba0c291SJonathan Doman                     if (tdp != nullptr)
876dba0c291SJonathan Doman                     {
877dba0c291SJonathan Doman                         json["TDPWatts"] = *tdp;
878dba0c291SJonathan Doman                     }
879dba0c291SJonathan Doman                 }
880dba0c291SJonathan Doman                 else if (key == "TurboProfile")
881dba0c291SJonathan Doman                 {
882dba0c291SJonathan Doman                     const auto* turboList =
883dba0c291SJonathan Doman                         std::get_if<TurboProfileProperty>(&variant);
884dba0c291SJonathan Doman                     if (turboList == nullptr)
885dba0c291SJonathan Doman                     {
886dba0c291SJonathan Doman                         continue;
887dba0c291SJonathan Doman                     }
888dba0c291SJonathan Doman 
889dba0c291SJonathan Doman                     nlohmann::json& turboArray = json["TurboProfile"];
890dba0c291SJonathan Doman                     turboArray = nlohmann::json::array();
891dba0c291SJonathan Doman                     for (const auto& [turboSpeed, coreCount] : *turboList)
892dba0c291SJonathan Doman                     {
893*1476687dSEd Tanous                         nlohmann::json::object_t turbo;
894*1476687dSEd Tanous                         turbo["ActiveCoreCount"] = coreCount;
895*1476687dSEd Tanous                         turbo["MaxSpeedMHz"] = turboSpeed;
896*1476687dSEd Tanous                         turboArray.push_back(std::move(turbo));
897dba0c291SJonathan Doman                     }
898dba0c291SJonathan Doman                 }
899dba0c291SJonathan Doman                 else if (key == "BaseSpeedPrioritySettings")
900dba0c291SJonathan Doman                 {
901dba0c291SJonathan Doman                     const auto* baseSpeedList =
902dba0c291SJonathan Doman                         std::get_if<BaseSpeedPrioritySettingsProperty>(
903dba0c291SJonathan Doman                             &variant);
904dba0c291SJonathan Doman                     if (baseSpeedList == nullptr)
905dba0c291SJonathan Doman                     {
906dba0c291SJonathan Doman                         continue;
907dba0c291SJonathan Doman                     }
908dba0c291SJonathan Doman 
909dba0c291SJonathan Doman                     nlohmann::json& baseSpeedArray =
910dba0c291SJonathan Doman                         json["BaseSpeedPrioritySettings"];
911dba0c291SJonathan Doman                     baseSpeedArray = nlohmann::json::array();
912dba0c291SJonathan Doman                     for (const auto& [baseSpeed, coreList] : *baseSpeedList)
913dba0c291SJonathan Doman                     {
914*1476687dSEd Tanous                         nlohmann::json::object_t speed;
915*1476687dSEd Tanous                         speed["CoreCount"] = coreList.size();
916*1476687dSEd Tanous                         speed["CoreIDs"] = coreList;
917*1476687dSEd Tanous                         speed["BaseSpeedMHz"] = baseSpeed;
918*1476687dSEd Tanous                         baseSpeedArray.push_back(std::move(speed));
919dba0c291SJonathan Doman                     }
920dba0c291SJonathan Doman                 }
921dba0c291SJonathan Doman             }
922dba0c291SJonathan Doman         },
923dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
924dba0c291SJonathan Doman         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
925dba0c291SJonathan Doman }
926dba0c291SJonathan Doman 
9273cde86f1SJonathan Doman /**
9283cde86f1SJonathan Doman  * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
9293cde86f1SJonathan Doman  * property. Main task is to translate error messages into Redfish errors.
9303cde86f1SJonathan Doman  *
9313cde86f1SJonathan Doman  * @param[in,out]   resp    HTTP response.
9323cde86f1SJonathan Doman  * @param[in]       setPropVal  Value which we attempted to set.
9333cde86f1SJonathan Doman  * @param[in]       ec      D-Bus response error code.
9343cde86f1SJonathan Doman  * @param[in]       msg     D-Bus response message.
9353cde86f1SJonathan Doman  */
9363cde86f1SJonathan Doman inline void
9373cde86f1SJonathan Doman     handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
9383cde86f1SJonathan Doman                                 const std::string& setPropVal,
9393cde86f1SJonathan Doman                                 boost::system::error_code ec,
9403cde86f1SJonathan Doman                                 const sdbusplus::message::message& msg)
9413cde86f1SJonathan Doman {
9423cde86f1SJonathan Doman     if (!ec)
9433cde86f1SJonathan Doman     {
9443cde86f1SJonathan Doman         BMCWEB_LOG_DEBUG << "Set Property succeeded";
9453cde86f1SJonathan Doman         return;
9463cde86f1SJonathan Doman     }
9473cde86f1SJonathan Doman 
9483cde86f1SJonathan Doman     BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
9493cde86f1SJonathan Doman 
9503cde86f1SJonathan Doman     const sd_bus_error* dbusError = msg.get_error();
9513cde86f1SJonathan Doman     if (dbusError == nullptr)
9523cde86f1SJonathan Doman     {
9533cde86f1SJonathan Doman         messages::internalError(resp->res);
9543cde86f1SJonathan Doman         return;
9553cde86f1SJonathan Doman     }
9563cde86f1SJonathan Doman 
9573cde86f1SJonathan Doman     // The asio error code doesn't know about our custom errors, so we have to
9583cde86f1SJonathan Doman     // parse the error string. Some of these D-Bus -> Redfish translations are a
9593cde86f1SJonathan Doman     // stretch, but it's good to try to communicate something vaguely useful.
9603cde86f1SJonathan Doman     if (strcmp(dbusError->name,
9613cde86f1SJonathan Doman                "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
9623cde86f1SJonathan Doman     {
9633cde86f1SJonathan Doman         // Service did not like the object_path we tried to set.
9643cde86f1SJonathan Doman         messages::propertyValueIncorrect(
9653cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
9663cde86f1SJonathan Doman     }
9673cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9683cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
9693cde86f1SJonathan Doman     {
9703cde86f1SJonathan Doman         // Service indicates we can never change the config for this processor.
9713cde86f1SJonathan Doman         messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
9723cde86f1SJonathan Doman     }
9733cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9743cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.Unavailable") == 0)
9753cde86f1SJonathan Doman     {
9763cde86f1SJonathan Doman         // Service indicates the config cannot be changed right now, but maybe
9773cde86f1SJonathan Doman         // in a different system state.
9783cde86f1SJonathan Doman         messages::resourceInStandby(resp->res);
9793cde86f1SJonathan Doman     }
9803cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9813cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Device.Error.WriteFailure") ==
9823cde86f1SJonathan Doman              0)
9833cde86f1SJonathan Doman     {
9843cde86f1SJonathan Doman         // Service tried to change the config, but it failed.
9853cde86f1SJonathan Doman         messages::operationFailed(resp->res);
9863cde86f1SJonathan Doman     }
9873cde86f1SJonathan Doman     else
9883cde86f1SJonathan Doman     {
9893cde86f1SJonathan Doman         messages::internalError(resp->res);
9903cde86f1SJonathan Doman     }
9913cde86f1SJonathan Doman }
9923cde86f1SJonathan Doman 
9933cde86f1SJonathan Doman /**
9943cde86f1SJonathan Doman  * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
9953cde86f1SJonathan Doman  * validation of the input data, and then set the D-Bus property.
9963cde86f1SJonathan Doman  *
9973cde86f1SJonathan Doman  * @param[in,out]   resp            Async HTTP response.
9983cde86f1SJonathan Doman  * @param[in]       processorId     Processor's Id.
9993cde86f1SJonathan Doman  * @param[in]       appliedConfigUri    New property value to apply.
10003cde86f1SJonathan Doman  * @param[in]       cpuObjectPath   Path of CPU object to modify.
10013cde86f1SJonathan Doman  * @param[in]       serviceMap      Service map for CPU object.
10023cde86f1SJonathan Doman  */
10033cde86f1SJonathan Doman inline void patchAppliedOperatingConfig(
10043cde86f1SJonathan Doman     const std::shared_ptr<bmcweb::AsyncResp>& resp,
10053cde86f1SJonathan Doman     const std::string& processorId, const std::string& appliedConfigUri,
10065df6eda2SShantappa Teekappanavar     const std::string& cpuObjectPath,
10075df6eda2SShantappa Teekappanavar     const dbus::utility::MapperServiceMap& serviceMap)
10083cde86f1SJonathan Doman {
10093cde86f1SJonathan Doman     // Check that the property even exists by checking for the interface
10103cde86f1SJonathan Doman     const std::string* controlService = nullptr;
10113cde86f1SJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
10123cde86f1SJonathan Doman     {
10133cde86f1SJonathan Doman         if (std::find(interfaceList.begin(), interfaceList.end(),
10143cde86f1SJonathan Doman                       "xyz.openbmc_project.Control.Processor."
10153cde86f1SJonathan Doman                       "CurrentOperatingConfig") != interfaceList.end())
10163cde86f1SJonathan Doman         {
10173cde86f1SJonathan Doman             controlService = &serviceName;
10183cde86f1SJonathan Doman             break;
10193cde86f1SJonathan Doman         }
10203cde86f1SJonathan Doman     }
10213cde86f1SJonathan Doman 
10223cde86f1SJonathan Doman     if (controlService == nullptr)
10233cde86f1SJonathan Doman     {
10243cde86f1SJonathan Doman         messages::internalError(resp->res);
10253cde86f1SJonathan Doman         return;
10263cde86f1SJonathan Doman     }
10273cde86f1SJonathan Doman 
10283cde86f1SJonathan Doman     // Check that the config URI is a child of the cpu URI being patched.
10293cde86f1SJonathan Doman     std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
10303cde86f1SJonathan Doman     expectedPrefix += processorId;
10313cde86f1SJonathan Doman     expectedPrefix += "/OperatingConfigs/";
10323cde86f1SJonathan Doman     if (!boost::starts_with(appliedConfigUri, expectedPrefix) ||
10333cde86f1SJonathan Doman         expectedPrefix.size() == appliedConfigUri.size())
10343cde86f1SJonathan Doman     {
10353cde86f1SJonathan Doman         messages::propertyValueIncorrect(
10363cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
10373cde86f1SJonathan Doman         return;
10383cde86f1SJonathan Doman     }
10393cde86f1SJonathan Doman 
10403cde86f1SJonathan Doman     // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
10413cde86f1SJonathan Doman     // direct child of the CPU object.
10423cde86f1SJonathan Doman     // Strip the expectedPrefix from the config URI to get the "filename", and
10433cde86f1SJonathan Doman     // append to the CPU's path.
10443cde86f1SJonathan Doman     std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
10453cde86f1SJonathan Doman     sdbusplus::message::object_path configPath(cpuObjectPath);
10463cde86f1SJonathan Doman     configPath /= configBaseName;
10473cde86f1SJonathan Doman 
10483cde86f1SJonathan Doman     BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
10493cde86f1SJonathan Doman 
10503cde86f1SJonathan Doman     // Set the property, with handler to check error responses
10513cde86f1SJonathan Doman     crow::connections::systemBus->async_method_call(
1052914e2d5dSEd Tanous         [resp, appliedConfigUri](const boost::system::error_code ec,
1053914e2d5dSEd Tanous                                  const sdbusplus::message::message& msg) {
10543cde86f1SJonathan Doman             handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
10553cde86f1SJonathan Doman         },
10563cde86f1SJonathan Doman         *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
10573cde86f1SJonathan Doman         "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
1058168e20c1SEd Tanous         "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
10593cde86f1SJonathan Doman }
10603cde86f1SJonathan Doman 
10617e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfigCollection(App& app)
1062dba0c291SJonathan Doman {
1063dba0c291SJonathan Doman 
10647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
10657e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
1066ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfigCollection)
10670fda0f12SGeorge Liu         .methods(
106845ca1b86SEd Tanous             boost::beast::http::verb::
106945ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
107045ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10717e860f15SJohn Edward Broadbent                             const std::string& cpuName) {
107245ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
107345ca1b86SEd Tanous             {
107445ca1b86SEd Tanous                 return;
107545ca1b86SEd Tanous             }
10768d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
1077dba0c291SJonathan Doman                 "#OperatingConfigCollection.OperatingConfigCollection";
10788d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = req.url;
10790fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1080dba0c291SJonathan Doman 
10817e860f15SJohn Edward Broadbent             // First find the matching CPU object so we know how to
10827e860f15SJohn Edward Broadbent             // constrain our search for related Config objects.
1083dba0c291SJonathan Doman             crow::connections::systemBus->async_method_call(
1084b9d36b47SEd Tanous                 [asyncResp,
1085b9d36b47SEd Tanous                  cpuName](const boost::system::error_code ec,
1086b9d36b47SEd Tanous                           const dbus::utility::MapperGetSubTreePathsResponse&
1087b9d36b47SEd Tanous                               objects) {
1088dba0c291SJonathan Doman                     if (ec)
1089dba0c291SJonathan Doman                     {
1090dba0c291SJonathan Doman                         BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1091dba0c291SJonathan Doman                                            << ec.message();
1092dba0c291SJonathan Doman                         messages::internalError(asyncResp->res);
1093dba0c291SJonathan Doman                         return;
1094dba0c291SJonathan Doman                     }
1095dba0c291SJonathan Doman 
1096dba0c291SJonathan Doman                     for (const std::string& object : objects)
1097dba0c291SJonathan Doman                     {
1098dba0c291SJonathan Doman                         if (!boost::ends_with(object, cpuName))
1099dba0c291SJonathan Doman                         {
1100dba0c291SJonathan Doman                             continue;
1101dba0c291SJonathan Doman                         }
1102dba0c291SJonathan Doman 
11037e860f15SJohn Edward Broadbent                         // Not expected that there will be multiple matching
11047e860f15SJohn Edward Broadbent                         // CPU objects, but if there are just use the first
11057e860f15SJohn Edward Broadbent                         // one.
1106dba0c291SJonathan Doman 
11077e860f15SJohn Edward Broadbent                         // Use the common search routine to construct the
11087e860f15SJohn Edward Broadbent                         // Collection of all Config objects under this CPU.
1109dba0c291SJonathan Doman                         collection_util::getCollectionMembers(
1110dba0c291SJonathan Doman                             asyncResp,
11110fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/Processors/" + cpuName +
11120fda0f12SGeorge Liu                                 "/OperatingConfigs",
11130fda0f12SGeorge Liu                             {"xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"},
1114dba0c291SJonathan Doman                             object.c_str());
1115dba0c291SJonathan Doman                         return;
1116dba0c291SJonathan Doman                     }
1117dba0c291SJonathan Doman                 },
1118dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper",
1119dba0c291SJonathan Doman                 "/xyz/openbmc_project/object_mapper",
1120dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1121dba0c291SJonathan Doman                 "/xyz/openbmc_project/inventory", 0,
11227e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
11230fda0f12SGeorge Liu                     "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
11247e860f15SJohn Edward Broadbent         });
1125dba0c291SJonathan Doman }
1126dba0c291SJonathan Doman 
11277e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfig(App& app)
1128dba0c291SJonathan Doman {
11297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
11307e860f15SJohn Edward Broadbent         app,
11317e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
1132ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfig)
11337e860f15SJohn Edward Broadbent         .methods(
113445ca1b86SEd Tanous             boost::beast::http::verb::
113545ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
113645ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11377e860f15SJohn Edward Broadbent                             const std::string& cpuName,
11387e860f15SJohn Edward Broadbent                             const std::string& configName) {
113945ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
114045ca1b86SEd Tanous             {
114145ca1b86SEd Tanous                 return;
114245ca1b86SEd Tanous             }
11437e860f15SJohn Edward Broadbent             // Ask for all objects implementing OperatingConfig so we can search
11447e860f15SJohn Edward Broadbent             // for one with a matching name
1145dba0c291SJonathan Doman             crow::connections::systemBus->async_method_call(
11465df6eda2SShantappa Teekappanavar                 [asyncResp, cpuName, configName, reqUrl{req.url}](
11475df6eda2SShantappa Teekappanavar                     boost::system::error_code ec,
11485df6eda2SShantappa Teekappanavar                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
1149dba0c291SJonathan Doman                     if (ec)
1150dba0c291SJonathan Doman                     {
1151dba0c291SJonathan Doman                         BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1152dba0c291SJonathan Doman                                            << ec.message();
1153dba0c291SJonathan Doman                         messages::internalError(asyncResp->res);
1154dba0c291SJonathan Doman                         return;
1155dba0c291SJonathan Doman                     }
11567e860f15SJohn Edward Broadbent                     const std::string expectedEnding =
11577e860f15SJohn Edward Broadbent                         cpuName + '/' + configName;
1158dba0c291SJonathan Doman                     for (const auto& [objectPath, serviceMap] : subtree)
1159dba0c291SJonathan Doman                     {
1160dba0c291SJonathan Doman                         // Ignore any configs without matching cpuX/configY
1161dba0c291SJonathan Doman                         if (!boost::ends_with(objectPath, expectedEnding) ||
1162dba0c291SJonathan Doman                             serviceMap.empty())
1163dba0c291SJonathan Doman                         {
1164dba0c291SJonathan Doman                             continue;
1165dba0c291SJonathan Doman                         }
1166dba0c291SJonathan Doman 
1167dba0c291SJonathan Doman                         nlohmann::json& json = asyncResp->res.jsonValue;
1168dba0c291SJonathan Doman                         json["@odata.type"] =
1169dba0c291SJonathan Doman                             "#OperatingConfig.v1_0_0.OperatingConfig";
1170dba0c291SJonathan Doman                         json["@odata.id"] = reqUrl;
1171dba0c291SJonathan Doman                         json["Name"] = "Processor Profile";
1172dba0c291SJonathan Doman                         json["Id"] = configName;
1173dba0c291SJonathan Doman 
1174dba0c291SJonathan Doman                         // Just use the first implementation of the object - not
11757e860f15SJohn Edward Broadbent                         // expected that there would be multiple matching
11767e860f15SJohn Edward Broadbent                         // services
11777e860f15SJohn Edward Broadbent                         getOperatingConfigData(
11787e860f15SJohn Edward Broadbent                             asyncResp, serviceMap.begin()->first, objectPath);
1179dba0c291SJonathan Doman                         return;
1180dba0c291SJonathan Doman                     }
11817e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res,
11827e860f15SJohn Edward Broadbent                                                "OperatingConfig", configName);
1183dba0c291SJonathan Doman                 },
1184dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper",
1185dba0c291SJonathan Doman                 "/xyz/openbmc_project/object_mapper",
1186dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1187dba0c291SJonathan Doman                 "/xyz/openbmc_project/inventory", 0,
1188dba0c291SJonathan Doman                 std::array<const char*, 1>{
1189dba0c291SJonathan Doman                     "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
11907e860f15SJohn Edward Broadbent         });
1191ac6a4445SGunnar Mills }
1192ac6a4445SGunnar Mills 
11937e860f15SJohn Edward Broadbent inline void requestRoutesProcessorCollection(App& app)
11947e860f15SJohn Edward Broadbent {
1195ac6a4445SGunnar Mills     /**
1196ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1197ac6a4445SGunnar Mills      */
11987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
1199ed398213SEd Tanous         .privileges(redfish::privileges::getProcessorCollection)
12007e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
120145ca1b86SEd Tanous             [&app](const crow::Request& req,
12027e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
120345ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
120445ca1b86SEd Tanous                 {
120545ca1b86SEd Tanous                     return;
120645ca1b86SEd Tanous                 }
12078d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
1208ac6a4445SGunnar Mills                     "#ProcessorCollection.ProcessorCollection";
12098d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Processor Collection";
1210ac6a4445SGunnar Mills 
12118d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
12128d1b46d7Szhanghch05                     "/redfish/v1/Systems/system/Processors";
1213ac6a4445SGunnar Mills 
121405030b8eSGunnar Mills                 collection_util::getCollectionMembers(
121505030b8eSGunnar Mills                     asyncResp, "/redfish/v1/Systems/system/Processors",
1216c951448aSJonathan Doman                     std::vector<const char*>(processorInterfaces.begin(),
1217c951448aSJonathan Doman                                              processorInterfaces.end()));
12187e860f15SJohn Edward Broadbent             });
1219ac6a4445SGunnar Mills }
1220ac6a4445SGunnar Mills 
12217e860f15SJohn Edward Broadbent inline void requestRoutesProcessor(App& app)
12227e860f15SJohn Edward Broadbent {
1223ac6a4445SGunnar Mills     /**
1224ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1225ac6a4445SGunnar Mills      */
12267e860f15SJohn Edward Broadbent 
12277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1228ed398213SEd Tanous         .privileges(redfish::privileges::getProcessor)
12297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
123045ca1b86SEd Tanous             [&app](const crow::Request& req,
12317e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12327e860f15SJohn Edward Broadbent                    const std::string& processorId) {
123345ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
123445ca1b86SEd Tanous                 {
123545ca1b86SEd Tanous                     return;
123645ca1b86SEd Tanous                 }
12378d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
12388d1b46d7Szhanghch05                     "#Processor.v1_11_0.Processor";
12398d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
1240ac6a4445SGunnar Mills                     "/redfish/v1/Systems/system/Processors/" + processorId;
1241ac6a4445SGunnar Mills 
1242c951448aSJonathan Doman                 getProcessorObject(asyncResp, processorId, getProcessorData);
12437e860f15SJohn Edward Broadbent             });
12443cde86f1SJonathan Doman 
12457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1246ed398213SEd Tanous         .privileges(redfish::privileges::patchProcessor)
12477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
124845ca1b86SEd Tanous             [&app](const crow::Request& req,
12497e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12507e860f15SJohn Edward Broadbent                    const std::string& processorId) {
125145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
125245ca1b86SEd Tanous                 {
125345ca1b86SEd Tanous                     return;
125445ca1b86SEd Tanous                 }
12553cde86f1SJonathan Doman                 std::optional<nlohmann::json> appliedConfigJson;
125615ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res,
12577e860f15SJohn Edward Broadbent                                               "AppliedOperatingConfig",
12583cde86f1SJonathan Doman                                               appliedConfigJson))
12593cde86f1SJonathan Doman                 {
12603cde86f1SJonathan Doman                     return;
12613cde86f1SJonathan Doman                 }
12623cde86f1SJonathan Doman 
12633cde86f1SJonathan Doman                 std::string appliedConfigUri;
12643cde86f1SJonathan Doman                 if (appliedConfigJson)
12653cde86f1SJonathan Doman                 {
12663cde86f1SJonathan Doman                     if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
12673cde86f1SJonathan Doman                                              "@odata.id", appliedConfigUri))
12683cde86f1SJonathan Doman                     {
12693cde86f1SJonathan Doman                         return;
12703cde86f1SJonathan Doman                     }
12717e860f15SJohn Edward Broadbent                     // Check for 404 and find matching D-Bus object, then run
12727e860f15SJohn Edward Broadbent                     // property patch handlers if that all succeeds.
12733cde86f1SJonathan Doman                     getProcessorObject(
12747e860f15SJohn Edward Broadbent                         asyncResp, processorId,
12753cde86f1SJonathan Doman                         [appliedConfigUri = std::move(appliedConfigUri)](
12763cde86f1SJonathan Doman                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12773cde86f1SJonathan Doman                             const std::string& processorId,
12783cde86f1SJonathan Doman                             const std::string& objectPath,
12795df6eda2SShantappa Teekappanavar                             const dbus::utility::MapperServiceMap& serviceMap) {
12803cde86f1SJonathan Doman                             patchAppliedOperatingConfig(asyncResp, processorId,
12817e860f15SJohn Edward Broadbent                                                         appliedConfigUri,
12827e860f15SJohn Edward Broadbent                                                         objectPath, serviceMap);
12833cde86f1SJonathan Doman                         });
12843cde86f1SJonathan Doman                 }
12857e860f15SJohn Edward Broadbent             });
12863cde86f1SJonathan Doman }
1287ac6a4445SGunnar Mills 
1288ac6a4445SGunnar Mills } // namespace redfish
1289