xref: /openbmc/pldm/pldmtool/pldm_platform_cmd.cpp (revision 15d42c55a8e0420f164e10621e42d5809770a478)
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 #pragma GCC diagnostic push
1035 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1036                 std::string nameString =
1037                     std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
1038                                          char16_t>{}
1039                         .to_bytes(u16NameString);
1040 #pragma GCC diagnostic pop
1041                 output[nameLanguageTagKey] = nameLanguageTag;
1042                 output[entityAuxNameKey] = nameString;
1043             }
1044         }
1045     }
1046 
printNumericEffecterPDR(uint8_t * data,ordered_json & output)1047     void printNumericEffecterPDR(uint8_t* data, ordered_json& output)
1048     {
1049         struct pldm_numeric_effecter_value_pdr* pdr =
1050             (struct pldm_numeric_effecter_value_pdr*)data;
1051         if (!pdr)
1052         {
1053             std::cerr << "Failed to get numeric effecter PDR" << std::endl;
1054             return;
1055         }
1056 
1057         output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
1058         output["effecterID"] = int(pdr->effecter_id);
1059         output["entityType"] = int(pdr->entity_type);
1060         output["entityInstanceNumber"] = int(pdr->entity_instance);
1061         output["containerID"] = int(pdr->container_id);
1062         output["effecterSemanticID"] = int(pdr->effecter_semantic_id);
1063         output["effecterInit"] = unsigned(pdr->effecter_init);
1064         output["effecterAuxiliaryNames"] =
1065             (unsigned(pdr->effecter_auxiliary_names) ? true : false);
1066         output["baseUnit"] = unsigned(pdr->base_unit);
1067         output["unitModifier"] = unsigned(pdr->unit_modifier);
1068         output["rateUnit"] = unsigned(pdr->rate_unit);
1069         output["baseOEMUnitHandle"] = unsigned(pdr->base_oem_unit_handle);
1070         output["auxUnit"] = unsigned(pdr->aux_unit);
1071         output["auxUnitModifier"] = unsigned(pdr->aux_unit_modifier);
1072         output["auxrateUnit"] = unsigned(pdr->aux_rate_unit);
1073         output["auxOEMUnitHandle"] = unsigned(pdr->aux_oem_unit_handle);
1074         output["isLinear"] = (unsigned(pdr->is_linear) ? true : false);
1075         output["effecterDataSize"] = unsigned(pdr->effecter_data_size);
1076         output["resolution"] = unsigned(pdr->resolution);
1077         output["offset"] = unsigned(pdr->offset);
1078         output["accuracy"] = unsigned(pdr->accuracy);
1079         output["plusTolerance"] = unsigned(pdr->plus_tolerance);
1080         output["minusTolerance"] = unsigned(pdr->minus_tolerance);
1081         output["stateTransitionInterval"] =
1082             unsigned(pdr->state_transition_interval);
1083         output["TransitionInterval"] = unsigned(pdr->transition_interval);
1084 
1085         switch (pdr->effecter_data_size)
1086         {
1087             case PLDM_EFFECTER_DATA_SIZE_UINT8:
1088                 output["maxSettable"] = unsigned(pdr->max_settable.value_u8);
1089                 output["minSettable"] = unsigned(pdr->min_settable.value_u8);
1090                 break;
1091             case PLDM_EFFECTER_DATA_SIZE_SINT8:
1092                 output["maxSettable"] = unsigned(pdr->max_settable.value_s8);
1093                 output["minSettable"] = unsigned(pdr->min_settable.value_s8);
1094                 break;
1095             case PLDM_EFFECTER_DATA_SIZE_UINT16:
1096                 output["maxSettable"] = unsigned(pdr->max_settable.value_u16);
1097                 output["minSettable"] = unsigned(pdr->min_settable.value_u16);
1098                 break;
1099             case PLDM_EFFECTER_DATA_SIZE_SINT16:
1100                 output["maxSettable"] = unsigned(pdr->max_settable.value_s16);
1101                 output["minSettable"] = unsigned(pdr->min_settable.value_s16);
1102                 break;
1103             case PLDM_EFFECTER_DATA_SIZE_UINT32:
1104                 output["maxSettable"] = unsigned(pdr->max_settable.value_u32);
1105                 output["minSettable"] = unsigned(pdr->min_settable.value_u32);
1106                 break;
1107             case PLDM_EFFECTER_DATA_SIZE_SINT32:
1108                 output["maxSettable"] = unsigned(pdr->max_settable.value_s32);
1109                 output["minSettable"] = unsigned(pdr->min_settable.value_s32);
1110                 break;
1111             default:
1112                 break;
1113         }
1114 
1115         output["rangeFieldFormat"] = unsigned(pdr->range_field_format);
1116         output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1117 
1118         switch (pdr->range_field_format)
1119         {
1120             case PLDM_RANGE_FIELD_FORMAT_UINT8:
1121                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u8);
1122                 output["normalMax"] = unsigned(pdr->normal_max.value_u8);
1123                 output["normalMin"] = unsigned(pdr->normal_min.value_u8);
1124                 output["ratedMax"] = unsigned(pdr->rated_max.value_u8);
1125                 output["ratedMin"] = unsigned(pdr->rated_min.value_u8);
1126                 break;
1127             case PLDM_RANGE_FIELD_FORMAT_SINT8:
1128                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s8);
1129                 output["normalMax"] = unsigned(pdr->normal_max.value_s8);
1130                 output["normalMin"] = unsigned(pdr->normal_min.value_s8);
1131                 output["ratedMax"] = unsigned(pdr->rated_max.value_s8);
1132                 output["ratedMin"] = unsigned(pdr->rated_min.value_s8);
1133                 break;
1134             case PLDM_RANGE_FIELD_FORMAT_UINT16:
1135                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u16);
1136                 output["normalMax"] = unsigned(pdr->normal_max.value_u16);
1137                 output["normalMin"] = unsigned(pdr->normal_min.value_u16);
1138                 output["ratedMax"] = unsigned(pdr->rated_max.value_u16);
1139                 output["ratedMin"] = unsigned(pdr->rated_min.value_u16);
1140                 break;
1141             case PLDM_RANGE_FIELD_FORMAT_SINT16:
1142                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s16);
1143                 output["normalMax"] = unsigned(pdr->normal_max.value_s16);
1144                 output["normalMin"] = unsigned(pdr->normal_min.value_s16);
1145                 output["ratedMax"] = unsigned(pdr->rated_max.value_s16);
1146                 output["ratedMin"] = unsigned(pdr->rated_min.value_s16);
1147                 break;
1148             case PLDM_RANGE_FIELD_FORMAT_UINT32:
1149                 output["nominalValue"] = unsigned(pdr->nominal_value.value_u32);
1150                 output["normalMax"] = unsigned(pdr->normal_max.value_u32);
1151                 output["normalMin"] = unsigned(pdr->normal_min.value_u32);
1152                 output["ratedMax"] = unsigned(pdr->rated_max.value_u32);
1153                 output["ratedMin"] = unsigned(pdr->rated_min.value_u32);
1154                 break;
1155             case PLDM_RANGE_FIELD_FORMAT_SINT32:
1156                 output["nominalValue"] = unsigned(pdr->nominal_value.value_s32);
1157                 output["normalMax"] = unsigned(pdr->normal_max.value_s32);
1158                 output["normalMin"] = unsigned(pdr->normal_min.value_s32);
1159                 output["ratedMax"] = unsigned(pdr->rated_max.value_s32);
1160                 output["ratedMin"] = unsigned(pdr->rated_min.value_s32);
1161                 break;
1162             case PLDM_RANGE_FIELD_FORMAT_REAL32:
1163                 output["nominalValue"] = unsigned(pdr->nominal_value.value_f32);
1164                 output["normalMax"] = unsigned(pdr->normal_max.value_f32);
1165                 output["normalMin"] = unsigned(pdr->normal_min.value_f32);
1166                 output["ratedMax"] = unsigned(pdr->rated_max.value_f32);
1167                 output["ratedMin"] = unsigned(pdr->rated_min.value_f32);
1168                 break;
1169             default:
1170                 break;
1171         }
1172     }
1173 
printStateEffecterPDR(const uint8_t * data,ordered_json & output)1174     void printStateEffecterPDR(const uint8_t* data, ordered_json& output)
1175     {
1176         auto pdr = reinterpret_cast<const pldm_state_effecter_pdr*>(data);
1177 
1178         output["PLDMTerminusHandle"] = pdr->terminus_handle;
1179         output["effecterID"] = pdr->effecter_id;
1180         output["entityType"] = getEntityName(pdr->entity_type);
1181         output["entityInstanceNumber"] = pdr->entity_instance;
1182         output["containerID"] = pdr->container_id;
1183         output["effecterSemanticID"] = pdr->effecter_semantic_id;
1184         output["effecterInit"] = effecterInit[pdr->effecter_init];
1185         output["effecterDescriptionPDR"] =
1186             (pdr->has_description_pdr ? true : false);
1187         output["compositeEffecterCount"] =
1188             unsigned(pdr->composite_effecter_count);
1189 
1190         auto statesPtr = pdr->possible_states;
1191         auto compEffCount = pdr->composite_effecter_count;
1192 
1193         while (compEffCount--)
1194         {
1195             auto state =
1196                 reinterpret_cast<const state_effecter_possible_states*>(
1197                     statesPtr);
1198             output.emplace(("stateSetID[" + std::to_string(compEffCount) + "]"),
1199                            getStateSetName(state->state_set_id));
1200             output.emplace(
1201                 ("possibleStatesSize[" + std::to_string(compEffCount) + "]"),
1202                 state->possible_states_size);
1203             output.emplace(
1204                 ("possibleStates[" + std::to_string(compEffCount) + "]"),
1205                 getStateSetPossibleStateNames(
1206                     state->state_set_id,
1207                     printPossibleStates(state->possible_states_size,
1208                                         state->states)));
1209 
1210             if (compEffCount)
1211             {
1212                 statesPtr += sizeof(state_effecter_possible_states) +
1213                              state->possible_states_size - 1;
1214             }
1215         }
1216     }
1217 
checkTerminusHandle(const uint8_t * data,std::optional<uint16_t> terminusHandle)1218     bool checkTerminusHandle(const uint8_t* data,
1219                              std::optional<uint16_t> terminusHandle)
1220     {
1221         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1222 
1223         if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1224         {
1225             auto tlpdr =
1226                 reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1227 
1228             if (tlpdr->terminus_handle != terminusHandle)
1229             {
1230                 return true;
1231             }
1232         }
1233         else if (pdr->type == PLDM_STATE_SENSOR_PDR)
1234         {
1235             auto sensor = reinterpret_cast<const pldm_state_sensor_pdr*>(data);
1236 
1237             if (sensor->terminus_handle != terminusHandle)
1238             {
1239                 return true;
1240             }
1241         }
1242         else if (pdr->type == PLDM_NUMERIC_EFFECTER_PDR)
1243         {
1244             auto numericEffecter =
1245                 reinterpret_cast<const pldm_numeric_effecter_value_pdr*>(data);
1246 
1247             if (numericEffecter->terminus_handle != terminusHandle)
1248             {
1249                 return true;
1250             }
1251         }
1252 
1253         else if (pdr->type == PLDM_STATE_EFFECTER_PDR)
1254         {
1255             auto stateEffecter =
1256                 reinterpret_cast<const pldm_state_effecter_pdr*>(data);
1257             if (stateEffecter->terminus_handle != terminusHandle)
1258             {
1259                 return true;
1260             }
1261         }
1262         else if (pdr->type == PLDM_PDR_FRU_RECORD_SET)
1263         {
1264             data += sizeof(pldm_pdr_hdr);
1265             auto fru = reinterpret_cast<const pldm_pdr_fru_record_set*>(data);
1266 
1267             if (fru->terminus_handle != terminusHandle)
1268             {
1269                 return true;
1270             }
1271         }
1272         else
1273         {
1274             // Entity association PDRs does not have terminus handle
1275             return true;
1276         }
1277 
1278         return false;
1279     }
1280 
printTerminusLocatorPDR(const uint8_t * data,ordered_json & output)1281     void printTerminusLocatorPDR(const uint8_t* data, ordered_json& output)
1282     {
1283         const std::array<std::string_view, 4> terminusLocatorType = {
1284             "UID", "MCTP_EID", "SMBusRelative", "systemSoftware"};
1285 
1286         auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1287 
1288         output["PLDMTerminusHandle"] = pdr->terminus_handle;
1289         output["validity"] = (pdr->validity ? "valid" : "notValid");
1290         output["TID"] = unsigned(pdr->tid);
1291         output["containerID"] = pdr->container_id;
1292         output["terminusLocatorType"] =
1293             terminusLocatorType[pdr->terminus_locator_type];
1294         output["terminusLocatorValueSize"] =
1295             unsigned(pdr->terminus_locator_value_size);
1296 
1297         if (pdr->terminus_locator_type == PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
1298         {
1299             auto locatorValue =
1300                 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
1301                     pdr->terminus_locator_value);
1302             output["EID"] = unsigned(locatorValue->eid);
1303         }
1304     }
1305 
getTerminusHandle(uint8_t * data,std::optional<uint8_t> tid)1306     std::optional<uint16_t> getTerminusHandle(uint8_t* data,
1307                                               std::optional<uint8_t> tid)
1308     {
1309         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1310         if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1311         {
1312             auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1313             if (pdr->tid == tid)
1314             {
1315                 handleFound = true;
1316                 return pdr->terminus_handle;
1317             }
1318         }
1319         return std::nullopt;
1320     }
1321 
1322     /** @brief Format the Numeric Sensor PDR types to json output
1323      *
1324      *  @param[in] data - reference to the Numeric Sensor PDR
1325      *  @param[in] data_length - number of PDR data bytes
1326      *  @param[out] output - PDRs data fields in Json format
1327      */
printNumericSensorPDR(const uint8_t * data,const uint16_t data_length,ordered_json & output)1328     void printNumericSensorPDR(const uint8_t* data, const uint16_t data_length,
1329                                ordered_json& output)
1330     {
1331         struct pldm_numeric_sensor_value_pdr pdr;
1332         int rc =
1333             decode_numeric_sensor_pdr_data(data, (size_t)data_length, &pdr);
1334         if (rc != PLDM_SUCCESS)
1335         {
1336             std::cerr << "Failed to get numeric sensor PDR" << std::endl;
1337             return;
1338         }
1339         output["PLDMTerminusHandle"] = pdr.terminus_handle;
1340         output["sensorID"] = pdr.sensor_id;
1341         output["entityType"] = getEntityName(pdr.entity_type);
1342         output["entityInstanceNumber"] = pdr.entity_instance_num;
1343         output["containerID"] = pdr.container_id;
1344         output["sensorInit"] = pdr.sensor_init;
1345         output["sensorAuxiliaryNamesPDR"] =
1346             (pdr.sensor_auxiliary_names_pdr) ? true : false;
1347         output["baseUnit"] = pdr.base_unit;
1348         output["unitModifier"] = pdr.unit_modifier;
1349         output["rateUnit"] = pdr.rate_unit;
1350         output["baseOEMUnitHandle"] = pdr.base_oem_unit_handle;
1351         output["auxUnit"] = pdr.aux_unit;
1352         output["auxUnitModifier"] = pdr.aux_unit_modifier;
1353         output["auxrateUnit"] = pdr.aux_rate_unit;
1354         output["rel"] = pdr.rel;
1355         output["auxOEMUnitHandle"] = pdr.aux_oem_unit_handle;
1356         output["isLinear"] = (pdr.is_linear) ? true : false;
1357         output["sensorDataSize"] = pdr.sensor_data_size;
1358         output["resolution"] = pdr.resolution;
1359         output["offset"] = pdr.offset;
1360         output["accuracy"] = pdr.accuracy;
1361         output["plusTolerance"] = pdr.plus_tolerance;
1362         output["minusTolerance"] = pdr.minus_tolerance;
1363 
1364         switch (pdr.sensor_data_size)
1365         {
1366             case PLDM_SENSOR_DATA_SIZE_UINT8:
1367                 output["hysteresis"] = pdr.hysteresis.value_u8;
1368                 output["maxReadable"] = pdr.max_readable.value_u8;
1369                 output["minReadable"] = pdr.min_readable.value_u8;
1370                 break;
1371             case PLDM_SENSOR_DATA_SIZE_SINT8:
1372                 output["hysteresis"] = pdr.hysteresis.value_s8;
1373                 output["maxReadable"] = pdr.max_readable.value_s8;
1374                 output["minReadable"] = pdr.min_readable.value_s8;
1375                 break;
1376             case PLDM_SENSOR_DATA_SIZE_UINT16:
1377                 output["hysteresis"] = pdr.hysteresis.value_u16;
1378                 output["maxReadable"] = pdr.max_readable.value_u16;
1379                 output["minReadable"] = pdr.min_readable.value_u16;
1380                 break;
1381             case PLDM_SENSOR_DATA_SIZE_SINT16:
1382                 output["hysteresis"] = pdr.hysteresis.value_s16;
1383                 output["maxReadable"] = pdr.max_readable.value_s16;
1384                 output["minReadable"] = pdr.min_readable.value_s16;
1385                 break;
1386             case PLDM_SENSOR_DATA_SIZE_UINT32:
1387                 output["hysteresis"] = pdr.hysteresis.value_u32;
1388                 output["maxReadable"] = pdr.max_readable.value_u32;
1389                 output["minReadable"] = pdr.min_readable.value_u32;
1390                 break;
1391             case PLDM_SENSOR_DATA_SIZE_SINT32:
1392                 output["hysteresis"] = pdr.hysteresis.value_s32;
1393                 output["maxReadable"] = pdr.max_readable.value_s32;
1394                 output["minReadable"] = pdr.min_readable.value_s32;
1395                 break;
1396             default:
1397                 break;
1398         }
1399 
1400         output["supportedThresholds"] = pdr.supported_thresholds.byte;
1401         output["thresholAndHysteresisVolatility"] =
1402             pdr.threshold_and_hysteresis_volatility.byte;
1403         output["stateTransitionInterval"] = pdr.state_transition_interval;
1404         output["updateInterval"] = pdr.update_interval;
1405         output["rangeFieldFormat"] = pdr.range_field_format;
1406         output["rangeFieldSupport"] = pdr.range_field_support.byte;
1407 
1408         switch (pdr.range_field_format)
1409         {
1410             case PLDM_RANGE_FIELD_FORMAT_UINT8:
1411                 output["nominalValue"] = pdr.nominal_value.value_u8;
1412                 output["normalMax"] = pdr.normal_max.value_u8;
1413                 output["normalMin"] = pdr.normal_min.value_u8;
1414                 output["warningHigh"] = pdr.warning_high.value_u8;
1415                 output["warningLow"] = pdr.warning_low.value_u8;
1416                 output["criticalHigh"] = pdr.critical_high.value_u8;
1417                 output["criticalLow"] = pdr.critical_low.value_u8;
1418                 output["fatalHigh"] = pdr.fatal_high.value_u8;
1419                 output["fatalLeow"] = pdr.fatal_low.value_u8;
1420                 break;
1421             case PLDM_RANGE_FIELD_FORMAT_SINT8:
1422                 output["nominalValue"] = pdr.nominal_value.value_s8;
1423                 output["normalMax"] = pdr.normal_max.value_s8;
1424                 output["normalMin"] = pdr.normal_min.value_s8;
1425                 output["warningHigh"] = pdr.warning_high.value_s8;
1426                 output["warningLow"] = pdr.warning_low.value_s8;
1427                 output["criticalHigh"] = pdr.critical_high.value_s8;
1428                 output["criticalLow"] = pdr.critical_low.value_s8;
1429                 output["fatalHigh"] = pdr.fatal_high.value_s8;
1430                 output["fatalLeow"] = pdr.fatal_low.value_s8;
1431                 break;
1432             case PLDM_RANGE_FIELD_FORMAT_UINT16:
1433                 output["nominalValue"] = pdr.nominal_value.value_u16;
1434                 output["normalMax"] = pdr.normal_max.value_u16;
1435                 output["normalMin"] = pdr.normal_min.value_u16;
1436                 output["warningHigh"] = pdr.warning_high.value_u16;
1437                 output["warningLow"] = pdr.warning_low.value_u16;
1438                 output["criticalHigh"] = pdr.critical_high.value_u16;
1439                 output["criticalLow"] = pdr.critical_low.value_u16;
1440                 output["fatalHigh"] = pdr.fatal_high.value_u16;
1441                 output["fatalLeow"] = pdr.fatal_low.value_u16;
1442                 break;
1443             case PLDM_RANGE_FIELD_FORMAT_SINT16:
1444                 output["nominalValue"] = pdr.nominal_value.value_s16;
1445                 output["normalMax"] = pdr.normal_max.value_s16;
1446                 output["normalMin"] = pdr.normal_min.value_s16;
1447                 output["warningHigh"] = pdr.warning_high.value_s16;
1448                 output["warningLow"] = pdr.warning_low.value_s16;
1449                 output["criticalHigh"] = pdr.critical_high.value_s16;
1450                 output["criticalLow"] = pdr.critical_low.value_s16;
1451                 output["fatalHigh"] = pdr.fatal_high.value_s16;
1452                 output["fatalLeow"] = pdr.fatal_low.value_s16;
1453                 break;
1454             case PLDM_RANGE_FIELD_FORMAT_UINT32:
1455                 output["nominalValue"] = pdr.nominal_value.value_u32;
1456                 output["normalMax"] = pdr.normal_max.value_u32;
1457                 output["normalMin"] = pdr.normal_min.value_u32;
1458                 output["warningHigh"] = pdr.warning_high.value_u32;
1459                 output["warningLow"] = pdr.warning_low.value_u32;
1460                 output["criticalHigh"] = pdr.critical_high.value_u32;
1461                 output["criticalLow"] = pdr.critical_low.value_u32;
1462                 output["fatalHigh"] = pdr.fatal_high.value_u32;
1463                 output["fatalLeow"] = pdr.fatal_low.value_u32;
1464                 break;
1465             case PLDM_RANGE_FIELD_FORMAT_SINT32:
1466                 output["nominalValue"] = pdr.nominal_value.value_s32;
1467                 output["normalMax"] = pdr.normal_max.value_s32;
1468                 output["normalMin"] = pdr.normal_min.value_s32;
1469                 output["warningHigh"] = pdr.warning_high.value_s32;
1470                 output["warningLow"] = pdr.warning_low.value_s32;
1471                 output["criticalHigh"] = pdr.critical_high.value_s32;
1472                 output["criticalLow"] = pdr.critical_low.value_s32;
1473                 output["fatalHigh"] = pdr.fatal_high.value_s32;
1474                 output["fatalLeow"] = pdr.fatal_low.value_s32;
1475                 break;
1476             case PLDM_RANGE_FIELD_FORMAT_REAL32:
1477                 output["nominalValue"] = pdr.nominal_value.value_f32;
1478                 output["normalMax"] = pdr.normal_max.value_f32;
1479                 output["normalMin"] = pdr.normal_min.value_f32;
1480                 output["warningHigh"] = pdr.warning_high.value_f32;
1481                 output["warningLow"] = pdr.warning_low.value_f32;
1482                 output["criticalHigh"] = pdr.critical_high.value_f32;
1483                 output["criticalLow"] = pdr.critical_low.value_f32;
1484                 output["fatalHigh"] = pdr.fatal_high.value_f32;
1485                 output["fatalLeow"] = pdr.fatal_low.value_f32;
1486                 break;
1487             default:
1488                 break;
1489         }
1490     }
1491 
1492     /** @brief Format the Compact Numeric Sensor PDR types to json output
1493      *
1494      *  @param[in] data - reference to the Compact Numeric Sensor PDR
1495      *  @param[out] output - PDRs data fields in Json format
1496      */
printCompactNumericSensorPDR(const uint8_t * data,ordered_json & output)1497     void printCompactNumericSensorPDR(const uint8_t* data, ordered_json& output)
1498     {
1499         struct pldm_compact_numeric_sensor_pdr* pdr =
1500             (struct pldm_compact_numeric_sensor_pdr*)data;
1501         if (!pdr)
1502         {
1503             std::cerr << "Failed to get compact numeric sensor PDR"
1504                       << std::endl;
1505             return;
1506         }
1507         output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
1508         output["sensorID"] = int(pdr->sensor_id);
1509         output["entityType"] = getEntityName(pdr->entity_type);
1510         output["entityInstanceNumber"] = int(pdr->entity_instance);
1511         output["containerID"] = int(pdr->container_id);
1512         output["sensorNameStringByteLength"] = int(pdr->sensor_name_length);
1513         if (pdr->sensor_name_length == 0)
1514         {
1515             output["Name"] = std::format("PLDM_Device_TID{}_SensorId{}",
1516                                          unsigned(pdr->terminus_handle),
1517                                          unsigned(pdr->sensor_id));
1518         }
1519         else
1520         {
1521             std::string sTemp(reinterpret_cast<const char*>(pdr->sensor_name),
1522                               pdr->sensor_name_length);
1523             output["Name"] = sTemp;
1524         }
1525         output["baseUnit"] = unsigned(pdr->base_unit);
1526         output["unitModifier"] = signed(pdr->unit_modifier);
1527         output["occurrenceRate"] = unsigned(pdr->occurrence_rate);
1528         output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1529         if (pdr->range_field_support.bits.bit0)
1530         {
1531             output["warningHigh"] = int(pdr->warning_high);
1532         }
1533         if (pdr->range_field_support.bits.bit1)
1534         {
1535             output["warningLow"] = int(pdr->warning_low);
1536         }
1537         if (pdr->range_field_support.bits.bit2)
1538         {
1539             output["criticalHigh"] = int(pdr->critical_high);
1540         }
1541         if (pdr->range_field_support.bits.bit3)
1542         {
1543             output["criticalLow"] = int(pdr->critical_low);
1544         }
1545         if (pdr->range_field_support.bits.bit4)
1546         {
1547             output["fatalHigh"] = int(pdr->fatal_high);
1548         }
1549         if (pdr->range_field_support.bits.bit5)
1550         {
1551             output["fatalLow"] = int(pdr->fatal_low);
1552         }
1553     }
1554 
printPDRMsg(uint32_t & nextRecordHndl,const uint16_t respCnt,uint8_t * data,std::optional<uint16_t> terminusHandle)1555     void printPDRMsg(uint32_t& nextRecordHndl, const uint16_t respCnt,
1556                      uint8_t* data, std::optional<uint16_t> terminusHandle)
1557     {
1558         if (data == nullptr)
1559         {
1560             std::cerr << "Failed to get PDR message" << std::endl;
1561             return;
1562         }
1563 
1564         ordered_json output;
1565         output["nextRecordHandle"] = nextRecordHndl;
1566         output["responseCount"] = respCnt;
1567 
1568         struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1569         if (!pdr)
1570         {
1571             return;
1572         }
1573 
1574         if (!pdrRecType.empty())
1575         {
1576             // Need to return if the requested PDR type
1577             // is not supported
1578             if (!strToPdrType.contains(pdrRecType))
1579             {
1580                 std::cerr << "PDR type '" << pdrRecType
1581                           << "' is not supported or invalid\n";
1582                 // PDR type not supported, setting next record handle to
1583                 // 0 to avoid looping through all PDR records
1584                 nextRecordHndl = 0;
1585                 return;
1586             }
1587 
1588             // Do not print PDR record if the current record
1589             // PDR type does not match with requested type
1590             if (pdr->type != strToPdrType.at(pdrRecType))
1591             {
1592                 return;
1593             }
1594         }
1595 
1596         if (pdrTerminus.has_value())
1597         {
1598             if (checkTerminusHandle(data, terminusHandle))
1599             {
1600                 std::cerr << "The Terminus handle doesn't match return"
1601                           << std::endl;
1602                 return;
1603             }
1604         }
1605 
1606         printCommonPDRHeader(pdr, output);
1607 
1608         switch (pdr->type)
1609         {
1610             case PLDM_TERMINUS_LOCATOR_PDR:
1611                 printTerminusLocatorPDR(data, output);
1612                 break;
1613             case PLDM_STATE_SENSOR_PDR:
1614                 printStateSensorPDR(data, output);
1615                 break;
1616             case PLDM_NUMERIC_EFFECTER_PDR:
1617                 printNumericEffecterPDR(data, output);
1618                 break;
1619             case PLDM_NUMERIC_SENSOR_PDR:
1620                 printNumericSensorPDR(data, respCnt, output);
1621                 break;
1622             case PLDM_SENSOR_AUXILIARY_NAMES_PDR:
1623             case PLDM_EFFECTER_AUXILIARY_NAMES_PDR:
1624                 printAuxNamePDR(data, output);
1625                 break;
1626             case PLDM_STATE_EFFECTER_PDR:
1627                 printStateEffecterPDR(data, output);
1628                 break;
1629             case PLDM_PDR_ENTITY_ASSOCIATION:
1630                 printPDREntityAssociation(data, output);
1631                 break;
1632             case PLDM_PDR_FRU_RECORD_SET:
1633                 printPDRFruRecordSet(data, output);
1634                 break;
1635             case PLDM_COMPACT_NUMERIC_SENSOR_PDR:
1636                 printCompactNumericSensorPDR(data, output);
1637                 break;
1638             default:
1639                 break;
1640         }
1641         pldmtool::helper::DisplayInJson(output);
1642     }
1643 
1644   private:
1645     bool optTIDSet = false;
1646     uint32_t recordHandle;
1647     bool allPDRs;
1648     std::string pdrRecType;
1649     std::optional<uint8_t> pdrTerminus;
1650     std::optional<uint16_t> terminusHandle;
1651     bool handleFound = false;
1652     CLI::Option* getPDRGroupOption = nullptr;
1653     uint32_t dataTransferHandle;
1654     uint8_t operationFlag;
1655     uint16_t requestCount;
1656     uint16_t recordChangeNumber;
1657     std::vector<uint8_t> recordData;
1658     bool nextPartRequired;
1659 };
1660 
1661 class SetStateEffecter : public CommandInterface
1662 {
1663   public:
1664     ~SetStateEffecter() = default;
1665     SetStateEffecter() = delete;
1666     SetStateEffecter(const SetStateEffecter&) = delete;
1667     SetStateEffecter(SetStateEffecter&&) = default;
1668     SetStateEffecter& operator=(const SetStateEffecter&) = delete;
1669     SetStateEffecter& operator=(SetStateEffecter&&) = delete;
1670 
1671     // compositeEffecterCount(value: 0x01 to 0x08) * stateField(2)
1672     static constexpr auto maxEffecterDataSize = 16;
1673 
1674     // compositeEffecterCount(value: 0x01 to 0x08)
1675     static constexpr auto minEffecterCount = 1;
1676     static constexpr auto maxEffecterCount = 8;
SetStateEffecter(const char * type,const char * name,CLI::App * app)1677     explicit SetStateEffecter(const char* type, const char* name,
1678                               CLI::App* app) : CommandInterface(type, name, app)
1679     {
1680         app->add_option(
1681                "-i, --id", effecterId,
1682                "A handle that is used to identify and access the effecter")
1683             ->required();
1684         app->add_option("-c, --count", effecterCount,
1685                         "The number of individual sets of effecter information")
1686             ->required();
1687         app->add_option(
1688                "-d,--data", effecterData,
1689                "Set effecter state data\n"
1690                "eg: requestSet0 effecterState0 noChange1 dummyState1 ...")
1691             ->required();
1692     }
1693 
createRequestMsg()1694     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1695     {
1696         std::vector<uint8_t> requestMsg(
1697             sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES);
1698         auto request = new (requestMsg.data()) pldm_msg;
1699 
1700         if (effecterCount > maxEffecterCount ||
1701             effecterCount < minEffecterCount)
1702         {
1703             std::cerr << "Request Message Error: effecterCount size "
1704                       << effecterCount << "is invalid\n";
1705             auto rc = PLDM_ERROR_INVALID_DATA;
1706             return {rc, requestMsg};
1707         }
1708 
1709         if (effecterData.size() > maxEffecterDataSize)
1710         {
1711             std::cerr << "Request Message Error: effecterData size "
1712                       << effecterData.size() << "is invalid\n";
1713             auto rc = PLDM_ERROR_INVALID_DATA;
1714             return {rc, requestMsg};
1715         }
1716 
1717         auto stateField = parseEffecterData(effecterData, effecterCount);
1718         if (!stateField)
1719         {
1720             std::cerr << "Failed to parse effecter data, effecterCount size "
1721                       << effecterCount << "\n";
1722             auto rc = PLDM_ERROR_INVALID_DATA;
1723             return {rc, requestMsg};
1724         }
1725 
1726         auto rc = encode_set_state_effecter_states_req(
1727             instanceId, effecterId, effecterCount, stateField->data(), request);
1728         return {rc, requestMsg};
1729     }
1730 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1731     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1732     {
1733         uint8_t completionCode = 0;
1734         auto rc = decode_set_state_effecter_states_resp(
1735             responsePtr, payloadLength, &completionCode);
1736 
1737         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1738         {
1739             std::cerr << "Response Message Error: "
1740                       << "rc=" << rc << ",cc=" << (int)completionCode << "\n";
1741             return;
1742         }
1743 
1744         ordered_json data;
1745         data["Response"] = "SUCCESS";
1746         pldmtool::helper::DisplayInJson(data);
1747     }
1748 
1749   private:
1750     uint16_t effecterId;
1751     uint8_t effecterCount;
1752     std::vector<uint8_t> effecterData;
1753 };
1754 
1755 class SetNumericEffecterValue : public CommandInterface
1756 {
1757   public:
1758     ~SetNumericEffecterValue() = default;
1759     SetNumericEffecterValue() = delete;
1760     SetNumericEffecterValue(const SetNumericEffecterValue&) = delete;
1761     SetNumericEffecterValue(SetNumericEffecterValue&&) = default;
1762     SetNumericEffecterValue& operator=(const SetNumericEffecterValue&) = delete;
1763     SetNumericEffecterValue& operator=(SetNumericEffecterValue&&) = delete;
1764 
SetNumericEffecterValue(const char * type,const char * name,CLI::App * app)1765     explicit SetNumericEffecterValue(const char* type, const char* name,
1766                                      CLI::App* app) :
1767         CommandInterface(type, name, app)
1768     {
1769         app->add_option(
1770                "-i, --id", effecterId,
1771                "A handle that is used to identify and access the effecter")
1772             ->required();
1773         app->add_option("-s, --size", effecterDataSize,
1774                         "The bit width and format of the setting value for the "
1775                         "effecter. enum value: {uint8, sint8, uint16, sint16, "
1776                         "uint32, sint32}\n")
1777             ->required();
1778         app->add_option("-d,--data", maxEffecterValue,
1779                         "The setting value of numeric effecter being "
1780                         "requested\n")
1781             ->required();
1782     }
1783 
createRequestMsg()1784     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1785     {
1786         std::vector<uint8_t> requestMsg(
1787             sizeof(pldm_msg_hdr) +
1788             PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3);
1789 
1790         uint8_t* effecterValue = (uint8_t*)&maxEffecterValue;
1791 
1792         auto request = new (requestMsg.data()) pldm_msg;
1793 
1794         size_t payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES;
1795 
1796         if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1797             effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT16)
1798         {
1799             payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1;
1800         }
1801         if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1802             effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT32)
1803         {
1804             payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3;
1805         }
1806         auto rc = encode_set_numeric_effecter_value_req(
1807             0, effecterId, effecterDataSize, effecterValue, request,
1808             payload_length);
1809 
1810         return {rc, requestMsg};
1811     }
1812 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1813     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1814     {
1815         uint8_t completionCode = 0;
1816         auto rc = decode_set_numeric_effecter_value_resp(
1817             responsePtr, payloadLength, &completionCode);
1818 
1819         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1820         {
1821             std::cerr << "Response Message Error: "
1822                       << "rc=" << rc << ",cc=" << (int)completionCode
1823                       << std::endl;
1824             return;
1825         }
1826 
1827         ordered_json data;
1828         data["Response"] = "SUCCESS";
1829         pldmtool::helper::DisplayInJson(data);
1830     }
1831 
1832   private:
1833     uint16_t effecterId;
1834     uint8_t effecterDataSize;
1835     uint64_t maxEffecterValue;
1836 };
1837 
1838 class GetStateSensorReadings : public CommandInterface
1839 {
1840   public:
1841     ~GetStateSensorReadings() = default;
1842     GetStateSensorReadings() = delete;
1843     GetStateSensorReadings(const GetStateSensorReadings&) = delete;
1844     GetStateSensorReadings(GetStateSensorReadings&&) = default;
1845     GetStateSensorReadings& operator=(const GetStateSensorReadings&) = delete;
1846     GetStateSensorReadings& operator=(GetStateSensorReadings&&) = delete;
1847 
GetStateSensorReadings(const char * type,const char * name,CLI::App * app)1848     explicit GetStateSensorReadings(const char* type, const char* name,
1849                                     CLI::App* app) :
1850         CommandInterface(type, name, app)
1851     {
1852         app->add_option(
1853                "-i, --sensor_id", sensorId,
1854                "Sensor ID that is used to identify and access the sensor")
1855             ->required();
1856         app->add_option("-r, --rearm", sensorRearm,
1857                         "Each bit location in this field corresponds to a "
1858                         "particular sensor")
1859             ->required();
1860     }
1861 
createRequestMsg()1862     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1863     {
1864         std::vector<uint8_t> requestMsg(
1865             sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
1866         auto request = new (requestMsg.data()) pldm_msg;
1867 
1868         uint8_t reserved = 0;
1869         bitfield8_t bf;
1870         bf.byte = sensorRearm;
1871         auto rc = encode_get_state_sensor_readings_req(instanceId, sensorId, bf,
1872                                                        reserved, request);
1873 
1874         return {rc, requestMsg};
1875     }
1876 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1877     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1878     {
1879         uint8_t completionCode = 0;
1880         uint8_t compSensorCount = 0;
1881         std::array<get_sensor_state_field, 8> stateField{};
1882         auto rc = decode_get_state_sensor_readings_resp(
1883             responsePtr, payloadLength, &completionCode, &compSensorCount,
1884             stateField.data());
1885 
1886         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1887         {
1888             std::cerr << "Response Message Error: "
1889                       << "rc=" << rc << ",cc=" << (int)completionCode
1890                       << std::endl;
1891             return;
1892         }
1893         ordered_json output;
1894         output["compositeSensorCount"] = (int)compSensorCount;
1895 
1896         for (size_t i = 0; i < compSensorCount; i++)
1897         {
1898             if (sensorOpState.contains(stateField[i].sensor_op_state))
1899             {
1900                 output.emplace(("sensorOpState[" + std::to_string(i) + "]"),
1901                                sensorOpState.at(stateField[i].sensor_op_state));
1902             }
1903 
1904             if (sensorPresState.contains(stateField[i].present_state))
1905             {
1906                 output.emplace(("presentState[" + std::to_string(i) + "]"),
1907                                sensorPresState.at(stateField[i].present_state));
1908             }
1909 
1910             if (sensorPresState.contains(stateField[i].previous_state))
1911             {
1912                 output.emplace(
1913                     ("previousState[" + std::to_string(i) + "]"),
1914                     sensorPresState.at(stateField[i].previous_state));
1915             }
1916 
1917             if (sensorPresState.contains(stateField[i].event_state))
1918             {
1919                 output.emplace(("eventState[" + std::to_string(i) + "]"),
1920                                sensorPresState.at(stateField[i].event_state));
1921             }
1922         }
1923 
1924         pldmtool::helper::DisplayInJson(output);
1925     }
1926 
1927   private:
1928     uint16_t sensorId;
1929     uint8_t sensorRearm;
1930 };
1931 
1932 class GetSensorReading : public CommandInterface
1933 {
1934   public:
1935     ~GetSensorReading() = default;
1936     GetSensorReading() = delete;
1937     GetSensorReading(const GetSensorReading&) = delete;
1938     GetSensorReading(GetSensorReading&&) = default;
1939     GetSensorReading& operator=(const GetSensorReading&) = delete;
1940     GetSensorReading& operator=(GetSensorReading&&) = delete;
1941 
GetSensorReading(const char * type,const char * name,CLI::App * app)1942     explicit GetSensorReading(const char* type, const char* name,
1943                               CLI::App* app) : CommandInterface(type, name, app)
1944     {
1945         app->add_option(
1946                "-i, --sensor_id", sensorId,
1947                "Sensor ID that is used to identify and access the sensor")
1948             ->required();
1949         app->add_option("-r, --rearm", rearm,
1950                         "Manually re-arm EventState after "
1951                         "responding to this request")
1952             ->required();
1953     }
1954 
createRequestMsg()1955     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1956     {
1957         std::vector<uint8_t> requestMsg(
1958             sizeof(pldm_msg_hdr) + PLDM_GET_SENSOR_READING_REQ_BYTES);
1959         auto request = new (requestMsg.data()) pldm_msg;
1960 
1961         auto rc =
1962             encode_get_sensor_reading_req(instanceId, sensorId, rearm, request);
1963 
1964         return {rc, requestMsg};
1965     }
1966 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)1967     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1968     {
1969         uint8_t completionCode = 0;
1970         uint8_t sensorDataSize = 0;
1971         uint8_t sensorOperationalState = 0;
1972         uint8_t sensorEventMessageEnable = 0;
1973         uint8_t presentState = 0;
1974         uint8_t previousState = 0;
1975         uint8_t eventState = 0;
1976         std::array<uint8_t, sizeof(uint32_t)>
1977             presentReading{}; // maximum size for the present Value is uint32
1978                               // according to spec DSP0248
1979 
1980         auto rc = decode_get_sensor_reading_resp(
1981             responsePtr, payloadLength, &completionCode, &sensorDataSize,
1982             &sensorOperationalState, &sensorEventMessageEnable, &presentState,
1983             &previousState, &eventState, presentReading.data());
1984 
1985         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1986         {
1987             std::cerr << "Response Message Error: "
1988                       << "rc=" << rc << ",cc=" << (int)completionCode
1989                       << std::endl;
1990             return;
1991         }
1992 
1993         ordered_json output;
1994         output["sensorDataSize"] =
1995             getSensorState(sensorDataSize, &sensorDataSz);
1996         output["sensorOperationalState"] =
1997             getSensorState(sensorOperationalState, &sensorOpState);
1998         output["sensorEventMessageEnable"] =
1999             getSensorState(sensorEventMessageEnable, &sensorEventMsgEnable);
2000         output["presentState"] = getSensorState(presentState, &sensorPresState);
2001         output["previousState"] =
2002             getSensorState(previousState, &sensorPresState);
2003         output["eventState"] = getSensorState(eventState, &sensorPresState);
2004 
2005         switch (sensorDataSize)
2006         {
2007             case PLDM_SENSOR_DATA_SIZE_UINT8:
2008             {
2009                 output["presentReading"] =
2010                     *(reinterpret_cast<uint8_t*>(presentReading.data()));
2011                 break;
2012             }
2013             case PLDM_SENSOR_DATA_SIZE_SINT8:
2014             {
2015                 output["presentReading"] =
2016                     *(reinterpret_cast<int8_t*>(presentReading.data()));
2017                 break;
2018             }
2019             case PLDM_SENSOR_DATA_SIZE_UINT16:
2020             {
2021                 output["presentReading"] =
2022                     *(reinterpret_cast<uint16_t*>(presentReading.data()));
2023                 break;
2024             }
2025             case PLDM_SENSOR_DATA_SIZE_SINT16:
2026             {
2027                 output["presentReading"] =
2028                     *(reinterpret_cast<int16_t*>(presentReading.data()));
2029                 break;
2030             }
2031             case PLDM_SENSOR_DATA_SIZE_UINT32:
2032             {
2033                 output["presentReading"] =
2034                     *(reinterpret_cast<uint32_t*>(presentReading.data()));
2035                 break;
2036             }
2037             case PLDM_SENSOR_DATA_SIZE_SINT32:
2038             {
2039                 output["presentReading"] =
2040                     *(reinterpret_cast<int32_t*>(presentReading.data()));
2041                 break;
2042             }
2043             default:
2044             {
2045                 std::cerr << "Unknown Sensor Data Size : "
2046                           << static_cast<int>(sensorDataSize) << std::endl;
2047                 break;
2048             }
2049         }
2050 
2051         pldmtool::helper::DisplayInJson(output);
2052     }
2053 
2054   private:
2055     uint16_t sensorId;
2056     uint8_t rearm;
2057 
2058     const std::map<uint8_t, std::string> sensorDataSz = {
2059         {PLDM_SENSOR_DATA_SIZE_UINT8, "uint8"},
2060         {PLDM_SENSOR_DATA_SIZE_SINT8, "uint8"},
2061         {PLDM_SENSOR_DATA_SIZE_UINT16, "uint16"},
2062         {PLDM_SENSOR_DATA_SIZE_SINT16, "uint16"},
2063         {PLDM_SENSOR_DATA_SIZE_UINT32, "uint32"},
2064         {PLDM_SENSOR_DATA_SIZE_SINT32, "uint32"}};
2065 
2066     static inline const std::map<uint8_t, std::string> sensorEventMsgEnable{
2067         {PLDM_NO_EVENT_GENERATION, "Sensor No Event Generation"},
2068         {PLDM_EVENTS_DISABLED, "Sensor Events Disabled"},
2069         {PLDM_EVENTS_ENABLED, "Sensor Events Enabled"},
2070         {PLDM_OP_EVENTS_ONLY_ENABLED, "Sensor Op Events Only Enabled"},
2071         {PLDM_STATE_EVENTS_ONLY_ENABLED, "Sensor State Events Only Enabled"}};
2072 
getSensorState(uint8_t state,const std::map<uint8_t,std::string> * cont)2073     std::string getSensorState(uint8_t state,
2074                                const std::map<uint8_t, std::string>* cont)
2075     {
2076         auto typeString = std::to_string(state);
2077         try
2078         {
2079             return cont->at(state);
2080         }
2081         catch (const std::out_of_range& e)
2082         {
2083             return typeString;
2084         }
2085     }
2086 };
2087 
2088 class GetStateEffecterStates : public CommandInterface
2089 {
2090   public:
2091     ~GetStateEffecterStates() = default;
2092     GetStateEffecterStates() = delete;
2093     GetStateEffecterStates(const GetStateEffecterStates&) = delete;
2094     GetStateEffecterStates(GetStateEffecterStates&&) = default;
2095     GetStateEffecterStates& operator=(const GetStateEffecterStates&) = delete;
2096     GetStateEffecterStates& operator=(GetStateEffecterStates&&) = delete;
2097 
GetStateEffecterStates(const char * type,const char * name,CLI::App * app)2098     explicit GetStateEffecterStates(const char* type, const char* name,
2099                                     CLI::App* app) :
2100         CommandInterface(type, name, app)
2101     {
2102         app->add_option(
2103                "-i, --effecter_id", effecter_id,
2104                "Effecter ID that is used to identify and access the effecter")
2105             ->required();
2106     }
2107 
createRequestMsg()2108     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
2109     {
2110         std::vector<uint8_t> requestMsg(
2111             sizeof(pldm_msg_hdr) + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
2112         auto request = new (requestMsg.data()) pldm_msg;
2113 
2114         auto rc = encode_get_state_effecter_states_req(
2115             instanceId, effecter_id, request,
2116             PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
2117 
2118         return {rc, requestMsg};
2119     }
2120 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)2121     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
2122     {
2123         struct pldm_get_state_effecter_states_resp resp;
2124         auto rc = decode_get_state_effecter_states_resp(responsePtr,
2125                                                         payloadLength, &resp);
2126 
2127         if (rc || resp.completion_code != PLDM_SUCCESS)
2128         {
2129             std::cerr << "Response Message Error: "
2130                       << "rc=" << rc << ",cc="
2131                       << static_cast<int>(resp.completion_code) << std::endl;
2132             return;
2133         }
2134         ordered_json output;
2135         auto comp_effecter_count = static_cast<int>(resp.comp_effecter_count);
2136         output["compositeEffecterCount"] = comp_effecter_count;
2137 
2138         for (auto i : std::views::iota(0, comp_effecter_count))
2139         {
2140             output[std::format("effecterOpState[{}])", i)] =
2141                 getEffecterOpState(resp.field[i].effecter_op_state);
2142 
2143             output[std::format("pendingState[{}]", i)] =
2144                 resp.field[i].pending_state;
2145 
2146             output[std::format("presentState[{}]", i)] =
2147                 resp.field[i].present_state;
2148         }
2149 
2150         pldmtool::helper::DisplayInJson(output);
2151     }
2152 
2153   private:
2154     uint16_t effecter_id;
2155 };
2156 
2157 class GetNumericEffecterValue : public CommandInterface
2158 {
2159   public:
2160     ~GetNumericEffecterValue() = default;
2161     GetNumericEffecterValue() = delete;
2162     GetNumericEffecterValue(const GetNumericEffecterValue&) = delete;
2163     GetNumericEffecterValue(GetNumericEffecterValue&&) = default;
2164     GetNumericEffecterValue& operator=(const GetNumericEffecterValue&) = delete;
2165     GetNumericEffecterValue& operator=(GetNumericEffecterValue&&) = delete;
2166 
GetNumericEffecterValue(const char * type,const char * name,CLI::App * app)2167     explicit GetNumericEffecterValue(const char* type, const char* name,
2168                                      CLI::App* app) :
2169         CommandInterface(type, name, app)
2170     {
2171         app->add_option(
2172                "-i, --effecter_id", effecterId,
2173                "A handle that is used to identify and access the effecter")
2174             ->required();
2175     }
2176 
createRequestMsg()2177     std::pair<int, std::vector<uint8_t>> createRequestMsg() override
2178     {
2179         std::vector<uint8_t> requestMsg(
2180             sizeof(pldm_msg_hdr) + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
2181         auto request = new (requestMsg.data()) pldm_msg;
2182 
2183         auto rc = encode_get_numeric_effecter_value_req(instanceId, effecterId,
2184                                                         request);
2185 
2186         return {rc, requestMsg};
2187     }
2188 
parseResponseMsg(pldm_msg * responsePtr,size_t payloadLength)2189     void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
2190     {
2191         uint8_t completionCode = 0;
2192         uint8_t effecterDataSize = 0;
2193         uint8_t effecterOperationalState = 0;
2194         std::array<uint8_t, sizeof(uint32_t)>
2195             pendingValue{}; // maximum size for the pending Value is uint32
2196                             // according to spec DSP0248
2197         std::array<uint8_t, sizeof(uint32_t)>
2198             presentValue{}; // maximum size for the present Value is uint32
2199                             // according to spec DSP0248
2200 
2201         auto rc = decode_get_numeric_effecter_value_resp(
2202             responsePtr, payloadLength, &completionCode, &effecterDataSize,
2203             &effecterOperationalState, pendingValue.data(),
2204             presentValue.data());
2205 
2206         if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
2207         {
2208             std::cerr << "Response Message Error: "
2209                       << "rc=" << rc << ",cc="
2210                       << static_cast<int>(completionCode) << std::endl;
2211             return;
2212         }
2213 
2214         ordered_json output;
2215         output["effecterDataSize"] = static_cast<int>(effecterDataSize);
2216         output["effecterOperationalState"] =
2217             getEffecterOpState(effecterOperationalState);
2218 
2219         switch (effecterDataSize)
2220         {
2221             case PLDM_EFFECTER_DATA_SIZE_UINT8:
2222             {
2223                 output["pendingValue"] =
2224                     *(reinterpret_cast<uint8_t*>(pendingValue.data()));
2225                 output["presentValue"] =
2226                     *(reinterpret_cast<uint8_t*>(presentValue.data()));
2227                 break;
2228             }
2229             case PLDM_EFFECTER_DATA_SIZE_SINT8:
2230             {
2231                 output["pendingValue"] =
2232                     *(reinterpret_cast<int8_t*>(pendingValue.data()));
2233                 output["presentValue"] =
2234                     *(reinterpret_cast<int8_t*>(presentValue.data()));
2235                 break;
2236             }
2237             case PLDM_EFFECTER_DATA_SIZE_UINT16:
2238             {
2239                 output["pendingValue"] =
2240                     *(reinterpret_cast<uint16_t*>(pendingValue.data()));
2241                 output["presentValue"] =
2242                     *(reinterpret_cast<uint16_t*>(presentValue.data()));
2243                 break;
2244             }
2245             case PLDM_EFFECTER_DATA_SIZE_SINT16:
2246             {
2247                 output["pendingValue"] =
2248                     *(reinterpret_cast<int16_t*>(pendingValue.data()));
2249                 output["presentValue"] =
2250                     *(reinterpret_cast<int16_t*>(presentValue.data()));
2251                 break;
2252             }
2253             case PLDM_EFFECTER_DATA_SIZE_UINT32:
2254             {
2255                 output["pendingValue"] =
2256                     *(reinterpret_cast<uint32_t*>(pendingValue.data()));
2257                 output["presentValue"] =
2258                     *(reinterpret_cast<uint32_t*>(presentValue.data()));
2259                 break;
2260             }
2261             case PLDM_EFFECTER_DATA_SIZE_SINT32:
2262             {
2263                 output["pendingValue"] =
2264                     *(reinterpret_cast<int32_t*>(pendingValue.data()));
2265                 output["presentValue"] =
2266                     *(reinterpret_cast<int32_t*>(presentValue.data()));
2267                 break;
2268             }
2269             default:
2270             {
2271                 std::cerr << "Unknown Effecter Data Size : "
2272                           << static_cast<int>(effecterDataSize) << std::endl;
2273                 break;
2274             }
2275         }
2276 
2277         pldmtool::helper::DisplayInJson(output);
2278     }
2279 
2280   private:
2281     uint16_t effecterId;
2282 };
2283 
registerCommand(CLI::App & app)2284 void registerCommand(CLI::App& app)
2285 {
2286     auto platform = app.add_subcommand("platform", "platform type command");
2287     platform->require_subcommand(1);
2288 
2289     auto getEventReceiver = platform->add_subcommand(
2290         "GetEventReceiver",
2291         "Get the configured event receiver from a terminus");
2292     commands.push_back(std::make_unique<GetEventReceiver>(
2293         "platform", "getEventReceiver", getEventReceiver));
2294 
2295     auto getPDR =
2296         platform->add_subcommand("GetPDR", "get platform descriptor records");
2297     commands.push_back(std::make_unique<GetPDR>("platform", "getPDR", getPDR));
2298 
2299     auto setStateEffecterStates = platform->add_subcommand(
2300         "SetStateEffecterStates", "set effecter states");
2301     commands.push_back(std::make_unique<SetStateEffecter>(
2302         "platform", "setStateEffecterStates", setStateEffecterStates));
2303 
2304     auto setNumericEffecterValue = platform->add_subcommand(
2305         "SetNumericEffecterValue", "set the value for a PLDM Numeric Effecter");
2306     commands.push_back(std::make_unique<SetNumericEffecterValue>(
2307         "platform", "setNumericEffecterValue", setNumericEffecterValue));
2308 
2309     auto getStateSensorReadings = platform->add_subcommand(
2310         "GetStateSensorReadings", "get the state sensor readings");
2311     commands.push_back(std::make_unique<GetStateSensorReadings>(
2312         "platform", "getStateSensorReadings", getStateSensorReadings));
2313 
2314     auto getNumericEffecterValue = platform->add_subcommand(
2315         "GetNumericEffecterValue", "get the numeric effecter value");
2316     commands.push_back(std::make_unique<GetNumericEffecterValue>(
2317         "platform", "getNumericEffecterValue", getNumericEffecterValue));
2318 
2319     auto getSensorReading = platform->add_subcommand(
2320         "GetSensorReading", "get the numeric sensor reading");
2321     commands.push_back(std::make_unique<GetSensorReading>(
2322         "platform", "getSensorReading", getSensorReading));
2323 
2324     auto getStateEffecterStates = platform->add_subcommand(
2325         "GetStateEffecterStates", "get the state effecter states");
2326     commands.push_back(std::make_unique<GetStateEffecterStates>(
2327         "platform", "getStateEffecterStates", getStateEffecterStates));
2328 }
2329 
parseGetPDROption()2330 void parseGetPDROption()
2331 {
2332     for (const auto& command : commands)
2333     {
2334         if (command.get()->getPLDMType() == "platform" &&
2335             command.get()->getCommandName() == "getPDR")
2336         {
2337             auto getPDR = dynamic_cast<GetPDR*>(command.get());
2338             getPDR->parseGetPDROptions();
2339         }
2340     }
2341 }
2342 
2343 } // namespace platform
2344 } // namespace pldmtool
2345