blob: c22231a69980d42cb55974f30dde3f7023dbde73 [file] [log] [blame]
Ed Tanousb65dc1c2023-03-07 17:41:57 -08001#include <boost/asio/io_context.hpp>
2#include <boost/asio/signal_set.hpp>
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +02003#include <sdbusplus/asio/connection.hpp>
4#include <sdbusplus/asio/object_server.hpp>
Krzysztof Grobelny807419d2020-09-28 11:39:22 +02005#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +02006#include <sdbusplus/bus.hpp>
7
8#include <iostream>
9
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010010const std::string demoServiceName = "demo.service";
11const std::string demoObjectPath = "/xyz/demo";
12const std::string demoInterfaceName = "xyz.demo";
13const std::string propertyGrettingName = "Greetings";
14const std::string propertyGoodbyesName = "Goodbyes";
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020015
16class Application
17{
18 public:
19 Application(boost::asio::io_context& ioc, sdbusplus::asio::connection& bus,
20 sdbusplus::asio::object_server& objServer) :
21 ioc_(ioc),
22 bus_(bus), objServer_(objServer)
23 {
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020024 demo_ = objServer_.add_unique_interface(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010025 demoObjectPath, demoInterfaceName,
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020026 [this](sdbusplus::asio::dbus_interface& demo) {
Patrick Williamsd2149042023-05-10 07:50:13 -050027 demo.register_property_r<std::string>(
28 propertyGrettingName, sdbusplus::vtable::property_::const_,
29 [this](const auto&) { return greetings_; });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020030
Patrick Williamsd2149042023-05-10 07:50:13 -050031 demo.register_property_rw<std::string>(
32 propertyGoodbyesName,
33 sdbusplus::vtable::property_::emits_change,
34 [this](const auto& newPropertyValue, const auto&) {
35 goodbyes_ = newPropertyValue;
36 return true;
37 },
38 [this](const auto&) { return goodbyes_; });
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +020039 });
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) {
Patrick Williamsd2149042023-05-10 07:50:13 -050061 if (ec)
62 {
63 std::cout
64 << "As expected failed to getProperty with wrong type: "
65 << ec << "\n";
66 return;
67 }
Ed Tanous95874d92021-02-19 17:14:27 -080068
Patrick Williamsd2149042023-05-10 07:50:13 -050069 std::cerr << "Error: it was expected to fail getProperty due "
70 "to wrong type\n";
71 ++fatalErrors_;
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020072 });
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) {
Patrick Williamsd2149042023-05-10 07:50:13 -050081 if (ec)
82 {
83 getFailed();
84 return;
85 }
86 std::cout << "Greetings value is: " << value << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -080087 });
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) {
Patrick Williamsd2149042023-05-10 07:50:13 -050093 if (ec)
94 {
95 getFailed();
96 return;
97 }
98 std::cout << "Goodbyes value is: " << value << "\n";
Ed Tanous95874d92021-02-19 17:14:27 -080099 });
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) {
Patrick Williamsd2149042023-05-10 07:50:13 -0500108 if (ec)
109 {
110 std::cout << "As expected, failed to set greetings property: "
111 << ec << "\n";
112 return;
113 }
Ed Tanous95874d92021-02-19 17:14:27 -0800114
Patrick Williamsd2149042023-05-10 07:50:13 -0500115 std::cout << "Error: it was expected to fail to change greetings\n";
116 ++fatalErrors_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200117 });
118
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100119 sdbusplus::asio::setProperty(
120 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
121 propertyGoodbyesName, "Bye bye",
122 [this](const boost::system::error_code& ec) {
Patrick Williamsd2149042023-05-10 07:50:13 -0500123 if (ec)
124 {
125 std::cout << "Error: it supposed to be ok to change goodbyes "
126 "property: "
127 << ec << "\n";
128 ++fatalErrors_;
129 return;
130 }
131 std::cout << "Changed goodbyes property as expected\n";
132 boost::asio::post(ioc_, [this] { asyncReadProperties(); });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200133 });
134 }
135
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200136 void syncChangeGoodbyes(std::string_view value)
137 {
138 goodbyes_ = value;
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100139 demo_->signal_property(propertyGoodbyesName);
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200140 }
141
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200142 private:
143 boost::asio::io_context& ioc_;
144 sdbusplus::asio::connection& bus_;
145 sdbusplus::asio::object_server& objServer_;
146
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200147 std::unique_ptr<sdbusplus::asio::dbus_interface> demo_;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200148 std::string greetings_ = "Hello";
149 std::string goodbyes_ = "Bye";
150
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200151 uint32_t fatalErrors_ = 0u;
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200152};
153
154int main(int, char**)
155{
156 boost::asio::io_context ioc;
157 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
158
159 signals.async_wait(
160 [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); });
161
162 auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
163 auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
164
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100165 bus->request_name(demoServiceName.c_str());
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200166
167 Application app(ioc, *bus, *objServer);
168
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200169 app.syncChangeGoodbyes("Good bye");
170
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200171 boost::asio::post(ioc,
172 [&app] { app.asyncReadPropertyWithIncorrectType(); });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200173 boost::asio::post(ioc, [&app] { app.asyncReadProperties(); });
174 boost::asio::post(ioc, [&app] { app.asyncChangeProperty(); });
175
176 ioc.run();
177
Krzysztof Grobelny807419d2020-09-28 11:39:22 +0200178 std::cout << "Fatal errors count: " << app.fatalErrors() << "\n";
179
180 return app.fatalErrors();
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200181}