blob: 339c0ce689439a6676083bd374e749125815c7df [file] [log] [blame]
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +02001#include <boost/asio.hpp>
2#include <sdbusplus/asio/connection.hpp>
3#include <sdbusplus/asio/object_server.hpp>
Krzysztof Grobelny807419d2020-09-28 11:39:22 +02004#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +02005#include <sdbusplus/bus.hpp>
6
7#include <iostream>
8
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +01009const std::string demoServiceName = "demo.service";
10const std::string demoObjectPath = "/xyz/demo";
11const std::string demoInterfaceName = "xyz.demo";
12const std::string propertyGrettingName = "Greetings";
13const std::string propertyGoodbyesName = "Goodbyes";
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020014
15class Application
16{
17 public:
18 Application(boost::asio::io_context& ioc, sdbusplus::asio::connection& bus,
19 sdbusplus::asio::object_server& objServer) :
20 ioc_(ioc),
21 bus_(bus), objServer_(objServer)
22 {
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020023 demo_ = objServer_.add_unique_interface(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010024 demoObjectPath, demoInterfaceName,
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020025 [this](sdbusplus::asio::dbus_interface& demo) {
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010026 demo.register_property_r<std::string>(
27 propertyGrettingName, sdbusplus::vtable::property_::const_,
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020028 [this](const auto&) { return greetings_; });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020029
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010030 demo.register_property_rw<std::string>(
31 propertyGoodbyesName,
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020032 sdbusplus::vtable::property_::emits_change,
33 [this](const auto& newPropertyValue, const auto&) {
34 goodbyes_ = newPropertyValue;
35 return 1;
36 },
37 [this](const auto&) { return goodbyes_; });
38 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020039 }
40
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020041 uint32_t fatalErrors() const
42 {
43 return fatalErrors_;
44 }
45
46 auto getFailed()
47 {
48 return [this](boost::system::error_code error) {
49 std::cerr << "Error: getProperty failed " << error << "\n";
50 ++fatalErrors_;
51 };
52 }
53
54 void asyncReadPropertyWithIncorrectType()
55 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010056 sdbusplus::asio::getProperty<uint32_t>(
57 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
58 propertyGrettingName,
59 [this](boost::system::error_code ec, uint32_t) {
60 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -080061 {
62 std::cout
63 << "As expected failed to getProperty with wrong type: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010064 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -080065 return;
66 }
67
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020068 std::cerr << "Error: it was expected to fail getProperty due "
69 "to wrong type\n";
70 ++fatalErrors_;
71 });
72 }
73
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020074 void asyncReadProperties()
75 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010076 sdbusplus::asio::getProperty<std::string>(
77 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
78 propertyGrettingName,
Ed Tanous95874d92021-02-19 17:14:27 -080079 [this](boost::system::error_code ec, std::string value) {
80 if (ec)
81 {
82 getFailed();
83 return;
84 }
85 std::cout << "Greetings value is: " << value << "\n";
86 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020087
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010088 sdbusplus::asio::getProperty<std::string>(
89 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
90 propertyGoodbyesName,
Ed Tanous95874d92021-02-19 17:14:27 -080091 [this](boost::system::error_code ec, std::string value) {
92 if (ec)
93 {
94 getFailed();
95 return;
96 }
97 std::cout << "Goodbyes value is: " << value << "\n";
98 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020099 }
100
101 void asyncChangeProperty()
102 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100103 sdbusplus::asio::setProperty(
104 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
105 propertyGrettingName, "Hi, hey, hello",
106 [this](const boost::system::error_code& ec) {
107 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -0800108 {
109 std::cout
110 << "As expected, failed to set greetings property: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100111 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -0800112 return;
113 }
114
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200115 std::cout
116 << "Error: it was expected to fail to change greetings\n";
117 ++fatalErrors_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200118 });
119
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100120 sdbusplus::asio::setProperty(
121 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
122 propertyGoodbyesName, "Bye bye",
123 [this](const boost::system::error_code& ec) {
124 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -0800125 {
126 std::cout
127 << "Error: it supposed to be ok to change goodbyes "
128 "property: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100129 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -0800130 ++fatalErrors_;
131 return;
132 }
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200133 std::cout << "Changed goodbyes property as expected\n";
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200134 boost::asio::post(ioc_, [this] { asyncReadProperties(); });
135 });
136 }
137
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200138 void syncChangeGoodbyes(std::string_view value)
139 {
140 goodbyes_ = value;
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100141 demo_->signal_property(propertyGoodbyesName);
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200142 }
143
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200144 private:
145 boost::asio::io_context& ioc_;
146 sdbusplus::asio::connection& bus_;
147 sdbusplus::asio::object_server& objServer_;
148
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200149 std::unique_ptr<sdbusplus::asio::dbus_interface> demo_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200150 std::string greetings_ = "Hello";
151 std::string goodbyes_ = "Bye";
152
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200153 uint32_t fatalErrors_ = 0u;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200154};
155
156int main(int, char**)
157{
158 boost::asio::io_context ioc;
159 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
160
161 signals.async_wait(
162 [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); });
163
164 auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
165 auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
166
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100167 bus->request_name(demoServiceName.c_str());
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200168
169 Application app(ioc, *bus, *objServer);
170
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200171 app.syncChangeGoodbyes("Good bye");
172
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200173 boost::asio::post(ioc,
174 [&app] { app.asyncReadPropertyWithIncorrectType(); });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200175 boost::asio::post(ioc, [&app] { app.asyncReadProperties(); });
176 boost::asio::post(ioc, [&app] { app.asyncChangeProperty(); });
177
178 ioc.run();
179
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200180 std::cout << "Fatal errors count: " << app.fatalErrors() << "\n";
181
182 return app.fatalErrors();
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200183}