bus: Add chrono types for timeouts
Tested:
Builds and runs unit tests as expected.
Additionally ran against all of the package in openbmc/* and
produced no fallout as expected.
Change-Id: Id9e9a33c789666869dd0a47c065d113acb2ceea5
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/sdbusplus/bus.hpp.in b/sdbusplus/bus.hpp.in
index 6fe3914..76b1566 100644
--- a/sdbusplus/bus.hpp.in
+++ b/sdbusplus/bus.hpp.in
@@ -6,6 +6,7 @@
#include <algorithm>
#include <climits>
#include <memory>
+#include <optional>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/message.hpp>
#include <sdbusplus/sdbus.hpp>
@@ -187,10 +188,14 @@
*
* @param[in] timeout_us - Timeout in usec.
*/
- void wait(uint64_t timeout_us = ULLONG_MAX)
+ void wait(uint64_t timeout_us)
{
_intf->sd_bus_wait(_bus.get(), timeout_us);
}
+ void wait(std::optional<SdBusDuration> timeout = std::nullopt)
+ {
+ wait(timeout ? timeout->count() : UINT64_MAX);
+ }
/** @brief Process waiting dbus messages or signals. */
auto process()
@@ -285,7 +290,7 @@
*
* @return The response message.
*/
- auto call(message::message& m, uint64_t timeout_us = 0)
+ auto call(message::message& m, uint64_t timeout_us)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message* reply = nullptr;
@@ -298,13 +303,18 @@
return message::message(reply, _intf, std::false_type());
}
+ auto call(message::message& m,
+ std::optional<SdBusDuration> timeout = std::nullopt)
+ {
+ return call(m, timeout ? timeout->count() : 0);
+ }
/** @brief Perform a message call, ignoring the reply.
*
* @param[in] m - The method_call message.
* @param[in] timeout_us - The timeout for the method call.
*/
- void call_noreply(message::message& m, uint64_t timeout_us = 0)
+ void call_noreply(message::message& m, uint64_t timeout_us)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
int r = _intf->sd_bus_call(_bus.get(), m.get(), timeout_us, &error,
@@ -314,6 +324,11 @@
throw exception::SdBusError(&error, "sd_bus_call noreply");
}
}
+ auto call_noreply(message::message& m,
+ std::optional<SdBusDuration> timeout = std::nullopt)
+ {
+ return call_noreply(m, timeout ? timeout->count() : 0);
+ }
/** @brief Perform a message call, ignoring the reply and any errors
* in the dbus stack.
@@ -321,7 +336,7 @@
* @param[in] m - The method_call message.
* @param[in] timeout_us - The timeout for the method call.
*/
- void call_noreply_noerror(message::message& m, uint64_t timeout_us = 0)
+ void call_noreply_noerror(message::message& m, uint64_t timeout_us)
{
try
{
@@ -332,6 +347,11 @@
// Intentionally ignoring these sd_bus errors
}
}
+ auto call_noreply_noerror(message::message& m,
+ std::optional<SdBusDuration> timeout = std::nullopt)
+ {
+ return call_noreply_noerror(m, timeout ? timeout->count() : 0);
+ }
/** @brief Get the bus unique name. Ex: ":1.11".
*
diff --git a/sdbusplus/sdbus.hpp b/sdbusplus/sdbus.hpp
index acc5f3f..bd42033 100644
--- a/sdbusplus/sdbus.hpp
+++ b/sdbusplus/sdbus.hpp
@@ -2,10 +2,16 @@
#include <systemd/sd-bus.h>
+#include <chrono>
+
// ABC for sdbus implementation.
namespace sdbusplus
{
+// Defined by systemd taking uint64_t usec params
+using SdBusDuration =
+ std::chrono::duration<uint64_t, std::chrono::microseconds::period>;
+
// A wrapper for interfacing or testing sd-bus. This will hold methods for
// buses, messages, etc.
class SdBusInterface