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