George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 3 | #include <libpldm/pdr.h> |
| 4 | #include <libpldm/pldm_types.h> |
Gilbert Chen | 6c7fed4 | 2022-02-22 15:40:17 +0000 | [diff] [blame] | 5 | #include <linux/mctp.h> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 6 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 7 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Logging/Create/client.hpp> |
| 10 | #include <xyz/openbmc_project/ObjectMapper/client.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 11 | |
Tom Joseph | 5492207 | 2021-06-19 02:45:46 -0700 | [diff] [blame] | 12 | #include <algorithm> |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 13 | #include <array> |
Tom Joseph | 5492207 | 2021-06-19 02:45:46 -0700 | [diff] [blame] | 14 | #include <cctype> |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 15 | #include <ctime> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 16 | #include <fstream> |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <map> |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 19 | #include <stdexcept> |
| 20 | #include <string> |
| 21 | #include <vector> |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 22 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 23 | PHOSPHOR_LOG2_USING; |
| 24 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 25 | namespace pldm |
| 26 | { |
| 27 | namespace utils |
| 28 | { |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 29 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 30 | std::vector<std::vector<uint8_t>> |
| 31 | findStateEffecterPDR(uint8_t /*tid*/, uint16_t entityID, |
| 32 | uint16_t stateSetId, const pldm_pdr* repo) |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 33 | { |
| 34 | uint8_t* outData = nullptr; |
| 35 | uint32_t size{}; |
| 36 | const pldm_pdr_record* record{}; |
| 37 | std::vector<std::vector<uint8_t>> pdrs; |
| 38 | try |
| 39 | { |
| 40 | do |
| 41 | { |
| 42 | record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_EFFECTER_PDR, |
| 43 | record, &outData, &size); |
| 44 | if (record) |
| 45 | { |
| 46 | auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(outData); |
| 47 | auto compositeEffecterCount = pdr->composite_effecter_count; |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 48 | auto possible_states_start = pdr->possible_states; |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 49 | |
| 50 | for (auto effecters = 0x00; effecters < compositeEffecterCount; |
| 51 | effecters++) |
| 52 | { |
| 53 | auto possibleStates = |
| 54 | reinterpret_cast<state_effecter_possible_states*>( |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 55 | possible_states_start); |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 56 | auto setId = possibleStates->state_set_id; |
| 57 | auto possibleStateSize = |
| 58 | possibleStates->possible_states_size; |
| 59 | |
| 60 | if (pdr->entity_type == entityID && setId == stateSetId) |
| 61 | { |
| 62 | std::vector<uint8_t> effecter_pdr(&outData[0], |
| 63 | &outData[size]); |
| 64 | pdrs.emplace_back(std::move(effecter_pdr)); |
| 65 | break; |
| 66 | } |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 67 | possible_states_start += possibleStateSize + sizeof(setId) + |
| 68 | sizeof(possibleStateSize); |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | } while (record); |
| 73 | } |
| 74 | catch (const std::exception& e) |
| 75 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 76 | error("Failed to obtain a record, error - {ERROR}", "ERROR", e); |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return pdrs; |
| 80 | } |
| 81 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 82 | std::vector<std::vector<uint8_t>> |
| 83 | findStateSensorPDR(uint8_t /*tid*/, uint16_t entityID, uint16_t stateSetId, |
| 84 | const pldm_pdr* repo) |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 85 | { |
| 86 | uint8_t* outData = nullptr; |
| 87 | uint32_t size{}; |
| 88 | const pldm_pdr_record* record{}; |
| 89 | std::vector<std::vector<uint8_t>> pdrs; |
| 90 | try |
| 91 | { |
| 92 | do |
| 93 | { |
| 94 | record = pldm_pdr_find_record_by_type(repo, PLDM_STATE_SENSOR_PDR, |
| 95 | record, &outData, &size); |
| 96 | if (record) |
| 97 | { |
| 98 | auto pdr = reinterpret_cast<pldm_state_sensor_pdr*>(outData); |
| 99 | auto compositeSensorCount = pdr->composite_sensor_count; |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 100 | auto possible_states_start = pdr->possible_states; |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 101 | |
| 102 | for (auto sensors = 0x00; sensors < compositeSensorCount; |
| 103 | sensors++) |
| 104 | { |
| 105 | auto possibleStates = |
| 106 | reinterpret_cast<state_sensor_possible_states*>( |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 107 | possible_states_start); |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 108 | auto setId = possibleStates->state_set_id; |
| 109 | auto possibleStateSize = |
| 110 | possibleStates->possible_states_size; |
| 111 | |
| 112 | if (pdr->entity_type == entityID && setId == stateSetId) |
| 113 | { |
| 114 | std::vector<uint8_t> sensor_pdr(&outData[0], |
| 115 | &outData[size]); |
| 116 | pdrs.emplace_back(std::move(sensor_pdr)); |
| 117 | break; |
| 118 | } |
Chicago Duan | a7aacc3 | 2020-06-10 18:03:38 +0800 | [diff] [blame] | 119 | possible_states_start += possibleStateSize + sizeof(setId) + |
| 120 | sizeof(possibleStateSize); |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | } while (record); |
| 125 | } |
| 126 | catch (const std::exception& e) |
| 127 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 128 | error( |
| 129 | "Failed to obtain a record with entity ID '{ENTITYID}', error - {ERROR}", |
| 130 | "ENTITYID", entityID, "ERROR", e); |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return pdrs; |
| 134 | } |
| 135 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 136 | uint8_t readHostEID() |
| 137 | { |
| 138 | uint8_t eid{}; |
Brad Bishop | 06052cc | 2021-08-16 15:17:16 -0400 | [diff] [blame] | 139 | std::ifstream eidFile{HOST_EID_PATH}; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 140 | if (!eidFile.good()) |
| 141 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 142 | error("Failed to open remote terminus EID file at path '{PATH}'", |
| 143 | "PATH", static_cast<std::string>(HOST_EID_PATH)); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
| 147 | std::string eidStr; |
| 148 | eidFile >> eidStr; |
| 149 | if (!eidStr.empty()) |
| 150 | { |
| 151 | eid = atoi(eidStr.c_str()); |
| 152 | } |
| 153 | else |
| 154 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 155 | error("Remote terminus EID file was empty"); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | return eid; |
| 160 | } |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 161 | |
Gilbert Chen | 6c7fed4 | 2022-02-22 15:40:17 +0000 | [diff] [blame] | 162 | bool isValidEID(eid mctpEid) |
| 163 | { |
| 164 | if (mctpEid == MCTP_ADDR_NULL || mctpEid < MCTP_START_VALID_EID || |
| 165 | mctpEid == MCTP_ADDR_ANY) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 173 | uint8_t getNumPadBytes(uint32_t data) |
| 174 | { |
| 175 | uint8_t pad; |
| 176 | pad = ((data % 4) ? (4 - data % 4) : 0); |
| 177 | return pad; |
| 178 | } // end getNumPadBytes |
| 179 | |
| 180 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 181 | uint8_t* hour, uint8_t* min, uint8_t* sec) |
| 182 | { |
| 183 | constexpr uint64_t max_data = 29991231115959; |
| 184 | constexpr uint64_t min_data = 19700101000000; |
| 185 | if (data < min_data || data > max_data) |
| 186 | { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | *year = data / 10000000000; |
| 191 | data = data % 10000000000; |
| 192 | *month = data / 100000000; |
| 193 | data = data % 100000000; |
| 194 | *day = data / 1000000; |
| 195 | data = data % 1000000; |
| 196 | *hour = data / 10000; |
| 197 | data = data % 10000; |
| 198 | *min = data / 100; |
| 199 | *sec = data % 100; |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 204 | std::optional<std::vector<set_effecter_state_field>> parseEffecterData( |
| 205 | const std::vector<uint8_t>& effecterData, uint8_t effecterCount) |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 206 | { |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 207 | std::vector<set_effecter_state_field> stateField; |
| 208 | |
| 209 | if (effecterData.size() != effecterCount * 2) |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 210 | { |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 211 | return std::nullopt; |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 212 | } |
| 213 | |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 214 | for (uint8_t i = 0; i < effecterCount; ++i) |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 215 | { |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 216 | uint8_t set_request = effecterData[i * 2] == PLDM_REQUEST_SET |
| 217 | ? PLDM_REQUEST_SET |
| 218 | : PLDM_NO_CHANGE; |
| 219 | set_effecter_state_field filed{set_request, effecterData[i * 2 + 1]}; |
| 220 | stateField.emplace_back(std::move(filed)); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 221 | } |
| 222 | |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 223 | return std::make_optional(std::move(stateField)); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 224 | } |
| 225 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 226 | std::string DBusHandler::getService(const char* path, |
| 227 | const char* interface) const |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 228 | { |
| 229 | using DbusInterfaceList = std::vector<std::string>; |
| 230 | std::map<std::string, std::vector<std::string>> mapperResponse; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 231 | auto& bus = DBusHandler::getBus(); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 232 | |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 233 | auto mapper = bus.new_method_call(ObjectMapper::default_service, |
| 234 | ObjectMapper::instance_path, |
| 235 | ObjectMapper::interface, "GetObject"); |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 236 | |
| 237 | if (interface) |
| 238 | { |
| 239 | mapper.append(path, DbusInterfaceList({interface})); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | mapper.append(path, DbusInterfaceList({})); |
| 244 | } |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 245 | |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 246 | auto mapperResponseMsg = bus.call(mapper, dbusTimeout); |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 247 | mapperResponseMsg.read(mapperResponse); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 248 | return mapperResponse.begin()->first; |
| 249 | } |
| 250 | |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 251 | GetSubTreeResponse |
| 252 | DBusHandler::getSubtree(const std::string& searchPath, int depth, |
| 253 | const std::vector<std::string>& ifaceList) const |
| 254 | { |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 255 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 256 | auto method = bus.new_method_call(ObjectMapper::default_service, |
| 257 | ObjectMapper::instance_path, |
| 258 | ObjectMapper::interface, "GetSubTree"); |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 259 | method.append(searchPath, depth, ifaceList); |
vkaverap@in.ibm.com | 91a092f | 2023-09-18 23:39:44 -0500 | [diff] [blame] | 260 | auto reply = bus.call(method, dbusTimeout); |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 261 | GetSubTreeResponse response; |
| 262 | reply.read(response); |
| 263 | return response; |
| 264 | } |
| 265 | |
Pavithra Barithaya | 2ec8269 | 2024-04-29 06:31:10 -0500 | [diff] [blame] | 266 | GetSubTreePathsResponse DBusHandler::getSubTreePaths( |
| 267 | const std::string& objectPath, int depth, |
| 268 | const std::vector<std::string>& ifaceList) const |
| 269 | { |
| 270 | std::vector<std::string> paths; |
| 271 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 272 | auto method = bus.new_method_call( |
| 273 | ObjectMapper::default_service, ObjectMapper::instance_path, |
| 274 | ObjectMapper::interface, "GetSubTreePaths"); |
| 275 | method.append(objectPath, depth, ifaceList); |
| 276 | auto reply = bus.call(method, dbusTimeout); |
| 277 | |
| 278 | reply.read(paths); |
| 279 | return paths; |
| 280 | } |
| 281 | |
Manojkiran Eda | 92fb0b5 | 2024-04-17 10:48:17 +0530 | [diff] [blame] | 282 | void reportError(const char* errorMsg) |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 283 | { |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 284 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 285 | using LoggingCreate = |
| 286 | sdbusplus::client::xyz::openbmc_project::logging::Create<>; |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 287 | try |
| 288 | { |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 289 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
Manojkiran Eda | 92fb0b5 | 2024-04-17 10:48:17 +0530 | [diff] [blame] | 290 | auto severity = |
| 291 | sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage( |
| 292 | sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level:: |
| 293 | Error); |
Pavithra Barithaya | 7b4d59a | 2024-02-05 09:09:30 -0600 | [diff] [blame] | 294 | auto method = bus.new_method_call(LoggingCreate::default_service, |
| 295 | LoggingCreate::instance_path, |
| 296 | LoggingCreate::interface, "Create"); |
| 297 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 298 | std::map<std::string, std::string> addlData{}; |
| 299 | method.append(errorMsg, severity, addlData); |
vkaverap@in.ibm.com | 5b71b86 | 2023-08-21 05:19:04 +0000 | [diff] [blame] | 300 | bus.call_noreply(method, dbusTimeout); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 301 | } |
| 302 | catch (const std::exception& e) |
| 303 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 304 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 305 | "Failed to do dbus call for creating error log for '{ERRMSG}' at path '{PATH}' and interface '{INTERFACE}', error - {ERROR}", |
| 306 | "ERRMSG", errorMsg, "PATH", LoggingCreate::instance_path, |
| 307 | "INTERFACE", LoggingCreate::interface, "ERROR", e); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 311 | void DBusHandler::setDbusProperty(const DBusMapping& dBusMap, |
| 312 | const PropertyValue& value) const |
| 313 | { |
| 314 | auto setDbusValue = [&dBusMap, this](const auto& variant) { |
| 315 | auto& bus = getBus(); |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 316 | auto service = |
| 317 | getService(dBusMap.objectPath.c_str(), dBusMap.interface.c_str()); |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 318 | auto method = bus.new_method_call( |
| 319 | service.c_str(), dBusMap.objectPath.c_str(), dbusProperties, "Set"); |
| 320 | method.append(dBusMap.interface.c_str(), dBusMap.propertyName.c_str(), |
| 321 | variant); |
vkaverap@in.ibm.com | 5b71b86 | 2023-08-21 05:19:04 +0000 | [diff] [blame] | 322 | bus.call_noreply(method, dbusTimeout); |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | if (dBusMap.propertyType == "uint8_t") |
| 326 | { |
| 327 | std::variant<uint8_t> v = std::get<uint8_t>(value); |
| 328 | setDbusValue(v); |
| 329 | } |
Deepak Kodihalli | fd279e1 | 2020-02-02 05:20:43 -0600 | [diff] [blame] | 330 | else if (dBusMap.propertyType == "bool") |
| 331 | { |
| 332 | std::variant<bool> v = std::get<bool>(value); |
| 333 | setDbusValue(v); |
| 334 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 335 | else if (dBusMap.propertyType == "int16_t") |
| 336 | { |
| 337 | std::variant<int16_t> v = std::get<int16_t>(value); |
| 338 | setDbusValue(v); |
| 339 | } |
| 340 | else if (dBusMap.propertyType == "uint16_t") |
| 341 | { |
| 342 | std::variant<uint16_t> v = std::get<uint16_t>(value); |
| 343 | setDbusValue(v); |
| 344 | } |
| 345 | else if (dBusMap.propertyType == "int32_t") |
| 346 | { |
| 347 | std::variant<int32_t> v = std::get<int32_t>(value); |
| 348 | setDbusValue(v); |
| 349 | } |
| 350 | else if (dBusMap.propertyType == "uint32_t") |
| 351 | { |
| 352 | std::variant<uint32_t> v = std::get<uint32_t>(value); |
| 353 | setDbusValue(v); |
| 354 | } |
| 355 | else if (dBusMap.propertyType == "int64_t") |
| 356 | { |
| 357 | std::variant<int64_t> v = std::get<int64_t>(value); |
| 358 | setDbusValue(v); |
| 359 | } |
| 360 | else if (dBusMap.propertyType == "uint64_t") |
| 361 | { |
| 362 | std::variant<uint64_t> v = std::get<uint64_t>(value); |
| 363 | setDbusValue(v); |
| 364 | } |
| 365 | else if (dBusMap.propertyType == "double") |
| 366 | { |
| 367 | std::variant<double> v = std::get<double>(value); |
| 368 | setDbusValue(v); |
| 369 | } |
| 370 | else if (dBusMap.propertyType == "string") |
| 371 | { |
| 372 | std::variant<std::string> v = std::get<std::string>(value); |
| 373 | setDbusValue(v); |
| 374 | } |
| 375 | else |
| 376 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 377 | error("Unsupported property type '{TYPE}'", "TYPE", |
| 378 | dBusMap.propertyType); |
| 379 | throw std::invalid_argument("UnSupported Dbus Type"); |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 383 | PropertyValue DBusHandler::getDbusPropertyVariant( |
| 384 | const char* objPath, const char* dbusProp, const char* dbusInterface) const |
| 385 | { |
| 386 | auto& bus = DBusHandler::getBus(); |
| 387 | auto service = getService(objPath, dbusInterface); |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 388 | auto method = |
| 389 | bus.new_method_call(service.c_str(), objPath, dbusProperties, "Get"); |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 390 | method.append(dbusInterface, dbusProp); |
Patrick Williams | 75b8f46 | 2024-02-07 10:59:26 -0600 | [diff] [blame] | 391 | return bus.call(method, dbusTimeout).unpack<PropertyValue>(); |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 392 | } |
| 393 | |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 394 | ObjectValueTree DBusHandler::getManagedObj(const char* service, |
| 395 | const char* rootPath) |
| 396 | { |
| 397 | auto& bus = DBusHandler::getBus(); |
| 398 | auto method = bus.new_method_call(service, rootPath, |
| 399 | "org.freedesktop.DBus.ObjectManager", |
| 400 | "GetManagedObjects"); |
| 401 | return bus.call(method).unpack<ObjectValueTree>(); |
| 402 | } |
| 403 | |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 404 | PropertyMap DBusHandler::getDbusPropertiesVariant( |
| 405 | const char* serviceName, const char* objPath, |
| 406 | const char* dbusInterface) const |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 407 | { |
| 408 | auto& bus = DBusHandler::getBus(); |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 409 | auto method = |
| 410 | bus.new_method_call(serviceName, objPath, dbusProperties, "GetAll"); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 411 | method.append(dbusInterface); |
| 412 | return bus.call(method, dbusTimeout).unpack<PropertyMap>(); |
| 413 | } |
| 414 | |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 415 | PropertyValue jsonEntryToDbusVal(std::string_view type, |
| 416 | const nlohmann::json& value) |
| 417 | { |
| 418 | PropertyValue propValue{}; |
| 419 | if (type == "uint8_t") |
| 420 | { |
| 421 | propValue = static_cast<uint8_t>(value); |
| 422 | } |
| 423 | else if (type == "uint16_t") |
| 424 | { |
| 425 | propValue = static_cast<uint16_t>(value); |
| 426 | } |
| 427 | else if (type == "uint32_t") |
| 428 | { |
| 429 | propValue = static_cast<uint32_t>(value); |
| 430 | } |
| 431 | else if (type == "uint64_t") |
| 432 | { |
| 433 | propValue = static_cast<uint64_t>(value); |
| 434 | } |
| 435 | else if (type == "int16_t") |
| 436 | { |
| 437 | propValue = static_cast<int16_t>(value); |
| 438 | } |
| 439 | else if (type == "int32_t") |
| 440 | { |
| 441 | propValue = static_cast<int32_t>(value); |
| 442 | } |
| 443 | else if (type == "int64_t") |
| 444 | { |
| 445 | propValue = static_cast<int64_t>(value); |
| 446 | } |
| 447 | else if (type == "bool") |
| 448 | { |
| 449 | propValue = static_cast<bool>(value); |
| 450 | } |
| 451 | else if (type == "double") |
| 452 | { |
| 453 | propValue = static_cast<double>(value); |
| 454 | } |
| 455 | else if (type == "string") |
| 456 | { |
| 457 | propValue = static_cast<std::string>(value); |
| 458 | } |
| 459 | else |
| 460 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 461 | error("Unknown D-Bus property type '{TYPE}'", "TYPE", type); |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return propValue; |
| 465 | } |
| 466 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 467 | uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType, |
| 468 | uint16_t entityInstance, uint16_t containerId, |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 469 | uint16_t stateSetId, bool localOrRemote) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 470 | { |
| 471 | uint8_t* pdrData = nullptr; |
| 472 | uint32_t pdrSize{}; |
| 473 | const pldm_pdr_record* record{}; |
| 474 | do |
| 475 | { |
| 476 | record = pldm_pdr_find_record_by_type(pdrRepo, PLDM_STATE_EFFECTER_PDR, |
| 477 | record, &pdrData, &pdrSize); |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 478 | if (record && (localOrRemote ^ pldm_pdr_record_is_remote(record))) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 479 | { |
| 480 | auto pdr = reinterpret_cast<pldm_state_effecter_pdr*>(pdrData); |
| 481 | auto compositeEffecterCount = pdr->composite_effecter_count; |
| 482 | auto possible_states_start = pdr->possible_states; |
| 483 | |
| 484 | for (auto effecters = 0x00; effecters < compositeEffecterCount; |
| 485 | effecters++) |
| 486 | { |
| 487 | auto possibleStates = |
| 488 | reinterpret_cast<state_effecter_possible_states*>( |
| 489 | possible_states_start); |
| 490 | auto setId = possibleStates->state_set_id; |
| 491 | auto possibleStateSize = possibleStates->possible_states_size; |
| 492 | |
| 493 | if (entityType == pdr->entity_type && |
| 494 | entityInstance == pdr->entity_instance && |
| 495 | containerId == pdr->container_id && stateSetId == setId) |
| 496 | { |
| 497 | return pdr->effecter_id; |
| 498 | } |
| 499 | possible_states_start += possibleStateSize + sizeof(setId) + |
| 500 | sizeof(possibleStateSize); |
| 501 | } |
| 502 | } |
| 503 | } while (record); |
| 504 | |
| 505 | return PLDM_INVALID_EFFECTER_ID; |
| 506 | } |
| 507 | |
Chicago Duan | fe4d88b | 2020-06-12 16:44:13 +0800 | [diff] [blame] | 508 | int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId, |
| 509 | uint8_t sensorOffset, uint8_t eventState, |
| 510 | uint8_t previousEventState) |
| 511 | { |
| 512 | try |
| 513 | { |
| 514 | auto& bus = DBusHandler::getBus(); |
| 515 | auto msg = bus.new_signal("/xyz/openbmc_project/pldm", |
| 516 | "xyz.openbmc_project.PLDM.Event", |
| 517 | "StateSensorEvent"); |
| 518 | msg.append(tid, sensorId, sensorOffset, eventState, previousEventState); |
| 519 | |
| 520 | msg.signal_send(); |
| 521 | } |
Patrick Williams | 5133058 | 2021-10-06 12:48:56 -0500 | [diff] [blame] | 522 | catch (const std::exception& e) |
Chicago Duan | fe4d88b | 2020-06-12 16:44:13 +0800 | [diff] [blame] | 523 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 524 | error("Failed to emit pldm event signal, error - {ERROR}", "ERROR", e); |
Chicago Duan | fe4d88b | 2020-06-12 16:44:13 +0800 | [diff] [blame] | 525 | return PLDM_ERROR; |
| 526 | } |
| 527 | |
| 528 | return PLDM_SUCCESS; |
| 529 | } |
| 530 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 531 | uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid, |
| 532 | uint16_t entityType, uint16_t entityInstance, |
| 533 | uint16_t containerId, uint16_t stateSetId) |
| 534 | { |
| 535 | auto pdrs = findStateSensorPDR(tid, entityType, stateSetId, pdrRepo); |
| 536 | for (auto pdr : pdrs) |
| 537 | { |
| 538 | auto sensorPdr = reinterpret_cast<pldm_state_sensor_pdr*>(pdr.data()); |
| 539 | auto compositeSensorCount = sensorPdr->composite_sensor_count; |
| 540 | auto possible_states_start = sensorPdr->possible_states; |
| 541 | |
| 542 | for (auto sensors = 0x00; sensors < compositeSensorCount; sensors++) |
| 543 | { |
| 544 | auto possibleStates = |
| 545 | reinterpret_cast<state_sensor_possible_states*>( |
| 546 | possible_states_start); |
| 547 | auto setId = possibleStates->state_set_id; |
| 548 | auto possibleStateSize = possibleStates->possible_states_size; |
| 549 | if (entityType == sensorPdr->entity_type && |
| 550 | entityInstance == sensorPdr->entity_instance && |
| 551 | stateSetId == setId && containerId == sensorPdr->container_id) |
| 552 | { |
| 553 | return sensorPdr->sensor_id; |
| 554 | } |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 555 | possible_states_start += |
| 556 | possibleStateSize + sizeof(setId) + sizeof(possibleStateSize); |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | return PLDM_INVALID_EFFECTER_ID; |
| 560 | } |
| 561 | |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 562 | void printBuffer(bool isTx, const std::vector<uint8_t>& buffer) |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 563 | { |
Manojkiran Eda | cd4cd45 | 2024-04-23 08:53:17 +0530 | [diff] [blame] | 564 | if (buffer.empty()) |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 565 | { |
Manojkiran Eda | cd4cd45 | 2024-04-23 08:53:17 +0530 | [diff] [blame] | 566 | return; |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 567 | } |
Manojkiran Eda | cd4cd45 | 2024-04-23 08:53:17 +0530 | [diff] [blame] | 568 | |
| 569 | std::cout << (isTx ? "Tx: " : "Rx: "); |
| 570 | |
| 571 | std::ranges::for_each(buffer, [](uint8_t byte) { |
| 572 | std::cout << std::format("{:02x} ", byte); |
| 573 | }); |
| 574 | |
| 575 | std::cout << std::endl; |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 576 | } |
| 577 | |
Tom Joseph | 5492207 | 2021-06-19 02:45:46 -0700 | [diff] [blame] | 578 | std::string toString(const struct variable_field& var) |
| 579 | { |
| 580 | if (var.ptr == nullptr || !var.length) |
| 581 | { |
| 582 | return ""; |
| 583 | } |
| 584 | |
| 585 | std::string str(reinterpret_cast<const char*>(var.ptr), var.length); |
| 586 | std::replace_if( |
| 587 | str.begin(), str.end(), [](const char& c) { return !isprint(c); }, ' '); |
| 588 | return str; |
| 589 | } |
| 590 | |
George Liu | 872f0f6 | 2021-11-25 16:26:16 +0800 | [diff] [blame] | 591 | std::vector<std::string> split(std::string_view srcStr, std::string_view delim, |
| 592 | std::string_view trimStr) |
| 593 | { |
| 594 | std::vector<std::string> out; |
| 595 | size_t start; |
| 596 | size_t end = 0; |
| 597 | |
| 598 | while ((start = srcStr.find_first_not_of(delim, end)) != std::string::npos) |
| 599 | { |
| 600 | end = srcStr.find(delim, start); |
| 601 | std::string_view dstStr = srcStr.substr(start, end - start); |
| 602 | if (!trimStr.empty()) |
| 603 | { |
| 604 | dstStr.remove_prefix(dstStr.find_first_not_of(trimStr)); |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 605 | dstStr.remove_suffix( |
| 606 | dstStr.size() - 1 - dstStr.find_last_not_of(trimStr)); |
George Liu | 872f0f6 | 2021-11-25 16:26:16 +0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | if (!dstStr.empty()) |
| 610 | { |
| 611 | out.push_back(std::string(dstStr)); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return out; |
| 616 | } |
| 617 | |
Manojkiran Eda | ef77305 | 2021-07-29 09:29:28 +0530 | [diff] [blame] | 618 | std::string getCurrentSystemTime() |
| 619 | { |
Manojkiran Eda | 09a8982 | 2024-04-24 11:02:44 +0530 | [diff] [blame] | 620 | const auto zonedTime{std::chrono::zoned_time{ |
| 621 | std::chrono::current_zone(), std::chrono::system_clock::now()}}; |
| 622 | return std::format("{:%F %Z %T}", zonedTime); |
Manojkiran Eda | ef77305 | 2021-07-29 09:29:28 +0530 | [diff] [blame] | 623 | } |
| 624 | |
Sridevi Ramesh | eefe49b | 2022-06-27 11:51:02 -0500 | [diff] [blame] | 625 | bool checkForFruPresence(const std::string& objPath) |
| 626 | { |
| 627 | bool isPresent = false; |
| 628 | static constexpr auto presentInterface = |
| 629 | "xyz.openbmc_project.Inventory.Item"; |
| 630 | static constexpr auto presentProperty = "Present"; |
| 631 | try |
| 632 | { |
| 633 | auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant( |
| 634 | objPath.c_str(), presentProperty, presentInterface); |
| 635 | isPresent = std::get<bool>(propVal); |
| 636 | } |
| 637 | catch (const sdbusplus::exception::SdBusError& e) |
| 638 | { |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 639 | error("Failed to check for FRU presence at {PATH}, error - {ERROR}", |
| 640 | "PATH", objPath, "ERROR", e); |
Sridevi Ramesh | eefe49b | 2022-06-27 11:51:02 -0500 | [diff] [blame] | 641 | } |
| 642 | return isPresent; |
| 643 | } |
| 644 | |
Sagar Srinivas | 5db6e87 | 2023-12-01 10:03:30 -0600 | [diff] [blame] | 645 | bool checkIfLogicalBitSet(const uint16_t& containerId) |
| 646 | { |
| 647 | return !(containerId & 0x8000); |
| 648 | } |
| 649 | |
Pavithra Barithaya | 5e542be | 2021-08-13 00:33:31 -0500 | [diff] [blame] | 650 | void setFruPresence(const std::string& fruObjPath, bool present) |
| 651 | { |
| 652 | pldm::utils::PropertyValue value{present}; |
| 653 | pldm::utils::DBusMapping dbusMapping; |
| 654 | dbusMapping.objectPath = fruObjPath; |
| 655 | dbusMapping.interface = "xyz.openbmc_project.Inventory.Item"; |
| 656 | dbusMapping.propertyName = "Present"; |
| 657 | dbusMapping.propertyType = "bool"; |
| 658 | try |
| 659 | { |
| 660 | pldm::utils::DBusHandler().setDbusProperty(dbusMapping, value); |
| 661 | } |
| 662 | catch (const std::exception& e) |
| 663 | { |
| 664 | error( |
Riya Dixit | 76f2c60 | 2024-03-28 07:34:12 -0500 | [diff] [blame] | 665 | "Failed to set the present property on path '{PATH}', error - {ERROR}.", |
Pavithra Barithaya | 5e542be | 2021-08-13 00:33:31 -0500 | [diff] [blame] | 666 | "PATH", fruObjPath, "ERROR", e); |
| 667 | } |
| 668 | } |
| 669 | |
Thu Nguyen | b8cf46b | 2024-06-15 02:44:35 +0000 | [diff] [blame] | 670 | std::string_view trimNameForDbus(std::string& name) |
| 671 | { |
| 672 | std::replace(name.begin(), name.end(), ' ', '_'); |
| 673 | auto nullTerminatorPos = name.find('\0'); |
| 674 | if (nullTerminatorPos != std::string::npos) |
| 675 | { |
| 676 | name.erase(nullTerminatorPos); |
| 677 | } |
| 678 | return name; |
| 679 | } |
Thu Nguyen | a34a64b | 2022-03-31 08:56:39 +0700 | [diff] [blame^] | 680 | |
| 681 | bool dbusPropValuesToDouble(const std::string_view& type, |
| 682 | const pldm::utils::PropertyValue& value, |
| 683 | double* doubleValue) |
| 684 | { |
| 685 | if (!dbusValueNumericTypeNames.contains(type)) |
| 686 | { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | if (!doubleValue) |
| 691 | { |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | try |
| 696 | { |
| 697 | if (type == "uint8_t") |
| 698 | { |
| 699 | *doubleValue = static_cast<double>(std::get<uint8_t>(value)); |
| 700 | } |
| 701 | else if (type == "int16_t") |
| 702 | { |
| 703 | *doubleValue = static_cast<double>(std::get<int16_t>(value)); |
| 704 | } |
| 705 | else if (type == "uint16_t") |
| 706 | { |
| 707 | *doubleValue = static_cast<double>(std::get<uint16_t>(value)); |
| 708 | } |
| 709 | else if (type == "int32_t") |
| 710 | { |
| 711 | *doubleValue = static_cast<double>(std::get<int32_t>(value)); |
| 712 | } |
| 713 | else if (type == "uint32_t") |
| 714 | { |
| 715 | *doubleValue = static_cast<double>(std::get<uint32_t>(value)); |
| 716 | } |
| 717 | else if (type == "int64_t") |
| 718 | { |
| 719 | *doubleValue = static_cast<double>(std::get<int64_t>(value)); |
| 720 | } |
| 721 | else if (type == "uint64_t") |
| 722 | { |
| 723 | *doubleValue = static_cast<double>(std::get<uint64_t>(value)); |
| 724 | } |
| 725 | else if (type == "double") |
| 726 | { |
| 727 | *doubleValue = static_cast<double>(std::get<double>(value)); |
| 728 | } |
| 729 | else |
| 730 | { |
| 731 | return false; |
| 732 | } |
| 733 | } |
| 734 | catch (const std::exception& e) |
| 735 | { |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | return true; |
| 740 | } |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 741 | } // namespace utils |
| 742 | } // namespace pldm |