Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 1 | #include "utils.hpp" |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 2 | #include "host-ipmid/ipmid-api.h" |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 6 | |
| 7 | namespace ipmi |
| 8 | { |
| 9 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 10 | using namespace phosphor::logging; |
| 11 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 12 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 13 | |
| 14 | //TODO There may be cases where an interface is implemented by multiple |
| 15 | // objects,to handle such cases we are interested on that object |
| 16 | // which are on interested busname. |
| 17 | // Currently mapper doesn't give the readable busname(gives busid) so we can't |
| 18 | // use busname to find the object,will do later once the support is there. |
| 19 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame^] | 20 | DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, |
| 21 | const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 22 | const std::string& serviceRoot, |
| 23 | const std::string& match) |
| 24 | { |
| 25 | std::vector<DbusInterface> interfaces; |
| 26 | interfaces.emplace_back(interface); |
| 27 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 28 | auto depth = 0; |
| 29 | |
| 30 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, |
| 31 | MAPPER_OBJ, |
| 32 | MAPPER_INTF, |
| 33 | "GetSubTree"); |
| 34 | |
| 35 | mapperCall.append(serviceRoot, depth, interfaces); |
| 36 | |
| 37 | auto mapperReply = bus.call(mapperCall); |
| 38 | if (mapperReply.is_method_error()) |
| 39 | { |
| 40 | log<level::ERR>("Error in mapper call"); |
| 41 | elog<InternalFailure>(); |
| 42 | } |
| 43 | |
| 44 | ObjectTree objectTree; |
| 45 | mapperReply.read(objectTree); |
| 46 | |
| 47 | if (objectTree.empty()) |
| 48 | { |
| 49 | log<level::ERR>("No Object has impelmented the interface", |
| 50 | entry("INTERFACE=%s", interface.c_str())); |
| 51 | elog<InternalFailure>(); |
| 52 | } |
| 53 | |
| 54 | DbusObjectInfo objectInfo; |
| 55 | |
| 56 | // if match is empty then return the first object |
| 57 | if(match == "") |
| 58 | { |
| 59 | objectInfo = std::make_pair(objectTree.begin()->first, |
| 60 | std::move(objectTree.begin()->second.begin()->first)); |
| 61 | return objectInfo; |
| 62 | } |
| 63 | |
| 64 | // else search the match string in the object path |
| 65 | auto objectFound = false; |
| 66 | for (auto& object : objectTree) |
| 67 | { |
| 68 | if(object.first.find(match) != std::string::npos) |
| 69 | { |
| 70 | objectFound = true; |
| 71 | objectInfo = make_pair(object.first, |
| 72 | std::move(object.second.begin()->first)); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if(!objectFound) |
| 78 | { |
| 79 | log<level::ERR>("Failed to find object which matches", |
| 80 | entry("MATCH=%s", match.c_str())); |
| 81 | elog<InternalFailure>(); |
| 82 | } |
| 83 | return objectInfo; |
| 84 | |
| 85 | } |
| 86 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame^] | 87 | Value getDbusProperty(sdbusplus::bus::bus& bus, |
| 88 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 89 | const std::string& objPath, |
| 90 | const std::string& interface, |
| 91 | const std::string& property) |
| 92 | { |
| 93 | |
| 94 | Value value; |
| 95 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 96 | auto method = bus.new_method_call( |
| 97 | service.c_str(), |
| 98 | objPath.c_str(), |
| 99 | PROP_INTF, |
| 100 | METHOD_GET); |
| 101 | |
| 102 | method.append(interface, property); |
| 103 | |
| 104 | auto reply = bus.call(method); |
| 105 | |
| 106 | if (reply.is_method_error()) |
| 107 | { |
| 108 | log<level::ERR>("Failed to get property", |
| 109 | entry("PROPERTY=%s", property.c_str()), |
| 110 | entry("PATH=%s", objPath.c_str()), |
| 111 | entry("INTERFACE=%s", interface.c_str())); |
| 112 | elog<InternalFailure>(); |
| 113 | } |
| 114 | |
| 115 | reply.read(value); |
| 116 | |
| 117 | return value; |
| 118 | } |
| 119 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame^] | 120 | PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus, |
| 121 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 122 | const std::string& objPath, |
| 123 | const std::string& interface) |
| 124 | { |
| 125 | PropertyMap properties; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 126 | |
| 127 | auto method = bus.new_method_call( |
| 128 | service.c_str(), |
| 129 | objPath.c_str(), |
| 130 | PROP_INTF, |
| 131 | METHOD_GET_ALL); |
| 132 | |
| 133 | method.append(interface); |
| 134 | |
| 135 | auto reply = bus.call(method); |
| 136 | |
| 137 | if (reply.is_method_error()) |
| 138 | { |
| 139 | log<level::ERR>("Failed to get all properties", |
| 140 | entry("PATH=%s", objPath.c_str()), |
| 141 | entry("INTERFACE=%s", interface.c_str())); |
| 142 | elog<InternalFailure>(); |
| 143 | } |
| 144 | |
| 145 | reply.read(properties); |
| 146 | return properties; |
| 147 | } |
| 148 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame^] | 149 | void setDbusProperty(sdbusplus::bus::bus& bus, |
| 150 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 151 | const std::string& objPath, |
| 152 | const std::string& interface, |
| 153 | const std::string& property, |
| 154 | const Value& value) |
| 155 | { |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 156 | auto method = bus.new_method_call( |
| 157 | service.c_str(), |
| 158 | objPath.c_str(), |
| 159 | PROP_INTF, |
| 160 | METHOD_SET); |
| 161 | |
| 162 | method.append(interface, property, value); |
| 163 | |
| 164 | if (!bus.call(method)) |
| 165 | { |
| 166 | log<level::ERR>("Failed to set property", |
| 167 | entry("PROPERTY=%s", property.c_str()), |
| 168 | entry("PATH=%s",objPath.c_str()), |
| 169 | entry("INTERFACE=%s",interface.c_str())); |
| 170 | elog<InternalFailure>(); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 176 | std::string getService(sdbusplus::bus::bus& bus, |
| 177 | const std::string& intf, |
| 178 | const std::string& path) |
| 179 | { |
| 180 | auto mapperCall = bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
Leonel Gonzalez | d15e6c9 | 2017-03-16 13:47:58 -0500 | [diff] [blame] | 181 | "/xyz/openbmc_project/object_mapper", |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 182 | "xyz.openbmc_project.ObjectMapper", |
| 183 | "GetObject"); |
| 184 | |
| 185 | mapperCall.append(path); |
| 186 | mapperCall.append(std::vector<std::string>({intf})); |
| 187 | |
| 188 | auto mapperResponseMsg = bus.call(mapperCall); |
| 189 | |
| 190 | if (mapperResponseMsg.is_method_error()) |
| 191 | { |
| 192 | throw std::runtime_error("ERROR in mapper call"); |
| 193 | } |
| 194 | |
| 195 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 196 | mapperResponseMsg.read(mapperResponse); |
| 197 | |
| 198 | if (mapperResponse.begin() == mapperResponse.end()) |
| 199 | { |
| 200 | throw std::runtime_error("ERROR in reading the mapper response"); |
| 201 | } |
| 202 | |
| 203 | return mapperResponse.begin()->first; |
| 204 | } |
| 205 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 206 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 207 | } // namespace ipmi |
| 208 | |
| 209 | |