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> |
| 15 | #include <base/string_number_conversions.h> |
| 16 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 17 | #include "shill/error.h" |
| 18 | #include "shill/event_dispatcher.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 19 | #include "shill/ieee80211.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 20 | #include "shill/key_value_store.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 21 | #include "shill/logging.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 22 | #include "shill/manager.h" |
| 23 | #include "shill/metrics.h" |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 24 | #include "shill/profile.h" |
| 25 | #include "shill/store_interface.h" |
| 26 | #include "shill/technology.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 27 | #include "shill/wifi_endpoint.h" |
| 28 | #include "shill/wifi_service.h" |
| 29 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 30 | using base::Bind; |
| 31 | using std::set; |
| 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 35 | namespace shill { |
| 36 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 37 | // Note that WiFiProvider generates some manager-level errors, because it |
| 38 | // implements the WiFi portion of the Manager.GetService flimflam API. The |
| 39 | // API is implemented here, rather than in manager, to keep WiFi-specific |
| 40 | // logic in the right place. |
| 41 | const char WiFiProvider::kManagerErrorSSIDRequired[] = "must specify SSID"; |
| 42 | const char WiFiProvider::kManagerErrorSSIDTooLong[] = "SSID is too long"; |
| 43 | const char WiFiProvider::kManagerErrorSSIDTooShort[] = "SSID is too short"; |
| 44 | const char WiFiProvider::kManagerErrorUnsupportedSecurityMode[] = |
| 45 | "security mode is unsupported"; |
| 46 | const char WiFiProvider::kManagerErrorUnsupportedServiceMode[] = |
| 47 | "service mode is unsupported"; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 48 | const char WiFiProvider::kFrequencyDelimiter[] = ":"; |
| 49 | const char WiFiProvider::kStorageId[] = "wifi_provider"; |
| 50 | const char WiFiProvider::kStorageFrequencies[] = "Frequencies"; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 51 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 52 | WiFiProvider::WiFiProvider(ControlInterface *control_interface, |
| 53 | EventDispatcher *dispatcher, |
| 54 | Metrics *metrics, |
| 55 | Manager *manager) |
| 56 | : control_interface_(control_interface), |
| 57 | dispatcher_(dispatcher), |
| 58 | metrics_(metrics), |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 59 | manager_(manager), |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 60 | running_(false), |
| 61 | total_frequency_connections_(-1L) {} |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 62 | |
| 63 | WiFiProvider::~WiFiProvider() {} |
| 64 | |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 65 | void WiFiProvider::Start() { |
| 66 | running_ = true; |
| 67 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 68 | |
| 69 | void WiFiProvider::Stop() { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 70 | SLOG(WiFi, 2) << __func__; |
| 71 | while (!services_.empty()) { |
| 72 | WiFiServiceRefPtr service = services_.back(); |
| 73 | ForgetService(service); |
| 74 | SLOG(WiFi, 3) << "WiFiProvider deregistering service " |
| 75 | << service->unique_name(); |
| 76 | manager_->DeregisterService(service); |
| 77 | } |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 78 | service_by_endpoint_.clear(); |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 79 | running_ = false; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 82 | void WiFiProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) { |
| 83 | const StoreInterface *storage = profile->GetConstStorage(); |
| 84 | KeyValueStore args; |
| 85 | args.SetString(flimflam::kTypeProperty, flimflam::kTypeWifi); |
| 86 | set<string> groups = storage->GetGroupsWithProperties(args); |
| 87 | bool created_hidden_service = false; |
| 88 | for (set<string>::const_iterator it = groups.begin(); it != groups.end(); |
| 89 | ++it) { |
| 90 | string ssid_hex; |
| 91 | vector<uint8_t> ssid_bytes; |
| 92 | if (!storage->GetString(*it, WiFiService::kStorageSSID, &ssid_hex) || |
| 93 | !base::HexStringToBytes(ssid_hex, &ssid_bytes)) { |
| 94 | SLOG(WiFi, 2) << "Storage group " << *it << " is missing valid \"" |
| 95 | << WiFiService::kStorageSSID << "\" property"; |
| 96 | continue; |
| 97 | } |
| 98 | string network_mode; |
| 99 | if (!storage->GetString(*it, WiFiService::kStorageMode, &network_mode) || |
| 100 | network_mode.empty()) { |
| 101 | SLOG(WiFi, 2) << "Storage group " << *it << " is missing \"" |
| 102 | << WiFiService::kStorageMode << "\" property"; |
| 103 | continue; |
| 104 | } |
| 105 | string security; |
| 106 | if (!storage->GetString(*it, WiFiService::kStorageSecurity, &security) || |
| 107 | !WiFiService::IsValidSecurityMethod(security)) { |
| 108 | SLOG(WiFi, 2) << "Storage group " << *it << " has missing or invalid \"" |
| 109 | << WiFiService::kStorageSecurity << "\" property"; |
| 110 | continue; |
| 111 | } |
| 112 | bool is_hidden = false; |
| 113 | if (!storage->GetBool(*it, WiFiService::kStorageHiddenSSID, &is_hidden)) { |
| 114 | SLOG(WiFi, 2) << "Storage group " << *it << " is missing \"" |
| 115 | << WiFiService::kStorageHiddenSSID << "\" property"; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | if (FindService(ssid_bytes, network_mode, security)) { |
| 120 | // If service already exists, we have nothing to do, since the |
| 121 | // service has already loaded its configuration from storage. |
| 122 | // This is guaranteed to happen in the single case where |
| 123 | // CreateServicesFromProfile() is called on a WiFiProvider from |
| 124 | // Manager::PushProfile(): |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | AddService(ssid_bytes, network_mode, security, is_hidden); |
| 129 | |
| 130 | // By registering the service in AddService, the rest of the configuration |
| 131 | // will be loaded from the profile into the service via ConfigureService(). |
| 132 | |
| 133 | if (is_hidden) { |
| 134 | created_hidden_service = true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // If WiFi is unconnected and we created a hidden service as a result |
| 139 | // of opening the profile, we should initiate a WiFi scan, which will |
| 140 | // allow us to find any hidden services that we may have created. |
| 141 | if (created_hidden_service && |
| 142 | !manager_->IsTechnologyConnected(Technology::kWifi)) { |
| 143 | Error unused_error; |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 144 | manager_->RequestScan(Device::kProgressiveScan, flimflam::kTypeWifi, |
| 145 | &unused_error); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 146 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 149 | WiFiServiceRefPtr WiFiProvider::FindSimilarService( |
| 150 | const KeyValueStore &args, Error *error) const { |
| 151 | vector<uint8_t> ssid; |
| 152 | string mode; |
| 153 | string security; |
| 154 | bool hidden_ssid; |
| 155 | |
| 156 | if (!GetServiceParametersFromArgs( |
| 157 | args, &ssid, &mode, &security, &hidden_ssid, error)) { |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | WiFiServiceRefPtr service(FindService(ssid, mode, security)); |
| 162 | if (!service) { |
| 163 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 164 | } |
| 165 | |
| 166 | return service; |
| 167 | } |
| 168 | |
| 169 | WiFiServiceRefPtr WiFiProvider::CreateTemporaryService( |
| 170 | const KeyValueStore &args, Error *error) { |
| 171 | vector<uint8_t> ssid; |
| 172 | string mode; |
| 173 | string security; |
| 174 | bool hidden_ssid; |
| 175 | |
| 176 | if (!GetServiceParametersFromArgs( |
| 177 | args, &ssid, &mode, &security, &hidden_ssid, error)) { |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | return new WiFiService(control_interface_, |
| 182 | dispatcher_, |
| 183 | metrics_, |
| 184 | manager_, |
| 185 | this, |
| 186 | ssid, |
| 187 | mode, |
| 188 | security, |
| 189 | hidden_ssid); |
| 190 | } |
| 191 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 192 | WiFiServiceRefPtr WiFiProvider::GetService( |
| 193 | const KeyValueStore &args, Error *error) { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 194 | vector<uint8_t> ssid_bytes; |
| 195 | string mode; |
| 196 | string security_method; |
| 197 | bool hidden_ssid; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 198 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 199 | if (!GetServiceParametersFromArgs( |
| 200 | args, &ssid_bytes, &mode, &security_method, &hidden_ssid, error)) { |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 201 | return NULL; |
| 202 | } |
| 203 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 204 | WiFiServiceRefPtr service(FindService(ssid_bytes, mode, security_method)); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 205 | if (!service) { |
| 206 | service = AddService(ssid_bytes, |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 207 | mode, |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 208 | security_method, |
| 209 | hidden_ssid); |
| 210 | } |
| 211 | |
| 212 | return service; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | WiFiServiceRefPtr WiFiProvider::FindServiceForEndpoint( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 216 | const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 217 | EndpointServiceMap::iterator service_it = |
| 218 | service_by_endpoint_.find(endpoint); |
| 219 | if (service_it == service_by_endpoint_.end()) |
| 220 | return NULL; |
| 221 | return service_it->second; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void WiFiProvider::OnEndpointAdded(const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 225 | if (!running_) { |
| 226 | return; |
| 227 | } |
| 228 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 229 | WiFiServiceRefPtr service = FindService(endpoint->ssid(), |
| 230 | endpoint->network_mode(), |
| 231 | endpoint->security_mode()); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 232 | if (!service) { |
| 233 | const bool hidden_ssid = false; |
Paul Stewart | 08a54eb | 2013-03-11 12:07:36 -0700 | [diff] [blame] | 234 | service = AddService( |
| 235 | endpoint->ssid(), |
| 236 | endpoint->network_mode(), |
| 237 | WiFiService::GetSecurityClass(endpoint->security_mode()), |
| 238 | hidden_ssid); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | service->AddEndpoint(endpoint); |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 242 | service_by_endpoint_[endpoint] = service; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 243 | |
| 244 | SLOG(WiFi, 1) << "Assigned endpoint " << endpoint->bssid_string() |
| 245 | << " to service " << service->unique_name() << "."; |
| 246 | |
| 247 | manager_->UpdateService(service); |
| 248 | } |
| 249 | |
| 250 | WiFiServiceRefPtr WiFiProvider::OnEndpointRemoved( |
| 251 | const WiFiEndpointConstRefPtr &endpoint) { |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 252 | if (!running_) { |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 256 | WiFiServiceRefPtr service = FindServiceForEndpoint(endpoint); |
| 257 | |
| 258 | CHECK(service) << "Can't find Service for Endpoint " |
| 259 | << "(with BSSID " << endpoint->bssid_string() << ")."; |
| 260 | SLOG(WiFi, 1) << "Removing endpoint " << endpoint->bssid_string() |
| 261 | << " from Service " << service->unique_name(); |
| 262 | service->RemoveEndpoint(endpoint); |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 263 | service_by_endpoint_.erase(endpoint); |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 264 | |
| 265 | if (service->HasEndpoints() || service->IsRemembered()) { |
| 266 | // Keep services around if they are in a profile or have remaining |
| 267 | // endpoints. |
| 268 | manager_->UpdateService(service); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | ForgetService(service); |
| 273 | manager_->DeregisterService(service); |
| 274 | |
| 275 | return service; |
| 276 | } |
| 277 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 278 | void WiFiProvider::OnEndpointUpdated(const WiFiEndpointConstRefPtr &endpoint) { |
| 279 | WiFiService *service = FindServiceForEndpoint(endpoint); |
| 280 | CHECK(service); |
| 281 | |
| 282 | // If the service still matches the endpoint in its new configuration, |
| 283 | // we need only to update the service. |
| 284 | if (service->ssid() == endpoint->ssid() && |
| 285 | service->mode() == endpoint->network_mode() && |
| 286 | service->IsSecurityMatch(endpoint->security_mode())) { |
| 287 | service->NotifyEndpointUpdated(endpoint); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | // The endpoint no longer matches the associated service. Remove the |
| 292 | // endpoint, so current references to the endpoint are reset, then add |
| 293 | // it again so it can be associated with a new service. |
| 294 | OnEndpointRemoved(endpoint); |
| 295 | OnEndpointAdded(endpoint); |
| 296 | } |
| 297 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 298 | bool WiFiProvider::OnServiceUnloaded(const WiFiServiceRefPtr &service) { |
| 299 | // If the service still has endpoints, it should remain in the service list. |
| 300 | if (service->HasEndpoints()) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | // This is the one place where we forget the service but do not also |
| 305 | // deregister the service with the manager. However, by returning |
| 306 | // true below, the manager will do so itself. |
| 307 | ForgetService(service); |
| 308 | return true; |
| 309 | } |
| 310 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 311 | void WiFiProvider::LoadAndFixupServiceEntries( |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 312 | StoreInterface *storage, bool is_default_profile) { |
| 313 | if (WiFiService::FixupServiceEntries(storage)) { |
| 314 | storage->Flush(); |
| 315 | Metrics::ServiceFixupProfileType profile_type = |
| 316 | is_default_profile ? |
| 317 | Metrics::kMetricServiceFixupDefaultProfile : |
| 318 | Metrics::kMetricServiceFixupUserProfile; |
| 319 | metrics_->SendEnumToUMA( |
| 320 | metrics_->GetFullMetricName(Metrics::kMetricServiceFixupEntries, |
| 321 | Technology::kWifi), |
| 322 | profile_type, |
| 323 | Metrics::kMetricServiceFixupMax); |
| 324 | } |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 325 | // TODO(wdg): Determine how this should be structured for, currently |
| 326 | // non-existant, autotests. |kStorageFrequencies| should only exist in the |
| 327 | // default profile except for autotests where a test_profile is pushed. This |
| 328 | // may need to be modified for that case. |
| 329 | if (is_default_profile) { |
| 330 | vector<string> frequencies; |
| 331 | if (storage->GetStringList(kStorageId, |
| 332 | kStorageFrequencies, |
| 333 | &frequencies)) { |
| 334 | StringListToFrequencyMap(frequencies, &connect_count_by_frequency_); |
| 335 | total_frequency_connections_ = 0L; |
| 336 | WiFiProvider::ConnectFrequencyMap::const_iterator i; |
| 337 | for (i = connect_count_by_frequency_.begin(); |
| 338 | i != connect_count_by_frequency_.end(); |
| 339 | ++i) { |
| 340 | total_frequency_connections_ += i->second; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | bool WiFiProvider::Save(StoreInterface *storage) const { |
| 347 | vector<string> frequencies; |
| 348 | FrequencyMapToStringList(connect_count_by_frequency_, &frequencies); |
| 349 | storage->SetStringList(kStorageId, kStorageFrequencies, frequencies); |
| 350 | return true; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | WiFiServiceRefPtr WiFiProvider::AddService(const vector<uint8_t> &ssid, |
| 354 | const string &mode, |
| 355 | const string &security, |
| 356 | bool is_hidden) { |
| 357 | WiFiServiceRefPtr service = new WiFiService(control_interface_, |
| 358 | dispatcher_, |
| 359 | metrics_, |
| 360 | manager_, |
| 361 | this, |
| 362 | ssid, |
| 363 | mode, |
| 364 | security, |
| 365 | is_hidden); |
| 366 | |
| 367 | services_.push_back(service); |
| 368 | manager_->RegisterService(service); |
| 369 | return service; |
| 370 | } |
| 371 | |
| 372 | WiFiServiceRefPtr WiFiProvider::FindService(const vector<uint8_t> &ssid, |
| 373 | const string &mode, |
| 374 | const string &security) const { |
| 375 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 376 | it != services_.end(); |
| 377 | ++it) { |
| 378 | if ((*it)->ssid() == ssid && (*it)->mode() == mode && |
| 379 | (*it)->IsSecurityMatch(security)) { |
| 380 | return *it; |
| 381 | } |
| 382 | } |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
| 385 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 386 | ByteArrays WiFiProvider::GetHiddenSSIDList() { |
| 387 | // Create a unique set of hidden SSIDs. |
| 388 | set<ByteArray> hidden_ssids_set; |
| 389 | for (vector<WiFiServiceRefPtr>::const_iterator it = services_.begin(); |
| 390 | it != services_.end(); |
| 391 | ++it) { |
| 392 | if ((*it)->hidden_ssid() && (*it)->IsRemembered()) { |
| 393 | hidden_ssids_set.insert((*it)->ssid()); |
| 394 | } |
| 395 | } |
| 396 | SLOG(WiFi, 2) << "Found " << hidden_ssids_set.size() << " hidden services"; |
| 397 | return ByteArrays(hidden_ssids_set.begin(), hidden_ssids_set.end()); |
| 398 | } |
| 399 | |
| 400 | void WiFiProvider::ForgetService(const WiFiServiceRefPtr &service) { |
| 401 | vector<WiFiServiceRefPtr>::iterator it; |
| 402 | it = std::find(services_.begin(), services_.end(), service); |
| 403 | if (it == services_.end()) { |
| 404 | return; |
| 405 | } |
| 406 | (*it)->ResetWiFi(); |
| 407 | services_.erase(it); |
| 408 | } |
| 409 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 410 | // static |
| 411 | bool WiFiProvider::GetServiceParametersFromArgs(const KeyValueStore &args, |
| 412 | vector<uint8_t> *ssid_bytes, |
| 413 | string *mode, |
| 414 | string *security_method, |
| 415 | bool *hidden_ssid, |
| 416 | Error *error) { |
| 417 | CHECK_EQ(args.LookupString(flimflam::kTypeProperty, ""), flimflam::kTypeWifi); |
| 418 | |
| 419 | string mode_test = |
| 420 | args.LookupString(flimflam::kModeProperty, flimflam::kModeManaged); |
| 421 | if (!WiFiService::IsValidMode(mode_test)) { |
| 422 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 423 | kManagerErrorUnsupportedServiceMode); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | if (!args.ContainsString(flimflam::kSSIDProperty)) { |
| 428 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 429 | kManagerErrorSSIDRequired); |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | string ssid = args.GetString(flimflam::kSSIDProperty); |
| 434 | |
| 435 | if (ssid.length() < 1) { |
| 436 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 437 | kManagerErrorSSIDTooShort); |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (ssid.length() > IEEE_80211::kMaxSSIDLen) { |
| 442 | Error::PopulateAndLog(error, Error::kInvalidNetworkName, |
| 443 | kManagerErrorSSIDTooLong); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | string security_method_test = args.LookupString(flimflam::kSecurityProperty, |
| 448 | flimflam::kSecurityNone); |
| 449 | |
| 450 | if (!WiFiService::IsValidSecurityMethod(security_method_test)) { |
| 451 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 452 | kManagerErrorUnsupportedSecurityMode); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | *ssid_bytes = vector<uint8_t>(ssid.begin(), ssid.end()); |
| 457 | *mode = mode_test; |
| 458 | *security_method = security_method_test; |
| 459 | |
| 460 | // If the caller hasn't specified otherwise, we assume it is a hidden service. |
| 461 | *hidden_ssid = args.LookupBool(flimflam::kWifiHiddenSsid, true); |
| 462 | |
| 463 | return true; |
| 464 | } |
| 465 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 466 | // static |
| 467 | void WiFiProvider::StringListToFrequencyMap(const vector<string> &strings, |
| 468 | ConnectFrequencyMap *numbers) { |
| 469 | if (!numbers) { |
| 470 | LOG(ERROR) << "Null |numbers| parameter"; |
| 471 | return; |
| 472 | } |
| 473 | |
| 474 | vector<string>::const_iterator i; |
| 475 | for (i = strings.begin(); i != strings.end(); ++i) { |
| 476 | size_t delimiter = i->find(kFrequencyDelimiter); |
| 477 | if (delimiter == i->npos) { |
| 478 | LOG(WARNING) << "Found no '" << kFrequencyDelimiter << "' in '" |
| 479 | << *i << "'"; |
| 480 | continue; |
| 481 | } |
| 482 | uint16 freq = atoi(i->c_str()); |
| 483 | uint64 connections = atoll(i->c_str() + delimiter + 1); |
| 484 | (*numbers)[freq] = connections; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // static |
| 489 | void WiFiProvider::FrequencyMapToStringList(const ConnectFrequencyMap &numbers, |
| 490 | vector<string> *strings) { |
| 491 | if (!strings) { |
| 492 | LOG(ERROR) << "Null |strings| parameter"; |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | ConnectFrequencyMap::const_iterator i; |
| 497 | for (i = numbers.begin(); i != numbers.end(); ++i) { |
| 498 | // Use base::Int64ToString() instead of using something like "%llu" |
| 499 | // (not correct for native 64 bit architectures) or PRId64 (does not |
| 500 | // work correctly using cros_workon_make due to include intricacies). |
| 501 | string result = StringPrintf("%u%s%s", i->first, kFrequencyDelimiter, |
| 502 | base::Int64ToString(i->second).c_str()); |
| 503 | strings->push_back(result); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void WiFiProvider::IncrementConnectCount(uint16 frequency_mhz) { |
| 508 | // Freeze the accumulation of frequency counts when the total maxes-out. |
| 509 | // This ensures that no count wraps and that the relative values are |
| 510 | // consistent. |
| 511 | // TODO(wdg): In future CL, |total_frequency_connections_| is used to |
| 512 | // calculate percentiles for progressive scan. This check needs to be in |
| 513 | // place so _that_ value doesn't wrap, either. |
| 514 | // TODO(wdg): Replace this, simple, 'forever' collection of connection |
| 515 | // statistics with a more clever 'aging' algorithm. crbug.com/227233 |
| 516 | if (total_frequency_connections_ + 1 == std::numeric_limits<int64_t>::max()) { |
| 517 | LOG(ERROR) << "Shill has logged " << total_frequency_connections_ |
| 518 | << " connections -- must be an error. Resetting connection " |
| 519 | << "count."; |
| 520 | connect_count_by_frequency_.clear(); |
| 521 | } |
| 522 | |
| 523 | int64 previous_value = 0; |
| 524 | if (ContainsKey(connect_count_by_frequency_, frequency_mhz)) { |
| 525 | previous_value = connect_count_by_frequency_[frequency_mhz]; |
| 526 | } |
| 527 | |
| 528 | connect_count_by_frequency_[frequency_mhz] = ++previous_value; |
| 529 | ++total_frequency_connections_; |
| 530 | manager_->UpdateWiFiProvider(); |
| 531 | |
| 532 | metrics_->SendToUMA( |
| 533 | Metrics::kMetricFrequenciesConnectedEver, |
| 534 | connect_count_by_frequency_.size(), |
| 535 | Metrics::kMetricFrequenciesConnectedMin, |
| 536 | Metrics::kMetricFrequenciesConnectedMax, |
| 537 | Metrics::kMetricFrequenciesConnectedNumBuckets); |
| 538 | } |
| 539 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 540 | } // namespace shill |