xref: /openbmc/pldm/pldmtool/pldm_platform_cmd.cpp (revision 972aa95df96e4c78314e57c2dd1681f8c544daab)
1 #include "common/types.hpp"
2 #include "pldm_cmd_helper.hpp"
3 
4 #include <libpldm/entity.h>
5 #include <libpldm/platform.h>
6 #include <libpldm/state_set.h>
7 
8 #include <algorithm>
9 #include <cstddef>
10 #include <format>
11 #include <map>
12 #include <memory>
13 #include <ranges>
14 
15 #ifdef OEM_IBM
16 #include "oem/ibm/oem_ibm_state_set.hpp"
17 #endif
18 
19 using namespace pldm::utils;
20 
21 namespace pldmtool
22 {
23 namespace platform
24 {
25 namespace
26 {
27 using namespace pldmtool::helper;
28 
29 static const std::map<uint8_t, std::string> sensorPresState{
30     {PLDM_SENSOR_UNKNOWN, "Sensor Unknown"},
31     {PLDM_SENSOR_NORMAL, "Sensor Normal"},
32     {PLDM_SENSOR_WARNING, "Sensor Warning"},
33     {PLDM_SENSOR_CRITICAL, "Sensor Critical"},
34     {PLDM_SENSOR_FATAL, "Sensor Fatal"},
35     {PLDM_SENSOR_LOWERWARNING, "Sensor Lower Warning"},
36     {PLDM_SENSOR_LOWERCRITICAL, "Sensor Lower Critical"},
37     {PLDM_SENSOR_LOWERFATAL, "Sensor Lower Fatal"},
38     {PLDM_SENSOR_UPPERWARNING, "Sensor Upper Warning"},
39     {PLDM_SENSOR_UPPERCRITICAL, "Sensor Upper Critical"},
40     {PLDM_SENSOR_UPPERFATAL, "Sensor Upper Fatal"}};
41 
42 static const std::map<uint8_t, std::string> sensorOpState{
43     {PLDM_SENSOR_ENABLED, "Sensor Enabled"},
44     {PLDM_SENSOR_DISABLED, "Sensor Disabled"},
45     {PLDM_SENSOR_UNAVAILABLE, "Sensor Unavailable"},
46     {PLDM_SENSOR_STATUSUNKOWN, "Sensor Status Unknown"},
47     {PLDM_SENSOR_FAILED, "Sensor Failed"},
48     {PLDM_SENSOR_INITIALIZING, "Sensor Sensor Initializing"},
49     {PLDM_SENSOR_SHUTTINGDOWN, "Sensor Shutting down"},
50     {PLDM_SENSOR_INTEST, "Sensor Intest"}};
51 
52 const std::map<uint8_t, std::string> effecterOpState{
53     {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING,
54      "Effecter Enabled Update Pending"},
55     {EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING,
56      "Effecter Enabled No Update Pending"},
57     {EFFECTER_OPER_STATE_DISABLED, "Effecter Disabled"},
58     {EFFECTER_OPER_STATE_UNAVAILABLE, "Effecter Unavailable"},
59     {EFFECTER_OPER_STATE_STATUSUNKNOWN, "Effecter Status Unknown"},
60     {EFFECTER_OPER_STATE_FAILED, "Effecter Failed"},
61     {EFFECTER_OPER_STATE_INITIALIZING, "Effecter Initializing"},
62     {EFFECTER_OPER_STATE_SHUTTINGDOWN, "Effecter Shutting Down"},
63     {EFFECTER_OPER_STATE_INTEST, "Effecter In Test"}};
64 
getEffecterOpState(uint8_t state)65 std::string getEffecterOpState(uint8_t state)
66 {
67     return effecterOpState.contains(state) ? effecterOpState.at(state)
68                                            : std::to_string(state);
69 }
70 
71 std::vector<std::unique_ptr<CommandInterface>> commands;
72 
73 } // namespace
74 
75 using ordered_json = nlohmann::ordered_json;
76 
77 class GetEventReceiver : public CommandInterface
78 {
79   public:
80     ~GetEventReceiver() = default;
81     GetEventReceiver() = delete;
82     GetEventReceiver(const GetEventReceiver&) = delete;
83     GetEventReceiver(GetEventReceiver&&) = default;
84     GetEventReceiver& operator=(const GetEventReceiver&) = delete;
85     GetEventReceiver& operator=(GetEventReceiver&&) = delete;
86 
GetEventReceiver(const char * type,const char * name,CLI::App * app)87     explicit GetEventReceiver(const char* type, const char* name,
88                               CLI::App* app) : CommandInterface(type, name, app)
89     {}
createRequestMsg()90     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
91     {
92         std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
93         auto request = new (requestMsg.data()) pldm_msg;
94         auto rc =
95             encode_pldm_header_only(PLDM_REQUEST, instanceId, PLDM_PLATFORM,
96                                     PLDM_GET_EVENT_RECEIVER, request);
97         if (rc != PLDM_SUCCESS)
98         {
99             std::cerr << "Failed to encode_pldm_header_only, return code " << rc
100                       << std::endl;
101         }
102         return {rc, requestMsg};
103     }
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)104     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
105     {
106         struct pldm_get_event_receiver_resp event_receiver_resp_data;
107         auto rc = decode_get_event_receiver_resp(responsePtr, payloadLength,
108                                                  &event_receiver_resp_data);
109         if (rc || event_receiver_resp_data.completion_code)
110         {
111             std::cerr << "Response Message Error: "
112                       << "return code " << rc << ", completion code "
113                       << static_cast<int>(
114                              event_receiver_resp_data.completion_code)
115                       << std::endl;
116             return;
117         }
118         if (event_receiver_resp_data.transport_protocol_type !=
119             PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP)
120         {
121             std::cerr << "Unsupported response protocol type " << std::endl;
122             return;
123         }
124 
125         ordered_json data;
126         data["completionCode"] = event_receiver_resp_data.completion_code;
127         data["transportProtocolType"] =
128             event_receiver_resp_data.transport_protocol_type;
129         data["eventReceiverAddressInfo"] =
130             event_receiver_resp_data.event_receiver_address.mctp_eid;
131         pldmtool::helper::DisplayInJson(data);
132     }
133 };
134 
135 class GetPDR : public CommandInterface
136 {
137   public:
138     ~GetPDR() = default;
139     GetPDR() = delete;
140     GetPDR(const GetPDR&) = delete;
141     GetPDR(GetPDR&&) = default;
142     GetPDR& operator=(const GetPDR&) = delete;
143     GetPDR& operator=(GetPDR&&) = delete;
144 
145     using CommandInterface::CommandInterface;
146 
GetPDR(const char * type,const char * name,CLI::App * app)147     explicit GetPDR(const char* type, const char* name, CLI::App* app) :
148         CommandInterface(type, name, app), dataTransferHandle(0),
149         operationFlag(PLDM_GET_FIRSTPART), requestCount(UINT16_MAX),
150         recordChangeNumber(0), nextPartRequired(false)
151     {
152         auto pdrOptionGroup = app->add_option_group(
153             "Required Option",
154             "Retrieve individual PDR, all PDRs, PDRs of a requested type or retrieve all PDRs of the requested terminusID");
155         pdrOptionGroup->add_option(
156             "-d,--data", recordHandle,
157             "retrieve individual PDRs from a PDR Repository\n"
158             "eg: The recordHandle value for the PDR to be retrieved and 0 "
159             "means get first PDR in the repository.");
160         pdrRecType = "";
161         std::string supportedPDRTypes = "";
162 
163         for (const auto& [type, _] : strToPdrType)
164         {
165             supportedPDRTypes += (type + ", ");
166         }
167         supportedPDRTypes = std::format("[{}...]", supportedPDRTypes);
168 
169         pdrOptionGroup->add_option("-t, --type", pdrRecType,
170                                    "retrieve all PDRs of the requested type\n"
171                                    "supported types:\n" +
172                                        supportedPDRTypes);
173 
174         getPDRGroupOption = pdrOptionGroup->add_option(
175             "-i, --terminusID", pdrTerminus,
176             "retrieve all PDRs of the requested terminusID\n"
177             "supported IDs:\n [1, 2, 208...]");
178 
179         allPDRs = false;
180         pdrOptionGroup->add_flag("-a, --all", allPDRs,
181                                  "retrieve all PDRs from a PDR repository");
182 
183         pdrOptionGroup->require_option(1);
184     }
185 
parseGetPDROptions()186     void parseGetPDROptions()
187     {
188         optTIDSet = false;
189         if (getPDRGroupOption->count() > 0)
190         {
191             optTIDSet = true;
192             getPDRs();
193         }
194     }
195 
getPDRs()196     void getPDRs()
197     {
198         // start the array
199         std::cout << "[";
200 
201         recordHandle = 0;
202         do
203         {
204             CommandInterface::exec();
205         } while (recordHandle != 0);
206 
207         // close the array
208         std::cout << "]\n";
209 
210         if (handleFound)
211         {
212             recordHandle = 0;
213             uint32_t prevRecordHandle = 0;
214             do
215             {
216                 CommandInterface::exec();
217                 if (recordHandle == prevRecordHandle)
218                 {
219                     return;
220                 }
221                 prevRecordHandle = recordHandle;
222             } while (recordHandle != 0);
223         }
224     }
225 
exec()226     void exec() override
227     {
228         if (allPDRs || !pdrRecType.empty())
229         {
230             if (!pdrRecType.empty())
231             {
232                 std::transform(pdrRecType.begin(), pdrRecType.end(),
233                                pdrRecType.begin(), tolower);
234             }
235 
236             // start the array
237             std::cout << "[\n";
238 
239             // Retrieve all PDR records starting from the first
240             recordHandle = 0;
241             uint32_t prevRecordHandle = 0;
242             std::map<uint32_t, uint32_t> recordsSeen;
243             isFirstPDR = true;
244             do
245             {
246                 CommandInterface::exec();
247                 // recordHandle is updated to nextRecord when
248                 // CommandInterface::exec() is successful.
249                 // In case of any error, return.
250                 if (recordHandle == prevRecordHandle && !nextPartRequired)
251                 {
252                     return;
253                 }
254 
255                 // check for circular references.
256                 auto result =
257                     recordsSeen.emplace(recordHandle, prevRecordHandle);
258                 if (!result.second && !nextPartRequired)
259                 {
260                     std::cerr
261                         << "Record handle " << recordHandle
262                         << " has multiple references: " << result.first->second
263                         << ", " << prevRecordHandle << "\n";
264                     return;
265                 }
266                 prevRecordHandle = recordHandle;
267             } while (recordHandle != 0);
268 
269             // close the array
270             std::cout << "]\n";
271         }
272         else
273         {
274             do
275             {
276                 CommandInterface::exec();
277             } while (nextPartRequired);
278         }
279     }
280 
createRequestMsg()281     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
282     {
283         std::vector<uint8_t> requestMsg(
284             sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES);
285         auto request = new (requestMsg.data()) pldm_msg;
286 
287         auto rc = encode_get_pdr_req(
288             instanceId, recordHandle, dataTransferHandle, operationFlag,
289             requestCount, recordChangeNumber, request, PLDM_GET_PDR_REQ_BYTES);
290         return {rc, requestMsg};
291     }
292 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)293     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
294     {
295         uint8_t completionCode = 0;
296         uint8_t respRecordData[UINT16_MAX] = {0};
297         uint32_t nextRecordHndl = 0;
298         uint32_t nextDataTransferHndl = 0;
299         uint8_t transferFlag = 0;
300         uint16_t respCnt = 0;
301         uint8_t transferCRC = 0;
302 
303         auto rc = decode_get_pdr_resp(
304             responsePtr, payloadLength, &completionCode, &nextRecordHndl,
305             &nextDataTransferHndl, &transferFlag, &respCnt, respRecordData,
306             sizeof(respRecordData), &transferCRC);
307 
308         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
309         {
310             std::cerr << "Response Message Error: "
311                       << "rc=" << rc << ",cc=" << (int)completionCode
312                       << std::endl;
313             nextPartRequired = false;
314             return;
315         }
316 
317         if (optTIDSet && !handleFound)
318         {
319             terminusHandle = getTerminusHandle(respRecordData, pdrTerminus);
320             if (terminusHandle.has_value())
321             {
322                 recordHandle = 0;
323                 return;
324             }
325             else
326             {
327                 recordHandle = nextRecordHndl;
328                 return;
329             }
330         }
331         else
332         {
333             recordData.insert(recordData.end(), respRecordData,
334                               respRecordData + respCnt);
335 
336             // End or StartAndEnd
337             if (transferFlag == PLDM_PLATFORM_TRANSFER_END ||
338                 transferFlag == PLDM_PLATFORM_TRANSFER_START_AND_END)
339             {
340                 printPDRMsg(nextRecordHndl, recordData.size(),
341                             recordData.data(), terminusHandle);
342                 nextPartRequired = false;
343                 recordHandle = nextRecordHndl;
344                 dataTransferHandle = 0;
345                 recordChangeNumber = 0;
346                 operationFlag = PLDM_GET_FIRSTPART;
347                 recordData.clear();
348             }
349             else
350             {
351                 nextPartRequired = true;
352                 dataTransferHandle = nextDataTransferHndl;
353                 struct pldm_pdr_hdr* pdr_hdr = new (recordData.data())
354                     pldm_pdr_hdr;
355                 recordChangeNumber = pdr_hdr->record_change_num;
356                 operationFlag = PLDM_GET_NEXTPART;
357             }
358         }
359     }
360 
361   private:
362     const std::map<pldm::pdr::EntityType, std::string> entityType = {
363         {PLDM_ENTITY_UNSPECIFIED, "Unspecified"},
364         {PLDM_ENTITY_OTHER, "Other"},
365         {PLDM_ENTITY_NETWORK, "Network"},
366         {PLDM_ENTITY_GROUP, "Group"},
367         {PLDM_ENTITY_REMOTE_MGMT_COMM_DEVICE,
368          "Remote Management Communication Device"},
369         {PLDM_ENTITY_EXTERNAL_ENVIRONMENT, "External Environment"},
370         {PLDM_ENTITY_COMM_CHANNEL, " Communication Channel"},
371         {PLDM_ENTITY_TERMINUS, "PLDM Terminus"},
372         {PLDM_ENTITY_PLATFORM_EVENT_LOG, " Platform Event Log"},
373         {PLDM_ENTITY_KEYPAD, "keypad"},
374         {PLDM_ENTITY_SWITCH, "Switch"},
375         {PLDM_ENTITY_PUSHBUTTON, "Pushbutton"},
376         {PLDM_ENTITY_DISPLAY, "Display"},
377         {PLDM_ENTITY_INDICATOR, "Indicator"},
378         {PLDM_ENTITY_SYS_MGMT_SW, "System Management Software"},
379         {PLDM_ENTITY_SYS_FIRMWARE, "System Firmware"},
380         {PLDM_ENTITY_OPERATING_SYS, "Operating System"},
381         {PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER, "Virtual Machine Manager"},
382         {PLDM_ENTITY_OS_LOADER, "OS Loader"},
383         {PLDM_ENTITY_DEVICE_DRIVER, "Device Driver"},
384         {PLDM_ENTITY_MGMT_CONTROLLER_FW, "Management Controller Firmware"},
385         {PLDM_ENTITY_SYSTEM_CHASSIS, "System chassis (main enclosure)"},
386         {PLDM_ENTITY_SUB_CHASSIS, "Sub-chassis"},
387         {PLDM_ENTITY_DISK_DRIVE_BAY, "Disk Drive Bay"},
388         {PLDM_ENTITY_PERIPHERAL_BAY, "Peripheral Bay"},
389         {PLDM_ENTITY_DEVICE_BAY, "Device bay"},
390         {PLDM_ENTITY_DOOR, "Door"},
391         {PLDM_ENTITY_ACCESS_PANEL, "Access Panel"},
392         {PLDM_ENTITY_COVER, "Cover"},
393         {PLDM_ENTITY_BOARD, "Board"},
394         {PLDM_ENTITY_CARD, "Card"},
395         {PLDM_ENTITY_MODULE, "Module"},
396         {PLDM_ENTITY_SYS_MGMT_MODULE, "System management module"},
397         {PLDM_ENTITY_SYS_BOARD, "System Board"},
398         {PLDM_ENTITY_MEMORY_BOARD, "Memory Board"},
399         {PLDM_ENTITY_MEMORY_MODULE, "Memory Module"},
400         {PLDM_ENTITY_PROC_MODULE, "Processor Module"},
401         {PLDM_ENTITY_ADD_IN_CARD, "Add-in Card"},
402         {PLDM_ENTITY_CHASSIS_FRONT_PANEL_BOARD,
403          "Chassis front panel board(control panel)"},
404         {PLDM_ENTITY_BACK_PANEL_BOARD, "Back panel board"},
405         {PLDM_ENTITY_POWER_MGMT, "Power management board"},
406         {PLDM_ENTITY_POWER_SYS_BOARD, "Power system board"},
407         {PLDM_ENTITY_DRIVE_BACKPLANE, "Drive backplane"},
408         {PLDM_ENTITY_SYS_INTERNAL_EXPANSION_BOARD,
409          "System internal expansion board"},
410         {PLDM_ENTITY_OTHER_SYS_BOARD, "Other system board"},
411         {PLDM_ENTITY_CHASSIS_BACK_PANEL_BOARD, "Chassis back panel board"},
412         {PLDM_ENTITY_PROCESSING_BLADE, "Processing blade"},
413         {PLDM_ENTITY_CONNECTIVITY_SWITCH, "Connectivity switch"},
414         {PLDM_ENTITY_PROC_MEMORY_MODULE, "Processor/Memory Module"},
415         {PLDM_ENTITY_IO_MODULE, "I/O Module"},
416         {PLDM_ENTITY_PROC_IO_MODULE, "Processor I/O Module"},
417         {PLDM_ENTITY_COOLING_DEVICE, "Cooling device"},
418         {PLDM_ENTITY_COOLING_SUBSYSTEM, "Cooling subsystem"},
419         {PLDM_ENTITY_COOLING_UNIT, "Cooling Unit"},
420         {PLDM_ENTITY_FAN, "Fan"},
421         {PLDM_ENTITY_PELTIER_COOLING_DEVICE, "Peltier Cooling Device"},
422         {PLDM_ENTITY_LIQUID_COOLING_DEVICE, "Liquid Cooling Device"},
423         {PLDM_ENTITY_LIQUID_COOLING_SUBSYSTEM, "Liquid Colling Subsystem"},
424         {PLDM_ENTITY_OTHER_STORAGE_DEVICE, "Other Storage Device"},
425         {PLDM_ENTITY_FLOPPY_DRIVE, "Floppy Drive"},
426         {PLDM_ENTITY_FIXED_DISK_HARD_DRIVE, "Hard Drive"},
427         {PLDM_ENTITY_CD_DRIVE, "CD Drive"},
428         {PLDM_ENTITY_CD_DVD_DRIVE, "CD/DVD Drive"},
429         {PLDM_ENTITY_OTHER_SILICON_STORAGE_DEVICE,
430          "Other Silicon Storage Device"},
431         {PLDM_ENTITY_SOLID_STATE_DRIVE, "Solid State Drive"},
432         {PLDM_ENTITY_POWER_SUPPLY, "Power supply"},
433         {PLDM_ENTITY_BATTERY, "Battery"},
434         {PLDM_ENTITY_SUPER_CAPACITOR, "Super Capacitor"},
435         {PLDM_ENTITY_POWER_CONVERTER, "Power Converter"},
436         {PLDM_ENTITY_DC_DC_CONVERTER, "DC-DC Converter"},
437         {PLDM_ENTITY_AC_MAINS_POWER_SUPPLY, "AC mains power supply"},
438         {PLDM_ENTITY_DC_MAINS_POWER_SUPPLY, "DC mains power supply"},
439         {PLDM_ENTITY_PROC, "Processor"},
440         {PLDM_ENTITY_CHIPSET_COMPONENT, "Chipset Component"},
441         {PLDM_ENTITY_MGMT_CONTROLLER, "Management Controller"},
442         {PLDM_ENTITY_PERIPHERAL_CONTROLLER, "Peripheral Controller"},
443         {PLDM_ENTITY_SEEPROM, "SEEPROM"},
444         {PLDM_ENTITY_NVRAM_CHIP, "NVRAM Chip"},
445         {PLDM_ENTITY_FLASH_MEMORY_CHIP, "FLASH Memory chip"},
446         {PLDM_ENTITY_MEMORY_CHIP, "Memory Chip"},
447         {PLDM_ENTITY_MEMORY_CONTROLLER, "Memory Controller"},
448         {PLDM_ENTITY_NETWORK_CONTROLLER, "Network Controller"},
449         {PLDM_ENTITY_IO_CONTROLLER, "I/O Controller"},
450         {PLDM_ENTITY_SOUTH_BRIDGE, "South Bridge"},
451         {PLDM_ENTITY_REAL_TIME_CLOCK, "Real Time Clock (RTC)"},
452         {PLDM_ENTITY_FPGA_CPLD_DEVICE, "FPGA/CPLD Configurable Logic Device"},
453         {PLDM_ENTITY_PROCESSOR_CORE, "Processor Core"},
454         {PLDM_ENTITY_OTHER_BUS, "Other Bus"},
455         {PLDM_ENTITY_SYS_BUS, "System Bus"},
456         {PLDM_ENTITY_I2C_BUS, "I2C Bus"},
457         {PLDM_ENTITY_SMBUS_BUS, "SMBus Bus"},
458         {PLDM_ENTITY_SPI_BUS, "SPI Bus"},
459         {PLDM_ENTITY_PCI_BUS, "PCI Bus"},
460         {PLDM_ENTITY_PCI_EXPRESS_BUS, "PCI Express Bus"},
461         {PLDM_ENTITY_PECI_BUS, "PECI Bus"},
462         {PLDM_ENTITY_LPC_BUS, "LPC Bus"},
463         {PLDM_ENTITY_USB_BUS, "USB Bus"},
464         {PLDM_ENTITY_FIREWIRE_BUS, "FireWire Bus"},
465         {PLDM_ENTITY_SCSI_BUS, "SCSI Bus"},
466         {PLDM_ENTITY_SATA_SAS_BUS, "SATA/SAS Bus"},
467         {PLDM_ENTITY_PROC_FRONT_SIDE_BUS, "Processor/Front-side Bus"},
468         {PLDM_ENTITY_INTER_PROC_BUS, "Inter-processor Bus"},
469         {PLDM_ENTITY_CONNECTOR, "Connector"},
470         {PLDM_ENTITY_SLOT, "Slot"},
471         {PLDM_ENTITY_CABLE, "Cable(electrical or optical)"},
472         {PLDM_ENTITY_INTERCONNECT, "Interconnect"},
473         {PLDM_ENTITY_PLUG, "Plug"},
474         {PLDM_ENTITY_SOCKET, "Socket"},
475     };
476 
477     const std::map<uint16_t, std::string> stateSet = {
478         {PLDM_STATE_SET_HEALTH_STATE, "Health State"},
479         {PLDM_STATE_SET_AVAILABILITY, "Availability"},
480         {PLDM_STATE_SET_PREDICTIVE_CONDITION, "Predictive Condition"},
481         {PLDM_STATE_SET_REDUNDANCY_STATUS, "Redundancy Status"},
482         {PLDM_STATE_SET_HEALTH_REDUNDANCY_TREND, "Health/Redundancy Trend"},
483         {PLDM_STATE_SET_GROUP_RESOURCE_LEVEL, "Group Resource Level"},
484         {PLDM_STATE_SET_REDUNDANCY_ENTITY_ROLE, "Redundancy Entity Role"},
485         {PLDM_STATE_SET_OPERATIONAL_STATUS, "Operational Status"},
486         {PLDM_STATE_SET_OPERATIONAL_STRESS_STATUS, "Operational Stress Status"},
487         {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS, "Operational Fault Status"},
488         {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
489          "Operational Running Status"},
490         {PLDM_STATE_SET_OPERATIONAL_CONNECTION_STATUS,
491          "Operational Connection Status"},
492         {PLDM_STATE_SET_PRESENCE, "Presence"},
493         {PLDM_STATE_SET_PERFORMANCE, "Performance"},
494         {PLDM_STATE_SET_CONFIGURATION_STATE, "Configuration State"},
495         {PLDM_STATE_SET_CHANGED_CONFIGURATION, "Changed Configuration"},
496         {PLDM_STATE_SET_IDENTIFY_STATE, "Identify State"},
497         {PLDM_STATE_SET_VERSION, "Version"},
498         {PLDM_STATE_SET_ALARM_STATE, "Alarm State"},
499         {PLDM_STATE_SET_DEVICE_INITIALIZATION, "Device Initialization"},
500         {PLDM_STATE_SET_THERMAL_TRIP, "Thermal Trip"},
501         {PLDM_STATE_SET_HEARTBEAT, "Heartbeat"},
502         {PLDM_STATE_SET_LINK_STATE, "Link State"},
503         {PLDM_STATE_SET_SMOKE_STATE, "Smoke State"},
504         {PLDM_STATE_SET_HUMIDITY_STATE, "Humidity State"},
505         {PLDM_STATE_SET_DOOR_STATE, "Door State"},
506         {PLDM_STATE_SET_SWITCH_STATE, "Switch State"},
507         {PLDM_STATE_SET_LOCK_STATE, "Lock State"},
508         {PLDM_STATE_SET_PHYSICAL_SECURITY, "Physical Security"},
509         {PLDM_STATE_SET_DOCK_AUTHORIZATION, "Dock Authorization"},
510         {PLDM_STATE_SET_HW_SECURITY, "Hardware Security"},
511         {PLDM_STATE_SET_PHYSICAL_COMM_CONNECTION,
512          "Physical Communication Connection"},
513         {PLDM_STATE_SET_COMM_LEASH_STATUS, "Communication Leash Status"},
514         {PLDM_STATE_SET_FOREIGN_NW_DETECTION_STATUS,
515          "Foreign Network Detection Status"},
516         {PLDM_STATE_SET_PASSWORD_PROTECTED_ACCESS_SECURITY,
517          "Password-Protected Access Security"},
518         {PLDM_STATE_SET_SECURITY_ACCESS_PRIVILEGE_LEVEL,
519          "Security Access –PrivilegeLevel"},
520         {PLDM_STATE_SET_SESSION_AUDIT, "PLDM Session Audit"},
521         {PLDM_STATE_SET_SW_TERMINATION_STATUS, "Software Termination Status"},
522         {PLDM_STATE_SET_STORAGE_MEDIA_ACTIVITY, "Storage Media Activity"},
523         {PLDM_STATE_SET_BOOT_RESTART_CAUSE, "Boot/Restart Cause"},
524         {PLDM_STATE_SET_BOOT_RESTART_REQUEST, "Boot/Restart Request"},
525         {PLDM_STATE_SET_ENTITY_BOOT_STATUS, "Entity Boot Status"},
526         {PLDM_STATE_SET_BOOT_ERROR_STATUS, "Boot ErrorStatus"},
527         {PLDM_STATE_SET_BOOT_PROGRESS, "Boot Progress"},
528         {PLDM_STATE_SET_SYS_FIRMWARE_HANG, "System Firmware Hang"},
529         {PLDM_STATE_SET_POST_ERRORS, "POST Errors"},
530         {PLDM_STATE_SET_LOG_FILL_STATUS, "Log Fill Status"},
531         {PLDM_STATE_SET_LOG_FILTER_STATUS, "Log Filter Status"},
532         {PLDM_STATE_SET_LOG_TIMESTAMP_CHANGE, "Log Timestamp Change"},
533         {PLDM_STATE_SET_INTERRUPT_REQUESTED, "Interrupt Requested"},
534         {PLDM_STATE_SET_INTERRUPT_RECEIVED, "Interrupt Received"},
535         {PLDM_STATE_SET_DIAGNOSTIC_INTERRUPT_REQUESTED,
536          "Diagnostic Interrupt Requested"},
537         {PLDM_STATE_SET_DIAGNOSTIC_INTERRUPT_RECEIVED,
538          "Diagnostic Interrupt Received"},
539         {PLDM_STATE_SET_IO_CHANNEL_CHECK_NMI_REQUESTED,
540          "I/O Channel Check NMI Requested"},
541         {PLDM_STATE_SET_IO_CHANNEL_CHECK_NMI_RECEIVED,
542          "I/O Channel Check NMI Received"},
543         {PLDM_STATE_SET_FATAL_NMI_REQUESTED, "Fatal NMI Requested"},
544         {PLDM_STATE_SET_FATAL_NMI_RECEIVED, "Fatal NMI Received"},
545         {PLDM_STATE_SET_SOFTWARE_NMI_REQUESTED, "Software NMI Requested"},
546         {PLDM_STATE_SET_SOFTWARE_NMI_RECEIVED, "Software NMI Received"},
547         {PLDM_STATE_SET_SMI_REQUESTED, "SMI Requested"},
548         {PLDM_STATE_SET_SMI_RECEIVED, "SMI Received"},
549         {PLDM_STATE_SET_PCI_PERR_REQUESTED, "PCI PERR Requested"},
550         {PLDM_STATE_SET_PCI_PERR_RECEIVED, "PCI PERR Received"},
551         {PLDM_STATE_SET_PCI_SERR_REQUESTED, "PCI SERR Requested "},
552         {PLDM_STATE_SET_PCI_SERR_RECEIVED, "PCI SERR Received"},
553         {PLDM_STATE_SET_BUS_ERROR_STATUS, "Bus Error Status"},
554         {PLDM_STATE_SET_WATCHDOG_STATUS, "Watchdog Status"},
555         {PLDM_STATE_SET_POWER_SUPPLY_STATE, "Power Supply State"},
556         {PLDM_STATE_SET_DEVICE_POWER_STATE, "Device Power State"},
557         {PLDM_STATE_SET_ACPI_POWER_STATE, "ACPI Power State"},
558         {PLDM_STATE_SET_BACKUP_POWER_SOURCE, "Backup Power Source"},
559         {PLDM_STATE_SET_SYSTEM_POWER_STATE, "System Power State "},
560         {PLDM_STATE_SET_BATTERY_ACTIVITY, "Battery Activity"},
561         {PLDM_STATE_SET_BATTERY_STATE, "Battery State"},
562         {PLDM_STATE_SET_PROC_POWER_STATE, "Processor Power State"},
563         {PLDM_STATE_SET_POWER_PERFORMANCE_STATE, "Power-Performance State"},
564         {PLDM_STATE_SET_PROC_ERROR_STATUS, "Processor Error Status"},
565         {PLDM_STATE_SET_BIST_FAILURE_STATUS, "BIST FailureStatus"},
566         {PLDM_STATE_SET_IBIST_FAILURE_STATUS, "IBIST FailureStatus"},
567         {PLDM_STATE_SET_PROC_HANG_IN_POST, "Processor Hang in POST"},
568         {PLDM_STATE_SET_PROC_STARTUP_FAILURE, "Processor Startup Failure"},
569         {PLDM_STATE_SET_UNCORRECTABLE_CPU_ERROR, "Uncorrectable CPU Error"},
570         {PLDM_STATE_SET_MACHINE_CHECK_ERROR, "Machine Check Error"},
571         {PLDM_STATE_SET_CORRECTED_MACHINE_CHECK, "Corrected Machine Check"},
572         {PLDM_STATE_SET_CACHE_STATUS, "Cache Status"},
573         {PLDM_STATE_SET_MEMORY_ERROR_STATUS, "Memory Error Status"},
574         {PLDM_STATE_SET_REDUNDANT_MEMORY_ACTIVITY_STATUS,
575          "Redundant Memory Activity Status"},
576         {PLDM_STATE_SET_ERROR_DETECTION_STATUS, "Error Detection Status"},
577         {PLDM_STATE_SET_STUCK_BIT_STATUS, "Stuck Bit Status"},
578         {PLDM_STATE_SET_SCRUB_STATUS, "Scrub Status"},
579         {PLDM_STATE_SET_SLOT_OCCUPANCY, "Slot Occupancy"},
580         {PLDM_STATE_SET_SLOT_STATE, "Slot State"},
581     };
582 
583     const std::array<std::string_view, 4> sensorInit = {
584         "noInit", "useInitPDR", "enableSensor", "disableSensor"};
585 
586     const std::array<std::string_view, 4> effecterInit = {
587         "noInit", "useInitPDR", "enableEffecter", "disableEffecter"};
588 
589     const std::map<uint8_t, std::string> pdrType = {
590         {PLDM_TERMINUS_LOCATOR_PDR, "Terminus Locator PDR"},
591         {PLDM_NUMERIC_SENSOR_PDR, "Numeric Sensor PDR"},
592         {PLDM_NUMERIC_SENSOR_INITIALIZATION_PDR,
593          "Numeric Sensor Initialization PDR"},
594         {PLDM_STATE_SENSOR_PDR, "State Sensor PDR"},
595         {PLDM_STATE_SENSOR_INITIALIZATION_PDR,
596          "State Sensor Initialization PDR"},
597         {PLDM_SENSOR_AUXILIARY_NAMES_PDR, "Sensor Auxiliary Names PDR"},
598         {PLDM_OEM_UNIT_PDR, "OEM Unit PDR"},
599         {PLDM_OEM_STATE_SET_PDR, "OEM State Set PDR"},
600         {PLDM_NUMERIC_EFFECTER_PDR, "Numeric Effecter PDR"},
601         {PLDM_NUMERIC_EFFECTER_INITIALIZATION_PDR,
602          "Numeric Effecter Initialization PDR"},
603         {PLDM_COMPACT_NUMERIC_SENSOR_PDR, "Compact Numeric Sensor PDR"},
604         {PLDM_STATE_EFFECTER_PDR, "State Effecter PDR"},
605         {PLDM_STATE_EFFECTER_INITIALIZATION_PDR,
606          "State Effecter Initialization PDR"},
607         {PLDM_EFFECTER_AUXILIARY_NAMES_PDR, "Effecter Auxiliary Names PDR"},
608         {PLDM_EFFECTER_OEM_SEMANTIC_PDR, "Effecter OEM Semantic PDR"},
609         {PLDM_PDR_ENTITY_ASSOCIATION, "Entity Association PDR"},
610         {PLDM_ENTITY_AUXILIARY_NAMES_PDR, "Entity Auxiliary Names PDR"},
611         {PLDM_OEM_ENTITY_ID_PDR, "OEM Entity ID PDR"},
612         {PLDM_INTERRUPT_ASSOCIATION_PDR, "Interrupt Association PDR"},
613         {PLDM_EVENT_LOG_PDR, "PLDM Event Log PDR"},
614         {PLDM_PDR_FRU_RECORD_SET, "FRU Record Set PDR"},
615         {PLDM_OEM_DEVICE_PDR, "OEM Device PDR"},
616         {PLDM_OEM_PDR, "OEM PDR"},
617     };
618 
619     static inline const std::map<uint8_t, std::string> setThermalTrip{
620         {PLDM_STATE_SET_THERMAL_TRIP_STATUS_NORMAL, "Normal"},
621         {PLDM_STATE_SET_THERMAL_TRIP_STATUS_THERMAL_TRIP, "Thermal Trip"}};
622 
623     static inline const std::map<uint8_t, std::string> setIdentifyState{
624         {PLDM_STATE_SET_IDENTIFY_STATE_UNASSERTED, "Identify State Unasserted"},
625         {PLDM_STATE_SET_IDENTIFY_STATE_ASSERTED, "Identify State Asserted"}};
626 
627     static inline const std::map<uint8_t, std::string> setBootProgressState{
628         {PLDM_STATE_SET_BOOT_PROG_STATE_NOT_ACTIVE, "Boot Not Active"},
629         {PLDM_STATE_SET_BOOT_PROG_STATE_COMPLETED, "Boot Completed"},
630         {PLDM_STATE_SET_BOOT_PROG_STATE_MEM_INITIALIZATION,
631          "Memory Initialization"},
632         {PLDM_STATE_SET_BOOT_PROG_STATE_SEC_PROC_INITIALIZATION,
633          "Secondary Processor(s) Initialization"},
634         {PLDM_STATE_SET_BOOT_PROG_STATE_PCI_RESOURCE_CONFIG,
635          "PCI Resource Configuration"},
636         {PLDM_STATE_SET_BOOT_PROG_STATE_STARTING_OP_SYS,
637          "Starting Operating System"},
638         {PLDM_STATE_SET_BOOT_PROG_STATE_BASE_BOARD_INITIALIZATION,
639          "Baseboard Initialization"},
640         {PLDM_STATE_SET_BOOT_PROG_STATE_PRIMARY_PROC_INITIALIZATION,
641          "Primary Processor Initialization"},
642         {PLDM_STATE_SET_BOOT_PROG_STATE_OSSTART, "OSStart"}};
643 
644     static inline const std::map<uint8_t, std::string> setOpFaultStatus{
645         {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS_NORMAL, "Normal"},
646         {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS_ERROR, "Error"},
647         {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS_NON_RECOVERABLE_ERROR,
648          "Non Recoverable Error"}};
649 
650     static inline const std::map<uint8_t, std::string> setSysPowerState{
651         {PLDM_STATE_SET_SYS_POWER_STATE_OFF_SOFT_GRACEFUL,
652          "Off-Soft Graceful"}};
653 
654     static inline const std::map<uint8_t, std::string> setSWTerminationStatus{
655         {PLDM_SW_TERM_GRACEFUL_RESTART_REQUESTED,
656          "Graceful Restart Requested"}};
657 
658     static inline const std::map<uint8_t, std::string> setAvailability{
659         {PLDM_STATE_SET_AVAILABILITY_REBOOTING, "Rebooting"}};
660 
661     static inline const std::map<uint8_t, std::string> setHealthState{
662         {PLDM_STATE_SET_HEALTH_STATE_NORMAL, "Normal"},
663         {PLDM_STATE_SET_HEALTH_STATE_NON_CRITICAL, "Non-Critical"},
664         {PLDM_STATE_SET_HEALTH_STATE_CRITICAL, "Critical"},
665         {PLDM_STATE_SET_HEALTH_STATE_FATAL, "Fatal"},
666         {PLDM_STATE_SET_HEALTH_STATE_UPPER_NON_CRITICAL, "Upper Non-Critical"},
667         {PLDM_STATE_SET_HEALTH_STATE_LOWER_NON_CRITICAL, "Lower Non-Critical"},
668         {PLDM_STATE_SET_HEALTH_STATE_UPPER_CRITICAL, "Upper Critical"},
669         {PLDM_STATE_SET_HEALTH_STATE_LOWER_CRITICAL, "Lower Critical"},
670         {PLDM_STATE_SET_HEALTH_STATE_UPPER_FATAL, "Upper Fatal"},
671         {PLDM_STATE_SET_HEALTH_STATE_LOWER_FATAL, "Lower Fatal"}};
672 
673     static inline const std::map<uint8_t, std::string>
674         setOperationalRunningState{
675             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STARTING, "Starting"},
676             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPING, "Stopping"},
677             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPED, "Stopped"},
678             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_IN_SERVICE,
679              "In Service"},
680             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_ABORTED, "Aborted"},
681             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_DORMANT, "Dormant"}};
682 
683     static inline const std::map<uint8_t, std::string> setPowerDeviceState{
684         {PLDM_STATE_SET_ACPI_DEVICE_POWER_STATE_UNKNOWN, "Unknown"},
685         {PLDM_STATE_SET_ACPI_DEVICE_POWER_STATE_FULLY_ON, "Fully-On"},
686         {PLDM_STATE_SET_ACPI_DEVICE_POWER_STATE_INTERMEDIATE_1,
687          "Intermediate State-1"},
688         {PLDM_STATE_SET_ACPI_DEVICE_POWER_STATE_INTERMEDIATE_2,
689          "Intermediate State-2"},
690         {PLDM_STATE_SET_ACPI_DEVICE_POWER_STATE_OFF, "Off"}};
691 
692     static inline const std::map<uint16_t, const std::map<uint8_t, std::string>>
693         populatePStateMaps{
694             {PLDM_STATE_SET_THERMAL_TRIP, setThermalTrip},
695             {PLDM_STATE_SET_IDENTIFY_STATE, setIdentifyState},
696             {PLDM_STATE_SET_BOOT_PROGRESS, setBootProgressState},
697             {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS, setOpFaultStatus},
698             {PLDM_STATE_SET_SYSTEM_POWER_STATE, setSysPowerState},
699             {PLDM_STATE_SET_SW_TERMINATION_STATUS, setSWTerminationStatus},
700             {PLDM_STATE_SET_AVAILABILITY, setAvailability},
701             {PLDM_STATE_SET_HEALTH_STATE, setHealthState},
702             {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
703              setOperationalRunningState},
704             {PLDM_STATE_SET_DEVICE_POWER_STATE, setPowerDeviceState},
705         };
706 
707     const std::map<std::string, uint8_t> strToPdrType = {
708         {"terminuslocator", PLDM_TERMINUS_LOCATOR_PDR},
709         {"statesensor", PLDM_STATE_SENSOR_PDR},
710         {"sensorauxname", PLDM_SENSOR_AUXILIARY_NAMES_PDR},
711         {"numericeffecter", PLDM_NUMERIC_EFFECTER_PDR},
712         {"effecterauxname", PLDM_EFFECTER_AUXILIARY_NAMES_PDR},
713         {"numericsensor", PLDM_NUMERIC_SENSOR_PDR},
714         {"compactnumericsensor", PLDM_COMPACT_NUMERIC_SENSOR_PDR},
715         {"stateeffecter", PLDM_STATE_EFFECTER_PDR},
716         {"entityassociation", PLDM_PDR_ENTITY_ASSOCIATION},
717         {"frurecord", PLDM_PDR_FRU_RECORD_SET},
718         // Add other types
719     };
720 
isLogicalBitSet(const uint16_t entity_type)721     bool isLogicalBitSet(const uint16_t entity_type)
722     {
723         return entity_type & 0x8000;
724     }
725 
getEntityTypeForLogicalEntity(const uint16_t logical_entity_type)726     uint16_t getEntityTypeForLogicalEntity(const uint16_t logical_entity_type)
727     {
728         return logical_entity_type & 0x7FFF;
729     }
730 
getEntityName(pldm::pdr::EntityType type)731     std::string getEntityName(pldm::pdr::EntityType type)
732     {
733         uint16_t entityNumber = type;
734         std::string entityName = "[Physical] ";
735 
736         if (isLogicalBitSet(type))
737         {
738             entityName = "[Logical] ";
739             entityNumber = getEntityTypeForLogicalEntity(type);
740         }
741 
742         try
743         {
744             return entityName + entityType.at(entityNumber);
745         }
746         catch (const std::out_of_range& e)
747         {
748             auto OemString =
749                 std::to_string(static_cast<unsigned>(entityNumber));
750             if (type >= PLDM_OEM_ENTITY_TYPE_START &&
751                 type <= PLDM_OEM_ENTITY_TYPE_END)
752             {
753 #ifdef OEM_IBM
754                 if (OemIBMEntityType.contains(entityNumber))
755                 {
756                     return entityName + OemIBMEntityType.at(entityNumber) +
757                            "(OEM)";
758                 }
759 #endif
760                 return entityName + OemString + "(OEM)";
761             }
762             return OemString;
763         }
764     }
765 
getStateSetName(uint16_t id)766     std::string getStateSetName(uint16_t id)
767     {
768         auto typeString = std::to_string(id);
769         try
770         {
771             return stateSet.at(id) + "(" + typeString + ")";
772         }
773         catch (const std::out_of_range& e)
774         {
775             return typeString;
776         }
777     }
778 
getStateSetPossibleStateNames(uint16_t stateId,const std::vector<uint8_t> & value)779     std::vector<std::string> getStateSetPossibleStateNames(
780         uint16_t stateId, const std::vector<uint8_t>& value)
781     {
782         std::vector<std::string> data{};
783 
784         for (const auto& s : value)
785         {
786             std::map<uint8_t, std::string> stateNameMaps;
787             auto pstr = std::to_string(s);
788 
789 #ifdef OEM_IBM
790             if (stateId >= PLDM_OEM_STATE_SET_ID_START &&
791                 stateId < PLDM_OEM_STATE_SET_ID_END)
792             {
793                 if (populateOemIBMStateMaps.contains(stateId))
794                 {
795                     const std::map<uint8_t, std::string> stateNames =
796                         populateOemIBMStateMaps.at(stateId);
797                     stateNameMaps.insert(stateNames.begin(), stateNames.end());
798                 }
799             }
800 #endif
801             if (populatePStateMaps.contains(stateId))
802             {
803                 const std::map<uint8_t, std::string> stateNames =
804                     populatePStateMaps.at(stateId);
805                 stateNameMaps.insert(stateNames.begin(), stateNames.end());
806             }
807             if (stateNameMaps.contains(s))
808             {
809                 data.push_back(stateNameMaps.at(s) + "(" + pstr + ")");
810             }
811             else
812             {
813                 data.push_back(pstr);
814             }
815         }
816         return data;
817     }
818 
getPDRType(uint8_t type)819     std::string getPDRType(uint8_t type)
820     {
821         auto typeString = std::to_string(type);
822         try
823         {
824             return pdrType.at(type);
825         }
826         catch (const std::out_of_range& e)
827         {
828             return typeString;
829         }
830     }
831 
printCommonPDRHeader(const pldm_pdr_hdr * hdr,ordered_json & output)832     void printCommonPDRHeader(const pldm_pdr_hdr* hdr, ordered_json& output)
833     {
834         output["recordHandle"] = hdr->record_handle;
835         output["PDRHeaderVersion"] = unsigned(hdr->version);
836         output["PDRType"] = getPDRType(hdr->type);
837         output["recordChangeNumber"] = hdr->record_change_num;
838         output["dataLength"] = hdr->length;
839     }
840 
printPossibleStates(uint8_t possibleStatesSize,const bitfield8_t * states)841     std::vector<uint8_t> printPossibleStates(uint8_t possibleStatesSize,
842                                              const bitfield8_t* states)
843     {
844         uint8_t possibleStatesPos{};
845         std::vector<uint8_t> data{};
846         auto printStates = [&possibleStatesPos, &data](const bitfield8_t& val) {
847             std::stringstream pstates;
848             for (int i = 0; i < CHAR_BIT; i++)
849             {
850                 if (val.byte & (1 << i))
851                 {
852                     pstates << (possibleStatesPos * CHAR_BIT + i);
853                     data.push_back(
854                         static_cast<uint8_t>(std::stoi(pstates.str())));
855                     pstates.str("");
856                 }
857             }
858             possibleStatesPos++;
859         };
860         std::for_each(states, states + possibleStatesSize, printStates);
861         return data;
862     }
863 
printStateSensorPDR(const uint8_t * data,ordered_json & output)864     void printStateSensorPDR(const uint8_t* data, ordered_json& output)
865     {
866         auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(data);
867         output["PLDMTerminusHandle"] = pdr->terminus_handle;
868         output["sensorID"] = pdr->sensor_id;
869         output["entityType"] = getEntityName(pdr->entity_type);
870         output["entityInstanceNumber"] = pdr->entity_instance;
871         output["containerID"] = pdr->container_id;
872         output["sensorInit"] = sensorInit[pdr->sensor_init];
873         output["sensorAuxiliaryNamesPDR"] =
874             (pdr->sensor_auxiliary_names_pdr ? true : false);
875         output["compositeSensorCount"] = unsigned(pdr->composite_sensor_count);
876 
877         auto statesPtr = pdr->possible_states;
878         auto compCount = pdr->composite_sensor_count;
879 
880         while (compCount--)
881         {
882             auto state = reinterpret_cast<const state_sensor_possible_states*>(
883                 statesPtr);
884             output.emplace(("stateSetID[" + std::to_string(compCount) + "]"),
885                            getStateSetName(state->state_set_id));
886             output.emplace(
887                 ("possibleStatesSize[" + std::to_string(compCount) + "]"),
888                 state->possible_states_size);
889             output.emplace(
890                 ("possibleStates[" + std::to_string(compCount) + "]"),
891                 getStateSetPossibleStateNames(
892                     state->state_set_id,
893                     printPossibleStates(state->possible_states_size,
894                                         state->states)));
895 
896             if (compCount)
897             {
898                 statesPtr += sizeof(state_sensor_possible_states) +
899                              state->possible_states_size - 1;
900             }
901         }
902     }
903 
printPDRFruRecordSet(uint8_t * data,ordered_json & output)904     void printPDRFruRecordSet(uint8_t* data, ordered_json& output)
905     {
906         if (data == nullptr)
907         {
908             return;
909         }
910 
911         data += sizeof(pldm_pdr_hdr);
912         pldm_pdr_fru_record_set* pdr = new (data) pldm_pdr_fru_record_set;
913         if (!pdr)
914         {
915             std::cerr << "Failed to get the FRU record set PDR" << std::endl;
916             return;
917         }
918 
919         output["PLDMTerminusHandle"] = unsigned(pdr->terminus_handle);
920         output["FRURecordSetIdentifier"] = unsigned(pdr->fru_rsi);
921         output["entityType"] = getEntityName(pdr->entity_type);
922         output["entityInstanceNumber"] = unsigned(pdr->entity_instance);
923         output["containerID"] = unsigned(pdr->container_id);
924     }
925 
printPDREntityAssociation(uint8_t * data,ordered_json & output)926     void printPDREntityAssociation(uint8_t* data, ordered_json& output)
927     {
928         const std::map<uint8_t, const char*> assocationType = {
929             {PLDM_ENTITY_ASSOCIAION_PHYSICAL, "Physical"},
930             {PLDM_ENTITY_ASSOCIAION_LOGICAL, "Logical"},
931         };
932 
933         if (data == nullptr)
934         {
935             return;
936         }
937 
938         data += sizeof(pldm_pdr_hdr);
939         pldm_pdr_entity_association* pdr = new (data)
940             pldm_pdr_entity_association;
941         if (!pdr)
942         {
943             std::cerr << "Failed to get the PDR eneity association"
944                       << std::endl;
945             return;
946         }
947 
948         output["containerID"] = int(pdr->container_id);
949         if (assocationType.contains(pdr->association_type))
950         {
951             output["associationType"] =
952                 assocationType.at(pdr->association_type);
953         }
954         else
955         {
956             std::cout << "Get associationType failed.\n";
957         }
958         output["containerEntityType"] =
959             getEntityName(pdr->container.entity_type);
960         output["containerEntityInstanceNumber"] =
961             int(pdr->container.entity_instance_num);
962         output["containerEntityContainerID"] =
963             int(pdr->container.entity_container_id);
964         output["containedEntityCount"] =
965             static_cast<unsigned>(pdr->num_children);
966 
967         pldm_entity* child = new (&pdr->children[0]) pldm_entity;
968         for (int i = 0; i < pdr->num_children; ++i)
969         {
970             output.emplace("containedEntityType[" + std::to_string(i + 1) + "]",
971                            getEntityName(child->entity_type));
972             output.emplace("containedEntityInstanceNumber[" +
973                                std::to_string(i + 1) + "]",
974                            unsigned(child->entity_instance_num));
975             output.emplace("containedEntityContainerID[" +
976                                std::to_string(i + 1) + "]",
977                            unsigned(child->entity_container_id));
978 
979             ++child;
980         }
981     }
982 
983     /** @brief Format the Sensor/Effecter Aux Name PDR types to json output
984      *
985      *  @param[in] data - reference to the Sensor/Effecter Aux Name PDR
986      *  @param[out] output - PDRs data fields in Json format
987      */
printAuxNamePDR(uint8_t * data,ordered_json & output)988     void printAuxNamePDR(uint8_t* data, ordered_json& output)
989     {
990         constexpr uint8_t nullTerminator = 0;
991         struct pldm_effecter_aux_name_pdr* auxNamePdr =
992             (struct pldm_effecter_aux_name_pdr*)data;
993 
994         if (!auxNamePdr)
995         {
996             std::cerr << "Failed to get Aux Name PDR" << std::endl;
997             return;
998         }
999 
1000         std::string sPrefix = "effecter";
1001         if (auxNamePdr->hdr.type == PLDM_SENSOR_AUXILIARY_NAMES_PDR)
1002         {
1003             sPrefix = "sensor";
1004         }
1005         output["terminusHandle"] = int(auxNamePdr->terminus_handle);
1006         output[sPrefix + "Id"] = int(auxNamePdr->effecter_id);
1007         output[sPrefix + "Count"] = int(auxNamePdr->effecter_count);
1008 
1009         const uint8_t* ptr = auxNamePdr->effecter_names;
1010         for (auto i : std::views::iota(0, (int)auxNamePdr->effecter_count))
1011         {
1012             const uint8_t nameStringCount = static_cast<uint8_t>(*ptr);
1013             ptr += sizeof(uint8_t);
1014             for (auto j : std::views::iota(0, (int)nameStringCount))
1015             {
1016                 std::string nameLanguageTagKey =
1017                     sPrefix + std::to_string(j) + "_nameLanguageTag" +
1018                     std::to_string(i);
1019                 std::string entityAuxNameKey =
1020                     sPrefix + std::to_string(j) + "_entityAuxName" +
1021                     std::to_string(i);
1022                 std::string nameLanguageTag(reinterpret_cast<const char*>(ptr),
1023                                             0, PLDM_STR_UTF_8_MAX_LEN);
1024                 ptr += nameLanguageTag.size() + sizeof(nullTerminator);
1025                 std::u16string u16NameString(
1026                     reinterpret_cast<const char16_t*>(ptr), 0,
1027                     PLDM_STR_UTF_16_MAX_LEN);
1028                 ptr += (u16NameString.size() + sizeof(nullTerminator)) *
1029                        sizeof(uint16_t);
1030                 std::transform(u16NameString.cbegin(), u16NameString.cend(),
1031                                u16NameString.begin(),
1032                                [](uint16_t utf16) { return be16toh(utf16); });
1033 #pragma GCC diagnostic push
1034 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1035                 std::string nameString =
1036                     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
1037                                          char16_t>{}
1038                         .to_bytes(u16NameString);
1039 #pragma GCC diagnostic pop
1040                 output[nameLanguageTagKey] = nameLanguageTag;
1041                 output[entityAuxNameKey] = nameString;
1042             }
1043         }
1044     }
1045 
printNumericEffecterPDR(uint8_t * data,ordered_json & output)1046     void printNumericEffecterPDR(uint8_t* data, ordered_json& output)
1047     {
1048         struct pldm_numeric_effecter_value_pdr* pdr =
1049             (struct pldm_numeric_effecter_value_pdr*)data;
1050         if (!pdr)
1051         {
1052             std::cerr << "Failed to get numeric effecter PDR" << std::endl;
1053             return;
1054         }
1055 
1056         output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
1057         output["effecterID"] = int(pdr->effecter_id);
1058         output["entityType"] = int(pdr->entity_type);
1059         output["entityInstanceNumber"] = int(pdr->entity_instance);
1060         output["containerID"] = int(pdr->container_id);
1061         output["effecterSemanticID"] = int(pdr->effecter_semantic_id);
1062         output["effecterInit"] = unsigned(pdr->effecter_init);
1063         output["effecterAuxiliaryNames"] =
1064             (unsigned(pdr->effecter_auxiliary_names) ? true : false);
1065         output["baseUnit"] = unsigned(pdr->base_unit);
1066         output["unitModifier"] = unsigned(pdr->unit_modifier);
1067         output["rateUnit"] = unsigned(pdr->rate_unit);
1068         output["baseOEMUnitHandle"] = unsigned(pdr->base_oem_unit_handle);
1069         output["auxUnit"] = unsigned(pdr->aux_unit);
1070         output["auxUnitModifier"] = unsigned(pdr->aux_unit_modifier);
1071         output["auxrateUnit"] = unsigned(pdr->aux_rate_unit);
1072         output["auxOEMUnitHandle"] = unsigned(pdr->aux_oem_unit_handle);
1073         output["isLinear"] = (unsigned(pdr->is_linear) ? true : false);
1074         output["effecterDataSize"] = unsigned(pdr->effecter_data_size);
1075         output["resolution"] = unsigned(pdr->resolution);
1076         output["offset"] = unsigned(pdr->offset);
1077         output["accuracy"] = unsigned(pdr->accuracy);
1078         output["plusTolerance"] = unsigned(pdr->plus_tolerance);
1079         output["minusTolerance"] = unsigned(pdr->minus_tolerance);
1080         output["stateTransitionInterval"] =
1081             unsigned(pdr->state_transition_interval);
1082         output["TransitionInterval"] = unsigned(pdr->transition_interval);
1083 
1084         switch (pdr->effecter_data_size)
1085         {
1086             case PLDM_EFFECTER_DATA_SIZE_UINT8:
1087                 output["maxSettable"] = unsigned(pdr->max_settable.value_u8);
1088                 output["minSettable"] = unsigned(pdr->min_settable.value_u8);
1089                 break;
1090             case PLDM_EFFECTER_DATA_SIZE_SINT8:
1091                 output["maxSettable"] = unsigned(pdr->max_settable.value_s8);
1092                 output["minSettable"] = unsigned(pdr->min_settable.value_s8);
1093                 break;
1094             case PLDM_EFFECTER_DATA_SIZE_UINT16:
1095                 output["maxSettable"] = unsigned(pdr->max_settable.value_u16);
1096                 output["minSettable"] = unsigned(pdr->min_settable.value_u16);
1097                 break;
1098             case PLDM_EFFECTER_DATA_SIZE_SINT16:
1099                 output["maxSettable"] = unsigned(pdr->max_settable.value_s16);
1100                 output["minSettable"] = unsigned(pdr->min_settable.value_s16);
1101                 break;
1102             case PLDM_EFFECTER_DATA_SIZE_UINT32:
1103                 output["maxSettable"] = unsigned(pdr->max_settable.value_u32);
1104                 output["minSettable"] = unsigned(pdr->min_settable.value_u32);
1105                 break;
1106             case PLDM_EFFECTER_DATA_SIZE_SINT32:
1107                 output["maxSettable"] = unsigned(pdr->max_settable.value_s32);
1108                 output["minSettable"] = unsigned(pdr->min_settable.value_s32);
1109                 break;
1110             default:
1111                 break;
1112         }
1113 
1114         output["rangeFieldFormat"] = unsigned(pdr->range_field_format);
1115         output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1116 
1117         switch (pdr->range_field_format)
1118         {
1119             case PLDM_RANGE_FIELD_FORMAT_UINT8:
1120                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u8);
1121                 output["normalMax"] = unsigned(pdr->normal_max.value_u8);
1122                 output["normalMin"] = unsigned(pdr->normal_min.value_u8);
1123                 output["ratedMax"] = unsigned(pdr->rated_max.value_u8);
1124                 output["ratedMin"] = unsigned(pdr->rated_min.value_u8);
1125                 break;
1126             case PLDM_RANGE_FIELD_FORMAT_SINT8:
1127                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s8);
1128                 output["normalMax"] = unsigned(pdr->normal_max.value_s8);
1129                 output["normalMin"] = unsigned(pdr->normal_min.value_s8);
1130                 output["ratedMax"] = unsigned(pdr->rated_max.value_s8);
1131                 output["ratedMin"] = unsigned(pdr->rated_min.value_s8);
1132                 break;
1133             case PLDM_RANGE_FIELD_FORMAT_UINT16:
1134                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u16);
1135                 output["normalMax"] = unsigned(pdr->normal_max.value_u16);
1136                 output["normalMin"] = unsigned(pdr->normal_min.value_u16);
1137                 output["ratedMax"] = unsigned(pdr->rated_max.value_u16);
1138                 output["ratedMin"] = unsigned(pdr->rated_min.value_u16);
1139                 break;
1140             case PLDM_RANGE_FIELD_FORMAT_SINT16:
1141                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s16);
1142                 output["normalMax"] = unsigned(pdr->normal_max.value_s16);
1143                 output["normalMin"] = unsigned(pdr->normal_min.value_s16);
1144                 output["ratedMax"] = unsigned(pdr->rated_max.value_s16);
1145                 output["ratedMin"] = unsigned(pdr->rated_min.value_s16);
1146                 break;
1147             case PLDM_RANGE_FIELD_FORMAT_UINT32:
1148                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u32);
1149                 output["normalMax"] = unsigned(pdr->normal_max.value_u32);
1150                 output["normalMin"] = unsigned(pdr->normal_min.value_u32);
1151                 output["ratedMax"] = unsigned(pdr->rated_max.value_u32);
1152                 output["ratedMin"] = unsigned(pdr->rated_min.value_u32);
1153                 break;
1154             case PLDM_RANGE_FIELD_FORMAT_SINT32:
1155                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s32);
1156                 output["normalMax"] = unsigned(pdr->normal_max.value_s32);
1157                 output["normalMin"] = unsigned(pdr->normal_min.value_s32);
1158                 output["ratedMax"] = unsigned(pdr->rated_max.value_s32);
1159                 output["ratedMin"] = unsigned(pdr->rated_min.value_s32);
1160                 break;
1161             case PLDM_RANGE_FIELD_FORMAT_REAL32:
1162                 output["nominalValue"] = unsigned(pdr->nominal_value.value_f32);
1163                 output["normalMax"] = unsigned(pdr->normal_max.value_f32);
1164                 output["normalMin"] = unsigned(pdr->normal_min.value_f32);
1165                 output["ratedMax"] = unsigned(pdr->rated_max.value_f32);
1166                 output["ratedMin"] = unsigned(pdr->rated_min.value_f32);
1167                 break;
1168             default:
1169                 break;
1170         }
1171     }
1172 
printStateEffecterPDR(const uint8_t * data,ordered_json & output)1173     void printStateEffecterPDR(const uint8_t* data, ordered_json& output)
1174     {
1175         auto pdr = reinterpret_cast<const pldm_state_effecter_pdr*>(data);
1176 
1177         output["PLDMTerminusHandle"] = pdr->terminus_handle;
1178         output["effecterID"] = pdr->effecter_id;
1179         output["entityType"] = getEntityName(pdr->entity_type);
1180         output["entityInstanceNumber"] = pdr->entity_instance;
1181         output["containerID"] = pdr->container_id;
1182         output["effecterSemanticID"] = pdr->effecter_semantic_id;
1183         output["effecterInit"] = effecterInit[pdr->effecter_init];
1184         output["effecterDescriptionPDR"] =
1185             (pdr->has_description_pdr ? true : false);
1186         output["compositeEffecterCount"] =
1187             unsigned(pdr->composite_effecter_count);
1188 
1189         auto statesPtr = pdr->possible_states;
1190         auto compEffCount = pdr->composite_effecter_count;
1191 
1192         while (compEffCount--)
1193         {
1194             auto state =
1195                 reinterpret_cast<const state_effecter_possible_states*>(
1196                     statesPtr);
1197             output.emplace(("stateSetID[" + std::to_string(compEffCount) + "]"),
1198                            getStateSetName(state->state_set_id));
1199             output.emplace(
1200                 ("possibleStatesSize[" + std::to_string(compEffCount) + "]"),
1201                 state->possible_states_size);
1202             output.emplace(
1203                 ("possibleStates[" + std::to_string(compEffCount) + "]"),
1204                 getStateSetPossibleStateNames(
1205                     state->state_set_id,
1206                     printPossibleStates(state->possible_states_size,
1207                                         state->states)));
1208 
1209             if (compEffCount)
1210             {
1211                 statesPtr += sizeof(state_effecter_possible_states) +
1212                              state->possible_states_size - 1;
1213             }
1214         }
1215     }
1216 
checkTerminusHandle(const uint8_t * data,std::optional<uint16_t> terminusHandle)1217     bool checkTerminusHandle(const uint8_t* data,
1218                              std::optional<uint16_t> terminusHandle)
1219     {
1220         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1221 
1222         if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1223         {
1224             auto tlpdr =
1225                 reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1226 
1227             if (tlpdr->terminus_handle != terminusHandle)
1228             {
1229                 return true;
1230             }
1231         }
1232         else if (pdr->type == PLDM_STATE_SENSOR_PDR)
1233         {
1234             auto sensor = reinterpret_cast<const pldm_state_sensor_pdr*>(data);
1235 
1236             if (sensor->terminus_handle != terminusHandle)
1237             {
1238                 return true;
1239             }
1240         }
1241         else if (pdr->type == PLDM_NUMERIC_EFFECTER_PDR)
1242         {
1243             auto numericEffecter =
1244                 reinterpret_cast<const pldm_numeric_effecter_value_pdr*>(data);
1245 
1246             if (numericEffecter->terminus_handle != terminusHandle)
1247             {
1248                 return true;
1249             }
1250         }
1251 
1252         else if (pdr->type == PLDM_STATE_EFFECTER_PDR)
1253         {
1254             auto stateEffecter =
1255                 reinterpret_cast<const pldm_state_effecter_pdr*>(data);
1256             if (stateEffecter->terminus_handle != terminusHandle)
1257             {
1258                 return true;
1259             }
1260         }
1261         else if (pdr->type == PLDM_PDR_FRU_RECORD_SET)
1262         {
1263             data += sizeof(pldm_pdr_hdr);
1264             auto fru = reinterpret_cast<const pldm_pdr_fru_record_set*>(data);
1265 
1266             if (fru->terminus_handle != terminusHandle)
1267             {
1268                 return true;
1269             }
1270         }
1271         else
1272         {
1273             // Entity association PDRs does not have terminus handle
1274             return true;
1275         }
1276 
1277         return false;
1278     }
1279 
printTerminusLocatorPDR(const uint8_t * data,ordered_json & output)1280     void printTerminusLocatorPDR(const uint8_t* data, ordered_json& output)
1281     {
1282         const std::array<std::string_view, 4> terminusLocatorType = {
1283             "UID", "MCTP_EID", "SMBusRelative", "systemSoftware"};
1284 
1285         auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1286 
1287         output["PLDMTerminusHandle"] = pdr->terminus_handle;
1288         output["validity"] = (pdr->validity ? "valid" : "notValid");
1289         output["TID"] = unsigned(pdr->tid);
1290         output["containerID"] = pdr->container_id;
1291         output["terminusLocatorType"] =
1292             terminusLocatorType[pdr->terminus_locator_type];
1293         output["terminusLocatorValueSize"] =
1294             unsigned(pdr->terminus_locator_value_size);
1295 
1296         if (pdr->terminus_locator_type == PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
1297         {
1298             auto locatorValue =
1299                 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
1300                     pdr->terminus_locator_value);
1301             output["EID"] = unsigned(locatorValue->eid);
1302         }
1303     }
1304 
getTerminusHandle(uint8_t * data,std::optional<uint8_t> tid)1305     std::optional<uint16_t> getTerminusHandle(uint8_t* data,
1306                                               std::optional<uint8_t> tid)
1307     {
1308         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1309         if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1310         {
1311             auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1312             if (pdr->tid == tid)
1313             {
1314                 handleFound = true;
1315                 return pdr->terminus_handle;
1316             }
1317         }
1318         return std::nullopt;
1319     }
1320 
1321     /** @brief Format the Numeric Sensor PDR types to json output
1322      *
1323      *  @param[in] data - reference to the Numeric Sensor PDR
1324      *  @param[in] data_length - number of PDR data bytes
1325      *  @param[out] output - PDRs data fields in Json format
1326      */
printNumericSensorPDR(const uint8_t * data,const uint16_t data_length,ordered_json & output)1327     void printNumericSensorPDR(const uint8_t* data, const uint16_t data_length,
1328                                ordered_json& output)
1329     {
1330         struct pldm_numeric_sensor_value_pdr pdr;
1331         int rc =
1332             decode_numeric_sensor_pdr_data(data, (size_t)data_length, &pdr);
1333         if (rc != PLDM_SUCCESS)
1334         {
1335             std::cerr << "Failed to get numeric sensor PDR" << std::endl;
1336             return;
1337         }
1338         output["PLDMTerminusHandle"] = pdr.terminus_handle;
1339         output["sensorID"] = pdr.sensor_id;
1340         output["entityType"] = getEntityName(pdr.entity_type);
1341         output["entityInstanceNumber"] = pdr.entity_instance_num;
1342         output["containerID"] = pdr.container_id;
1343         output["sensorInit"] = pdr.sensor_init;
1344         output["sensorAuxiliaryNamesPDR"] =
1345             (pdr.sensor_auxiliary_names_pdr) ? true : false;
1346         output["baseUnit"] = pdr.base_unit;
1347         output["unitModifier"] = pdr.unit_modifier;
1348         output["rateUnit"] = pdr.rate_unit;
1349         output["baseOEMUnitHandle"] = pdr.base_oem_unit_handle;
1350         output["auxUnit"] = pdr.aux_unit;
1351         output["auxUnitModifier"] = pdr.aux_unit_modifier;
1352         output["auxrateUnit"] = pdr.aux_rate_unit;
1353         output["rel"] = pdr.rel;
1354         output["auxOEMUnitHandle"] = pdr.aux_oem_unit_handle;
1355         output["isLinear"] = (pdr.is_linear) ? true : false;
1356         output["sensorDataSize"] = pdr.sensor_data_size;
1357         output["resolution"] = pdr.resolution;
1358         output["offset"] = pdr.offset;
1359         output["accuracy"] = pdr.accuracy;
1360         output["plusTolerance"] = pdr.plus_tolerance;
1361         output["minusTolerance"] = pdr.minus_tolerance;
1362 
1363         switch (pdr.sensor_data_size)
1364         {
1365             case PLDM_SENSOR_DATA_SIZE_UINT8:
1366                 output["hysteresis"] = pdr.hysteresis.value_u8;
1367                 output["maxReadable"] = pdr.max_readable.value_u8;
1368                 output["minReadable"] = pdr.min_readable.value_u8;
1369                 break;
1370             case PLDM_SENSOR_DATA_SIZE_SINT8:
1371                 output["hysteresis"] = pdr.hysteresis.value_s8;
1372                 output["maxReadable"] = pdr.max_readable.value_s8;
1373                 output["minReadable"] = pdr.min_readable.value_s8;
1374                 break;
1375             case PLDM_SENSOR_DATA_SIZE_UINT16:
1376                 output["hysteresis"] = pdr.hysteresis.value_u16;
1377                 output["maxReadable"] = pdr.max_readable.value_u16;
1378                 output["minReadable"] = pdr.min_readable.value_u16;
1379                 break;
1380             case PLDM_SENSOR_DATA_SIZE_SINT16:
1381                 output["hysteresis"] = pdr.hysteresis.value_s16;
1382                 output["maxReadable"] = pdr.max_readable.value_s16;
1383                 output["minReadable"] = pdr.min_readable.value_s16;
1384                 break;
1385             case PLDM_SENSOR_DATA_SIZE_UINT32:
1386                 output["hysteresis"] = pdr.hysteresis.value_u32;
1387                 output["maxReadable"] = pdr.max_readable.value_u32;
1388                 output["minReadable"] = pdr.min_readable.value_u32;
1389                 break;
1390             case PLDM_SENSOR_DATA_SIZE_SINT32:
1391                 output["hysteresis"] = pdr.hysteresis.value_s32;
1392                 output["maxReadable"] = pdr.max_readable.value_s32;
1393                 output["minReadable"] = pdr.min_readable.value_s32;
1394                 break;
1395             default:
1396                 break;
1397         }
1398 
1399         output["supportedThresholds"] = pdr.supported_thresholds.byte;
1400         output["thresholAndHysteresisVolatility"] =
1401             pdr.threshold_and_hysteresis_volatility.byte;
1402         output["stateTransitionInterval"] = pdr.state_transition_interval;
1403         output["updateInterval"] = pdr.update_interval;
1404         output["rangeFieldFormat"] = pdr.range_field_format;
1405         output["rangeFieldSupport"] = pdr.range_field_support.byte;
1406 
1407         switch (pdr.range_field_format)
1408         {
1409             case PLDM_RANGE_FIELD_FORMAT_UINT8:
1410                 output["nominalValue"] = pdr.nominal_value.value_u8;
1411                 output["normalMax"] = pdr.normal_max.value_u8;
1412                 output["normalMin"] = pdr.normal_min.value_u8;
1413                 output["warningHigh"] = pdr.warning_high.value_u8;
1414                 output["warningLow"] = pdr.warning_low.value_u8;
1415                 output["criticalHigh"] = pdr.critical_high.value_u8;
1416                 output["criticalLow"] = pdr.critical_low.value_u8;
1417                 output["fatalHigh"] = pdr.fatal_high.value_u8;
1418                 output["fatalLow"] = pdr.fatal_low.value_u8;
1419                 break;
1420             case PLDM_RANGE_FIELD_FORMAT_SINT8:
1421                 output["nominalValue"] = pdr.nominal_value.value_s8;
1422                 output["normalMax"] = pdr.normal_max.value_s8;
1423                 output["normalMin"] = pdr.normal_min.value_s8;
1424                 output["warningHigh"] = pdr.warning_high.value_s8;
1425                 output["warningLow"] = pdr.warning_low.value_s8;
1426                 output["criticalHigh"] = pdr.critical_high.value_s8;
1427                 output["criticalLow"] = pdr.critical_low.value_s8;
1428                 output["fatalHigh"] = pdr.fatal_high.value_s8;
1429                 output["fatalLow"] = pdr.fatal_low.value_s8;
1430                 break;
1431             case PLDM_RANGE_FIELD_FORMAT_UINT16:
1432                 output["nominalValue"] = pdr.nominal_value.value_u16;
1433                 output["normalMax"] = pdr.normal_max.value_u16;
1434                 output["normalMin"] = pdr.normal_min.value_u16;
1435                 output["warningHigh"] = pdr.warning_high.value_u16;
1436                 output["warningLow"] = pdr.warning_low.value_u16;
1437                 output["criticalHigh"] = pdr.critical_high.value_u16;
1438                 output["criticalLow"] = pdr.critical_low.value_u16;
1439                 output["fatalHigh"] = pdr.fatal_high.value_u16;
1440                 output["fatalLow"] = pdr.fatal_low.value_u16;
1441                 break;
1442             case PLDM_RANGE_FIELD_FORMAT_SINT16:
1443                 output["nominalValue"] = pdr.nominal_value.value_s16;
1444                 output["normalMax"] = pdr.normal_max.value_s16;
1445                 output["normalMin"] = pdr.normal_min.value_s16;
1446                 output["warningHigh"] = pdr.warning_high.value_s16;
1447                 output["warningLow"] = pdr.warning_low.value_s16;
1448                 output["criticalHigh"] = pdr.critical_high.value_s16;
1449                 output["criticalLow"] = pdr.critical_low.value_s16;
1450                 output["fatalHigh"] = pdr.fatal_high.value_s16;
1451                 output["fatalLow"] = pdr.fatal_low.value_s16;
1452                 break;
1453             case PLDM_RANGE_FIELD_FORMAT_UINT32:
1454                 output["nominalValue"] = pdr.nominal_value.value_u32;
1455                 output["normalMax"] = pdr.normal_max.value_u32;
1456                 output["normalMin"] = pdr.normal_min.value_u32;
1457                 output["warningHigh"] = pdr.warning_high.value_u32;
1458                 output["warningLow"] = pdr.warning_low.value_u32;
1459                 output["criticalHigh"] = pdr.critical_high.value_u32;
1460                 output["criticalLow"] = pdr.critical_low.value_u32;
1461                 output["fatalHigh"] = pdr.fatal_high.value_u32;
1462                 output["fatalLow"] = pdr.fatal_low.value_u32;
1463                 break;
1464             case PLDM_RANGE_FIELD_FORMAT_SINT32:
1465                 output["nominalValue"] = pdr.nominal_value.value_s32;
1466                 output["normalMax"] = pdr.normal_max.value_s32;
1467                 output["normalMin"] = pdr.normal_min.value_s32;
1468                 output["warningHigh"] = pdr.warning_high.value_s32;
1469                 output["warningLow"] = pdr.warning_low.value_s32;
1470                 output["criticalHigh"] = pdr.critical_high.value_s32;
1471                 output["criticalLow"] = pdr.critical_low.value_s32;
1472                 output["fatalHigh"] = pdr.fatal_high.value_s32;
1473                 output["fatalLow"] = pdr.fatal_low.value_s32;
1474                 break;
1475             case PLDM_RANGE_FIELD_FORMAT_REAL32:
1476                 output["nominalValue"] = pdr.nominal_value.value_f32;
1477                 output["normalMax"] = pdr.normal_max.value_f32;
1478                 output["normalMin"] = pdr.normal_min.value_f32;
1479                 output["warningHigh"] = pdr.warning_high.value_f32;
1480                 output["warningLow"] = pdr.warning_low.value_f32;
1481                 output["criticalHigh"] = pdr.critical_high.value_f32;
1482                 output["criticalLow"] = pdr.critical_low.value_f32;
1483                 output["fatalHigh"] = pdr.fatal_high.value_f32;
1484                 output["fatalLow"] = pdr.fatal_low.value_f32;
1485                 break;
1486             default:
1487                 break;
1488         }
1489     }
1490 
1491     /** @brief Format the Compact Numeric Sensor PDR types to json output
1492      *
1493      *  @param[in] data - reference to the Compact Numeric Sensor PDR
1494      *  @param[out] output - PDRs data fields in Json format
1495      */
printCompactNumericSensorPDR(const uint8_t * data,ordered_json & output)1496     void printCompactNumericSensorPDR(const uint8_t* data, ordered_json& output)
1497     {
1498         struct pldm_compact_numeric_sensor_pdr* pdr =
1499             (struct pldm_compact_numeric_sensor_pdr*)data;
1500         if (!pdr)
1501         {
1502             std::cerr << "Failed to get compact numeric sensor PDR"
1503                       << std::endl;
1504             return;
1505         }
1506         output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
1507         output["sensorID"] = int(pdr->sensor_id);
1508         output["entityType"] = getEntityName(pdr->entity_type);
1509         output["entityInstanceNumber"] = int(pdr->entity_instance);
1510         output["containerID"] = int(pdr->container_id);
1511         output["sensorNameStringByteLength"] = int(pdr->sensor_name_length);
1512         if (pdr->sensor_name_length == 0)
1513         {
1514             output["Name"] = std::format("PLDM_Device_TID{}_SensorId{}",
1515                                          unsigned(pdr->terminus_handle),
1516                                          unsigned(pdr->sensor_id));
1517         }
1518         else
1519         {
1520             std::string sTemp(reinterpret_cast<const char*>(pdr->sensor_name),
1521                               pdr->sensor_name_length);
1522             output["Name"] = sTemp;
1523         }
1524         output["baseUnit"] = unsigned(pdr->base_unit);
1525         output["unitModifier"] = signed(pdr->unit_modifier);
1526         output["occurrenceRate"] = unsigned(pdr->occurrence_rate);
1527         output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1528         if (pdr->range_field_support.bits.bit0)
1529         {
1530             output["warningHigh"] = int(pdr->warning_high);
1531         }
1532         if (pdr->range_field_support.bits.bit1)
1533         {
1534             output["warningLow"] = int(pdr->warning_low);
1535         }
1536         if (pdr->range_field_support.bits.bit2)
1537         {
1538             output["criticalHigh"] = int(pdr->critical_high);
1539         }
1540         if (pdr->range_field_support.bits.bit3)
1541         {
1542             output["criticalLow"] = int(pdr->critical_low);
1543         }
1544         if (pdr->range_field_support.bits.bit4)
1545         {
1546             output["fatalHigh"] = int(pdr->fatal_high);
1547         }
1548         if (pdr->range_field_support.bits.bit5)
1549         {
1550             output["fatalLow"] = int(pdr->fatal_low);
1551         }
1552     }
1553 
printPDRMsg(uint32_t & nextRecordHndl,const uint16_t respCnt,uint8_t * data,std::optional<uint16_t> terminusHandle)1554     void printPDRMsg(uint32_t& nextRecordHndl, const uint16_t respCnt,
1555                      uint8_t* data, std::optional<uint16_t> terminusHandle)
1556     {
1557         if (data == nullptr)
1558         {
1559             std::cerr << "Failed to get PDR message" << std::endl;
1560             return;
1561         }
1562 
1563         ordered_json output;
1564         output["nextRecordHandle"] = nextRecordHndl;
1565         output["responseCount"] = respCnt;
1566 
1567         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1568         if (!pdr)
1569         {
1570             return;
1571         }
1572 
1573         if (!pdrRecType.empty())
1574         {
1575             // Need to return if the requested PDR type
1576             // is not supported
1577             if (!strToPdrType.contains(pdrRecType))
1578             {
1579                 std::cerr << "PDR type '" << pdrRecType
1580                           << "' is not supported or invalid\n";
1581                 // PDR type not supported, setting next record handle to
1582                 // 0 to avoid looping through all PDR records
1583                 nextRecordHndl = 0;
1584                 return;
1585             }
1586 
1587             // Do not print PDR record if the current record
1588             // PDR type does not match with requested type
1589             if (pdr->type != strToPdrType.at(pdrRecType))
1590             {
1591                 return;
1592             }
1593         }
1594 
1595         if (pdrTerminus.has_value())
1596         {
1597             if (checkTerminusHandle(data, terminusHandle))
1598             {
1599                 std::cerr << "The Terminus handle doesn't match return"
1600                           << std::endl;
1601                 return;
1602             }
1603         }
1604 
1605         printCommonPDRHeader(pdr, output);
1606 
1607         switch (pdr->type)
1608         {
1609             case PLDM_TERMINUS_LOCATOR_PDR:
1610                 printTerminusLocatorPDR(data, output);
1611                 break;
1612             case PLDM_STATE_SENSOR_PDR:
1613                 printStateSensorPDR(data, output);
1614                 break;
1615             case PLDM_NUMERIC_EFFECTER_PDR:
1616                 printNumericEffecterPDR(data, output);
1617                 break;
1618             case PLDM_NUMERIC_SENSOR_PDR:
1619                 printNumericSensorPDR(data, respCnt, output);
1620                 break;
1621             case PLDM_SENSOR_AUXILIARY_NAMES_PDR:
1622             case PLDM_EFFECTER_AUXILIARY_NAMES_PDR:
1623                 printAuxNamePDR(data, output);
1624                 break;
1625             case PLDM_STATE_EFFECTER_PDR:
1626                 printStateEffecterPDR(data, output);
1627                 break;
1628             case PLDM_PDR_ENTITY_ASSOCIATION:
1629                 printPDREntityAssociation(data, output);
1630                 break;
1631             case PLDM_PDR_FRU_RECORD_SET:
1632                 printPDRFruRecordSet(data, output);
1633                 break;
1634             case PLDM_COMPACT_NUMERIC_SENSOR_PDR:
1635                 printCompactNumericSensorPDR(data, output);
1636                 break;
1637             default:
1638                 break;
1639         }
1640 
1641         if (!isFirstPDR)
1642         {
1643             std::cout << ",\n";
1644         }
1645         isFirstPDR = false;
1646         pldmtool::helper::DisplayInJson(output);
1647     }
1648 
1649   private:
1650     bool optTIDSet = false;
1651     uint32_t recordHandle;
1652     bool allPDRs;
1653     std::string pdrRecType;
1654     std::optional<uint8_t> pdrTerminus;
1655     std::optional<uint16_t> terminusHandle;
1656     bool handleFound = false;
1657     CLI::Option* getPDRGroupOption = nullptr;
1658     uint32_t dataTransferHandle;
1659     uint8_t operationFlag;
1660     uint16_t requestCount;
1661     uint16_t recordChangeNumber;
1662     std::vector<uint8_t> recordData;
1663     bool nextPartRequired;
1664     bool isFirstPDR = true;
1665 };
1666 
1667 class SetStateEffecter : public CommandInterface
1668 {
1669   public:
1670     ~SetStateEffecter() = default;
1671     SetStateEffecter() = delete;
1672     SetStateEffecter(const SetStateEffecter&) = delete;
1673     SetStateEffecter(SetStateEffecter&&) = default;
1674     SetStateEffecter& operator=(const SetStateEffecter&) = delete;
1675     SetStateEffecter& operator=(SetStateEffecter&&) = delete;
1676 
1677     // compositeEffecterCount(value: 0x01 to 0x08) * stateField(2)
1678     static constexpr auto maxEffecterDataSize = 16;
1679 
1680     // compositeEffecterCount(value: 0x01 to 0x08)
1681     static constexpr auto minEffecterCount = 1;
1682     static constexpr auto maxEffecterCount = 8;
SetStateEffecter(const char * type,const char * name,CLI::App * app)1683     explicit SetStateEffecter(const char* type, const char* name,
1684                               CLI::App* app) : CommandInterface(type, name, app)
1685     {
1686         app->add_option(
1687                "-i, --id", effecterId,
1688                "A handle that is used to identify and access the effecter")
1689             ->required();
1690         app->add_option("-c, --count", effecterCount,
1691                         "The number of individual sets of effecter information")
1692             ->required();
1693         app->add_option(
1694                "-d,--data", effecterData,
1695                "Set effecter state data\n"
1696                "eg: requestSet0 effecterState0 noChange1 dummyState1 ...")
1697             ->required();
1698     }
1699 
createRequestMsg()1700     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1701     {
1702         std::vector<uint8_t> requestMsg(
1703             sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES);
1704         auto request = new (requestMsg.data()) pldm_msg;
1705 
1706         if (effecterCount > maxEffecterCount ||
1707             effecterCount < minEffecterCount)
1708         {
1709             std::cerr << "Request Message Error: effecterCount size "
1710                       << effecterCount << "is invalid\n";
1711             auto rc = PLDM_ERROR_INVALID_DATA;
1712             return {rc, requestMsg};
1713         }
1714 
1715         if (effecterData.size() > maxEffecterDataSize)
1716         {
1717             std::cerr << "Request Message Error: effecterData size "
1718                       << effecterData.size() << "is invalid\n";
1719             auto rc = PLDM_ERROR_INVALID_DATA;
1720             return {rc, requestMsg};
1721         }
1722 
1723         auto stateField = parseEffecterData(effecterData, effecterCount);
1724         if (!stateField)
1725         {
1726             std::cerr << "Failed to parse effecter data, effecterCount size "
1727                       << effecterCount << "\n";
1728             auto rc = PLDM_ERROR_INVALID_DATA;
1729             return {rc, requestMsg};
1730         }
1731 
1732         auto rc = encode_set_state_effecter_states_req(
1733             instanceId, effecterId, effecterCount, stateField->data(), request);
1734         return {rc, requestMsg};
1735     }
1736 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1737     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1738     {
1739         uint8_t completionCode = 0;
1740         auto rc = decode_set_state_effecter_states_resp(
1741             responsePtr, payloadLength, &completionCode);
1742 
1743         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1744         {
1745             std::cerr << "Response Message Error: "
1746                       << "rc=" << rc << ",cc=" << (int)completionCode << "\n";
1747             return;
1748         }
1749 
1750         ordered_json data;
1751         data["Response"] = "SUCCESS";
1752         pldmtool::helper::DisplayInJson(data);
1753     }
1754 
1755   private:
1756     uint16_t effecterId;
1757     uint8_t effecterCount;
1758     std::vector<uint8_t> effecterData;
1759 };
1760 
1761 class SetNumericEffecterValue : public CommandInterface
1762 {
1763   public:
1764     ~SetNumericEffecterValue() = default;
1765     SetNumericEffecterValue() = delete;
1766     SetNumericEffecterValue(const SetNumericEffecterValue&) = delete;
1767     SetNumericEffecterValue(SetNumericEffecterValue&&) = default;
1768     SetNumericEffecterValue& operator=(const SetNumericEffecterValue&) = delete;
1769     SetNumericEffecterValue& operator=(SetNumericEffecterValue&&) = delete;
1770 
SetNumericEffecterValue(const char * type,const char * name,CLI::App * app)1771     explicit SetNumericEffecterValue(const char* type, const char* name,
1772                                      CLI::App* app) :
1773         CommandInterface(type, name, app)
1774     {
1775         app->add_option(
1776                "-i, --id", effecterId,
1777                "A handle that is used to identify and access the effecter")
1778             ->required();
1779         app->add_option("-s, --size", effecterDataSize,
1780                         "The bit width and format of the setting value for the "
1781                         "effecter. enum value: {uint8, sint8, uint16, sint16, "
1782                         "uint32, sint32}\n")
1783             ->required();
1784         app->add_option("-d,--data", maxEffecterValue,
1785                         "The setting value of numeric effecter being "
1786                         "requested\n")
1787             ->required();
1788     }
1789 
createRequestMsg()1790     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1791     {
1792         std::vector<uint8_t> requestMsg(
1793             sizeof(pldm_msg_hdr) +
1794             PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3);
1795 
1796         uint8_t* effecterValue = (uint8_t*)&maxEffecterValue;
1797 
1798         auto request = new (requestMsg.data()) pldm_msg;
1799 
1800         size_t payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES;
1801 
1802         if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1803             effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT16)
1804         {
1805             payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1;
1806         }
1807         if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1808             effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT32)
1809         {
1810             payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3;
1811         }
1812         auto rc = encode_set_numeric_effecter_value_req(
1813             0, effecterId, effecterDataSize, effecterValue, request,
1814             payload_length);
1815 
1816         return {rc, requestMsg};
1817     }
1818 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1819     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1820     {
1821         uint8_t completionCode = 0;
1822         auto rc = decode_set_numeric_effecter_value_resp(
1823             responsePtr, payloadLength, &completionCode);
1824 
1825         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1826         {
1827             std::cerr << "Response Message Error: "
1828                       << "rc=" << rc << ",cc=" << (int)completionCode
1829                       << std::endl;
1830             return;
1831         }
1832 
1833         ordered_json data;
1834         data["Response"] = "SUCCESS";
1835         pldmtool::helper::DisplayInJson(data);
1836     }
1837 
1838   private:
1839     uint16_t effecterId;
1840     uint8_t effecterDataSize;
1841     uint64_t maxEffecterValue;
1842 };
1843 
1844 class GetStateSensorReadings : public CommandInterface
1845 {
1846   public:
1847     ~GetStateSensorReadings() = default;
1848     GetStateSensorReadings() = delete;
1849     GetStateSensorReadings(const GetStateSensorReadings&) = delete;
1850     GetStateSensorReadings(GetStateSensorReadings&&) = default;
1851     GetStateSensorReadings& operator=(const GetStateSensorReadings&) = delete;
1852     GetStateSensorReadings& operator=(GetStateSensorReadings&&) = delete;
1853 
GetStateSensorReadings(const char * type,const char * name,CLI::App * app)1854     explicit GetStateSensorReadings(const char* type, const char* name,
1855                                     CLI::App* app) :
1856         CommandInterface(type, name, app)
1857     {
1858         app->add_option(
1859                "-i, --sensor_id", sensorId,
1860                "Sensor ID that is used to identify and access the sensor")
1861             ->required();
1862         app->add_option("-r, --rearm", sensorRearm,
1863                         "Each bit location in this field corresponds to a "
1864                         "particular sensor")
1865             ->required();
1866     }
1867 
createRequestMsg()1868     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1869     {
1870         std::vector<uint8_t> requestMsg(
1871             sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
1872         auto request = new (requestMsg.data()) pldm_msg;
1873 
1874         uint8_t reserved = 0;
1875         bitfield8_t bf;
1876         bf.byte = sensorRearm;
1877         auto rc = encode_get_state_sensor_readings_req(instanceId, sensorId, bf,
1878                                                        reserved, request);
1879 
1880         return {rc, requestMsg};
1881     }
1882 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1883     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1884     {
1885         uint8_t completionCode = 0;
1886         uint8_t compSensorCount = 0;
1887         std::array<get_sensor_state_field, 8> stateField{};
1888         auto rc = decode_get_state_sensor_readings_resp(
1889             responsePtr, payloadLength, &completionCode, &compSensorCount,
1890             stateField.data());
1891 
1892         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1893         {
1894             std::cerr << "Response Message Error: "
1895                       << "rc=" << rc << ",cc=" << (int)completionCode
1896                       << std::endl;
1897             return;
1898         }
1899         ordered_json output;
1900         output["compositeSensorCount"] = (int)compSensorCount;
1901 
1902         for (size_t i = 0; i < compSensorCount; i++)
1903         {
1904             if (sensorOpState.contains(stateField[i].sensor_op_state))
1905             {
1906                 output.emplace(("sensorOpState[" + std::to_string(i) + "]"),
1907                                sensorOpState.at(stateField[i].sensor_op_state));
1908             }
1909             output.emplace(("presentState[" + std::to_string(i) + "]"),
1910                            std::to_string(stateField[i].present_state));
1911             output.emplace(("previousState[" + std::to_string(i) + "]"),
1912                            std::to_string(stateField[i].previous_state));
1913             output.emplace(("eventState[" + std::to_string(i) + "]"),
1914                            std::to_string(stateField[i].event_state));
1915         }
1916 
1917         pldmtool::helper::DisplayInJson(output);
1918     }
1919 
1920   private:
1921     uint16_t sensorId;
1922     uint8_t sensorRearm;
1923 };
1924 
1925 class GetSensorReading : public CommandInterface
1926 {
1927   public:
1928     ~GetSensorReading() = default;
1929     GetSensorReading() = delete;
1930     GetSensorReading(const GetSensorReading&) = delete;
1931     GetSensorReading(GetSensorReading&&) = default;
1932     GetSensorReading& operator=(const GetSensorReading&) = delete;
1933     GetSensorReading& operator=(GetSensorReading&&) = delete;
1934 
GetSensorReading(const char * type,const char * name,CLI::App * app)1935     explicit GetSensorReading(const char* type, const char* name,
1936                               CLI::App* app) : CommandInterface(type, name, app)
1937     {
1938         app->add_option(
1939                "-i, --sensor_id", sensorId,
1940                "Sensor ID that is used to identify and access the sensor")
1941             ->required();
1942         app->add_option("-r, --rearm", rearm,
1943                         "Manually re-arm EventState after "
1944                         "responding to this request")
1945             ->required();
1946     }
1947 
createRequestMsg()1948     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1949     {
1950         std::vector<uint8_t> requestMsg(
1951             sizeof(pldm_msg_hdr) + PLDM_GET_SENSOR_READING_REQ_BYTES);
1952         auto request = new (requestMsg.data()) pldm_msg;
1953 
1954         auto rc =
1955             encode_get_sensor_reading_req(instanceId, sensorId, rearm, request);
1956 
1957         return {rc, requestMsg};
1958     }
1959 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1960     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1961     {
1962         uint8_t completionCode = 0;
1963         uint8_t sensorDataSize = 0;
1964         uint8_t sensorOperationalState = 0;
1965         uint8_t sensorEventMessageEnable = 0;
1966         uint8_t presentState = 0;
1967         uint8_t previousState = 0;
1968         uint8_t eventState = 0;
1969         std::array<uint8_t, sizeof(uint32_t)>
1970             presentReading{}; // maximum size for the present Value is uint32
1971                               // according to spec DSP0248
1972 
1973         auto rc = decode_get_sensor_reading_resp(
1974             responsePtr, payloadLength, &completionCode, &sensorDataSize,
1975             &sensorOperationalState, &sensorEventMessageEnable, &presentState,
1976             &previousState, &eventState, presentReading.data());
1977 
1978         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1979         {
1980             std::cerr << "Response Message Error: "
1981                       << "rc=" << rc << ",cc=" << (int)completionCode
1982                       << std::endl;
1983             return;
1984         }
1985 
1986         ordered_json output;
1987         output["sensorDataSize"] =
1988             getSensorState(sensorDataSize, &sensorDataSz);
1989         output["sensorOperationalState"] =
1990             getSensorState(sensorOperationalState, &sensorOpState);
1991         output["sensorEventMessageEnable"] =
1992             getSensorState(sensorEventMessageEnable, &sensorEventMsgEnable);
1993         output["presentState"] = getSensorState(presentState, &sensorPresState);
1994         output["previousState"] =
1995             getSensorState(previousState, &sensorPresState);
1996         output["eventState"] = getSensorState(eventState, &sensorPresState);
1997 
1998         switch (sensorDataSize)
1999         {
2000             case PLDM_SENSOR_DATA_SIZE_UINT8:
2001             {
2002                 output["presentReading"] =
2003                     *(reinterpret_cast<uint8_t*>(presentReading.data()));
2004                 break;
2005             }
2006             case PLDM_SENSOR_DATA_SIZE_SINT8:
2007             {
2008                 output["presentReading"] =
2009                     *(reinterpret_cast<int8_t*>(presentReading.data()));
2010                 break;
2011             }
2012             case PLDM_SENSOR_DATA_SIZE_UINT16:
2013             {
2014                 output["presentReading"] =
2015                     *(reinterpret_cast<uint16_t*>(presentReading.data()));
2016                 break;
2017             }
2018             case PLDM_SENSOR_DATA_SIZE_SINT16:
2019             {
2020                 output["presentReading"] =
2021                     *(reinterpret_cast<int16_t*>(presentReading.data()));
2022                 break;
2023             }
2024             case PLDM_SENSOR_DATA_SIZE_UINT32:
2025             {
2026                 output["presentReading"] =
2027                     *(reinterpret_cast<uint32_t*>(presentReading.data()));
2028                 break;
2029             }
2030             case PLDM_SENSOR_DATA_SIZE_SINT32:
2031             {
2032                 output["presentReading"] =
2033                     *(reinterpret_cast<int32_t*>(presentReading.data()));
2034                 break;
2035             }
2036             default:
2037             {
2038                 std::cerr << "Unknown Sensor Data Size : "
2039                           << static_cast<int>(sensorDataSize) << std::endl;
2040                 break;
2041             }
2042         }
2043 
2044         pldmtool::helper::DisplayInJson(output);
2045     }
2046 
2047   private:
2048     uint16_t sensorId;
2049     uint8_t rearm;
2050 
2051     const std::map<uint8_t, std::string> sensorDataSz = {
2052         {PLDM_SENSOR_DATA_SIZE_UINT8, "uint8"},
2053         {PLDM_SENSOR_DATA_SIZE_SINT8, "uint8"},
2054         {PLDM_SENSOR_DATA_SIZE_UINT16, "uint16"},
2055         {PLDM_SENSOR_DATA_SIZE_SINT16, "uint16"},
2056         {PLDM_SENSOR_DATA_SIZE_UINT32, "uint32"},
2057         {PLDM_SENSOR_DATA_SIZE_SINT32, "uint32"}};
2058 
2059     static inline const std::map<uint8_t, std::string> sensorEventMsgEnable{
2060         {PLDM_NO_EVENT_GENERATION, "Sensor No Event Generation"},
2061         {PLDM_EVENTS_DISABLED, "Sensor Events Disabled"},
2062         {PLDM_EVENTS_ENABLED, "Sensor Events Enabled"},
2063         {PLDM_OP_EVENTS_ONLY_ENABLED, "Sensor Op Events Only Enabled"},
2064         {PLDM_STATE_EVENTS_ONLY_ENABLED, "Sensor State Events Only Enabled"}};
2065 
getSensorState(uint8_t state,const std::map<uint8_t,std::string> * cont)2066     std::string getSensorState(uint8_t state,
2067                                const std::map<uint8_t, std::string>* cont)
2068     {
2069         auto typeString = std::to_string(state);
2070         try
2071         {
2072             return cont->at(state);
2073         }
2074         catch (const std::out_of_range& e)
2075         {
2076             return typeString;
2077         }
2078     }
2079 };
2080 
2081 class GetStateEffecterStates : public CommandInterface
2082 {
2083   public:
2084     ~GetStateEffecterStates() = default;
2085     GetStateEffecterStates() = delete;
2086     GetStateEffecterStates(const GetStateEffecterStates&) = delete;
2087     GetStateEffecterStates(GetStateEffecterStates&&) = default;
2088     GetStateEffecterStates& operator=(const GetStateEffecterStates&) = delete;
2089     GetStateEffecterStates& operator=(GetStateEffecterStates&&) = delete;
2090 
GetStateEffecterStates(const char * type,const char * name,CLI::App * app)2091     explicit GetStateEffecterStates(const char* type, const char* name,
2092                                     CLI::App* app) :
2093         CommandInterface(type, name, app)
2094     {
2095         app->add_option(
2096                "-i, --effecter_id", effecter_id,
2097                "Effecter ID that is used to identify and access the effecter")
2098             ->required();
2099     }
2100 
createRequestMsg()2101     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
2102     {
2103         std::vector<uint8_t> requestMsg(
2104             sizeof(pldm_msg_hdr) + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
2105         auto request = new (requestMsg.data()) pldm_msg;
2106 
2107         auto rc = encode_get_state_effecter_states_req(
2108             instanceId, effecter_id, request,
2109             PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
2110 
2111         return {rc, requestMsg};
2112     }
2113 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)2114     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
2115     {
2116         struct pldm_get_state_effecter_states_resp resp;
2117         auto rc = decode_get_state_effecter_states_resp(responsePtr,
2118                                                         payloadLength, &resp);
2119 
2120         if (rc || resp.completion_code != PLDM_SUCCESS)
2121         {
2122             std::cerr << "Response Message Error: "
2123                       << "rc=" << rc << ",cc="
2124                       << static_cast<int>(resp.completion_code) << std::endl;
2125             return;
2126         }
2127         ordered_json output;
2128         auto comp_effecter_count = static_cast<int>(resp.comp_effecter_count);
2129         output["compositeEffecterCount"] = comp_effecter_count;
2130 
2131         for (auto i : std::views::iota(0, comp_effecter_count))
2132         {
2133             output[std::format("effecterOpState[{}])", i)] =
2134                 getEffecterOpState(resp.field[i].effecter_op_state);
2135 
2136             output[std::format("pendingState[{}]", i)] =
2137                 resp.field[i].pending_state;
2138 
2139             output[std::format("presentState[{}]", i)] =
2140                 resp.field[i].present_state;
2141         }
2142 
2143         pldmtool::helper::DisplayInJson(output);
2144     }
2145 
2146   private:
2147     uint16_t effecter_id;
2148 };
2149 
2150 class GetNumericEffecterValue : public CommandInterface
2151 {
2152   public:
2153     ~GetNumericEffecterValue() = default;
2154     GetNumericEffecterValue() = delete;
2155     GetNumericEffecterValue(const GetNumericEffecterValue&) = delete;
2156     GetNumericEffecterValue(GetNumericEffecterValue&&) = default;
2157     GetNumericEffecterValue& operator=(const GetNumericEffecterValue&) = delete;
2158     GetNumericEffecterValue& operator=(GetNumericEffecterValue&&) = delete;
2159 
GetNumericEffecterValue(const char * type,const char * name,CLI::App * app)2160     explicit GetNumericEffecterValue(const char* type, const char* name,
2161                                      CLI::App* app) :
2162         CommandInterface(type, name, app)
2163     {
2164         app->add_option(
2165                "-i, --effecter_id", effecterId,
2166                "A handle that is used to identify and access the effecter")
2167             ->required();
2168     }
2169 
createRequestMsg()2170     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
2171     {
2172         std::vector<uint8_t> requestMsg(
2173             sizeof(pldm_msg_hdr) + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
2174         auto request = new (requestMsg.data()) pldm_msg;
2175 
2176         auto rc = encode_get_numeric_effecter_value_req(instanceId, effecterId,
2177                                                         request);
2178 
2179         return {rc, requestMsg};
2180     }
2181 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)2182     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
2183     {
2184         uint8_t completionCode = 0;
2185         uint8_t effecterDataSize = 0;
2186         uint8_t effecterOperationalState = 0;
2187         std::array<uint8_t, sizeof(uint32_t)>
2188             pendingValue{}; // maximum size for the pending Value is uint32
2189                             // according to spec DSP0248
2190         std::array<uint8_t, sizeof(uint32_t)>
2191             presentValue{}; // maximum size for the present Value is uint32
2192                             // according to spec DSP0248
2193 
2194         auto rc = decode_get_numeric_effecter_value_resp(
2195             responsePtr, payloadLength, &completionCode, &effecterDataSize,
2196             &effecterOperationalState, pendingValue.data(),
2197             presentValue.data());
2198 
2199         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
2200         {
2201             std::cerr << "Response Message Error: "
2202                       << "rc=" << rc << ",cc="
2203                       << static_cast<int>(completionCode) << std::endl;
2204             return;
2205         }
2206 
2207         ordered_json output;
2208         output["effecterDataSize"] = static_cast<int>(effecterDataSize);
2209         output["effecterOperationalState"] =
2210             getEffecterOpState(effecterOperationalState);
2211 
2212         switch (effecterDataSize)
2213         {
2214             case PLDM_EFFECTER_DATA_SIZE_UINT8:
2215             {
2216                 output["pendingValue"] =
2217                     *(reinterpret_cast<uint8_t*>(pendingValue.data()));
2218                 output["presentValue"] =
2219                     *(reinterpret_cast<uint8_t*>(presentValue.data()));
2220                 break;
2221             }
2222             case PLDM_EFFECTER_DATA_SIZE_SINT8:
2223             {
2224                 output["pendingValue"] =
2225                     *(reinterpret_cast<int8_t*>(pendingValue.data()));
2226                 output["presentValue"] =
2227                     *(reinterpret_cast<int8_t*>(presentValue.data()));
2228                 break;
2229             }
2230             case PLDM_EFFECTER_DATA_SIZE_UINT16:
2231             {
2232                 output["pendingValue"] =
2233                     *(reinterpret_cast<uint16_t*>(pendingValue.data()));
2234                 output["presentValue"] =
2235                     *(reinterpret_cast<uint16_t*>(presentValue.data()));
2236                 break;
2237             }
2238             case PLDM_EFFECTER_DATA_SIZE_SINT16:
2239             {
2240                 output["pendingValue"] =
2241                     *(reinterpret_cast<int16_t*>(pendingValue.data()));
2242                 output["presentValue"] =
2243                     *(reinterpret_cast<int16_t*>(presentValue.data()));
2244                 break;
2245             }
2246             case PLDM_EFFECTER_DATA_SIZE_UINT32:
2247             {
2248                 output["pendingValue"] =
2249                     *(reinterpret_cast<uint32_t*>(pendingValue.data()));
2250                 output["presentValue"] =
2251                     *(reinterpret_cast<uint32_t*>(presentValue.data()));
2252                 break;
2253             }
2254             case PLDM_EFFECTER_DATA_SIZE_SINT32:
2255             {
2256                 output["pendingValue"] =
2257                     *(reinterpret_cast<int32_t*>(pendingValue.data()));
2258                 output["presentValue"] =
2259                     *(reinterpret_cast<int32_t*>(presentValue.data()));
2260                 break;
2261             }
2262             default:
2263             {
2264                 std::cerr << "Unknown Effecter Data Size : "
2265                           << static_cast<int>(effecterDataSize) << std::endl;
2266                 break;
2267             }
2268         }
2269 
2270         pldmtool::helper::DisplayInJson(output);
2271     }
2272 
2273   private:
2274     uint16_t effecterId;
2275 };
2276 
registerCommand(CLI::App & app)2277 void registerCommand(CLI::App& app)
2278 {
2279     auto platform = app.add_subcommand("platform", "platform type command");
2280     platform->require_subcommand(1);
2281 
2282     auto getEventReceiver = platform->add_subcommand(
2283         "GetEventReceiver",
2284         "Get the configured event receiver from a terminus");
2285     commands.push_back(std::make_unique<GetEventReceiver>(
2286         "platform", "getEventReceiver", getEventReceiver));
2287 
2288     auto getPDR =
2289         platform->add_subcommand("GetPDR", "get platform descriptor records");
2290     commands.push_back(std::make_unique<GetPDR>("platform", "getPDR", getPDR));
2291 
2292     auto setStateEffecterStates = platform->add_subcommand(
2293         "SetStateEffecterStates", "set effecter states");
2294     commands.push_back(std::make_unique<SetStateEffecter>(
2295         "platform", "setStateEffecterStates", setStateEffecterStates));
2296 
2297     auto setNumericEffecterValue = platform->add_subcommand(
2298         "SetNumericEffecterValue", "set the value for a PLDM Numeric Effecter");
2299     commands.push_back(std::make_unique<SetNumericEffecterValue>(
2300         "platform", "setNumericEffecterValue", setNumericEffecterValue));
2301 
2302     auto getStateSensorReadings = platform->add_subcommand(
2303         "GetStateSensorReadings", "get the state sensor readings");
2304     commands.push_back(std::make_unique<GetStateSensorReadings>(
2305         "platform", "getStateSensorReadings", getStateSensorReadings));
2306 
2307     auto getNumericEffecterValue = platform->add_subcommand(
2308         "GetNumericEffecterValue", "get the numeric effecter value");
2309     commands.push_back(std::make_unique<GetNumericEffecterValue>(
2310         "platform", "getNumericEffecterValue", getNumericEffecterValue));
2311 
2312     auto getSensorReading = platform->add_subcommand(
2313         "GetSensorReading", "get the numeric sensor reading");
2314     commands.push_back(std::make_unique<GetSensorReading>(
2315         "platform", "getSensorReading", getSensorReading));
2316 
2317     auto getStateEffecterStates = platform->add_subcommand(
2318         "GetStateEffecterStates", "get the state effecter states");
2319     commands.push_back(std::make_unique<GetStateEffecterStates>(
2320         "platform", "getStateEffecterStates", getStateEffecterStates));
2321 }
2322 
parseGetPDROption()2323 void parseGetPDROption()
2324 {
2325     for (const auto& command : commands)
2326     {
2327         if (command.get()->getPLDMType() == "platform" &&
2328             command.get()->getCommandName() == "getPDR")
2329         {
2330             auto getPDR = dynamic_cast<GetPDR*>(command.get());
2331             getPDR->parseGetPDROptions();
2332         }
2333     }
2334 }
2335 
2336 } // namespace platform
2337 } // namespace pldmtool
2338