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