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 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 5 | #ifndef SHILL_WIFI_PROVIDER_H_ |
| 6 | #define SHILL_WIFI_PROVIDER_H_ |
| 7 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 8 | #include <time.h> |
| 9 | |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 10 | #include <deque> |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 11 | #include <map> |
| 12 | |
| 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 14 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 15 | #include "shill/accessor_interface.h" // for ByteArrays |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 16 | #include "shill/refptr_types.h" |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | class ControlInterface; |
| 21 | class Error; |
| 22 | class EventDispatcher; |
| 23 | class KeyValueStore; |
| 24 | class Manager; |
| 25 | class Metrics; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 26 | class StoreInterface; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 27 | class Time; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 28 | class WiFiEndpoint; |
| 29 | class 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. |
| 34 | class WiFiProvider { |
| 35 | public: |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 36 | static const char kStorageFrequencies[]; |
| 37 | static const int kMaxStorageFrequencies; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 38 | typedef std::map<uint16, int64> ConnectFrequencyMap; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 39 | // The key to |ConnectFrequencyMapDated| is the number of days since the |
| 40 | // Epoch. |
| 41 | typedef std::map<time_t, ConnectFrequencyMap> ConnectFrequencyMapDated; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 42 | 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 51 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 52 | 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 62 | virtual void CreateServicesFromProfile(const ProfileRefPtr &profile); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 63 | virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error); |
| 64 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 65 | // 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 78 | // 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 Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 93 | // 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 100 | // 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 108 | 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 Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 115 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 116 | // 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 Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 120 | private: |
| 121 | friend class WiFiProviderTest; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 122 | FRIEND_TEST(WiFiProviderTest, FrequencyMapAgingIllegalDay); |
| 123 | FRIEND_TEST(WiFiProviderTest, FrequencyMapBasicAging); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 124 | FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringList); |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 125 | FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringListEmpty); |
| 126 | FRIEND_TEST(WiFiProviderTest, IncrementConnectCount); |
| 127 | FRIEND_TEST(WiFiProviderTest, IncrementConnectCountCreateNew); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 128 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntries); |
| 129 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo); |
| 130 | FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMap); |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 131 | FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMapEmpty); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 132 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 133 | typedef std::map<const WiFiEndpoint *, WiFiServiceRefPtr> EndpointServiceMap; |
| 134 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 135 | 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 Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 140 | static const char kFrequencyDelimiter; |
| 141 | static const char kStartWeekHeader[]; |
| 142 | static const time_t kIllegalStartWeek; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 143 | static const char kStorageId[]; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 144 | static const time_t kWeeksToKeepFrequencyCounts; |
| 145 | static const time_t kSecondsPerWeek; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 146 | |
| 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 Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 162 | // 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 175 | // Converts frequency profile information from a list of strings of the form |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 176 | // "frequency:connection_count" to a form consistent with |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 177 | // |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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 191 | 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 Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 195 | // "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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 200 | std::vector<std::string> *strings); |
| 201 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 202 | ControlInterface *control_interface_; |
| 203 | EventDispatcher *dispatcher_; |
| 204 | Metrics *metrics_; |
| 205 | Manager *manager_; |
| 206 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 207 | std::vector<WiFiServiceRefPtr> services_; |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 208 | EndpointServiceMap service_by_endpoint_; |
| 209 | |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 210 | bool running_; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 211 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 212 | // 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 Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 216 | // A number of entries of |ConnectFrequencyMap| stored by date of creation. |
| 217 | ConnectFrequencyMapDated connect_count_by_frequency_dated_; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 218 | |
| 219 | // Count of successful wifi connections we've made. |
| 220 | int64_t total_frequency_connections_; |
| 221 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 222 | Time *time_; |
| 223 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 224 | DISALLOW_COPY_AND_ASSIGN(WiFiProvider); |
| 225 | }; |
| 226 | |
| 227 | } // namespace shill |
| 228 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 229 | #endif // SHILL_WIFI_PROVIDER_H_ |