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 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 9 | const std::string demoServiceName = "demo.service"; |
| 10 | const std::string demoObjectPath = "/xyz/demo"; |
| 11 | const std::string demoInterfaceName = "xyz.demo"; |
| 12 | const std::string propertyGrettingName = "Greetings"; |
| 13 | const std::string propertyGoodbyesName = "Goodbyes"; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 14 | |
| 15 | class Application |
| 16 | { |
| 17 | public: |
| 18 | Application(boost::asio::io_context& ioc, sdbusplus::asio::connection& bus, |
| 19 | sdbusplus::asio::object_server& objServer) : |
| 20 | ioc_(ioc), |
| 21 | bus_(bus), objServer_(objServer) |
| 22 | { |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 23 | demo_ = objServer_.add_unique_interface( |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 24 | demoObjectPath, demoInterfaceName, |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 25 | [this](sdbusplus::asio::dbus_interface& demo) { |
Krzysztof Grobelny | 90fab6b | 2022-02-18 14:21:21 +0100 | [diff] [blame] | 26 | demo.register_property_r<std::string>( |
| 27 | propertyGrettingName, sdbusplus::vtable::property_::const_, |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 28 | [this](const auto&) { return greetings_; }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 29 | |
Krzysztof Grobelny | 90fab6b | 2022-02-18 14:21:21 +0100 | [diff] [blame] | 30 | demo.register_property_rw<std::string>( |
| 31 | propertyGoodbyesName, |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 32 | sdbusplus::vtable::property_::emits_change, |
| 33 | [this](const auto& newPropertyValue, const auto&) { |
| 34 | goodbyes_ = newPropertyValue; |
Jonathan Doman | ae47928 | 2022-06-03 13:29:50 -0700 | [diff] [blame^] | 35 | return true; |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 36 | }, |
| 37 | [this](const auto&) { return goodbyes_; }); |
| 38 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 41 | 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 Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 56 | sdbusplus::asio::getProperty<uint32_t>( |
| 57 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
| 58 | propertyGrettingName, |
| 59 | [this](boost::system::error_code ec, uint32_t) { |
| 60 | if (ec) |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 61 | { |
| 62 | std::cout |
| 63 | << "As expected failed to getProperty with wrong type: " |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 64 | << ec << "\n"; |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 68 | std::cerr << "Error: it was expected to fail getProperty due " |
| 69 | "to wrong type\n"; |
| 70 | ++fatalErrors_; |
| 71 | }); |
| 72 | } |
| 73 | |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 74 | void asyncReadProperties() |
| 75 | { |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 76 | sdbusplus::asio::getProperty<std::string>( |
| 77 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
| 78 | propertyGrettingName, |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 79 | [this](boost::system::error_code ec, std::string value) { |
| 80 | if (ec) |
| 81 | { |
| 82 | getFailed(); |
| 83 | return; |
| 84 | } |
| 85 | std::cout << "Greetings value is: " << value << "\n"; |
| 86 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 87 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 88 | sdbusplus::asio::getProperty<std::string>( |
| 89 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
| 90 | propertyGoodbyesName, |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 91 | [this](boost::system::error_code ec, std::string value) { |
| 92 | if (ec) |
| 93 | { |
| 94 | getFailed(); |
| 95 | return; |
| 96 | } |
| 97 | std::cout << "Goodbyes value is: " << value << "\n"; |
| 98 | }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void asyncChangeProperty() |
| 102 | { |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 103 | sdbusplus::asio::setProperty( |
| 104 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
| 105 | propertyGrettingName, "Hi, hey, hello", |
| 106 | [this](const boost::system::error_code& ec) { |
| 107 | if (ec) |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 108 | { |
| 109 | std::cout |
| 110 | << "As expected, failed to set greetings property: " |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 111 | << ec << "\n"; |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 115 | std::cout |
| 116 | << "Error: it was expected to fail to change greetings\n"; |
| 117 | ++fatalErrors_; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 118 | }); |
| 119 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 120 | sdbusplus::asio::setProperty( |
| 121 | bus_, demoServiceName, demoObjectPath, demoInterfaceName, |
| 122 | propertyGoodbyesName, "Bye bye", |
| 123 | [this](const boost::system::error_code& ec) { |
| 124 | if (ec) |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 125 | { |
| 126 | std::cout |
| 127 | << "Error: it supposed to be ok to change goodbyes " |
| 128 | "property: " |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 129 | << ec << "\n"; |
Ed Tanous | 95874d9 | 2021-02-19 17:14:27 -0800 | [diff] [blame] | 130 | ++fatalErrors_; |
| 131 | return; |
| 132 | } |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 133 | std::cout << "Changed goodbyes property as expected\n"; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 134 | boost::asio::post(ioc_, [this] { asyncReadProperties(); }); |
| 135 | }); |
| 136 | } |
| 137 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 138 | void syncChangeGoodbyes(std::string_view value) |
| 139 | { |
| 140 | goodbyes_ = value; |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 141 | demo_->signal_property(propertyGoodbyesName); |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 144 | private: |
| 145 | boost::asio::io_context& ioc_; |
| 146 | sdbusplus::asio::connection& bus_; |
| 147 | sdbusplus::asio::object_server& objServer_; |
| 148 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 149 | std::unique_ptr<sdbusplus::asio::dbus_interface> demo_; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 150 | std::string greetings_ = "Hello"; |
| 151 | std::string goodbyes_ = "Bye"; |
| 152 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 153 | uint32_t fatalErrors_ = 0u; |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | int main(int, char**) |
| 157 | { |
| 158 | boost::asio::io_context ioc; |
| 159 | boost::asio::signal_set signals(ioc, SIGINT, SIGTERM); |
| 160 | |
| 161 | signals.async_wait( |
| 162 | [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); }); |
| 163 | |
| 164 | auto bus = std::make_shared<sdbusplus::asio::connection>(ioc); |
| 165 | auto objServer = std::make_unique<sdbusplus::asio::object_server>(bus); |
| 166 | |
Krzysztof Grobelny | 6adfe94 | 2021-12-21 13:57:22 +0100 | [diff] [blame] | 167 | bus->request_name(demoServiceName.c_str()); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 168 | |
| 169 | Application app(ioc, *bus, *objServer); |
| 170 | |
Krzysztof Grobelny | 8fc0639 | 2020-09-28 13:11:22 +0200 | [diff] [blame] | 171 | app.syncChangeGoodbyes("Good bye"); |
| 172 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 173 | boost::asio::post(ioc, |
| 174 | [&app] { app.asyncReadPropertyWithIncorrectType(); }); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 175 | boost::asio::post(ioc, [&app] { app.asyncReadProperties(); }); |
| 176 | boost::asio::post(ioc, [&app] { app.asyncChangeProperty(); }); |
| 177 | |
| 178 | ioc.run(); |
| 179 | |
Krzysztof Grobelny | 807419d | 2020-09-28 11:39:22 +0200 | [diff] [blame] | 180 | std::cout << "Fatal errors count: " << app.fatalErrors() << "\n"; |
| 181 | |
| 182 | return app.fatalErrors(); |
Krzysztof Grobelny | 2be0e17 | 2020-07-27 11:12:07 +0200 | [diff] [blame] | 183 | } |