blob: b77ed9662df5cbda70a9e8adc73e45ccc21689a8 [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 Guthrie7c2d34e2013-05-09 14:02:20 -07008#include <time.h>
9
Wade Guthriea60a11c2013-04-12 17:47:34 -070010#include <deque>
Wade Guthrie60a37062013-04-02 11:39:09 -070011#include <map>
12
13#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart21f2aae2013-01-17 17:10:08 -080014
Paul Stewart3c504012013-01-17 17:49:58 -080015#include "shill/accessor_interface.h" // for ByteArrays
Paul Stewart21f2aae2013-01-17 17:10:08 -080016#include "shill/refptr_types.h"
17
18namespace shill {
19
20class ControlInterface;
21class Error;
22class EventDispatcher;
23class KeyValueStore;
24class Manager;
25class Metrics;
Paul Stewart3c504012013-01-17 17:49:58 -080026class StoreInterface;
Wade Guthrie7c2d34e2013-05-09 14:02:20 -070027class Time;
Paul Stewart21f2aae2013-01-17 17:10:08 -080028class WiFiEndpoint;
29class WiFiService;
30
31// The WiFi Provider is the holder of all WiFi Services. It holds both
32// visible (created due to an Endpoint becoming visible) and invisible
33// (created due to user or storage configuration) Services.
34class WiFiProvider {
35 public:
Wade Guthrie7c2d34e2013-05-09 14:02:20 -070036 static const char kStorageFrequencies[];
37 static const int kMaxStorageFrequencies;
Wade Guthrie60a37062013-04-02 11:39:09 -070038 typedef std::map<uint16, int64> ConnectFrequencyMap;
Wade Guthrie7c2d34e2013-05-09 14:02:20 -070039 // The key to |ConnectFrequencyMapDated| is the number of days since the
40 // Epoch.
41 typedef std::map<time_t, ConnectFrequencyMap> ConnectFrequencyMapDated;
Wade Guthriea60a11c2013-04-12 17:47:34 -070042 struct FrequencyCount {
43 FrequencyCount() : frequency(0), connection_count(0) {}
44 FrequencyCount(uint16 freq, size_t conn)
45 : frequency(freq), connection_count(conn) {}
46 uint16 frequency;
47 size_t connection_count; // Number of successful connections at this
48 // frequency.
49 };
50 typedef std::deque<FrequencyCount> FrequencyCountList;
Wade Guthrie60a37062013-04-02 11:39:09 -070051
Paul Stewart21f2aae2013-01-17 17:10:08 -080052 WiFiProvider(ControlInterface *control_interface,
53 EventDispatcher *dispatcher,
54 Metrics *metrics,
55 Manager *manager);
56 virtual ~WiFiProvider();
57
58 virtual void Start();
59 virtual void Stop();
60
61 // Called by Manager.
Paul Stewart3c504012013-01-17 17:49:58 -080062 virtual void CreateServicesFromProfile(const ProfileRefPtr &profile);
Paul Stewart21f2aae2013-01-17 17:10:08 -080063 virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error);
64
Paul Stewartd2e1c362013-03-03 19:06:07 -080065 // Find a Service with the same SSID, mode and security as provided
66 // in |args|. Returns a reference to a matching service if one
67 // exists. Otherwise it returns a NULL reference and populates |error|.
68 virtual WiFiServiceRefPtr FindSimilarService(
69 const KeyValueStore &args, Error *error) const;
70
71 // Create a temporary WiFiService with the mode, ssid, security and
72 // hidden properties populated from |args|. Callers outside of the
73 // WiFiProvider must must never register this service with the Manager
74 // or connect it since it was never added to the provider's service list.
75 virtual WiFiServiceRefPtr CreateTemporaryService(
76 const KeyValueStore &args, Error *error);
77
Paul Stewart3c504012013-01-17 17:49:58 -080078 // Find a Service this Endpoint should be associated with.
79 virtual WiFiServiceRefPtr FindServiceForEndpoint(
80 const WiFiEndpointConstRefPtr &endpoint);
81
82 // Find or create a Service for |endpoint| to be associated with. This
83 // method first calls FindServiceForEndpoint, and failing this, creates
84 // a new Service. It then associates |endpoint| with this service.
85 virtual void OnEndpointAdded(const WiFiEndpointConstRefPtr &endpoint);
86
87 // Called by a Device when it removes an Endpoint. If the Provider
88 // forgets a service as a result, it returns a reference to the
89 // forgotten service, otherwise it returns a null reference.
90 virtual WiFiServiceRefPtr OnEndpointRemoved(
91 const WiFiEndpointConstRefPtr &endpoint);
92
Paul Stewart0427cc12013-03-25 13:50:39 -070093 // Called by a Device when it receives notification that an Endpoint
94 // has changed. Ensure the updated endpoint still matches its
95 // associated service. If necessary re-assign the endpoint to a new
96 // service, otherwise notify the associated service of the update to
97 // the endpoint.
98 virtual void OnEndpointUpdated(const WiFiEndpointConstRefPtr &endpoint);
99
Paul Stewart3c504012013-01-17 17:49:58 -0800100 // Called by a WiFiService when it is unloaded and no longer visible.
101 virtual bool OnServiceUnloaded(const WiFiServiceRefPtr &service);
102
103 // Get the list of SSIDs for hidden WiFi services we are aware of.
104 virtual ByteArrays GetHiddenSSIDList();
105
106 // Calls WiFiService::FixupServiceEntries() and adds a UMA metric if
107 // this causes entries to be updated.
Wade Guthrie60a37062013-04-02 11:39:09 -0700108 virtual void LoadAndFixupServiceEntries(StoreInterface *storage,
109 bool is_default_profile);
110
111 // Save configuration for wifi_provider to |storage|.
112 virtual bool Save(StoreInterface *storage) const;
113
114 virtual void IncrementConnectCount(uint16 frequency_mhz);
Paul Stewart21f2aae2013-01-17 17:10:08 -0800115
Wade Guthrie5a4e2ef2013-04-30 12:51:39 -0700116 // Returns a list of all of the frequencies on which this device has
117 // connected. This data is accumulated across multiple shill runs.
118 virtual FrequencyCountList GetScanFrequencies() const;
119
Paul Stewart21f2aae2013-01-17 17:10:08 -0800120 private:
121 friend class WiFiProviderTest;
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700122 FRIEND_TEST(WiFiProviderTest, FrequencyMapAgingIllegalDay);
123 FRIEND_TEST(WiFiProviderTest, FrequencyMapBasicAging);
Wade Guthrie60a37062013-04-02 11:39:09 -0700124 FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringList);
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700125 FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringListEmpty);
126 FRIEND_TEST(WiFiProviderTest, IncrementConnectCount);
127 FRIEND_TEST(WiFiProviderTest, IncrementConnectCountCreateNew);
Wade Guthrie60a37062013-04-02 11:39:09 -0700128 FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntries);
129 FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo);
130 FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMap);
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700131 FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMapEmpty);
Paul Stewart21f2aae2013-01-17 17:10:08 -0800132
Paul Stewart0427cc12013-03-25 13:50:39 -0700133 typedef std::map<const WiFiEndpoint *, WiFiServiceRefPtr> EndpointServiceMap;
134
Paul Stewart3c504012013-01-17 17:49:58 -0800135 static const char kManagerErrorSSIDTooLong[];
136 static const char kManagerErrorSSIDTooShort[];
137 static const char kManagerErrorSSIDRequired[];
138 static const char kManagerErrorUnsupportedSecurityMode[];
139 static const char kManagerErrorUnsupportedServiceMode[];
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700140 static const char kFrequencyDelimiter;
141 static const char kStartWeekHeader[];
142 static const time_t kIllegalStartWeek;
Wade Guthrie60a37062013-04-02 11:39:09 -0700143 static const char kStorageId[];
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700144 static const time_t kWeeksToKeepFrequencyCounts;
145 static const time_t kSecondsPerWeek;
Paul Stewart3c504012013-01-17 17:49:58 -0800146
147 // Add a service to the service_ vector and register it with the Manager.
148 WiFiServiceRefPtr AddService(const std::vector<uint8_t> &ssid,
149 const std::string &mode,
150 const std::string &security,
151 bool is_hidden);
152
153 // Find a service given its properties.
154 WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid,
155 const std::string &mode,
156 const std::string &security) const;
157
158 // Disassociate the service from its WiFi device and remove it from the
159 // services_ vector.
160 void ForgetService(const WiFiServiceRefPtr &service);
161
Paul Stewartd2e1c362013-03-03 19:06:07 -0800162 // Retrieve a WiFi service's identifying properties from passed-in |args|.
163 // Returns true if |args| are valid and populates |ssid|, |mode|,
164 // |security| and |hidden_ssid|, if successful. Otherwise, this function
165 // returns false and populates |error| with the reason for failure. It
166 // is a fatal error if the "Type" parameter passed in |args| is not
167 // flimflam::kWiFi.
168 static bool GetServiceParametersFromArgs(const KeyValueStore &args,
169 std::vector<uint8_t> *ssid_bytes,
170 std::string *mode,
171 std::string *security_method,
172 bool *hidden_ssid,
173 Error *error);
174
Wade Guthrie60a37062013-04-02 11:39:09 -0700175 // Converts frequency profile information from a list of strings of the form
Wade Guthriea60a11c2013-04-12 17:47:34 -0700176 // "frequency:connection_count" to a form consistent with
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700177 // |connect_count_by_frequency_|. The first string must be of the form
178 // [nnn] where |nnn| is a positive integer that represents the creation time
179 // (number of days since the Epoch) of the data.
180 static time_t StringListToFrequencyMap(
181 const std::vector<std::string> &strings,
182 ConnectFrequencyMap *numbers);
183
184 // Extracts the start week from the first string in the StringList for
185 // |StringListToFrequencyMap|.
186 static time_t GetStringListStartWeek(const std::string &week_string);
187
188 // Extracts frequency and connection count from a string from the StringList
189 // for |StringListToFrequencyMap|. Places those values in |numbers|.
190 static void ParseStringListFreqCount(const std::string &freq_count_string,
Wade Guthrie60a37062013-04-02 11:39:09 -0700191 ConnectFrequencyMap *numbers);
192
193 // Converts frequency profile information from a form consistent with
194 // |connect_count_by_frequency_| to a list of strings of the form
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700195 // "frequency:connection_count". The |creation_day| is the day that the
196 // data was first createed (represented as the number of days since the
197 // Epoch).
198 static void FrequencyMapToStringList(time_t creation_day,
199 const ConnectFrequencyMap &numbers,
Wade Guthrie60a37062013-04-02 11:39:09 -0700200 std::vector<std::string> *strings);
201
Paul Stewart21f2aae2013-01-17 17:10:08 -0800202 ControlInterface *control_interface_;
203 EventDispatcher *dispatcher_;
204 Metrics *metrics_;
205 Manager *manager_;
206
Paul Stewart3c504012013-01-17 17:49:58 -0800207 std::vector<WiFiServiceRefPtr> services_;
Paul Stewart0427cc12013-03-25 13:50:39 -0700208 EndpointServiceMap service_by_endpoint_;
209
Paul Stewart6c351ff2013-02-25 15:13:03 -0800210 bool running_;
Paul Stewart3c504012013-01-17 17:49:58 -0800211
Wade Guthrie60a37062013-04-02 11:39:09 -0700212 // Map of frequencies at which we've connected and the number of times a
213 // successful connection has been made at that frequency. Absent frequencies
214 // have not had a successful connection.
215 ConnectFrequencyMap connect_count_by_frequency_;
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700216 // A number of entries of |ConnectFrequencyMap| stored by date of creation.
217 ConnectFrequencyMapDated connect_count_by_frequency_dated_;
Wade Guthrie60a37062013-04-02 11:39:09 -0700218
219 // Count of successful wifi connections we've made.
220 int64_t total_frequency_connections_;
221
Wade Guthrie7c2d34e2013-05-09 14:02:20 -0700222 Time *time_;
223
Paul Stewart21f2aae2013-01-17 17:10:08 -0800224 DISALLOW_COPY_AND_ASSIGN(WiFiProvider);
225};
226
227} // namespace shill
228
Wade Guthrie60a37062013-04-02 11:39:09 -0700229#endif // SHILL_WIFI_PROVIDER_H_