-
Notifications
You must be signed in to change notification settings - Fork 3
Add sdbus-c++ ConnMan client example #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| add_executable(connman_client | ||
| main.cc | ||
| connman_client.cc | ||
| connman_client.h | ||
| ) | ||
|
|
||
| target_link_libraries(connman_client | ||
| PUBLIC | ||
| utils | ||
| sdbus-c++ | ||
| spdlog::spdlog | ||
| ) | ||
|
|
||
| if (ENABLE_LTO AND IPO_SUPPORT_RESULT) | ||
| set_property(TARGET connman_client PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) | ||
| endif () | ||
|
|
||
| install(TARGETS connman_client | ||
| RUNTIME DESTINATION share/sdbus-cpp-examples) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| // Copyright (c) 2026 Jian De | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "connman_client.h" | ||
|
|
||
| #include <sstream> | ||
|
|
||
| #include "../utils/logging.h" | ||
| #include "../utils/utils.h" | ||
|
|
||
| // | ||
| // ConnmanManagerClient | ||
| // | ||
|
|
||
| ConnmanManagerClient::ConnmanManagerClient(sdbus::IConnection& connection) | ||
| : ProxyInterfaces(connection, | ||
| sdbus::ServiceName{SERVICE_NAME}, | ||
| sdbus::ObjectPath{OBJECT_PATH}) { | ||
| registerProxy(); | ||
|
|
||
| try { | ||
| // Load initial technologies | ||
| for (const auto& tech : GetTechnologies()) { | ||
| onTechnologyAdded(tech.get<0>(), tech.get<1>()); | ||
| } | ||
|
|
||
| // Load initial services | ||
| for (const auto& service : GetServices()) { | ||
| const auto& path = service.get<0>(); | ||
| if (services_.find(path) == services_.end()) { | ||
| services_[path] = std::make_unique<ConnmanServiceClient>( | ||
| getProxy().getConnection(), path); | ||
| } | ||
| } | ||
| } catch (...) { | ||
| unregisterProxy(); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| ConnmanManagerClient::~ConnmanManagerClient() { | ||
| unregisterProxy(); | ||
| } | ||
|
|
||
| void ConnmanManagerClient::onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) { | ||
| try { | ||
| std::ostringstream os; | ||
| Utils::append_property(value, os); | ||
| LOG_INFO("Manager PropertyChanged: {} = {}", name, os.str()); | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in Manager onPropertyChanged: {}", e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ConnmanManagerClient::onTechnologyAdded( | ||
| const sdbus::ObjectPath& path, | ||
| const std::map<std::string, sdbus::Variant>& properties) { | ||
| (void)properties; | ||
| try { | ||
| LOG_INFO("Technology added: {}", path); | ||
| if (technologies_.find(path) == technologies_.end()) { | ||
| technologies_[path] = std::make_unique<ConnmanTechnologyClient>( | ||
| getProxy().getConnection(), path); | ||
| } | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in onTechnologyAdded: {}", e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ConnmanManagerClient::onTechnologyRemoved(const sdbus::ObjectPath& path) { | ||
| try { | ||
| LOG_INFO("Technology removed: {}", path); | ||
| technologies_.erase(path); | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in onTechnologyRemoved: {}", e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ConnmanManagerClient::onServicesChanged( | ||
| const std::vector<sdbus::Struct<sdbus::ObjectPath, | ||
| std::map<std::string, sdbus::Variant>>>& | ||
| changed, | ||
| const std::vector<sdbus::ObjectPath>& removed) { | ||
| try { | ||
| for (const auto& service : changed) { | ||
| const auto& path = service.get<0>(); | ||
| const auto& props = service.get<1>(); | ||
|
|
||
| if (services_.find(path) == services_.end()) { | ||
| LOG_INFO("Service added: {}", path); | ||
| services_[path] = std::make_unique<ConnmanServiceClient>( | ||
| getProxy().getConnection(), path); | ||
| } | ||
|
|
||
| // Log WiFi info only when Type is explicitly "wifi" in this delta | ||
| bool is_wifi = false; | ||
| if (auto it = props.find("Type"); | ||
| it != props.end() && it->second.containsValueOfType<std::string>()) { | ||
| is_wifi = (it->second.get<std::string>() == "wifi"); | ||
| } | ||
|
|
||
| if (is_wifi) { | ||
| std::string ssid = "Unknown"; | ||
| if (auto it = props.find("Name"); | ||
| it != props.end() && | ||
| it->second.containsValueOfType<std::string>()) { | ||
| ssid = it->second.get<std::string>(); | ||
| } | ||
| LOG_INFO(" WiFi Update: {} [{}]", ssid, path); | ||
| } | ||
| } | ||
|
|
||
| for (const auto& path : removed) { | ||
| LOG_INFO(" Service removed: {}", path); | ||
| services_.erase(path); | ||
| } | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in onServicesChanged: {}", e.what()); | ||
| } | ||
| } | ||
|
|
||
| void ConnmanManagerClient::onPeersChanged( | ||
| const std::vector<sdbus::Struct<sdbus::ObjectPath, | ||
| std::map<std::string, sdbus::Variant>>>&, | ||
| const std::vector<sdbus::ObjectPath>&) {} | ||
|
|
||
| void ConnmanManagerClient::onTetheringClientsChanged( | ||
| const std::vector<std::string>&, | ||
| const std::vector<std::string>&) {} | ||
|
|
||
| // | ||
| // ConnmanTechnologyClient | ||
| // | ||
|
|
||
| ConnmanTechnologyClient::ConnmanTechnologyClient(sdbus::IConnection& connection, | ||
| sdbus::ObjectPath path) | ||
| : ProxyInterfaces(connection, | ||
| sdbus::ServiceName{SERVICE_NAME}, | ||
| std::move(path)) { | ||
| registerProxy(); | ||
| } | ||
|
|
||
| ConnmanTechnologyClient::~ConnmanTechnologyClient() { | ||
| unregisterProxy(); | ||
| } | ||
|
|
||
| void ConnmanTechnologyClient::onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) { | ||
| try { | ||
| if (name == "Scanning" && value.containsValueOfType<bool>()) { | ||
| bool scanning = value.get<bool>(); | ||
| LOG_INFO("Technology [{}] WiFi Scan: {}", getProxy().getObjectPath(), | ||
| scanning ? "STARTED" : "COMPLETED"); | ||
| } | ||
|
|
||
| std::ostringstream os; | ||
| Utils::append_property(value, os); | ||
| LOG_INFO("Technology [{}] PropertyChanged: {} = {}", | ||
| getProxy().getObjectPath(), name, os.str()); | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in Technology onPropertyChanged: {}", e.what()); | ||
| } | ||
| } | ||
|
|
||
| // | ||
| // ConnmanServiceClient | ||
| // | ||
|
|
||
| ConnmanServiceClient::ConnmanServiceClient(sdbus::IConnection& connection, | ||
| sdbus::ObjectPath path) | ||
| : ProxyInterfaces(connection, | ||
| sdbus::ServiceName{SERVICE_NAME}, | ||
| std::move(path)) { | ||
| registerProxy(); | ||
| } | ||
|
|
||
| ConnmanServiceClient::~ConnmanServiceClient() { | ||
| unregisterProxy(); | ||
| } | ||
|
|
||
| void ConnmanServiceClient::onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) { | ||
| try { | ||
| std::ostringstream os; | ||
| Utils::append_property(value, os); | ||
| LOG_INFO("Service [{}] PropertyChanged: {} = {}", | ||
| getProxy().getObjectPath(), name, os.str()); | ||
| } catch (const std::exception& e) { | ||
| LOG_ERROR("Exception in Service onPropertyChanged: {}", e.what()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (c) 2026 Jian De | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef SRC_CONNMAN_CONNMAN_CLIENT_H | ||
| #define SRC_CONNMAN_CONNMAN_CLIENT_H | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "../proxy/net/connman/Manager/manager_proxy.h" | ||
| #include "../proxy/net/connman/Service/service_proxy.h" | ||
| #include "../proxy/net/connman/Technology/technology_proxy.h" | ||
|
|
||
| class ConnmanTechnologyClient; | ||
| class ConnmanServiceClient; | ||
|
|
||
| class ConnmanManagerClient final | ||
| : public sdbus::ProxyInterfaces<net::connman::Manager_proxy> { | ||
| public: | ||
| static constexpr auto SERVICE_NAME = "net.connman"; | ||
| static constexpr auto OBJECT_PATH = "/"; | ||
|
|
||
| explicit ConnmanManagerClient(sdbus::IConnection& connection); | ||
| ~ConnmanManagerClient(); | ||
|
|
||
| void onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) override; | ||
|
|
||
| void onTechnologyAdded( | ||
| const sdbus::ObjectPath& path, | ||
| const std::map<std::string, sdbus::Variant>& properties) override; | ||
|
|
||
| void onTechnologyRemoved(const sdbus::ObjectPath& path) override; | ||
|
|
||
| void onServicesChanged( | ||
| const std::vector<sdbus::Struct<sdbus::ObjectPath, | ||
| std::map<std::string, sdbus::Variant>>>& | ||
| changed, | ||
| const std::vector<sdbus::ObjectPath>& removed) override; | ||
|
|
||
| void onPeersChanged( | ||
| const std::vector<sdbus::Struct<sdbus::ObjectPath, | ||
| std::map<std::string, sdbus::Variant>>>& | ||
| changed, | ||
| const std::vector<sdbus::ObjectPath>& removed) override; | ||
|
|
||
| void onTetheringClientsChanged( | ||
| const std::vector<std::string>& registered, | ||
| const std::vector<std::string>& removed) override; | ||
|
|
||
| private: | ||
| std::map<sdbus::ObjectPath, std::unique_ptr<ConnmanTechnologyClient>> | ||
| technologies_; | ||
| std::map<sdbus::ObjectPath, std::unique_ptr<ConnmanServiceClient>> services_; | ||
| }; | ||
|
jwinarske marked this conversation as resolved.
|
||
|
|
||
| class ConnmanTechnologyClient final | ||
| : public sdbus::ProxyInterfaces<net::connman::Technology_proxy> { | ||
| public: | ||
| static constexpr auto SERVICE_NAME = "net.connman"; | ||
|
|
||
| ConnmanTechnologyClient(sdbus::IConnection& connection, | ||
| sdbus::ObjectPath path); | ||
| ~ConnmanTechnologyClient(); | ||
|
|
||
| void onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) override; | ||
| }; | ||
|
|
||
| class ConnmanServiceClient final | ||
| : public sdbus::ProxyInterfaces<net::connman::Service_proxy> { | ||
| public: | ||
| static constexpr auto SERVICE_NAME = "net.connman"; | ||
|
|
||
| ConnmanServiceClient(sdbus::IConnection& connection, sdbus::ObjectPath path); | ||
| ~ConnmanServiceClient(); | ||
|
|
||
| void onPropertyChanged(const std::string& name, | ||
| const sdbus::Variant& value) override; | ||
| }; | ||
|
|
||
| #endif // SRC_CONNMAN_CONNMAN_CLIENT_H | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.