1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 #include "base/at_exit.h"
10 #include "base/cancelable_callback.h"
11 #include "base/command_line.h"
12 #include "base/file_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "components/wifi/wifi_service.h"
24 #if defined(OS_MACOSX)
25 #include "base/mac/scoped_nsautorelease_pool.h"
37 RESULT_WRONG_USAGE
= -1,
42 Result
Main(int argc
, const char* argv
[]);
45 bool ParseCommandLine(int argc
, const char* argv
[]);
48 void Finish(Result result
) {
49 DCHECK_NE(RESULT_PENDING
, result
);
51 if (base::MessageLoop::current())
52 base::MessageLoop::current()->Quit();
55 #if defined(OS_MACOSX)
56 // Without this there will be a mem leak on osx.
57 base::mac::ScopedNSAutoreleasePool scoped_pool_
;
60 // Need AtExitManager to support AsWeakPtr (in NetLog).
61 base::AtExitManager exit_manager_
;
66 WiFiTest::Result
WiFiTest::Main(int argc
, const char* argv
[]) {
67 if (!ParseCommandLine(argc
, argv
)) {
68 VLOG(0) << "Usage: " << argv
[0] <<
75 " [--network_guid=<network_guid>]"
76 " [--frequency=0|2400|5000]"
77 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
78 " [--password=<wifi_password>]"
79 " [<network_guid>]\n";
80 return RESULT_WRONG_USAGE
;
83 base::MessageLoopForIO loop
;
84 result_
= RESULT_PENDING
;
89 bool WiFiTest::ParseCommandLine(int argc
, const char* argv
[]) {
90 CommandLine::Init(argc
, argv
);
91 const CommandLine
& parsed_command_line
= *CommandLine::ForCurrentProcess();
92 std::string network_guid
=
93 parsed_command_line
.GetSwitchValueASCII("network_guid");
94 std::string frequency
=
95 parsed_command_line
.GetSwitchValueASCII("frequency");
96 std::string password
=
97 parsed_command_line
.GetSwitchValueASCII("password");
98 std::string security
=
99 parsed_command_line
.GetSwitchValueASCII("security");
101 if (parsed_command_line
.GetArgs().size() == 1) {
103 network_guid
= base::UTF16ToASCII(parsed_command_line
.GetArgs()[0]);
105 network_guid
= parsed_command_line
.GetArgs()[0];
110 if (parsed_command_line
.HasSwitch("debug"))
111 MessageBoxA(NULL
, __FUNCTION__
, "Debug Me!", MB_OK
);
114 #if defined(OS_WIN) || defined(OS_MACOSX)
115 scoped_ptr
<WiFiService
> wifi_service(WiFiService::Create());
117 scoped_ptr
<WiFiService
> wifi_service(WiFiService::CreateForTest());
120 wifi_service
->Initialize(NULL
);
122 if (parsed_command_line
.HasSwitch("list")) {
123 base::ListValue network_list
;
124 wifi_service
->GetVisibleNetworks(std::string(), &network_list
);
125 VLOG(0) << network_list
;
129 if (parsed_command_line
.HasSwitch("get_properties")) {
130 if (network_guid
.length() > 0) {
131 base::DictionaryValue properties
;
133 wifi_service
->GetProperties(network_guid
, &properties
, &error
);
134 VLOG(0) << error
<< ":\n" << properties
;
139 // Optional properties (frequency, password) to use for connect or create.
140 scoped_ptr
<base::DictionaryValue
> properties(new base::DictionaryValue());
142 if (!frequency
.empty()) {
144 if (base::StringToInt(frequency
, &value
)) {
145 properties
->SetInteger("WiFi.Frequency", value
);
146 // fall through to connect.
150 if (!password
.empty())
151 properties
->SetString("WiFi.Passphrase", password
);
153 if (!security
.empty())
154 properties
->SetString("WiFi.Security", security
);
156 if (parsed_command_line
.HasSwitch("create")) {
157 if (!network_guid
.empty()) {
159 std::string new_network_guid
;
160 properties
->SetString("WiFi.SSID", network_guid
);
161 VLOG(0) << "Creating Network: " << *properties
;
162 wifi_service
->CreateNetwork(false,
166 VLOG(0) << error
<< ":\n" << new_network_guid
;
171 if (parsed_command_line
.HasSwitch("connect")) {
172 if (!network_guid
.empty()) {
174 if (!properties
->empty()) {
175 VLOG(0) << "Using connect properties: " << *properties
;
176 wifi_service
->SetProperties(network_guid
,
180 wifi_service
->StartConnect(network_guid
, &error
);
186 if (parsed_command_line
.HasSwitch("disconnect")) {
187 if (network_guid
.length() > 0) {
189 wifi_service
->StartDisconnect(network_guid
, &error
);
195 if (parsed_command_line
.HasSwitch("get_key")) {
196 if (network_guid
.length() > 0) {
198 std::string key_data
;
199 wifi_service
->GetKeyFromSystem(network_guid
,
202 VLOG(0) << key_data
<< error
;
212 int main(int argc
, const char* argv
[]) {
213 CommandLine::Init(argc
, argv
);
214 logging::LoggingSettings settings
;
215 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
216 logging::InitLogging(settings
);
218 wifi::WiFiTest wifi_test
;
219 return wifi_test
.Main(argc
, argv
);