OLD | NEW |
(Empty) | |
| 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 <iostream> |
| 7 #include <string> |
| 8 |
| 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/cancelable_callback.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop/message_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/time/time.h" |
| 22 #include "components/wifi/wifi_service.h" |
| 23 |
| 24 #if defined(OS_MACOSX) |
| 25 #include "base/mac/scoped_nsautorelease_pool.h" |
| 26 #endif |
| 27 |
| 28 namespace wifi { |
| 29 |
| 30 class WiFiTest { |
| 31 public: |
| 32 WiFiTest() {} |
| 33 ~WiFiTest() {} |
| 34 |
| 35 enum Result { |
| 36 RESULT_ERROR = -2, |
| 37 RESULT_WRONG_USAGE = -1, |
| 38 RESULT_OK = 0, |
| 39 RESULT_PENDING = 1, |
| 40 }; |
| 41 |
| 42 Result Main(int argc, const char* argv[]); |
| 43 |
| 44 private: |
| 45 bool ParseCommandLine(int argc, const char* argv[]); |
| 46 |
| 47 void Start() {} |
| 48 void Finish(Result result) { |
| 49 DCHECK_NE(RESULT_PENDING, result); |
| 50 result_ = result; |
| 51 if (base::MessageLoop::current()) |
| 52 base::MessageLoop::current()->Quit(); |
| 53 } |
| 54 |
| 55 #if defined(OS_MACOSX) |
| 56 // Without this there will be a mem leak on osx. |
| 57 base::mac::ScopedNSAutoreleasePool scoped_pool_; |
| 58 #endif |
| 59 |
| 60 // Need AtExitManager to support AsWeakPtr (in NetLog). |
| 61 base::AtExitManager exit_manager_; |
| 62 |
| 63 Result result_; |
| 64 }; |
| 65 |
| 66 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) { |
| 67 if (!ParseCommandLine(argc, argv)) { |
| 68 fprintf(stderr, |
| 69 "usage: %s [--list]" |
| 70 " [--get_properties]" |
| 71 " [--connect]" |
| 72 " [--disconnect]" |
| 73 " [--network_guid=<network_guid>]" |
| 74 " [--frequency=0|2400|5000]" |
| 75 " [<network_guid>]\n", |
| 76 argv[0]); |
| 77 return RESULT_WRONG_USAGE; |
| 78 } |
| 79 |
| 80 base::MessageLoopForIO loop; |
| 81 result_ = RESULT_PENDING; |
| 82 |
| 83 return result_; |
| 84 } |
| 85 |
| 86 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) { |
| 87 CommandLine::Init(argc, argv); |
| 88 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 89 std::string network_guid = |
| 90 parsed_command_line.GetSwitchValueASCII("network_guid"); |
| 91 |
| 92 if (parsed_command_line.GetArgs().size() == 1) { |
| 93 #if defined(OS_WIN) |
| 94 network_guid = WideToASCII(parsed_command_line.GetArgs()[0]); |
| 95 #else |
| 96 network_guid = parsed_command_line.GetArgs()[0]; |
| 97 #endif |
| 98 } |
| 99 |
| 100 #if defined(OS_WIN) |
| 101 if (parsed_command_line.HasSwitch("debug")) |
| 102 MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK); |
| 103 #endif |
| 104 |
| 105 #if defined(OS_WIN) |
| 106 scoped_ptr<WiFiService> wifi_service(WiFiService::Create()); |
| 107 #else |
| 108 scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest()); |
| 109 #endif |
| 110 |
| 111 wifi_service->Initialize(NULL); |
| 112 |
| 113 if (parsed_command_line.HasSwitch("list")) { |
| 114 ListValue network_list; |
| 115 wifi_service->GetVisibleNetworks(&network_list); |
| 116 std::cout << network_list; |
| 117 return true; |
| 118 } |
| 119 |
| 120 if (parsed_command_line.HasSwitch("get_properties")) { |
| 121 if (network_guid.length() > 0) { |
| 122 DictionaryValue properties; |
| 123 std::string error; |
| 124 wifi_service->GetProperties(network_guid, &properties, &error); |
| 125 std::cout << error << ":\n" << properties; |
| 126 return true; |
| 127 } |
| 128 } |
| 129 |
| 130 if (parsed_command_line.HasSwitch("connect")) { |
| 131 if (network_guid.length() > 0) { |
| 132 std::string error; |
| 133 wifi_service->StartConnect(network_guid, &error); |
| 134 std::cout << error; |
| 135 return true; |
| 136 } |
| 137 } |
| 138 |
| 139 if (parsed_command_line.HasSwitch("disconnect")) { |
| 140 if (network_guid.length() > 0) { |
| 141 std::string error; |
| 142 wifi_service->StartDisconnect(network_guid, &error); |
| 143 std::cout << error; |
| 144 return true; |
| 145 } |
| 146 } |
| 147 |
| 148 return false; |
| 149 } |
| 150 |
| 151 } // namespace wifi |
| 152 |
| 153 int main(int argc, const char* argv[]) { |
| 154 wifi::WiFiTest wifi_test; |
| 155 return wifi_test.Main(argc, argv); |
| 156 } |
OLD | NEW |