Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 1 | #pragma once |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 2 | #include <experimental/optional> |
| 3 | #include <sdbusplus/server.hpp> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 4 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 5 | #include "types.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 | constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper"; |
| 11 | constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper"; |
| 12 | constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper"; |
| 13 | |
| 14 | constexpr auto ROOT = "/"; |
| 15 | constexpr auto HOST_MATCH = "host0"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 16 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 17 | constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties"; |
| 18 | constexpr auto DELETE_INTERFACE = "xyz.openbmc_project.Object.Delete"; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 19 | |
| 20 | constexpr auto METHOD_GET = "Get"; |
| 21 | constexpr auto METHOD_GET_ALL = "GetAll"; |
| 22 | constexpr auto METHOD_SET = "Set"; |
| 23 | |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 24 | /** @class ServiceCache |
| 25 | * @brief Caches lookups of service names from the object mapper. |
| 26 | * @details Most ipmi commands need to talk to other dbus daemons to perform |
| 27 | * their intended actions on the BMC. This usually means they will |
| 28 | * first look up the service name providing the interface they |
| 29 | * require. This class reduces the number of such calls by caching |
| 30 | * the lookup for a specific service. |
| 31 | */ |
| 32 | class ServiceCache { |
| 33 | public: |
| 34 | /** @brief Creates a new service cache for the given interface |
| 35 | * and path. |
| 36 | * |
| 37 | * @param[in] intf - The interface used for each lookup |
| 38 | * @param[in] path - The path used for each lookup |
| 39 | */ |
| 40 | ServiceCache(const std::string& intf, const std::string& path); |
| 41 | ServiceCache(std::string&& intf, std::string&& path); |
| 42 | |
| 43 | /** @brief Gets the service name from the cache or does in a |
| 44 | * lookup when invalid. |
| 45 | * |
| 46 | * @param[in] bus - The bus associated with and used for looking |
| 47 | * up the service. |
| 48 | */ |
| 49 | const std::string& getService(sdbusplus::bus::bus& bus); |
| 50 | |
| 51 | /** @brief Invalidates the current service name */ |
| 52 | void invalidate(); |
| 53 | |
| 54 | /** @brief A wrapper around sdbusplus bus.new_method_call |
| 55 | * |
| 56 | * @param[in] bus - The bus used for calling the method |
| 57 | * @param[in] intf - The interface containing the method |
| 58 | * @param[in] method - The method name |
| 59 | * @return The message containing the method call. |
| 60 | */ |
| 61 | sdbusplus::message::message newMethodCall(sdbusplus::bus::bus& bus, |
| 62 | const char *intf, |
| 63 | const char *method); |
William A. Kennington III | 82c173a | 2018-05-11 16:10:12 -0700 | [diff] [blame] | 64 | |
| 65 | /** @brief Check to see if the current cache is valid |
| 66 | * |
| 67 | * @param[in] bus - The bus used for the service lookup |
| 68 | * @return True if the cache is valid false otherwise. |
| 69 | */ |
| 70 | bool isValid(sdbusplus::bus::bus& bus) const; |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 71 | private: |
| 72 | /** @brief DBUS interface provided by the service */ |
| 73 | const std::string intf; |
| 74 | /** @brief DBUS path provided by the service */ |
| 75 | const std::string path; |
| 76 | /** @brief The name of the service if valid */ |
| 77 | std::experimental::optional<std::string> cachedService; |
| 78 | /** @brief The name of the bus used in the service lookup */ |
| 79 | std::experimental::optional<std::string> cachedBusName; |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 82 | /** |
| 83 | * @brief Get the DBUS Service name for the input dbus path |
| 84 | * |
| 85 | * @param[in] bus - DBUS Bus Object |
| 86 | * @param[in] intf - DBUS Interface |
| 87 | * @param[in] path - DBUS Object Path |
| 88 | * |
| 89 | */ |
| 90 | std::string getService(sdbusplus::bus::bus& bus, |
| 91 | const std::string& intf, |
| 92 | const std::string& path); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 93 | |
| 94 | /** @brief Gets the dbus object info implementing the given interface |
| 95 | * from the given subtree. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 96 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 97 | * @param[in] interface - Dbus interface. |
| 98 | * @param[in] subtreePath - subtree from where the search should start. |
| 99 | * @param[in] match - identifier for object. |
| 100 | * @return On success returns the object having objectpath and servicename. |
| 101 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 102 | DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, |
| 103 | const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 104 | const std::string& subtreePath = ROOT, |
| 105 | const std::string& match = {}); |
| 106 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 107 | /** @brief Get the ipObject of first dbus IP object of Non-LinkLocalIPAddress |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 108 | * type from the given subtree, if not available gets IP object of |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 109 | * LinkLocalIPAddress type. |
| 110 | * @param[in] bus - DBUS Bus Object. |
| 111 | * @param[in] interface - Dbus interface. |
| 112 | * @param[in] subtreePath - subtree from where the search should start. |
| 113 | * @param[in] match - identifier for object. |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 114 | * @return On success returns the object having objectpath and servicename. |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 115 | */ |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 116 | DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus, |
| 117 | const std::string& interface, |
| 118 | const std::string& subtreePath, |
| 119 | const std::string& match); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 120 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 121 | /** @brief Gets the value associated with the given object |
| 122 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 123 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 124 | * @param[in] service - Dbus service name. |
| 125 | * @param[in] objPath - Dbus object path. |
| 126 | * @param[in] interface - Dbus interface. |
| 127 | * @param[in] property - name of the property. |
| 128 | * @return On success returns the value of the property. |
| 129 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 130 | Value getDbusProperty(sdbusplus::bus::bus& bus, |
| 131 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 132 | const std::string& objPath, |
| 133 | const std::string& interface, |
| 134 | const std::string& property); |
| 135 | |
| 136 | /** @brief Gets all the properties associated with the given object |
| 137 | * and the interface. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 138 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 139 | * @param[in] service - Dbus service name. |
| 140 | * @param[in] objPath - Dbus object path. |
| 141 | * @param[in] interface - Dbus interface. |
| 142 | * @return On success returns the map of name value pair. |
| 143 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 144 | PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus, |
| 145 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 146 | const std::string& objPath, |
| 147 | const std::string& interface); |
| 148 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 149 | /** @brief Gets all managed objects associated with the given object |
| 150 | * path and service. |
| 151 | * @param[in] bus - D-Bus Bus Object. |
| 152 | * @param[in] service - D-Bus service name. |
| 153 | * @param[in] objPath - D-Bus object path. |
| 154 | * @return On success returns the map of name value pair. |
| 155 | */ |
| 156 | ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus, |
| 157 | const std::string& service, |
| 158 | const std::string& objPath); |
| 159 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 160 | /** @brief Sets the property value of the given object. |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 161 | * @param[in] bus - DBUS Bus Object. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 162 | * @param[in] service - Dbus service name. |
| 163 | * @param[in] objPath - Dbus object path. |
| 164 | * @param[in] interface - Dbus interface. |
| 165 | * @param[in] property - name of the property. |
| 166 | * @param[in] value - value which needs to be set. |
| 167 | */ |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 168 | void setDbusProperty(sdbusplus::bus::bus& bus, |
| 169 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 170 | const std::string& objPath, |
| 171 | const std::string& interface, |
| 172 | const std::string& property, |
| 173 | const Value& value); |
| 174 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 175 | /** @brief Gets all the dbus objects from the given service root |
| 176 | * which matches the object identifier. |
| 177 | * @param[in] bus - DBUS Bus Object. |
| 178 | * @param[in] serviceRoot - Service root path. |
| 179 | * @param[in] interface - Dbus interface. |
| 180 | * @param[in] match - Identifier for a path. |
| 181 | * @returns map of object path and service info. |
| 182 | */ |
| 183 | ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus, |
| 184 | const std::string& serviceRoot, |
| 185 | const std::string& interface, |
| 186 | const std::string& match); |
| 187 | |
| 188 | /** @brief Deletes all the dbus objects from the given service root |
| 189 | which matches the object identifier. |
| 190 | * @param[in] bus - DBUS Bus Object. |
| 191 | * @param[in] serviceRoot - Service root path. |
| 192 | * @param[in] interface - Dbus interface. |
| 193 | * @param[in] match - Identifier for object. |
| 194 | */ |
| 195 | void deleteAllDbusObjects(sdbusplus::bus::bus& bus, |
| 196 | const std::string& serviceRoot, |
| 197 | const std::string& interface, |
| 198 | const std::string& match = {}); |
| 199 | |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 200 | /** @brief Gets the ancestor objects of the given object |
| 201 | which implements the given interface. |
| 202 | * @param[in] bus - Dbus bus object. |
| 203 | * @param[in] path - Child Dbus object path. |
| 204 | * @param[in] interfaces - Dbus interface list. |
| 205 | * @return map of object path and service info. |
| 206 | */ |
| 207 | ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, |
| 208 | const std::string& path, |
| 209 | InterfaceList&& interfaces); |
| 210 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 211 | namespace method_no_args |
| 212 | { |
| 213 | |
| 214 | /** @brief Calls the Dbus method which waits for response. |
| 215 | * @param[in] bus - DBUS Bus Object. |
| 216 | * @param[in] service - Dbus service name. |
| 217 | * @param[in] objPath - Dbus object path. |
| 218 | * @param[in] interface - Dbus interface. |
| 219 | * @param[in] method - Dbus method. |
| 220 | */ |
| 221 | void callDbusMethod(sdbusplus::bus::bus& bus, |
| 222 | const std::string& service, |
| 223 | const std::string& objPath, |
| 224 | const std::string& interface, |
| 225 | const std::string& method); |
| 226 | |
| 227 | } //namespace method_no_args |
| 228 | |
| 229 | namespace network |
| 230 | { |
| 231 | |
| 232 | constexpr auto ROOT = "/xyz/openbmc_project/network"; |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 233 | constexpr auto SERVICE = "xyz.openbmc_project.Network"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 234 | constexpr auto IP_TYPE = "ipv4"; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 235 | constexpr auto IPV4_PREFIX = "169.254"; |
| 236 | constexpr auto IPV6_PREFIX = "fe80"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 237 | constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP"; |
| 238 | constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress"; |
| 239 | constexpr auto SYSTEMCONFIG_INTERFACE = "xyz.openbmc_project.Network.SystemConfiguration"; |
| 240 | constexpr auto ETHERNET_INTERFACE = "xyz.openbmc_project.Network.EthernetInterface"; |
| 241 | constexpr auto IP_CREATE_INTERFACE = "xyz.openbmc_project.Network.IP.Create"; |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 242 | constexpr auto VLAN_CREATE_INTERFACE = "xyz.openbmc_project.Network.VLAN.Create"; |
| 243 | constexpr auto VLAN_INTERFACE = "xyz.openbmc_project.Network.VLAN"; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 244 | |
| 245 | /* @brief converts the given subnet into prefix notation. |
| 246 | * @param[in] addressFamily - IP address family(AF_INET/AF_INET6). |
| 247 | * @param[in] mask - Subnet Mask. |
| 248 | * @returns prefix. |
| 249 | */ |
| 250 | uint8_t toPrefix(int addressFamily, const std::string& subnetMask); |
| 251 | |
| 252 | |
| 253 | /** @brief Sets the ip on the system. |
| 254 | * @param[in] bus - DBUS Bus Object. |
| 255 | * @param[in] service - Dbus service name. |
| 256 | * @param[in] objPath - Dbus object path. |
| 257 | * @param[in] protocolType - Protocol type |
| 258 | * @param[in] ipaddress - IPaddress. |
| 259 | * @param[in] prefix - Prefix length. |
| 260 | */ |
| 261 | void createIP(sdbusplus::bus::bus& bus, |
| 262 | const std::string& service, |
| 263 | const std::string& objPath, |
| 264 | const std::string& protocolType, |
| 265 | const std::string& ipaddress, |
| 266 | uint8_t prefix); |
| 267 | |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 268 | /** @brief Creates the VLAN on the given interface. |
| 269 | * @param[in] bus - DBUS Bus Object. |
| 270 | * @param[in] service - Dbus service name. |
| 271 | * @param[in] objPath - Dbus object path. |
| 272 | * @param[in] interface - EthernetInterface. |
| 273 | * @param[in] vlanID - Vlan ID. |
| 274 | */ |
| 275 | void createVLAN(sdbusplus::bus::bus& bus, |
| 276 | const std::string& service, |
| 277 | const std::string& objPath, |
| 278 | const std::string& interface, |
| 279 | uint32_t vlanID); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 280 | |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 281 | /** @brief Gets the vlan id from the given object path. |
| 282 | * @param[in] path - Dbus object path. |
| 283 | */ |
| 284 | uint32_t getVLAN(const std::string& path); |
| 285 | |
| 286 | } //namespace network |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 287 | } // namespace ipmi |
| 288 | |
| 289 | |