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 | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 16 | #include "shill/provider_interface.h" |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 17 | #include "shill/refptr_types.h" |
| 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | class ControlInterface; |
| 22 | class Error; |
| 23 | class EventDispatcher; |
| 24 | class KeyValueStore; |
| 25 | class Manager; |
| 26 | class Metrics; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 27 | class StoreInterface; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 28 | class Time; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 29 | class WiFiEndpoint; |
| 30 | class WiFiService; |
| 31 | |
| 32 | // The WiFi Provider is the holder of all WiFi Services. It holds both |
| 33 | // visible (created due to an Endpoint becoming visible) and invisible |
| 34 | // (created due to user or storage configuration) Services. |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 35 | class WiFiProvider : public ProviderInterface { |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 36 | public: |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 37 | static const char kStorageFrequencies[]; |
| 38 | static const int kMaxStorageFrequencies; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 39 | typedef std::map<uint16, int64> ConnectFrequencyMap; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 40 | // The key to |ConnectFrequencyMapDated| is the number of days since the |
| 41 | // Epoch. |
| 42 | typedef std::map<time_t, ConnectFrequencyMap> ConnectFrequencyMapDated; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 43 | struct FrequencyCount { |
| 44 | FrequencyCount() : frequency(0), connection_count(0) {} |
| 45 | FrequencyCount(uint16 freq, size_t conn) |
| 46 | : frequency(freq), connection_count(conn) {} |
| 47 | uint16 frequency; |
| 48 | size_t connection_count; // Number of successful connections at this |
| 49 | // frequency. |
| 50 | }; |
| 51 | typedef std::deque<FrequencyCount> FrequencyCountList; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 52 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 53 | WiFiProvider(ControlInterface *control_interface, |
| 54 | EventDispatcher *dispatcher, |
| 55 | Metrics *metrics, |
| 56 | Manager *manager); |
| 57 | virtual ~WiFiProvider(); |
| 58 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 59 | // Called by Manager as a part of the Provider interface. The attributes |
| 60 | // used for matching services for the WiFi provider are the SSID, mode and |
| 61 | // security parameters. |
| 62 | virtual void CreateServicesFromProfile(const ProfileRefPtr &profile) override; |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 63 | virtual ServiceRefPtr FindSimilarService( |
| 64 | const KeyValueStore &args, Error *error) const override; |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 65 | virtual ServiceRefPtr GetService(const KeyValueStore &args, |
| 66 | Error *error) override; |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 67 | virtual ServiceRefPtr CreateTemporaryService( |
| 68 | const KeyValueStore &args, Error *error) override; |
| 69 | virtual void Start() override; |
| 70 | virtual void Stop() override; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 71 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 72 | // Find a Service this Endpoint should be associated with. |
| 73 | virtual WiFiServiceRefPtr FindServiceForEndpoint( |
| 74 | const WiFiEndpointConstRefPtr &endpoint); |
| 75 | |
| 76 | // Find or create a Service for |endpoint| to be associated with. This |
| 77 | // method first calls FindServiceForEndpoint, and failing this, creates |
| 78 | // a new Service. It then associates |endpoint| with this service. |
| 79 | virtual void OnEndpointAdded(const WiFiEndpointConstRefPtr &endpoint); |
| 80 | |
| 81 | // Called by a Device when it removes an Endpoint. If the Provider |
| 82 | // forgets a service as a result, it returns a reference to the |
| 83 | // forgotten service, otherwise it returns a null reference. |
| 84 | virtual WiFiServiceRefPtr OnEndpointRemoved( |
| 85 | const WiFiEndpointConstRefPtr &endpoint); |
| 86 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 87 | // Called by a Device when it receives notification that an Endpoint |
| 88 | // has changed. Ensure the updated endpoint still matches its |
| 89 | // associated service. If necessary re-assign the endpoint to a new |
| 90 | // service, otherwise notify the associated service of the update to |
| 91 | // the endpoint. |
| 92 | virtual void OnEndpointUpdated(const WiFiEndpointConstRefPtr &endpoint); |
| 93 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 94 | // Called by a WiFiService when it is unloaded and no longer visible. |
| 95 | virtual bool OnServiceUnloaded(const WiFiServiceRefPtr &service); |
| 96 | |
| 97 | // Get the list of SSIDs for hidden WiFi services we are aware of. |
| 98 | virtual ByteArrays GetHiddenSSIDList(); |
| 99 | |
| 100 | // Calls WiFiService::FixupServiceEntries() and adds a UMA metric if |
| 101 | // this causes entries to be updated. |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 102 | virtual void LoadAndFixupServiceEntries(StoreInterface *storage, |
| 103 | bool is_default_profile); |
| 104 | |
| 105 | // Save configuration for wifi_provider to |storage|. |
| 106 | virtual bool Save(StoreInterface *storage) const; |
| 107 | |
| 108 | virtual void IncrementConnectCount(uint16 frequency_mhz); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 109 | |
Wade Guthrie | 5a4e2ef | 2013-04-30 12:51:39 -0700 | [diff] [blame] | 110 | // Returns a list of all of the frequencies on which this device has |
| 111 | // connected. This data is accumulated across multiple shill runs. |
| 112 | virtual FrequencyCountList GetScanFrequencies() const; |
| 113 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 114 | private: |
| 115 | friend class WiFiProviderTest; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 116 | FRIEND_TEST(WiFiProviderTest, FrequencyMapAgingIllegalDay); |
| 117 | FRIEND_TEST(WiFiProviderTest, FrequencyMapBasicAging); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 118 | FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringList); |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 119 | FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringListEmpty); |
| 120 | FRIEND_TEST(WiFiProviderTest, IncrementConnectCount); |
| 121 | FRIEND_TEST(WiFiProviderTest, IncrementConnectCountCreateNew); |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 122 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntries); |
| 123 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo); |
| 124 | FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMap); |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 125 | FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMapEmpty); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 126 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 127 | typedef std::map<const WiFiEndpoint *, WiFiServiceRefPtr> EndpointServiceMap; |
| 128 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 129 | static const char kManagerErrorSSIDTooLong[]; |
| 130 | static const char kManagerErrorSSIDTooShort[]; |
| 131 | static const char kManagerErrorSSIDRequired[]; |
| 132 | static const char kManagerErrorUnsupportedSecurityMode[]; |
| 133 | static const char kManagerErrorUnsupportedServiceMode[]; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 134 | static const char kFrequencyDelimiter; |
| 135 | static const char kStartWeekHeader[]; |
| 136 | static const time_t kIllegalStartWeek; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 137 | static const char kStorageId[]; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 138 | static const time_t kWeeksToKeepFrequencyCounts; |
| 139 | static const time_t kSecondsPerWeek; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 140 | |
| 141 | // Add a service to the service_ vector and register it with the Manager. |
| 142 | WiFiServiceRefPtr AddService(const std::vector<uint8_t> &ssid, |
| 143 | const std::string &mode, |
| 144 | const std::string &security, |
| 145 | bool is_hidden); |
| 146 | |
| 147 | // Find a service given its properties. |
| 148 | WiFiServiceRefPtr FindService(const std::vector<uint8_t> &ssid, |
| 149 | const std::string &mode, |
| 150 | const std::string &security) const; |
| 151 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 152 | // Returns a WiFiServiceRefPtr for unit tests and for down-casting to a |
| 153 | // ServiceRefPtr in GetService(). |
| 154 | WiFiServiceRefPtr GetWiFiService(const KeyValueStore &args, Error *error); |
| 155 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 156 | // Disassociate the service from its WiFi device and remove it from the |
| 157 | // services_ vector. |
| 158 | void ForgetService(const WiFiServiceRefPtr &service); |
| 159 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 160 | // Retrieve a WiFi service's identifying properties from passed-in |args|. |
| 161 | // Returns true if |args| are valid and populates |ssid|, |mode|, |
| 162 | // |security| and |hidden_ssid|, if successful. Otherwise, this function |
| 163 | // returns false and populates |error| with the reason for failure. It |
| 164 | // is a fatal error if the "Type" parameter passed in |args| is not |
| 165 | // flimflam::kWiFi. |
| 166 | static bool GetServiceParametersFromArgs(const KeyValueStore &args, |
| 167 | std::vector<uint8_t> *ssid_bytes, |
| 168 | std::string *mode, |
| 169 | std::string *security_method, |
| 170 | bool *hidden_ssid, |
| 171 | Error *error); |
| 172 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 173 | // Converts frequency profile information from a list of strings of the form |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 174 | // "frequency:connection_count" to a form consistent with |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 175 | // |connect_count_by_frequency_|. The first string must be of the form |
| 176 | // [nnn] where |nnn| is a positive integer that represents the creation time |
| 177 | // (number of days since the Epoch) of the data. |
| 178 | static time_t StringListToFrequencyMap( |
| 179 | const std::vector<std::string> &strings, |
| 180 | ConnectFrequencyMap *numbers); |
| 181 | |
| 182 | // Extracts the start week from the first string in the StringList for |
| 183 | // |StringListToFrequencyMap|. |
| 184 | static time_t GetStringListStartWeek(const std::string &week_string); |
| 185 | |
| 186 | // Extracts frequency and connection count from a string from the StringList |
| 187 | // for |StringListToFrequencyMap|. Places those values in |numbers|. |
| 188 | static void ParseStringListFreqCount(const std::string &freq_count_string, |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 189 | ConnectFrequencyMap *numbers); |
| 190 | |
| 191 | // Converts frequency profile information from a form consistent with |
| 192 | // |connect_count_by_frequency_| to a list of strings of the form |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 193 | // "frequency:connection_count". The |creation_day| is the day that the |
| 194 | // data was first createed (represented as the number of days since the |
| 195 | // Epoch). |
| 196 | static void FrequencyMapToStringList(time_t creation_day, |
| 197 | const ConnectFrequencyMap &numbers, |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 198 | std::vector<std::string> *strings); |
| 199 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 200 | ControlInterface *control_interface_; |
| 201 | EventDispatcher *dispatcher_; |
| 202 | Metrics *metrics_; |
| 203 | Manager *manager_; |
| 204 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 205 | std::vector<WiFiServiceRefPtr> services_; |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 206 | EndpointServiceMap service_by_endpoint_; |
| 207 | |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 208 | bool running_; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 209 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 210 | // Map of frequencies at which we've connected and the number of times a |
| 211 | // successful connection has been made at that frequency. Absent frequencies |
| 212 | // have not had a successful connection. |
| 213 | ConnectFrequencyMap connect_count_by_frequency_; |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 214 | // A number of entries of |ConnectFrequencyMap| stored by date of creation. |
| 215 | ConnectFrequencyMapDated connect_count_by_frequency_dated_; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 216 | |
| 217 | // Count of successful wifi connections we've made. |
| 218 | int64_t total_frequency_connections_; |
| 219 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 220 | Time *time_; |
| 221 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 222 | DISALLOW_COPY_AND_ASSIGN(WiFiProvider); |
| 223 | }; |
| 224 | |
| 225 | } // namespace shill |
| 226 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 227 | #endif // SHILL_WIFI_PROVIDER_H_ |