| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1 | #include <arpa/inet.h> | 
|  | 2 | #include <dirent.h> | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 3 | #include <fcntl.h> | 
|  | 4 | #include <linux/i2c-dev.h> | 
|  | 5 | #include <linux/i2c.h> | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 6 | #include <net/if.h> | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 7 | #include <sys/ioctl.h> | 
|  | 8 | #include <sys/types.h> | 
|  | 9 | #include <unistd.h> | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 10 |  | 
| Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 11 | #include <ipmid/utils.hpp> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/elog-errors.hpp> | 
|  | 13 | #include <phosphor-logging/log.hpp> | 
| William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 14 | #include <sdbusplus/message/types.hpp> | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> | 
|  | 16 |  | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 17 | #include <algorithm> | 
|  | 18 | #include <chrono> | 
|  | 19 |  | 
| Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 20 | namespace ipmi | 
|  | 21 | { | 
|  | 22 |  | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 23 | using namespace phosphor::logging; | 
| Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 24 | using namespace sdbusplus::error::xyz::openbmc_project::common; | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 25 |  | 
| Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 26 | namespace network | 
|  | 27 | { | 
|  | 28 |  | 
|  | 29 | /** @brief checks if the given ip is Link Local Ip or not. | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 30 | *  @param[in] ipaddress - IPAddress. | 
|  | 31 | */ | 
| Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 32 | bool isLinkLocalIP(const std::string& ipaddress); | 
|  | 33 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 34 | } // namespace network | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 35 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 36 | // TODO There may be cases where an interface is implemented by multiple | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 37 | //  objects,to handle such cases we are interested on that object | 
|  | 38 | //  which are on interested busname. | 
|  | 39 | //  Currently mapper doesn't give the readable busname(gives busid) so we can't | 
|  | 40 | //  use busname to find the object,will do later once the support is there. | 
|  | 41 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 42 | DbusObjectInfo | 
|  | 43 | getDbusObject(sdbusplus::bus_t& bus, const std::string& interface, | 
|  | 44 | const std::string& serviceRoot, const std::string& match) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 45 | { | 
|  | 46 | std::vector<DbusInterface> interfaces; | 
|  | 47 | interfaces.emplace_back(interface); | 
|  | 48 |  | 
| George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 49 | ObjectTree objectTree = getSubTree(bus, interfaces, serviceRoot); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 50 | if (objectTree.empty()) | 
|  | 51 | { | 
| Patrick Venture | f0c4878 | 2017-09-21 18:48:51 -0700 | [diff] [blame] | 52 | log<level::ERR>("No Object has implemented the interface", | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 53 | entry("INTERFACE=%s", interface.c_str())); | 
|  | 54 | elog<InternalFailure>(); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | DbusObjectInfo objectInfo; | 
|  | 58 |  | 
|  | 59 | // if match is empty then return the first object | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 60 | if (match == "") | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 61 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 62 | objectInfo = std::make_pair( | 
|  | 63 | objectTree.begin()->first, | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 64 | std::move(objectTree.begin()->second.begin()->first)); | 
|  | 65 | return objectInfo; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // else search the match string in the object path | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 69 | auto found = std::find_if( | 
|  | 70 | objectTree.begin(), objectTree.end(), [&match](const auto& object) { | 
|  | 71 | return (object.first.find(match) != std::string::npos); | 
|  | 72 | }); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 73 |  | 
| Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 74 | if (found == objectTree.end()) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 75 | { | 
|  | 76 | log<level::ERR>("Failed to find object which matches", | 
|  | 77 | entry("MATCH=%s", match.c_str())); | 
|  | 78 | elog<InternalFailure>(); | 
| Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 79 | // elog<> throws an exception. | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 80 | } | 
| Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 81 |  | 
|  | 82 | return make_pair(found->first, std::move(found->second.begin()->first)); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 85 | Value getDbusProperty(sdbusplus::bus_t& bus, const std::string& service, | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 86 | const std::string& objPath, const std::string& interface, | 
| Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 87 | const std::string& property, | 
|  | 88 | std::chrono::microseconds timeout) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 89 | { | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 90 | Value value; | 
|  | 91 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 92 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 93 | PROP_INTF, METHOD_GET); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 94 |  | 
|  | 95 | method.append(interface, property); | 
|  | 96 |  | 
| Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 97 | auto reply = bus.call(method, timeout.count()); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 98 | reply.read(value); | 
|  | 99 |  | 
|  | 100 | return value; | 
|  | 101 | } | 
|  | 102 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 103 | PropertyMap getAllDbusProperties( | 
|  | 104 | sdbusplus::bus_t& bus, const std::string& service, | 
|  | 105 | const std::string& objPath, const std::string& interface, | 
|  | 106 | std::chrono::microseconds timeout) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 107 | { | 
|  | 108 | PropertyMap properties; | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 109 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 110 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 111 | PROP_INTF, METHOD_GET_ALL); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 112 |  | 
|  | 113 | method.append(interface); | 
|  | 114 |  | 
| Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 115 | auto reply = bus.call(method, timeout.count()); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 116 | reply.read(properties); | 
| George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 117 |  | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 118 | return properties; | 
|  | 119 | } | 
|  | 120 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 121 | ObjectValueTree getManagedObjects(sdbusplus::bus_t& bus, | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 122 | const std::string& service, | 
|  | 123 | const std::string& objPath) | 
| Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 124 | { | 
|  | 125 | ipmi::ObjectValueTree interfaces; | 
|  | 126 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 127 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 128 | "org.freedesktop.DBus.ObjectManager", | 
|  | 129 | "GetManagedObjects"); | 
| Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 130 | auto reply = bus.call(method); | 
| Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 131 | reply.read(interfaces); | 
| George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 132 |  | 
| Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 133 | return interfaces; | 
|  | 134 | } | 
|  | 135 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 136 | void setDbusProperty(sdbusplus::bus_t& bus, const std::string& service, | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 137 | const std::string& objPath, const std::string& interface, | 
| Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 138 | const std::string& property, const Value& value, | 
|  | 139 | std::chrono::microseconds timeout) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 140 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 141 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 142 | PROP_INTF, METHOD_SET); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 143 |  | 
|  | 144 | method.append(interface, property, value); | 
|  | 145 |  | 
| Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 146 | if (!bus.call(method, timeout.count())) | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 147 | { | 
|  | 148 | log<level::ERR>("Failed to set property", | 
|  | 149 | entry("PROPERTY=%s", property.c_str()), | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 150 | entry("PATH=%s", objPath.c_str()), | 
|  | 151 | entry("INTERFACE=%s", interface.c_str())); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 152 | elog<InternalFailure>(); | 
|  | 153 | } | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 156 | ServiceCache::ServiceCache(const std::string& intf, const std::string& path) : | 
| Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 157 | intf(intf), path(path), cachedService(std::nullopt), | 
|  | 158 | cachedBusName(std::nullopt) | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 159 | {} | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 160 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 161 | ServiceCache::ServiceCache(std::string&& intf, std::string&& path) : | 
| Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 162 | intf(std::move(intf)), path(std::move(path)), cachedService(std::nullopt), | 
|  | 163 | cachedBusName(std::nullopt) | 
| Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 164 | {} | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 165 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 166 | const std::string& ServiceCache::getService(sdbusplus::bus_t& bus) | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 167 | { | 
|  | 168 | if (!isValid(bus)) | 
|  | 169 | { | 
|  | 170 | cachedBusName = bus.get_unique_name(); | 
|  | 171 | cachedService = ::ipmi::getService(bus, intf, path); | 
|  | 172 | } | 
|  | 173 | return cachedService.value(); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void ServiceCache::invalidate() | 
|  | 177 | { | 
| Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 178 | cachedBusName = std::nullopt; | 
|  | 179 | cachedService = std::nullopt; | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 182 | sdbusplus::message_t ServiceCache::newMethodCall( | 
|  | 183 | sdbusplus::bus_t& bus, const char* intf, const char* method) | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 184 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 185 | return bus.new_method_call(getService(bus).c_str(), path.c_str(), intf, | 
|  | 186 | method); | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 189 | bool ServiceCache::isValid(sdbusplus::bus_t& bus) const | 
| William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 190 | { | 
|  | 191 | return cachedService && cachedBusName == bus.get_unique_name(); | 
|  | 192 | } | 
|  | 193 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 194 | std::string getService(sdbusplus::bus_t& bus, const std::string& intf, | 
| Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 195 | const std::string& path) | 
|  | 196 | { | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 197 | auto mapperCall = | 
|  | 198 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", | 
|  | 199 | "/xyz/openbmc_project/object_mapper", | 
|  | 200 | "xyz.openbmc_project.ObjectMapper", "GetObject"); | 
| Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 201 |  | 
|  | 202 | mapperCall.append(path); | 
|  | 203 | mapperCall.append(std::vector<std::string>({intf})); | 
|  | 204 |  | 
|  | 205 | auto mapperResponseMsg = bus.call(mapperCall); | 
|  | 206 |  | 
| Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 207 | std::map<std::string, std::vector<std::string>> mapperResponse; | 
|  | 208 | mapperResponseMsg.read(mapperResponse); | 
|  | 209 |  | 
|  | 210 | if (mapperResponse.begin() == mapperResponse.end()) | 
|  | 211 | { | 
|  | 212 | throw std::runtime_error("ERROR in reading the mapper response"); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | return mapperResponse.begin()->first; | 
|  | 216 | } | 
|  | 217 |  | 
| George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 218 | ObjectTree getSubTree(sdbusplus::bus_t& bus, const InterfaceList& interfaces, | 
|  | 219 | const std::string& subtreePath, int32_t depth) | 
|  | 220 | { | 
|  | 221 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, | 
|  | 222 | MAPPER_INTF, "GetSubTree"); | 
|  | 223 |  | 
|  | 224 | mapperCall.append(subtreePath, depth, interfaces); | 
|  | 225 |  | 
|  | 226 | auto mapperReply = bus.call(mapperCall); | 
|  | 227 | ObjectTree objectTree; | 
|  | 228 | mapperReply.read(objectTree); | 
|  | 229 |  | 
|  | 230 | return objectTree; | 
|  | 231 | } | 
|  | 232 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 233 | ipmi::ObjectTree | 
|  | 234 | getAllDbusObjects(sdbusplus::bus_t& bus, const std::string& serviceRoot, | 
|  | 235 | const std::string& interface, const std::string& match) | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 236 | { | 
|  | 237 | std::vector<std::string> interfaces; | 
|  | 238 | interfaces.emplace_back(interface); | 
| Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 239 |  | 
| George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 240 | ObjectTree objectTree = getSubTree(bus, interfaces, serviceRoot); | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 241 | for (auto it = objectTree.begin(); it != objectTree.end();) | 
|  | 242 | { | 
|  | 243 | if (it->first.find(match) == std::string::npos) | 
|  | 244 | { | 
|  | 245 | it = objectTree.erase(it); | 
|  | 246 | } | 
|  | 247 | else | 
|  | 248 | { | 
|  | 249 | ++it; | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | return objectTree; | 
|  | 254 | } | 
|  | 255 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 256 | void deleteAllDbusObjects(sdbusplus::bus_t& bus, const std::string& serviceRoot, | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 257 | const std::string& interface, | 
|  | 258 | const std::string& match) | 
|  | 259 | { | 
|  | 260 | try | 
|  | 261 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 262 | auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match); | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 263 |  | 
|  | 264 | for (auto& object : objectTree) | 
|  | 265 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 266 | method_no_args::callDbusMethod(bus, object.second.begin()->first, | 
|  | 267 | object.first, DELETE_INTERFACE, | 
|  | 268 | "Delete"); | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 269 | } | 
|  | 270 | } | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 271 | catch (const sdbusplus::exception_t& e) | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 272 | { | 
| Vernon Mauery | 7a61418 | 2018-11-27 12:54:52 -0800 | [diff] [blame] | 273 | log<level::INFO>("sdbusplus exception - Unable to delete the objects", | 
|  | 274 | entry("ERROR=%s", e.what()), | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 275 | entry("INTERFACE=%s", interface.c_str()), | 
|  | 276 | entry("SERVICE=%s", serviceRoot.c_str())); | 
|  | 277 | } | 
|  | 278 | } | 
|  | 279 |  | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 280 | static inline std::string convertToString(const InterfaceList& interfaces) | 
|  | 281 | { | 
|  | 282 | std::string intfStr; | 
|  | 283 | for (const auto& intf : interfaces) | 
|  | 284 | { | 
|  | 285 | intfStr += "," + intf; | 
|  | 286 | } | 
|  | 287 | return intfStr; | 
|  | 288 | } | 
|  | 289 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 290 | ObjectTree getAllAncestors(sdbusplus::bus_t& bus, const std::string& path, | 
| Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 291 | InterfaceList&& interfaces) | 
|  | 292 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 293 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, | 
|  | 294 | MAPPER_INTF, "GetAncestors"); | 
| Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 295 | mapperCall.append(path, interfaces); | 
|  | 296 |  | 
|  | 297 | auto mapperReply = bus.call(mapperCall); | 
| Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 298 | ObjectTree objectTree; | 
|  | 299 | mapperReply.read(objectTree); | 
|  | 300 |  | 
|  | 301 | if (objectTree.empty()) | 
|  | 302 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 303 | log<level::ERR>( | 
|  | 304 | "No Object has implemented the interface", | 
|  | 305 | entry("PATH=%s", path.c_str()), | 
|  | 306 | entry("INTERFACES=%s", convertToString(interfaces).c_str())); | 
| Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 307 | elog<InternalFailure>(); | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | return objectTree; | 
|  | 311 | } | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 312 |  | 
|  | 313 | namespace method_no_args | 
|  | 314 | { | 
|  | 315 |  | 
| Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 316 | void callDbusMethod(sdbusplus::bus_t& bus, const std::string& service, | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 317 | const std::string& objPath, const std::string& interface, | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 318 | const std::string& method) | 
|  | 319 |  | 
|  | 320 | { | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 321 | auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 322 | interface.c_str(), method.c_str()); | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 323 | auto reply = bus.call(busMethod); | 
| Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
| Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 326 | } // namespace method_no_args | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 327 |  | 
|  | 328 | /********* Begin co-routine yielding alternatives ***************/ | 
|  | 329 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 330 | boost::system::error_code | 
|  | 331 | getService(Context::ptr ctx, const std::string& intf, | 
|  | 332 | const std::string& path, std::string& service) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 333 | { | 
|  | 334 | boost::system::error_code ec; | 
|  | 335 | std::map<std::string, std::vector<std::string>> mapperResponse = | 
|  | 336 | ctx->bus->yield_method_call<decltype(mapperResponse)>( | 
|  | 337 | ctx->yield, ec, "xyz.openbmc_project.ObjectMapper", | 
|  | 338 | "/xyz/openbmc_project/object_mapper", | 
| Jason M. Bills | f596798 | 2020-03-06 10:42:08 -0800 | [diff] [blame] | 339 | "xyz.openbmc_project.ObjectMapper", "GetObject", path, | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 340 | std::vector<std::string>({intf})); | 
|  | 341 |  | 
|  | 342 | if (!ec) | 
|  | 343 | { | 
|  | 344 | service = std::move(mapperResponse.begin()->first); | 
|  | 345 | } | 
|  | 346 | return ec; | 
|  | 347 | } | 
|  | 348 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 349 | boost::system::error_code getSubTree( | 
|  | 350 | Context::ptr ctx, const InterfaceList& interfaces, | 
|  | 351 | const std::string& subtreePath, int32_t depth, ObjectTree& objectTree) | 
| George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 352 | { | 
|  | 353 | boost::system::error_code ec; | 
|  | 354 | objectTree = ctx->bus->yield_method_call<ObjectTree>( | 
|  | 355 | ctx->yield, ec, MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, "GetSubTree", | 
|  | 356 | subtreePath, depth, interfaces); | 
|  | 357 |  | 
|  | 358 | return ec; | 
|  | 359 | } | 
|  | 360 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 361 | boost::system::error_code | 
|  | 362 | getDbusObject(Context::ptr ctx, const std::string& interface, | 
|  | 363 | const std::string& subtreePath, const std::string& match, | 
|  | 364 | DbusObjectInfo& dbusObject) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 365 | { | 
|  | 366 | std::vector<DbusInterface> interfaces; | 
|  | 367 | interfaces.emplace_back(interface); | 
|  | 368 |  | 
|  | 369 | auto depth = 0; | 
| George Liu | 50f186c | 2024-02-04 16:51:26 +0800 | [diff] [blame] | 370 | ObjectTree objectTree; | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 371 | boost::system::error_code ec = | 
|  | 372 | getSubTree(ctx, interfaces, subtreePath, depth, objectTree); | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 373 |  | 
|  | 374 | if (ec) | 
|  | 375 | { | 
|  | 376 | return ec; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | if (objectTree.empty()) | 
|  | 380 | { | 
|  | 381 | log<level::ERR>("No Object has implemented the interface", | 
|  | 382 | entry("INTERFACE=%s", interface.c_str()), | 
|  | 383 | entry("NETFN=%x", ctx->netFn), | 
|  | 384 | entry("CMD=%x,", ctx->cmd)); | 
|  | 385 | return boost::system::errc::make_error_code( | 
|  | 386 | boost::system::errc::no_such_process); | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | // if match is empty then return the first object | 
|  | 390 | if (match == "") | 
|  | 391 | { | 
|  | 392 | dbusObject = std::make_pair( | 
|  | 393 | std::move(objectTree.begin()->first), | 
|  | 394 | std::move(objectTree.begin()->second.begin()->first)); | 
|  | 395 | return ec; | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | // else search the match string in the object path | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 399 | auto found = std::find_if( | 
|  | 400 | objectTree.begin(), objectTree.end(), [&match](const auto& object) { | 
|  | 401 | return (object.first.find(match) != std::string::npos); | 
|  | 402 | }); | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 403 |  | 
|  | 404 | if (found == objectTree.end()) | 
|  | 405 | { | 
|  | 406 | log<level::ERR>("Failed to find object which matches", | 
|  | 407 | entry("MATCH=%s", match.c_str()), | 
|  | 408 | entry("NETFN=%x", ctx->netFn), | 
|  | 409 | entry("CMD=%x,", ctx->cmd)); | 
|  | 410 | // set ec | 
|  | 411 | return boost::system::errc::make_error_code( | 
|  | 412 | boost::system::errc::no_such_file_or_directory); | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | dbusObject = std::make_pair(std::move(found->first), | 
|  | 416 | std::move(found->second.begin()->first)); | 
|  | 417 | return ec; | 
|  | 418 | } | 
|  | 419 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 420 | boost::system::error_code getAllDbusProperties( | 
|  | 421 | Context::ptr ctx, const std::string& service, const std::string& objPath, | 
|  | 422 | const std::string& interface, PropertyMap& properties) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 423 | { | 
|  | 424 | boost::system::error_code ec; | 
|  | 425 | properties = ctx->bus->yield_method_call<PropertyMap>( | 
|  | 426 | ctx->yield, ec, service.c_str(), objPath.c_str(), PROP_INTF, | 
|  | 427 | METHOD_GET_ALL, interface); | 
|  | 428 | return ec; | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | boost::system::error_code | 
|  | 432 | setDbusProperty(Context::ptr ctx, const std::string& service, | 
|  | 433 | const std::string& objPath, const std::string& interface, | 
|  | 434 | const std::string& property, const Value& value) | 
|  | 435 | { | 
|  | 436 | boost::system::error_code ec; | 
|  | 437 | ctx->bus->yield_method_call(ctx->yield, ec, service.c_str(), | 
|  | 438 | objPath.c_str(), PROP_INTF, METHOD_SET, | 
|  | 439 | interface, property, value); | 
|  | 440 | return ec; | 
|  | 441 | } | 
|  | 442 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 443 | boost::system::error_code | 
|  | 444 | getAllDbusObjects(Context::ptr ctx, const std::string& serviceRoot, | 
|  | 445 | const std::string& interface, const std::string& match, | 
|  | 446 | ObjectTree& objectTree) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 447 | { | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 448 | std::vector<std::string> interfaces; | 
|  | 449 | interfaces.emplace_back(interface); | 
|  | 450 |  | 
|  | 451 | auto depth = 0; | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 452 | boost::system::error_code ec = | 
|  | 453 | getSubTree(ctx, interfaces, serviceRoot, depth, objectTree); | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 454 | if (ec) | 
|  | 455 | { | 
|  | 456 | return ec; | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | for (auto it = objectTree.begin(); it != objectTree.end();) | 
|  | 460 | { | 
|  | 461 | if (it->first.find(match) == std::string::npos) | 
|  | 462 | { | 
|  | 463 | it = objectTree.erase(it); | 
|  | 464 | } | 
|  | 465 | else | 
|  | 466 | { | 
|  | 467 | ++it; | 
|  | 468 | } | 
|  | 469 | } | 
|  | 470 |  | 
|  | 471 | return ec; | 
|  | 472 | } | 
|  | 473 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 474 | boost::system::error_code | 
|  | 475 | deleteAllDbusObjects(Context::ptr ctx, const std::string& serviceRoot, | 
|  | 476 | const std::string& interface, const std::string& match) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 477 | { | 
|  | 478 | ObjectTree objectTree; | 
|  | 479 | boost::system::error_code ec = | 
|  | 480 | getAllDbusObjects(ctx, serviceRoot, interface, match, objectTree); | 
|  | 481 | if (ec) | 
|  | 482 | { | 
|  | 483 | return ec; | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | for (auto& object : objectTree) | 
|  | 487 | { | 
|  | 488 | ctx->bus->yield_method_call(ctx->yield, ec, | 
|  | 489 | object.second.begin()->first, object.first, | 
|  | 490 | DELETE_INTERFACE, "Delete"); | 
|  | 491 | if (ec) | 
|  | 492 | { | 
|  | 493 | log<level::ERR>("Failed to delete all objects", | 
|  | 494 | entry("INTERFACE=%s", interface.c_str()), | 
|  | 495 | entry("SERVICE=%s", serviceRoot.c_str()), | 
|  | 496 | entry("NETFN=%x", ctx->netFn), | 
|  | 497 | entry("CMD=%x,", ctx->cmd), | 
|  | 498 | entry("ERROR=%s", ec.message().c_str())); | 
|  | 499 | break; | 
|  | 500 | } | 
|  | 501 | } | 
|  | 502 | return ec; | 
|  | 503 | } | 
|  | 504 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 505 | boost::system::error_code | 
|  | 506 | getManagedObjects(Context::ptr ctx, const std::string& service, | 
|  | 507 | const std::string& objPath, ObjectValueTree& objects) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 508 | { | 
|  | 509 | boost::system::error_code ec; | 
|  | 510 | objects = ctx->bus->yield_method_call<ipmi::ObjectValueTree>( | 
|  | 511 | ctx->yield, ec, service.c_str(), objPath.c_str(), | 
|  | 512 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); | 
|  | 513 | return ec; | 
|  | 514 | } | 
|  | 515 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 516 | boost::system::error_code | 
|  | 517 | getAllAncestors(Context::ptr ctx, const std::string& path, | 
|  | 518 | const InterfaceList& interfaces, ObjectTree& objectTree) | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 519 | { | 
|  | 520 | std::string interfaceList = convertToString(interfaces); | 
|  | 521 |  | 
|  | 522 | boost::system::error_code ec; | 
|  | 523 | objectTree = ctx->bus->yield_method_call<ObjectTree>( | 
|  | 524 | ctx->yield, ec, MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_INTF, | 
|  | 525 | "GetAncestors", path, interfaceList); | 
|  | 526 |  | 
|  | 527 | if (ec) | 
|  | 528 | { | 
|  | 529 | return ec; | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | if (objectTree.empty()) | 
|  | 533 | { | 
|  | 534 | log<level::ERR>("No Object has implemented the interface", | 
|  | 535 | entry("PATH=%s", path.c_str()), | 
|  | 536 | entry("INTERFACES=%s", interfaceList.c_str())); | 
|  | 537 | elog<InternalFailure>(); | 
|  | 538 | } | 
|  | 539 |  | 
|  | 540 | return ec; | 
|  | 541 | } | 
|  | 542 |  | 
| Patrick Williams | 1318a5e | 2024-08-16 15:19:54 -0400 | [diff] [blame^] | 543 | boost::system::error_code callDbusMethod( | 
|  | 544 | Context::ptr ctx, const std::string& service, const std::string& objPath, | 
|  | 545 | const std::string& interface, const std::string& method) | 
| Albert Zhang | b53049e | 2022-04-02 15:39:51 +0800 | [diff] [blame] | 546 | { | 
|  | 547 | boost::system::error_code ec; | 
|  | 548 | ctx->bus->yield_method_call(ctx->yield, ec, service, objPath, interface, | 
|  | 549 | method); | 
|  | 550 | return ec; | 
|  | 551 | } | 
|  | 552 |  | 
| Vernon Mauery | eeb0f98 | 2019-05-29 16:36:58 -0700 | [diff] [blame] | 553 | /********* End co-routine yielding alternatives ***************/ | 
|  | 554 |  | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 555 | ipmi::Cc i2cWriteRead(std::string i2cBus, const uint8_t targetAddr, | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 556 | std::vector<uint8_t> writeData, | 
|  | 557 | std::vector<uint8_t>& readBuf) | 
|  | 558 | { | 
|  | 559 | // Open the i2c device, for low-level combined data write/read | 
|  | 560 | int i2cDev = ::open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); | 
|  | 561 | if (i2cDev < 0) | 
|  | 562 | { | 
|  | 563 | log<level::ERR>("Failed to open i2c bus", | 
|  | 564 | phosphor::logging::entry("BUS=%s", i2cBus.c_str())); | 
|  | 565 | return ipmi::ccInvalidFieldRequest; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | const size_t writeCount = writeData.size(); | 
|  | 569 | const size_t readCount = readBuf.size(); | 
|  | 570 | int msgCount = 0; | 
| Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 571 | i2c_msg i2cmsg[2] = {}; | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 572 | if (writeCount) | 
|  | 573 | { | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 574 | // Data will be writtern to the target address | 
|  | 575 | i2cmsg[msgCount].addr = targetAddr; | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 576 | i2cmsg[msgCount].flags = 0x00; | 
|  | 577 | i2cmsg[msgCount].len = writeCount; | 
|  | 578 | i2cmsg[msgCount].buf = writeData.data(); | 
|  | 579 | msgCount++; | 
|  | 580 | } | 
|  | 581 | if (readCount) | 
|  | 582 | { | 
| Matt Simmering | 68d9d40 | 2023-11-09 14:22:11 -0800 | [diff] [blame] | 583 | // Data will be read into the buffer from the target address | 
|  | 584 | i2cmsg[msgCount].addr = targetAddr; | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 585 | i2cmsg[msgCount].flags = I2C_M_RD; | 
|  | 586 | i2cmsg[msgCount].len = readCount; | 
|  | 587 | i2cmsg[msgCount].buf = readBuf.data(); | 
|  | 588 | msgCount++; | 
|  | 589 | } | 
|  | 590 |  | 
| Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 591 | i2c_rdwr_ioctl_data msgReadWrite = {}; | 
| Yong Li | 7dc4ac0 | 2019-08-23 17:44:32 +0800 | [diff] [blame] | 592 | msgReadWrite.msgs = i2cmsg; | 
|  | 593 | msgReadWrite.nmsgs = msgCount; | 
|  | 594 |  | 
|  | 595 | // Perform the combined write/read | 
|  | 596 | int ret = ::ioctl(i2cDev, I2C_RDWR, &msgReadWrite); | 
|  | 597 | ::close(i2cDev); | 
|  | 598 |  | 
|  | 599 | if (ret < 0) | 
|  | 600 | { | 
|  | 601 | log<level::ERR>("I2C WR Failed!", | 
|  | 602 | phosphor::logging::entry("RET=%d", ret)); | 
|  | 603 | return ipmi::ccUnspecifiedError; | 
|  | 604 | } | 
|  | 605 | if (readCount) | 
|  | 606 | { | 
|  | 607 | readBuf.resize(msgReadWrite.msgs[msgCount - 1].len); | 
|  | 608 | } | 
|  | 609 |  | 
|  | 610 | return ipmi::ccSuccess; | 
|  | 611 | } | 
|  | 612 |  | 
| Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 613 | } // namespace ipmi |