blob: 09bb1b4513e73b2b4fc43fe4b67cde80a5cb52fc [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
Kun Yi5dcf41e2019-03-05 14:02:51 -08004#include <chrono>
Vernon Mauery54012502018-11-07 10:17:31 -08005#include <optional>
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -07006#include <sdbusplus/server.hpp>
Tom Josephbe703f72017-03-09 12:34:35 +05307
Tom Josephbe703f72017-03-09 12:34:35 +05308namespace ipmi
9{
10
Kun Yi5dcf41e2019-03-05 14:02:51 -080011using namespace std::literals::chrono_literals;
12
Ratan Guptacc8feb42017-07-25 21:52:10 +053013constexpr auto MAPPER_BUS_NAME = "xyz.openbmc_project.ObjectMapper";
14constexpr auto MAPPER_OBJ = "/xyz/openbmc_project/object_mapper";
15constexpr auto MAPPER_INTF = "xyz.openbmc_project.ObjectMapper";
16
17constexpr auto ROOT = "/";
18constexpr auto HOST_MATCH = "host0";
Ratan Guptacc8feb42017-07-25 21:52:10 +053019
Ratan Guptab8e99552017-07-27 07:07:48 +053020constexpr auto PROP_INTF = "org.freedesktop.DBus.Properties";
21constexpr auto DELETE_INTERFACE = "xyz.openbmc_project.Object.Delete";
Ratan Guptacc8feb42017-07-25 21:52:10 +053022
23constexpr auto METHOD_GET = "Get";
24constexpr auto METHOD_GET_ALL = "GetAll";
25constexpr auto METHOD_SET = "Set";
26
Kun Yi5dcf41e2019-03-05 14:02:51 -080027/* Use a value of 5s which aligns with BT/KCS bridged timeouts, rather
28 * than the default 25s D-Bus timeout. */
29constexpr std::chrono::microseconds IPMI_DBUS_TIMEOUT = 5s;
30
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070031/** @class ServiceCache
32 * @brief Caches lookups of service names from the object mapper.
33 * @details Most ipmi commands need to talk to other dbus daemons to perform
34 * their intended actions on the BMC. This usually means they will
35 * first look up the service name providing the interface they
36 * require. This class reduces the number of such calls by caching
37 * the lookup for a specific service.
38 */
Patrick Venture0b02be92018-08-31 11:55:55 -070039class ServiceCache
40{
41 public:
42 /** @brief Creates a new service cache for the given interface
43 * and path.
44 *
45 * @param[in] intf - The interface used for each lookup
46 * @param[in] path - The path used for each lookup
47 */
48 ServiceCache(const std::string& intf, const std::string& path);
49 ServiceCache(std::string&& intf, std::string&& path);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070050
Patrick Venture0b02be92018-08-31 11:55:55 -070051 /** @brief Gets the service name from the cache or does in a
52 * lookup when invalid.
53 *
54 * @param[in] bus - The bus associated with and used for looking
55 * up the service.
56 */
57 const std::string& getService(sdbusplus::bus::bus& bus);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070058
Patrick Venture0b02be92018-08-31 11:55:55 -070059 /** @brief Invalidates the current service name */
60 void invalidate();
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070061
Patrick Venture0b02be92018-08-31 11:55:55 -070062 /** @brief A wrapper around sdbusplus bus.new_method_call
63 *
64 * @param[in] bus - The bus used for calling the method
65 * @param[in] intf - The interface containing the method
66 * @param[in] method - The method name
67 * @return The message containing the method call.
68 */
69 sdbusplus::message::message newMethodCall(sdbusplus::bus::bus& bus,
70 const char* intf,
71 const char* method);
William A. Kennington III82c173a2018-05-11 16:10:12 -070072
Patrick Venture0b02be92018-08-31 11:55:55 -070073 /** @brief Check to see if the current cache is valid
74 *
75 * @param[in] bus - The bus used for the service lookup
76 * @return True if the cache is valid false otherwise.
77 */
78 bool isValid(sdbusplus::bus::bus& bus) const;
79
80 private:
81 /** @brief DBUS interface provided by the service */
82 const std::string intf;
83 /** @brief DBUS path provided by the service */
84 const std::string path;
85 /** @brief The name of the service if valid */
Vernon Mauery54012502018-11-07 10:17:31 -080086 std::optional<std::string> cachedService;
Patrick Venture0b02be92018-08-31 11:55:55 -070087 /** @brief The name of the bus used in the service lookup */
Vernon Mauery54012502018-11-07 10:17:31 -080088 std::optional<std::string> cachedBusName;
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -070089};
90
Tom Josephbe703f72017-03-09 12:34:35 +053091/**
92 * @brief Get the DBUS Service name for the input dbus path
93 *
94 * @param[in] bus - DBUS Bus Object
95 * @param[in] intf - DBUS Interface
96 * @param[in] path - DBUS Object Path
97 *
98 */
Patrick Venture0b02be92018-08-31 11:55:55 -070099std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Tom Josephbe703f72017-03-09 12:34:35 +0530100 const std::string& path);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530101
102/** @brief Gets the dbus object info implementing the given interface
103 * from the given subtree.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530104 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530105 * @param[in] interface - Dbus interface.
106 * @param[in] subtreePath - subtree from where the search should start.
107 * @param[in] match - identifier for object.
108 * @return On success returns the object having objectpath and servicename.
109 */
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530110DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus,
111 const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530112 const std::string& subtreePath = ROOT,
113 const std::string& match = {});
114
Ratan Guptadd646202017-11-21 17:46:59 +0530115/** @brief Get the ipObject of first dbus IP object of Non-LinkLocalIPAddress
Gunnar Mills8991dd62017-10-25 17:11:29 -0500116 * type from the given subtree, if not available gets IP object of
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500117 * LinkLocalIPAddress type.
118 * @param[in] bus - DBUS Bus Object.
119 * @param[in] interface - Dbus interface.
120 * @param[in] subtreePath - subtree from where the search should start.
121 * @param[in] match - identifier for object.
Ratan Guptadd646202017-11-21 17:46:59 +0530122 * @return On success returns the object having objectpath and servicename.
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500123 */
Ratan Guptadd646202017-11-21 17:46:59 +0530124DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus,
125 const std::string& interface,
126 const std::string& subtreePath,
127 const std::string& match);
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500128
Ratan Guptacc8feb42017-07-25 21:52:10 +0530129/** @brief Gets the value associated with the given object
130 * and the interface.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530131 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530132 * @param[in] service - Dbus service name.
133 * @param[in] objPath - Dbus object path.
134 * @param[in] interface - Dbus interface.
135 * @param[in] property - name of the property.
136 * @return On success returns the value of the property.
137 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700138Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
139 const std::string& objPath, const std::string& interface,
Kun Yi5dcf41e2019-03-05 14:02:51 -0800140 const std::string& property,
141 std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530142
143/** @brief Gets all the properties associated with the given object
144 * and the interface.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530145 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530146 * @param[in] service - Dbus service name.
147 * @param[in] objPath - Dbus object path.
148 * @param[in] interface - Dbus interface.
149 * @return On success returns the map of name value pair.
150 */
Kun Yi5dcf41e2019-03-05 14:02:51 -0800151PropertyMap
152 getAllDbusProperties(sdbusplus::bus::bus& bus, const std::string& service,
153 const std::string& objPath,
154 const std::string& interface,
155 std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530156
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600157/** @brief Gets all managed objects associated with the given object
158 * path and service.
159 * @param[in] bus - D-Bus Bus Object.
160 * @param[in] service - D-Bus service name.
161 * @param[in] objPath - D-Bus object path.
162 * @return On success returns the map of name value pair.
163 */
164ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700165 const std::string& service,
166 const std::string& objPath);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600167
Ratan Guptacc8feb42017-07-25 21:52:10 +0530168/** @brief Sets the property value of the given object.
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530169 * @param[in] bus - DBUS Bus Object.
Ratan Guptacc8feb42017-07-25 21:52:10 +0530170 * @param[in] service - Dbus service name.
171 * @param[in] objPath - Dbus object path.
172 * @param[in] interface - Dbus interface.
173 * @param[in] property - name of the property.
174 * @param[in] value - value which needs to be set.
175 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700176void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
177 const std::string& objPath, const std::string& interface,
Kun Yi5dcf41e2019-03-05 14:02:51 -0800178 const std::string& property, const Value& value,
179 std::chrono::microseconds timeout = IPMI_DBUS_TIMEOUT);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530180
Ratan Guptab8e99552017-07-27 07:07:48 +0530181/** @brief Gets all the dbus objects from the given service root
182 * which matches the object identifier.
183 * @param[in] bus - DBUS Bus Object.
184 * @param[in] serviceRoot - Service root path.
185 * @param[in] interface - Dbus interface.
186 * @param[in] match - Identifier for a path.
187 * @returns map of object path and service info.
188 */
189ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus,
190 const std::string& serviceRoot,
191 const std::string& interface,
Vernon Mauery6f2c8cd2018-07-28 06:40:39 -0700192 const std::string& match = {});
Ratan Guptab8e99552017-07-27 07:07:48 +0530193
194/** @brief Deletes all the dbus objects from the given service root
195 which matches the object identifier.
196 * @param[in] bus - DBUS Bus Object.
197 * @param[in] serviceRoot - Service root path.
198 * @param[in] interface - Dbus interface.
199 * @param[in] match - Identifier for object.
200 */
201void deleteAllDbusObjects(sdbusplus::bus::bus& bus,
202 const std::string& serviceRoot,
203 const std::string& interface,
204 const std::string& match = {});
205
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530206/** @brief Gets the ancestor objects of the given object
207 which implements the given interface.
208 * @param[in] bus - Dbus bus object.
209 * @param[in] path - Child Dbus object path.
210 * @param[in] interfaces - Dbus interface list.
211 * @return map of object path and service info.
212 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700213ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path,
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530214 InterfaceList&& interfaces);
215
James Feist1e121122018-07-31 11:44:09 -0700216/** @struct VariantToDoubleVisitor
217 * @brief Visitor to convert variants to doubles
218 * @details Performs a static cast on the underlying type
219 */
220struct VariantToDoubleVisitor
221{
222 template <typename T>
223 std::enable_if_t<std::is_arithmetic<T>::value, double>
Patrick Venture0b02be92018-08-31 11:55:55 -0700224 operator()(const T& t) const
James Feist1e121122018-07-31 11:44:09 -0700225 {
226 return static_cast<double>(t);
227 }
228
229 template <typename T>
230 std::enable_if_t<!std::is_arithmetic<T>::value, double>
Patrick Venture0b02be92018-08-31 11:55:55 -0700231 operator()(const T& t) const
James Feist1e121122018-07-31 11:44:09 -0700232 {
233 throw std::invalid_argument("Cannot translate type to double");
234 }
235};
236
Ratan Guptab8e99552017-07-27 07:07:48 +0530237namespace method_no_args
238{
239
240/** @brief Calls the Dbus method which waits for response.
241 * @param[in] bus - DBUS Bus Object.
242 * @param[in] service - Dbus service name.
243 * @param[in] objPath - Dbus object path.
244 * @param[in] interface - Dbus interface.
245 * @param[in] method - Dbus method.
246 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700247void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service,
248 const std::string& objPath, const std::string& interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530249 const std::string& method);
250
Patrick Venture0b02be92018-08-31 11:55:55 -0700251} // namespace method_no_args
Ratan Guptab8e99552017-07-27 07:07:48 +0530252
253namespace network
254{
255
256constexpr auto ROOT = "/xyz/openbmc_project/network";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530257constexpr auto SERVICE = "xyz.openbmc_project.Network";
Ratan Guptab8e99552017-07-27 07:07:48 +0530258constexpr auto IP_TYPE = "ipv4";
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500259constexpr auto IPV4_PREFIX = "169.254";
260constexpr auto IPV6_PREFIX = "fe80";
Ratan Guptab8e99552017-07-27 07:07:48 +0530261constexpr auto IP_INTERFACE = "xyz.openbmc_project.Network.IP";
262constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
Patrick Venture0b02be92018-08-31 11:55:55 -0700263constexpr auto SYSTEMCONFIG_INTERFACE =
264 "xyz.openbmc_project.Network.SystemConfiguration";
265constexpr auto ETHERNET_INTERFACE =
266 "xyz.openbmc_project.Network.EthernetInterface";
Ratan Guptab8e99552017-07-27 07:07:48 +0530267constexpr auto IP_CREATE_INTERFACE = "xyz.openbmc_project.Network.IP.Create";
Patrick Venture0b02be92018-08-31 11:55:55 -0700268constexpr auto VLAN_CREATE_INTERFACE =
269 "xyz.openbmc_project.Network.VLAN.Create";
Ratan Gupta533d03b2017-07-30 10:39:22 +0530270constexpr auto VLAN_INTERFACE = "xyz.openbmc_project.Network.VLAN";
Ratan Guptab8e99552017-07-27 07:07:48 +0530271
272/* @brief converts the given subnet into prefix notation.
273 * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
274 * @param[in] mask - Subnet Mask.
275 * @returns prefix.
276 */
277uint8_t toPrefix(int addressFamily, const std::string& subnetMask);
278
Ratan Guptab8e99552017-07-27 07:07:48 +0530279/** @brief Sets the ip on the system.
280 * @param[in] bus - DBUS Bus Object.
281 * @param[in] service - Dbus service name.
282 * @param[in] objPath - Dbus object path.
283 * @param[in] protocolType - Protocol type
284 * @param[in] ipaddress - IPaddress.
285 * @param[in] prefix - Prefix length.
286 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700287void createIP(sdbusplus::bus::bus& bus, const std::string& service,
288 const std::string& objPath, const std::string& protocolType,
289 const std::string& ipaddress, uint8_t prefix);
Ratan Guptab8e99552017-07-27 07:07:48 +0530290
Ratan Gupta533d03b2017-07-30 10:39:22 +0530291/** @brief Creates the VLAN on the given interface.
292 * @param[in] bus - DBUS Bus Object.
293 * @param[in] service - Dbus service name.
294 * @param[in] objPath - Dbus object path.
295 * @param[in] interface - EthernetInterface.
296 * @param[in] vlanID - Vlan ID.
297 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700298void createVLAN(sdbusplus::bus::bus& bus, const std::string& service,
299 const std::string& objPath, const std::string& interface,
Ratan Gupta533d03b2017-07-30 10:39:22 +0530300 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
Patrick Venture0b02be92018-08-31 11:55:55 -0700307} // namespace network
Tom Josephbe703f72017-03-09 12:34:35 +0530308} // namespace ipmi