Home | History | Annotate | Download | only in wifi
      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/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"
     22 
     23 #if defined(OS_MACOSX)
     24 #include "base/mac/scoped_nsautorelease_pool.h"
     25 #endif
     26 
     27 namespace wifi {
     28 
     29 class WiFiTest {
     30  public:
     31   WiFiTest() {}
     32   ~WiFiTest() {}
     33 
     34   enum Result {
     35     RESULT_ERROR = -2,
     36     RESULT_WRONG_USAGE = -1,
     37     RESULT_OK = 0,
     38     RESULT_PENDING = 1,
     39   };
     40 
     41   Result Main(int argc, const char* argv[]);
     42 
     43  private:
     44   bool ParseCommandLine(int argc, const char* argv[]);
     45 
     46   void Start() {}
     47   void Finish(Result result) {
     48     DCHECK_NE(RESULT_PENDING, result);
     49     result_ = result;
     50     if (base::MessageLoop::current())
     51       base::MessageLoop::current()->Quit();
     52   }
     53 
     54 #if defined(OS_MACOSX)
     55   // Without this there will be a mem leak on osx.
     56   base::mac::ScopedNSAutoreleasePool scoped_pool_;
     57 #endif
     58 
     59   // Need AtExitManager to support AsWeakPtr (in NetLog).
     60   base::AtExitManager exit_manager_;
     61 
     62   Result result_;
     63 };
     64 
     65 WiFiTest::Result WiFiTest::Main(int argc, const char* argv[]) {
     66   if (!ParseCommandLine(argc, argv)) {
     67     VLOG(0) <<  "Usage: " << argv[0] <<
     68                 " [--list]"
     69                 " [--get_properties]"
     70                 " [--create]"
     71                 " [--connect]"
     72                 " [--disconnect]"
     73                 " [--network_guid=<network_guid>]"
     74                 " [--frequency=0|2400|5000]"
     75                 " [--security=none|WEP-PSK|WPA-PSK|WPA2-PSK]"
     76                 " [--password=<wifi password>]"
     77                 " [<network_guid>]\n";
     78     return RESULT_WRONG_USAGE;
     79   }
     80 
     81   base::MessageLoopForIO loop;
     82   result_ = RESULT_PENDING;
     83 
     84   return result_;
     85 }
     86 
     87 bool WiFiTest::ParseCommandLine(int argc, const char* argv[]) {
     88   CommandLine::Init(argc, argv);
     89   const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
     90   std::string network_guid =
     91       parsed_command_line.GetSwitchValueASCII("network_guid");
     92   std::string frequency =
     93       parsed_command_line.GetSwitchValueASCII("frequency");
     94   std::string password =
     95       parsed_command_line.GetSwitchValueASCII("password");
     96   std::string security =
     97       parsed_command_line.GetSwitchValueASCII("security");
     98 
     99   if (parsed_command_line.GetArgs().size() == 1) {
    100 #if defined(OS_WIN)
    101     network_guid = WideToASCII(parsed_command_line.GetArgs()[0]);
    102 #else
    103     network_guid = parsed_command_line.GetArgs()[0];
    104 #endif
    105   }
    106 
    107 #if defined(OS_WIN)
    108   if (parsed_command_line.HasSwitch("debug"))
    109     MessageBoxA(NULL, __FUNCTION__, "Debug Me!", MB_OK);
    110 #endif
    111 
    112 #if defined(OS_WIN)
    113   scoped_ptr<WiFiService> wifi_service(WiFiService::Create());
    114 #else
    115   scoped_ptr<WiFiService> wifi_service(WiFiService::CreateForTest());
    116 #endif
    117 
    118   wifi_service->Initialize(NULL);
    119 
    120   if (parsed_command_line.HasSwitch("list")) {
    121     ListValue network_list;
    122     wifi_service->GetVisibleNetworks(std::string(), &network_list);
    123     VLOG(0) << network_list;
    124     return true;
    125   }
    126 
    127   if (parsed_command_line.HasSwitch("get_properties")) {
    128     if (network_guid.length() > 0) {
    129       DictionaryValue properties;
    130       std::string error;
    131       wifi_service->GetProperties(network_guid, &properties, &error);
    132       VLOG(0) << error << ":\n" << properties;
    133       return true;
    134     }
    135   }
    136 
    137   // Optional properties (frequency, password) to use for connect or create.
    138   scoped_ptr<DictionaryValue> properties(new DictionaryValue());
    139 
    140   if (!frequency.empty()) {
    141     int value = 0;
    142     if (base::StringToInt(frequency, &value)) {
    143       properties->SetInteger("WiFi.Frequency", value);
    144       // fall through to connect.
    145     }
    146   }
    147 
    148   if (!password.empty())
    149     properties->SetString("WiFi.Passphrase", password);
    150 
    151   if (!security.empty())
    152     properties->SetString("WiFi.Security", security);
    153 
    154   if (parsed_command_line.HasSwitch("create")) {
    155     if (!network_guid.empty()) {
    156       std::string error;
    157       std::string new_network_guid;
    158       properties->SetString("WiFi.SSID", network_guid);
    159       VLOG(0) << "Creating Network: " << *properties;
    160       wifi_service->CreateNetwork(false,
    161                                   properties.Pass(),
    162                                   &new_network_guid,
    163                                   &error);
    164       VLOG(0) << error << ":\n" << new_network_guid;
    165       return true;
    166     }
    167   }
    168 
    169   if (parsed_command_line.HasSwitch("connect")) {
    170     if (!network_guid.empty()) {
    171       std::string error;
    172       if (!properties->empty()) {
    173         VLOG(0) << "Using connect properties: " << *properties;
    174         wifi_service->SetProperties(network_guid,
    175                                     properties.Pass(),
    176                                     &error);
    177       }
    178       wifi_service->StartConnect(network_guid, &error);
    179       VLOG(0) << error;
    180       return true;
    181     }
    182   }
    183 
    184   if (parsed_command_line.HasSwitch("disconnect")) {
    185     if (network_guid.length() > 0) {
    186       std::string error;
    187       wifi_service->StartDisconnect(network_guid, &error);
    188       VLOG(0) << error;
    189       return true;
    190     }
    191   }
    192 
    193   return false;
    194 }
    195 
    196 }  // namespace wifi
    197 
    198 int main(int argc, const char* argv[]) {
    199   CommandLine::Init(argc, argv);
    200   logging::LoggingSettings settings;
    201   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
    202   logging::InitLogging(settings);
    203 
    204   wifi::WiFiTest wifi_test;
    205   return wifi_test.Main(argc, argv);
    206 }
    207