xref: /openbmc/bmcweb/features/redfish/lib/processor.hpp (revision 49e429ca172365bd4ba16c924501321c73527cbe)
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>
22dba0c291SJonathan Doman #include <sdbusplus/message/native_types.hpp>
23dba0c291SJonathan Doman #include <sdbusplus/utility/dedup_variant.hpp>
24ac6a4445SGunnar Mills #include <utils/collection.hpp>
25ac6a4445SGunnar Mills #include <utils/json_utils.hpp>
26ac6a4445SGunnar Mills 
27ac6a4445SGunnar Mills namespace redfish
28ac6a4445SGunnar Mills {
29ac6a4445SGunnar Mills 
30ac6a4445SGunnar Mills using InterfacesProperties = boost::container::flat_map<
31ac6a4445SGunnar Mills     std::string,
32ac6a4445SGunnar Mills     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>>;
33ac6a4445SGunnar Mills 
34c951448aSJonathan Doman // Map of service name to list of interfaces
35c951448aSJonathan Doman using MapperServiceMap =
36c951448aSJonathan Doman     std::vector<std::pair<std::string, std::vector<std::string>>>;
37c951448aSJonathan Doman 
38c951448aSJonathan Doman // Map of object paths to MapperServiceMaps
39c951448aSJonathan Doman using MapperGetSubTreeResponse =
40c951448aSJonathan Doman     std::vector<std::pair<std::string, MapperServiceMap>>;
41c951448aSJonathan Doman 
42c951448aSJonathan Doman // Interfaces which imply a D-Bus object represents a Processor
43c951448aSJonathan Doman constexpr std::array<const char*, 2> processorInterfaces = {
44c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Cpu",
45c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Accelerator"};
462bab9831SJonathan Doman 
4771b82f26SSharad Yadav /**
4871b82f26SSharad Yadav  * @brief Fill out uuid info of a processor by
4971b82f26SSharad Yadav  * requesting data from the given D-Bus object.
5071b82f26SSharad Yadav  *
5171b82f26SSharad Yadav  * @param[in,out]   aResp       Async HTTP response.
5271b82f26SSharad Yadav  * @param[in]       service     D-Bus service to query.
5371b82f26SSharad Yadav  * @param[in]       objPath     D-Bus object to query.
5471b82f26SSharad Yadav  */
5571b82f26SSharad Yadav inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
5671b82f26SSharad Yadav                              const std::string& service,
5771b82f26SSharad Yadav                              const std::string& objPath)
5871b82f26SSharad Yadav {
5971b82f26SSharad Yadav     BMCWEB_LOG_DEBUG << "Get Processor UUID";
6071b82f26SSharad Yadav     crow::connections::systemBus->async_method_call(
6171b82f26SSharad Yadav         [objPath,
6271b82f26SSharad Yadav          aResp{std::move(aResp)}](const boost::system::error_code ec,
6371b82f26SSharad Yadav                                   const std::variant<std::string>& property) {
6471b82f26SSharad Yadav             if (ec)
6571b82f26SSharad Yadav             {
6671b82f26SSharad Yadav                 BMCWEB_LOG_DEBUG << "DBUS response error";
6771b82f26SSharad Yadav                 messages::internalError(aResp->res);
6871b82f26SSharad Yadav                 return;
6971b82f26SSharad Yadav             }
7071b82f26SSharad Yadav             const std::string* value = std::get_if<std::string>(&property);
7171b82f26SSharad Yadav             if (value == nullptr)
7271b82f26SSharad Yadav             {
7371b82f26SSharad Yadav                 BMCWEB_LOG_DEBUG << "Null value returned "
7471b82f26SSharad Yadav                                     "for UUID";
7571b82f26SSharad Yadav                 messages::internalError(aResp->res);
7671b82f26SSharad Yadav                 return;
7771b82f26SSharad Yadav             }
7871b82f26SSharad Yadav             aResp->res.jsonValue["UUID"] = *value;
7971b82f26SSharad Yadav         },
8071b82f26SSharad Yadav         service, objPath, "org.freedesktop.DBus.Properties", "Get",
8171b82f26SSharad Yadav         "xyz.openbmc_project.Common.UUID", "UUID");
8271b82f26SSharad Yadav }
8371b82f26SSharad Yadav 
84ac6a4445SGunnar Mills inline void
858d1b46d7Szhanghch05     getCpuDataByInterface(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
86ac6a4445SGunnar Mills                           const InterfacesProperties& cpuInterfacesProperties)
87ac6a4445SGunnar Mills {
88ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
89ac6a4445SGunnar Mills 
90a1649ec6SChicago Duan     // Set the default value of state
91a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["State"] = "Enabled";
92a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["Health"] = "OK";
93ac6a4445SGunnar Mills 
94ac6a4445SGunnar Mills     for (const auto& interface : cpuInterfacesProperties)
95ac6a4445SGunnar Mills     {
96ac6a4445SGunnar Mills         for (const auto& property : interface.second)
97ac6a4445SGunnar Mills         {
98a1649ec6SChicago Duan             if (property.first == "Present")
99ac6a4445SGunnar Mills             {
100a1649ec6SChicago Duan                 const bool* cpuPresent = std::get_if<bool>(&property.second);
101a1649ec6SChicago Duan                 if (cpuPresent == nullptr)
102ac6a4445SGunnar Mills                 {
103ac6a4445SGunnar Mills                     // Important property not in desired type
104ac6a4445SGunnar Mills                     messages::internalError(aResp->res);
105ac6a4445SGunnar Mills                     return;
106ac6a4445SGunnar Mills                 }
107a1649ec6SChicago Duan                 if (*cpuPresent == false)
108ac6a4445SGunnar Mills                 {
109a1649ec6SChicago Duan                     // Slot is not populated
110ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
111a1649ec6SChicago Duan                 }
112a1649ec6SChicago Duan             }
113a1649ec6SChicago Duan             else if (property.first == "Functional")
114a1649ec6SChicago Duan             {
115a1649ec6SChicago Duan                 const bool* cpuFunctional = std::get_if<bool>(&property.second);
116a1649ec6SChicago Duan                 if (cpuFunctional == nullptr)
117a1649ec6SChicago Duan                 {
118a1649ec6SChicago Duan                     messages::internalError(aResp->res);
119ac6a4445SGunnar Mills                     return;
120ac6a4445SGunnar Mills                 }
121a1649ec6SChicago Duan                 if (*cpuFunctional == false)
122a1649ec6SChicago Duan                 {
123a1649ec6SChicago Duan                     aResp->res.jsonValue["Status"]["Health"] = "Critical";
124a1649ec6SChicago Duan                 }
125a1649ec6SChicago Duan             }
126a1649ec6SChicago Duan             else if (property.first == "CoreCount")
127a1649ec6SChicago Duan             {
128a1649ec6SChicago Duan                 const uint16_t* coresCount =
129a1649ec6SChicago Duan                     std::get_if<uint16_t>(&property.second);
130a1649ec6SChicago Duan                 if (coresCount == nullptr)
131a1649ec6SChicago Duan                 {
132a1649ec6SChicago Duan                     messages::internalError(aResp->res);
133a1649ec6SChicago Duan                     return;
134a1649ec6SChicago Duan                 }
135ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = *coresCount;
136ac6a4445SGunnar Mills             }
137dc3fa667SJonathan Doman             else if (property.first == "MaxSpeedInMhz")
138dc3fa667SJonathan Doman             {
139dc3fa667SJonathan Doman                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
140dc3fa667SJonathan Doman                 if (value != nullptr)
141dc3fa667SJonathan Doman                 {
142dc3fa667SJonathan Doman                     aResp->res.jsonValue["MaxSpeedMHz"] = *value;
143dc3fa667SJonathan Doman                 }
144dc3fa667SJonathan Doman             }
145ac6a4445SGunnar Mills             else if (property.first == "Socket")
146ac6a4445SGunnar Mills             {
147ac6a4445SGunnar Mills                 const std::string* value =
148ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
149ac6a4445SGunnar Mills                 if (value != nullptr)
150ac6a4445SGunnar Mills                 {
151ac6a4445SGunnar Mills                     aResp->res.jsonValue["Socket"] = *value;
152ac6a4445SGunnar Mills                 }
153ac6a4445SGunnar Mills             }
154ac6a4445SGunnar Mills             else if (property.first == "ThreadCount")
155ac6a4445SGunnar Mills             {
156dc3fa667SJonathan Doman                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
157ac6a4445SGunnar Mills                 if (value != nullptr)
158ac6a4445SGunnar Mills                 {
159ac6a4445SGunnar Mills                     aResp->res.jsonValue["TotalThreads"] = *value;
160ac6a4445SGunnar Mills                 }
161ac6a4445SGunnar Mills             }
162ac6a4445SGunnar Mills             else if (property.first == "Family")
163ac6a4445SGunnar Mills             {
164ac6a4445SGunnar Mills                 const std::string* value =
165ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
166ac6a4445SGunnar Mills                 if (value != nullptr)
167ac6a4445SGunnar Mills                 {
168ac6a4445SGunnar Mills                     aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
169ac6a4445SGunnar Mills                         *value;
170ac6a4445SGunnar Mills                 }
171ac6a4445SGunnar Mills             }
172ac6a4445SGunnar Mills             else if (property.first == "Id")
173ac6a4445SGunnar Mills             {
174ac6a4445SGunnar Mills                 const uint64_t* value = std::get_if<uint64_t>(&property.second);
175ac6a4445SGunnar Mills                 if (value != nullptr && *value != 0)
176ac6a4445SGunnar Mills                 {
177ac6a4445SGunnar Mills                     aResp->res
178ac6a4445SGunnar Mills                         .jsonValue["ProcessorId"]["IdentificationRegisters"] =
179ac6a4445SGunnar Mills                         boost::lexical_cast<std::string>(*value);
180ac6a4445SGunnar Mills                 }
181ac6a4445SGunnar Mills             }
182ac6a4445SGunnar Mills         }
183ac6a4445SGunnar Mills     }
184ac6a4445SGunnar Mills 
185ac6a4445SGunnar Mills     return;
186ac6a4445SGunnar Mills }
187ac6a4445SGunnar Mills 
1888d1b46d7Szhanghch05 inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
189ac6a4445SGunnar Mills                                 const std::string& cpuId,
190ac6a4445SGunnar Mills                                 const std::string& service,
191ac6a4445SGunnar Mills                                 const std::string& objPath)
192ac6a4445SGunnar Mills {
193ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
194ac6a4445SGunnar Mills 
195ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
196ac6a4445SGunnar Mills         [cpuId, service, objPath, aResp{std::move(aResp)}](
197ac6a4445SGunnar Mills             const boost::system::error_code ec,
198ac6a4445SGunnar Mills             const dbus::utility::ManagedObjectType& dbusData) {
199ac6a4445SGunnar Mills             if (ec)
200ac6a4445SGunnar Mills             {
201ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
202ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
203ac6a4445SGunnar Mills                 return;
204ac6a4445SGunnar Mills             }
205ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = cpuId;
206ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
207ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "CPU";
208ac6a4445SGunnar Mills 
209ac6a4445SGunnar Mills             bool slotPresent = false;
210ac6a4445SGunnar Mills             std::string corePath = objPath + "/core";
211ac6a4445SGunnar Mills             size_t totalCores = 0;
212ac6a4445SGunnar Mills             for (const auto& object : dbusData)
213ac6a4445SGunnar Mills             {
214ac6a4445SGunnar Mills                 if (object.first.str == objPath)
215ac6a4445SGunnar Mills                 {
216ac6a4445SGunnar Mills                     getCpuDataByInterface(aResp, object.second);
217ac6a4445SGunnar Mills                 }
218ac6a4445SGunnar Mills                 else if (boost::starts_with(object.first.str, corePath))
219ac6a4445SGunnar Mills                 {
220ac6a4445SGunnar Mills                     for (const auto& interface : object.second)
221ac6a4445SGunnar Mills                     {
222ac6a4445SGunnar Mills                         if (interface.first ==
223ac6a4445SGunnar Mills                             "xyz.openbmc_project.Inventory.Item")
224ac6a4445SGunnar Mills                         {
225ac6a4445SGunnar Mills                             for (const auto& property : interface.second)
226ac6a4445SGunnar Mills                             {
227ac6a4445SGunnar Mills                                 if (property.first == "Present")
228ac6a4445SGunnar Mills                                 {
229ac6a4445SGunnar Mills                                     const bool* present =
230ac6a4445SGunnar Mills                                         std::get_if<bool>(&property.second);
231ac6a4445SGunnar Mills                                     if (present != nullptr)
232ac6a4445SGunnar Mills                                     {
233ac6a4445SGunnar Mills                                         if (*present == true)
234ac6a4445SGunnar Mills                                         {
235ac6a4445SGunnar Mills                                             slotPresent = true;
236ac6a4445SGunnar Mills                                             totalCores++;
237ac6a4445SGunnar Mills                                         }
238ac6a4445SGunnar Mills                                     }
239ac6a4445SGunnar Mills                                 }
240ac6a4445SGunnar Mills                             }
241ac6a4445SGunnar Mills                         }
242ac6a4445SGunnar Mills                     }
243ac6a4445SGunnar Mills                 }
244ac6a4445SGunnar Mills             }
245ac6a4445SGunnar Mills             // In getCpuDataByInterface(), state and health are set
246ac6a4445SGunnar Mills             // based on the present and functional status. If core
247ac6a4445SGunnar Mills             // count is zero, then it has a higher precedence.
248ac6a4445SGunnar Mills             if (slotPresent)
249ac6a4445SGunnar Mills             {
250ac6a4445SGunnar Mills                 if (totalCores == 0)
251ac6a4445SGunnar Mills                 {
252ac6a4445SGunnar Mills                     // Slot is not populated, set status end return
253ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
254ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["Health"] = "OK";
255ac6a4445SGunnar Mills                 }
256ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = totalCores;
257ac6a4445SGunnar Mills             }
258ac6a4445SGunnar Mills             return;
259ac6a4445SGunnar Mills         },
260ac6a4445SGunnar Mills         service, "/xyz/openbmc_project/inventory",
261ac6a4445SGunnar Mills         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
262ac6a4445SGunnar Mills }
263ac6a4445SGunnar Mills 
2648d1b46d7Szhanghch05 inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
265ac6a4445SGunnar Mills                             const std::string& service,
266ac6a4445SGunnar Mills                             const std::string& objPath)
267ac6a4445SGunnar Mills {
268ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
269ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
270ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
271ac6a4445SGunnar Mills             const boost::system::error_code ec,
272ac6a4445SGunnar Mills             const boost::container::flat_map<
273ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
274ac6a4445SGunnar Mills                                           bool>>& properties) {
275ac6a4445SGunnar Mills             if (ec)
276ac6a4445SGunnar Mills             {
277ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
278ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
279ac6a4445SGunnar Mills                 return;
280ac6a4445SGunnar Mills             }
281ac6a4445SGunnar Mills 
282ac6a4445SGunnar Mills             for (const auto& property : properties)
283ac6a4445SGunnar Mills             {
284ac6a4445SGunnar Mills                 if (property.first == "SerialNumber")
285ac6a4445SGunnar Mills                 {
286ac6a4445SGunnar Mills                     const std::string* sn =
287ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
288ac6a4445SGunnar Mills                     if (sn != nullptr && !sn->empty())
289ac6a4445SGunnar Mills                     {
290ac6a4445SGunnar Mills                         aResp->res.jsonValue["SerialNumber"] = *sn;
291ac6a4445SGunnar Mills                     }
292ac6a4445SGunnar Mills                 }
293ac6a4445SGunnar Mills                 else if (property.first == "Model")
294ac6a4445SGunnar Mills                 {
295ac6a4445SGunnar Mills                     const std::string* model =
296ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
297ac6a4445SGunnar Mills                     if (model != nullptr && !model->empty())
298ac6a4445SGunnar Mills                     {
299ac6a4445SGunnar Mills                         aResp->res.jsonValue["Model"] = *model;
300ac6a4445SGunnar Mills                     }
301ac6a4445SGunnar Mills                 }
302ac6a4445SGunnar Mills                 else if (property.first == "Manufacturer")
303ac6a4445SGunnar Mills                 {
304ac6a4445SGunnar Mills 
305ac6a4445SGunnar Mills                     const std::string* mfg =
306ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
307ac6a4445SGunnar Mills                     if (mfg != nullptr)
308ac6a4445SGunnar Mills                     {
309ac6a4445SGunnar Mills                         aResp->res.jsonValue["Manufacturer"] = *mfg;
310ac6a4445SGunnar Mills 
311ac6a4445SGunnar Mills                         // Otherwise would be unexpected.
312ac6a4445SGunnar Mills                         if (mfg->find("Intel") != std::string::npos)
313ac6a4445SGunnar Mills                         {
314ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
315ac6a4445SGunnar Mills                                 "x86";
316ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "x86-64";
317ac6a4445SGunnar Mills                         }
318ac6a4445SGunnar Mills                         else if (mfg->find("IBM") != std::string::npos)
319ac6a4445SGunnar Mills                         {
320ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
321ac6a4445SGunnar Mills                                 "Power";
322ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "PowerISA";
323ac6a4445SGunnar Mills                         }
324ac6a4445SGunnar Mills                     }
325ac6a4445SGunnar Mills                 }
326cba4f448SSunnySrivastava1984                 else if (property.first == "PartNumber")
327cba4f448SSunnySrivastava1984                 {
328cba4f448SSunnySrivastava1984                     const std::string* partNumber =
329cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
330cba4f448SSunnySrivastava1984 
331cba4f448SSunnySrivastava1984                     if (partNumber == nullptr)
332cba4f448SSunnySrivastava1984                     {
333cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
334cba4f448SSunnySrivastava1984                         return;
335cba4f448SSunnySrivastava1984                     }
336cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["PartNumber"] = *partNumber;
337cba4f448SSunnySrivastava1984                 }
338cba4f448SSunnySrivastava1984                 else if (property.first == "SparePartNumber")
339cba4f448SSunnySrivastava1984                 {
340cba4f448SSunnySrivastava1984                     const std::string* sparePartNumber =
341cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
342cba4f448SSunnySrivastava1984 
343cba4f448SSunnySrivastava1984                     if (sparePartNumber == nullptr)
344cba4f448SSunnySrivastava1984                     {
345cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
346cba4f448SSunnySrivastava1984                         return;
347cba4f448SSunnySrivastava1984                     }
348cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
349cba4f448SSunnySrivastava1984                 }
350ac6a4445SGunnar Mills             }
351ac6a4445SGunnar Mills         },
352ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
353ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Asset");
354ac6a4445SGunnar Mills }
355ac6a4445SGunnar Mills 
3568d1b46d7Szhanghch05 inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
357ac6a4445SGunnar Mills                                const std::string& service,
358ac6a4445SGunnar Mills                                const std::string& objPath)
359ac6a4445SGunnar Mills {
360ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
361ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
362ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
363ac6a4445SGunnar Mills             const boost::system::error_code ec,
364ac6a4445SGunnar Mills             const boost::container::flat_map<
365ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
366ac6a4445SGunnar Mills                                           bool>>& properties) {
367ac6a4445SGunnar Mills             if (ec)
368ac6a4445SGunnar Mills             {
369ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
370ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
371ac6a4445SGunnar Mills                 return;
372ac6a4445SGunnar Mills             }
373ac6a4445SGunnar Mills 
374ac6a4445SGunnar Mills             for (const auto& property : properties)
375ac6a4445SGunnar Mills             {
376ac6a4445SGunnar Mills                 if (property.first == "Version")
377ac6a4445SGunnar Mills                 {
378ac6a4445SGunnar Mills                     const std::string* ver =
379ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
380ac6a4445SGunnar Mills                     if (ver != nullptr)
381ac6a4445SGunnar Mills                     {
382ac6a4445SGunnar Mills                         aResp->res.jsonValue["Version"] = *ver;
383ac6a4445SGunnar Mills                     }
384ac6a4445SGunnar Mills                     break;
385ac6a4445SGunnar Mills                 }
386ac6a4445SGunnar Mills             }
387ac6a4445SGunnar Mills         },
388ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
389ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Revision");
390ac6a4445SGunnar Mills }
391ac6a4445SGunnar Mills 
3928d1b46d7Szhanghch05 inline void getAcceleratorDataByService(
3938d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
3948d1b46d7Szhanghch05     const std::string& service, const std::string& objPath)
395ac6a4445SGunnar Mills {
396ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG
397ac6a4445SGunnar Mills         << "Get available system Accelerator resources by service.";
398ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
399ac6a4445SGunnar Mills         [acclrtrId, aResp{std::move(aResp)}](
400ac6a4445SGunnar Mills             const boost::system::error_code ec,
401ac6a4445SGunnar Mills             const boost::container::flat_map<
402ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
403ac6a4445SGunnar Mills                                           bool>>& properties) {
404ac6a4445SGunnar Mills             if (ec)
405ac6a4445SGunnar Mills             {
406ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
407ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
408ac6a4445SGunnar Mills                 return;
409ac6a4445SGunnar Mills             }
410ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = acclrtrId;
411ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
412ac6a4445SGunnar Mills             const bool* accPresent = nullptr;
413ac6a4445SGunnar Mills             const bool* accFunctional = nullptr;
414ac6a4445SGunnar Mills 
415ac6a4445SGunnar Mills             for (const auto& property : properties)
416ac6a4445SGunnar Mills             {
417ac6a4445SGunnar Mills                 if (property.first == "Functional")
418ac6a4445SGunnar Mills                 {
419ac6a4445SGunnar Mills                     accFunctional = std::get_if<bool>(&property.second);
420ac6a4445SGunnar Mills                 }
421ac6a4445SGunnar Mills                 else if (property.first == "Present")
422ac6a4445SGunnar Mills                 {
423ac6a4445SGunnar Mills                     accPresent = std::get_if<bool>(&property.second);
424ac6a4445SGunnar Mills                 }
425ac6a4445SGunnar Mills             }
426ac6a4445SGunnar Mills 
427ac6a4445SGunnar Mills             std::string state = "Enabled";
428ac6a4445SGunnar Mills             std::string health = "OK";
429ac6a4445SGunnar Mills 
430ac6a4445SGunnar Mills             if (accPresent != nullptr && *accPresent == false)
431ac6a4445SGunnar Mills             {
432ac6a4445SGunnar Mills                 state = "Absent";
433ac6a4445SGunnar Mills             }
434ac6a4445SGunnar Mills 
435ac6a4445SGunnar Mills             if ((accFunctional != nullptr) && (*accFunctional == false))
436ac6a4445SGunnar Mills             {
437ac6a4445SGunnar Mills                 if (state == "Enabled")
438ac6a4445SGunnar Mills                 {
439ac6a4445SGunnar Mills                     health = "Critical";
440ac6a4445SGunnar Mills                 }
441ac6a4445SGunnar Mills             }
442ac6a4445SGunnar Mills 
443ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = state;
444ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = health;
445ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "Accelerator";
446ac6a4445SGunnar Mills         },
447ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
448ac6a4445SGunnar Mills }
449ac6a4445SGunnar Mills 
450dba0c291SJonathan Doman // OperatingConfig D-Bus Types
451dba0c291SJonathan Doman using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
452dba0c291SJonathan Doman using BaseSpeedPrioritySettingsProperty =
453dba0c291SJonathan Doman     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
454dba0c291SJonathan Doman // uint32_t and size_t may or may not be the same type, requiring a dedup'd
455dba0c291SJonathan Doman // variant
456dba0c291SJonathan Doman using OperatingConfigProperties = std::vector<std::pair<
457dba0c291SJonathan Doman     std::string,
458dba0c291SJonathan Doman     sdbusplus::utility::dedup_variant<uint32_t, size_t, TurboProfileProperty,
459dba0c291SJonathan Doman                                       BaseSpeedPrioritySettingsProperty>>>;
460dba0c291SJonathan Doman 
461dba0c291SJonathan Doman /**
462dba0c291SJonathan Doman  * Fill out the HighSpeedCoreIDs in a Processor resource from the given
463dba0c291SJonathan Doman  * OperatingConfig D-Bus property.
464dba0c291SJonathan Doman  *
465dba0c291SJonathan Doman  * @param[in,out]   aResp               Async HTTP response.
466dba0c291SJonathan Doman  * @param[in]       baseSpeedSettings   Full list of base speed priority groups,
467dba0c291SJonathan Doman  *                                      to use to determine the list of high
468dba0c291SJonathan Doman  *                                      speed cores.
469dba0c291SJonathan Doman  */
470dba0c291SJonathan Doman inline void highSpeedCoreIdsHandler(
4718d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
472dba0c291SJonathan Doman     const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
473dba0c291SJonathan Doman {
474dba0c291SJonathan Doman     // The D-Bus property does not indicate which bucket is the "high
475dba0c291SJonathan Doman     // priority" group, so let's discern that by looking for the one with
476dba0c291SJonathan Doman     // highest base frequency.
477dba0c291SJonathan Doman     auto highPriorityGroup = baseSpeedSettings.cend();
478dba0c291SJonathan Doman     uint32_t highestBaseSpeed = 0;
479dba0c291SJonathan Doman     for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
480dba0c291SJonathan Doman          ++it)
481dba0c291SJonathan Doman     {
482dba0c291SJonathan Doman         const uint32_t baseFreq = std::get<uint32_t>(*it);
483dba0c291SJonathan Doman         if (baseFreq > highestBaseSpeed)
484dba0c291SJonathan Doman         {
485dba0c291SJonathan Doman             highestBaseSpeed = baseFreq;
486dba0c291SJonathan Doman             highPriorityGroup = it;
487dba0c291SJonathan Doman         }
488dba0c291SJonathan Doman     }
489dba0c291SJonathan Doman 
490dba0c291SJonathan Doman     nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
491dba0c291SJonathan Doman     jsonCoreIds = nlohmann::json::array();
492dba0c291SJonathan Doman 
493dba0c291SJonathan Doman     // There may not be any entries in the D-Bus property, so only populate
494dba0c291SJonathan Doman     // if there was actually something there.
495dba0c291SJonathan Doman     if (highPriorityGroup != baseSpeedSettings.cend())
496dba0c291SJonathan Doman     {
497dba0c291SJonathan Doman         jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
498dba0c291SJonathan Doman     }
499dba0c291SJonathan Doman }
500dba0c291SJonathan Doman 
501dba0c291SJonathan Doman /**
502dba0c291SJonathan Doman  * Fill out OperatingConfig related items in a Processor resource by requesting
503dba0c291SJonathan Doman  * data from the given D-Bus object.
504dba0c291SJonathan Doman  *
505dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
506dba0c291SJonathan Doman  * @param[in]       cpuId       CPU D-Bus name.
507dba0c291SJonathan Doman  * @param[in]       service     D-Bus service to query.
508dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
509dba0c291SJonathan Doman  */
5108d1b46d7Szhanghch05 inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
511dba0c291SJonathan Doman                              const std::string& cpuId,
512dba0c291SJonathan Doman                              const std::string& service,
513dba0c291SJonathan Doman                              const std::string& objPath)
514dba0c291SJonathan Doman {
515dba0c291SJonathan Doman     BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
516dba0c291SJonathan Doman 
517dba0c291SJonathan Doman     // First, GetAll CurrentOperatingConfig properties on the object
518dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
519dba0c291SJonathan Doman         [aResp, cpuId, service](
520dba0c291SJonathan Doman             const boost::system::error_code ec,
521dba0c291SJonathan Doman             const std::vector<
522dba0c291SJonathan Doman                 std::pair<std::string,
523dba0c291SJonathan Doman                           std::variant<sdbusplus::message::object_path, bool>>>&
524dba0c291SJonathan Doman                 properties) {
525dba0c291SJonathan Doman             if (ec)
526dba0c291SJonathan Doman             {
527dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
528dba0c291SJonathan Doman                                    << ec.message();
529dba0c291SJonathan Doman                 messages::internalError(aResp->res);
530dba0c291SJonathan Doman                 return;
531dba0c291SJonathan Doman             }
532dba0c291SJonathan Doman 
533dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
534dba0c291SJonathan Doman 
535dba0c291SJonathan Doman             for (const auto& [dbusPropName, variantVal] : properties)
536dba0c291SJonathan Doman             {
537dba0c291SJonathan Doman                 if (dbusPropName == "AppliedConfig")
538dba0c291SJonathan Doman                 {
539dba0c291SJonathan Doman                     const sdbusplus::message::object_path* dbusPathWrapper =
540dba0c291SJonathan Doman                         std::get_if<sdbusplus::message::object_path>(
541dba0c291SJonathan Doman                             &variantVal);
542dba0c291SJonathan Doman                     if (dbusPathWrapper == nullptr)
543dba0c291SJonathan Doman                     {
544dba0c291SJonathan Doman                         continue;
545dba0c291SJonathan Doman                     }
546dba0c291SJonathan Doman 
547dba0c291SJonathan Doman                     const std::string& dbusPath = dbusPathWrapper->str;
548dba0c291SJonathan Doman                     std::string uri = "/redfish/v1/Systems/system/Processors/" +
549dba0c291SJonathan Doman                                       cpuId + "/OperatingConfigs";
550dba0c291SJonathan Doman                     json["OperatingConfigs"] = {{"@odata.id", uri}};
551dba0c291SJonathan Doman 
552dba0c291SJonathan Doman                     // Reuse the D-Bus config object name for the Redfish
553dba0c291SJonathan Doman                     // URI
554dba0c291SJonathan Doman                     size_t baseNamePos = dbusPath.rfind('/');
555dba0c291SJonathan Doman                     if (baseNamePos == std::string::npos ||
556dba0c291SJonathan Doman                         baseNamePos == (dbusPath.size() - 1))
557dba0c291SJonathan Doman                     {
558dba0c291SJonathan Doman                         // If the AppliedConfig was somehow not a valid path,
559dba0c291SJonathan Doman                         // skip adding any more properties, since everything
560dba0c291SJonathan Doman                         // else is tied to this applied config.
561dba0c291SJonathan Doman                         messages::internalError(aResp->res);
562dba0c291SJonathan Doman                         break;
563dba0c291SJonathan Doman                     }
564dba0c291SJonathan Doman                     uri += '/';
565dba0c291SJonathan Doman                     uri += dbusPath.substr(baseNamePos + 1);
566dba0c291SJonathan Doman                     json["AppliedOperatingConfig"] = {{"@odata.id", uri}};
567dba0c291SJonathan Doman 
568dba0c291SJonathan Doman                     // Once we found the current applied config, queue another
569dba0c291SJonathan Doman                     // request to read the base freq core ids out of that
570dba0c291SJonathan Doman                     // config.
571dba0c291SJonathan Doman                     crow::connections::systemBus->async_method_call(
572dba0c291SJonathan Doman                         [aResp](
573dba0c291SJonathan Doman                             const boost::system::error_code ec,
574dba0c291SJonathan Doman                             const std::variant<
575dba0c291SJonathan Doman                                 BaseSpeedPrioritySettingsProperty>& property) {
576dba0c291SJonathan Doman                             if (ec)
577dba0c291SJonathan Doman                             {
578dba0c291SJonathan Doman                                 BMCWEB_LOG_WARNING
579dba0c291SJonathan Doman                                     << "D-Bus Property Get error: " << ec;
580dba0c291SJonathan Doman                                 messages::internalError(aResp->res);
581dba0c291SJonathan Doman                                 return;
582dba0c291SJonathan Doman                             }
583dba0c291SJonathan Doman                             auto baseSpeedList =
584dba0c291SJonathan Doman                                 std::get_if<BaseSpeedPrioritySettingsProperty>(
585dba0c291SJonathan Doman                                     &property);
586dba0c291SJonathan Doman                             if (baseSpeedList != nullptr)
587dba0c291SJonathan Doman                             {
588dba0c291SJonathan Doman                                 highSpeedCoreIdsHandler(aResp, *baseSpeedList);
589dba0c291SJonathan Doman                             }
590dba0c291SJonathan Doman                         },
591dba0c291SJonathan Doman                         service, dbusPath, "org.freedesktop.DBus.Properties",
592dba0c291SJonathan Doman                         "Get",
593dba0c291SJonathan Doman                         "xyz.openbmc_project.Inventory.Item.Cpu."
594dba0c291SJonathan Doman                         "OperatingConfig",
595dba0c291SJonathan Doman                         "BaseSpeedPrioritySettings");
596dba0c291SJonathan Doman                 }
597dba0c291SJonathan Doman                 else if (dbusPropName == "BaseSpeedPriorityEnabled")
598dba0c291SJonathan Doman                 {
599dba0c291SJonathan Doman                     const bool* state = std::get_if<bool>(&variantVal);
600dba0c291SJonathan Doman                     if (state != nullptr)
601dba0c291SJonathan Doman                     {
602dba0c291SJonathan Doman                         json["BaseSpeedPriorityState"] =
603dba0c291SJonathan Doman                             *state ? "Enabled" : "Disabled";
604dba0c291SJonathan Doman                     }
605dba0c291SJonathan Doman                 }
606dba0c291SJonathan Doman             }
607dba0c291SJonathan Doman         },
608dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
609dba0c291SJonathan Doman         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig");
610dba0c291SJonathan Doman }
611dba0c291SJonathan Doman 
612cba4f448SSunnySrivastava1984 /**
613cba4f448SSunnySrivastava1984  * @brief Fill out location info of a processor by
614cba4f448SSunnySrivastava1984  * requesting data from the given D-Bus object.
615cba4f448SSunnySrivastava1984  *
616cba4f448SSunnySrivastava1984  * @param[in,out]   aResp       Async HTTP response.
617cba4f448SSunnySrivastava1984  * @param[in]       service     D-Bus service to query.
618cba4f448SSunnySrivastava1984  * @param[in]       objPath     D-Bus object to query.
619cba4f448SSunnySrivastava1984  */
6208d1b46d7Szhanghch05 inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
621cba4f448SSunnySrivastava1984                                const std::string& service,
622cba4f448SSunnySrivastava1984                                const std::string& objPath)
623cba4f448SSunnySrivastava1984 {
624cba4f448SSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
625cba4f448SSunnySrivastava1984     crow::connections::systemBus->async_method_call(
626cba4f448SSunnySrivastava1984         [objPath,
627cba4f448SSunnySrivastava1984          aResp{std::move(aResp)}](const boost::system::error_code ec,
628cba4f448SSunnySrivastava1984                                   const std::variant<std::string>& property) {
629cba4f448SSunnySrivastava1984             if (ec)
630cba4f448SSunnySrivastava1984             {
631cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error";
632cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
633cba4f448SSunnySrivastava1984                 return;
634cba4f448SSunnySrivastava1984             }
635cba4f448SSunnySrivastava1984 
636cba4f448SSunnySrivastava1984             const std::string* value = std::get_if<std::string>(&property);
637cba4f448SSunnySrivastava1984 
638cba4f448SSunnySrivastava1984             if (value == nullptr)
639cba4f448SSunnySrivastava1984             {
640cba4f448SSunnySrivastava1984                 // illegal value
641cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "Location code value error";
642cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
643cba4f448SSunnySrivastava1984                 return;
644cba4f448SSunnySrivastava1984             }
645cba4f448SSunnySrivastava1984 
646cba4f448SSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
647cba4f448SSunnySrivastava1984                 *value;
648cba4f448SSunnySrivastava1984         },
649cba4f448SSunnySrivastava1984         service, objPath, "org.freedesktop.DBus.Properties", "Get",
650cba4f448SSunnySrivastava1984         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode");
651cba4f448SSunnySrivastava1984 }
652cba4f448SSunnySrivastava1984 
653c951448aSJonathan Doman /**
654*49e429caSJonathan Doman  * Populate the unique identifier in a Processor resource by requesting data
655*49e429caSJonathan Doman  * from the given D-Bus object.
656*49e429caSJonathan Doman  *
657*49e429caSJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
658*49e429caSJonathan Doman  * @param[in]       service     D-Bus service to query.
659*49e429caSJonathan Doman  * @param[in]       objPath     D-Bus object to query.
660*49e429caSJonathan Doman  */
661*49e429caSJonathan Doman inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
662*49e429caSJonathan Doman                            const std::string& service,
663*49e429caSJonathan Doman                            const std::string& objectPath)
664*49e429caSJonathan Doman {
665*49e429caSJonathan Doman     BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
666*49e429caSJonathan Doman     crow::connections::systemBus->async_method_call(
667*49e429caSJonathan Doman         [aResp](boost::system::error_code ec,
668*49e429caSJonathan Doman                 const std::variant<std::string>& property) {
669*49e429caSJonathan Doman             const std::string* id = std::get_if<std::string>(&property);
670*49e429caSJonathan Doman             if (ec || id == nullptr)
671*49e429caSJonathan Doman             {
672*49e429caSJonathan Doman                 BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
673*49e429caSJonathan Doman                 messages::internalError(aResp->res);
674*49e429caSJonathan Doman                 return;
675*49e429caSJonathan Doman             }
676*49e429caSJonathan Doman             aResp->res
677*49e429caSJonathan Doman                 .jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
678*49e429caSJonathan Doman                 *id;
679*49e429caSJonathan Doman         },
680*49e429caSJonathan Doman         service, objectPath, "org.freedesktop.DBus.Properties", "Get",
681*49e429caSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
682*49e429caSJonathan Doman         "UniqueIdentifier");
683*49e429caSJonathan Doman }
684*49e429caSJonathan Doman 
685*49e429caSJonathan Doman /**
686c951448aSJonathan Doman  * Find the D-Bus object representing the requested Processor, and call the
687c951448aSJonathan Doman  * handler with the results. If matching object is not found, add 404 error to
688c951448aSJonathan Doman  * response and don't call the handler.
689c951448aSJonathan Doman  *
690c951448aSJonathan Doman  * @param[in,out]   resp            Async HTTP response.
691c951448aSJonathan Doman  * @param[in]       processorId     Redfish Processor Id.
692c951448aSJonathan Doman  * @param[in]       handler         Callback to continue processing request upon
693c951448aSJonathan Doman  *                                  successfully finding object.
694c951448aSJonathan Doman  */
695c951448aSJonathan Doman template <typename Handler>
6968d1b46d7Szhanghch05 inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
697c951448aSJonathan Doman                                const std::string& processorId,
698c951448aSJonathan Doman                                Handler&& handler)
699ac6a4445SGunnar Mills {
700ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system processor resources.";
701ac6a4445SGunnar Mills 
702c951448aSJonathan Doman     // GetSubTree on all interfaces which provide info about a Processor
703ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
704c951448aSJonathan Doman         [resp, processorId, handler = std::forward<Handler>(handler)](
705c951448aSJonathan Doman             boost::system::error_code ec,
706c951448aSJonathan Doman             const MapperGetSubTreeResponse& subtree) mutable {
707ac6a4445SGunnar Mills             if (ec)
708ac6a4445SGunnar Mills             {
709c951448aSJonathan Doman                 BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
710c951448aSJonathan Doman                 messages::internalError(resp->res);
711ac6a4445SGunnar Mills                 return;
712ac6a4445SGunnar Mills             }
7132bab9831SJonathan Doman             for (const auto& [objectPath, serviceMap] : subtree)
714ac6a4445SGunnar Mills             {
7152bab9831SJonathan Doman                 // Ignore any objects which don't end with our desired cpu name
7162bab9831SJonathan Doman                 if (!boost::ends_with(objectPath, processorId))
717ac6a4445SGunnar Mills                 {
7182bab9831SJonathan Doman                     continue;
719ac6a4445SGunnar Mills                 }
7202bab9831SJonathan Doman 
721c951448aSJonathan Doman                 bool found = false;
722c951448aSJonathan Doman                 // Filter out objects that don't have the CPU-specific
723c951448aSJonathan Doman                 // interfaces to make sure we can return 404 on non-CPUs
724c951448aSJonathan Doman                 // (e.g. /redfish/../Processors/dimm0)
7252bab9831SJonathan Doman                 for (const auto& [serviceName, interfaceList] : serviceMap)
726ac6a4445SGunnar Mills                 {
727c951448aSJonathan Doman                     if (std::find_first_of(
728c951448aSJonathan Doman                             interfaceList.begin(), interfaceList.end(),
729c951448aSJonathan Doman                             processorInterfaces.begin(),
730c951448aSJonathan Doman                             processorInterfaces.end()) != interfaceList.end())
7312bab9831SJonathan Doman                     {
732c951448aSJonathan Doman                         found = true;
733c951448aSJonathan Doman                         break;
734c951448aSJonathan Doman                     }
735c951448aSJonathan Doman                 }
736c951448aSJonathan Doman 
737c951448aSJonathan Doman                 if (!found)
7382bab9831SJonathan Doman                 {
739c951448aSJonathan Doman                     continue;
740ac6a4445SGunnar Mills                 }
741c951448aSJonathan Doman 
742c951448aSJonathan Doman                 // Process the first object which does match our cpu name and
743c951448aSJonathan Doman                 // required interfaces, and potentially ignore any other
744c951448aSJonathan Doman                 // matching objects. Assume all interfaces we want to process
745c951448aSJonathan Doman                 // must be on the same object path.
746c951448aSJonathan Doman 
747c951448aSJonathan Doman                 handler(resp, processorId, objectPath, serviceMap);
748ac6a4445SGunnar Mills                 return;
749ac6a4445SGunnar Mills             }
750c951448aSJonathan Doman             messages::resourceNotFound(resp->res, "Processor", processorId);
751ac6a4445SGunnar Mills         },
752ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper",
753ac6a4445SGunnar Mills         "/xyz/openbmc_project/object_mapper",
754ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
7552bab9831SJonathan Doman         "/xyz/openbmc_project/inventory", 0,
756*49e429caSJonathan Doman         std::array<const char*, 8>{
75771b82f26SSharad Yadav             "xyz.openbmc_project.Common.UUID",
7582bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Asset",
7592bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Revision",
7602bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Cpu",
761cba4f448SSunnySrivastava1984             "xyz.openbmc_project.Inventory.Decorator.LocationCode",
762dba0c291SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Accelerator",
763*49e429caSJonathan Doman             "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
764*49e429caSJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"});
765ac6a4445SGunnar Mills }
766ac6a4445SGunnar Mills 
7678d1b46d7Szhanghch05 inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
768c951448aSJonathan Doman                              const std::string& processorId,
769c951448aSJonathan Doman                              const std::string& objectPath,
770c951448aSJonathan Doman                              const MapperServiceMap& serviceMap)
771c951448aSJonathan Doman {
772c951448aSJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
773c951448aSJonathan Doman     {
774c951448aSJonathan Doman         for (const auto& interface : interfaceList)
775c951448aSJonathan Doman         {
776c951448aSJonathan Doman             if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
777c951448aSJonathan Doman             {
778c951448aSJonathan Doman                 getCpuAssetData(aResp, serviceName, objectPath);
779c951448aSJonathan Doman             }
780c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory."
781c951448aSJonathan Doman                                   "Decorator.Revision")
782c951448aSJonathan Doman             {
783c951448aSJonathan Doman                 getCpuRevisionData(aResp, serviceName, objectPath);
784c951448aSJonathan Doman             }
785c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
786c951448aSJonathan Doman             {
787c951448aSJonathan Doman                 getCpuDataByService(aResp, processorId, serviceName,
788c951448aSJonathan Doman                                     objectPath);
789c951448aSJonathan Doman             }
790c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory."
791c951448aSJonathan Doman                                   "Item.Accelerator")
792c951448aSJonathan Doman             {
793c951448aSJonathan Doman                 getAcceleratorDataByService(aResp, processorId, serviceName,
794c951448aSJonathan Doman                                             objectPath);
795c951448aSJonathan Doman             }
796c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Control.Processor."
797c951448aSJonathan Doman                                   "CurrentOperatingConfig")
798c951448aSJonathan Doman             {
799c951448aSJonathan Doman                 getCpuConfigData(aResp, processorId, serviceName, objectPath);
800c951448aSJonathan Doman             }
801c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory."
802c951448aSJonathan Doman                                   "Decorator.LocationCode")
803c951448aSJonathan Doman             {
804c951448aSJonathan Doman                 getCpuLocationCode(aResp, serviceName, objectPath);
805c951448aSJonathan Doman             }
80671b82f26SSharad Yadav             else if (interface == "xyz.openbmc_project.Common.UUID")
80771b82f26SSharad Yadav             {
80871b82f26SSharad Yadav                 getProcessorUUID(aResp, serviceName, objectPath);
80971b82f26SSharad Yadav             }
810*49e429caSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory."
811*49e429caSJonathan Doman                                   "Decorator.UniqueIdentifier")
812*49e429caSJonathan Doman             {
813*49e429caSJonathan Doman                 getCpuUniqueId(aResp, serviceName, objectPath);
814*49e429caSJonathan Doman             }
815c951448aSJonathan Doman         }
816c951448aSJonathan Doman     }
817c951448aSJonathan Doman }
818c951448aSJonathan Doman 
819dba0c291SJonathan Doman /**
820dba0c291SJonathan Doman  * Request all the properties for the given D-Bus object and fill out the
821dba0c291SJonathan Doman  * related entries in the Redfish OperatingConfig response.
822dba0c291SJonathan Doman  *
823dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
824dba0c291SJonathan Doman  * @param[in]       service     D-Bus service name to query.
825dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
826dba0c291SJonathan Doman  */
8278d1b46d7Szhanghch05 inline void
8288d1b46d7Szhanghch05     getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
829dba0c291SJonathan Doman                            const std::string& service,
830dba0c291SJonathan Doman                            const std::string& objPath)
831dba0c291SJonathan Doman {
832dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
833dba0c291SJonathan Doman         [aResp](boost::system::error_code ec,
834dba0c291SJonathan Doman                 const OperatingConfigProperties& properties) {
835dba0c291SJonathan Doman             if (ec)
836dba0c291SJonathan Doman             {
837dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
838dba0c291SJonathan Doman                                    << ec.message();
839dba0c291SJonathan Doman                 messages::internalError(aResp->res);
840dba0c291SJonathan Doman                 return;
841dba0c291SJonathan Doman             }
842dba0c291SJonathan Doman 
843dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
844dba0c291SJonathan Doman             for (const auto& [key, variant] : properties)
845dba0c291SJonathan Doman             {
846dba0c291SJonathan Doman                 if (key == "AvailableCoreCount")
847dba0c291SJonathan Doman                 {
848dba0c291SJonathan Doman                     const size_t* cores = std::get_if<size_t>(&variant);
849dba0c291SJonathan Doman                     if (cores != nullptr)
850dba0c291SJonathan Doman                     {
851dba0c291SJonathan Doman                         json["TotalAvailableCoreCount"] = *cores;
852dba0c291SJonathan Doman                     }
853dba0c291SJonathan Doman                 }
854dba0c291SJonathan Doman                 else if (key == "BaseSpeed")
855dba0c291SJonathan Doman                 {
856dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
857dba0c291SJonathan Doman                     if (speed != nullptr)
858dba0c291SJonathan Doman                     {
859dba0c291SJonathan Doman                         json["BaseSpeedMHz"] = *speed;
860dba0c291SJonathan Doman                     }
861dba0c291SJonathan Doman                 }
862dba0c291SJonathan Doman                 else if (key == "MaxJunctionTemperature")
863dba0c291SJonathan Doman                 {
864dba0c291SJonathan Doman                     const uint32_t* temp = std::get_if<uint32_t>(&variant);
865dba0c291SJonathan Doman                     if (temp != nullptr)
866dba0c291SJonathan Doman                     {
867dba0c291SJonathan Doman                         json["MaxJunctionTemperatureCelsius"] = *temp;
868dba0c291SJonathan Doman                     }
869dba0c291SJonathan Doman                 }
870dba0c291SJonathan Doman                 else if (key == "MaxSpeed")
871dba0c291SJonathan Doman                 {
872dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
873dba0c291SJonathan Doman                     if (speed != nullptr)
874dba0c291SJonathan Doman                     {
875dba0c291SJonathan Doman                         json["MaxSpeedMHz"] = *speed;
876dba0c291SJonathan Doman                     }
877dba0c291SJonathan Doman                 }
878dba0c291SJonathan Doman                 else if (key == "PowerLimit")
879dba0c291SJonathan Doman                 {
880dba0c291SJonathan Doman                     const uint32_t* tdp = std::get_if<uint32_t>(&variant);
881dba0c291SJonathan Doman                     if (tdp != nullptr)
882dba0c291SJonathan Doman                     {
883dba0c291SJonathan Doman                         json["TDPWatts"] = *tdp;
884dba0c291SJonathan Doman                     }
885dba0c291SJonathan Doman                 }
886dba0c291SJonathan Doman                 else if (key == "TurboProfile")
887dba0c291SJonathan Doman                 {
888dba0c291SJonathan Doman                     const auto* turboList =
889dba0c291SJonathan Doman                         std::get_if<TurboProfileProperty>(&variant);
890dba0c291SJonathan Doman                     if (turboList == nullptr)
891dba0c291SJonathan Doman                     {
892dba0c291SJonathan Doman                         continue;
893dba0c291SJonathan Doman                     }
894dba0c291SJonathan Doman 
895dba0c291SJonathan Doman                     nlohmann::json& turboArray = json["TurboProfile"];
896dba0c291SJonathan Doman                     turboArray = nlohmann::json::array();
897dba0c291SJonathan Doman                     for (const auto& [turboSpeed, coreCount] : *turboList)
898dba0c291SJonathan Doman                     {
899dba0c291SJonathan Doman                         turboArray.push_back({{"ActiveCoreCount", coreCount},
900dba0c291SJonathan Doman                                               {"MaxSpeedMHz", turboSpeed}});
901dba0c291SJonathan Doman                     }
902dba0c291SJonathan Doman                 }
903dba0c291SJonathan Doman                 else if (key == "BaseSpeedPrioritySettings")
904dba0c291SJonathan Doman                 {
905dba0c291SJonathan Doman                     const auto* baseSpeedList =
906dba0c291SJonathan Doman                         std::get_if<BaseSpeedPrioritySettingsProperty>(
907dba0c291SJonathan Doman                             &variant);
908dba0c291SJonathan Doman                     if (baseSpeedList == nullptr)
909dba0c291SJonathan Doman                     {
910dba0c291SJonathan Doman                         continue;
911dba0c291SJonathan Doman                     }
912dba0c291SJonathan Doman 
913dba0c291SJonathan Doman                     nlohmann::json& baseSpeedArray =
914dba0c291SJonathan Doman                         json["BaseSpeedPrioritySettings"];
915dba0c291SJonathan Doman                     baseSpeedArray = nlohmann::json::array();
916dba0c291SJonathan Doman                     for (const auto& [baseSpeed, coreList] : *baseSpeedList)
917dba0c291SJonathan Doman                     {
918dba0c291SJonathan Doman                         baseSpeedArray.push_back(
919dba0c291SJonathan Doman                             {{"CoreCount", coreList.size()},
920dba0c291SJonathan Doman                              {"CoreIDs", coreList},
921dba0c291SJonathan Doman                              {"BaseSpeedMHz", baseSpeed}});
922dba0c291SJonathan Doman                     }
923dba0c291SJonathan Doman                 }
924dba0c291SJonathan Doman             }
925dba0c291SJonathan Doman         },
926dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
927dba0c291SJonathan Doman         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
928dba0c291SJonathan Doman }
929dba0c291SJonathan Doman 
9303cde86f1SJonathan Doman /**
9313cde86f1SJonathan Doman  * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
9323cde86f1SJonathan Doman  * property. Main task is to translate error messages into Redfish errors.
9333cde86f1SJonathan Doman  *
9343cde86f1SJonathan Doman  * @param[in,out]   resp    HTTP response.
9353cde86f1SJonathan Doman  * @param[in]       setPropVal  Value which we attempted to set.
9363cde86f1SJonathan Doman  * @param[in]       ec      D-Bus response error code.
9373cde86f1SJonathan Doman  * @param[in]       msg     D-Bus response message.
9383cde86f1SJonathan Doman  */
9393cde86f1SJonathan Doman inline void
9403cde86f1SJonathan Doman     handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
9413cde86f1SJonathan Doman                                 const std::string& setPropVal,
9423cde86f1SJonathan Doman                                 boost::system::error_code ec,
9433cde86f1SJonathan Doman                                 const sdbusplus::message::message& msg)
9443cde86f1SJonathan Doman {
9453cde86f1SJonathan Doman     if (!ec)
9463cde86f1SJonathan Doman     {
9473cde86f1SJonathan Doman         BMCWEB_LOG_DEBUG << "Set Property succeeded";
9483cde86f1SJonathan Doman         return;
9493cde86f1SJonathan Doman     }
9503cde86f1SJonathan Doman 
9513cde86f1SJonathan Doman     BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
9523cde86f1SJonathan Doman 
9533cde86f1SJonathan Doman     const sd_bus_error* dbusError = msg.get_error();
9543cde86f1SJonathan Doman     if (dbusError == nullptr)
9553cde86f1SJonathan Doman     {
9563cde86f1SJonathan Doman         messages::internalError(resp->res);
9573cde86f1SJonathan Doman         return;
9583cde86f1SJonathan Doman     }
9593cde86f1SJonathan Doman 
9603cde86f1SJonathan Doman     // The asio error code doesn't know about our custom errors, so we have to
9613cde86f1SJonathan Doman     // parse the error string. Some of these D-Bus -> Redfish translations are a
9623cde86f1SJonathan Doman     // stretch, but it's good to try to communicate something vaguely useful.
9633cde86f1SJonathan Doman     if (strcmp(dbusError->name,
9643cde86f1SJonathan Doman                "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
9653cde86f1SJonathan Doman     {
9663cde86f1SJonathan Doman         // Service did not like the object_path we tried to set.
9673cde86f1SJonathan Doman         messages::propertyValueIncorrect(
9683cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
9693cde86f1SJonathan Doman     }
9703cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9713cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
9723cde86f1SJonathan Doman     {
9733cde86f1SJonathan Doman         // Service indicates we can never change the config for this processor.
9743cde86f1SJonathan Doman         messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
9753cde86f1SJonathan Doman     }
9763cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9773cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.Unavailable") == 0)
9783cde86f1SJonathan Doman     {
9793cde86f1SJonathan Doman         // Service indicates the config cannot be changed right now, but maybe
9803cde86f1SJonathan Doman         // in a different system state.
9813cde86f1SJonathan Doman         messages::resourceInStandby(resp->res);
9823cde86f1SJonathan Doman     }
9833cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9843cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Device.Error.WriteFailure") ==
9853cde86f1SJonathan Doman              0)
9863cde86f1SJonathan Doman     {
9873cde86f1SJonathan Doman         // Service tried to change the config, but it failed.
9883cde86f1SJonathan Doman         messages::operationFailed(resp->res);
9893cde86f1SJonathan Doman     }
9903cde86f1SJonathan Doman     else
9913cde86f1SJonathan Doman     {
9923cde86f1SJonathan Doman         messages::internalError(resp->res);
9933cde86f1SJonathan Doman     }
9943cde86f1SJonathan Doman }
9953cde86f1SJonathan Doman 
9963cde86f1SJonathan Doman /**
9973cde86f1SJonathan Doman  * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
9983cde86f1SJonathan Doman  * validation of the input data, and then set the D-Bus property.
9993cde86f1SJonathan Doman  *
10003cde86f1SJonathan Doman  * @param[in,out]   resp            Async HTTP response.
10013cde86f1SJonathan Doman  * @param[in]       processorId     Processor's Id.
10023cde86f1SJonathan Doman  * @param[in]       appliedConfigUri    New property value to apply.
10033cde86f1SJonathan Doman  * @param[in]       cpuObjectPath   Path of CPU object to modify.
10043cde86f1SJonathan Doman  * @param[in]       serviceMap      Service map for CPU object.
10053cde86f1SJonathan Doman  */
10063cde86f1SJonathan Doman inline void patchAppliedOperatingConfig(
10073cde86f1SJonathan Doman     const std::shared_ptr<bmcweb::AsyncResp>& resp,
10083cde86f1SJonathan Doman     const std::string& processorId, const std::string& appliedConfigUri,
10093cde86f1SJonathan Doman     const std::string& cpuObjectPath, const MapperServiceMap& serviceMap)
10103cde86f1SJonathan Doman {
10113cde86f1SJonathan Doman     // Check that the property even exists by checking for the interface
10123cde86f1SJonathan Doman     const std::string* controlService = nullptr;
10133cde86f1SJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
10143cde86f1SJonathan Doman     {
10153cde86f1SJonathan Doman         if (std::find(interfaceList.begin(), interfaceList.end(),
10163cde86f1SJonathan Doman                       "xyz.openbmc_project.Control.Processor."
10173cde86f1SJonathan Doman                       "CurrentOperatingConfig") != interfaceList.end())
10183cde86f1SJonathan Doman         {
10193cde86f1SJonathan Doman             controlService = &serviceName;
10203cde86f1SJonathan Doman             break;
10213cde86f1SJonathan Doman         }
10223cde86f1SJonathan Doman     }
10233cde86f1SJonathan Doman 
10243cde86f1SJonathan Doman     if (controlService == nullptr)
10253cde86f1SJonathan Doman     {
10263cde86f1SJonathan Doman         messages::internalError(resp->res);
10273cde86f1SJonathan Doman         return;
10283cde86f1SJonathan Doman     }
10293cde86f1SJonathan Doman 
10303cde86f1SJonathan Doman     // Check that the config URI is a child of the cpu URI being patched.
10313cde86f1SJonathan Doman     std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
10323cde86f1SJonathan Doman     expectedPrefix += processorId;
10333cde86f1SJonathan Doman     expectedPrefix += "/OperatingConfigs/";
10343cde86f1SJonathan Doman     if (!boost::starts_with(appliedConfigUri, expectedPrefix) ||
10353cde86f1SJonathan Doman         expectedPrefix.size() == appliedConfigUri.size())
10363cde86f1SJonathan Doman     {
10373cde86f1SJonathan Doman         messages::propertyValueIncorrect(
10383cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
10393cde86f1SJonathan Doman         return;
10403cde86f1SJonathan Doman     }
10413cde86f1SJonathan Doman 
10423cde86f1SJonathan Doman     // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
10433cde86f1SJonathan Doman     // direct child of the CPU object.
10443cde86f1SJonathan Doman     // Strip the expectedPrefix from the config URI to get the "filename", and
10453cde86f1SJonathan Doman     // append to the CPU's path.
10463cde86f1SJonathan Doman     std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
10473cde86f1SJonathan Doman     sdbusplus::message::object_path configPath(cpuObjectPath);
10483cde86f1SJonathan Doman     configPath /= configBaseName;
10493cde86f1SJonathan Doman 
10503cde86f1SJonathan Doman     BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
10513cde86f1SJonathan Doman 
10523cde86f1SJonathan Doman     // Set the property, with handler to check error responses
10533cde86f1SJonathan Doman     crow::connections::systemBus->async_method_call(
10543cde86f1SJonathan Doman         [resp, appliedConfigUri](boost::system::error_code ec,
10553cde86f1SJonathan Doman                                  sdbusplus::message::message& msg) {
10563cde86f1SJonathan Doman             handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
10573cde86f1SJonathan Doman         },
10583cde86f1SJonathan Doman         *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
10593cde86f1SJonathan Doman         "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
10603cde86f1SJonathan Doman         "AppliedConfig",
10613cde86f1SJonathan Doman         std::variant<sdbusplus::message::object_path>(std::move(configPath)));
10623cde86f1SJonathan Doman }
10633cde86f1SJonathan Doman 
10647e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfigCollection(App& app)
1065dba0c291SJonathan Doman {
1066dba0c291SJonathan Doman 
10677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
10687e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
1069432a890cSEd Tanous         .privileges({{"Login"}})
10707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
10717e860f15SJohn Edward Broadbent             [](const crow::Request& req,
10727e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10737e860f15SJohn Edward Broadbent                const std::string& cpuName) {
10748d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
1075dba0c291SJonathan Doman                     "#OperatingConfigCollection.OperatingConfigCollection";
10768d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] = req.url;
10777e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
10787e860f15SJohn Edward Broadbent                     "Operating Config Collection";
1079dba0c291SJonathan Doman 
10807e860f15SJohn Edward Broadbent                 // First find the matching CPU object so we know how to
10817e860f15SJohn Edward Broadbent                 // constrain our search for related Config objects.
1082dba0c291SJonathan Doman                 crow::connections::systemBus->async_method_call(
10837e860f15SJohn Edward Broadbent                     [asyncResp,
10847e860f15SJohn Edward Broadbent                      cpuName](const boost::system::error_code ec,
1085dba0c291SJonathan Doman                               const std::vector<std::string>& objects) {
1086dba0c291SJonathan Doman                         if (ec)
1087dba0c291SJonathan Doman                         {
1088dba0c291SJonathan Doman                             BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1089dba0c291SJonathan Doman                                                << ec.message();
1090dba0c291SJonathan Doman                             messages::internalError(asyncResp->res);
1091dba0c291SJonathan Doman                             return;
1092dba0c291SJonathan Doman                         }
1093dba0c291SJonathan Doman 
1094dba0c291SJonathan Doman                         for (const std::string& object : objects)
1095dba0c291SJonathan Doman                         {
1096dba0c291SJonathan Doman                             if (!boost::ends_with(object, cpuName))
1097dba0c291SJonathan Doman                             {
1098dba0c291SJonathan Doman                                 continue;
1099dba0c291SJonathan Doman                             }
1100dba0c291SJonathan Doman 
11017e860f15SJohn Edward Broadbent                             // Not expected that there will be multiple matching
11027e860f15SJohn Edward Broadbent                             // CPU objects, but if there are just use the first
11037e860f15SJohn Edward Broadbent                             // one.
1104dba0c291SJonathan Doman 
11057e860f15SJohn Edward Broadbent                             // Use the common search routine to construct the
11067e860f15SJohn Edward Broadbent                             // Collection of all Config objects under this CPU.
1107dba0c291SJonathan Doman                             collection_util::getCollectionMembers(
1108dba0c291SJonathan Doman                                 asyncResp,
11097e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/Processors/" +
11107e860f15SJohn Edward Broadbent                                     cpuName + "/OperatingConfigs",
1111dba0c291SJonathan Doman                                 {"xyz.openbmc_project.Inventory.Item.Cpu."
1112dba0c291SJonathan Doman                                  "OperatingConfig"},
1113dba0c291SJonathan Doman                                 object.c_str());
1114dba0c291SJonathan Doman                             return;
1115dba0c291SJonathan Doman                         }
1116dba0c291SJonathan Doman                     },
1117dba0c291SJonathan Doman                     "xyz.openbmc_project.ObjectMapper",
1118dba0c291SJonathan Doman                     "/xyz/openbmc_project/object_mapper",
1119dba0c291SJonathan Doman                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
1120dba0c291SJonathan Doman                     "/xyz/openbmc_project/inventory", 0,
11217e860f15SJohn Edward Broadbent                     std::array<const char*, 1>{
11227e860f15SJohn Edward Broadbent                         "xyz.openbmc_project.Control.Processor."
1123dba0c291SJonathan Doman                         "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>/")
1132432a890cSEd Tanous         .privileges({{"Login"}})
11337e860f15SJohn Edward Broadbent         .methods(
11347e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request& req,
11357e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
11367e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
11377e860f15SJohn Edward Broadbent                                               const std::string& cpuName,
11387e860f15SJohn Edward Broadbent                                               const std::string& configName) {
11397e860f15SJohn Edward Broadbent             // Ask for all objects implementing OperatingConfig so we can search
11407e860f15SJohn Edward Broadbent             // for one with a matching name
1141dba0c291SJonathan Doman             crow::connections::systemBus->async_method_call(
1142dba0c291SJonathan Doman                 [asyncResp, cpuName, configName,
1143dba0c291SJonathan Doman                  reqUrl{req.url}](boost::system::error_code ec,
1144dba0c291SJonathan Doman                                   const MapperGetSubTreeResponse& subtree) {
1145dba0c291SJonathan Doman                     if (ec)
1146dba0c291SJonathan Doman                     {
1147dba0c291SJonathan Doman                         BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1148dba0c291SJonathan Doman                                            << ec.message();
1149dba0c291SJonathan Doman                         messages::internalError(asyncResp->res);
1150dba0c291SJonathan Doman                         return;
1151dba0c291SJonathan Doman                     }
11527e860f15SJohn Edward Broadbent                     const std::string expectedEnding =
11537e860f15SJohn Edward Broadbent                         cpuName + '/' + configName;
1154dba0c291SJonathan Doman                     for (const auto& [objectPath, serviceMap] : subtree)
1155dba0c291SJonathan Doman                     {
1156dba0c291SJonathan Doman                         // Ignore any configs without matching cpuX/configY
1157dba0c291SJonathan Doman                         if (!boost::ends_with(objectPath, expectedEnding) ||
1158dba0c291SJonathan Doman                             serviceMap.empty())
1159dba0c291SJonathan Doman                         {
1160dba0c291SJonathan Doman                             continue;
1161dba0c291SJonathan Doman                         }
1162dba0c291SJonathan Doman 
1163dba0c291SJonathan Doman                         nlohmann::json& json = asyncResp->res.jsonValue;
1164dba0c291SJonathan Doman                         json["@odata.type"] =
1165dba0c291SJonathan Doman                             "#OperatingConfig.v1_0_0.OperatingConfig";
1166dba0c291SJonathan Doman                         json["@odata.id"] = reqUrl;
1167dba0c291SJonathan Doman                         json["Name"] = "Processor Profile";
1168dba0c291SJonathan Doman                         json["Id"] = configName;
1169dba0c291SJonathan Doman 
1170dba0c291SJonathan Doman                         // Just use the first implementation of the object - not
11717e860f15SJohn Edward Broadbent                         // expected that there would be multiple matching
11727e860f15SJohn Edward Broadbent                         // services
11737e860f15SJohn Edward Broadbent                         getOperatingConfigData(
11747e860f15SJohn Edward Broadbent                             asyncResp, serviceMap.begin()->first, objectPath);
1175dba0c291SJonathan Doman                         return;
1176dba0c291SJonathan Doman                     }
11777e860f15SJohn Edward Broadbent                     messages::resourceNotFound(asyncResp->res,
11787e860f15SJohn Edward Broadbent                                                "OperatingConfig", configName);
1179dba0c291SJonathan Doman                 },
1180dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper",
1181dba0c291SJonathan Doman                 "/xyz/openbmc_project/object_mapper",
1182dba0c291SJonathan Doman                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1183dba0c291SJonathan Doman                 "/xyz/openbmc_project/inventory", 0,
1184dba0c291SJonathan Doman                 std::array<const char*, 1>{
1185dba0c291SJonathan Doman                     "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
11867e860f15SJohn Edward Broadbent         });
1187ac6a4445SGunnar Mills }
1188ac6a4445SGunnar Mills 
11897e860f15SJohn Edward Broadbent inline void requestRoutesProcessorCollection(App& app)
11907e860f15SJohn Edward Broadbent {
1191ac6a4445SGunnar Mills     /**
1192ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1193ac6a4445SGunnar Mills      */
11947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/")
1195432a890cSEd Tanous         .privileges({{"Login"}})
11967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11977e860f15SJohn Edward Broadbent             [](const crow::Request&,
11987e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
11998d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
1200ac6a4445SGunnar Mills                     "#ProcessorCollection.ProcessorCollection";
12018d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Processor Collection";
1202ac6a4445SGunnar Mills 
12038d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
12048d1b46d7Szhanghch05                     "/redfish/v1/Systems/system/Processors";
1205ac6a4445SGunnar Mills 
120605030b8eSGunnar Mills                 collection_util::getCollectionMembers(
120705030b8eSGunnar Mills                     asyncResp, "/redfish/v1/Systems/system/Processors",
1208c951448aSJonathan Doman                     std::vector<const char*>(processorInterfaces.begin(),
1209c951448aSJonathan Doman                                              processorInterfaces.end()));
12107e860f15SJohn Edward Broadbent             });
1211ac6a4445SGunnar Mills }
1212ac6a4445SGunnar Mills 
12137e860f15SJohn Edward Broadbent inline void requestRoutesProcessor(App& app)
12147e860f15SJohn Edward Broadbent {
1215ac6a4445SGunnar Mills     /**
1216ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1217ac6a4445SGunnar Mills      */
12187e860f15SJohn Edward Broadbent 
12197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1220432a890cSEd Tanous         .privileges({{"Login"}})
12217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12227e860f15SJohn Edward Broadbent             [](const crow::Request&,
12237e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12247e860f15SJohn Edward Broadbent                const std::string& processorId) {
12258d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
12268d1b46d7Szhanghch05                     "#Processor.v1_11_0.Processor";
12278d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
1228ac6a4445SGunnar Mills                     "/redfish/v1/Systems/system/Processors/" + processorId;
1229ac6a4445SGunnar Mills 
1230c951448aSJonathan Doman                 getProcessorObject(asyncResp, processorId, getProcessorData);
12317e860f15SJohn Edward Broadbent             });
12323cde86f1SJonathan Doman 
12337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Processors/<str>/")
1234432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
12357e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
12367e860f15SJohn Edward Broadbent             [](const crow::Request& req,
12377e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12387e860f15SJohn Edward Broadbent                const std::string& processorId) {
12393cde86f1SJonathan Doman                 std::optional<nlohmann::json> appliedConfigJson;
12407e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res,
12417e860f15SJohn Edward Broadbent                                          "AppliedOperatingConfig",
12423cde86f1SJonathan Doman                                          appliedConfigJson))
12433cde86f1SJonathan Doman                 {
12443cde86f1SJonathan Doman                     return;
12453cde86f1SJonathan Doman                 }
12463cde86f1SJonathan Doman 
12473cde86f1SJonathan Doman                 std::string appliedConfigUri;
12483cde86f1SJonathan Doman                 if (appliedConfigJson)
12493cde86f1SJonathan Doman                 {
12503cde86f1SJonathan Doman                     if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
12513cde86f1SJonathan Doman                                              "@odata.id", appliedConfigUri))
12523cde86f1SJonathan Doman                     {
12533cde86f1SJonathan Doman                         return;
12543cde86f1SJonathan Doman                     }
12557e860f15SJohn Edward Broadbent                     // Check for 404 and find matching D-Bus object, then run
12567e860f15SJohn Edward Broadbent                     // property patch handlers if that all succeeds.
12573cde86f1SJonathan Doman                     getProcessorObject(
12587e860f15SJohn Edward Broadbent                         asyncResp, processorId,
12593cde86f1SJonathan Doman                         [appliedConfigUri = std::move(appliedConfigUri)](
12603cde86f1SJonathan Doman                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12613cde86f1SJonathan Doman                             const std::string& processorId,
12623cde86f1SJonathan Doman                             const std::string& objectPath,
12633cde86f1SJonathan Doman                             const MapperServiceMap& serviceMap) {
12643cde86f1SJonathan Doman                             patchAppliedOperatingConfig(asyncResp, processorId,
12657e860f15SJohn Edward Broadbent                                                         appliedConfigUri,
12667e860f15SJohn Edward Broadbent                                                         objectPath, serviceMap);
12673cde86f1SJonathan Doman                         });
12683cde86f1SJonathan Doman                 }
12697e860f15SJohn Edward Broadbent             });
12703cde86f1SJonathan Doman }
1271ac6a4445SGunnar Mills 
1272ac6a4445SGunnar Mills } // namespace redfish
1273