summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Tan <samueltan@google.com>2016-05-24 20:17:49 +0000
committerandroid-build-merger <android-build-merger@google.com>2016-05-24 20:17:49 +0000
commitdc4661ce35794d06e527c94af57a9f90e1900219 (patch)
tree1a6357f4e0b9fdddc0f676f877bd873010805194
parentcd107fbb054be8a1483c647ee8bbf0ba9cf2b741 (diff)
parent2662997dbe2ea80cd3cb4013ea8608ff493c5df5 (diff)
downloadshill-dc4661ce35794d06e527c94af57a9f90e1900219.tar.gz
shill_setup_wifi: refactor DBusDaemon-related code am: cbe9d2ec5f
am: 2662997dbe * commit '2662997dbe2ea80cd3cb4013ea8608ff493c5df5': shill_setup_wifi: refactor DBusDaemon-related code Change-Id: I1ed052973b6ff04f377e53fa4aedc4d286f5fdae
-rw-r--r--Android.mk4
-rw-r--r--setup_wifi/dbus_client.cc128
-rw-r--r--setup_wifi/dbus_client.h54
-rw-r--r--setup_wifi/main.cc153
4 files changed, 207 insertions, 132 deletions
diff --git a/Android.mk b/Android.mk
index 458c50d2..2ec4d4e7 100644
--- a/Android.mk
+++ b/Android.mk
@@ -646,7 +646,9 @@ LOCAL_MODULE_TAGS := eng
endif
LOCAL_STATIC_LIBRARIES := libgtest_prod
LOCAL_C_INCLUDES := $(shill_c_includes)
-LOCAL_SRC_FILES := setup_wifi/main.cc
+LOCAL_SRC_FILES := \
+ setup_wifi/dbus_client.cc \
+ setup_wifi/main.cc
$(eval $(shill_cpp_common))
include $(BUILD_EXECUTABLE)
diff --git a/setup_wifi/dbus_client.cc b/setup_wifi/dbus_client.cc
new file mode 100644
index 00000000..94657793
--- /dev/null
+++ b/setup_wifi/dbus_client.cc
@@ -0,0 +1,128 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 "shill/setup_wifi/dbus_client.h"
+
+#include <sysexits.h>
+
+#include <base/logging.h>
+#include <brillo/any.h>
+#if defined(__ANDROID__)
+#include <dbus/service_constants.h>
+#else
+#include <chromeos/dbus/service_constants.h>
+#endif // __ANDROID__
+
+using brillo::Any;
+using std::map;
+using std::string;
+
+namespace {
+static const char kOnlineState[] = "online";
+static const int kTimeoutBetweenStateChecksMs = 100;
+} // namespace
+
+namespace setup_wifi {
+
+DBusClient::DBusClient(const string& ssid, const string& psk, bool is_hex_ssid,
+ int timeout)
+ : ssid_(ssid), psk_(psk), is_hex_ssid_(is_hex_ssid), timeout_(timeout) {}
+
+int DBusClient::OnInit() {
+ int ret = DBusDaemon::OnInit();
+ if (ret != EX_OK) {
+ return ret;
+ }
+ ConfigureAndConnect();
+ // Timeout if we can't get online.
+ brillo::MessageLoop::current()->PostDelayedTask(
+ base::Bind(&DBusClient::Quit, base::Unretained(this)),
+ base::TimeDelta::FromSeconds(timeout_));
+ return EX_OK;
+}
+
+bool DBusClient::ConfigureAndConnect() {
+ std::unique_ptr<org::chromium::flimflam::ManagerProxy> shill_manager_proxy(
+ new org::chromium::flimflam::ManagerProxy(bus_));
+
+ dbus::ObjectPath created_service;
+ brillo::ErrorPtr configure_error;
+ if (!shill_manager_proxy->ConfigureService(
+ GetServiceConfig(), &created_service, &configure_error)) {
+ LOG(ERROR) << "Configure service failed";
+ return false;
+ }
+
+ brillo::ErrorPtr connect_error;
+ shill_service_proxy_ = std::unique_ptr<org::chromium::flimflam::ServiceProxy>(
+ new org::chromium::flimflam::ServiceProxy(bus_, created_service));
+ if (!shill_service_proxy_->Connect(&connect_error)) {
+ LOG(ERROR) << "Connect service failed";
+ return false;
+ }
+
+ PostCheckWifiStatusTask();
+ return true;
+}
+
+void DBusClient::PostCheckWifiStatusTask() {
+ LOG(INFO) << "Sleeping now. Will check wifi status in 100 ms.";
+ brillo::MessageLoop::current()->PostDelayedTask(
+ base::Bind(&DBusClient::QuitIfOnline, base::Unretained(this)),
+ base::TimeDelta::FromMilliseconds(kTimeoutBetweenStateChecksMs));
+}
+
+// Check if the device is online. If it is, quit.
+void DBusClient::QuitIfOnline() {
+ if (IsOnline())
+ Quit();
+ else
+ PostCheckWifiStatusTask();
+}
+
+bool DBusClient::IsOnline() {
+ brillo::VariantDictionary properties;
+ if (!shill_service_proxy_->GetProperties(&properties, nullptr)) {
+ LOG(ERROR) << "Cannot get properties.";
+ PostCheckWifiStatusTask();
+ return false;
+ }
+ auto property_it = properties.find(shill::kStateProperty);
+ if (property_it == properties.end()) {
+ PostCheckWifiStatusTask();
+ return false;
+ }
+
+ std::string state = property_it->second.TryGet<std::string>();
+ return state == kOnlineState;
+}
+
+map<string, Any> DBusClient::GetServiceConfig() {
+ map<string, Any> configure_dict;
+ configure_dict[shill::kTypeProperty] = shill::kTypeWifi;
+ if (is_hex_ssid_) {
+ configure_dict[shill::kWifiHexSsid] = ssid_;
+ } else {
+ configure_dict[shill::kSSIDProperty] = ssid_;
+ }
+ if (!psk_.empty()) {
+ configure_dict[shill::kPassphraseProperty] = psk_;
+ configure_dict[shill::kSecurityProperty] = shill::kSecurityPsk;
+ }
+ return configure_dict;
+}
+
+} // namespace setup_wifi
diff --git a/setup_wifi/dbus_client.h b/setup_wifi/dbus_client.h
new file mode 100644
index 00000000..75f972d9
--- /dev/null
+++ b/setup_wifi/dbus_client.h
@@ -0,0 +1,54 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 SHILL_SETUP_WIFI_DBUS_CLIENT_H_
+#define SHILL_SETUP_WIFI_DBUS_CLIENT_H_
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include <brillo/daemons/dbus_daemon.h>
+#include <shill/dbus-proxies.h>
+
+namespace setup_wifi {
+
+class DBusClient : public brillo::DBusDaemon {
+ public:
+ DBusClient(const std::string& ssid, const std::string& psk, bool is_hex_ssid,
+ int timeout);
+ ~DBusClient() override {}
+
+ protected:
+ int OnInit() override;
+
+ private:
+ bool ConfigureAndConnect();
+ void PostCheckWifiStatusTask();
+ void QuitIfOnline();
+ bool IsOnline();
+ std::map<std::string, brillo::Any> GetServiceConfig();
+
+ std::unique_ptr<org::chromium::flimflam::ServiceProxy> shill_service_proxy_;
+ std::string ssid_;
+ std::string psk_;
+ bool is_hex_ssid_;
+ int timeout_;
+};
+
+} // namespace setup_wifi
+
+#endif // SHILL_SETUP_WIFI_DBUS_CLIENT_H_
diff --git a/setup_wifi/main.cc b/setup_wifi/main.cc
index 647213fe..949cda1e 100644
--- a/setup_wifi/main.cc
+++ b/setup_wifi/main.cc
@@ -23,24 +23,18 @@
#include <base/command_line.h>
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>
-#include <brillo/any.h>
-#include <brillo/daemons/dbus_daemon.h>
-#if defined(__ANDROID__)
-#include <dbus/service_constants.h>
-#else
-#include <chromeos/dbus/service_constants.h>
-#endif // __ANDROID__
-#include <shill/dbus-proxies.h>
+#include <brillo/daemons/daemon.h>
-namespace {
+#include "shill/setup_wifi/dbus_client.h"
-namespace switches {
+namespace {
static const char kHelp[] = "help";
static const char kPassphrase[] = "passphrase";
static const char kHexSsid[] = "hex-ssid";
static const char kSSID[] = "ssid";
static const char kTimeOut[] = "wait-for-online-seconds";
-static const char kHelpMessage[] = "\n"
+static const char kHelpMessage[] =
+ "\n"
"Available Switches: \n"
" --ssid=<ssid>\n"
" Set the SSID to configure (mandatory).\n"
@@ -50,145 +44,42 @@ static const char kHelpMessage[] = "\n"
" Set the passphrase for PSK networks\n"
" --wait-for-online-seconds=<seconds>\n"
" Number of seconds to wait to connect the SSID\n";
-} // namespace switches
-
-static const char kOnlineState[] = "online";
-static const int kTimeoutBetweenStateChecksMs = 100;
-
} // namespace
-class MyClient : public brillo::DBusDaemon {
- public:
- MyClient(std::string ssid, bool is_hex_ssid, std::string psk, int timeout)
- : ssid_(ssid), is_hex_ssid_(is_hex_ssid), psk_(psk), timeout_(timeout) {}
- ~MyClient() override = default;
-
- protected:
- int OnInit() override {
- int ret = DBusDaemon::OnInit();
- if (ret != EX_OK) {
- return ret;
- }
- ConfigureAndConnect();
- // Timeout if we can't get online.
- brillo::MessageLoop::current()->PostDelayedTask(
- base::Bind(&MyClient::Quit, base::Unretained(this)),
- base::TimeDelta::FromSeconds(timeout_));
- return EX_OK;
- }
-
- bool ConfigureAndConnect() {
- std::unique_ptr<org::chromium::flimflam::ManagerProxy> shill_manager_proxy(
- new org::chromium::flimflam::ManagerProxy(bus_));
-
- dbus::ObjectPath created_service;
- brillo::ErrorPtr configure_error;
- if (!shill_manager_proxy->ConfigureService(
- GetServiceConfig(), &created_service, &configure_error)) {
- LOG(ERROR) << "Configure service failed";
- return false;
- }
-
- brillo::ErrorPtr connect_error;
- shill_service_proxy_ =
- std::unique_ptr<org::chromium::flimflam::ServiceProxy>(
- new org::chromium::flimflam::ServiceProxy(bus_, created_service));
- if (!shill_service_proxy_->Connect(&connect_error)) {
- LOG(ERROR) << "Connect service failed";
- return false;
- }
-
- PostCheckWifiStatusTask();
- return true;
- }
-
- void PostCheckWifiStatusTask() {
- LOG(INFO) << "Sleeping now. Will check wifi status in 100 ms.";
- brillo::MessageLoop::current()->PostDelayedTask(
- base::Bind(&MyClient::QuitIfOnline, base::Unretained(this)),
- base::TimeDelta::FromMilliseconds(kTimeoutBetweenStateChecksMs));
- }
-
- // Check if the device is online. If it is, quit.
- void QuitIfOnline() {
- if (IsOnline())
- Quit();
- else
- PostCheckWifiStatusTask();
- }
-
- bool IsOnline() {
- brillo::VariantDictionary properties;
- if (!shill_service_proxy_->GetProperties(&properties, nullptr)) {
- LOG(ERROR) << "Cannot get properties.";
- PostCheckWifiStatusTask();
- return false;
- }
- auto property_it = properties.find(shill::kStateProperty);
- if (property_it == properties.end()) {
- PostCheckWifiStatusTask();
- return false;
- }
-
- std::string state = property_it->second.TryGet<std::string>();
- return state == kOnlineState;
- }
-
- std::map<std::string, brillo::Any> GetServiceConfig() {
- std::map<std::string, brillo::Any> configure_dict;
- configure_dict[shill::kTypeProperty] = shill::kTypeWifi;
- if (is_hex_ssid_) {
- configure_dict[shill::kWifiHexSsid] = ssid_;
- } else {
- configure_dict[shill::kSSIDProperty] = ssid_;
- }
- if (!psk_.empty()) {
- configure_dict[shill::kPassphraseProperty] = psk_;
- configure_dict[shill::kSecurityProperty] = shill::kSecurityPsk;
- }
- return configure_dict;
- }
-
- std::unique_ptr<org::chromium::flimflam::ServiceProxy> shill_service_proxy_;
- std::string ssid_;
- bool is_hex_ssid_;
- std::string psk_;
- int timeout_;
-};
-
int main(int argc, char** argv) {
base::CommandLine::Init(argc, argv);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
- if (cl->HasSwitch(switches::kHelp)) {
- LOG(INFO) << switches::kHelpMessage;
+ if (cl->HasSwitch(kHelp)) {
+ LOG(INFO) << kHelpMessage;
return EXIT_SUCCESS;
}
- if (!cl->HasSwitch(switches::kSSID)) {
+ if (!cl->HasSwitch(kSSID)) {
LOG(ERROR) << "ssid switch is mandatory.";
- LOG(ERROR) << switches::kHelpMessage;
+ LOG(ERROR) << kHelpMessage;
return EXIT_FAILURE;
}
- std::string ssid = cl->GetSwitchValueASCII(switches::kSSID);
+ std::string ssid = cl->GetSwitchValueASCII(kSSID);
std::string psk;
- if (cl->HasSwitch(switches::kPassphrase)) {
- psk = cl->GetSwitchValueASCII(switches::kPassphrase);
+ if (cl->HasSwitch(kPassphrase)) {
+ psk = cl->GetSwitchValueASCII(kPassphrase);
}
- bool hex_ssid = cl->HasSwitch(switches::kHexSsid);
+ bool is_hex_ssid = cl->HasSwitch(kHexSsid);
int timeout = 0;
- if (cl->HasSwitch(switches::kTimeOut)) {
- auto value = cl->GetSwitchValueASCII(switches::kTimeOut);
- if (!base::StringToInt(value, &timeout)) {
- LOG(ERROR) << "Timeout value invalid";
- return EXIT_FAILURE;
- }
+ if (cl->HasSwitch(kTimeOut)) {
+ auto value = cl->GetSwitchValueASCII(kTimeOut);
+ if (!base::StringToInt(value, &timeout)) {
+ LOG(ERROR) << "Timeout value invalid";
+ return EXIT_FAILURE;
+ }
}
- MyClient client(ssid, hex_ssid, psk, timeout);
- client.Run();
+ std::unique_ptr<brillo::Daemon> client(
+ new setup_wifi::DBusClient(ssid, psk, is_hex_ssid, timeout));
+ client->Run();
LOG(INFO) << "Process exiting.";
return EXIT_SUCCESS;