Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 1 | #include <boost/asio.hpp> |
| 2 | #include <sdbusplus/asio/connection.hpp> |
| 3 | #include <sdbusplus/asio/object_server.hpp> |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 4 | #include <sdbusplus/asio/property.hpp> |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | namespace xyz |
| 10 | { |
| 11 | namespace demo |
| 12 | { |
| 13 | |
| 14 | const std::string path = "/xyz/demo"; |
| 15 | const std::string name = "xyz.demo"; |
| 16 | |
| 17 | } // namespace demo |
| 18 | } // namespace xyz |
| 19 | |
| 20 | namespace name |
| 21 | { |
| 22 | |
| 23 | const std::string greetings = "Greetings"; |
| 24 | const std::string goodbyes = "Goodbyes"; |
| 25 | |
| 26 | } // namespace name |
| 27 | |
| 28 | namespace utils |
| 29 | { |
| 30 | |
| 31 | template <class T> |
| 32 | class Property |
| 33 | { |
| 34 | public: |
| 35 | Property(sdbusplus::asio::connection& bus, std::string_view service, |
| 36 | std::string_view path, std::string_view interface, |
| 37 | std::string_view name) : |
| 38 | bus_(bus), |
| 39 | service_(service), path_(path), interface_(interface), name_(name) |
| 40 | {} |
| 41 | |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 42 | template <class Handler> |
| 43 | void async_get(Handler&& handler) |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 44 | { |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 45 | sdbusplus::asio::getProperty<T>(bus_, service_, path_, interface_, |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 46 | name_, std::forward<Handler>(handler)); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 49 | template <class Handler> |
| 50 | void async_set(const T& value, Handler&& handler) |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 51 | { |
Krzysztof Grobelny | 4f65175 | 2020-12-18 14:09:00 +0000 | [diff] [blame] | 52 | sdbusplus::asio::setProperty(bus_, service_, path_, interface_, name_, |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 53 | value, std::forward<Handler>(handler)); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
| 57 | sdbusplus::asio::connection& bus_; |
| 58 | std::string service_; |
| 59 | std::string path_; |
| 60 | std::string interface_; |
| 61 | std::string name_; |
| 62 | }; |
| 63 | |
| 64 | } // namespace utils |
| 65 | |
| 66 | class Application |
| 67 | { |
| 68 | public: |
| 69 | Application(boost::asio::io_context& ioc, sdbusplus::asio::connection& bus, |
| 70 | sdbusplus::asio::object_server& objServer) : |
| 71 | ioc_(ioc), |
| 72 | bus_(bus), objServer_(objServer) |
| 73 | { |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 74 | demo_ = objServer_.add_unique_interface( |
| 75 | xyz::demo::path, xyz::demo::name, |
| 76 | [this](sdbusplus::asio::dbus_interface& demo) { |
| 77 | demo.register_property_r( |
| 78 | name::greetings, std::string(), |
| 79 | sdbusplus::vtable::property_::const_, |
| 80 | [this](const auto&) { return greetings_; }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 81 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 82 | demo.register_property_rw( |
| 83 | name::goodbyes, std::string(), |
| 84 | sdbusplus::vtable::property_::emits_change, |
| 85 | [this](const auto& newPropertyValue, const auto&) { |
| 86 | goodbyes_ = newPropertyValue; |
| 87 | return 1; |
| 88 | }, |
| 89 | [this](const auto&) { return goodbyes_; }); |
| 90 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 93 | uint32_t fatalErrors() const |
| 94 | { |
| 95 | return fatalErrors_; |
| 96 | } |
| 97 | |
| 98 | auto getFailed() |
| 99 | { |
| 100 | return [this](boost::system::error_code error) { |
| 101 | std::cerr << "Error: getProperty failed " << error << "\n"; |
| 102 | ++fatalErrors_; |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | void asyncReadPropertyWithIncorrectType() |
| 107 | { |
| 108 | utils::Property<uint32_t> propertyWithWrongType{ |
| 109 | bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name, |
| 110 | name::greetings}; |
| 111 | |
| 112 | propertyWithWrongType.async_get( |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 113 | [this](boost::system::error_code error, uint32_t) { |
| 114 | if (error) |
| 115 | { |
| 116 | std::cout |
| 117 | << "As expected failed to getProperty with wrong type: " |
| 118 | << error << "\n"; |
| 119 | return; |
| 120 | } |
| 121 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 122 | std::cerr << "Error: it was expected to fail getProperty due " |
| 123 | "to wrong type\n"; |
| 124 | ++fatalErrors_; |
| 125 | }); |
| 126 | } |
| 127 | |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 128 | void asyncReadProperties() |
| 129 | { |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 130 | propertyGreetings.async_get( |
| 131 | [this](boost::system::error_code ec, std::string value) { |
| 132 | if (ec) |
| 133 | { |
| 134 | getFailed(); |
| 135 | return; |
| 136 | } |
| 137 | std::cout << "Greetings value is: " << value << "\n"; |
| 138 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 139 | |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 140 | propertyGoodbyes.async_get( |
| 141 | [this](boost::system::error_code ec, std::string value) { |
| 142 | if (ec) |
| 143 | { |
| 144 | getFailed(); |
| 145 | return; |
| 146 | } |
| 147 | std::cout << "Goodbyes value is: " << value << "\n"; |
| 148 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void asyncChangeProperty() |
| 152 | { |
| 153 | propertyGreetings.async_set( |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 154 | "Hi, hey, hello", [this](const boost::system::error_code& error) { |
| 155 | if (error) |
| 156 | { |
| 157 | std::cout |
| 158 | << "As expected, failed to set greetings property: " |
| 159 | << error << "\n"; |
| 160 | return; |
| 161 | } |
| 162 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 163 | std::cout |
| 164 | << "Error: it was expected to fail to change greetings\n"; |
| 165 | ++fatalErrors_; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 166 | }); |
| 167 | |
| 168 | propertyGoodbyes.async_set( |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 169 | "Bye bye", [this](const boost::system::error_code& error) { |
| 170 | if (error) |
| 171 | { |
| 172 | std::cout |
| 173 | << "Error: it supposed to be ok to change goodbyes " |
| 174 | "property: " |
| 175 | << error << "\n"; |
| 176 | ++fatalErrors_; |
| 177 | return; |
| 178 | } |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 179 | std::cout << "Changed goodbyes property as expected\n"; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 180 | boost::asio::post(ioc_, [this] { asyncReadProperties(); }); |
| 181 | }); |
| 182 | } |
| 183 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 184 | void syncChangeGoodbyes(std::string_view value) |
| 185 | { |
| 186 | goodbyes_ = value; |
| 187 | demo_->signal_property(name::goodbyes); |
| 188 | } |
| 189 | |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 190 | private: |
| 191 | boost::asio::io_context& ioc_; |
| 192 | sdbusplus::asio::connection& bus_; |
| 193 | sdbusplus::asio::object_server& objServer_; |
| 194 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 195 | std::unique_ptr<sdbusplus::asio::dbus_interface> demo_; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 196 | std::string greetings_ = "Hello"; |
| 197 | std::string goodbyes_ = "Bye"; |
| 198 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 199 | uint32_t fatalErrors_ = 0u; |
| 200 | |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 201 | utils::Property<std::string> propertyGreetings{ |
| 202 | bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name, |
| 203 | name::greetings}; |
| 204 | utils::Property<std::string> propertyGoodbyes{ |
| 205 | bus_, xyz::demo::name, xyz::demo::path, xyz::demo::name, |
| 206 | name::goodbyes}; |
| 207 | }; |
| 208 | |
| 209 | int main(int, char**) |
| 210 | { |
| 211 | boost::asio::io_context ioc; |
| 212 | boost::asio::signal_set signals(ioc, SIGINT, SIGTERM); |
| 213 | |
| 214 | signals.async_wait( |
| 215 | [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); }); |
| 216 | |
| 217 | auto bus = std::make_shared<sdbusplus::asio::connection>(ioc); |
| 218 | auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus); |
| 219 | |
| 220 | bus->request_name(xyz::demo::name.c_str()); |
| 221 | |
| 222 | Application app(ioc, *bus, *objServer); |
| 223 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 224 | app.syncChangeGoodbyes("Good bye"); |
| 225 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 226 | boost::asio::post(ioc, |
| 227 | [&app] { app.asyncReadPropertyWithIncorrectType(); }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 228 | boost::asio::post(ioc, [&app] { app.asyncReadProperties(); }); |
| 229 | boost::asio::post(ioc, [&app] { app.asyncChangeProperty(); }); |
| 230 | |
| 231 | ioc.run(); |
| 232 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 233 | std::cout << "Fatal errors count: " << app.fatalErrors() << "\n"; |
| 234 | |
| 235 | return app.fatalErrors(); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 236 | } |