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