Skip to content

Instantly share code, notes, and snippets.

@williamspatrick
Last active October 19, 2016 19:03
Show Gist options
  • Save williamspatrick/9f4d38e1ec84aa0b803dc18507aac460 to your computer and use it in GitHub Desktop.
Save williamspatrick/9f4d38e1ec84aa0b803dc18507aac460 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sdbusplus/server.hpp>
#include <net/poettering/Calculator/server.hpp>
using Calculator_inherit = sdbusplus::server::object_t<
sdbusplus::server::net::poettering::Calculator>;
struct Calculator : Calculator_inherit
{
Calculator(sdbusplus::bus::bus& bus, const char* path) :
Calculator_inherit(bus, path) {}
int64_t multiply(int64_t x, int64_t y) override
{
return lastResult(x*y);
}
int64_t divide(int64_t x, int64_t y) override
{
return lastResult(x/y);
}
void clear() override
{
auto v = lastResult();
lastResult(0);
cleared(v);
return;
}
};
int main()
{
auto b = sdbusplus::bus::new_default();
sdbusplus::server::manager_t m{b, "/"};
b.request_name("net.poettering.Calculator");
Calculator c{b, "/net/poettering/Calculator"};
while(1)
{
b.process_discard();
b.wait();
}
return 0;
}
#include <sdbusplus/server.hpp>
#include <net/poettering/Calculator/server.hpp>
namespace sdbusplus
{
namespace server
{
namespace net
{
namespace poettering
{
Calculator::Calculator(bus::bus& bus, const char* path)
: _net_poettering_Calculator_interface(
bus, path, _interface, _vtable, this)
{
}
int Calculator::_callback_Multiply(
sd_bus_message* msg, void* context, sd_bus_error* error)
{
auto m = message::message(sd_bus_message_ref(msg));
int64_t x{};
int64_t y{};
m.read(x, y);
auto o = static_cast<Calculator*>(context);
auto r = o->multiply(x, y);
auto reply = m.new_method_return();
reply.append(std::move(r));
reply.method_return();
return 0;
}
namespace details
{
namespace Calculator
{
static const auto _param_Multiply =
utility::tuple_to_array(message::types::type_id<
int64_t, int64_t>());
static const auto _return_Multiply =
utility::tuple_to_array(message::types::type_id<
int64_t>());
}
}
int Calculator::_callback_Divide(
sd_bus_message* msg, void* context, sd_bus_error* error)
{
auto m = message::message(sd_bus_message_ref(msg));
int64_t x{};
int64_t y{};
m.read(x, y);
auto o = static_cast<Calculator*>(context);
auto r = o->divide(x, y);
auto reply = m.new_method_return();
reply.append(std::move(r));
reply.method_return();
return 0;
}
namespace details
{
namespace Calculator
{
static const auto _param_Divide =
utility::tuple_to_array(message::types::type_id<
int64_t, int64_t>());
static const auto _return_Divide =
utility::tuple_to_array(message::types::type_id<
int64_t>());
}
}
int Calculator::_callback_Clear(
sd_bus_message* msg, void* context, sd_bus_error* error)
{
auto m = message::message(sd_bus_message_ref(msg));
auto o = static_cast<Calculator*>(context);
o->clear();
auto reply = m.new_method_return();
// No data to append on reply.
reply.method_return();
return 0;
}
namespace details
{
namespace Calculator
{
static const auto _param_Clear =
utility::tuple_to_array(std::make_tuple('\0'));
static const auto _return_Clear =
utility::tuple_to_array(std::make_tuple('\0'));
}
}
void Calculator::cleared(
int64_t unnamed)
{
auto& i = _net_poettering_Calculator_interface;
auto m = i.new_signal("Cleared");
m.append(unnamed);
m.signal_send();
}
namespace details
{
namespace Calculator
{
static const auto _signal_Cleared =
utility::tuple_to_array(message::types::type_id<
int64_t>());
}
}
int64_t Calculator::lastResult() const
{
return _lastResult;
}
int Calculator::_callback_get_LastResult(
sd_bus* bus, const char* path, const char* interface,
const char* property, sd_bus_message* reply, void* context,
sd_bus_error* error)
{
auto m = message::message(sd_bus_message_ref(reply));
auto o = static_cast<Calculator*>(context);
m.append(o->lastResult());
return 0;
}
int64_t Calculator::lastResult(int64_t value)
{
if (_lastResult != value)
{
_lastResult = value;
_net_poettering_Calculator_interface.property_changed("LastResult");
}
return _lastResult;
}
int Calculator::_callback_set_LastResult(
sd_bus* bus, const char* path, const char* interface,
const char* property, sd_bus_message* value, void* context,
sd_bus_error* error)
{
auto m = message::message(sd_bus_message_ref(value));
auto o = static_cast<Calculator*>(context);
decltype(_lastResult) v{};
m.read(v);
o->lastResult(v);
return 0;
}
namespace details
{
namespace Calculator
{
static const auto _property_LastResult =
utility::tuple_to_array(message::types::type_id<
int64_t>());
}
}
const vtable::vtable_t Calculator::_vtable[] = {
vtable::start(),
vtable::method("Multiply",
details::Calculator::_param_Multiply
.data(),
details::Calculator::_return_Multiply
.data(),
_callback_Multiply),
vtable::method("Divide",
details::Calculator::_param_Divide
.data(),
details::Calculator::_return_Divide
.data(),
_callback_Divide),
vtable::method("Clear",
details::Calculator::_param_Clear
.data(),
details::Calculator::_return_Clear
.data(),
_callback_Clear),
vtable::signal("Cleared",
details::Calculator::_signal_Cleared
.data()),
vtable::property("LastResult",
details::Calculator::_property_LastResult
.data(),
_callback_get_LastResult,
_callback_set_LastResult,
vtable::property_::emits_change),
vtable::end()
};
} // namespace poettering
} // namespace net
} // namespace server
} // namespace sdbusplus
description: >
An example interface originally described as part of the announcment
of new sd-bus interfaces at:
http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html
methods:
- name: Multiply
description: >
Multiplies two integers 'x' and 'y' and returns the result.
parameters:
- name: x
type: int64_t
description: >
The first integer to multiply.
- name: y
type: int64_t
description: >
The second integer to multiply.
default: 1
returns:
- name: z
type: int64_t
description: >
The result of (x*y).
- name: Divide
description: >
Divides two integers 'x' and 'y' and returns the result.
parameters:
- name: x
type: int64_t
description: >
The first integer to divide.
- name: y
type: int64_t
description: >
The second integer to divide.
default: 1
returns:
- name: z
type: int64_t
description: >
The result of (x/y).
errors:
- self.DivisionByZero
- name: Clear
description: >
Reset the LastResult property to zero.
properties:
- name: LastResult
type: int64_t
default: 0
description: >
The result of the most recent calculation.
signals:
- name: Cleared
description: >
Signal indicating the LastReset property has been set to zero by the
'Clear' method.
properties:
- type: int64_t
description: >
Value of LastReset prior to Clear.

net.poettering.Calculator

An example interface originally described as part of the announcment of new sd-bus interfaces at: http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html

Methods

Multiply

Multiplies two integers 'x' and 'y' and returns the result.

Parameters and Returns

direction name type description
in x int64_t The first integer to multiply.
in y int64_t The second integer to multiply.
out z int64_t The result of (x*y).

Divide

Divides two integers 'x' and 'y' and returns the result.

Parameters and Returns

direction name type description
in x int64_t The first integer to divide.
in y int64_t The second integer to divide.
out z int64_t The result of (x/y).

Clear

Reset the LastResult property to zero.

Properties

name type description
LastResult int64_t The result of the most recent calculation.

Signals

Cleared

Signal indicating the LastReset property has been set to zero by the 'Clear' method.

Properties

name type description
unnamed int64_t Value of LastReset prior to Clear.
#pragma once
#include <tuple>
#include <systemd/sd-bus.h>
#include <sdbusplus/server.hpp>
namespace sdbusplus
{
namespace server
{
namespace net
{
namespace poettering
{
class Calculator
{
public:
/* Define all of the basic class operations:
* Not allowed:
* - Default constructor to avoid nullptrs.
* - Copy operations due to internal unique_ptr.
* Allowed:
* - Move operations.
* - Destructor.
*/
Calculator() = delete;
Calculator(const Calculator&) = delete;
Calculator& operator=(const Calculator&) = delete;
Calculator(Calculator&&) = default;
Calculator& operator=(Calculator&&) = default;
virtual ~Calculator() = default;
/** @brief Constructor to put object onto bus at a dbus path.
* @param[in] bus - Bus to attach to.
* @param[in] path - Path to attach at.
*/
Calculator(bus::bus& bus, const char* path);
/** @brief Implementation for Multiply
* Multiplies two integers 'x' and 'y' and returns the result.
*
* @param[in] x - The first integer to multiply.
* @param[in] y - The second integer to multiply.
*
* @return z[int64_t] - The result of (x*y).
*/
virtual int64_t multiply(
int64_t x,
int64_t y) = 0;
/** @brief Implementation for Divide
* Divides two integers 'x' and 'y' and returns the result.
*
* @param[in] x - The first integer to divide.
* @param[in] y - The second integer to divide.
*
* @return z[int64_t] - The result of (x/y).
*/
virtual int64_t divide(
int64_t x,
int64_t y) = 0;
/** @brief Implementation for Clear
* Reset the LastResult property to zero.
*/
virtual void clear(
) = 0;
/** @brief Send signal 'Cleared'
*
* Signal indicating the LastReset property has been set to zero by the 'Clear' method.
*
* @param[in] unnamed - Value of LastReset prior to Clear.
*/
void cleared(
int64_t unnamed);
/** Get value of LastResult */
virtual int64_t lastResult() const;
/** Set value of LastResult */
virtual int64_t lastResult(int64_t value);
private:
/** @brief sd-bus callback for Multiply
*/
static int _callback_Multiply(
sd_bus_message*, void*, sd_bus_error*);
/** @brief sd-bus callback for Divide
*/
static int _callback_Divide(
sd_bus_message*, void*, sd_bus_error*);
/** @brief sd-bus callback for Clear
*/
static int _callback_Clear(
sd_bus_message*, void*, sd_bus_error*);
/** @brief sd-bus callback for get-property 'LastResult' */
static int _callback_get_LastResult(
sd_bus*, const char*, const char*, const char*,
sd_bus_message*, void*, sd_bus_error*);
/** @brief sd-bus callback for set-property 'LastResult' */
static int _callback_set_LastResult(
sd_bus*, const char*, const char*, const char*,
sd_bus_message*, void*, sd_bus_error*);
static constexpr auto _interface = "net.poettering.Calculator";
static const vtable::vtable_t _vtable[];
interface::interface _net_poettering_Calculator_interface;
int64_t _lastResult{};
};
} // namespace poettering
} // namespace net
} // namespace server
} // namespace sdbusplus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment