Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/wifi.h" |
| 6 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 7 | #include <time.h> |
| 8 | #include <stdio.h> |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 9 | #include <string.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 10 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 11 | #include <map> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 12 | #include <string> |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 14 | |
| 15 | #include <base/logging.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/control_interface.h" |
| 19 | #include "shill/device.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 20 | #include "shill/manager.h" |
| 21 | #include "shill/profile.h" |
Darin Petkov | d196726 | 2011-07-18 14:55:18 -0700 | [diff] [blame] | 22 | #include "shill/proxy_factory.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 23 | #include "shill/shill_event.h" |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 24 | #include "shill/supplicant_interface_proxy_interface.h" |
| 25 | #include "shill/supplicant_process_proxy_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 26 | #include "shill/wifi_endpoint.h" |
| 27 | #include "shill/wifi_service.h" |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 29 | using std::string; |
| 30 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 31 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 32 | const char WiFi::kSupplicantPath[] = "/fi/w1/wpa_supplicant1"; |
| 33 | const char WiFi::kSupplicantDBusAddr[] = "fi.w1.wpa_supplicant1"; |
| 34 | const char WiFi::kSupplicantWiFiDriver[] = "nl80211"; |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 35 | const char WiFi::kSupplicantErrorInterfaceExists[] = |
| 36 | "fi.w1.wpa_supplicant1.InterfaceExists"; |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 37 | const char WiFi::kSupplicantPropertySSID[] = "ssid"; |
| 38 | const char WiFi::kSupplicantPropertyNetworkMode[] = "mode"; |
| 39 | const char WiFi::kSupplicantPropertyKeyMode[] = "key_mgmt"; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 40 | const char WiFi::kSupplicantKeyModeNone[] = "NONE"; |
| 41 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 42 | // NB: we assume supplicant is already running. [quiche.20110518] |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 43 | WiFi::WiFi(ControlInterface *control_interface, |
| 44 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 45 | Manager *manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 46 | const string& link, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 47 | int interface_index) |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 48 | : Device(control_interface, |
| 49 | dispatcher, |
| 50 | manager, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 51 | link, |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 52 | interface_index), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 53 | task_factory_(this), |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 54 | bgscan_short_interval_(0), |
| 55 | bgscan_signal_threshold_(0), |
| 56 | scan_pending_(false), |
| 57 | scan_interval_(0) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 58 | store_.RegisterString(flimflam::kBgscanMethodProperty, &bgscan_method_); |
| 59 | store_.RegisterUint16(flimflam::kBgscanShortIntervalProperty, |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 60 | &bgscan_short_interval_); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 61 | store_.RegisterInt32(flimflam::kBgscanSignalThresholdProperty, |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 62 | &bgscan_signal_threshold_); |
| 63 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 64 | // TODO(quiche): Decide if scan_pending_ is close enough to |
| 65 | // "currently scanning" that we don't care, or if we want to track |
| 66 | // scan pending/currently scanning/no scan scheduled as a tri-state |
| 67 | // kind of thing. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 68 | store_.RegisterConstBool(flimflam::kScanningProperty, &scan_pending_); |
| 69 | store_.RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 70 | VLOG(2) << "WiFi device " << link_name_ << " initialized."; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 71 | } |
| 72 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 73 | WiFi::~WiFi() {} |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 74 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 75 | void WiFi::Start() { |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 76 | ::DBus::Path interface_path; |
| 77 | |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 78 | supplicant_process_proxy_.reset( |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 79 | ProxyFactory::factory()->CreateSupplicantProcessProxy( |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 80 | kSupplicantPath, kSupplicantDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 81 | try { |
| 82 | std::map<string, DBus::Variant> create_interface_args; |
| 83 | create_interface_args["Ifname"].writer(). |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 84 | append_string(link_name_.c_str()); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 85 | create_interface_args["Driver"].writer(). |
| 86 | append_string(kSupplicantWiFiDriver); |
| 87 | // TODO(quiche) create_interface_args["ConfigFile"].writer().append_string |
| 88 | // (file with pkcs config info) |
| 89 | interface_path = |
| 90 | supplicant_process_proxy_->CreateInterface(create_interface_args); |
| 91 | } catch (const DBus::Error e) { // NOLINT |
| 92 | if (!strcmp(e.name(), kSupplicantErrorInterfaceExists)) { |
| 93 | interface_path = |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 94 | supplicant_process_proxy_->GetInterface(link_name_); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 95 | // XXX crash here, if device missing? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 96 | } else { |
| 97 | // XXX |
| 98 | } |
| 99 | } |
| 100 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 101 | supplicant_interface_proxy_.reset( |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 102 | ProxyFactory::factory()->CreateSupplicantInterfaceProxy( |
mukesh agrawal | af57195 | 2011-07-14 14:31:12 -0700 | [diff] [blame] | 103 | this, interface_path, kSupplicantDBusAddr)); |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 104 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 105 | // TODO(quiche) set ApScan=1 and BSSExpireAge=190, like flimflam does? |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 106 | |
| 107 | // clear out any networks that might previously have been configured |
| 108 | // for this interface. |
| 109 | supplicant_interface_proxy_->RemoveAllNetworks(); |
| 110 | |
| 111 | // flush interface's BSS cache, so that we get BSSAdded signals for |
| 112 | // all BSSes (not just new ones since the last scan). |
| 113 | supplicant_interface_proxy_->FlushBSS(0); |
| 114 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 115 | LOG(INFO) << "initiating Scan."; |
mukesh agrawal | c7426a4 | 2011-06-03 13:04:28 -0700 | [diff] [blame] | 116 | std::map<string, DBus::Variant> scan_args; |
| 117 | scan_args["Type"].writer().append_string("active"); |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 118 | // TODO(quiche) indicate scanning in UI |
| 119 | supplicant_interface_proxy_->Scan(scan_args); |
| 120 | scan_pending_ = true; |
| 121 | Device::Start(); |
| 122 | } |
| 123 | |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 124 | void WiFi::Stop() { |
| 125 | LOG(INFO) << __func__; |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 126 | // TODO(quiche): remove interface from supplicant |
| 127 | supplicant_interface_proxy_.reset(); // breaks a reference cycle |
| 128 | supplicant_process_proxy_.reset(); |
| 129 | endpoint_by_bssid_.clear(); |
| 130 | service_by_private_id_.clear(); // breaks reference cycles |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 131 | Device::Stop(); |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 132 | // XXX anything else to do? |
mukesh agrawal | ab87ea4 | 2011-05-18 11:44:49 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 135 | bool WiFi::TechnologyIs(const Device::Technology type) const { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 136 | return type == Device::kWifi; |
| 137 | } |
| 138 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 139 | void WiFi::BSSAdded( |
| 140 | const ::DBus::Path &BSS, |
| 141 | const std::map<string, ::DBus::Variant> &properties) { |
| 142 | // TODO(quiche): write test to verify correct behavior in the case |
| 143 | // where we get multiple BSSAdded events for a single endpoint. |
| 144 | // (old Endpoint's refcount should fall to zero, and old Endpoint |
| 145 | // should be destroyed) |
| 146 | // |
| 147 | // note: we assume that BSSIDs are unique across endpoints. this |
| 148 | // means that if an AP reuses the same BSSID for multiple SSIDs, we |
| 149 | // lose. |
| 150 | WiFiEndpointRefPtr endpoint(new WiFiEndpoint(properties)); |
| 151 | endpoint_by_bssid_[endpoint->bssid_hex()] = endpoint; |
| 152 | } |
| 153 | |
| 154 | void WiFi::ScanDone() { |
| 155 | LOG(INFO) << __func__; |
| 156 | |
| 157 | // defer handling of scan result processing, because that processing |
| 158 | // may require the the registration of new D-Bus objects. and such |
| 159 | // registration can't be done in the context of a D-Bus signal |
| 160 | // handler. |
| 161 | dispatcher_->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 162 | task_factory_.NewRunnableMethod(&WiFi::ScanDoneTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 163 | } |
| 164 | |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 165 | void WiFi::ConnectTo(const WiFiService &service) { |
| 166 | std::map<string, DBus::Variant> add_network_args; |
| 167 | DBus::MessageIter writer; |
| 168 | DBus::Path network_path; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 169 | |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 170 | add_network_args[kSupplicantPropertyNetworkMode].writer(). |
| 171 | append_uint32(service.mode()); |
| 172 | add_network_args[kSupplicantPropertyKeyMode].writer(). |
| 173 | append_string(service.key_management().c_str()); |
| 174 | // TODO(quiche): figure out why we can't use operator<< without the |
| 175 | // temporary variable. |
| 176 | writer = add_network_args[kSupplicantPropertySSID].writer(); |
| 177 | writer << service.ssid(); |
| 178 | // TODO(quiche): set scan_ssid=1, like flimflam does? |
| 179 | |
| 180 | network_path = |
| 181 | supplicant_interface_proxy_->AddNetwork(add_network_args); |
| 182 | supplicant_interface_proxy_->SelectNetwork(network_path); |
| 183 | // XXX add to favorite networks list? |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 184 | } |
| 185 | |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 186 | void WiFi::ScanDoneTask() { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 187 | LOG(INFO) << __func__; |
| 188 | |
| 189 | scan_pending_ = false; |
| 190 | |
| 191 | // TODO(quiche): group endpoints into services, instead of creating |
| 192 | // a service for every endpoint. |
| 193 | for (EndpointMap::iterator i(endpoint_by_bssid_.begin()); |
| 194 | i != endpoint_by_bssid_.end(); ++i) { |
| 195 | const WiFiEndpoint &endpoint(*(i->second)); |
| 196 | string service_id_private; |
| 197 | |
| 198 | service_id_private = |
| 199 | endpoint.ssid_hex() + "_" + endpoint.bssid_hex(); |
| 200 | if (service_by_private_id_.find(service_id_private) == |
| 201 | service_by_private_id_.end()) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 202 | LOG(INFO) << "found new endpoint. " |
| 203 | << "ssid: " << endpoint.ssid_string() << ", " |
| 204 | << "bssid: " << endpoint.bssid_string() << ", " |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 205 | << "signal: " << endpoint.signal_strength(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 206 | |
| 207 | // XXX key mode should reflect endpoint params (not always use |
| 208 | // kSupplicantKeyModeNone) |
mukesh agrawal | 3195024 | 2011-07-14 11:53:38 -0700 | [diff] [blame] | 209 | WiFiServiceRefPtr service( |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 210 | new WiFiService(control_interface_, |
| 211 | dispatcher_, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 212 | manager_, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 213 | this, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 214 | endpoint.ssid(), |
| 215 | endpoint.network_mode(), |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 216 | kSupplicantKeyModeNone)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 217 | services_.push_back(service); |
| 218 | service_by_private_id_[service_id_private] = service; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 219 | |
| 220 | LOG(INFO) << "new service " << service->GetRpcIdentifier(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | // TODO(quiche): register new services with manager |
| 225 | // TODO(quiche): unregister removed services from manager |
| 226 | } |
| 227 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 228 | } // namespace shill |