blob: 6537aca030f48446ca7e34fec9f27985906a780b [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) {
26 demo.register_property_r(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010027 propertyGrettingName, std::string(),
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020028 sdbusplus::vtable::property_::const_,
29 [this](const auto&) { return greetings_; });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020030
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020031 demo.register_property_rw(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010032 propertyGoodbyesName, std::string(),
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020033 sdbusplus::vtable::property_::emits_change,
34 [this](const auto& newPropertyValue, const auto&) {
35 goodbyes_ = newPropertyValue;
36 return 1;
37 },
38 [this](const auto&) { return goodbyes_; });
39 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020040 }
41
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020042 uint32_t fatalErrors() const
43 {
44 return fatalErrors_;
45 }
46
47 auto getFailed()
48 {
49 return [this](boost::system::error_code error) {
50 std::cerr << "Error: getProperty failed " << error << "\n";
51 ++fatalErrors_;
52 };
53 }
54
55 void asyncReadPropertyWithIncorrectType()
56 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010057 sdbusplus::asio::getProperty<uint32_t>(
58 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
59 propertyGrettingName,
60 [this](boost::system::error_code ec, uint32_t) {
61 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -080062 {
63 std::cout
64 << "As expected failed to getProperty with wrong type: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010065 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -080066 return;
67 }
68
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020069 std::cerr << "Error: it was expected to fail getProperty due "
70 "to wrong type\n";
71 ++fatalErrors_;
72 });
73 }
74
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020075 void asyncReadProperties()
76 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010077 sdbusplus::asio::getProperty<std::string>(
78 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
79 propertyGrettingName,
Ed Tanous95874d92021-02-19 17:14:27 -080080 [this](boost::system::error_code ec, std::string value) {
81 if (ec)
82 {
83 getFailed();
84 return;
85 }
86 std::cout << "Greetings value is: " << value << "\n";
87 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020088
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010089 sdbusplus::asio::getProperty<std::string>(
90 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
91 propertyGoodbyesName,
Ed Tanous95874d92021-02-19 17:14:27 -080092 [this](boost::system::error_code ec, std::string value) {
93 if (ec)
94 {
95 getFailed();
96 return;
97 }
98 std::cout << "Goodbyes value is: " << value << "\n";
99 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200100 }
101
102 void asyncChangeProperty()
103 {
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100104 sdbusplus::asio::setProperty(
105 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
106 propertyGrettingName, "Hi, hey, hello",
107 [this](const boost::system::error_code& ec) {
108 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -0800109 {
110 std::cout
111 << "As expected, failed to set greetings property: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100112 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -0800113 return;
114 }
115
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200116 std::cout
117 << "Error: it was expected to fail to change greetings\n";
118 ++fatalErrors_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200119 });
120
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100121 sdbusplus::asio::setProperty(
122 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
123 propertyGoodbyesName, "Bye bye",
124 [this](const boost::system::error_code& ec) {
125 if (ec)
Ed Tanous95874d92021-02-19 17:14:27 -0800126 {
127 std::cout
128 << "Error: it supposed to be ok to change goodbyes "
129 "property: "
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100130 << ec << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -0800131 ++fatalErrors_;
132 return;
133 }
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200134 std::cout << "Changed goodbyes property as expected\n";
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200135 boost::asio::post(ioc_, [this] { asyncReadProperties(); });
136 });
137 }
138
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200139 void syncChangeGoodbyes(std::string_view value)
140 {
141 goodbyes_ = value;
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100142 demo_->signal_property(propertyGoodbyesName);
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200143 }
144
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200145 private:
146 boost::asio::io_context& ioc_;
147 sdbusplus::asio::connection& bus_;
148 sdbusplus::asio::object_server& objServer_;
149
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200150 std::unique_ptr<sdbusplus::asio::dbus_interface> demo_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200151 std::string greetings_ = "Hello";
152 std::string goodbyes_ = "Bye";
153
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200154 uint32_t fatalErrors_ = 0u;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200155};
156
157int main(int, char**)
158{
159 boost::asio::io_context ioc;
160 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
161
162 signals.async_wait(
163 [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); });
164
165 auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
166 auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
167
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100168 bus->request_name(demoServiceName.c_str());
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200169
170 Application app(ioc, *bus, *objServer);
171
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200172 app.syncChangeGoodbyes("Good bye");
173
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200174 boost::asio::post(ioc,
175 [&app] { app.asyncReadPropertyWithIncorrectType(); });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200176 boost::asio::post(ioc, [&app] { app.asyncReadProperties(); });
177 boost::asio::post(ioc, [&app] { app.asyncChangeProperty(); });
178
179 ioc.run();
180
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200181 std::cout << "Fatal errors count: " << app.fatalErrors() << "\n";
182
183 return app.fatalErrors();
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200184}