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