xref: /openbmc/bmcweb/features/redfish/lib/processor.hpp (revision 168e20c1306e3e689907ba43e14ea679e2328611)
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 
18ac6a4445SGunnar Mills #include "health.hpp"
19ac6a4445SGunnar Mills 
207e860f15SJohn Edward Broadbent #include <app.hpp>
21ac6a4445SGunnar Mills #include <boost/container/flat_map.hpp>
22*168e20c1SEd Tanous #include <dbus_utility.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
24dba0c291SJonathan Doman #include <sdbusplus/message/native_types.hpp>
25dba0c291SJonathan Doman #include <sdbusplus/utility/dedup_variant.hpp>
26ac6a4445SGunnar Mills #include <utils/collection.hpp>
27ac6a4445SGunnar Mills #include <utils/json_utils.hpp>
28ac6a4445SGunnar Mills 
29ac6a4445SGunnar Mills namespace redfish
30ac6a4445SGunnar Mills {
31ac6a4445SGunnar Mills 
32ac6a4445SGunnar Mills using InterfacesProperties = boost::container::flat_map<
33ac6a4445SGunnar Mills     std::string,
34ac6a4445SGunnar Mills     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>>;
35ac6a4445SGunnar Mills 
36c951448aSJonathan Doman // Map of service name to list of interfaces
37c951448aSJonathan Doman using MapperServiceMap =
38c951448aSJonathan Doman     std::vector<std::pair<std::string, std::vector<std::string>>>;
39c951448aSJonathan Doman 
40c951448aSJonathan Doman // Map of object paths to MapperServiceMaps
41c951448aSJonathan Doman using MapperGetSubTreeResponse =
42c951448aSJonathan Doman     std::vector<std::pair<std::string, MapperServiceMap>>;
43c951448aSJonathan Doman 
44c951448aSJonathan Doman // Interfaces which imply a D-Bus object represents a Processor
45c951448aSJonathan Doman constexpr std::array<const char*, 2> processorInterfaces = {
46c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Cpu",
47c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Accelerator"};
482bab9831SJonathan Doman 
4971b82f26SSharad Yadav /**
5071b82f26SSharad Yadav  * @brief Fill out uuid info of a processor by
5171b82f26SSharad Yadav  * requesting data from the given D-Bus object.
5271b82f26SSharad Yadav  *
5371b82f26SSharad Yadav  * @param[in,out]   aResp       Async HTTP response.
5471b82f26SSharad Yadav  * @param[in]       service     D-Bus service to query.
5571b82f26SSharad Yadav  * @param[in]       objPath     D-Bus object to query.
5671b82f26SSharad Yadav  */
5771b82f26SSharad Yadav inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
5871b82f26SSharad Yadav                              const std::string& service,
5971b82f26SSharad Yadav                              const std::string& objPath)
6071b82f26SSharad Yadav {
6171b82f26SSharad Yadav     BMCWEB_LOG_DEBUG << "Get Processor UUID";
6271b82f26SSharad Yadav     crow::connections::systemBus->async_method_call(
63*168e20c1SEd Tanous         [objPath, aResp{std::move(aResp)}](
64*168e20c1SEd Tanous             const boost::system::error_code ec,
65*168e20c1SEd Tanous             const dbus::utility::DbusVariantType& property) {
6671b82f26SSharad Yadav             if (ec)
6771b82f26SSharad Yadav             {
6871b82f26SSharad Yadav                 BMCWEB_LOG_DEBUG << "DBUS response error";
6971b82f26SSharad Yadav                 messages::internalError(aResp->res);
7071b82f26SSharad Yadav                 return;
7171b82f26SSharad Yadav             }
7271b82f26SSharad Yadav             const std::string* value = std::get_if<std::string>(&property);
7371b82f26SSharad Yadav             if (value == nullptr)
7471b82f26SSharad Yadav             {
7571b82f26SSharad Yadav                 BMCWEB_LOG_DEBUG << "Null value returned "
7671b82f26SSharad Yadav                                     "for UUID";
7771b82f26SSharad Yadav                 messages::internalError(aResp->res);
7871b82f26SSharad Yadav                 return;
7971b82f26SSharad Yadav             }
8071b82f26SSharad Yadav             aResp->res.jsonValue["UUID"] = *value;
8171b82f26SSharad Yadav         },
8271b82f26SSharad Yadav         service, objPath, "org.freedesktop.DBus.Properties", "Get",
8371b82f26SSharad Yadav         "xyz.openbmc_project.Common.UUID", "UUID");
8471b82f26SSharad Yadav }
8571b82f26SSharad Yadav 
86ac6a4445SGunnar Mills inline void
878d1b46d7Szhanghch05     getCpuDataByInterface(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
88ac6a4445SGunnar Mills                           const InterfacesProperties& cpuInterfacesProperties)
89ac6a4445SGunnar Mills {
90ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
91ac6a4445SGunnar Mills 
92a1649ec6SChicago Duan     // Set the default value of state
93a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["State"] = "Enabled";
94a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["Health"] = "OK";
95ac6a4445SGunnar Mills 
96ac6a4445SGunnar Mills     for (const auto& interface : cpuInterfacesProperties)
97ac6a4445SGunnar Mills     {
98ac6a4445SGunnar Mills         for (const auto& property : interface.second)
99ac6a4445SGunnar Mills         {
100a1649ec6SChicago Duan             if (property.first == "Present")
101ac6a4445SGunnar Mills             {
102a1649ec6SChicago Duan                 const bool* cpuPresent = std::get_if<bool>(&property.second);
103a1649ec6SChicago Duan                 if (cpuPresent == nullptr)
104ac6a4445SGunnar Mills                 {
105ac6a4445SGunnar Mills                     // Important property not in desired type
106ac6a4445SGunnar Mills                     messages::internalError(aResp->res);
107ac6a4445SGunnar Mills                     return;
108ac6a4445SGunnar Mills                 }
109a1649ec6SChicago Duan                 if (*cpuPresent == false)
110ac6a4445SGunnar Mills                 {
111a1649ec6SChicago Duan                     // Slot is not populated
112ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
113a1649ec6SChicago Duan                 }
114a1649ec6SChicago Duan             }
115a1649ec6SChicago Duan             else if (property.first == "Functional")
116a1649ec6SChicago Duan             {
117a1649ec6SChicago Duan                 const bool* cpuFunctional = std::get_if<bool>(&property.second);
118a1649ec6SChicago Duan                 if (cpuFunctional == nullptr)
119a1649ec6SChicago Duan                 {
120a1649ec6SChicago Duan                     messages::internalError(aResp->res);
121ac6a4445SGunnar Mills                     return;
122ac6a4445SGunnar Mills                 }
123a1649ec6SChicago Duan                 if (*cpuFunctional == false)
124a1649ec6SChicago Duan                 {
125a1649ec6SChicago Duan                     aResp->res.jsonValue["Status"]["Health"] = "Critical";
126a1649ec6SChicago Duan                 }
127a1649ec6SChicago Duan             }
128a1649ec6SChicago Duan             else if (property.first == "CoreCount")
129a1649ec6SChicago Duan             {
130a1649ec6SChicago Duan                 const uint16_t* coresCount =
131a1649ec6SChicago Duan                     std::get_if<uint16_t>(&property.second);
132a1649ec6SChicago Duan                 if (coresCount == nullptr)
133a1649ec6SChicago Duan                 {
134a1649ec6SChicago Duan                     messages::internalError(aResp->res);
135a1649ec6SChicago Duan                     return;
136a1649ec6SChicago Duan                 }
137ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = *coresCount;
138ac6a4445SGunnar Mills             }
139dc3fa667SJonathan Doman             else if (property.first == "MaxSpeedInMhz")
140dc3fa667SJonathan Doman             {
141dc3fa667SJonathan Doman                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
142dc3fa667SJonathan Doman                 if (value != nullptr)
143dc3fa667SJonathan Doman                 {
144dc3fa667SJonathan Doman                     aResp->res.jsonValue["MaxSpeedMHz"] = *value;
145dc3fa667SJonathan Doman                 }
146dc3fa667SJonathan Doman             }
147ac6a4445SGunnar Mills             else if (property.first == "Socket")
148ac6a4445SGunnar Mills             {
149ac6a4445SGunnar Mills                 const std::string* value =
150ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
151ac6a4445SGunnar Mills                 if (value != nullptr)
152ac6a4445SGunnar Mills                 {
153ac6a4445SGunnar Mills                     aResp->res.jsonValue["Socket"] = *value;
154ac6a4445SGunnar Mills                 }
155ac6a4445SGunnar Mills             }
156ac6a4445SGunnar Mills             else if (property.first == "ThreadCount")
157ac6a4445SGunnar Mills             {
158dc3fa667SJonathan Doman                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
159ac6a4445SGunnar Mills                 if (value != nullptr)
160ac6a4445SGunnar Mills                 {
161ac6a4445SGunnar Mills                     aResp->res.jsonValue["TotalThreads"] = *value;
162ac6a4445SGunnar Mills                 }
163ac6a4445SGunnar Mills             }
1641930fbd4SBrandon Kim             else if (property.first == "EffectiveFamily")
165ac6a4445SGunnar Mills             {
1661930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
167ac6a4445SGunnar Mills                 if (value != nullptr)
168ac6a4445SGunnar Mills                 {
169ac6a4445SGunnar Mills                     aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
1701930fbd4SBrandon Kim                         "0x" + intToHexString(*value);
171ac6a4445SGunnar Mills                 }
172ac6a4445SGunnar Mills             }
1731930fbd4SBrandon Kim             else if (property.first == "EffectiveModel")
1741930fbd4SBrandon Kim             {
1751930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
1761930fbd4SBrandon Kim                 if (value == nullptr)
1771930fbd4SBrandon Kim                 {
1781930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1791930fbd4SBrandon Kim                     return;
1801930fbd4SBrandon Kim                 }
1811930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
1821930fbd4SBrandon Kim                     "0x" + intToHexString(*value);
1831930fbd4SBrandon Kim             }
184ac6a4445SGunnar Mills             else if (property.first == "Id")
185ac6a4445SGunnar Mills             {
186ac6a4445SGunnar Mills                 const uint64_t* value = std::get_if<uint64_t>(&property.second);
187ac6a4445SGunnar Mills                 if (value != nullptr && *value != 0)
188ac6a4445SGunnar Mills                 {
189ac6a4445SGunnar Mills                     aResp->res
190ac6a4445SGunnar Mills                         .jsonValue["ProcessorId"]["IdentificationRegisters"] =
191f201ffb4SEd Tanous                         "0x" + intToHexString(*value);
192ac6a4445SGunnar Mills                 }
193ac6a4445SGunnar Mills             }
1941930fbd4SBrandon Kim             else if (property.first == "Microcode")
1951930fbd4SBrandon Kim             {
1961930fbd4SBrandon Kim                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
1971930fbd4SBrandon Kim                 if (value == nullptr)
1981930fbd4SBrandon Kim                 {
1991930fbd4SBrandon Kim                     messages::internalError(aResp->res);
2001930fbd4SBrandon Kim                     return;
2011930fbd4SBrandon Kim                 }
2021930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
2031930fbd4SBrandon Kim                     "0x" + intToHexString(*value);
2041930fbd4SBrandon Kim             }
2051930fbd4SBrandon Kim             else if (property.first == "Step")
2061930fbd4SBrandon Kim             {
2071930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
2081930fbd4SBrandon Kim                 if (value == nullptr)
2091930fbd4SBrandon Kim                 {
2101930fbd4SBrandon Kim                     messages::internalError(aResp->res);
2111930fbd4SBrandon Kim                     return;
2121930fbd4SBrandon Kim                 }
2131930fbd4SBrandon Kim                 aResp->res.jsonValue["ProcessorId"]["Step"] =
2141930fbd4SBrandon Kim                     "0x" + intToHexString(*value);
2151930fbd4SBrandon Kim             }
216ac6a4445SGunnar Mills         }
217ac6a4445SGunnar Mills     }
218ac6a4445SGunnar Mills 
219ac6a4445SGunnar Mills     return;
220ac6a4445SGunnar Mills }
221ac6a4445SGunnar Mills 
2228d1b46d7Szhanghch05 inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
223ac6a4445SGunnar Mills                                 const std::string& cpuId,
224ac6a4445SGunnar Mills                                 const std::string& service,
225ac6a4445SGunnar Mills                                 const std::string& objPath)
226ac6a4445SGunnar Mills {
227ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
228ac6a4445SGunnar Mills 
229ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
230ac6a4445SGunnar Mills         [cpuId, service, objPath, aResp{std::move(aResp)}](
231ac6a4445SGunnar Mills             const boost::system::error_code ec,
232ac6a4445SGunnar Mills             const dbus::utility::ManagedObjectType& dbusData) {
233ac6a4445SGunnar Mills             if (ec)
234ac6a4445SGunnar Mills             {
235ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
236ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
237ac6a4445SGunnar Mills                 return;
238ac6a4445SGunnar Mills             }
239ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = cpuId;
240ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
241ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "CPU";
242ac6a4445SGunnar Mills 
243ac6a4445SGunnar Mills             bool slotPresent = false;
244ac6a4445SGunnar Mills             std::string corePath = objPath + "/core";
245ac6a4445SGunnar Mills             size_t totalCores = 0;
246ac6a4445SGunnar Mills             for (const auto& object : dbusData)
247ac6a4445SGunnar Mills             {
248ac6a4445SGunnar Mills                 if (object.first.str == objPath)
249ac6a4445SGunnar Mills                 {
250ac6a4445SGunnar Mills                     getCpuDataByInterface(aResp, object.second);
251ac6a4445SGunnar Mills                 }
252ac6a4445SGunnar Mills                 else if (boost::starts_with(object.first.str, corePath))
253ac6a4445SGunnar Mills                 {
254ac6a4445SGunnar Mills                     for (const auto& interface : object.second)
255ac6a4445SGunnar Mills                     {
256ac6a4445SGunnar Mills                         if (interface.first ==
257ac6a4445SGunnar Mills                             "xyz.openbmc_project.Inventory.Item")
258ac6a4445SGunnar Mills                         {
259ac6a4445SGunnar Mills                             for (const auto& property : interface.second)
260ac6a4445SGunnar Mills                             {
261ac6a4445SGunnar Mills                                 if (property.first == "Present")
262ac6a4445SGunnar Mills                                 {
263ac6a4445SGunnar Mills                                     const bool* present =
264ac6a4445SGunnar Mills                                         std::get_if<bool>(&property.second);
265ac6a4445SGunnar Mills                                     if (present != nullptr)
266ac6a4445SGunnar Mills                                     {
267ac6a4445SGunnar Mills                                         if (*present == true)
268ac6a4445SGunnar Mills                                         {
269ac6a4445SGunnar Mills                                             slotPresent = true;
270ac6a4445SGunnar Mills                                             totalCores++;
271ac6a4445SGunnar Mills                                         }
272ac6a4445SGunnar Mills                                     }
273ac6a4445SGunnar Mills                                 }
274ac6a4445SGunnar Mills                             }
275ac6a4445SGunnar Mills                         }
276ac6a4445SGunnar Mills                     }
277ac6a4445SGunnar Mills                 }
278ac6a4445SGunnar Mills             }
279ac6a4445SGunnar Mills             // In getCpuDataByInterface(), state and health are set
280ac6a4445SGunnar Mills             // based on the present and functional status. If core
281ac6a4445SGunnar Mills             // count is zero, then it has a higher precedence.
282ac6a4445SGunnar Mills             if (slotPresent)
283ac6a4445SGunnar Mills             {
284ac6a4445SGunnar Mills                 if (totalCores == 0)
285ac6a4445SGunnar Mills                 {
286ac6a4445SGunnar Mills                     // Slot is not populated, set status end return
287ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
288ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["Health"] = "OK";
289ac6a4445SGunnar Mills                 }
290ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = totalCores;
291ac6a4445SGunnar Mills             }
292ac6a4445SGunnar Mills             return;
293ac6a4445SGunnar Mills         },
294ac6a4445SGunnar Mills         service, "/xyz/openbmc_project/inventory",
295ac6a4445SGunnar Mills         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
296ac6a4445SGunnar Mills }
297ac6a4445SGunnar Mills 
2988d1b46d7Szhanghch05 inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
299ac6a4445SGunnar Mills                             const std::string& service,
300ac6a4445SGunnar Mills                             const std::string& objPath)
301ac6a4445SGunnar Mills {
302ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
303ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
304ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
305ac6a4445SGunnar Mills             const boost::system::error_code ec,
306ac6a4445SGunnar Mills             const boost::container::flat_map<
307*168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
308ac6a4445SGunnar Mills             if (ec)
309ac6a4445SGunnar Mills             {
310ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
311ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
312ac6a4445SGunnar Mills                 return;
313ac6a4445SGunnar Mills             }
314ac6a4445SGunnar Mills 
315ac6a4445SGunnar Mills             for (const auto& property : properties)
316ac6a4445SGunnar Mills             {
317ac6a4445SGunnar Mills                 if (property.first == "SerialNumber")
318ac6a4445SGunnar Mills                 {
319ac6a4445SGunnar Mills                     const std::string* sn =
320ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
321ac6a4445SGunnar Mills                     if (sn != nullptr && !sn->empty())
322ac6a4445SGunnar Mills                     {
323ac6a4445SGunnar Mills                         aResp->res.jsonValue["SerialNumber"] = *sn;
324ac6a4445SGunnar Mills                     }
325ac6a4445SGunnar Mills                 }
326ac6a4445SGunnar Mills                 else if (property.first == "Model")
327ac6a4445SGunnar Mills                 {
328ac6a4445SGunnar Mills                     const std::string* model =
329ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
330ac6a4445SGunnar Mills                     if (model != nullptr && !model->empty())
331ac6a4445SGunnar Mills                     {
332ac6a4445SGunnar Mills                         aResp->res.jsonValue["Model"] = *model;
333ac6a4445SGunnar Mills                     }
334ac6a4445SGunnar Mills                 }
335ac6a4445SGunnar Mills                 else if (property.first == "Manufacturer")
336ac6a4445SGunnar Mills                 {
337ac6a4445SGunnar Mills 
338ac6a4445SGunnar Mills                     const std::string* mfg =
339ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
340ac6a4445SGunnar Mills                     if (mfg != nullptr)
341ac6a4445SGunnar Mills                     {
342ac6a4445SGunnar Mills                         aResp->res.jsonValue["Manufacturer"] = *mfg;
343ac6a4445SGunnar Mills 
344ac6a4445SGunnar Mills                         // Otherwise would be unexpected.
345ac6a4445SGunnar Mills                         if (mfg->find("Intel") != std::string::npos)
346ac6a4445SGunnar Mills                         {
347ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
348ac6a4445SGunnar Mills                                 "x86";
349ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "x86-64";
350ac6a4445SGunnar Mills                         }
351ac6a4445SGunnar Mills                         else if (mfg->find("IBM") != std::string::npos)
352ac6a4445SGunnar Mills                         {
353ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
354ac6a4445SGunnar Mills                                 "Power";
355ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "PowerISA";
356ac6a4445SGunnar Mills                         }
357ac6a4445SGunnar Mills                     }
358ac6a4445SGunnar Mills                 }
359cba4f448SSunnySrivastava1984                 else if (property.first == "PartNumber")
360cba4f448SSunnySrivastava1984                 {
361cba4f448SSunnySrivastava1984                     const std::string* partNumber =
362cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
363cba4f448SSunnySrivastava1984 
364cba4f448SSunnySrivastava1984                     if (partNumber == nullptr)
365cba4f448SSunnySrivastava1984                     {
366cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
367cba4f448SSunnySrivastava1984                         return;
368cba4f448SSunnySrivastava1984                     }
369cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["PartNumber"] = *partNumber;
370cba4f448SSunnySrivastava1984                 }
371cba4f448SSunnySrivastava1984                 else if (property.first == "SparePartNumber")
372cba4f448SSunnySrivastava1984                 {
373cba4f448SSunnySrivastava1984                     const std::string* sparePartNumber =
374cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
375cba4f448SSunnySrivastava1984 
376cba4f448SSunnySrivastava1984                     if (sparePartNumber == nullptr)
377cba4f448SSunnySrivastava1984                     {
378cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
379cba4f448SSunnySrivastava1984                         return;
380cba4f448SSunnySrivastava1984                     }
381cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
382cba4f448SSunnySrivastava1984                 }
383ac6a4445SGunnar Mills             }
384ac6a4445SGunnar Mills         },
385ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
386ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Asset");
387ac6a4445SGunnar Mills }
388ac6a4445SGunnar Mills 
3898d1b46d7Szhanghch05 inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
390ac6a4445SGunnar Mills                                const std::string& service,
391ac6a4445SGunnar Mills                                const std::string& objPath)
392ac6a4445SGunnar Mills {
393ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
394ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
395ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
396ac6a4445SGunnar Mills             const boost::system::error_code ec,
397ac6a4445SGunnar Mills             const boost::container::flat_map<
398*168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
399ac6a4445SGunnar Mills             if (ec)
400ac6a4445SGunnar Mills             {
401ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
402ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
403ac6a4445SGunnar Mills                 return;
404ac6a4445SGunnar Mills             }
405ac6a4445SGunnar Mills 
406ac6a4445SGunnar Mills             for (const auto& property : properties)
407ac6a4445SGunnar Mills             {
408ac6a4445SGunnar Mills                 if (property.first == "Version")
409ac6a4445SGunnar Mills                 {
410ac6a4445SGunnar Mills                     const std::string* ver =
411ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
412ac6a4445SGunnar Mills                     if (ver != nullptr)
413ac6a4445SGunnar Mills                     {
414ac6a4445SGunnar Mills                         aResp->res.jsonValue["Version"] = *ver;
415ac6a4445SGunnar Mills                     }
416ac6a4445SGunnar Mills                     break;
417ac6a4445SGunnar Mills                 }
418ac6a4445SGunnar Mills             }
419ac6a4445SGunnar Mills         },
420ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
421ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Revision");
422ac6a4445SGunnar Mills }
423ac6a4445SGunnar Mills 
4248d1b46d7Szhanghch05 inline void getAcceleratorDataByService(
4258d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
4268d1b46d7Szhanghch05     const std::string& service, const std::string& objPath)
427ac6a4445SGunnar Mills {
428ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG
429ac6a4445SGunnar Mills         << "Get available system Accelerator resources by service.";
430ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
431ac6a4445SGunnar Mills         [acclrtrId, aResp{std::move(aResp)}](
432ac6a4445SGunnar Mills             const boost::system::error_code ec,
433ac6a4445SGunnar Mills             const boost::container::flat_map<
434*168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>& properties) {
435ac6a4445SGunnar Mills             if (ec)
436ac6a4445SGunnar Mills             {
437ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
438ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
439ac6a4445SGunnar Mills                 return;
440ac6a4445SGunnar Mills             }
441ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = acclrtrId;
442ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
443ac6a4445SGunnar Mills             const bool* accPresent = nullptr;
444ac6a4445SGunnar Mills             const bool* accFunctional = nullptr;
445ac6a4445SGunnar Mills 
446ac6a4445SGunnar Mills             for (const auto& property : properties)
447ac6a4445SGunnar Mills             {
448ac6a4445SGunnar Mills                 if (property.first == "Functional")
449ac6a4445SGunnar Mills                 {
450ac6a4445SGunnar Mills                     accFunctional = std::get_if<bool>(&property.second);
451ac6a4445SGunnar Mills                 }
452ac6a4445SGunnar Mills                 else if (property.first == "Present")
453ac6a4445SGunnar Mills                 {
454ac6a4445SGunnar Mills                     accPresent = std::get_if<bool>(&property.second);
455ac6a4445SGunnar Mills                 }
456ac6a4445SGunnar Mills             }
457ac6a4445SGunnar Mills 
458ac6a4445SGunnar Mills             std::string state = "Enabled";
459ac6a4445SGunnar Mills             std::string health = "OK";
460ac6a4445SGunnar Mills 
461ac6a4445SGunnar Mills             if (accPresent != nullptr && *accPresent == false)
462ac6a4445SGunnar Mills             {
463ac6a4445SGunnar Mills                 state = "Absent";
464ac6a4445SGunnar Mills             }
465ac6a4445SGunnar Mills 
466ac6a4445SGunnar Mills             if ((accFunctional != nullptr) && (*accFunctional == false))
467ac6a4445SGunnar Mills             {
468ac6a4445SGunnar Mills                 if (state == "Enabled")
469ac6a4445SGunnar Mills                 {
470ac6a4445SGunnar Mills                     health = "Critical";
471ac6a4445SGunnar Mills                 }
472ac6a4445SGunnar Mills             }
473ac6a4445SGunnar Mills 
474ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = state;
475ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = health;
476ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "Accelerator";
477ac6a4445SGunnar Mills         },
478ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
479ac6a4445SGunnar Mills }
480ac6a4445SGunnar Mills 
481dba0c291SJonathan Doman // OperatingConfig D-Bus Types
482dba0c291SJonathan Doman using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
483dba0c291SJonathan Doman using BaseSpeedPrioritySettingsProperty =
484dba0c291SJonathan Doman     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
485dba0c291SJonathan Doman // uint32_t and size_t may or may not be the same type, requiring a dedup'd
486dba0c291SJonathan Doman // variant
487*168e20c1SEd Tanous using OperatingConfigProperties =
488*168e20c1SEd Tanous     std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>;
489dba0c291SJonathan Doman 
490dba0c291SJonathan Doman /**
491dba0c291SJonathan Doman  * Fill out the HighSpeedCoreIDs in a Processor resource from the given
492dba0c291SJonathan Doman  * OperatingConfig D-Bus property.
493dba0c291SJonathan Doman  *
494dba0c291SJonathan Doman  * @param[in,out]   aResp               Async HTTP response.
495dba0c291SJonathan Doman  * @param[in]       baseSpeedSettings   Full list of base speed priority groups,
496dba0c291SJonathan Doman  *                                      to use to determine the list of high
497dba0c291SJonathan Doman  *                                      speed cores.
498dba0c291SJonathan Doman  */
499dba0c291SJonathan Doman inline void highSpeedCoreIdsHandler(
5008d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
501dba0c291SJonathan Doman     const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
502dba0c291SJonathan Doman {
503dba0c291SJonathan Doman     // The D-Bus property does not indicate which bucket is the "high
504dba0c291SJonathan Doman     // priority" group, so let's discern that by looking for the one with
505dba0c291SJonathan Doman     // highest base frequency.
506dba0c291SJonathan Doman     auto highPriorityGroup = baseSpeedSettings.cend();
507dba0c291SJonathan Doman     uint32_t highestBaseSpeed = 0;
508dba0c291SJonathan Doman     for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
509dba0c291SJonathan Doman          ++it)
510dba0c291SJonathan Doman     {
511dba0c291SJonathan Doman         const uint32_t baseFreq = std::get<uint32_t>(*it);
512dba0c291SJonathan Doman         if (baseFreq > highestBaseSpeed)
513dba0c291SJonathan Doman         {
514dba0c291SJonathan Doman             highestBaseSpeed = baseFreq;
515dba0c291SJonathan Doman             highPriorityGroup = it;
516dba0c291SJonathan Doman         }
517dba0c291SJonathan Doman     }
518dba0c291SJonathan Doman 
519dba0c291SJonathan Doman     nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
520dba0c291SJonathan Doman     jsonCoreIds = nlohmann::json::array();
521dba0c291SJonathan Doman 
522dba0c291SJonathan Doman     // There may not be any entries in the D-Bus property, so only populate
523dba0c291SJonathan Doman     // if there was actually something there.
524dba0c291SJonathan Doman     if (highPriorityGroup != baseSpeedSettings.cend())
525dba0c291SJonathan Doman     {
526dba0c291SJonathan Doman         jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
527dba0c291SJonathan Doman     }
528dba0c291SJonathan Doman }
529dba0c291SJonathan Doman 
530dba0c291SJonathan Doman /**
531dba0c291SJonathan Doman  * Fill out OperatingConfig related items in a Processor resource by requesting
532dba0c291SJonathan Doman  * data from the given D-Bus object.
533dba0c291SJonathan Doman  *
534dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
535dba0c291SJonathan Doman  * @param[in]       cpuId       CPU D-Bus name.
536dba0c291SJonathan Doman  * @param[in]       service     D-Bus service to query.
537dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
538dba0c291SJonathan Doman  */
5398d1b46d7Szhanghch05 inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
540dba0c291SJonathan Doman                              const std::string& cpuId,
541dba0c291SJonathan Doman                              const std::string& service,
542dba0c291SJonathan Doman                              const std::string& objPath)
543dba0c291SJonathan Doman {
544dba0c291SJonathan Doman     BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
545dba0c291SJonathan Doman 
546dba0c291SJonathan Doman     // First, GetAll CurrentOperatingConfig properties on the object
547dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
548dba0c291SJonathan Doman         [aResp, cpuId, service](
549dba0c291SJonathan Doman             const boost::system::error_code ec,
550*168e20c1SEd Tanous             const std::vector<std::pair<
551*168e20c1SEd Tanous                 std::string, dbus::utility::DbusVariantType>>& properties) {
552dba0c291SJonathan Doman             if (ec)
553dba0c291SJonathan Doman             {
554dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
555dba0c291SJonathan Doman                                    << ec.message();
556dba0c291SJonathan Doman                 messages::internalError(aResp->res);
557dba0c291SJonathan Doman                 return;
558dba0c291SJonathan Doman             }
559dba0c291SJonathan Doman 
560dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
561dba0c291SJonathan Doman 
562dba0c291SJonathan Doman             for (const auto& [dbusPropName, variantVal] : properties)
563dba0c291SJonathan Doman             {
564dba0c291SJonathan Doman                 if (dbusPropName == "AppliedConfig")
565dba0c291SJonathan Doman                 {
566dba0c291SJonathan Doman                     const sdbusplus::message::object_path* dbusPathWrapper =
567dba0c291SJonathan Doman                         std::get_if<sdbusplus::message::object_path>(
568dba0c291SJonathan Doman                             &variantVal);
569dba0c291SJonathan Doman                     if (dbusPathWrapper == nullptr)
570dba0c291SJonathan Doman                     {
571dba0c291SJonathan Doman                         continue;
572dba0c291SJonathan Doman                     }
573dba0c291SJonathan Doman 
574dba0c291SJonathan Doman                     const std::string& dbusPath = dbusPathWrapper->str;
575dba0c291SJonathan Doman                     std::string uri = "/redfish/v1/Systems/system/Processors/" +
576dba0c291SJonathan Doman                                       cpuId + "/OperatingConfigs";
577dba0c291SJonathan Doman                     json["OperatingConfigs"] = {{"@odata.id", uri}};
578dba0c291SJonathan Doman 
579dba0c291SJonathan Doman                     // Reuse the D-Bus config object name for the Redfish
580dba0c291SJonathan Doman                     // URI
581dba0c291SJonathan Doman                     size_t baseNamePos = dbusPath.rfind('/');
582dba0c291SJonathan Doman                     if (baseNamePos == std::string::npos ||
583dba0c291SJonathan Doman                         baseNamePos == (dbusPath.size() - 1))
584dba0c291SJonathan Doman                     {
585dba0c291SJonathan Doman                         // If the AppliedConfig was somehow not a valid path,
586dba0c291SJonathan Doman                         // skip adding any more properties, since everything
587dba0c291SJonathan Doman                         // else is tied to this applied config.
588dba0c291SJonathan Doman                         messages::internalError(aResp->res);
589dba0c291SJonathan Doman                         break;
590dba0c291SJonathan Doman                     }
591dba0c291SJonathan Doman                     uri += '/';
592dba0c291SJonathan Doman                     uri += dbusPath.substr(baseNamePos + 1);
593dba0c291SJonathan Doman                     json["AppliedOperatingConfig"] = {{"@odata.id", uri}};
594dba0c291SJonathan Doman 
595dba0c291SJonathan Doman                     // Once we found the current applied config, queue another
596dba0c291SJonathan Doman                     // request to read the base freq core ids out of that
597dba0c291SJonathan Doman                     // config.
598dba0c291SJonathan Doman                     crow::connections::systemBus->async_method_call(
599dba0c291SJonathan Doman                         [aResp](
600dba0c291SJonathan Doman                             const boost::system::error_code ec,
601*168e20c1SEd Tanous                             const dbus::utility::DbusVariantType& property) {
602dba0c291SJonathan Doman                             if (ec)
603dba0c291SJonathan Doman                             {
604dba0c291SJonathan Doman                                 BMCWEB_LOG_WARNING
605dba0c291SJonathan Doman                                     << "D-Bus Property Get error: " << ec;
606dba0c291SJonathan Doman                                 messages::internalError(aResp->res);
607dba0c291SJonathan Doman                                 return;
608dba0c291SJonathan Doman                             }
609dba0c291SJonathan Doman                             auto baseSpeedList =
610dba0c291SJonathan Doman                                 std::get_if<BaseSpeedPrioritySettingsProperty>(
611dba0c291SJonathan Doman                                     &property);
612dba0c291SJonathan Doman                             if (baseSpeedList != nullptr)
613dba0c291SJonathan Doman                             {
614dba0c291SJonathan Doman                                 highSpeedCoreIdsHandler(aResp, *baseSpeedList);
615dba0c291SJonathan Doman                             }
616dba0c291SJonathan Doman                         },
617dba0c291SJonathan Doman                         service, dbusPath, "org.freedesktop.DBus.Properties",
618dba0c291SJonathan Doman                         "Get",
6190fda0f12SGeorge Liu                         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig",
620dba0c291SJonathan Doman                         "BaseSpeedPrioritySettings");
621dba0c291SJonathan Doman                 }
622dba0c291SJonathan Doman                 else if (dbusPropName == "BaseSpeedPriorityEnabled")
623dba0c291SJonathan Doman                 {
624dba0c291SJonathan Doman                     const bool* state = std::get_if<bool>(&variantVal);
625dba0c291SJonathan Doman                     if (state != nullptr)
626dba0c291SJonathan Doman                     {
627dba0c291SJonathan Doman                         json["BaseSpeedPriorityState"] =
628dba0c291SJonathan Doman                             *state ? "Enabled" : "Disabled";
629dba0c291SJonathan Doman                     }
630dba0c291SJonathan Doman                 }
631dba0c291SJonathan Doman             }
632dba0c291SJonathan Doman         },
633dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
634dba0c291SJonathan Doman         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig");
635dba0c291SJonathan Doman }
636dba0c291SJonathan Doman 
637cba4f448SSunnySrivastava1984 /**
638cba4f448SSunnySrivastava1984  * @brief Fill out location info of a processor by
639cba4f448SSunnySrivastava1984  * requesting data from the given D-Bus object.
640cba4f448SSunnySrivastava1984  *
641cba4f448SSunnySrivastava1984  * @param[in,out]   aResp       Async HTTP response.
642cba4f448SSunnySrivastava1984  * @param[in]       service     D-Bus service to query.
643cba4f448SSunnySrivastava1984  * @param[in]       objPath     D-Bus object to query.
644cba4f448SSunnySrivastava1984  */
6458d1b46d7Szhanghch05 inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
646cba4f448SSunnySrivastava1984                                const std::string& service,
647cba4f448SSunnySrivastava1984                                const std::string& objPath)
648cba4f448SSunnySrivastava1984 {
649cba4f448SSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
650cba4f448SSunnySrivastava1984     crow::connections::systemBus->async_method_call(
651*168e20c1SEd Tanous         [objPath, aResp{std::move(aResp)}](
652*168e20c1SEd Tanous             const boost::system::error_code ec,
653*168e20c1SEd Tanous             const dbus::utility::DbusVariantType& property) {
654cba4f448SSunnySrivastava1984             if (ec)
655cba4f448SSunnySrivastava1984             {
656cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error";
657cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
658cba4f448SSunnySrivastava1984                 return;
659cba4f448SSunnySrivastava1984             }
660cba4f448SSunnySrivastava1984 
661cba4f448SSunnySrivastava1984             const std::string* value = std::get_if<std::string>(&property);
662cba4f448SSunnySrivastava1984 
663cba4f448SSunnySrivastava1984             if (value == nullptr)
664cba4f448SSunnySrivastava1984             {
665cba4f448SSunnySrivastava1984                 // illegal value
666cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "Location code value error";
667cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
668cba4f448SSunnySrivastava1984                 return;
669cba4f448SSunnySrivastava1984             }
670cba4f448SSunnySrivastava1984 
671cba4f448SSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
672cba4f448SSunnySrivastava1984                 *value;
673cba4f448SSunnySrivastava1984         },
674cba4f448SSunnySrivastava1984         service, objPath, "org.freedesktop.DBus.Properties", "Get",
675cba4f448SSunnySrivastava1984         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode");
676cba4f448SSunnySrivastava1984 }
677cba4f448SSunnySrivastava1984 
678c951448aSJonathan Doman /**
67949e429caSJonathan Doman  * Populate the unique identifier in a Processor resource by requesting data
68049e429caSJonathan Doman  * from the given D-Bus object.
68149e429caSJonathan Doman  *
68249e429caSJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
68349e429caSJonathan Doman  * @param[in]       service     D-Bus service to query.
68449e429caSJonathan Doman  * @param[in]       objPath     D-Bus object to query.
68549e429caSJonathan Doman  */
68649e429caSJonathan Doman inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
68749e429caSJonathan Doman                            const std::string& service,
68849e429caSJonathan Doman                            const std::string& objectPath)
68949e429caSJonathan Doman {
69049e429caSJonathan Doman     BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
69149e429caSJonathan Doman     crow::connections::systemBus->async_method_call(
69249e429caSJonathan Doman         [aResp](boost::system::error_code ec,
693*168e20c1SEd Tanous                 const dbus::utility::DbusVariantType& property) {
69449e429caSJonathan Doman             const std::string* id = std::get_if<std::string>(&property);
69549e429caSJonathan Doman             if (ec || id == nullptr)
69649e429caSJonathan Doman             {
69749e429caSJonathan Doman                 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
69849e429caSJonathan Doman                 messages::internalError(aResp->res);
69949e429caSJonathan Doman                 return;
70049e429caSJonathan Doman             }
70149e429caSJonathan Doman             aResp->res
70249e429caSJonathan Doman                 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
70349e429caSJonathan Doman                 *id;
70449e429caSJonathan Doman         },
70549e429caSJonathan Doman         service, objectPath, "org.freedesktop.DBus.Properties", "Get",
70649e429caSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
70749e429caSJonathan Doman         "UniqueIdentifier");
70849e429caSJonathan Doman }
70949e429caSJonathan Doman 
71049e429caSJonathan Doman /**
711c951448aSJonathan Doman  * Find the D-Bus object representing the requested Processor, and call the
712c951448aSJonathan Doman  * handler with the results. If matching object is not found, add 404 error to
713c951448aSJonathan Doman  * response and don't call the handler.
714c951448aSJonathan Doman  *
715c951448aSJonathan Doman  * @param[in,out]   resp            Async HTTP response.
716c951448aSJonathan Doman  * @param[in]       processorId     Redfish Processor Id.
717c951448aSJonathan Doman  * @param[in]       handler         Callback to continue processing request upon
718c951448aSJonathan Doman  *                                  successfully finding object.
719c951448aSJonathan Doman  */
720c951448aSJonathan Doman template <typename Handler>
7218d1b46d7Szhanghch05 inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
722c951448aSJonathan Doman                                const std::string& processorId,
723c951448aSJonathan Doman                                Handler&& handler)
724ac6a4445SGunnar Mills {
725ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system processor resources.";
726ac6a4445SGunnar Mills 
727c951448aSJonathan Doman     // GetSubTree on all interfaces which provide info about a Processor
728ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
729c951448aSJonathan Doman         [resp, processorId, handler = std::forward<Handler>(handler)](
730c951448aSJonathan Doman             boost::system::error_code ec,
731c951448aSJonathan Doman             const MapperGetSubTreeResponse& subtree) mutable {
732ac6a4445SGunnar Mills             if (ec)
733ac6a4445SGunnar Mills             {
734c951448aSJonathan Doman                 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
735c951448aSJonathan Doman                 messages::internalError(resp->res);
736ac6a4445SGunnar Mills                 return;
737ac6a4445SGunnar Mills             }
7382bab9831SJonathan Doman             for (const auto& [objectPath, serviceMap] : subtree)
739ac6a4445SGunnar Mills             {
7402bab9831SJonathan Doman                 // Ignore any objects which don't end with our desired cpu name
7412bab9831SJonathan Doman                 if (!boost::ends_with(objectPath, processorId))
742ac6a4445SGunnar Mills                 {
7432bab9831SJonathan Doman                     continue;
744ac6a4445SGunnar Mills                 }
7452bab9831SJonathan Doman 
746c951448aSJonathan Doman                 bool found = false;
747c951448aSJonathan Doman                 // Filter out objects that don't have the CPU-specific
748c951448aSJonathan Doman                 // interfaces to make sure we can return 404 on non-CPUs
749c951448aSJonathan Doman                 // (e.g. /redfish/../Processors/dimm0)
7502bab9831SJonathan Doman                 for (const auto& [serviceName, interfaceList] : serviceMap)
751ac6a4445SGunnar Mills                 {
752c951448aSJonathan Doman                     if (std::find_first_of(
753c951448aSJonathan Doman                             interfaceList.begin(), interfaceList.end(),
754c951448aSJonathan Doman                             processorInterfaces.begin(),
755c951448aSJonathan Doman                             processorInterfaces.end()) != interfaceList.end())
7562bab9831SJonathan Doman                     {
757c951448aSJonathan Doman                         found = true;
758c951448aSJonathan Doman                         break;
759c951448aSJonathan Doman                     }
760c951448aSJonathan Doman                 }
761c951448aSJonathan Doman 
762c951448aSJonathan Doman                 if (!found)
7632bab9831SJonathan Doman                 {
764c951448aSJonathan Doman                     continue;
765ac6a4445SGunnar Mills                 }
766c951448aSJonathan Doman 
767c951448aSJonathan Doman                 // Process the first object which does match our cpu name and
768c951448aSJonathan Doman                 // required interfaces, and potentially ignore any other
769c951448aSJonathan Doman                 // matching objects. Assume all interfaces we want to process
770c951448aSJonathan Doman                 // must be on the same object path.
771c951448aSJonathan Doman 
772c951448aSJonathan Doman                 handler(resp, processorId, objectPath, serviceMap);
773ac6a4445SGunnar Mills                 return;
774ac6a4445SGunnar Mills             }
775c951448aSJonathan Doman             messages::resourceNotFound(resp->res, "Processor", processorId);
776ac6a4445SGunnar Mills         },
777ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper",
778ac6a4445SGunnar Mills         "/xyz/openbmc_project/object_mapper",
779ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7802bab9831SJonathan Doman         "/xyz/openbmc_project/inventory", 0,
78149e429caSJonathan Doman         std::array<const char*, 8>{
78271b82f26SSharad Yadav             "xyz.openbmc_project.Common.UUID",
7832bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Asset",
7842bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Revision",
7852bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Cpu",
786cba4f448SSunnySrivastava1984             "xyz.openbmc_project.Inventory.Decorator.LocationCode",
787dba0c291SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Accelerator",
78849e429caSJonathan Doman             "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
78949e429caSJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"});
790ac6a4445SGunnar Mills }
791ac6a4445SGunnar Mills 
7928d1b46d7Szhanghch05 inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
793c951448aSJonathan Doman                              const std::string& processorId,
794c951448aSJonathan Doman                              const std::string& objectPath,
795c951448aSJonathan Doman                              const MapperServiceMap& serviceMap)
796c951448aSJonathan Doman {
797c951448aSJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
798c951448aSJonathan Doman     {
799c951448aSJonathan Doman         for (const auto& interface : interfaceList)
800c951448aSJonathan Doman         {
801c951448aSJonathan Doman             if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
802c951448aSJonathan Doman             {
803c951448aSJonathan Doman                 getCpuAssetData(aResp, serviceName, objectPath);
804c951448aSJonathan Doman             }
8050fda0f12SGeorge Liu             else if (interface ==
8060fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.Revision")
807c951448aSJonathan Doman             {
808c951448aSJonathan Doman                 getCpuRevisionData(aResp, serviceName, objectPath);
809c951448aSJonathan Doman             }
810c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
811c951448aSJonathan Doman             {
812c951448aSJonathan Doman                 getCpuDataByService(aResp, processorId, serviceName,
813c951448aSJonathan Doman                                     objectPath);
814c951448aSJonathan Doman             }
8150fda0f12SGeorge Liu             else if (interface ==
8160fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Item.Accelerator")
817c951448aSJonathan Doman             {
818c951448aSJonathan Doman                 getAcceleratorDataByService(aResp, processorId, serviceName,
819c951448aSJonathan Doman                                             objectPath);
820c951448aSJonathan Doman             }
8210fda0f12SGeorge Liu             else if (
8220fda0f12SGeorge Liu                 interface ==
8230fda0f12SGeorge Liu                 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
824c951448aSJonathan Doman             {
825c951448aSJonathan Doman                 getCpuConfigData(aResp, processorId, serviceName, objectPath);
826c951448aSJonathan Doman             }
8270fda0f12SGeorge Liu             else if (interface ==
8280fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.LocationCode")
829c951448aSJonathan Doman             {
830c951448aSJonathan Doman                 getCpuLocationCode(aResp, serviceName, objectPath);
831c951448aSJonathan Doman             }
83271b82f26SSharad Yadav             else if (interface == "xyz.openbmc_project.Common.UUID")
83371b82f26SSharad Yadav             {
83471b82f26SSharad Yadav                 getProcessorUUID(aResp, serviceName, objectPath);
83571b82f26SSharad Yadav             }
8360fda0f12SGeorge Liu             else if (interface ==
8370fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
83849e429caSJonathan Doman             {
83949e429caSJonathan Doman                 getCpuUniqueId(aResp, serviceName, objectPath);
84049e429caSJonathan Doman             }
841c951448aSJonathan Doman         }
842c951448aSJonathan Doman     }
843c951448aSJonathan Doman }
844c951448aSJonathan Doman 
845dba0c291SJonathan Doman /**
846dba0c291SJonathan Doman  * Request all the properties for the given D-Bus object and fill out the
847dba0c291SJonathan Doman  * related entries in the Redfish OperatingConfig response.
848dba0c291SJonathan Doman  *
849dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
850dba0c291SJonathan Doman  * @param[in]       service     D-Bus service name to query.
851dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
852dba0c291SJonathan Doman  */
8538d1b46d7Szhanghch05 inline void
8548d1b46d7Szhanghch05     getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
855dba0c291SJonathan Doman                            const std::string& service,
856dba0c291SJonathan Doman                            const std::string& objPath)
857dba0c291SJonathan Doman {
858dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
859dba0c291SJonathan Doman         [aResp](boost::system::error_code ec,
860dba0c291SJonathan Doman                 const OperatingConfigProperties& properties) {
861dba0c291SJonathan Doman             if (ec)
862dba0c291SJonathan Doman             {
863dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
864dba0c291SJonathan Doman                                    << ec.message();
865dba0c291SJonathan Doman                 messages::internalError(aResp->res);
866dba0c291SJonathan Doman                 return;
867dba0c291SJonathan Doman             }
868dba0c291SJonathan Doman 
869dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
870dba0c291SJonathan Doman             for (const auto& [key, variant] : properties)
871dba0c291SJonathan Doman             {
872dba0c291SJonathan Doman                 if (key == "AvailableCoreCount")
873dba0c291SJonathan Doman                 {
874dba0c291SJonathan Doman                     const size_t* cores = std::get_if<size_t>(&variant);
875dba0c291SJonathan Doman                     if (cores != nullptr)
876dba0c291SJonathan Doman                     {
877dba0c291SJonathan Doman                         json["TotalAvailableCoreCount"] = *cores;
878dba0c291SJonathan Doman                     }
879dba0c291SJonathan Doman                 }
880dba0c291SJonathan Doman                 else if (key == "BaseSpeed")
881dba0c291SJonathan Doman                 {
882dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
883dba0c291SJonathan Doman                     if (speed != nullptr)
884dba0c291SJonathan Doman                     {
885dba0c291SJonathan Doman                         json["BaseSpeedMHz"] = *speed;
886dba0c291SJonathan Doman                     }
887dba0c291SJonathan Doman                 }
888dba0c291SJonathan Doman                 else if (key == "MaxJunctionTemperature")
889dba0c291SJonathan Doman                 {
890dba0c291SJonathan Doman                     const uint32_t* temp = std::get_if<uint32_t>(&variant);
891dba0c291SJonathan Doman                     if (temp != nullptr)
892dba0c291SJonathan Doman                     {
893dba0c291SJonathan Doman                         json["MaxJunctionTemperatureCelsius"] = *temp;
894dba0c291SJonathan Doman                     }
895dba0c291SJonathan Doman                 }
896dba0c291SJonathan Doman                 else if (key == "MaxSpeed")
897dba0c291SJonathan Doman                 {
898dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
899dba0c291SJonathan Doman                     if (speed != nullptr)
900dba0c291SJonathan Doman                     {
901dba0c291SJonathan Doman                         json["MaxSpeedMHz"] = *speed;
902dba0c291SJonathan Doman                     }
903dba0c291SJonathan Doman                 }
904dba0c291SJonathan Doman                 else if (key == "PowerLimit")
905dba0c291SJonathan Doman                 {
906dba0c291SJonathan Doman                     const uint32_t* tdp = std::get_if<uint32_t>(&variant);
907dba0c291SJonathan Doman                     if (tdp != nullptr)
908dba0c291SJonathan Doman                     {
909dba0c291SJonathan Doman                         json["TDPWatts"] = *tdp;
910dba0c291SJonathan Doman                     }
911dba0c291SJonathan Doman                 }
912dba0c291SJonathan Doman                 else if (key == "TurboProfile")
913dba0c291SJonathan Doman                 {
914dba0c291SJonathan Doman                     const auto* turboList =
915dba0c291SJonathan Doman                         std::get_if<TurboProfileProperty>(&variant);
916dba0c291SJonathan Doman                     if (turboList == nullptr)
917dba0c291SJonathan Doman                     {
918dba0c291SJonathan Doman                         continue;
919dba0c291SJonathan Doman                     }
920dba0c291SJonathan Doman 
921dba0c291SJonathan Doman                     nlohmann::json& turboArray = json["TurboProfile"];
922dba0c291SJonathan Doman                     turboArray = nlohmann::json::array();
923dba0c291SJonathan Doman                     for (const auto& [turboSpeed, coreCount] : *turboList)
924dba0c291SJonathan Doman                     {
925dba0c291SJonathan Doman                         turboArray.push_back({{"ActiveCoreCount", coreCount},
926dba0c291SJonathan Doman                                               {"MaxSpeedMHz", turboSpeed}});
927dba0c291SJonathan Doman                     }
928dba0c291SJonathan Doman                 }
929dba0c291SJonathan Doman                 else if (key == "BaseSpeedPrioritySettings")
930dba0c291SJonathan Doman                 {
931dba0c291SJonathan Doman                     const auto* baseSpeedList =
932dba0c291SJonathan Doman                         std::get_if<BaseSpeedPrioritySettingsProperty>(
933dba0c291SJonathan Doman                             &variant);
934dba0c291SJonathan Doman                     if (baseSpeedList == nullptr)
935dba0c291SJonathan Doman                     {
936dba0c291SJonathan Doman                         continue;
937dba0c291SJonathan Doman                     }
938dba0c291SJonathan Doman 
939dba0c291SJonathan Doman                     nlohmann::json& baseSpeedArray =
940dba0c291SJonathan Doman                         json["BaseSpeedPrioritySettings"];
941dba0c291SJonathan Doman                     baseSpeedArray = nlohmann::json::array();
942dba0c291SJonathan Doman                     for (const auto& [baseSpeed, coreList] : *baseSpeedList)
943dba0c291SJonathan Doman                     {
944dba0c291SJonathan Doman                         baseSpeedArray.push_back(
945dba0c291SJonathan Doman                             {{"CoreCount", coreList.size()},
946dba0c291SJonathan Doman                              {"CoreIDs", coreList},
947dba0c291SJonathan Doman                              {"BaseSpeedMHz", baseSpeed}});
948dba0c291SJonathan Doman                     }
949dba0c291SJonathan Doman                 }
950dba0c291SJonathan Doman             }
951dba0c291SJonathan Doman         },
952dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
953dba0c291SJonathan Doman         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
954dba0c291SJonathan Doman }
955dba0c291SJonathan Doman 
9563cde86f1SJonathan Doman /**
9573cde86f1SJonathan Doman  * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
9583cde86f1SJonathan Doman  * property. Main task is to translate error messages into Redfish errors.
9593cde86f1SJonathan Doman  *
9603cde86f1SJonathan Doman  * @param[in,out]   resp    HTTP response.
9613cde86f1SJonathan Doman  * @param[in]       setPropVal  Value which we attempted to set.
9623cde86f1SJonathan Doman  * @param[in]       ec      D-Bus response error code.
9633cde86f1SJonathan Doman  * @param[in]       msg     D-Bus response message.
9643cde86f1SJonathan Doman  */
9653cde86f1SJonathan Doman inline void
9663cde86f1SJonathan Doman     handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
9673cde86f1SJonathan Doman                                 const std::string& setPropVal,
9683cde86f1SJonathan Doman                                 boost::system::error_code ec,
9693cde86f1SJonathan Doman                                 const sdbusplus::message::message& msg)
9703cde86f1SJonathan Doman {
9713cde86f1SJonathan Doman     if (!ec)
9723cde86f1SJonathan Doman     {
9733cde86f1SJonathan Doman         BMCWEB_LOG_DEBUG << "Set Property succeeded";
9743cde86f1SJonathan Doman         return;
9753cde86f1SJonathan Doman     }
9763cde86f1SJonathan Doman 
9773cde86f1SJonathan Doman     BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
9783cde86f1SJonathan Doman 
9793cde86f1SJonathan Doman     const sd_bus_error* dbusError = msg.get_error();
9803cde86f1SJonathan Doman     if (dbusError == nullptr)
9813cde86f1SJonathan Doman     {
9823cde86f1SJonathan Doman         messages::internalError(resp->res);
9833cde86f1SJonathan Doman         return;
9843cde86f1SJonathan Doman     }
9853cde86f1SJonathan Doman 
9863cde86f1SJonathan Doman     // The asio error code doesn't know about our custom errors, so we have to
9873cde86f1SJonathan Doman     // parse the error string. Some of these D-Bus -> Redfish translations are a
9883cde86f1SJonathan Doman     // stretch, but it's good to try to communicate something vaguely useful.
9893cde86f1SJonathan Doman     if (strcmp(dbusError->name,
9903cde86f1SJonathan Doman                "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
9913cde86f1SJonathan Doman     {
9923cde86f1SJonathan Doman         // Service did not like the object_path we tried to set.
9933cde86f1SJonathan Doman         messages::propertyValueIncorrect(
9943cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
9953cde86f1SJonathan Doman     }
9963cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9973cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
9983cde86f1SJonathan Doman     {
9993cde86f1SJonathan Doman         // Service indicates we can never change the config for this processor.
10003cde86f1SJonathan Doman         messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
10013cde86f1SJonathan Doman     }
10023cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
10033cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.Unavailable") == 0)
10043cde86f1SJonathan Doman     {
10053cde86f1SJonathan Doman         // Service indicates the config cannot be changed right now, but maybe
10063cde86f1SJonathan Doman         // in a different system state.
10073cde86f1SJonathan Doman         messages::resourceInStandby(resp->res);
10083cde86f1SJonathan Doman     }
10093cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
10103cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Device.Error.WriteFailure") ==
10113cde86f1SJonathan Doman              0)
10123cde86f1SJonathan Doman     {
10133cde86f1SJonathan Doman         // Service tried to change the config, but it failed.
10143cde86f1SJonathan Doman         messages::operationFailed(resp->res);
10153cde86f1SJonathan Doman     }
10163cde86f1SJonathan Doman     else
10173cde86f1SJonathan Doman     {
10183cde86f1SJonathan Doman         messages::internalError(resp->res);
10193cde86f1SJonathan Doman     }
10203cde86f1SJonathan Doman }
10213cde86f1SJonathan Doman 
10223cde86f1SJonathan Doman /**
10233cde86f1SJonathan Doman  * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
10243cde86f1SJonathan Doman  * validation of the input data, and then set the D-Bus property.
10253cde86f1SJonathan Doman  *
10263cde86f1SJonathan Doman  * @param[in,out]   resp            Async HTTP response.
10273cde86f1SJonathan Doman  * @param[in]       processorId     Processor's Id.
10283cde86f1SJonathan Doman  * @param[in]       appliedConfigUri    New property value to apply.
10293cde86f1SJonathan Doman  * @param[in]       cpuObjectPath   Path of CPU object to modify.
10303cde86f1SJonathan Doman  * @param[in]       serviceMap      Service map for CPU object.
10313cde86f1SJonathan Doman  */
10323cde86f1SJonathan Doman inline void patchAppliedOperatingConfig(
10333cde86f1SJonathan Doman     const std::shared_ptr<bmcweb::AsyncResp>& resp,
10343cde86f1SJonathan Doman     const std::string& processorId, const std::string& appliedConfigUri,
10353cde86f1SJonathan Doman     const std::string& cpuObjectPath, const MapperServiceMap& serviceMap)
10363cde86f1SJonathan Doman {
10373cde86f1SJonathan Doman     // Check that the property even exists by checking for the interface
10383cde86f1SJonathan Doman     const std::string* controlService = nullptr;
10393cde86f1SJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
10403cde86f1SJonathan Doman     {
10413cde86f1SJonathan Doman         if (std::find(interfaceList.begin(), interfaceList.end(),
10423cde86f1SJonathan Doman                       "xyz.openbmc_project.Control.Processor."
10433cde86f1SJonathan Doman                       "CurrentOperatingConfig") != interfaceList.end())
10443cde86f1SJonathan Doman         {
10453cde86f1SJonathan Doman             controlService = &serviceName;
10463cde86f1SJonathan Doman             break;
10473cde86f1SJonathan Doman         }
10483cde86f1SJonathan Doman     }
10493cde86f1SJonathan Doman 
10503cde86f1SJonathan Doman     if (controlService == nullptr)
10513cde86f1SJonathan Doman     {
10523cde86f1SJonathan Doman         messages::internalError(resp->res);
10533cde86f1SJonathan Doman         return;
10543cde86f1SJonathan Doman     }
10553cde86f1SJonathan Doman 
10563cde86f1SJonathan Doman     // Check that the config URI is a child of the cpu URI being patched.
10573cde86f1SJonathan Doman     std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
10583cde86f1SJonathan Doman     expectedPrefix += processorId;
10593cde86f1SJonathan Doman     expectedPrefix += "/OperatingConfigs/";
10603cde86f1SJonathan Doman     if (!boost::starts_with(appliedConfigUri, expectedPrefix) ||
10613cde86f1SJonathan Doman         expectedPrefix.size() == appliedConfigUri.size())
10623cde86f1SJonathan Doman     {
10633cde86f1SJonathan Doman         messages::propertyValueIncorrect(
10643cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
10653cde86f1SJonathan Doman         return;
10663cde86f1SJonathan Doman     }
10673cde86f1SJonathan Doman 
10683cde86f1SJonathan Doman     // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
10693cde86f1SJonathan Doman     // direct child of the CPU object.
10703cde86f1SJonathan Doman     // Strip the expectedPrefix from the config URI to get the "filename", and
10713cde86f1SJonathan Doman     // append to the CPU's path.
10723cde86f1SJonathan Doman     std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
10733cde86f1SJonathan Doman     sdbusplus::message::object_path configPath(cpuObjectPath);
10743cde86f1SJonathan Doman     configPath /= configBaseName;
10753cde86f1SJonathan Doman 
10763cde86f1SJonathan Doman     BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
10773cde86f1SJonathan Doman 
10783cde86f1SJonathan Doman     // Set the property, with handler to check error responses
10793cde86f1SJonathan Doman     crow::connections::systemBus->async_method_call(
10803cde86f1SJonathan Doman         [resp, appliedConfigUri](boost::system::error_code ec,
10813cde86f1SJonathan Doman                                  sdbusplus::message::message& msg) {
10823cde86f1SJonathan Doman             handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
10833cde86f1SJonathan Doman         },
10843cde86f1SJonathan Doman         *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
10853cde86f1SJonathan Doman         "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
1086*168e20c1SEd Tanous         "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
10873cde86f1SJonathan Doman }
10883cde86f1SJonathan Doman 
10897e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfigCollection(App& app)
1090dba0c291SJonathan Doman {
1091dba0c291SJonathan Doman 
10927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
10937e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
1094ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfigCollection)
10950fda0f12SGeorge Liu         .methods(
10960fda0f12SGeorge Liu             boost::beast::http::verb::get)([](const crow::Request& req,
10970fda0f12SGeorge Liu                                               const std::shared_ptr<
10980fda0f12SGeorge Liu                                                   bmcweb::AsyncResp>& asyncResp,
10997e860f15SJohn Edward Broadbent                                               const std::string& cpuName) {
11008d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] =
1101dba0c291SJonathan Doman                 "#OperatingConfigCollection.OperatingConfigCollection";
11028d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] = req.url;
11030fda0f12SGeorge Liu             asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1104dba0c291SJonathan Doman 
11057e860f15SJohn Edward Broadbent             // First find the matching CPU object so we know how to
11067e860f15SJohn Edward Broadbent             // constrain our search for related Config objects.
1107dba0c291SJonathan Doman             crow::connections::systemBus->async_method_call(
11080fda0f12SGeorge Liu                 [asyncResp, cpuName](const boost::system::error_code ec,
1109dba0c291SJonathan Doman                                      const std::vector<std::string>& objects) {
1110dba0c291SJonathan Doman                     if (ec)
1111dba0c291SJonathan Doman                     {
1112dba0c291SJonathan Doman                         BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1113dba0c291SJonathan Doman                                            << ec.message();
1114dba0c291SJonathan Doman                         messages::internalError(asyncResp->res);
1115dba0c291SJonathan Doman                         return;
1116dba0c291SJonathan Doman                     }
1117dba0c291SJonathan Doman 
1118dba0c291SJonathan Doman                     for (const std::string& object : objects)
1119dba0c291SJonathan Doman                     {
1120dba0c291SJonathan Doman                         if (!boost::ends_with(object, cpuName))
1121dba0c291SJonathan Doman                         {
1122dba0c291SJonathan Doman                             continue;
1123dba0c291SJonathan Doman                         }
1124dba0c291SJonathan Doman 
11257e860f15SJohn Edward Broadbent                         // Not expected that there will be multiple matching
11267e860f15SJohn Edward Broadbent                         // CPU objects, but if there are just use the first
11277e860f15SJohn Edward Broadbent                         // one.
1128dba0c291SJonathan Doman 
11297e860f15SJohn Edward Broadbent                         // Use the common search routine to construct the
11307e860f15SJohn Edward Broadbent                         // Collection of all Config objects under this CPU.
1131dba0c291SJonathan Doman                         collection_util::getCollectionMembers(
1132dba0c291SJonathan Doman                             asyncResp,
11330fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/Processors/" + cpuName +
11340fda0f12SGeorge Liu                                 "/OperatingConfigs",
11350fda0f12SGeorge Liu                             {"xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"},
1136dba0c291SJonathan Doman                             object.c_str());
1137dba0c291SJonathan Doman                         return;
1138dba0c291SJonathan Doman                     }
1139dba0c291SJonathan Doman                 },
1140dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper",
1141dba0c291SJonathan Doman                 "/xyz/openbmc_project/object_mapper",
1142dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1143dba0c291SJonathan Doman                 "/xyz/openbmc_project/inventory", 0,
11447e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
11450fda0f12SGeorge Liu                     "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
11467e860f15SJohn Edward Broadbent         });
1147dba0c291SJonathan Doman }
1148dba0c291SJonathan Doman 
11497e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfig(App& app)
1150dba0c291SJonathan Doman {
11517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
11527e860f15SJohn Edward Broadbent         app,
11537e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
1154ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfig)
11557e860f15SJohn Edward Broadbent         .methods(
11567e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request& req,
11577e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
11587e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
11597e860f15SJohn Edward Broadbent                                               const std::string& cpuName,
11607e860f15SJohn Edward Broadbent                                               const std::string& configName) {
11617e860f15SJohn Edward Broadbent             // Ask for all objects implementing OperatingConfig so we can search
11627e860f15SJohn Edward Broadbent             // for one with a matching name
1163dba0c291SJonathan Doman             crow::connections::systemBus->async_method_call(
1164dba0c291SJonathan Doman                 [asyncResp, cpuName, configName,
1165dba0c291SJonathan Doman                  reqUrl{req.url}](boost::system::error_code ec,
1166dba0c291SJonathan Doman                                   const MapperGetSubTreeResponse& subtree) {
1167dba0c291SJonathan Doman                     if (ec)
1168dba0c291SJonathan Doman                     {
1169dba0c291SJonathan Doman                         BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1170dba0c291SJonathan Doman                                            << ec.message();
1171dba0c291SJonathan Doman                         messages::internalError(asyncResp->res);
1172dba0c291SJonathan Doman                         return;
1173dba0c291SJonathan Doman                     }
11747e860f15SJohn Edward Broadbent                     const std::string expectedEnding =
11757e860f15SJohn Edward Broadbent                         cpuName + '/' + configName;
1176dba0c291SJonathan Doman                     for (const auto& [objectPath, serviceMap] : subtree)
1177dba0c291SJonathan Doman                     {
1178dba0c291SJonathan Doman                         // Ignore any configs without matching cpuX/configY
1179dba0c291SJonathan Doman                         if (!boost::ends_with(objectPath, expectedEnding) ||
1180dba0c291SJonathan Doman                             serviceMap.empty())
1181dba0c291SJonathan Doman                         {
1182dba0c291SJonathan Doman                             continue;
1183dba0c291SJonathan Doman                         }
1184dba0c291SJonathan Doman 
1185dba0c291SJonathan Doman                         nlohmann::json& json = asyncResp->res.jsonValue;
1186dba0c291SJonathan Doman                         json["@odata.type"] =
1187dba0c291SJonathan Doman                             "#OperatingConfig.v1_0_0.OperatingConfig";
1188dba0c291SJonathan Doman                         json["@odata.id"] = reqUrl;
1189dba0c291SJonathan Doman                         json["Name"] = "Processor Profile";
1190dba0c291SJonathan Doman                         json["Id"] = configName;
1191dba0c291SJonathan Doman 
1192dba0c291SJonathan Doman                         // Just use the first implementation of the object - not
11937e860f15SJohn Edward Broadbent                         // expected that there would be multiple matching
11947e860f15SJohn Edward Broadbent                         // services
11957e860f15SJohn Edward Broadbent                         getOperatingConfigData(
11967e860f15SJohn Edward Broadbent                             asyncResp, serviceMap.begin()->first, objectPath);
1197dba0c291SJonathan Doman                         return;
1198dba0c291SJonathan Doman                     }
11997e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res,
12007e860f15SJohn Edward Broadbent                                                "OperatingConfig", configName);
1201dba0c291SJonathan Doman                 },
1202dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper",
1203dba0c291SJonathan Doman                 "/xyz/openbmc_project/object_mapper",
1204dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1205dba0c291SJonathan Doman                 "/xyz/openbmc_project/inventory", 0,
1206dba0c291SJonathan Doman                 std::array<const char*, 1>{
1207dba0c291SJonathan Doman                     "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
12087e860f15SJohn Edward Broadbent         });
1209ac6a4445SGunnar Mills }
1210ac6a4445SGunnar Mills 
12117e860f15SJohn Edward Broadbent inline void requestRoutesProcessorCollection(App& app)
12127e860f15SJohn Edward Broadbent {
1213ac6a4445SGunnar Mills     /**
1214ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1215ac6a4445SGunnar Mills      */
12167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
1217ed398213SEd Tanous         .privileges(redfish::privileges::getProcessorCollection)
12187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12197e860f15SJohn Edward Broadbent             [](const crow::Request&,
12207e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
12218d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
1222ac6a4445SGunnar Mills                     "#ProcessorCollection.ProcessorCollection";
12238d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Processor Collection";
1224ac6a4445SGunnar Mills 
12258d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
12268d1b46d7Szhanghch05                     "/redfish/v1/Systems/system/Processors";
1227ac6a4445SGunnar Mills 
122805030b8eSGunnar Mills                 collection_util::getCollectionMembers(
122905030b8eSGunnar Mills                     asyncResp, "/redfish/v1/Systems/system/Processors",
1230c951448aSJonathan Doman                     std::vector<const char*>(processorInterfaces.begin(),
1231c951448aSJonathan Doman                                              processorInterfaces.end()));
12327e860f15SJohn Edward Broadbent             });
1233ac6a4445SGunnar Mills }
1234ac6a4445SGunnar Mills 
12357e860f15SJohn Edward Broadbent inline void requestRoutesProcessor(App& app)
12367e860f15SJohn Edward Broadbent {
1237ac6a4445SGunnar Mills     /**
1238ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1239ac6a4445SGunnar Mills      */
12407e860f15SJohn Edward Broadbent 
12417e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1242ed398213SEd Tanous         .privileges(redfish::privileges::getProcessor)
12437e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12447e860f15SJohn Edward Broadbent             [](const crow::Request&,
12457e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12467e860f15SJohn Edward Broadbent                const std::string& processorId) {
12478d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
12488d1b46d7Szhanghch05                     "#Processor.v1_11_0.Processor";
12498d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
1250ac6a4445SGunnar Mills                     "/redfish/v1/Systems/system/Processors/" + processorId;
1251ac6a4445SGunnar Mills 
1252c951448aSJonathan Doman                 getProcessorObject(asyncResp, processorId, getProcessorData);
12537e860f15SJohn Edward Broadbent             });
12543cde86f1SJonathan Doman 
12557e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1256ed398213SEd Tanous         .privileges(redfish::privileges::patchProcessor)
12577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
12587e860f15SJohn Edward Broadbent             [](const crow::Request& req,
12597e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12607e860f15SJohn Edward Broadbent                const std::string& processorId) {
12613cde86f1SJonathan Doman                 std::optional<nlohmann::json> appliedConfigJson;
12627e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res,
12637e860f15SJohn Edward Broadbent                                          "AppliedOperatingConfig",
12643cde86f1SJonathan Doman                                          appliedConfigJson))
12653cde86f1SJonathan Doman                 {
12663cde86f1SJonathan Doman                     return;
12673cde86f1SJonathan Doman                 }
12683cde86f1SJonathan Doman 
12693cde86f1SJonathan Doman                 std::string appliedConfigUri;
12703cde86f1SJonathan Doman                 if (appliedConfigJson)
12713cde86f1SJonathan Doman                 {
12723cde86f1SJonathan Doman                     if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
12733cde86f1SJonathan Doman                                              "@odata.id", appliedConfigUri))
12743cde86f1SJonathan Doman                     {
12753cde86f1SJonathan Doman                         return;
12763cde86f1SJonathan Doman                     }
12777e860f15SJohn Edward Broadbent                     // Check for 404 and find matching D-Bus object, then run
12787e860f15SJohn Edward Broadbent                     // property patch handlers if that all succeeds.
12793cde86f1SJonathan Doman                     getProcessorObject(
12807e860f15SJohn Edward Broadbent                         asyncResp, processorId,
12813cde86f1SJonathan Doman                         [appliedConfigUri = std::move(appliedConfigUri)](
12823cde86f1SJonathan Doman                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12833cde86f1SJonathan Doman                             const std::string& processorId,
12843cde86f1SJonathan Doman                             const std::string& objectPath,
12853cde86f1SJonathan Doman                             const MapperServiceMap& serviceMap) {
12863cde86f1SJonathan Doman                             patchAppliedOperatingConfig(asyncResp, processorId,
12877e860f15SJohn Edward Broadbent                                                         appliedConfigUri,
12887e860f15SJohn Edward Broadbent                                                         objectPath, serviceMap);
12893cde86f1SJonathan Doman                         });
12903cde86f1SJonathan Doman                 }
12917e860f15SJohn Edward Broadbent             });
12923cde86f1SJonathan Doman }
1293ac6a4445SGunnar Mills 
1294ac6a4445SGunnar Mills } // namespace redfish
1295