Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / wifi / wifi_test.cc
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.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/cancelable_callback.h"
11 #include "base/command_line.h"
12 #include "base/files/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/run_loop.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/time.h"
23 #include "components/wifi/wifi_service.h"
24
25 #if defined(OS_MACOSX)
26 #include "base/mac/scoped_nsautorelease_pool.h"
27 #endif
28
29 namespace wifi {
30
31 class WiFiTest {
32  public:
33   WiFiTest() {}
34   ~WiFiTest() {}
35
36   enum Result {
37     RESULT_ERROR = -2,
38     RESULT_WRONG_USAGE = -1,
39     RESULT_OK = 0,
40     RESULT_PENDING = 1,
41   };
42
43   Result Main(int argc, const char* argv[]);
44
45  private:
46   bool ParseCommandLine(int argc, const char* argv[]);
47
48   void Start() {}
49   void Finish(Result result) {
50     DCHECK_NE(RESULT_PENDING, result);
51     result_ = result;
52     if (base::MessageLoop::current())
53       base::MessageLoop::current()->Quit();
54   }
55
56   void OnNetworksChanged(
57       const WiFiService::NetworkGuidList& network_guid_list) {
58     VLOG(0) << "Networks Changed: " << network_guid_list[0];
59     base::DictionaryValue properties;
60     std::string error;
61     wifi_service_->GetProperties(network_guid_list[0], &properties, &error);
62     VLOG(0) << error << ":\n" << properties;
63   }
64
65   void OnNetworkListChanged(
66       const WiFiService::NetworkGuidList& network_guid_list) {
67     VLOG(0) << "Network List Changed: " << network_guid_list.size();
68   }
69
70 #if defined(OS_MACOSX)
71   // Without this there will be a mem leak on osx.
72   base::mac::ScopedNSAutoreleasePool scoped_pool_;
73 #endif
74
75   scoped_ptr<WiFiService> wifi_service_;
76
77   // Need AtExitManager to support AsWeakPtr (in NetLog).
78   base::AtExitManager exit_manager_;
79
80   Result result_;
81 };
82
83 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) {
84   if (!ParseCommandLine(argc, argv)) {
85     VLOG(0) <<  "Usage: " << argv[0] <<
86                 " [--list]"
87                 " [--get_key]"
88                 " [--get_properties]"
89                 " [--create]"
90                 " [--connect]"
91                 " [--disconnect]"
92                 " [--scan]"
93                 " [--network_guid=<network_guid>]"
94                 " [--frequency=0|2400|5000]"
95                 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
96                 " [--password=<wifi_password>]"
97                 " [<network_guid>]\n";
98     return RESULT_WRONG_USAGE;
99   }
100
101   base::MessageLoopForIO loop;
102   result_ = RESULT_PENDING;
103
104   return result_;
105 }
106
107 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
108   CommandLine::Init(argc, argv);
109   const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
110   std::string network_guid =
111       parsed_command_line.GetSwitchValueASCII("network_guid");
112   std::string frequency =
113       parsed_command_line.GetSwitchValueASCII("frequency");
114   std::string password =
115       parsed_command_line.GetSwitchValueASCII("password");
116   std::string security =
117       parsed_command_line.GetSwitchValueASCII("security");
118
119   if (parsed_command_line.GetArgs().size() == 1) {
120 #if defined(OS_WIN)
121     network_guid = base::UTF16ToASCII(parsed_command_line.GetArgs()[0]);
122 #else
123     network_guid = parsed_command_line.GetArgs()[0];
124 #endif
125   }
126
127 #if defined(OS_WIN)
128   if (parsed_command_line.HasSwitch("debug"))
129     MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
130 #endif
131
132   base::MessageLoopForIO loop;
133
134   wifi_service_.reset(WiFiService::Create());
135   wifi_service_->Initialize(loop.message_loop_proxy());
136
137   if (parsed_command_line.HasSwitch("list")) {
138     base::ListValue network_list;
139     wifi_service_->GetVisibleNetworks(std::string(), &network_list, true);
140     VLOG(0) << network_list;
141     return true;
142   }
143
144   if (parsed_command_line.HasSwitch("get_properties")) {
145     if (network_guid.length() > 0) {
146       base::DictionaryValue properties;
147       std::string error;
148       wifi_service_->GetProperties(network_guid, &properties, &error);
149       VLOG(0) << error << ":\n" << properties;
150       return true;
151     }
152   }
153
154   // Optional properties (frequency, password) to use for connect or create.
155   scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue());
156
157   if (!frequency.empty()) {
158     int value = 0;
159     if (base::StringToInt(frequency, &value)) {
160       properties->SetInteger("WiFi.Frequency", value);
161       // fall through to connect.
162     }
163   }
164
165   if (!password.empty())
166     properties->SetString("WiFi.Passphrase", password);
167
168   if (!security.empty())
169     properties->SetString("WiFi.Security", security);
170
171   if (parsed_command_line.HasSwitch("create")) {
172     if (!network_guid.empty()) {
173       std::string error;
174       std::string new_network_guid;
175       properties->SetString("WiFi.SSID", network_guid);
176       VLOG(0) << "Creating Network: " << *properties;
177       wifi_service_->CreateNetwork(
178           false, properties.Pass(), &new_network_guid, &error);
179       VLOG(0) << error << ":\n" << new_network_guid;
180       return true;
181     }
182   }
183
184   if (parsed_command_line.HasSwitch("connect")) {
185     if (!network_guid.empty()) {
186       std::string error;
187       if (!properties->empty()) {
188         VLOG(0) << "Using connect properties: " << *properties;
189         wifi_service_->SetProperties(network_guid, properties.Pass(), &error);
190       }
191
192       wifi_service_->SetEventObservers(
193           loop.message_loop_proxy(),
194           base::Bind(&WiFiTest::OnNetworksChanged, base::Unretained(this)),
195           base::Bind(&WiFiTest::OnNetworkListChanged, base::Unretained(this)));
196
197       wifi_service_->StartConnect(network_guid, &error);
198       VLOG(0) << error;
199       if (error.empty())
200         base::MessageLoop::current()->Run();
201       return true;
202     }
203   }
204
205   if (parsed_command_line.HasSwitch("disconnect")) {
206     if (network_guid.length() > 0) {
207       std::string error;
208       wifi_service_->StartDisconnect(network_guid, &error);
209       VLOG(0) << error;
210       return true;
211     }
212   }
213
214   if (parsed_command_line.HasSwitch("get_key")) {
215     if (network_guid.length() > 0) {
216       std::string error;
217       std::string key_data;
218       wifi_service_->GetKeyFromSystem(network_guid, &key_data, &error);
219       VLOG(0) << key_data << error;
220       return true;
221     }
222   }
223
224   if (parsed_command_line.HasSwitch("scan")) {
225     wifi_service_->SetEventObservers(
226         loop.message_loop_proxy(),
227         base::Bind(&WiFiTest::OnNetworksChanged, base::Unretained(this)),
228         base::Bind(&WiFiTest::OnNetworkListChanged, base::Unretained(this)));
229     wifi_service_->RequestNetworkScan();
230     base::MessageLoop::current()->Run();
231     return true;
232   }
233
234   return false;
235 }
236
237 }  // namespace wifi
238
239 int main(int argc, const char* argv[]) {
240   CommandLine::Init(argc, argv);
241   logging::LoggingSettings settings;
242   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
243   logging::InitLogging(settings);
244
245   wifi::WiFiTest wifi_test;
246   return wifi_test.Main(argc, argv);
247 }