blob: 7b91d55b7f15a167fbb034ba8108601231a8ab8d [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 Grobelny09b88f22020-09-02 14:49:01 +02003#include <sdbusplus/asio/connection.hpp>
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +02004#include <sdbusplus/asio/object_server.hpp>
Krzysztof Grobelny807419d2020-09-28 11:39:22 +02005#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +02006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/unpack_properties.hpp>
8
9#include <iostream>
10
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010011const std::string demoServiceName = "demo.service";
12const std::string demoObjectPath = "/xyz/demo";
13const std::string demoInterfaceName = "xyz.demo";
14const std::string propertyGrettingName = "Greetings";
15const std::string propertyGoodbyesName = "Goodbyes";
16const std::string propertyValueName = "Value";
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020017
18class Application
19{
20 public:
Ed Tanous760e66a2023-01-05 10:12:00 -080021 Application(sdbusplus::asio::connection& bus,
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020022 sdbusplus::asio::object_server& objServer) :
Patrick Williams06f265f2024-08-16 15:19:49 -040023 bus_(bus), objServer_(objServer)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020024 {
Patrick Williams06f265f2024-08-16 15:19:49 -040025 demo_ =
26 objServer_.add_unique_interface(demoObjectPath, demoInterfaceName);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020027
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010028 demo_->register_property_r<std::string>(
29 propertyGrettingName, sdbusplus::vtable::property_::const_,
30 [this](const auto&) { return greetings_; });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020031
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010032 demo_->register_property_rw<std::string>(
33 propertyGoodbyesName, sdbusplus::vtable::property_::emits_change,
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020034 [this](const auto& newPropertyValue, const auto&) {
Patrick Williams06f265f2024-08-16 15:19:49 -040035 goodbyes_ = newPropertyValue;
36 return true;
37 },
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020038 [this](const auto&) { return goodbyes_; });
39
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010040 demo_->register_property_r<uint32_t>(
41 propertyValueName, sdbusplus::vtable::property_::const_,
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020042 [](const auto& value) -> uint32_t { return value; });
43
44 demo_->initialize();
45 }
46
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020047 uint32_t fatalErrors() const
48 {
49 return fatalErrors_;
50 }
51
Ed Tanous95874d92021-02-19 17:14:27 -080052 auto logSystemErrorCode(boost::system::error_code ec)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020053 {
Ed Tanous95874d92021-02-19 17:14:27 -080054 std::cerr << "Error: " << ec << "\n";
55 ++fatalErrors_;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020056 }
57
58 void logException(const std::exception& e)
59 {
60 std::cerr << "Error: " << e.what() << "\n";
61 ++fatalErrors_;
62 }
63
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010064 void logUnpackError(const sdbusplus::UnpackErrorReason reason,
65 const std::string& property)
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010066 {
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010067 std::cerr << "UnpackError: " << static_cast<int>(reason) << ", "
68 << property << "\n";
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010069 ++fatalErrors_;
70 }
71
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020072 void logExpectedException(
73 const sdbusplus::exception::UnpackPropertyError& error)
74 {
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010075 std::cout << "As expected " << error.what() << "\n";
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020076 }
77
78 void asyncGetAllPropertiesStringTypeOnly()
79 {
80 sdbusplus::asio::getAllProperties(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010081 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010082 [this](const boost::system::error_code ec,
83 const std::vector<std::pair<
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020084 std::string, std::variant<std::monostate, std::string>>>&
Ed Tanous95874d92021-02-19 17:14:27 -080085 properties) -> void {
Patrick Williams06f265f2024-08-16 15:19:49 -040086 if (ec)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020087 {
Patrick Williams06f265f2024-08-16 15:19:49 -040088 logSystemErrorCode(ec);
89 return;
Patrick Williams6db88382023-10-20 11:18:17 -050090 }
Patrick Williams6db88382023-10-20 11:18:17 -050091 {
Patrick Williams06f265f2024-08-16 15:19:49 -040092 const std::string* greetings = nullptr;
93 const std::string* goodbyes = nullptr;
94 const bool success = sdbusplus::unpackPropertiesNoThrow(
95 [this](const sdbusplus::UnpackErrorReason reason,
96 const std::string& property) {
97 logUnpackError(reason, property);
98 },
99 properties, propertyGrettingName, greetings,
100 propertyGoodbyesName, goodbyes);
101
102 if (success)
103 {
104 std::cout
105 << "value of greetings: " << *greetings << "\n";
106 std::cout << "value of goodbyes: " << *goodbyes << "\n";
107 }
108 else
109 {
110 ++fatalErrors_;
111 }
112 }
113
114 try
115 {
116 std::string value;
117 sdbusplus::unpackProperties(properties, propertyValueName,
118 value);
119
120 std::cerr << "Error: it should fail because of "
121 "not matched type\n";
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200122 ++fatalErrors_;
123 }
Patrick Williams06f265f2024-08-16 15:19:49 -0400124 catch (const sdbusplus::exception::UnpackPropertyError& error)
125 {
126 logExpectedException(error);
127 }
128 });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200129 }
130
131 void asyncGetAllProperties()
132 {
133 sdbusplus::asio::getAllProperties(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100134 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +0100135 [this](const boost::system::error_code ec,
136 const std::vector<std::pair<
Ed Tanous95874d92021-02-19 17:14:27 -0800137 std::string,
138 std::variant<std::monostate, std::string, uint32_t>>>&
139 properties) -> void {
Patrick Williams06f265f2024-08-16 15:19:49 -0400140 if (ec)
141 {
142 logSystemErrorCode(ec);
143 return;
144 }
145 try
146 {
147 std::string greetings;
148 std::string goodbyes;
149 uint32_t value = 0u;
150 sdbusplus::unpackProperties(
151 properties, propertyGrettingName, greetings,
152 propertyGoodbyesName, goodbyes, propertyValueName,
153 value);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200154
Patrick Williams06f265f2024-08-16 15:19:49 -0400155 std::cout << "value of greetings: " << greetings << "\n";
156 std::cout << "value of goodbyes: " << goodbyes << "\n";
157 std::cout << "value of value: " << value << "\n";
158 }
159 catch (const sdbusplus::exception::UnpackPropertyError& error)
160 {
161 logException(error);
162 }
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200163
Patrick Williams06f265f2024-08-16 15:19:49 -0400164 try
165 {
166 std::string unknownProperty;
167 sdbusplus::unpackProperties(
168 properties, "UnknownPropertyName", unknownProperty);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200169
Patrick Williams06f265f2024-08-16 15:19:49 -0400170 std::cerr << "Error: it should fail because of "
171 "missing property\n";
172 ++fatalErrors_;
173 }
174 catch (const sdbusplus::exception::UnpackPropertyError& error)
175 {
176 logExpectedException(error);
177 }
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200178
Patrick Williams06f265f2024-08-16 15:19:49 -0400179 try
180 {
181 uint32_t notMatchingType;
182 sdbusplus::unpackProperties(
183 properties, propertyGrettingName, notMatchingType);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200184
Patrick Williams06f265f2024-08-16 15:19:49 -0400185 std::cerr << "Error: it should fail because of "
186 "not matched type\n";
187 ++fatalErrors_;
188 }
189 catch (const sdbusplus::exception::UnpackPropertyError& error)
190 {
191 logExpectedException(error);
192 }
193 });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200194 }
195
196 private:
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200197 sdbusplus::asio::connection& bus_;
198 sdbusplus::asio::object_server& objServer_;
199
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200200 std::unique_ptr<sdbusplus::asio::dbus_interface> demo_;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200201 std::string greetings_ = "Hello";
202 std::string goodbyes_ = "Bye";
203
204 uint32_t fatalErrors_ = 0u;
205};
206
207int main(int, char**)
208{
209 boost::asio::io_context ioc;
210 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
211
Patrick Williams06f265f2024-08-16 15:19:49 -0400212 signals.async_wait([&ioc](const boost::system::error_code&, const int&) {
213 ioc.stop();
214 });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200215
216 auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
217 auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
218
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100219 bus->request_name(demoServiceName.c_str());
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200220
Ed Tanous760e66a2023-01-05 10:12:00 -0800221 Application app(*bus, *objServer);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200222
223 boost::asio::post(ioc,
224 [&app] { app.asyncGetAllPropertiesStringTypeOnly(); });
225 boost::asio::post(ioc, [&app] { app.asyncGetAllProperties(); });
226
227 ioc.run();
228
229 std::cout << "Fatal errors count: " << app.fatalErrors() << "\n";
230
231 return app.fatalErrors();
232}