blob: 0e7640c5c58546e576eae0dd9ea3cd4b7e9581fd [file] [log] [blame]
Paul Stewart21f2aae2013-01-17 17:10:08 -08001// 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
Wade Guthrie60a37062013-04-02 11:39:09 -07005#ifndef SHILL_WIFI_PROVIDER_H_
6#define SHILL_WIFI_PROVIDER_H_
7
Wade Guthriea60a11c2013-04-12 17:47:34 -07008#include <deque>
Wade Guthrie60a37062013-04-02 11:39:09 -07009#include <map>
10
11#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart21f2aae2013-01-17 17:10:08 -080012
Paul Stewart3c504012013-01-17 17:49:58 -080013#include "shill/accessor_interface.h" // for ByteArrays
Paul Stewart21f2aae2013-01-17 17:10:08 -080014#include "shill/refptr_types.h"
15
16namespace shill {
17
18class ControlInterface;
19class Error;
20class EventDispatcher;
21class KeyValueStore;
22class Manager;
23class Metrics;
Paul Stewart3c504012013-01-17 17:49:58 -080024class StoreInterface;
Paul Stewart21f2aae2013-01-17 17:10:08 -080025class WiFiEndpoint;
26class WiFiService;
27
28// The WiFi Provider is the holder of all WiFi Services. It holds both
29// visible (created due to an Endpoint becoming visible) and invisible
30// (created due to user or storage configuration) Services.
31class WiFiProvider {
32 public:
Wade Guthrie60a37062013-04-02 11:39:09 -070033 typedef std::map<uint16, int64> ConnectFrequencyMap;
Wade Guthriea60a11c2013-04-12 17:47:34 -070034 struct FrequencyCount {
35 FrequencyCount() : frequency(0), connection_count(0) {}
36 FrequencyCount(uint16 freq, size_t conn)
37 : frequency(freq), connection_count(conn) {}
38 uint16 frequency;
39 size_t connection_count; // Number of successful connections at this
40 // frequency.
41 };
42 typedef std::deque<FrequencyCount> FrequencyCountList;
Wade Guthrie60a37062013-04-02 11:39:09 -070043
Paul Stewart21f2aae2013-01-17 17:10:08 -080044 WiFiProvider(ControlInterface *control_interface,
45 EventDispatcher *dispatcher,
46 Metrics *metrics,
47 Manager *manager);
48 virtual ~WiFiProvider();
49
50 virtual void Start();
51 virtual void Stop();
52
53 // Called by Manager.
Paul Stewart3c504012013-01-17 17:49:58 -080054 virtual void CreateServicesFromProfile(const ProfileRefPtr &profile);
Paul Stewart21f2aae2013-01-17 17:10:08 -080055 virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error);
56
Paul Stewartd2e1c362013-03-03 19:06:07 -080057 // Find a Service with the same SSID, mode and security as provided
58 // in |args|. Returns a reference to a matching service if one
59 // exists. Otherwise it returns a NULL reference and populates |error|.
60 virtual WiFiServiceRefPtr FindSimilarService(
61 const KeyValueStore &args, Error *error) const;
62
63 // Create a temporary WiFiService with the mode, ssid, security and
64 // hidden properties populated from |args|. Callers outside of the
65 // WiFiProvider must must never register this service with the Manager
66 // or connect it since it was never added to the provider's service list.
67 virtual WiFiServiceRefPtr CreateTemporaryService(
68 const KeyValueStore &args, Error *error);
69
Paul Stewart3c504012013-01-17 17:49:58 -080070 // Find a Service this Endpoint should be associated with.
71 virtual WiFiServiceRefPtr FindServiceForEndpoint(
72 const WiFiEndpointConstRefPtr &endpoint);
73
74 // Find or create a Service for |endpoint| to be associated with. This
75 // method first calls FindServiceForEndpoint, and failing this, creates
76 // a new Service. It then associates |endpoint| with this service.
77 virtual void OnEndpointAdded(const WiFiEndpointConstRefPtr &endpoint);
78
79 // Called by a Device when it removes an Endpoint. If the Provider
80 // forgets a service as a result, it returns a reference to the
81 // forgotten service, otherwise it returns a null reference.
82 virtual WiFiServiceRefPtr OnEndpointRemoved(
83 const WiFiEndpointConstRefPtr &endpoint);
84
Paul Stewart0427cc12013-03-25 13:50:39 -070085 // Called by a Device when it receives notification that an Endpoint
86 // has changed. Ensure the updated endpoint still matches its
87 // associated service. If necessary re-assign the endpoint to a new
88 // service, otherwise notify the associated service of the update to
89 // the endpoint.
90 virtual void OnEndpointUpdated(const WiFiEndpointConstRefPtr &endpoint);
91
Paul Stewart3c504012013-01-17 17:49:58 -080092 // Called by a WiFiService when it is unloaded and no longer visible.
93 virtual bool OnServiceUnloaded(const WiFiServiceRefPtr &service);
94
95 // Get the list of SSIDs for hidden WiFi services we are aware of.
96 virtual ByteArrays GetHiddenSSIDList();
97
98 // Calls WiFiService::FixupServiceEntries() and adds a UMA metric if
99 // this causes entries to be updated.
Wade Guthrie60a37062013-04-02 11:39:09 -0700100 virtual void LoadAndFixupServiceEntries(StoreInterface *storage,
101 bool is_default_profile);
102
103 // Save configuration for wifi_provider to |storage|.
104 virtual bool Save(StoreInterface *storage) const;
105
106 virtual void IncrementConnectCount(uint16 frequency_mhz);
Paul Stewart21f2aae2013-01-17 17:10:08 -0800107
108 private:
109 friend class WiFiProviderTest;
Wade Guthrie60a37062013-04-02 11:39:09 -0700110 FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringList);
111 FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntries);
112 FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo);
113 FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMap);
Paul Stewart21f2aae2013-01-17 17:10:08 -0800114
Paul Stewart0427cc12013-03-25 13:50:39 -0700115 typedef std::map<const WiFiEndpoint *, WiFiServiceRefPtr> EndpointServiceMap;
116
Paul Stewart3c504012013-01-17 17:49:58 -0800117 static const char kManagerErrorSSIDTooLong[];
118 static const char kManagerErrorSSIDTooShort[];
119 static const char kManagerErrorSSIDRequired[];
120 static const char kManagerErrorUnsupportedSecurityMode[];
121 static const char kManagerErrorUnsupportedServiceMode[];
Wade Guthrie60a37062013-04-02 11:39:09 -0700122 static const char kFrequencyDelimiter[];
123 static const char kStorageId[];
124 static const char kStorageFrequencies[];
Paul Stewart3c504012013-01-17 17:49:58 -0800125
126 // Add a service to the service_ vector and register it with the Manager.
127 WiFiServiceRefPtr AddService(const std::vector<uint8_t> &ssid,
128 const std::string &mode,
129 const std::string &security,
130 bool is_hidden);
131
132 // Find a service given its properties.
133 WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid,
134 const std::string &mode,
135 const std::string &security) const;
136
137 // Disassociate the service from its WiFi device and remove it from the
138 // services_ vector.
139 void ForgetService(const WiFiServiceRefPtr &service);
140
Paul Stewartd2e1c362013-03-03 19:06:07 -0800141 // Retrieve a WiFi service's identifying properties from passed-in |args|.
142 // Returns true if |args| are valid and populates |ssid|, |mode|,
143 // |security| and |hidden_ssid|, if successful. Otherwise, this function
144 // returns false and populates |error| with the reason for failure. It
145 // is a fatal error if the "Type" parameter passed in |args| is not
146 // flimflam::kWiFi.
147 static bool GetServiceParametersFromArgs(const KeyValueStore &args,
148 std::vector<uint8_t> *ssid_bytes,
149 std::string *mode,
150 std::string *security_method,
151 bool *hidden_ssid,
152 Error *error);
153
Wade Guthrie60a37062013-04-02 11:39:09 -0700154 // Converts frequency profile information from a list of strings of the form
Wade Guthriea60a11c2013-04-12 17:47:34 -0700155 // "frequency:connection_count" to a form consistent with
Wade Guthrie60a37062013-04-02 11:39:09 -0700156 // |connect_count_by_frequency_|
157 static void StringListToFrequencyMap(const std::vector<std::string> &strings,
158 ConnectFrequencyMap *numbers);
159
160 // Converts frequency profile information from a form consistent with
161 // |connect_count_by_frequency_| to a list of strings of the form
Wade Guthriea60a11c2013-04-12 17:47:34 -0700162 // "frequency:connection_count"
Wade Guthrie60a37062013-04-02 11:39:09 -0700163 static void FrequencyMapToStringList(const ConnectFrequencyMap &numbers,
164 std::vector<std::string> *strings);
165
Paul Stewart21f2aae2013-01-17 17:10:08 -0800166 ControlInterface *control_interface_;
167 EventDispatcher *dispatcher_;
168 Metrics *metrics_;
169 Manager *manager_;
170
Paul Stewart3c504012013-01-17 17:49:58 -0800171 std::vector<WiFiServiceRefPtr> services_;
Paul Stewart0427cc12013-03-25 13:50:39 -0700172 EndpointServiceMap service_by_endpoint_;
173
Paul Stewart6c351ff2013-02-25 15:13:03 -0800174 bool running_;
Paul Stewart3c504012013-01-17 17:49:58 -0800175
Wade Guthrie60a37062013-04-02 11:39:09 -0700176 // Map of frequencies at which we've connected and the number of times a
177 // successful connection has been made at that frequency. Absent frequencies
178 // have not had a successful connection.
179 ConnectFrequencyMap connect_count_by_frequency_;
180
181 // Count of successful wifi connections we've made.
182 int64_t total_frequency_connections_;
183
Paul Stewart21f2aae2013-01-17 17:10:08 -0800184 DISALLOW_COPY_AND_ASSIGN(WiFiProvider);
185};
186
187} // namespace shill
188
Wade Guthrie60a37062013-04-02 11:39:09 -0700189#endif // SHILL_WIFI_PROVIDER_H_