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