blob: 35e9783d8f5f646731b2b6cbd2433c1dba6aa6e8 [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) :
Ed Tanous760e66a2023-01-05 10:12:00 -080023 bus_(bus),
24 objServer_(objServer)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020025 {
Patrick Williams1a25a102022-09-29 17:18:26 -050026 demo_ = objServer_.add_unique_interface(demoObjectPath,
27 demoInterfaceName);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020028
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010029 demo_->register_property_r<std::string>(
30 propertyGrettingName, sdbusplus::vtable::property_::const_,
31 [this](const auto&) { return greetings_; });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020032
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010033 demo_->register_property_rw<std::string>(
34 propertyGoodbyesName, sdbusplus::vtable::property_::emits_change,
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020035 [this](const auto& newPropertyValue, const auto&) {
Patrick Williamsd2149042023-05-10 07:50:13 -050036 goodbyes_ = newPropertyValue;
37 return true;
Patrick Williams6db88382023-10-20 11:18:17 -050038 },
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020039 [this](const auto&) { return goodbyes_; });
40
Krzysztof Grobelny90fab6b2022-02-18 14:21:21 +010041 demo_->register_property_r<uint32_t>(
42 propertyValueName, sdbusplus::vtable::property_::const_,
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020043 [](const auto& value) -> uint32_t { return value; });
44
45 demo_->initialize();
46 }
47
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020048 uint32_t fatalErrors() const
49 {
50 return fatalErrors_;
51 }
52
Ed Tanous95874d92021-02-19 17:14:27 -080053 auto logSystemErrorCode(boost::system::error_code ec)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020054 {
Ed Tanous95874d92021-02-19 17:14:27 -080055 std::cerr << "Error: " << ec << "\n";
56 ++fatalErrors_;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020057 }
58
59 void logException(const std::exception& e)
60 {
61 std::cerr << "Error: " << e.what() << "\n";
62 ++fatalErrors_;
63 }
64
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010065 void logUnpackError(const sdbusplus::UnpackErrorReason reason,
66 const std::string& property)
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010067 {
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010068 std::cerr << "UnpackError: " << static_cast<int>(reason) << ", "
69 << property << "\n";
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010070 ++fatalErrors_;
71 }
72
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020073 void logExpectedException(
74 const sdbusplus::exception::UnpackPropertyError& error)
75 {
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010076 std::cout << "As expected " << error.what() << "\n";
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020077 }
78
79 void asyncGetAllPropertiesStringTypeOnly()
80 {
81 sdbusplus::asio::getAllProperties(
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +010082 bus_, demoServiceName, demoObjectPath, demoInterfaceName,
Krzysztof Grobelnyc8447d52022-01-05 13:21:37 +010083 [this](const boost::system::error_code ec,
84 const std::vector<std::pair<
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +020085 std::string, std::variant<std::monostate, std::string>>>&
Ed Tanous95874d92021-02-19 17:14:27 -080086 properties) -> void {
Patrick Williams6db88382023-10-20 11:18:17 -050087 if (ec)
88 {
89 logSystemErrorCode(ec);
90 return;
91 }
92 {
93 const std::string* greetings = nullptr;
94 const std::string* goodbyes = nullptr;
95 const bool success = sdbusplus::unpackPropertiesNoThrow(
96 [this](const sdbusplus::UnpackErrorReason reason,
97 const std::string& property) {
Patrick Williamsd2149042023-05-10 07:50:13 -050098 logUnpackError(reason, property);
Patrick Williams6db88382023-10-20 11:18:17 -050099 },
100 properties, propertyGrettingName, greetings,
101 propertyGoodbyesName, goodbyes);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200102
Patrick Williams6db88382023-10-20 11:18:17 -0500103 if (success)
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200104 {
Patrick Williams6db88382023-10-20 11:18:17 -0500105 std::cout << "value of greetings: " << *greetings << "\n";
106 std::cout << "value of goodbyes: " << *goodbyes << "\n";
107 }
108 else
109 {
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200110 ++fatalErrors_;
111 }
Patrick Williams6db88382023-10-20 11:18:17 -0500112 }
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";
122 ++fatalErrors_;
123 }
124 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 Williams6db88382023-10-20 11:18:17 -0500140 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(properties, propertyGrettingName,
151 greetings, propertyGoodbyesName,
152 goodbyes, propertyValueName, value);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200153
Patrick Williams6db88382023-10-20 11:18:17 -0500154 std::cout << "value of greetings: " << greetings << "\n";
155 std::cout << "value of goodbyes: " << goodbyes << "\n";
156 std::cout << "value of value: " << value << "\n";
157 }
158 catch (const sdbusplus::exception::UnpackPropertyError& error)
159 {
160 logException(error);
161 }
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200162
Patrick Williams6db88382023-10-20 11:18:17 -0500163 try
164 {
165 std::string unknownProperty;
166 sdbusplus::unpackProperties(properties, "UnknownPropertyName",
167 unknownProperty);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200168
Patrick Williams6db88382023-10-20 11:18:17 -0500169 std::cerr << "Error: it should fail because of "
170 "missing property\n";
171 ++fatalErrors_;
172 }
173 catch (const sdbusplus::exception::UnpackPropertyError& error)
174 {
175 logExpectedException(error);
176 }
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200177
Patrick Williams6db88382023-10-20 11:18:17 -0500178 try
179 {
180 uint32_t notMatchingType;
181 sdbusplus::unpackProperties(properties, propertyGrettingName,
182 notMatchingType);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200183
Patrick Williams6db88382023-10-20 11:18:17 -0500184 std::cerr << "Error: it should fail because of "
185 "not matched type\n";
186 ++fatalErrors_;
187 }
188 catch (const sdbusplus::exception::UnpackPropertyError& error)
189 {
190 logExpectedException(error);
191 }
192 });
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200193 }
194
195 private:
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200196 sdbusplus::asio::connection& bus_;
197 sdbusplus::asio::object_server& objServer_;
198
Krzysztof Grobelny8fc06392020-09-28 13:11:22 +0200199 std::unique_ptr<sdbusplus::asio::dbus_interface> demo_;
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200200 std::string greetings_ = "Hello";
201 std::string goodbyes_ = "Bye";
202
203 uint32_t fatalErrors_ = 0u;
204};
205
206int main(int, char**)
207{
208 boost::asio::io_context ioc;
209 boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
210
211 signals.async_wait(
212 [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); });
213
214 auto bus = std::make_shared<sdbusplus::asio::connection>(ioc);
215 auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus);
216
Krzysztof Grobelny6adfe942021-12-21 13:57:22 +0100217 bus->request_name(demoServiceName.c_str());
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200218
Ed Tanous760e66a2023-01-05 10:12:00 -0800219 Application app(*bus, *objServer);
Krzysztof Grobelny09b88f22020-09-02 14:49:01 +0200220
221 boost::asio::post(ioc,
222 [&app] { app.asyncGetAllPropertiesStringTypeOnly(); });
223 boost::asio::post(ioc, [&app] { app.asyncGetAllProperties(); });
224
225 ioc.run();
226
227 std::cout << "Fatal errors count: " << app.fatalErrors() << "\n";
228
229 return app.fatalErrors();
230}