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 | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 8 | #include <deque> |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 9 | #include <map> |
| 10 | |
| 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 12 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 13 | #include "shill/accessor_interface.h" // for ByteArrays |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 14 | #include "shill/refptr_types.h" |
| 15 | |
| 16 | namespace shill { |
| 17 | |
| 18 | class ControlInterface; |
| 19 | class Error; |
| 20 | class EventDispatcher; |
| 21 | class KeyValueStore; |
| 22 | class Manager; |
| 23 | class Metrics; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 24 | class StoreInterface; |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 25 | class WiFiEndpoint; |
| 26 | class 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. |
| 31 | class WiFiProvider { |
| 32 | public: |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 33 | typedef std::map<uint16, int64> ConnectFrequencyMap; |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 34 | 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 43 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 44 | 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 54 | virtual void CreateServicesFromProfile(const ProfileRefPtr &profile); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 55 | virtual WiFiServiceRefPtr GetService(const KeyValueStore &args, Error *error); |
| 56 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 57 | // 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 70 | // 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 Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 85 | // 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 Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 92 | // 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 100 | 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 Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 107 | |
| 108 | private: |
| 109 | friend class WiFiProviderTest; |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 110 | FRIEND_TEST(WiFiProviderTest, FrequencyMapToStringList); |
| 111 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntries); |
| 112 | FRIEND_TEST(WiFiProviderTest, LoadAndFixupServiceEntriesNothingToDo); |
| 113 | FRIEND_TEST(WiFiProviderTest, StringListToFrequencyMap); |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 114 | |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 115 | typedef std::map<const WiFiEndpoint *, WiFiServiceRefPtr> EndpointServiceMap; |
| 116 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 117 | 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 122 | static const char kFrequencyDelimiter[]; |
| 123 | static const char kStorageId[]; |
| 124 | static const char kStorageFrequencies[]; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 125 | |
| 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 Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 141 | // 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 Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 154 | // Converts frequency profile information from a list of strings of the form |
Wade Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 155 | // "frequency:connection_count" to a form consistent with |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 156 | // |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 Guthrie | a60a11c | 2013-04-12 17:47:34 -0700 | [diff] [blame] | 162 | // "frequency:connection_count" |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 163 | static void FrequencyMapToStringList(const ConnectFrequencyMap &numbers, |
| 164 | std::vector<std::string> *strings); |
| 165 | |
Paul Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 166 | ControlInterface *control_interface_; |
| 167 | EventDispatcher *dispatcher_; |
| 168 | Metrics *metrics_; |
| 169 | Manager *manager_; |
| 170 | |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 171 | std::vector<WiFiServiceRefPtr> services_; |
Paul Stewart | 0427cc1 | 2013-03-25 13:50:39 -0700 | [diff] [blame] | 172 | EndpointServiceMap service_by_endpoint_; |
| 173 | |
Paul Stewart | 6c351ff | 2013-02-25 15:13:03 -0800 | [diff] [blame] | 174 | bool running_; |
Paul Stewart | 3c50401 | 2013-01-17 17:49:58 -0800 | [diff] [blame] | 175 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 176 | // 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 Stewart | 21f2aae | 2013-01-17 17:10:08 -0800 | [diff] [blame] | 184 | DISALLOW_COPY_AND_ASSIGN(WiFiProvider); |
| 185 | }; |
| 186 | |
| 187 | } // namespace shill |
| 188 | |
Wade Guthrie | 60a3706 | 2013-04-02 11:39:09 -0700 | [diff] [blame] | 189 | #endif // SHILL_WIFI_PROVIDER_H_ |