blob: 46d074419d3eb541545148db4551869ecc7be1ae [file] [log] [blame]
Tom Josepha65c0412020-07-03 21:14:44 +05301#include "common/types.hpp"
George Liud6649362019-11-27 19:06:51 +08002#include "pldm_cmd_helper.hpp"
3
George Liuc453e162022-12-21 17:16:23 +08004#include <libpldm/entity.h>
Thu Nguyen846d38f2022-03-26 08:57:21 +07005#include <libpldm/platform.h>
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/state_set.h>
7
Thu Nguyen846d38f2022-03-26 08:57:21 +07008#include <algorithm>
Brad Bishop02d71142021-10-14 19:00:17 -04009#include <cstddef>
10#include <map>
Thu Nguyen846d38f2022-03-26 08:57:21 +070011#include <ranges>
Brad Bishop02d71142021-10-14 19:00:17 -040012
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -050013#ifdef OEM_IBM
14#include "oem/ibm/oem_ibm_state_set.hpp"
15#endif
16
Brad Bishop5079ac42021-08-19 18:35:06 -040017using namespace pldm::utils;
18
George Liud6649362019-11-27 19:06:51 +080019namespace pldmtool
20{
George Liud6649362019-11-27 19:06:51 +080021namespace platform
22{
George Liud6649362019-11-27 19:06:51 +080023namespace
24{
George Liud6649362019-11-27 19:06:51 +080025using namespace pldmtool::helper;
Sridevi Rameshf31b5042021-01-22 05:42:07 -060026
27static 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
40static 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
George Liud6649362019-11-27 19:06:51 +080050std::vector<std::unique_ptr<CommandInterface>> commands;
51
52} // namespace
53
Sridevi Ramesh27c512a2020-08-12 03:29:42 -050054using ordered_json = nlohmann::ordered_json;
55
George Liud6649362019-11-27 19:06:51 +080056class 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;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -050064 GetPDR& operator=(GetPDR&&) = delete;
George Liud6649362019-11-27 19:06:51 +080065
66 using CommandInterface::CommandInterface;
67
George Liud6649362019-11-27 19:06:51 +080068 explicit GetPDR(const char* type, const char* name, CLI::App* app) :
69 CommandInterface(type, name, app)
70 {
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -050071 auto pdrOptionGroup = app->add_option_group(
72 "Required Option",
Pavithra Barithayaa340eed2023-02-14 03:32:47 -060073 "Retrieve individual PDR, all PDRs, PDRs of a requested type or retrieve all PDRs of the requested terminusID");
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -050074 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, "
Thu Nguyen846d38f2022-03-26 08:57:21 +070085 "compactNumericSensor, sensorauxname, "
86 "efffecterAuxName, "
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -050087 "EntityAssociation, fruRecord, ... ]");
Pavithra Barithayaa340eed2023-02-14 03:32:47 -060088
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
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -050094 allPDRs = false;
95 pdrOptionGroup->add_flag("-a, --all", allPDRs,
96 "retrieve all PDRs from a PDR repository");
Pavithra Barithayaa340eed2023-02-14 03:32:47 -060097
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -050098 pdrOptionGroup->require_option(1);
99 }
100
Pavithra Barithayaa340eed2023-02-14 03:32:47 -0600101 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
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500141 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
Brad Bishop16eff862021-10-14 19:44:32 -0400151 // start the array
152 std::cout << "[\n";
153
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500154 // Retrieve all PDR records starting from the first
155 recordHandle = 0;
156 uint32_t prevRecordHandle = 0;
Brad Bishop02d71142021-10-14 19:00:17 -0400157 std::map<uint32_t, uint32_t> recordsSeen;
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500158 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 }
Brad Bishop02d71142021-10-14 19:00:17 -0400168
169 // check for circular references.
Patrick Williams6da4f912023-05-10 07:50:53 -0500170 auto result = recordsSeen.emplace(recordHandle,
171 prevRecordHandle);
Brad Bishop02d71142021-10-14 19:00:17 -0400172 if (!result.second)
173 {
174 std::cerr
175 << "Record handle " << recordHandle
176 << " has multiple references: " << result.first->second
177 << ", " << prevRecordHandle << "\n";
178 return;
179 }
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500180 prevRecordHandle = recordHandle;
Brad Bishop16eff862021-10-14 19:44:32 -0400181
182 if (recordHandle != 0)
183 {
184 // close the array
185 std::cout << ",";
186 }
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500187 } while (recordHandle != 0);
Brad Bishop16eff862021-10-14 19:44:32 -0400188
189 // close the array
190 std::cout << "]\n";
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500191 }
192 else
193 {
194 CommandInterface::exec();
195 }
George Liud6649362019-11-27 19:06:51 +0800196 }
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
Patrick Williams6da4f912023-05-10 07:50:53 -0500204 auto rc = encode_get_pdr_req(instanceId, recordHandle, 0,
205 PLDM_GET_FIRSTPART, UINT16_MAX, 0, request,
206 PLDM_GET_PDR_REQ_BYTES);
George Liud6649362019-11-27 19:06:51 +0800207 return {rc, requestMsg};
208 }
209
210 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
211 {
212 uint8_t completionCode = 0;
Deepak Kodihallia59cdc62020-07-14 05:11:33 -0500213 uint8_t recordData[UINT16_MAX] = {0};
George Liud6649362019-11-27 19:06:51 +0800214 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
Pavithra Barithayaa340eed2023-02-14 03:32:47 -0600233 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 }
George Liud6649362019-11-27 19:06:51 +0800253 }
254
255 private:
Tom Josepha65c0412020-07-03 21:14:44 +0530256 const std::map<pldm::pdr::EntityType, std::string> entityType = {
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530257 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530273 {PLDM_ENTITY_SYS_FIRMWARE, "System Firmware"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530274 {PLDM_ENTITY_OPERATING_SYS, "Operating System"},
Tom Josepha65c0412020-07-03 21:14:44 +0530275 {PLDM_ENTITY_VIRTUAL_MACHINE_MANAGER, "Virtual Machine Manager"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530276 {PLDM_ENTITY_OS_LOADER, "OS Loader"},
277 {PLDM_ENTITY_DEVICE_DRIVER, "Device Driver"},
278 {PLDM_ENTITY_MGMT_CONTROLLER_FW, "Management Controller Firmware"},
Tom Josepha65c0412020-07-03 21:14:44 +0530279 {PLDM_ENTITY_SYSTEM_CHASSIS, "System chassis (main enclosure)"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530280 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530291 {PLDM_ENTITY_SYS_BOARD, "System Board"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530292 {PLDM_ENTITY_MEMORY_BOARD, "Memory Board"},
Tom Josepha65c0412020-07-03 21:14:44 +0530293 {PLDM_ENTITY_MEMORY_MODULE, "Memory Module"},
294 {PLDM_ENTITY_PROC_MODULE, "Processor Module"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530295 {PLDM_ENTITY_ADD_IN_CARD, "Add-in Card"},
Tom Josepha65c0412020-07-03 21:14:44 +0530296 {PLDM_ENTITY_CHASSIS_FRONT_PANEL_BOARD,
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530297 "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"},
Jayashankar Padath79175b42021-05-20 22:54:34 -0500314 {PLDM_ENTITY_FAN, "Fan"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530315 {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"},
Jayashankar Padath79175b42021-05-20 22:54:34 -0500325 {PLDM_ENTITY_SOLID_STATE_SRIVE, "Solid State Drive"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530326 {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"},
Jayashankar Padath79175b42021-05-20 22:54:34 -0500345 {PLDM_ENTITY_REAL_TIME_CLOCK, "Real Time Clock (RTC)"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530346 {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"},
Jayashankar Padath79175b42021-05-20 22:54:34 -0500363 {PLDM_ENTITY_SLOT, "Slot"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530364 {PLDM_ENTITY_CABLE, "Cable(electrical or optical)"},
365 {PLDM_ENTITY_INTERCONNECT, "Interconnect"},
366 {PLDM_ENTITY_PLUG, "Plug"},
367 {PLDM_ENTITY_SOCKET, "Socket"},
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500368 };
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500369
Tom Josepha65c0412020-07-03 21:14:44 +0530370 const std::map<uint16_t, std::string> stateSet = {
371 {PLDM_STATE_SET_HEALTH_STATE, "Health State"},
372 {PLDM_STATE_SET_AVAILABILITY, "Availability"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530373 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530378 {PLDM_STATE_SET_OPERATIONAL_STATUS, "Operational Status"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530379 {PLDM_STATE_SET_OPERATIONAL_STRESS_STATUS, "Operational Stress Status"},
380 {PLDM_STATE_SET_OPERATIONAL_FAULT_STATUS, "Operational Fault Status"},
Tom Josepha65c0412020-07-03 21:14:44 +0530381 {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
382 "Operational Running Status"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530383 {PLDM_STATE_SET_OPERATIONAL_CONNECTION_STATUS,
384 "Operational Connection Status"},
Tom Josepha65c0412020-07-03 21:14:44 +0530385 {PLDM_STATE_SET_PRESENCE, "Presence"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530386 {PLDM_STATE_SET_PERFORMANCE, "Performance"},
Tom Josepha65c0412020-07-03 21:14:44 +0530387 {PLDM_STATE_SET_CONFIGURATION_STATE, "Configuration State"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530388 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530395 {PLDM_STATE_SET_LINK_STATE, "Link State"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530396 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530414 {PLDM_STATE_SET_SW_TERMINATION_STATUS, "Software Termination Status"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530415 {PLDM_STATE_SET_STORAGE_MEDIA_ACTIVITY, "Storage Media Activity"},
Tom Josepha65c0412020-07-03 21:14:44 +0530416 {PLDM_STATE_SET_BOOT_RESTART_CAUSE, "Boot/Restart Cause"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530417 {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"},
Tom Josepha65c0412020-07-03 21:14:44 +0530420 {PLDM_STATE_SET_BOOT_PROGRESS, "Boot Progress"},
Manojkiran Edaafbdb6d2021-05-27 17:19:51 +0530421 {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"},
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500473 {PLDM_STATE_SET_SLOT_STATE, "Slot State"},
474 };
Tom Josepha65c0412020-07-03 21:14:44 +0530475
476 const std::array<std::string_view, 4> sensorInit = {
477 "noInit", "useInitPDR", "enableSensor", "disableSensor"};
478
Tom Joseph97a7a762020-07-06 10:37:18 +0530479 const std::array<std::string_view, 4> effecterInit = {
480 "noInit", "useInitPDR", "enableEffecter", "disableEffecter"};
481
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500482 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"},
Thu Nguyen9d0e58c2021-12-09 13:53:00 +0700496 {PLDM_COMPACT_NUMERIC_SENSOR_PDR, "Compact Numeric Sensor PDR"},
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500497 {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
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500512 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,
kamal00a076d2022-11-08 04:24:36 -0600534 "Primary Processor Initialization"},
535 {PLDM_STATE_SET_BOOT_PROG_STATE_OSSTART, "OSStart"}};
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500536
537 static inline const std::map<uint8_t, std::string> setOpFaultStatus{
Pavithra Barithaya6e20f7c2023-08-22 00:50:47 -0500538 {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"}};
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500542
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"},
Manojkiran Eda5de3de52022-01-03 12:12:53 +0530556 {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"},
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500561 {PLDM_STATE_SET_HEALTH_STATE_UPPER_CRITICAL, "Upper Critical"},
Manojkiran Eda5de3de52022-01-03 12:12:53 +0530562 {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"}};
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500575
Riya Dixit43e33d22024-02-07 09:53:49 -0600576 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
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500585 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},
Manojkiran Eda5de3de52022-01-03 12:12:53 +0530595 {PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
596 setOperationalRunningState},
Riya Dixit43e33d22024-02-07 09:53:49 -0600597 {PLDM_STATE_SET_DEVICE_POWER_STATE, setPowerDeviceState},
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500598 };
599
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500600 const std::map<std::string, uint8_t> strToPdrType = {
601 {"terminuslocator", PLDM_TERMINUS_LOCATOR_PDR},
602 {"statesensor", PLDM_STATE_SENSOR_PDR},
Thu Nguyen846d38f2022-03-26 08:57:21 +0700603 {"sensorauxname", PLDM_SENSOR_AUXILIARY_NAMES_PDR},
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500604 {"numericeffecter", PLDM_NUMERIC_EFFECTER_PDR},
Thu Nguyen846d38f2022-03-26 08:57:21 +0700605 {"efffecterauxname", PLDM_EFFECTER_AUXILIARY_NAMES_PDR},
Thu Nguyen9d0e58c2021-12-09 13:53:00 +0700606 {"compactnumericsensor", PLDM_COMPACT_NUMERIC_SENSOR_PDR},
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -0500607 {"stateeffecter", PLDM_STATE_EFFECTER_PDR},
608 {"entityassociation", PLDM_PDR_ENTITY_ASSOCIATION},
609 {"frurecord", PLDM_PDR_FRU_RECORD_SET},
610 // Add other types
611 };
612
Manojkiran Eda0c5d1ec2021-05-29 11:21:25 +0530613 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
Tom Josepha65c0412020-07-03 21:14:44 +0530623 std::string getEntityName(pldm::pdr::EntityType type)
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500624 {
Manojkiran Eda0c5d1ec2021-05-29 11:21:25 +0530625 uint16_t entityNumber = type;
626 std::string entityName = "[Physical] ";
627
628 if (isLogicalBitSet(type))
629 {
630 entityName = "[Logical] ";
631 entityNumber = getEntityTypeForLogicalEntity(type);
632 }
633
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500634 try
635 {
Manojkiran Eda0c5d1ec2021-05-29 11:21:25 +0530636 return entityName + entityType.at(entityNumber);
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500637 }
638 catch (const std::out_of_range& e)
639 {
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500640 auto OemString =
641 std::to_string(static_cast<unsigned>(entityNumber));
Manojkiran Eda0c5d1ec2021-05-29 11:21:25 +0530642 if (type >= PLDM_OEM_ENTITY_TYPE_START &&
643 type <= PLDM_OEM_ENTITY_TYPE_END)
644 {
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500645#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)";
Manojkiran Eda0c5d1ec2021-05-29 11:21:25 +0530653 }
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500654 return OemString;
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500655 }
656 }
657
Tom Josepha65c0412020-07-03 21:14:44 +0530658 std::string getStateSetName(uint16_t id)
659 {
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500660 auto typeString = std::to_string(id);
Tom Josepha65c0412020-07-03 21:14:44 +0530661 try
662 {
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500663 return stateSet.at(id) + "(" + typeString + ")";
Tom Josepha65c0412020-07-03 21:14:44 +0530664 }
665 catch (const std::out_of_range& e)
666 {
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500667 return typeString;
668 }
669 }
670
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500671 std::vector<std::string>
672 getStateSetPossibleStateNames(uint16_t stateId,
673 const std::vector<uint8_t>& value)
674 {
675 std::vector<std::string> data{};
676 std::map<uint8_t, std::string> stateNameMaps;
677
678 for (auto& s : value)
679 {
680 std::map<uint8_t, std::string> stateNameMaps;
681 auto pstr = std::to_string(s);
682
683#ifdef OEM_IBM
Manojkiran Edabe1a8572022-01-06 11:20:36 +0530684 if (stateId >= PLDM_OEM_STATE_SET_ID_START &&
685 stateId < PLDM_OEM_STATE_SET_ID_END)
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500686 {
687 if (populateOemIBMStateMaps.contains(stateId))
688 {
689 const std::map<uint8_t, std::string> stateNames =
690 populateOemIBMStateMaps.at(stateId);
691 stateNameMaps.insert(stateNames.begin(), stateNames.end());
692 }
693 }
694#endif
695 if (populatePStateMaps.contains(stateId))
696 {
697 const std::map<uint8_t, std::string> stateNames =
698 populatePStateMaps.at(stateId);
699 stateNameMaps.insert(stateNames.begin(), stateNames.end());
700 }
701 if (stateNameMaps.contains(s))
702 {
703 data.push_back(stateNameMaps.at(s) + "(" + pstr + ")");
704 }
705 else
706 {
707 data.push_back(pstr);
708 }
709 }
710 return data;
711 }
712
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500713 std::string getPDRType(uint8_t type)
714 {
715 auto typeString = std::to_string(type);
716 try
717 {
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500718 return pdrType.at(type);
Sridevi Ramesha1bfb782020-07-14 02:51:23 -0500719 }
720 catch (const std::out_of_range& e)
721 {
722 return typeString;
Tom Josepha65c0412020-07-03 21:14:44 +0530723 }
724 }
725
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500726 void printCommonPDRHeader(const pldm_pdr_hdr* hdr, ordered_json& output)
Tom Joseph952abfa2020-07-03 12:25:15 +0530727 {
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500728 output["recordHandle"] = hdr->record_handle;
729 output["PDRHeaderVersion"] = unsigned(hdr->version);
730 output["PDRType"] = getPDRType(hdr->type);
731 output["recordChangeNumber"] = hdr->record_change_num;
732 output["dataLength"] = hdr->length;
Tom Joseph952abfa2020-07-03 12:25:15 +0530733 }
734
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500735 std::vector<uint8_t> printPossibleStates(uint8_t possibleStatesSize,
736 const bitfield8_t* states)
Tom Josepha65c0412020-07-03 21:14:44 +0530737 {
738 uint8_t possibleStatesPos{};
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500739 std::vector<uint8_t> data{};
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500740 auto printStates = [&possibleStatesPos, &data](const bitfield8_t& val) {
741 std::stringstream pstates;
Tom Josepha65c0412020-07-03 21:14:44 +0530742 for (int i = 0; i < CHAR_BIT; i++)
743 {
744 if (val.byte & (1 << i))
745 {
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500746 pstates << (possibleStatesPos * CHAR_BIT + i);
747 data.push_back(
748 static_cast<uint8_t>(std::stoi(pstates.str())));
Sridevi Ramesh148ccab2020-11-23 08:23:09 -0600749 pstates.str("");
Tom Josepha65c0412020-07-03 21:14:44 +0530750 }
751 }
752 possibleStatesPos++;
753 };
754 std::for_each(states, states + possibleStatesSize, printStates);
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500755 return data;
Tom Josepha65c0412020-07-03 21:14:44 +0530756 }
757
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500758 void printStateSensorPDR(const uint8_t* data, ordered_json& output)
Tom Josepha65c0412020-07-03 21:14:44 +0530759 {
760 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(data);
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500761 output["PLDMTerminusHandle"] = pdr->terminus_handle;
762 output["sensorID"] = pdr->sensor_id;
763 output["entityType"] = getEntityName(pdr->entity_type);
764 output["entityInstanceNumber"] = pdr->entity_instance;
765 output["containerID"] = pdr->container_id;
766 output["sensorInit"] = sensorInit[pdr->sensor_init];
767 output["sensorAuxiliaryNamesPDR"] =
768 (pdr->sensor_auxiliary_names_pdr ? true : false);
769 output["compositeSensorCount"] = unsigned(pdr->composite_sensor_count);
Tom Josepha65c0412020-07-03 21:14:44 +0530770
771 auto statesPtr = pdr->possible_states;
Sridevi Ramesh148ccab2020-11-23 08:23:09 -0600772 auto compCount = pdr->composite_sensor_count;
Tom Josepha65c0412020-07-03 21:14:44 +0530773
Sridevi Ramesh148ccab2020-11-23 08:23:09 -0600774 while (compCount--)
Tom Josepha65c0412020-07-03 21:14:44 +0530775 {
776 auto state = reinterpret_cast<const state_sensor_possible_states*>(
777 statesPtr);
Sridevi Ramesh148ccab2020-11-23 08:23:09 -0600778 output.emplace(("stateSetID[" + std::to_string(compCount) + "]"),
779 getStateSetName(state->state_set_id));
780 output.emplace(
781 ("possibleStatesSize[" + std::to_string(compCount) + "]"),
782 state->possible_states_size);
783 output.emplace(
784 ("possibleStates[" + std::to_string(compCount) + "]"),
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -0500785 getStateSetPossibleStateNames(
786 state->state_set_id,
787 printPossibleStates(state->possible_states_size,
788 state->states)));
Tom Josepha65c0412020-07-03 21:14:44 +0530789
Sridevi Ramesh148ccab2020-11-23 08:23:09 -0600790 if (compCount)
Tom Josepha65c0412020-07-03 21:14:44 +0530791 {
792 statesPtr += sizeof(state_sensor_possible_states) +
793 state->possible_states_size - 1;
794 }
795 }
796 }
797
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500798 void printPDRFruRecordSet(uint8_t* data, ordered_json& output)
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500799 {
George Liu62d12ec2020-02-05 16:27:08 +0800800 if (data == NULL)
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500801 {
802 return;
803 }
804
805 data += sizeof(pldm_pdr_hdr);
806 pldm_pdr_fru_record_set* pdr =
807 reinterpret_cast<pldm_pdr_fru_record_set*>(data);
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530808 if (!pdr)
809 {
810 std::cerr << "Failed to get the FRU record set PDR" << std::endl;
811 return;
812 }
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500813
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500814 output["PLDMTerminusHandle"] = unsigned(pdr->terminus_handle);
815 output["FRURecordSetIdentifier"] = unsigned(pdr->fru_rsi);
816 output["entityType"] = getEntityName(pdr->entity_type);
Sagar Srinivasebf8bb52023-10-06 01:15:55 -0500817 output["entityInstanceNumber"] = unsigned(pdr->entity_instance);
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500818 output["containerID"] = unsigned(pdr->container_id);
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500819 }
820
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500821 void printPDREntityAssociation(uint8_t* data, ordered_json& output)
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500822 {
823 const std::map<uint8_t, const char*> assocationType = {
824 {PLDM_ENTITY_ASSOCIAION_PHYSICAL, "Physical"},
825 {PLDM_ENTITY_ASSOCIAION_LOGICAL, "Logical"},
826 };
827
George Liu62d12ec2020-02-05 16:27:08 +0800828 if (data == NULL)
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500829 {
830 return;
831 }
832
833 data += sizeof(pldm_pdr_hdr);
834 pldm_pdr_entity_association* pdr =
835 reinterpret_cast<pldm_pdr_entity_association*>(data);
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530836 if (!pdr)
837 {
838 std::cerr << "Failed to get the PDR eneity association"
839 << std::endl;
840 return;
841 }
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500842
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500843 output["containerID"] = int(pdr->container_id);
George Liubd5e2ea2021-04-22 20:33:06 +0800844 if (assocationType.contains(pdr->association_type))
845 {
846 output["associationType"] =
847 assocationType.at(pdr->association_type);
848 }
849 else
850 {
851 std::cout << "Get associationType failed.\n";
852 }
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500853 output["containerEntityType"] =
854 getEntityName(pdr->container.entity_type);
855 output["containerEntityInstanceNumber"] =
856 int(pdr->container.entity_instance_num);
857 output["containerEntityContainerID"] =
858 int(pdr->container.entity_container_id);
859 output["containedEntityCount"] =
860 static_cast<unsigned>(pdr->num_children);
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500861
862 auto child = reinterpret_cast<pldm_entity*>(&pdr->children[0]);
863 for (int i = 0; i < pdr->num_children; ++i)
864 {
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500865 output.emplace("containedEntityType[" + std::to_string(i + 1) + "]",
866 getEntityName(child->entity_type));
867 output.emplace("containedEntityInstanceNumber[" +
868 std::to_string(i + 1) + "]",
869 unsigned(child->entity_instance_num));
870 output.emplace("containedEntityContainerID[" +
871 std::to_string(i + 1) + "]",
872 unsigned(child->entity_container_id));
873
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -0500874 ++child;
875 }
876 }
877
Thu Nguyen846d38f2022-03-26 08:57:21 +0700878 /** @brief Format the Sensor/Effecter Aux Name PDR types to json output
879 *
880 * @param[in] data - reference to the Sensor/Effecter Aux Name PDR
881 * @param[out] output - PDRs data fields in Json format
882 */
883 void printAuxNamePDR(uint8_t* data, ordered_json& output)
884 {
885 constexpr uint8_t nullTerminator = 0;
886 struct pldm_effecter_aux_name_pdr* auxNamePdr =
887 (struct pldm_effecter_aux_name_pdr*)data;
888
889 if (!auxNamePdr)
890 {
891 std::cerr << "Failed to get Aux Name PDR" << std::endl;
892 return;
893 }
894
895 std::string sPrefix = "effecter";
896 if (auxNamePdr->hdr.type == PLDM_SENSOR_AUXILIARY_NAMES_PDR)
897 {
898 sPrefix = "sensor";
899 }
900 output["terminusHandle"] = int(auxNamePdr->terminus_handle);
901 output[sPrefix + "Id"] = int(auxNamePdr->effecter_id);
902 output[sPrefix + "Count"] = int(auxNamePdr->effecter_count);
903
904 const uint8_t* ptr = auxNamePdr->effecter_names;
905 for (auto i : std::views::iota(0, (int)auxNamePdr->effecter_count))
906 {
907 const uint8_t nameStringCount = static_cast<uint8_t>(*ptr);
908 ptr += sizeof(uint8_t);
909 for (auto j : std::views::iota(0, (int)nameStringCount))
910 {
911 std::string nameLanguageTagKey = sPrefix + std::to_string(j) +
912 "_nameLanguageTag" +
913 std::to_string(i);
914 std::string entityAuxNameKey = sPrefix + std::to_string(j) +
915 "_entityAuxName" +
916 std::to_string(i);
917 std::string nameLanguageTag(reinterpret_cast<const char*>(ptr),
918 0, PLDM_STR_UTF_8_MAX_LEN);
919 ptr += nameLanguageTag.size() + sizeof(nullTerminator);
920 std::u16string u16NameString(
921 reinterpret_cast<const char16_t*>(ptr), 0,
922 PLDM_STR_UTF_16_MAX_LEN);
923 ptr += (u16NameString.size() + sizeof(nullTerminator)) *
924 sizeof(uint16_t);
925 std::transform(u16NameString.cbegin(), u16NameString.cend(),
926 u16NameString.begin(),
927 [](uint16_t utf16) { return be16toh(utf16); });
928 std::string nameString =
929 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
930 char16_t>{}
931 .to_bytes(u16NameString);
932 output[nameLanguageTagKey] = nameLanguageTag;
933 output[entityAuxNameKey] = nameString;
934 }
935 }
936 }
937
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500938 void printNumericEffecterPDR(uint8_t* data, ordered_json& output)
George Liu62d12ec2020-02-05 16:27:08 +0800939 {
940 struct pldm_numeric_effecter_value_pdr* pdr =
941 (struct pldm_numeric_effecter_value_pdr*)data;
Manojkiran Edabcf91ac2021-03-14 13:50:48 +0530942 if (!pdr)
943 {
944 std::cerr << "Failed to get numeric effecter PDR" << std::endl;
945 return;
946 }
Sridevi Ramesh27c512a2020-08-12 03:29:42 -0500947
948 output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
949 output["effecterID"] = int(pdr->effecter_id);
950 output["entityType"] = int(pdr->entity_type);
951 output["entityInstanceNumber"] = int(pdr->entity_instance);
952 output["containerID"] = int(pdr->container_id);
953 output["effecterSemanticID"] = int(pdr->effecter_semantic_id);
954 output["effecterInit"] = unsigned(pdr->effecter_init);
955 output["effecterAuxiliaryNames"] =
956 (unsigned(pdr->effecter_auxiliary_names) ? true : false);
957 output["baseUnit"] = unsigned(pdr->base_unit);
958 output["unitModifier"] = unsigned(pdr->unit_modifier);
959 output["rateUnit"] = unsigned(pdr->rate_unit);
960 output["baseOEMUnitHandle"] = unsigned(pdr->base_oem_unit_handle);
961 output["auxUnit"] = unsigned(pdr->aux_unit);
962 output["auxUnitModifier"] = unsigned(pdr->aux_unit_modifier);
963 output["auxrateUnit"] = unsigned(pdr->aux_rate_unit);
964 output["auxOEMUnitHandle"] = unsigned(pdr->aux_oem_unit_handle);
965 output["isLinear"] = (unsigned(pdr->is_linear) ? true : false);
966 output["effecterDataSize"] = unsigned(pdr->effecter_data_size);
967 output["resolution"] = unsigned(pdr->resolution);
968 output["offset"] = unsigned(pdr->offset);
969 output["accuracy"] = unsigned(pdr->accuracy);
970 output["plusTolerance"] = unsigned(pdr->plus_tolerance);
971 output["minusTolerance"] = unsigned(pdr->minus_tolerance);
972 output["stateTransitionInterval"] =
973 unsigned(pdr->state_transition_interval);
974 output["TransitionInterval"] = unsigned(pdr->transition_interval);
975
George Liu62d12ec2020-02-05 16:27:08 +0800976 switch (pdr->effecter_data_size)
977 {
978 case PLDM_EFFECTER_DATA_SIZE_UINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530979 output["maxSettable"] = unsigned(pdr->max_settable.value_u8);
980 output["minSettable"] = unsigned(pdr->min_settable.value_u8);
George Liu62d12ec2020-02-05 16:27:08 +0800981 break;
982 case PLDM_EFFECTER_DATA_SIZE_SINT8:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530983 output["maxSettable"] = unsigned(pdr->max_settable.value_s8);
984 output["minSettable"] = unsigned(pdr->min_settable.value_s8);
George Liu62d12ec2020-02-05 16:27:08 +0800985 break;
986 case PLDM_EFFECTER_DATA_SIZE_UINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530987 output["maxSettable"] = unsigned(pdr->max_settable.value_u16);
988 output["minSettable"] = unsigned(pdr->min_settable.value_u16);
George Liu62d12ec2020-02-05 16:27:08 +0800989 break;
990 case PLDM_EFFECTER_DATA_SIZE_SINT16:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530991 output["maxSettable"] = unsigned(pdr->max_settable.value_s16);
992 output["minSettable"] = unsigned(pdr->min_settable.value_s16);
George Liu62d12ec2020-02-05 16:27:08 +0800993 break;
994 case PLDM_EFFECTER_DATA_SIZE_UINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530995 output["maxSettable"] = unsigned(pdr->max_settable.value_u32);
996 output["minSettable"] = unsigned(pdr->min_settable.value_u32);
George Liu62d12ec2020-02-05 16:27:08 +0800997 break;
998 case PLDM_EFFECTER_DATA_SIZE_SINT32:
Manojkiran Edafc0ce972022-06-25 09:38:28 +0530999 output["maxSettable"] = unsigned(pdr->max_settable.value_s32);
1000 output["minSettable"] = unsigned(pdr->min_settable.value_s32);
George Liu62d12ec2020-02-05 16:27:08 +08001001 break;
1002 default:
1003 break;
1004 }
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001005
1006 output["rangeFieldFormat"] = unsigned(pdr->range_field_format);
1007 output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1008
George Liu62d12ec2020-02-05 16:27:08 +08001009 switch (pdr->range_field_format)
1010 {
1011 case PLDM_RANGE_FIELD_FORMAT_UINT8:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001012 output["nominalValue"] = unsigned(pdr->nominal_value.value_u8);
1013 output["normalMax"] = unsigned(pdr->normal_max.value_u8);
1014 output["normalMin"] = unsigned(pdr->normal_min.value_u8);
1015 output["ratedMax"] = unsigned(pdr->rated_max.value_u8);
1016 output["ratedMin"] = unsigned(pdr->rated_min.value_u8);
George Liu62d12ec2020-02-05 16:27:08 +08001017 break;
1018 case PLDM_RANGE_FIELD_FORMAT_SINT8:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001019 output["nominalValue"] = unsigned(pdr->nominal_value.value_s8);
1020 output["normalMax"] = unsigned(pdr->normal_max.value_s8);
1021 output["normalMin"] = unsigned(pdr->normal_min.value_s8);
1022 output["ratedMax"] = unsigned(pdr->rated_max.value_s8);
1023 output["ratedMin"] = unsigned(pdr->rated_min.value_s8);
George Liu62d12ec2020-02-05 16:27:08 +08001024 break;
1025 case PLDM_RANGE_FIELD_FORMAT_UINT16:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001026 output["nominalValue"] = unsigned(pdr->nominal_value.value_u16);
1027 output["normalMax"] = unsigned(pdr->normal_max.value_u16);
1028 output["normalMin"] = unsigned(pdr->normal_min.value_u16);
1029 output["ratedMax"] = unsigned(pdr->rated_max.value_u16);
1030 output["ratedMin"] = unsigned(pdr->rated_min.value_u16);
George Liu62d12ec2020-02-05 16:27:08 +08001031 break;
1032 case PLDM_RANGE_FIELD_FORMAT_SINT16:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001033 output["nominalValue"] = unsigned(pdr->nominal_value.value_s16);
1034 output["normalMax"] = unsigned(pdr->normal_max.value_s16);
1035 output["normalMin"] = unsigned(pdr->normal_min.value_s16);
1036 output["ratedMax"] = unsigned(pdr->rated_max.value_s16);
1037 output["ratedMin"] = unsigned(pdr->rated_min.value_s16);
George Liu62d12ec2020-02-05 16:27:08 +08001038 break;
1039 case PLDM_RANGE_FIELD_FORMAT_UINT32:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001040 output["nominalValue"] = unsigned(pdr->nominal_value.value_u32);
1041 output["normalMax"] = unsigned(pdr->normal_max.value_u32);
1042 output["normalMin"] = unsigned(pdr->normal_min.value_u32);
1043 output["ratedMax"] = unsigned(pdr->rated_max.value_u32);
1044 output["ratedMin"] = unsigned(pdr->rated_min.value_u32);
George Liu62d12ec2020-02-05 16:27:08 +08001045 break;
1046 case PLDM_RANGE_FIELD_FORMAT_SINT32:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001047 output["nominalValue"] = unsigned(pdr->nominal_value.value_s32);
1048 output["normalMax"] = unsigned(pdr->normal_max.value_s32);
1049 output["normalMin"] = unsigned(pdr->normal_min.value_s32);
1050 output["ratedMax"] = unsigned(pdr->rated_max.value_s32);
1051 output["ratedMin"] = unsigned(pdr->rated_min.value_s32);
George Liu62d12ec2020-02-05 16:27:08 +08001052 break;
1053 case PLDM_RANGE_FIELD_FORMAT_REAL32:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001054 output["nominalValue"] = unsigned(pdr->nominal_value.value_f32);
1055 output["normalMax"] = unsigned(pdr->normal_max.value_f32);
1056 output["normalMin"] = unsigned(pdr->normal_min.value_f32);
1057 output["ratedMax"] = unsigned(pdr->rated_max.value_f32);
1058 output["ratedMin"] = unsigned(pdr->rated_min.value_f32);
George Liu62d12ec2020-02-05 16:27:08 +08001059 break;
1060 default:
1061 break;
1062 }
1063 }
1064
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001065 void printStateEffecterPDR(const uint8_t* data, ordered_json& output)
George Liu62d12ec2020-02-05 16:27:08 +08001066 {
Tom Joseph97a7a762020-07-06 10:37:18 +05301067 auto pdr = reinterpret_cast<const pldm_state_effecter_pdr*>(data);
George Liu62d12ec2020-02-05 16:27:08 +08001068
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001069 output["PLDMTerminusHandle"] = pdr->terminus_handle;
1070 output["effecterID"] = pdr->effecter_id;
1071 output["entityType"] = getEntityName(pdr->entity_type);
1072 output["entityInstanceNumber"] = pdr->entity_instance;
1073 output["containerID"] = pdr->container_id;
1074 output["effecterSemanticID"] = pdr->effecter_semantic_id;
1075 output["effecterInit"] = effecterInit[pdr->effecter_init];
Patrick Williams6da4f912023-05-10 07:50:53 -05001076 output["effecterDescriptionPDR"] = (pdr->has_description_pdr ? true
1077 : false);
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001078 output["compositeEffecterCount"] =
1079 unsigned(pdr->composite_effecter_count);
George Liud6649362019-11-27 19:06:51 +08001080
Tom Joseph97a7a762020-07-06 10:37:18 +05301081 auto statesPtr = pdr->possible_states;
Sridevi Ramesh148ccab2020-11-23 08:23:09 -06001082 auto compEffCount = pdr->composite_effecter_count;
Tom Joseph97a7a762020-07-06 10:37:18 +05301083
Sridevi Ramesh148ccab2020-11-23 08:23:09 -06001084 while (compEffCount--)
George Liud6649362019-11-27 19:06:51 +08001085 {
Tom Joseph97a7a762020-07-06 10:37:18 +05301086 auto state =
1087 reinterpret_cast<const state_effecter_possible_states*>(
1088 statesPtr);
Sridevi Ramesh148ccab2020-11-23 08:23:09 -06001089 output.emplace(("stateSetID[" + std::to_string(compEffCount) + "]"),
1090 getStateSetName(state->state_set_id));
1091 output.emplace(
1092 ("possibleStatesSize[" + std::to_string(compEffCount) + "]"),
1093 state->possible_states_size);
1094 output.emplace(
1095 ("possibleStates[" + std::to_string(compEffCount) + "]"),
Sridevi Rameshdcdcd3b2021-06-15 04:06:49 -05001096 getStateSetPossibleStateNames(
1097 state->state_set_id,
1098 printPossibleStates(state->possible_states_size,
1099 state->states)));
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001100
Sridevi Ramesh148ccab2020-11-23 08:23:09 -06001101 if (compEffCount)
Tom Joseph97a7a762020-07-06 10:37:18 +05301102 {
1103 statesPtr += sizeof(state_effecter_possible_states) +
1104 state->possible_states_size - 1;
1105 }
George Liud6649362019-11-27 19:06:51 +08001106 }
1107 }
1108
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001109 bool checkTerminusHandle(const uint8_t* data,
1110 std::optional<uint16_t> terminusHandle)
1111 {
1112 struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1113
1114 if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1115 {
1116 auto tlpdr =
1117 reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1118
1119 if (tlpdr->terminus_handle != terminusHandle)
1120 {
1121 return true;
1122 }
1123 }
1124 else if (pdr->type == PLDM_STATE_SENSOR_PDR)
1125 {
1126 auto sensor = reinterpret_cast<const pldm_state_sensor_pdr*>(data);
1127
1128 if (sensor->terminus_handle != terminusHandle)
1129 {
1130 return true;
1131 }
1132 }
1133 else if (pdr->type == PLDM_NUMERIC_EFFECTER_PDR)
1134 {
1135 auto numericEffecter =
1136 reinterpret_cast<const pldm_numeric_effecter_value_pdr*>(data);
1137
1138 if (numericEffecter->terminus_handle != terminusHandle)
1139 {
1140 return true;
1141 }
1142 }
1143
1144 else if (pdr->type == PLDM_STATE_EFFECTER_PDR)
1145 {
1146 auto stateEffecter =
1147 reinterpret_cast<const pldm_state_effecter_pdr*>(data);
1148 if (stateEffecter->terminus_handle != terminusHandle)
1149 {
1150 return true;
1151 }
1152 }
1153 else if (pdr->type == PLDM_PDR_FRU_RECORD_SET)
1154 {
1155 data += sizeof(pldm_pdr_hdr);
1156 auto fru = reinterpret_cast<const pldm_pdr_fru_record_set*>(data);
1157
1158 if (fru->terminus_handle != terminusHandle)
1159 {
1160 return true;
1161 }
1162 }
1163 else
1164 {
1165 // Entity association PDRs does not have terminus handle
1166 return true;
1167 }
1168
1169 return false;
1170 }
1171
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001172 void printTerminusLocatorPDR(const uint8_t* data, ordered_json& output)
Sampa Misra12afe112020-05-25 11:40:44 -05001173 {
1174 const std::array<std::string_view, 4> terminusLocatorType = {
1175 "UID", "MCTP_EID", "SMBusRelative", "systemSoftware"};
1176
1177 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1178
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001179 output["PLDMTerminusHandle"] = pdr->terminus_handle;
1180 output["validity"] = (pdr->validity ? "valid" : "notValid");
1181 output["TID"] = unsigned(pdr->tid);
1182 output["containerID"] = pdr->container_id;
1183 output["terminusLocatorType"] =
1184 terminusLocatorType[pdr->terminus_locator_type];
1185 output["terminusLocatorValueSize"] =
1186 unsigned(pdr->terminus_locator_value_size);
Sampa Misra12afe112020-05-25 11:40:44 -05001187
1188 if (pdr->terminus_locator_type == PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
1189 {
1190 auto locatorValue =
1191 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
1192 pdr->terminus_locator_value);
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001193 output["EID"] = unsigned(locatorValue->eid);
Sampa Misra12afe112020-05-25 11:40:44 -05001194 }
1195 }
1196
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001197 std::optional<uint16_t> getTerminusHandle(uint8_t* data,
1198 std::optional<uint8_t> tid)
1199 {
1200 struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
1201 if (pdr->type == PLDM_TERMINUS_LOCATOR_PDR)
1202 {
1203 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(data);
1204 if (pdr->tid == tid)
1205 {
1206 handleFound = true;
1207 return pdr->terminus_handle;
1208 }
1209 }
1210 return std::nullopt;
1211 }
1212
Thu Nguyen9d0e58c2021-12-09 13:53:00 +07001213 /** @brief Format the Compact Numeric Sensor PDR types to json output
1214 *
1215 * @param[in] data - reference to the Compact Numeric Sensor PDR
1216 * @param[out] output - PDRs data fields in Json format
1217 */
1218 void printCompactNumericSensorPDR(const uint8_t* data, ordered_json& output)
1219 {
1220 struct pldm_compact_numeric_sensor_pdr* pdr =
1221 (struct pldm_compact_numeric_sensor_pdr*)data;
1222 if (!pdr)
1223 {
1224 std::cerr << "Failed to get compact numeric sensor PDR"
1225 << std::endl;
1226 return;
1227 }
1228 output["PLDMTerminusHandle"] = int(pdr->terminus_handle);
1229 output["sensorID"] = int(pdr->sensor_id);
1230 output["entityType"] = getEntityName(pdr->entity_type);
1231 output["entityInstanceNumber"] = int(pdr->entity_instance);
1232 output["containerID"] = int(pdr->container_id);
1233 output["sensorNameStringByteLength"] = int(pdr->sensor_name_length);
1234 if (pdr->sensor_name_length == 0)
1235 {
1236 output["Name"] = std::format("PLDM_Device_TID{}_SensorId{}",
1237 unsigned(pdr->terminus_handle),
1238 unsigned(pdr->sensor_id));
1239 }
1240 else
1241 {
1242 std::string sTemp(reinterpret_cast<const char*>(pdr->sensor_name),
1243 pdr->sensor_name_length);
1244 output["Name"] = sTemp;
1245 }
1246 output["baseUnit"] = unsigned(pdr->base_unit);
1247 output["unitModifier"] = signed(pdr->unit_modifier);
1248 output["occurrenceRate"] = unsigned(pdr->occurrence_rate);
1249 output["rangeFieldSupport"] = unsigned(pdr->range_field_support.byte);
1250 if (pdr->range_field_support.bits.bit0)
1251 {
1252 output["warningHigh"] = int(pdr->warning_high);
1253 }
1254 if (pdr->range_field_support.bits.bit1)
1255 {
1256 output["warningLow"] = int(pdr->warning_low);
1257 }
1258 if (pdr->range_field_support.bits.bit2)
1259 {
1260 output["criticalHigh"] = int(pdr->critical_high);
1261 }
1262 if (pdr->range_field_support.bits.bit3)
1263 {
1264 output["criticalLow"] = int(pdr->critical_low);
1265 }
1266 if (pdr->range_field_support.bits.bit4)
1267 {
1268 output["fatalHigh"] = int(pdr->fatal_high);
1269 }
1270 if (pdr->range_field_support.bits.bit5)
1271 {
1272 output["fatalLow"] = int(pdr->fatal_low);
1273 }
1274 }
1275
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -05001276 void printPDRMsg(uint32_t& nextRecordHndl, const uint16_t respCnt,
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001277 uint8_t* data, std::optional<uint16_t> terminusHandle)
George Liud6649362019-11-27 19:06:51 +08001278 {
George Liu62d12ec2020-02-05 16:27:08 +08001279 if (data == NULL)
George Liud6649362019-11-27 19:06:51 +08001280 {
Manojkiran Edabcf91ac2021-03-14 13:50:48 +05301281 std::cerr << "Failed to get PDR message" << std::endl;
George Liud6649362019-11-27 19:06:51 +08001282 return;
1283 }
1284
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001285 ordered_json output;
1286 output["nextRecordHandle"] = nextRecordHndl;
1287 output["responseCount"] = respCnt;
George Liud6649362019-11-27 19:06:51 +08001288
1289 struct pldm_pdr_hdr* pdr = (struct pldm_pdr_hdr*)data;
Manojkiran Edabcf91ac2021-03-14 13:50:48 +05301290 if (!pdr)
1291 {
1292 return;
1293 }
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -05001294
1295 if (!pdrRecType.empty())
1296 {
1297 // Need to return if the requested PDR type
1298 // is not supported
1299 if (!strToPdrType.contains(pdrRecType))
1300 {
1301 std::cerr << "PDR type '" << pdrRecType
1302 << "' is not supported or invalid\n";
Pavithra Barithaya6e20f7c2023-08-22 00:50:47 -05001303 // PDR type not supported, setting next record handle to
1304 // 0 to avoid looping through all PDR records
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -05001305 nextRecordHndl = 0;
1306 return;
1307 }
1308
1309 // Do not print PDR record if the current record
1310 // PDR type does not match with requested type
1311 if (pdr->type != strToPdrType.at(pdrRecType))
1312 {
1313 return;
1314 }
1315 }
1316
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001317 if (pdrTerminus.has_value())
1318 {
1319 if (checkTerminusHandle(data, terminusHandle))
1320 {
1321 std::cerr << "The Terminus handle doesn't match return"
1322 << std::endl;
1323 return;
1324 }
1325 }
1326
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001327 printCommonPDRHeader(pdr, output);
1328
George Liud6649362019-11-27 19:06:51 +08001329 switch (pdr->type)
1330 {
Sampa Misra12afe112020-05-25 11:40:44 -05001331 case PLDM_TERMINUS_LOCATOR_PDR:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001332 printTerminusLocatorPDR(data, output);
Sampa Misra12afe112020-05-25 11:40:44 -05001333 break;
Tom Josepha65c0412020-07-03 21:14:44 +05301334 case PLDM_STATE_SENSOR_PDR:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001335 printStateSensorPDR(data, output);
Tom Josepha65c0412020-07-03 21:14:44 +05301336 break;
George Liu62d12ec2020-02-05 16:27:08 +08001337 case PLDM_NUMERIC_EFFECTER_PDR:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001338 printNumericEffecterPDR(data, output);
George Liu62d12ec2020-02-05 16:27:08 +08001339 break;
Thu Nguyen846d38f2022-03-26 08:57:21 +07001340 case PLDM_SENSOR_AUXILIARY_NAMES_PDR:
1341 printAuxNamePDR(data, output);
1342 break;
1343 case PLDM_EFFECTER_AUXILIARY_NAMES_PDR:
1344 printAuxNamePDR(data, output);
1345 break;
George Liud6649362019-11-27 19:06:51 +08001346 case PLDM_STATE_EFFECTER_PDR:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001347 printStateEffecterPDR(data, output);
George Liud6649362019-11-27 19:06:51 +08001348 break;
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -05001349 case PLDM_PDR_ENTITY_ASSOCIATION:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001350 printPDREntityAssociation(data, output);
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -05001351 break;
1352 case PLDM_PDR_FRU_RECORD_SET:
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001353 printPDRFruRecordSet(data, output);
Deepak Kodihalli5544ccd2020-03-15 23:36:16 -05001354 break;
Thu Nguyen9d0e58c2021-12-09 13:53:00 +07001355 case PLDM_COMPACT_NUMERIC_SENSOR_PDR:
1356 printCompactNumericSensorPDR(data, output);
1357 break;
George Liud6649362019-11-27 19:06:51 +08001358 default:
1359 break;
1360 }
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001361 pldmtool::helper::DisplayInJson(output);
George Liud6649362019-11-27 19:06:51 +08001362 }
1363
1364 private:
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001365 bool optTIDSet = false;
George Liud6649362019-11-27 19:06:51 +08001366 uint32_t recordHandle;
Shantappa Teekappanavar3ddbe432021-06-29 15:46:06 -05001367 bool allPDRs;
1368 std::string pdrRecType;
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001369 std::optional<uint8_t> pdrTerminus;
1370 std::optional<uint16_t> terminusHandle;
1371 bool handleFound = false;
1372 CLI::Option* getPDRGroupOption = nullptr;
George Liud6649362019-11-27 19:06:51 +08001373};
1374
1375class SetStateEffecter : public CommandInterface
1376{
1377 public:
1378 ~SetStateEffecter() = default;
1379 SetStateEffecter() = delete;
1380 SetStateEffecter(const SetStateEffecter&) = delete;
1381 SetStateEffecter(SetStateEffecter&&) = default;
1382 SetStateEffecter& operator=(const SetStateEffecter&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -05001383 SetStateEffecter& operator=(SetStateEffecter&&) = delete;
George Liud6649362019-11-27 19:06:51 +08001384
George Liuba4c1fb2020-02-05 14:13:30 +08001385 // compositeEffecterCount(value: 0x01 to 0x08) * stateField(2)
1386 static constexpr auto maxEffecterDataSize = 16;
1387
1388 // compositeEffecterCount(value: 0x01 to 0x08)
1389 static constexpr auto minEffecterCount = 1;
1390 static constexpr auto maxEffecterCount = 8;
George Liud6649362019-11-27 19:06:51 +08001391 explicit SetStateEffecter(const char* type, const char* name,
1392 CLI::App* app) :
1393 CommandInterface(type, name, app)
1394 {
1395 app->add_option(
George Liuba4c1fb2020-02-05 14:13:30 +08001396 "-i, --id", effecterId,
1397 "A handle that is used to identify and access the effecter")
1398 ->required();
1399 app->add_option("-c, --count", effecterCount,
1400 "The number of individual sets of effecter information")
1401 ->required();
1402 app->add_option(
George Liud6649362019-11-27 19:06:51 +08001403 "-d,--data", effecterData,
George Liuba4c1fb2020-02-05 14:13:30 +08001404 "Set effecter state data\n"
1405 "eg: requestSet0 effecterState0 noChange1 dummyState1 ...")
1406 ->required();
George Liud6649362019-11-27 19:06:51 +08001407 }
1408
1409 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1410 {
1411 std::vector<uint8_t> requestMsg(
1412 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES);
1413 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1414
George Liuba4c1fb2020-02-05 14:13:30 +08001415 if (effecterCount > maxEffecterCount ||
1416 effecterCount < minEffecterCount)
George Liud6649362019-11-27 19:06:51 +08001417 {
George Liuba4c1fb2020-02-05 14:13:30 +08001418 std::cerr << "Request Message Error: effecterCount size "
1419 << effecterCount << "is invalid\n";
George Liud6649362019-11-27 19:06:51 +08001420 auto rc = PLDM_ERROR_INVALID_DATA;
1421 return {rc, requestMsg};
1422 }
1423
George Liuba4c1fb2020-02-05 14:13:30 +08001424 if (effecterData.size() > maxEffecterDataSize)
George Liud6649362019-11-27 19:06:51 +08001425 {
George Liuba4c1fb2020-02-05 14:13:30 +08001426 std::cerr << "Request Message Error: effecterData size "
1427 << effecterData.size() << "is invalid\n";
1428 auto rc = PLDM_ERROR_INVALID_DATA;
1429 return {rc, requestMsg};
1430 }
1431
1432 auto stateField = parseEffecterData(effecterData, effecterCount);
1433 if (!stateField)
1434 {
1435 std::cerr << "Failed to parse effecter data, effecterCount size "
1436 << effecterCount << "\n";
George Liud6649362019-11-27 19:06:51 +08001437 auto rc = PLDM_ERROR_INVALID_DATA;
1438 return {rc, requestMsg};
1439 }
1440
1441 auto rc = encode_set_state_effecter_states_req(
Pavithra Barithayaac3c45a2020-03-05 02:28:26 -06001442 instanceId, effecterId, effecterCount, stateField->data(), request);
George Liud6649362019-11-27 19:06:51 +08001443 return {rc, requestMsg};
1444 }
1445
1446 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1447 {
1448 uint8_t completionCode = 0;
1449 auto rc = decode_set_state_effecter_states_resp(
1450 responsePtr, payloadLength, &completionCode);
1451
1452 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1453 {
1454 std::cerr << "Response Message Error: "
George Liuba4c1fb2020-02-05 14:13:30 +08001455 << "rc=" << rc << ",cc=" << (int)completionCode << "\n";
George Liud6649362019-11-27 19:06:51 +08001456 return;
1457 }
1458
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001459 ordered_json data;
1460 data["Response"] = "SUCCESS";
1461 pldmtool::helper::DisplayInJson(data);
George Liud6649362019-11-27 19:06:51 +08001462 }
1463
1464 private:
George Liuba4c1fb2020-02-05 14:13:30 +08001465 uint16_t effecterId;
1466 uint8_t effecterCount;
George Liud6649362019-11-27 19:06:51 +08001467 std::vector<uint8_t> effecterData;
1468};
1469
George Liucc9c20d2020-02-05 10:24:11 +08001470class SetNumericEffecterValue : public CommandInterface
1471{
1472 public:
1473 ~SetNumericEffecterValue() = default;
1474 SetNumericEffecterValue() = delete;
1475 SetNumericEffecterValue(const SetNumericEffecterValue&) = delete;
1476 SetNumericEffecterValue(SetNumericEffecterValue&&) = default;
1477 SetNumericEffecterValue& operator=(const SetNumericEffecterValue&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -05001478 SetNumericEffecterValue& operator=(SetNumericEffecterValue&&) = delete;
George Liucc9c20d2020-02-05 10:24:11 +08001479
1480 explicit SetNumericEffecterValue(const char* type, const char* name,
1481 CLI::App* app) :
1482 CommandInterface(type, name, app)
1483 {
1484 app->add_option(
1485 "-i, --id", effecterId,
1486 "A handle that is used to identify and access the effecter")
1487 ->required();
1488 app->add_option("-s, --size", effecterDataSize,
1489 "The bit width and format of the setting value for the "
1490 "effecter. enum value: {uint8, sint8, uint16, sint16, "
1491 "uint32, sint32}\n")
1492 ->required();
1493 app->add_option("-d,--data", maxEffecterValue,
1494 "The setting value of numeric effecter being "
1495 "requested\n")
1496 ->required();
1497 }
1498
1499 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1500 {
1501 std::vector<uint8_t> requestMsg(
1502 sizeof(pldm_msg_hdr) +
1503 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3);
1504
1505 uint8_t* effecterValue = (uint8_t*)&maxEffecterValue;
1506
1507 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1508 size_t payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES;
1509
1510 if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT16 ||
1511 effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT16)
1512 {
1513 payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1;
1514 }
1515 if (effecterDataSize == PLDM_EFFECTER_DATA_SIZE_UINT32 ||
1516 effecterDataSize == PLDM_EFFECTER_DATA_SIZE_SINT32)
1517 {
1518 payload_length = PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3;
1519 }
1520 auto rc = encode_set_numeric_effecter_value_req(
1521 0, effecterId, effecterDataSize, effecterValue, request,
1522 payload_length);
1523
1524 return {rc, requestMsg};
1525 }
1526
1527 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1528 {
1529 uint8_t completionCode = 0;
1530 auto rc = decode_set_numeric_effecter_value_resp(
1531 responsePtr, payloadLength, &completionCode);
1532
1533 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1534 {
1535 std::cerr << "Response Message Error: "
1536 << "rc=" << rc << ",cc=" << (int)completionCode
1537 << std::endl;
1538 return;
1539 }
1540
Sridevi Ramesh27c512a2020-08-12 03:29:42 -05001541 ordered_json data;
1542 data["Response"] = "SUCCESS";
1543 pldmtool::helper::DisplayInJson(data);
George Liucc9c20d2020-02-05 10:24:11 +08001544 }
1545
1546 private:
1547 uint16_t effecterId;
1548 uint8_t effecterDataSize;
1549 uint64_t maxEffecterValue;
1550};
1551
Sridevi Rameshf31b5042021-01-22 05:42:07 -06001552class GetStateSensorReadings : public CommandInterface
1553{
1554 public:
1555 ~GetStateSensorReadings() = default;
1556 GetStateSensorReadings() = delete;
1557 GetStateSensorReadings(const GetStateSensorReadings&) = delete;
1558 GetStateSensorReadings(GetStateSensorReadings&&) = default;
1559 GetStateSensorReadings& operator=(const GetStateSensorReadings&) = delete;
Pavithra Barithayaa7dbca52023-07-07 04:19:37 -05001560 GetStateSensorReadings& operator=(GetStateSensorReadings&&) = delete;
Sridevi Rameshf31b5042021-01-22 05:42:07 -06001561
1562 explicit GetStateSensorReadings(const char* type, const char* name,
1563 CLI::App* app) :
1564 CommandInterface(type, name, app)
1565 {
1566 app->add_option(
1567 "-i, --sensor_id", sensorId,
1568 "Sensor ID that is used to identify and access the sensor")
1569 ->required();
1570 app->add_option("-r, --rearm", sensorRearm,
1571 "Each bit location in this field corresponds to a "
1572 "particular sensor")
1573 ->required();
1574 }
1575
1576 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1577 {
1578 std::vector<uint8_t> requestMsg(
1579 sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
1580 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1581
1582 uint8_t reserved = 0;
1583 bitfield8_t bf;
1584 bf.byte = sensorRearm;
1585 auto rc = encode_get_state_sensor_readings_req(instanceId, sensorId, bf,
1586 reserved, request);
1587
1588 return {rc, requestMsg};
1589 }
1590
1591 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1592 {
1593 uint8_t completionCode = 0;
1594 uint8_t compSensorCount = 0;
1595 std::array<get_sensor_state_field, 8> stateField{};
1596 auto rc = decode_get_state_sensor_readings_resp(
1597 responsePtr, payloadLength, &completionCode, &compSensorCount,
1598 stateField.data());
1599
1600 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1601 {
1602 std::cerr << "Response Message Error: "
1603 << "rc=" << rc << ",cc=" << (int)completionCode
1604 << std::endl;
1605 return;
1606 }
1607 ordered_json output;
1608 output["compositeSensorCount"] = (int)compSensorCount;
1609
1610 for (size_t i = 0; i < compSensorCount; i++)
1611 {
George Liubd5e2ea2021-04-22 20:33:06 +08001612 if (sensorOpState.contains(stateField[i].sensor_op_state))
1613 {
1614 output.emplace(("sensorOpState[" + std::to_string(i) + "]"),
1615 sensorOpState.at(stateField[i].sensor_op_state));
1616 }
Sridevi Rameshf31b5042021-01-22 05:42:07 -06001617
George Liubd5e2ea2021-04-22 20:33:06 +08001618 if (sensorPresState.contains(stateField[i].present_state))
1619 {
1620 output.emplace(("presentState[" + std::to_string(i) + "]"),
1621 sensorPresState.at(stateField[i].present_state));
1622 }
1623
1624 if (sensorPresState.contains(stateField[i].previous_state))
1625 {
1626 output.emplace(
1627 ("previousState[" + std::to_string(i) + "]"),
1628 sensorPresState.at(stateField[i].previous_state));
1629 }
1630
1631 if (sensorPresState.contains(stateField[i].event_state))
1632 {
1633 output.emplace(("eventState[" + std::to_string(i) + "]"),
1634 sensorPresState.at(stateField[i].event_state));
1635 }
Sridevi Rameshf31b5042021-01-22 05:42:07 -06001636 }
1637
1638 pldmtool::helper::DisplayInJson(output);
1639 }
1640
1641 private:
1642 uint16_t sensorId;
1643 uint8_t sensorRearm;
1644};
1645
Pavithra Barithaya8c15e002023-10-17 04:14:21 -05001646class GetNumericEffecterValue : public CommandInterface
1647{
1648 public:
1649 ~GetNumericEffecterValue() = default;
1650 GetNumericEffecterValue() = delete;
1651 GetNumericEffecterValue(const GetNumericEffecterValue&) = delete;
1652 GetNumericEffecterValue(GetNumericEffecterValue&&) = default;
1653 GetNumericEffecterValue& operator=(const GetNumericEffecterValue&) = delete;
Pavithra Barithayaae088dc2023-11-09 01:14:05 -06001654 GetNumericEffecterValue& operator=(GetNumericEffecterValue&&) = delete;
Pavithra Barithaya8c15e002023-10-17 04:14:21 -05001655
1656 explicit GetNumericEffecterValue(const char* type, const char* name,
1657 CLI::App* app) :
1658 CommandInterface(type, name, app)
1659 {
1660 app->add_option(
1661 "-i, --effecter_id", effecterId,
1662 "A handle that is used to identify and access the effecter")
1663 ->required();
1664 }
1665
1666 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
1667 {
1668 std::vector<uint8_t> requestMsg(
1669 sizeof(pldm_msg_hdr) + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
1670 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1671
1672 auto rc = encode_get_numeric_effecter_value_req(instanceId, effecterId,
1673 request);
1674
1675 return {rc, requestMsg};
1676 }
1677
1678 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
1679 {
1680 uint8_t completionCode = 0;
1681 uint8_t effecterDataSize = 0;
1682 uint8_t effecterOperationalState = 0;
1683 std::array<uint8_t, sizeof(uint32_t)>
1684 pendingValue{}; // maximum size for the pending Value is uint32
1685 // according to spec DSP0248
1686 std::array<uint8_t, sizeof(uint32_t)>
1687 presentValue{}; // maximum size for the present Value is uint32
1688 // according to spec DSP0248
1689
1690 auto rc = decode_get_numeric_effecter_value_resp(
1691 responsePtr, payloadLength, &completionCode, &effecterDataSize,
1692 &effecterOperationalState, pendingValue.data(),
1693 presentValue.data());
1694
1695 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
1696 {
1697 std::cerr << "Response Message Error: "
1698 << "rc=" << rc
1699 << ",cc=" << static_cast<int>(completionCode)
1700 << std::endl;
1701 return;
1702 }
1703
1704 ordered_json output;
1705 output["effecterDataSize"] = static_cast<int>(effecterDataSize);
1706 output["effecterOperationalState"] =
1707 getOpState(effecterOperationalState);
1708
1709 switch (effecterDataSize)
1710 {
1711 case PLDM_EFFECTER_DATA_SIZE_UINT8:
1712 {
1713 output["pendingValue"] =
1714 *(reinterpret_cast<uint8_t*>(pendingValue.data()));
1715 output["presentValue"] =
1716 *(reinterpret_cast<uint8_t*>(presentValue.data()));
1717 break;
1718 }
1719 case PLDM_EFFECTER_DATA_SIZE_SINT8:
1720 {
1721 output["pendingValue"] =
1722 *(reinterpret_cast<int8_t*>(pendingValue.data()));
1723 output["presentValue"] =
1724 *(reinterpret_cast<int8_t*>(presentValue.data()));
1725 break;
1726 }
1727 case PLDM_EFFECTER_DATA_SIZE_UINT16:
1728 {
1729 output["pendingValue"] =
1730 *(reinterpret_cast<uint16_t*>(pendingValue.data()));
1731 output["presentValue"] =
1732 *(reinterpret_cast<uint16_t*>(presentValue.data()));
1733 break;
1734 }
1735 case PLDM_EFFECTER_DATA_SIZE_SINT16:
1736 {
1737 output["pendingValue"] =
1738 *(reinterpret_cast<int16_t*>(pendingValue.data()));
1739 output["presentValue"] =
1740 *(reinterpret_cast<int16_t*>(presentValue.data()));
1741 break;
1742 }
1743 case PLDM_EFFECTER_DATA_SIZE_UINT32:
1744 {
1745 output["pendingValue"] =
1746 *(reinterpret_cast<uint32_t*>(pendingValue.data()));
1747 output["presentValue"] =
1748 *(reinterpret_cast<uint32_t*>(presentValue.data()));
1749 break;
1750 }
1751 case PLDM_EFFECTER_DATA_SIZE_SINT32:
1752 {
1753 output["pendingValue"] =
1754 *(reinterpret_cast<int32_t*>(pendingValue.data()));
1755 output["presentValue"] =
1756 *(reinterpret_cast<int32_t*>(presentValue.data()));
1757 break;
1758 }
1759 default:
1760 {
1761 std::cerr << "Unknown Effecter Data Size : "
1762 << static_cast<int>(effecterDataSize) << std::endl;
1763 break;
1764 }
1765 }
1766
1767 pldmtool::helper::DisplayInJson(output);
1768 }
1769
1770 private:
1771 uint16_t effecterId;
1772
1773 static inline const std::map<uint8_t, std::string> numericEffecterOpState{
1774 {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING,
1775 "Effecter Enabled Update Pending"},
1776 {EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING,
1777 "Effecter Enabled No Update Pending"},
1778 {EFFECTER_OPER_STATE_DISABLED, "Effecter Disabled"},
1779 {EFFECTER_OPER_STATE_UNAVAILABLE, "Effecter Unavailable"},
1780 {EFFECTER_OPER_STATE_STATUSUNKNOWN, "Effecter Status Unknown"},
1781 {EFFECTER_OPER_STATE_FAILED, "Effecter Failed"},
1782 {EFFECTER_OPER_STATE_INITIALIZING, "Effecter Initializing"},
1783 {EFFECTER_OPER_STATE_SHUTTINGDOWN, "Effecter Shutting Down"},
1784 {EFFECTER_OPER_STATE_INTEST, "Effecter In Test"}};
1785
1786 std::string getOpState(uint8_t state)
1787 {
1788 auto typeString = std::to_string(state);
1789 try
1790 {
1791 return numericEffecterOpState.at(state);
1792 }
1793 catch (const std::out_of_range& e)
1794 {
1795 return typeString;
1796 }
1797 }
1798};
1799
George Liud6649362019-11-27 19:06:51 +08001800void registerCommand(CLI::App& app)
1801{
1802 auto platform = app.add_subcommand("platform", "platform type command");
1803 platform->require_subcommand(1);
1804
Patrick Williams6da4f912023-05-10 07:50:53 -05001805 auto getPDR = platform->add_subcommand("GetPDR",
1806 "get platform descriptor records");
George Liud6649362019-11-27 19:06:51 +08001807 commands.push_back(std::make_unique<GetPDR>("platform", "getPDR", getPDR));
1808
1809 auto setStateEffecterStates = platform->add_subcommand(
1810 "SetStateEffecterStates", "set effecter states");
1811 commands.push_back(std::make_unique<SetStateEffecter>(
1812 "platform", "setStateEffecterStates", setStateEffecterStates));
George Liucc9c20d2020-02-05 10:24:11 +08001813
1814 auto setNumericEffecterValue = platform->add_subcommand(
1815 "SetNumericEffecterValue", "set the value for a PLDM Numeric Effecter");
1816 commands.push_back(std::make_unique<SetNumericEffecterValue>(
1817 "platform", "setNumericEffecterValue", setNumericEffecterValue));
Sridevi Rameshf31b5042021-01-22 05:42:07 -06001818
1819 auto getStateSensorReadings = platform->add_subcommand(
1820 "GetStateSensorReadings", "get the state sensor readings");
1821 commands.push_back(std::make_unique<GetStateSensorReadings>(
1822 "platform", "getStateSensorReadings", getStateSensorReadings));
Pavithra Barithaya8c15e002023-10-17 04:14:21 -05001823
1824 auto getNumericEffecterValue = platform->add_subcommand(
1825 "GetNumericEffecterValue", "get the numeric effecter value");
1826 commands.push_back(std::make_unique<GetNumericEffecterValue>(
1827 "platform", "getNumericEffecterValue", getNumericEffecterValue));
George Liud6649362019-11-27 19:06:51 +08001828}
1829
Pavithra Barithayaa340eed2023-02-14 03:32:47 -06001830void parseGetPDROption()
1831{
1832 for (const auto& command : commands)
1833 {
1834 if (command.get()->getPLDMType() == "platform" &&
1835 command.get()->getCommandName() == "getPDR")
1836 {
1837 auto getPDR = dynamic_cast<GetPDR*>(command.get());
1838 getPDR->parseGetPDROptions();
1839 }
1840 }
1841}
1842
George Liud6649362019-11-27 19:06:51 +08001843} // namespace platform
1844} // namespace pldmtool