blob: 45c8a51f58fa05dd4dbacb31a1625de2addfcbfd [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) :
Patrick Williams06f265f2024-08-16 15:19:49 -040021 ioc_(ioc), bus_(bus), objServer_(objServer)
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020022 {
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) {
Patrick Williams06f265f2024-08-16 15:19:49 -040026 demo.register_property_r<std::string>(
27 propertyGrettingName, sdbusplus::vtable::property_::const_,
28 [this](const auto&) { return greetings_; });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +020029
Patrick Williams06f265f2024-08-16 15:19:49 -040030 demo.register_property_rw<std::string>(
31 propertyGoodbyesName,
32 sdbusplus::vtable::property_::emits_change,
33 [this](const auto& newPropertyValue, const auto&) {
34 goodbyes_ = newPropertyValue;
35 return true;
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) {
Patrick Williams06f265f2024-08-16 15:19:49 -040060 if (ec)
61 {
62 std::cout
63 << "As expected failed to getProperty with wrong type: "
64 << ec << "\n";
65 return;
66 }
Ed Tanous95874d92021-02-19 17:14:27 -080067
Patrick Williams06f265f2024-08-16 15:19:49 -040068 std::cerr << "Error: it was expected to fail getProperty due "
69 "to wrong type\n";
70 ++fatalErrors_;
71 });
Krzysztof Grobelny807419d2020-09-28 11:39:22 +020072 }
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) {
Patrick Williams06f265f2024-08-16 15:19:49 -040080 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) {
Patrick Williams06f265f2024-08-16 15:19:49 -040092 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) {
Patrick Williams06f265f2024-08-16 15:19:49 -0400107 if (ec)
108 {
109 std::cout
110 << "As expected, failed to set greetings property: "
111 << ec << "\n";
112 return;
113 }
Ed Tanous95874d92021-02-19 17:14:27 -0800114
Patrick Williams06f265f2024-08-16 15:19:49 -0400115 std::cout
116 << "Error: it was expected to fail to change greetings\n";
117 ++fatalErrors_;
118 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200119
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) {
Patrick Williams06f265f2024-08-16 15:19:49 -0400124 if (ec)
125 {
126 std::cout
127 << "Error: it supposed to be ok to change goodbyes "
128 "property: "
129 << ec << "\n";
130 ++fatalErrors_;
131 return;
132 }
133 std::cout << "Changed goodbyes property as expected\n";
134 boost::asio::post(ioc_, [this] { asyncReadProperties(); });
135 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200136 }
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
Patrick Williams06f265f2024-08-16 15:19:49 -0400161 signals.async_wait([&ioc](const boost::system::error_code&, const int&) {
162 ioc.stop();
163 });
Krzysztof Grobelny2be0e172020-07-27 11:12:07 +0200164
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}