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