Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 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 "shill/wifi_provider.h" |
| 6 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include <limits> |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <base/bind.h> |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 15 | #include <base/format_macros.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 16 | #include <base/strings/string_number_conversions.h> |
| 17 | #include <base/strings/string_split.h> |
| 18 | #include <base/strings/string_util.h> |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 19 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 20 | #include "shill/error.h" |
| 21 | #include "shill/event_dispatcher.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 22 | #include "shill/ieee80211.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 23 | #include "shill/key_value_store.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 24 | #include "shill/logging.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 25 | #include "shill/manager.h" |
| 26 | #include "shill/metrics.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 27 | #include "shill/profile.h" |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 28 | #include "shill/shill_time.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 29 | #include "shill/store_interface.h" |
| 30 | #include "shill/technology.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 31 | #include "shill/wifi_endpoint.h" |
| 32 | #include "shill/wifi_service.h" |
| 33 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 34 | using base::Bind; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 35 | using base::SplitString; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 36 | using base::StringPrintf; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 37 | using std::set; |
| 38 | using std::string; |
| 39 | using std::vector; |
| 40 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 41 | namespace shill { |
| 42 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 43 | // Note that WiFiProvider generates some manager-level errors, because it |
| 44 | // implements the WiFi portion of the Manager.GetService flimflam API. The |
| 45 | // API is implemented here, rather than in manager, to keep WiFi-specific |
| 46 | // logic in the right place. |
| 47 | const char WiFiProvider::kManagerErrorSSIDRequired[] = "must specify SSID"; |
| 48 | const char WiFiProvider::kManagerErrorSSIDTooLong[] = "SSID is too long"; |
| 49 | const char WiFiProvider::kManagerErrorSSIDTooShort[] = "SSID is too short"; |
| 50 | const char WiFiProvider::kManagerErrorUnsupportedSecurityMode[] = |
| 51 | "security mode is unsupported"; |
| 52 | const char WiFiProvider::kManagerErrorUnsupportedServiceMode[] = |
| 53 | "service mode is unsupported"; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 54 | const char WiFiProvider::kFrequencyDelimiter = ':'; |
| 55 | const char WiFiProvider::kStartWeekHeader[] = "@"; |
| 56 | const time_t WiFiProvider::kIllegalStartWeek = |
| 57 | std::numeric_limits<time_t>::max(); |
Jason Abele | 2866654 | 2013-05-15 11:58:21 -0700 | [diff] [blame] | 58 | const char WiFiProvider::kStorageId[] = "provider_of_wifi"; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 59 | const char WiFiProvider::kStorageFrequencies[] = "Frequencies"; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 60 | const int WiFiProvider::kMaxStorageFrequencies = 20; |
| 61 | const time_t WiFiProvider::kWeeksToKeepFrequencyCounts = 3; |
| 62 | const time_t WiFiProvider::kSecondsPerWeek = 60 * 60 * 24 * 7; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 63 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 64 | WiFiProvider::WiFiProvider(ControlInterface *control_interface, |
| 65 | EventDispatcher *dispatcher, |
| 66 | Metrics *metrics, |
| 67 | Manager *manager) |
| 68 | : control_interface_(control_interface), |
| 69 | dispatcher_(dispatcher), |
| 70 | metrics_(metrics), |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 71 | manager_(manager), |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 72 | running_(false), |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 73 | total_frequency_connections_(-1L), |
Paul Stewart | 9d97b7d | 2014-03-13 21:47:03 -0700 | [diff] [blame] | 74 | time_(Time::GetInstance()), |
| 75 | disable_vht_(false) {} |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 76 | |
| 77 | WiFiProvider::~WiFiProvider() {} |
| 78 | |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 79 | void WiFiProvider::Start() { |
| 80 | running_ = true; |
| 81 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 82 | |
| 83 | void WiFiProvider::Stop() { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 84 | SLOG(WiFi, 2) << __func__; |
| 85 | while (!services_.empty()) { |
| 86 | WiFiServiceRefPtr service = services_.back(); |
| 87 | ForgetService(service); |
| 88 | SLOG(WiFi, 3) << "WiFiProvider deregistering service " |
| 89 | << service->unique_name(); |
| 90 | manager_->DeregisterService(service); |
| 91 | } |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 92 | service_by_endpoint_.clear(); |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 93 | running_ = false; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 96 | void WiFiProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) { |
| 97 | const StoreInterface *storage = profile->GetConstStorage(); |
| 98 | KeyValueStore args; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 99 | args.SetString(kTypeProperty, kTypeWifi); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 100 | bool created_hidden_service = false; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 101 | for (const auto &group : storage->GetGroupsWithProperties(args)) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 102 | string ssid_hex; |
| 103 | vector<uint8_t> ssid_bytes; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 104 | if (!storage->GetString(group, WiFiService::kStorageSSID, &ssid_hex) || |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 105 | !base::HexStringToBytes(ssid_hex, &ssid_bytes)) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 106 | SLOG(WiFi, 2) << "Storage group " << group << " is missing valid \"" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 107 | << WiFiService::kStorageSSID << "\" property"; |
| 108 | continue; |
| 109 | } |
| 110 | string network_mode; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 111 | if (!storage->GetString(group, WiFiService::kStorageMode, &network_mode) || |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 112 | network_mode.empty()) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 113 | SLOG(WiFi, 2) << "Storage group " << group << " is missing \"" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 114 | << WiFiService::kStorageMode << "\" property"; |
| 115 | continue; |
| 116 | } |
| 117 | string security; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 118 | if (!storage->GetString(group, WiFiService::kStorageSecurity, &security) || |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 119 | !WiFiService::IsValidSecurityMethod(security)) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 120 | SLOG(WiFi, 2) << "Storage group " << group << " has missing or invalid \"" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 121 | << WiFiService::kStorageSecurity << "\" property"; |
| 122 | continue; |
| 123 | } |
| 124 | bool is_hidden = false; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 125 | if (!storage->GetBool(group, WiFiService::kStorageHiddenSSID, &is_hidden)) { |
| 126 | SLOG(WiFi, 2) << "Storage group " << group << " is missing \"" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 127 | << WiFiService::kStorageHiddenSSID << "\" property"; |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | if (FindService(ssid_bytes, network_mode, security)) { |
| 132 | // If service already exists, we have nothing to do, since the |
| 133 | // service has already loaded its configuration from storage. |
| 134 | // This is guaranteed to happen in the single case where |
| 135 | // CreateServicesFromProfile() is called on a WiFiProvider from |
| 136 | // Manager::PushProfile(): |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | AddService(ssid_bytes, network_mode, security, is_hidden); |
| 141 | |
| 142 | // By registering the service in AddService, the rest of the configuration |
| 143 | // will be loaded from the profile into the service via ConfigureService(). |
| 144 | |
| 145 | if (is_hidden) { |
| 146 | created_hidden_service = true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // If WiFi is unconnected and we created a hidden service as a result |
| 151 | // of opening the profile, we should initiate a WiFi scan, which will |
| 152 | // allow us to find any hidden services that we may have created. |
| 153 | if (created_hidden_service && |
| 154 | !manager_->IsTechnologyConnected(Technology::kWifi)) { |
| 155 | Error unused_error; |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 156 | manager_->RequestScan(Device::kProgressiveScan, kTypeWifi, &unused_error); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 157 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 160 | ServiceRefPtr WiFiProvider::FindSimilarService( |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 161 | const KeyValueStore &args, Error *error) const { |
| 162 | vector<uint8_t> ssid; |
| 163 | string mode; |
| 164 | string security; |
| 165 | bool hidden_ssid; |
| 166 | |
| 167 | if (!GetServiceParametersFromArgs( |
| 168 | args, &ssid, &mode, &security, &hidden_ssid, error)) { |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | WiFiServiceRefPtr service(FindService(ssid, mode, security)); |
| 173 | if (!service) { |
| 174 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 175 | } |
| 176 | |
| 177 | return service; |
| 178 | } |
| 179 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 180 | ServiceRefPtr WiFiProvider::CreateTemporaryService( |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 181 | const KeyValueStore &args, Error *error) { |
| 182 | vector<uint8_t> ssid; |
| 183 | string mode; |
| 184 | string security; |
| 185 | bool hidden_ssid; |
| 186 | |
| 187 | if (!GetServiceParametersFromArgs( |
| 188 | args, &ssid, &mode, &security, &hidden_ssid, error)) { |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | return new WiFiService(control_interface_, |
| 193 | dispatcher_, |
| 194 | metrics_, |
| 195 | manager_, |
| 196 | this, |
| 197 | ssid, |
| 198 | mode, |
| 199 | security, |
| 200 | hidden_ssid); |
| 201 | } |
| 202 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 203 | ServiceRefPtr WiFiProvider::GetService( |
| 204 | const KeyValueStore &args, Error *error) { |
| 205 | return GetWiFiService(args, error); |
| 206 | } |
| 207 | |
| 208 | WiFiServiceRefPtr WiFiProvider::GetWiFiService( |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 209 | const KeyValueStore &args, Error *error) { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 210 | vector<uint8_t> ssid_bytes; |
| 211 | string mode; |
| 212 | string security_method; |
| 213 | bool hidden_ssid; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 214 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 215 | if (!GetServiceParametersFromArgs( |
| 216 | args, &ssid_bytes, &mode, &security_method, &hidden_ssid, error)) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 220 | WiFiServiceRefPtr service(FindService(ssid_bytes, mode, security_method)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 221 | if (!service) { |
| 222 | service = AddService(ssid_bytes, |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 223 | mode, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 224 | security_method, |
| 225 | hidden_ssid); |
| 226 | } |
| 227 | |
| 228 | return service; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | WiFiServiceRefPtr WiFiProvider::FindServiceForEndpoint( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 232 | const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 233 | EndpointServiceMap::iterator service_it = |
| 234 | service_by_endpoint_.find(endpoint); |
| 235 | if (service_it == service_by_endpoint_.end()) |
| 236 | return NULL; |
| 237 | return service_it->second; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void WiFiProvider::OnEndpointAdded(const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 241 | if (!running_) { |
| 242 | return; |
| 243 | } |
| 244 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 245 | WiFiServiceRefPtr service = FindService(endpoint->ssid(), |
| 246 | endpoint->network_mode(), |
| 247 | endpoint->security_mode()); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 248 | if (!service) { |
| 249 | const bool hidden_ssid = false; |
Paul Stewart | 08a54eb | 2013-03-11 12:07:36 -0700 | [diff] [blame] | 250 | service = AddService( |
| 251 | endpoint->ssid(), |
| 252 | endpoint->network_mode(), |
| 253 | WiFiService::GetSecurityClass(endpoint->security_mode()), |
| 254 | hidden_ssid); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | service->AddEndpoint(endpoint); |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 258 | service_by_endpoint_[endpoint] = service; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 259 | |
| 260 | SLOG(WiFi, 1) << "Assigned endpoint " << endpoint->bssid_string() |
| 261 | << " to service " << service->unique_name() << "."; |
| 262 | |
| 263 | manager_->UpdateService(service); |
| 264 | } |
| 265 | |
| 266 | WiFiServiceRefPtr WiFiProvider::OnEndpointRemoved( |
| 267 | const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 268 | if (!running_) { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 272 | WiFiServiceRefPtr service = FindServiceForEndpoint(endpoint); |
| 273 | |
| 274 | CHECK(service) << "Can't find Service for Endpoint " |
| 275 | << "(with BSSID " << endpoint->bssid_string() << ")."; |
| 276 | SLOG(WiFi, 1) << "Removing endpoint " << endpoint->bssid_string() |
| 277 | << " from Service " << service->unique_name(); |
| 278 | service->RemoveEndpoint(endpoint); |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 279 | service_by_endpoint_.erase(endpoint); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 280 | |
| 281 | if (service->HasEndpoints() || service->IsRemembered()) { |
| 282 | // Keep services around if they are in a profile or have remaining |
| 283 | // endpoints. |
| 284 | manager_->UpdateService(service); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | ForgetService(service); |
| 289 | manager_->DeregisterService(service); |
| 290 | |
| 291 | return service; |
| 292 | } |
| 293 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 294 | void WiFiProvider::OnEndpointUpdated(const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | b3be59c | 2013-08-21 21:03:12 -0700 | [diff] [blame] | 295 | if (!running_) { |
| 296 | return; |
| 297 | } |
| 298 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 299 | WiFiService *service = FindServiceForEndpoint(endpoint); |
| 300 | CHECK(service); |
| 301 | |
| 302 | // If the service still matches the endpoint in its new configuration, |
| 303 | // we need only to update the service. |
| 304 | if (service->ssid() == endpoint->ssid() && |
| 305 | service->mode() == endpoint->network_mode() && |
| 306 | service->IsSecurityMatch(endpoint->security_mode())) { |
| 307 | service->NotifyEndpointUpdated(endpoint); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | // The endpoint no longer matches the associated service. Remove the |
| 312 | // endpoint, so current references to the endpoint are reset, then add |
| 313 | // it again so it can be associated with a new service. |
| 314 | OnEndpointRemoved(endpoint); |
| 315 | OnEndpointAdded(endpoint); |
| 316 | } |
| 317 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 318 | bool WiFiProvider::OnServiceUnloaded(const WiFiServiceRefPtr &service) { |
| 319 | // If the service still has endpoints, it should remain in the service list. |
| 320 | if (service->HasEndpoints()) { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // This is the one place where we forget the service but do not also |
| 325 | // deregister the service with the manager. However, by returning |
| 326 | // true below, the manager will do so itself. |
| 327 | ForgetService(service); |
| 328 | return true; |
| 329 | } |
| 330 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 331 | void WiFiProvider::LoadAndFixupServiceEntries( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 332 | StoreInterface *storage, bool is_default_profile) { |
| 333 | if (WiFiService::FixupServiceEntries(storage)) { |
| 334 | storage->Flush(); |
| 335 | Metrics::ServiceFixupProfileType profile_type = |
| 336 | is_default_profile ? |
| 337 | Metrics::kMetricServiceFixupDefaultProfile : |
| 338 | Metrics::kMetricServiceFixupUserProfile; |
| 339 | metrics_->SendEnumToUMA( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 340 | metrics_->GetFullMetricName(Metrics::kMetricServiceFixupEntriesSuffix, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 341 | Technology::kWifi), |
| 342 | profile_type, |
| 343 | Metrics::kMetricServiceFixupMax); |
| 344 | } |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 345 | // TODO(wdg): Determine how this should be structured for, currently |
| 346 | // non-existant, autotests. |kStorageFrequencies| should only exist in the |
| 347 | // default profile except for autotests where a test_profile is pushed. This |
| 348 | // may need to be modified for that case. |
| 349 | if (is_default_profile) { |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 350 | COMPILE_ASSERT(kMaxStorageFrequencies > kWeeksToKeepFrequencyCounts, |
| 351 | persistently_storing_more_frequencies_than_we_can_hold); |
| 352 | total_frequency_connections_ = 0L; |
| 353 | connect_count_by_frequency_.clear(); |
| 354 | time_t this_week = time_->GetSecondsSinceEpoch() / kSecondsPerWeek; |
| 355 | for (int freq = 0; freq < kMaxStorageFrequencies; ++freq) { |
| 356 | ConnectFrequencyMap connect_count_by_frequency; |
| 357 | string freq_string = StringPrintf("%s%d", kStorageFrequencies, freq); |
| 358 | vector<string> frequencies; |
| 359 | if (!storage->GetStringList(kStorageId, freq_string, &frequencies)) { |
| 360 | SLOG(WiFi, 7) << "Frequency list " << freq_string << " not found"; |
| 361 | break; |
| 362 | } |
| 363 | time_t start_week = StringListToFrequencyMap(frequencies, |
| 364 | &connect_count_by_frequency); |
| 365 | if (start_week == kIllegalStartWeek) { |
| 366 | continue; // |StringListToFrequencyMap| will have output an error msg. |
| 367 | } |
| 368 | |
| 369 | if (start_week > this_week) { |
| 370 | LOG(WARNING) << "Discarding frequency count info from the future"; |
| 371 | continue; |
| 372 | } |
| 373 | connect_count_by_frequency_dated_[start_week] = |
| 374 | connect_count_by_frequency; |
| 375 | |
| 376 | for (const auto &freq_count : |
| 377 | connect_count_by_frequency_dated_[start_week]) { |
| 378 | connect_count_by_frequency_[freq_count.first] += freq_count.second; |
| 379 | total_frequency_connections_ += freq_count.second; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 382 | SLOG(WiFi, 7) << __func__ << " - total count=" |
| 383 | << total_frequency_connections_; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | bool WiFiProvider::Save(StoreInterface *storage) const { |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 388 | int freq = 0; |
| 389 | // Iterating backwards since I want to make sure that I get the newest data. |
| 390 | ConnectFrequencyMapDated::const_reverse_iterator freq_count; |
| 391 | for (freq_count = connect_count_by_frequency_dated_.crbegin(); |
| 392 | freq_count != connect_count_by_frequency_dated_.crend(); |
| 393 | ++freq_count) { |
| 394 | vector<string> frequencies; |
| 395 | FrequencyMapToStringList(freq_count->first, freq_count->second, |
| 396 | &frequencies); |
| 397 | string freq_string = StringPrintf("%s%d", kStorageFrequencies, freq); |
| 398 | storage->SetStringList(kStorageId, freq_string, frequencies); |
| 399 | if (++freq >= kMaxStorageFrequencies) { |
| 400 | LOG(WARNING) << "Internal frequency count list has more entries than the " |
| 401 | << "string list we had allocated for it."; |
| 402 | break; |
| 403 | } |
| 404 | } |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 405 | return true; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | WiFiServiceRefPtr WiFiProvider::AddService(const vector<uint8_t> &ssid, |
| 409 | const string &mode, |
| 410 | const string &security, |
| 411 | bool is_hidden) { |
| 412 | WiFiServiceRefPtr service = new WiFiService(control_interface_, |
| 413 | dispatcher_, |
| 414 | metrics_, |
| 415 | manager_, |
| 416 | this, |
| 417 | ssid, |
| 418 | mode, |
| 419 | security, |
| 420 | is_hidden); |
| 421 | |
| 422 | services_.push_back(service); |
| 423 | manager_->RegisterService(service); |
| 424 | return service; |
| 425 | } |
| 426 | |
| 427 | WiFiServiceRefPtr WiFiProvider::FindService(const vector<uint8_t> &ssid, |
| 428 | const string &mode, |
| 429 | const string &security) const { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 430 | for (const auto &service : services_) { |
| 431 | if (service->ssid() == ssid && service->mode() == mode && |
| 432 | service->IsSecurityMatch(security)) { |
| 433 | return service; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
| 438 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 439 | ByteArrays WiFiProvider::GetHiddenSSIDList() { |
| 440 | // Create a unique set of hidden SSIDs. |
| 441 | set<ByteArray> hidden_ssids_set; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 442 | for (const auto &service : services_) { |
| 443 | if (service->hidden_ssid() && service->IsRemembered()) { |
| 444 | hidden_ssids_set.insert(service->ssid()); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | SLOG(WiFi, 2) << "Found " << hidden_ssids_set.size() << " hidden services"; |
| 448 | return ByteArrays(hidden_ssids_set.begin(), hidden_ssids_set.end()); |
| 449 | } |
| 450 | |
| 451 | void WiFiProvider::ForgetService(const WiFiServiceRefPtr &service) { |
| 452 | vector<WiFiServiceRefPtr>::iterator it; |
| 453 | it = std::find(services_.begin(), services_.end(), service); |
| 454 | if (it == services_.end()) { |
| 455 | return; |
| 456 | } |
| 457 | (*it)->ResetWiFi(); |
| 458 | services_.erase(it); |
| 459 | } |
| 460 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 461 | // static |
| 462 | bool WiFiProvider::GetServiceParametersFromArgs(const KeyValueStore &args, |
| 463 | vector<uint8_t> *ssid_bytes, |
| 464 | string *mode, |
| 465 | string *security_method, |
| 466 | bool *hidden_ssid, |
| 467 | Error *error) { |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 468 | CHECK_EQ(args.LookupString(kTypeProperty, ""), kTypeWifi); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 469 | |
| 470 | string mode_test = |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 471 | args.LookupString(kModeProperty, kModeManaged); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 472 | if (!WiFiService::IsValidMode(mode_test)) { |
| 473 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 474 | kManagerErrorUnsupportedServiceMode); |
| 475 | return false; |
| 476 | } |
| 477 | |
Paul Stewart | 4539d26 | 2013-10-10 12:56:31 -0700 | [diff] [blame] | 478 | vector<uint8_t> ssid; |
| 479 | if (args.ContainsString(kWifiHexSsid)) { |
| 480 | string ssid_hex_string = args.GetString(kWifiHexSsid); |
| 481 | if (!base::HexStringToBytes(ssid_hex_string, &ssid)) { |
| 482 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 483 | "Hex SSID parameter is not valid"); |
| 484 | return false; |
| 485 | } |
| 486 | } else if (args.ContainsString(kSSIDProperty)) { |
| 487 | string ssid_string = args.GetString(kSSIDProperty); |
| 488 | ssid = vector<uint8_t>(ssid_string.begin(), ssid_string.end()); |
| 489 | } else { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 490 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 491 | kManagerErrorSSIDRequired); |
| 492 | return false; |
| 493 | } |
| 494 | |
Paul Stewart | 4539d26 | 2013-10-10 12:56:31 -0700 | [diff] [blame] | 495 | if (ssid.size() < 1) { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 496 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 497 | kManagerErrorSSIDTooShort); |
| 498 | return false; |
| 499 | } |
| 500 | |
Paul Stewart | 4539d26 | 2013-10-10 12:56:31 -0700 | [diff] [blame] | 501 | if (ssid.size() > IEEE_80211::kMaxSSIDLen) { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 502 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 503 | kManagerErrorSSIDTooLong); |
| 504 | return false; |
| 505 | } |
| 506 | |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 507 | string security_method_test = args.LookupString(kSecurityProperty, |
| 508 | kSecurityNone); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 509 | |
| 510 | if (!WiFiService::IsValidSecurityMethod(security_method_test)) { |
| 511 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 512 | kManagerErrorUnsupportedSecurityMode); |
| 513 | return false; |
| 514 | } |
| 515 | |
Paul Stewart | 4539d26 | 2013-10-10 12:56:31 -0700 | [diff] [blame] | 516 | *ssid_bytes = ssid; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 517 | *mode = mode_test; |
| 518 | *security_method = security_method_test; |
| 519 | |
| 520 | // If the caller hasn't specified otherwise, we assume it is a hidden service. |
Ben Chan | f024ef4 | 2013-09-20 14:21:38 -0700 | [diff] [blame] | 521 | *hidden_ssid = args.LookupBool(kWifiHiddenSsid, true); |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 526 | // static |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 527 | time_t WiFiProvider::StringListToFrequencyMap(const vector<string> &strings, |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 528 | ConnectFrequencyMap *numbers) { |
| 529 | if (!numbers) { |
| 530 | LOG(ERROR) << "Null |numbers| parameter"; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 531 | return kIllegalStartWeek; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 534 | // Extract the start week from the first string. |
| 535 | vector<string>::const_iterator strings_it = strings.begin(); |
| 536 | if (strings_it == strings.end()) { |
| 537 | SLOG(WiFi, 7) << "Empty |strings|."; |
| 538 | return kIllegalStartWeek; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 539 | } |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 540 | time_t start_week = GetStringListStartWeek(*strings_it); |
| 541 | if (start_week == kIllegalStartWeek) { |
| 542 | return kIllegalStartWeek; |
| 543 | } |
| 544 | |
| 545 | // Extract the frequency:count values from the remaining strings. |
| 546 | for (++strings_it; strings_it != strings.end(); ++strings_it) { |
| 547 | ParseStringListFreqCount(*strings_it, numbers); |
| 548 | } |
| 549 | return start_week; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // static |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 553 | time_t WiFiProvider::GetStringListStartWeek(const string &week_string) { |
| 554 | if (!StartsWithASCII(week_string, kStartWeekHeader, false)) { |
| 555 | LOG(ERROR) << "Found no leading '" << kStartWeekHeader << "' in '" |
| 556 | << week_string << "'"; |
| 557 | return kIllegalStartWeek; |
| 558 | } |
| 559 | return atoll(week_string.c_str() + 1); |
| 560 | } |
| 561 | |
| 562 | // static |
| 563 | void WiFiProvider::ParseStringListFreqCount(const string &freq_count_string, |
| 564 | ConnectFrequencyMap *numbers) { |
| 565 | vector<string> freq_count; |
| 566 | SplitString(freq_count_string, kFrequencyDelimiter, &freq_count); |
| 567 | if (freq_count.size() != 2) { |
| 568 | LOG(WARNING) << "Found " << freq_count.size() - 1 << " '" |
| 569 | << kFrequencyDelimiter << "' in '" << freq_count_string |
| 570 | << "'. Expected 1."; |
| 571 | return; |
| 572 | } |
| 573 | uint16 freq = atoi(freq_count[0].c_str()); |
| 574 | uint64 connections = atoll(freq_count[1].c_str()); |
| 575 | (*numbers)[freq] = connections; |
| 576 | } |
| 577 | |
| 578 | // static |
| 579 | void WiFiProvider::FrequencyMapToStringList(time_t start_week, |
| 580 | const ConnectFrequencyMap &numbers, |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 581 | vector<string> *strings) { |
| 582 | if (!strings) { |
| 583 | LOG(ERROR) << "Null |strings| parameter"; |
| 584 | return; |
| 585 | } |
| 586 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 587 | strings->push_back(StringPrintf("%s%" PRIu64, kStartWeekHeader, |
| 588 | static_cast<uint64_t>(start_week))); |
| 589 | |
| 590 | for (const auto &freq_conn : numbers) { |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 591 | // Use base::Int64ToString() instead of using something like "%llu" |
| 592 | // (not correct for native 64 bit architectures) or PRId64 (does not |
| 593 | // work correctly using cros_workon_make due to include intricacies). |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 594 | strings->push_back(StringPrintf("%u%c%s", |
| 595 | freq_conn.first, kFrequencyDelimiter, |
| 596 | base::Int64ToString(freq_conn.second).c_str())); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | void WiFiProvider::IncrementConnectCount(uint16 frequency_mhz) { |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 601 | CHECK(total_frequency_connections_ < std::numeric_limits<int64_t>::max()); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 602 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 603 | ++connect_count_by_frequency_[frequency_mhz]; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 604 | ++total_frequency_connections_; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 605 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 606 | time_t this_week = time_->GetSecondsSinceEpoch() / kSecondsPerWeek; |
| 607 | ++connect_count_by_frequency_dated_[this_week][frequency_mhz]; |
| 608 | |
| 609 | ConnectFrequencyMapDated::iterator oldest = |
| 610 | connect_count_by_frequency_dated_.begin(); |
| 611 | time_t oldest_legal_week = this_week - kWeeksToKeepFrequencyCounts; |
| 612 | while (oldest->first < oldest_legal_week) { |
Wade Guthrie | 086eb1e | 2013-05-31 17:31:13 -0700 | [diff] [blame] | 613 | SLOG(WiFi, 6) << "Discarding frequency count info that's " |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 614 | << this_week - oldest->first << " weeks old"; |
| 615 | for (const auto &freq_count : oldest->second) { |
| 616 | connect_count_by_frequency_[freq_count.first] -= freq_count.second; |
| 617 | if (connect_count_by_frequency_[freq_count.first] <= 0) { |
| 618 | connect_count_by_frequency_.erase(freq_count.first); |
| 619 | } |
| 620 | total_frequency_connections_ -= freq_count.second; |
| 621 | } |
| 622 | connect_count_by_frequency_dated_.erase(oldest); |
| 623 | oldest = connect_count_by_frequency_dated_.begin(); |
| 624 | } |
| 625 | |
| 626 | manager_->UpdateWiFiProvider(); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 627 | metrics_->SendToUMA( |
| 628 | Metrics::kMetricFrequenciesConnectedEver, |
| 629 | connect_count_by_frequency_.size(), |
| 630 | Metrics::kMetricFrequenciesConnectedMin, |
| 631 | Metrics::kMetricFrequenciesConnectedMax, |
| 632 | Metrics::kMetricFrequenciesConnectedNumBuckets); |
| 633 | } |
| 634 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 635 | WiFiProvider::FrequencyCountList WiFiProvider::GetScanFrequencies() const { |
| 636 | FrequencyCountList freq_connects_list; |
| 637 | for (const auto freq_count : connect_count_by_frequency_) { |
| 638 | freq_connects_list.push_back(FrequencyCount(freq_count.first, |
| 639 | freq_count.second)); |
| 640 | } |
| 641 | return freq_connects_list; |
| 642 | } |
| 643 | |
Peter Qiu | 574996a | 2014-04-04 10:55:47 -0700 | [diff] [blame] | 644 | void WiFiProvider::ReportAutoConnectableServices() { |
| 645 | const char *reason = NULL; |
| 646 | int num_services = 0; |
| 647 | |
| 648 | // Determine the number of services available for auto-connect. |
| 649 | for (const auto &service : services_) { |
| 650 | // Service is available for auto connect if it is configured for auto |
| 651 | // connect, and is auto-connectable. |
| 652 | if (service->auto_connect() && service->IsAutoConnectable(&reason)) { |
| 653 | num_services++; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // Only report stats when there are wifi services available. |
| 658 | if (num_services) { |
| 659 | metrics_->NotifyWifiAutoConnectableServices(num_services); |
| 660 | } |
| 661 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 662 | } // namespace shill |