George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
| 3 | #include <array> |
| 4 | #include <ctime> |
| 5 | #include <iostream> |
| 6 | #include <map> |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 7 | #include <stdexcept> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | |
| 12 | namespace pldm |
| 13 | { |
| 14 | namespace utils |
| 15 | { |
| 16 | |
| 17 | constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 18 | constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 19 | constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper"; |
| 20 | |
| 21 | uint8_t getNumPadBytes(uint32_t data) |
| 22 | { |
| 23 | uint8_t pad; |
| 24 | pad = ((data % 4) ? (4 - data % 4) : 0); |
| 25 | return pad; |
| 26 | } // end getNumPadBytes |
| 27 | |
| 28 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 29 | uint8_t* hour, uint8_t* min, uint8_t* sec) |
| 30 | { |
| 31 | constexpr uint64_t max_data = 29991231115959; |
| 32 | constexpr uint64_t min_data = 19700101000000; |
| 33 | if (data < min_data || data > max_data) |
| 34 | { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | *year = data / 10000000000; |
| 39 | data = data % 10000000000; |
| 40 | *month = data / 100000000; |
| 41 | data = data % 100000000; |
| 42 | *day = data / 1000000; |
| 43 | data = data % 1000000; |
| 44 | *hour = data / 10000; |
| 45 | data = data % 10000; |
| 46 | *min = data / 100; |
| 47 | *sec = data % 100; |
| 48 | |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool decodeEffecterData(const std::vector<uint8_t>& effecterData, |
| 53 | uint16_t& effecter_id, |
| 54 | std::vector<set_effecter_state_field>& stateField) |
| 55 | { |
| 56 | int flag = 0; |
| 57 | for (auto data : effecterData) |
| 58 | { |
| 59 | switch (flag) |
| 60 | { |
| 61 | case 0: |
| 62 | effecter_id = data; |
| 63 | flag = 1; |
| 64 | break; |
| 65 | case 1: |
| 66 | if (data == PLDM_REQUEST_SET) |
| 67 | { |
| 68 | flag = 2; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | stateField.push_back({PLDM_NO_CHANGE, 0}); |
| 73 | } |
| 74 | break; |
| 75 | case 2: |
| 76 | stateField.push_back({PLDM_REQUEST_SET, data}); |
| 77 | flag = 1; |
| 78 | break; |
| 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (stateField.size() < 1 || stateField.size() > 8) |
| 85 | { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 92 | std::string DBusHandler::getService(const char* path, |
| 93 | const char* interface) const |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 94 | { |
| 95 | using DbusInterfaceList = std::vector<std::string>; |
| 96 | std::map<std::string, std::vector<std::string>> mapperResponse; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 97 | auto& bus = DBusHandler::getBus(); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 98 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 99 | auto mapper = bus.new_method_call(mapperBusName, mapperPath, |
| 100 | mapperInterface, "GetObject"); |
| 101 | mapper.append(path, DbusInterfaceList({interface})); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 102 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 103 | auto mapperResponseMsg = bus.call(mapper); |
| 104 | mapperResponseMsg.read(mapperResponse); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 105 | return mapperResponse.begin()->first; |
| 106 | } |
| 107 | |
| 108 | void reportError(const char* errorMsg) |
| 109 | { |
| 110 | static constexpr auto logObjPath = "/xyz/openbmc_project/logging"; |
| 111 | static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create"; |
| 112 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 113 | auto& bus = pldm::utils::DBusHandler::getBus(); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 114 | |
| 115 | try |
| 116 | { |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 117 | auto service = DBusHandler().getService(logObjPath, logInterface); |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 118 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 119 | auto severity = |
| 120 | sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage( |
| 121 | sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level:: |
| 122 | Error); |
| 123 | auto method = bus.new_method_call(service.c_str(), logObjPath, |
| 124 | logInterface, "Create"); |
| 125 | std::map<std::string, std::string> addlData{}; |
| 126 | method.append(errorMsg, severity, addlData); |
| 127 | bus.call_noreply(method); |
| 128 | } |
| 129 | catch (const std::exception& e) |
| 130 | { |
| 131 | std::cerr << "failed to make a d-bus call to create error log, ERROR=" |
| 132 | << e.what() << "\n"; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace utils |
| 137 | } // namespace pldm |