blob: 535ea66e3bab236e362f07236cc961637ab29a60 [file] [log] [blame]
Tom Josephbe703f72017-03-09 12:34:35 +05301#pragma once
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -07002#include <experimental/optional>
3#include <sdbusplus/server.hpp>
Tom Josephbe703f72017-03-09 12:34:35 +05304
Ratan Guptacc8feb42017-07-25 21:52:10 +05305#include "types.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +05306
7namespace 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 */
32class 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 III82c173a2018-05-11 16:10:12 -070064
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 IIIe47fdfb2018-03-15 17:09:28 -070071 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 IIIe47fdfb2018-03-15 17:09:28 -070080};
81
Tom Josephbe703f72017-03-09 12:34:35 +053082/**
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 */
90std::string getService(sdbusplus::bus::bus& bus,
91 const std::string& intf,
92 const std::string& path);
Ratan Guptacc8feb42017-07-25 21:52:10 +053093
94/** @brief Gets the dbus object info implementing the given interface
95 * from the given subtree.
Ratan Gupta01d4bd12017-08-07 15:53:25 +053096 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +053097 * @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 Gupta01d4bd12017-08-07 15:53:25 +0530102DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus,
103 const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530104 const std::string& subtreePath = ROOT,
105 const std::string& match = {});
106
Ratan Guptadd646202017-11-21 17:46:59 +0530107/** @brief Get the ipObject of first dbus IP object of Non-LinkLocalIPAddress
Gunnar Mills8991dd62017-10-25 17:11:29 -0500108 * type from the given subtree, if not available gets IP object of
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500109 * 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 Guptadd646202017-11-21 17:46:59 +0530114 * @return On success returns the object having objectpath and servicename.
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500115 */
Ratan Guptadd646202017-11-21 17:46:59 +0530116DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus,
117 const std::string& interface,
118 const std::string& subtreePath,
119 const std::string& match);
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500120
Ratan Guptacc8feb42017-07-25 21:52:10 +0530121/** @brief Gets the value associated with the given object
122 * and the interface.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530123 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530124 * @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 Gupta01d4bd12017-08-07 15:53:25 +0530130Value getDbusProperty(sdbusplus::bus::bus& bus,
131 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530132 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 Gupta01d4bd12017-08-07 15:53:25 +0530138 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530139 * @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 Gupta01d4bd12017-08-07 15:53:25 +0530144PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus,
145 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530146 const std::string& objPath,
147 const std::string& interface);
148
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600149/** @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 */
156ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
157 const std::string& service,
158 const std::string& objPath);
159
Ratan Guptacc8feb42017-07-25 21:52:10 +0530160/** @brief Sets the property value of the given object.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530161 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530162 * @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 Gupta01d4bd12017-08-07 15:53:25 +0530168void setDbusProperty(sdbusplus::bus::bus& bus,
169 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530170 const std::string& objPath,
171 const std::string& interface,
172 const std::string& property,
173 const Value& value);
174
Ratan Guptab8e99552017-07-27 07:07:48 +0530175/** @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 */
183ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus,
184 const std::string& serviceRoot,
185 const std::string& interface,
Vernon Mauery6f2c8cd2018-07-28 06:40:39 -0700186 const std::string& match = {});
Ratan Guptab8e99552017-07-27 07:07:48 +0530187
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 */
195void deleteAllDbusObjects(sdbusplus::bus::bus& bus,
196 const std::string& serviceRoot,
197 const std::string& interface,
198 const std::string& match = {});
199
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530200/** @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 */
207ObjectTree getAllAncestors(sdbusplus::bus::bus& bus,
208 const std::string& path,
209 InterfaceList&& interfaces);
210
James Feist1e121122018-07-31 11:44:09 -0700211/** @struct VariantToDoubleVisitor
212 * @brief Visitor to convert variants to doubles
213 * @details Performs a static cast on the underlying type
214 */
215struct VariantToDoubleVisitor
216{
217 template <typename T>
218 std::enable_if_t<std::is_arithmetic<T>::value, double>
219 operator()(const T &t) const
220 {
221 return static_cast<double>(t);
222 }
223
224 template <typename T>
225 std::enable_if_t<!std::is_arithmetic<T>::value, double>
226 operator()(const T &t) const
227 {
228 throw std::invalid_argument("Cannot translate type to double");
229 }
230};
231
Ratan Guptab8e99552017-07-27 07:07:48 +0530232namespace method_no_args
233{
234
235/** @brief Calls the Dbus method which waits for response.
236 * @param[in] bus - DBUS Bus Object.
237 * @param[in] service - Dbus service name.
238 * @param[in] objPath - Dbus object path.
239 * @param[in] interface - Dbus interface.
240 * @param[in] method - Dbus method.
241 */
242void callDbusMethod(sdbusplus::bus::bus& bus,
243 const std::string& service,
244 const std::string& objPath,
245 const std::string& interface,
246 const std::string& method);
247
248} //namespace method_no_args
249
250namespace network
251{
252
253constexpr auto ROOT = "/xyz/openbmc_project/network";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530254constexpr auto SERVICE = "xyz.openbmc_project.Network";
Ratan Guptab8e99552017-07-27 07:07:48 +0530255constexpr auto IP_TYPE = "ipv4";
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500256constexpr auto IPV4_PREFIX = "169.254";
257constexpr auto IPV6_PREFIX = "fe80";
Ratan Guptab8e99552017-07-27 07:07:48 +0530258constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
259constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
260constexpr auto SYSTEMCONFIG_INTERFACE = "xyz.openbmc_project.Network.SystemConfiguration";
261constexpr auto ETHERNET_INTERFACE = "xyz.openbmc_project.Network.EthernetInterface";
262constexpr auto IP_CREATE_INTERFACE = "xyz.openbmc_project.Network.IP.Create";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530263constexpr auto VLAN_CREATE_INTERFACE = "xyz.openbmc_project.Network.VLAN.Create";
264constexpr auto VLAN_INTERFACE = "xyz.openbmc_project.Network.VLAN";
Ratan Guptab8e99552017-07-27 07:07:48 +0530265
266/* @brief converts the given subnet into prefix notation.
267 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
268 * @param[in] mask - Subnet Mask.
269 * @returns prefix.
270 */
271uint8_t toPrefix(int addressFamily, const std::string& subnetMask);
272
273
274/** @brief Sets the ip on the system.
275 * @param[in] bus - DBUS Bus Object.
276 * @param[in] service - Dbus service name.
277 * @param[in] objPath - Dbus object path.
278 * @param[in] protocolType - Protocol type
279 * @param[in] ipaddress - IPaddress.
280 * @param[in] prefix - Prefix length.
281 */
282void createIP(sdbusplus::bus::bus& bus,
283 const std::string& service,
284 const std::string& objPath,
285 const std::string& protocolType,
286 const std::string& ipaddress,
287 uint8_t prefix);
288
Ratan Gupta533d03b2017-07-30 10:39:22 +0530289/** @brief Creates the VLAN on the given interface.
290 * @param[in] bus - DBUS Bus Object.
291 * @param[in] service - Dbus service name.
292 * @param[in] objPath - Dbus object path.
293 * @param[in] interface - EthernetInterface.
294 * @param[in] vlanID - Vlan ID.
295 */
296void createVLAN(sdbusplus::bus::bus& bus,
297 const std::string& service,
298 const std::string& objPath,
299 const std::string& interface,
300 uint32_t vlanID);
Ratan Guptab8e99552017-07-27 07:07:48 +0530301
Ratan Gupta533d03b2017-07-30 10:39:22 +0530302/** @brief Gets the vlan id from the given object path.
303 * @param[in] path - Dbus object path.
304 */
305uint32_t getVLAN(const std::string& path);
306
307} //namespace network
Tom Josephbe703f72017-03-09 12:34:35 +0530308} // namespace ipmi
309
310