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