blob: e304baca99daac4cac6b12f261e5c6d837b54161 [file] [log] [blame]
Paul Stewart523f4032015-01-16 16:07:34 -08001// Copyright 2015 The Chromium OS 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 <sysexits.h>
6
7#include <map>
8#include <memory>
9#include <string>
10
11#include <base/command_line.h>
12#include <base/logging.h>
13#include <chromeos/any.h>
14#include <chromeos/daemons/dbus_daemon.h>
15#include <chromeos/dbus/service_constants.h>
Alex Deymo448b95b2015-08-28 11:50:19 -070016#include <shill/dbus-proxies.h>
Paul Stewart523f4032015-01-16 16:07:34 -080017
18namespace {
19
20namespace switches {
21static const char kHelp[] = "help";
22static const char kPassphrase[] = "passphrase";
23static const char kHexSsid[] = "hex-ssid";
24static const char kSSID[] = "ssid";
25static const char kHelpMessage[] = "\n"
26 "Available Switches: \n"
27 " --ssid=<ssid>\n"
28 " Set the SSID to configure (mandatory).\n"
29 " --hex-ssid\n"
30 " SSID is provided in hexadecimal\n"
31 " --passphrase=<passprhase>\n"
32 " Set the passphrase for PSK networks\n";
33} // namespace switches
34
35} // namespace
36
37class MyClient : public chromeos::DBusDaemon {
38 public:
39 MyClient(std::string ssid, bool is_hex_ssid, std::string psk)
40 : ssid_(ssid), is_hex_ssid_(is_hex_ssid), psk_(psk) {}
41 ~MyClient() override = default;
42
43 protected:
44 int OnInit() override {
45 int ret = DBusDaemon::OnInit();
46 if (ret != EX_OK) {
47 return ret;
48 }
49 ConfigureAndConnect();
50 Quit();
51 return EX_OK;
52 }
53
54 bool ConfigureAndConnect() {
55 std::unique_ptr<org::chromium::flimflam::ManagerProxy> shill_manager_proxy(
Alex Deymo448b95b2015-08-28 11:50:19 -070056 new org::chromium::flimflam::ManagerProxy(bus_));
Paul Stewart523f4032015-01-16 16:07:34 -080057
58 dbus::ObjectPath created_service;
59 chromeos::ErrorPtr configure_error;
60 if (!shill_manager_proxy->ConfigureService(
61 GetServiceConfig(), &created_service, &configure_error)) {
62 LOG(ERROR) << "Configure service failed";
63 return false;
64 }
65
66 chromeos::ErrorPtr connect_error;
67 std::unique_ptr<org::chromium::flimflam::ServiceProxy> shill_service_proxy(
Alex Deymo448b95b2015-08-28 11:50:19 -070068 new org::chromium::flimflam::ServiceProxy(bus_,
69 created_service));
Paul Stewart523f4032015-01-16 16:07:34 -080070 if (!shill_service_proxy->Connect(&connect_error)) {
71 LOG(ERROR) << "Connect service failed";
72 return false;
73 }
74
75 // TODO(pstew): Monitor service as it attempts to connect.
76
77 return true;
78 }
79
80 std::map<std::string, chromeos::Any> GetServiceConfig() {
81 std::map<std::string, chromeos::Any> configure_dict;
82 configure_dict[shill::kTypeProperty] = shill::kTypeWifi;
83 if (is_hex_ssid_) {
84 configure_dict[shill::kWifiHexSsid] = ssid_;
85 } else {
86 configure_dict[shill::kSSIDProperty] = ssid_;
87 }
88 if (!psk_.empty()) {
89 configure_dict[shill::kPassphraseProperty] = psk_;
90 configure_dict[shill::kSecurityProperty] = shill::kSecurityPsk;
91 }
92 return configure_dict;
93 }
94
95 std::string ssid_;
96 bool is_hex_ssid_;
97 std::string psk_;
98};
99
100int main(int argc, char** argv) {
Alex Vakulenko127f2562015-04-02 14:31:10 -0700101 base::CommandLine::Init(argc, argv);
102 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
Paul Stewart523f4032015-01-16 16:07:34 -0800103
104 if (cl->HasSwitch(switches::kHelp)) {
105 LOG(INFO) << switches::kHelpMessage;
106 return EXIT_SUCCESS;
107 }
108
109 if (!cl->HasSwitch(switches::kSSID)) {
110 LOG(ERROR) << "ssid switch is mandatory.";
111 LOG(ERROR) << switches::kHelpMessage;
112 return EXIT_FAILURE;
113 }
114
115 std::string ssid = cl->GetSwitchValueASCII(switches::kSSID);
116 std::string psk;
117 if (cl->HasSwitch(switches::kPassphrase)) {
118 psk = cl->GetSwitchValueASCII(switches::kPassphrase);
119 }
120 bool hex_ssid = cl->HasSwitch(switches::kHexSsid);
121
122 MyClient client(ssid, hex_ssid, psk);
123 client.Run();
124 LOG(INFO) << "Process exiting.";
125
126 return EXIT_SUCCESS;
127}
128