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