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/time/time.h"
21 #include "components/wifi/wifi_service.h"
23 #if defined(OS_MACOSX)
24 #include "base/mac/scoped_nsautorelease_pool.h"
36 RESULT_WRONG_USAGE
= -1,
41 Result
Main(int argc
, const char* argv
[]);
44 bool ParseCommandLine(int argc
, const char* argv
[]);
47 void Finish(Result result
) {
48 DCHECK_NE(RESULT_PENDING
, result
);
50 if (base::MessageLoop::current())
51 base::MessageLoop::current()->Quit();
54 #if defined(OS_MACOSX)
55 // Without this there will be a mem leak on osx.
56 base::mac::ScopedNSAutoreleasePool scoped_pool_
;
59 // Need AtExitManager to support AsWeakPtr (in NetLog).
60 base::AtExitManager exit_manager_
;
65 WiFiTest::Result
WiFiTest::Main(int argc
, const char* argv
[]) {
66 if (!ParseCommandLine(argc
, argv
)) {
67 VLOG(0) << "Usage: " << argv
[0] <<
74 " [--network_guid=<network_guid>]"
75 " [--frequency=0|2400|5000]"
76 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
77 " [--password=<wifi_password>]"
78 " [<network_guid>]\n";
79 return RESULT_WRONG_USAGE
;
82 base::MessageLoopForIO loop
;
83 result_
= RESULT_PENDING
;
88 bool WiFiTest::ParseCommandLine(int argc
, const char* argv
[]) {
89 CommandLine::Init(argc
, argv
);
90 const CommandLine
& parsed_command_line
= *CommandLine::ForCurrentProcess();
91 std::string network_guid
=
92 parsed_command_line
.GetSwitchValueASCII("network_guid");
93 std::string frequency
=
94 parsed_command_line
.GetSwitchValueASCII("frequency");
95 std::string password
=
96 parsed_command_line
.GetSwitchValueASCII("password");
97 std::string security
=
98 parsed_command_line
.GetSwitchValueASCII("security");
100 if (parsed_command_line
.GetArgs().size() == 1) {
102 network_guid
= WideToASCII(parsed_command_line
.GetArgs()[0]);
104 network_guid
= parsed_command_line
.GetArgs()[0];
109 if (parsed_command_line
.HasSwitch("debug"))
110 MessageBoxA(NULL
, __FUNCTION__
, "Debug Me!", MB_OK
);
113 #if defined(OS_WIN) || defined(OS_MACOSX)
114 scoped_ptr
<WiFiService
> wifi_service(WiFiService::Create());
116 scoped_ptr
<WiFiService
> wifi_service(WiFiService::CreateForTest());
119 wifi_service
->Initialize(NULL
);
121 if (parsed_command_line
.HasSwitch("list")) {
122 base::ListValue network_list
;
123 wifi_service
->GetVisibleNetworks(std::string(), &network_list
);
124 VLOG(0) << network_list
;
128 if (parsed_command_line
.HasSwitch("get_properties")) {
129 if (network_guid
.length() > 0) {
130 base::DictionaryValue properties
;
132 wifi_service
->GetProperties(network_guid
, &properties
, &error
);
133 VLOG(0) << error
<< ":\n" << properties
;
138 // Optional properties (frequency, password) to use for connect or create.
139 scoped_ptr
<base::DictionaryValue
> properties(new base::DictionaryValue());
141 if (!frequency
.empty()) {
143 if (base::StringToInt(frequency
, &value
)) {
144 properties
->SetInteger("WiFi.Frequency", value
);
145 // fall through to connect.
149 if (!password
.empty())
150 properties
->SetString("WiFi.Passphrase", password
);
152 if (!security
.empty())
153 properties
->SetString("WiFi.Security", security
);
155 if (parsed_command_line
.HasSwitch("create")) {
156 if (!network_guid
.empty()) {
158 std::string new_network_guid
;
159 properties
->SetString("WiFi.SSID", network_guid
);
160 VLOG(0) << "Creating Network: " << *properties
;
161 wifi_service
->CreateNetwork(false,
165 VLOG(0) << error
<< ":\n" << new_network_guid
;
170 if (parsed_command_line
.HasSwitch("connect")) {
171 if (!network_guid
.empty()) {
173 if (!properties
->empty()) {
174 VLOG(0) << "Using connect properties: " << *properties
;
175 wifi_service
->SetProperties(network_guid
,
179 wifi_service
->StartConnect(network_guid
, &error
);
185 if (parsed_command_line
.HasSwitch("disconnect")) {
186 if (network_guid
.length() > 0) {
188 wifi_service
->StartDisconnect(network_guid
, &error
);
194 if (parsed_command_line
.HasSwitch("get_key")) {
195 if (network_guid
.length() > 0) {
197 std::string key_data
;
198 wifi_service
->GetKeyFromSystem(network_guid
,
201 VLOG(0) << key_data
<< error
;
211 int main(int argc
, const char* argv
[]) {
212 CommandLine::Init(argc
, argv
);
213 logging::LoggingSettings settings
;
214 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
215 logging::InitLogging(settings
);
217 wifi::WiFiTest wifi_test
;
218 return wifi_test
.Main(argc
, argv
);