blob: a100718d589eb799c1b4ce9450dfda91065dfd5c [file] [log] [blame]
Tom Josephbe703f72017-03-09 12:34:35 +05301#pragma once
Patrick Venture0b02be92018-08-31 11:55:55 -07002#include "types.hpp"
3
Vernon Mauery54012502018-11-07 10:17:31 -08004#include <optional>
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -07005#include <sdbusplus/server.hpp>
Tom Josephbe703f72017-03-09 12:34:35 +05306
Tom Josephbe703f72017-03-09 12:34:35 +05307namespace ipmi
8{
9
Ratan Guptacc8feb42017-07-25 21:52:10 +053010constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper";
11constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper";
12constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper";
13
14constexpr auto ROOT = "/";
15constexpr auto HOST_MATCH = "host0";
Ratan Guptacc8feb42017-07-25 21:52:10 +053016
Ratan Guptab8e99552017-07-27 07:07:48 +053017constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
18constexpr auto DELETE_INTERFACE = "xyz.openbmc_project.Object.Delete";
Ratan Guptacc8feb42017-07-25 21:52:10 +053019
20constexpr auto METHOD_GET = "Get";
21constexpr auto METHOD_GET_ALL = "GetAll";
22constexpr auto METHOD_SET = "Set";
23
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070024/** @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 */
Patrick Venture0b02be92018-08-31 11:55:55 -070032class ServiceCache
33{
34 public:
35 /** @brief Creates a new service cache for the given interface
36 * and path.
37 *
38 * @param[in] intf - The interface used for each lookup
39 * @param[in] path - The path used for each lookup
40 */
41 ServiceCache(const std::string& intf, const std::string& path);
42 ServiceCache(std::string&& intf, std::string&& path);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070043
Patrick Venture0b02be92018-08-31 11:55:55 -070044 /** @brief Gets the service name from the cache or does in a
45 * lookup when invalid.
46 *
47 * @param[in] bus - The bus associated with and used for looking
48 * up the service.
49 */
50 const std::string& getService(sdbusplus::bus::bus& bus);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070051
Patrick Venture0b02be92018-08-31 11:55:55 -070052 /** @brief Invalidates the current service name */
53 void invalidate();
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070054
Patrick Venture0b02be92018-08-31 11:55:55 -070055 /** @brief A wrapper around sdbusplus bus.new_method_call
56 *
57 * @param[in] bus - The bus used for calling the method
58 * @param[in] intf - The interface containing the method
59 * @param[in] method - The method name
60 * @return The message containing the method call.
61 */
62 sdbusplus::message::message newMethodCall(sdbusplus::bus::bus& bus,
63 const char* intf,
64 const char* method);
William A. Kennington III82c173a2018-05-11 16:10:12 -070065
Patrick Venture0b02be92018-08-31 11:55:55 -070066 /** @brief Check to see if the current cache is valid
67 *
68 * @param[in] bus - The bus used for the service lookup
69 * @return True if the cache is valid false otherwise.
70 */
71 bool isValid(sdbusplus::bus::bus& bus) const;
72
73 private:
74 /** @brief DBUS interface provided by the service */
75 const std::string intf;
76 /** @brief DBUS path provided by the service */
77 const std::string path;
78 /** @brief The name of the service if valid */
Vernon Mauery54012502018-11-07 10:17:31 -080079 std::optional<std::string> cachedService;
Patrick Venture0b02be92018-08-31 11:55:55 -070080 /** @brief The name of the bus used in the service lookup */
Vernon Mauery54012502018-11-07 10:17:31 -080081 std::optional<std::string> cachedBusName;
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070082};
83
Tom Josephbe703f72017-03-09 12:34:35 +053084/**
85 * @brief Get the DBUS Service name for the input dbus path
86 *
87 * @param[in] bus - DBUS Bus Object
88 * @param[in] intf - DBUS Interface
89 * @param[in] path - DBUS Object Path
90 *
91 */
Patrick Venture0b02be92018-08-31 11:55:55 -070092std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Tom Josephbe703f72017-03-09 12:34:35 +053093 const std::string& path);
Ratan Guptacc8feb42017-07-25 21:52:10 +053094
95/** @brief Gets the dbus object info implementing the given interface
96 * from the given subtree.
Ratan Gupta01d4bd12017-08-07 15:53:25 +053097 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +053098 * @param[in] interface - Dbus interface.
99 * @param[in] subtreePath - subtree from where the search should start.
100 * @param[in] match - identifier for object.
101 * @return On success returns the object having objectpath and servicename.
102 */
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530103DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus,
104 const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530105 const std::string& subtreePath = ROOT,
106 const std::string& match = {});
107
Ratan Guptadd646202017-11-21 17:46:59 +0530108/** @brief Get the ipObject of first dbus IP object of Non-LinkLocalIPAddress
Gunnar Mills8991dd62017-10-25 17:11:29 -0500109 * type from the given subtree, if not available gets IP object of
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500110 * LinkLocalIPAddress type.
111 * @param[in] bus - DBUS Bus Object.
112 * @param[in] interface - Dbus interface.
113 * @param[in] subtreePath - subtree from where the search should start.
114 * @param[in] match - identifier for object.
Ratan Guptadd646202017-11-21 17:46:59 +0530115 * @return On success returns the object having objectpath and servicename.
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500116 */
Ratan Guptadd646202017-11-21 17:46:59 +0530117DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus,
118 const std::string& interface,
119 const std::string& subtreePath,
120 const std::string& match);
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500121
Ratan Guptacc8feb42017-07-25 21:52:10 +0530122/** @brief Gets the value associated with the given object
123 * and the interface.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530124 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530125 * @param[in] service - Dbus service name.
126 * @param[in] objPath - Dbus object path.
127 * @param[in] interface - Dbus interface.
128 * @param[in] property - name of the property.
129 * @return On success returns the value of the property.
130 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700131Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
132 const std::string& objPath, const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530133 const std::string& property);
134
135/** @brief Gets all the properties associated with the given object
136 * and the interface.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530137 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530138 * @param[in] service - Dbus service name.
139 * @param[in] objPath - Dbus object path.
140 * @param[in] interface - Dbus interface.
141 * @return On success returns the map of name value pair.
142 */
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530143PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus,
144 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530145 const std::string& objPath,
146 const std::string& interface);
147
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600148/** @brief Gets all managed objects associated with the given object
149 * path and service.
150 * @param[in] bus - D-Bus Bus Object.
151 * @param[in] service - D-Bus service name.
152 * @param[in] objPath - D-Bus object path.
153 * @return On success returns the map of name value pair.
154 */
155ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700156 const std::string& service,
157 const std::string& objPath);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600158
Ratan Guptacc8feb42017-07-25 21:52:10 +0530159/** @brief Sets the property value of the given object.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530160 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530161 * @param[in] service - Dbus service name.
162 * @param[in] objPath - Dbus object path.
163 * @param[in] interface - Dbus interface.
164 * @param[in] property - name of the property.
165 * @param[in] value - value which needs to be set.
166 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700167void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
168 const std::string& objPath, const std::string& interface,
169 const std::string& property, const Value& value);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530170
Ratan Guptab8e99552017-07-27 07:07:48 +0530171/** @brief Gets all the dbus objects from the given service root
172 * which matches the object identifier.
173 * @param[in] bus - DBUS Bus Object.
174 * @param[in] serviceRoot - Service root path.
175 * @param[in] interface - Dbus interface.
176 * @param[in] match - Identifier for a path.
177 * @returns map of object path and service info.
178 */
179ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus,
180 const std::string& serviceRoot,
181 const std::string& interface,
Vernon Mauery6f2c8cd2018-07-28 06:40:39 -0700182 const std::string& match = {});
Ratan Guptab8e99552017-07-27 07:07:48 +0530183
184/** @brief Deletes all the dbus objects from the given service root
185 which matches the object identifier.
186 * @param[in] bus - DBUS Bus Object.
187 * @param[in] serviceRoot - Service root path.
188 * @param[in] interface - Dbus interface.
189 * @param[in] match - Identifier for object.
190 */
191void deleteAllDbusObjects(sdbusplus::bus::bus& bus,
192 const std::string& serviceRoot,
193 const std::string& interface,
194 const std::string& match = {});
195
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530196/** @brief Gets the ancestor objects of the given object
197 which implements the given interface.
198 * @param[in] bus - Dbus bus object.
199 * @param[in] path - Child Dbus object path.
200 * @param[in] interfaces - Dbus interface list.
201 * @return map of object path and service info.
202 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700203ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path,
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530204 InterfaceList&& interfaces);
205
James Feist1e121122018-07-31 11:44:09 -0700206/** @struct VariantToDoubleVisitor
207 * @brief Visitor to convert variants to doubles
208 * @details Performs a static cast on the underlying type
209 */
210struct VariantToDoubleVisitor
211{
212 template <typename T>
213 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 operator()(const T& t) const
James Feist1e121122018-07-31 11:44:09 -0700215 {
216 return static_cast<double>(t);
217 }
218
219 template <typename T>
220 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Venture0b02be92018-08-31 11:55:55 -0700221 operator()(const T& t) const
James Feist1e121122018-07-31 11:44:09 -0700222 {
223 throw std::invalid_argument("Cannot translate type to double");
224 }
225};
226
Ratan Guptab8e99552017-07-27 07:07:48 +0530227namespace method_no_args
228{
229
230/** @brief Calls the Dbus method which waits for response.
231 * @param[in] bus - DBUS Bus Object.
232 * @param[in] service - Dbus service name.
233 * @param[in] objPath - Dbus object path.
234 * @param[in] interface - Dbus interface.
235 * @param[in] method - Dbus method.
236 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700237void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service,
238 const std::string& objPath, const std::string& interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530239 const std::string& method);
240
Patrick Venture0b02be92018-08-31 11:55:55 -0700241} // namespace method_no_args
Ratan Guptab8e99552017-07-27 07:07:48 +0530242
243namespace network
244{
245
246constexpr auto ROOT = "/xyz/openbmc_project/network";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530247constexpr auto SERVICE = "xyz.openbmc_project.Network";
Ratan Guptab8e99552017-07-27 07:07:48 +0530248constexpr auto IP_TYPE = "ipv4";
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500249constexpr auto IPV4_PREFIX = "169.254";
250constexpr auto IPV6_PREFIX = "fe80";
Ratan Guptab8e99552017-07-27 07:07:48 +0530251constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
252constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
Patrick Venture0b02be92018-08-31 11:55:55 -0700253constexpr auto SYSTEMCONFIG_INTERFACE =
254 "xyz.openbmc_project.Network.SystemConfiguration";
255constexpr auto ETHERNET_INTERFACE =
256 "xyz.openbmc_project.Network.EthernetInterface";
Ratan Guptab8e99552017-07-27 07:07:48 +0530257constexpr auto IP_CREATE_INTERFACE = "xyz.openbmc_project.Network.IP.Create";
Patrick Venture0b02be92018-08-31 11:55:55 -0700258constexpr auto VLAN_CREATE_INTERFACE =
259 "xyz.openbmc_project.Network.VLAN.Create";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530260constexpr auto VLAN_INTERFACE = "xyz.openbmc_project.Network.VLAN";
Ratan Guptab8e99552017-07-27 07:07:48 +0530261
262/* @brief converts the given subnet into prefix notation.
263 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
264 * @param[in] mask - Subnet Mask.
265 * @returns prefix.
266 */
267uint8_t toPrefix(int addressFamily, const std::string& subnetMask);
268
Ratan Guptab8e99552017-07-27 07:07:48 +0530269/** @brief Sets the ip on the system.
270 * @param[in] bus - DBUS Bus Object.
271 * @param[in] service - Dbus service name.
272 * @param[in] objPath - Dbus object path.
273 * @param[in] protocolType - Protocol type
274 * @param[in] ipaddress - IPaddress.
275 * @param[in] prefix - Prefix length.
276 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700277void createIP(sdbusplus::bus::bus& bus, const std::string& service,
278 const std::string& objPath, const std::string& protocolType,
279 const std::string& ipaddress, uint8_t prefix);
Ratan Guptab8e99552017-07-27 07:07:48 +0530280
Ratan Gupta533d03b2017-07-30 10:39:22 +0530281/** @brief Creates the VLAN on the given interface.
282 * @param[in] bus - DBUS Bus Object.
283 * @param[in] service - Dbus service name.
284 * @param[in] objPath - Dbus object path.
285 * @param[in] interface - EthernetInterface.
286 * @param[in] vlanID - Vlan ID.
287 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700288void createVLAN(sdbusplus::bus::bus& bus, const std::string& service,
289 const std::string& objPath, const std::string& interface,
Ratan Gupta533d03b2017-07-30 10:39:22 +0530290 uint32_t vlanID);
Ratan Guptab8e99552017-07-27 07:07:48 +0530291
Ratan Gupta533d03b2017-07-30 10:39:22 +0530292/** @brief Gets the vlan id from the given object path.
293 * @param[in] path - Dbus object path.
294 */
295uint32_t getVLAN(const std::string& path);
296
Patrick Venture0b02be92018-08-31 11:55:55 -0700297} // namespace network
Tom Josephbe703f72017-03-09 12:34:35 +0530298} // namespace ipmi