blob: 7cf8d7727164abfeac7d560443f1ebd3322588b3 [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawalb54601c2011-06-07 17:39:22 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_WIFI_SERVICE_
6#define SHILL_WIFI_SERVICE_
7
mukesh agrawal261daca2011-12-02 18:56:56 +00008#include <set>
mukesh agrawalb54601c2011-06-07 17:39:22 -07009#include <string>
10#include <vector>
11
Darin Petkov4a09b6b2011-07-19 12:52:06 -070012#include "shill/dbus_bindings/supplicant-interface.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070013#include "shill/event_dispatcher.h"
Chris Masone2b105542011-06-22 10:58:09 -070014#include "shill/refptr_types.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070015#include "shill/service.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070016
17namespace shill {
18
Chris Masone6791a432011-07-12 13:23:19 -070019class ControlInterface;
20class EventDispatcher;
mukesh agrawal1a056262011-10-05 14:36:54 -070021class Error;
Chris Masone6791a432011-07-12 13:23:19 -070022class Manager;
23
mukesh agrawalb54601c2011-06-07 17:39:22 -070024class WiFiService : public Service {
25 public:
26 WiFiService(ControlInterface *control_interface,
27 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070028 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070029 const WiFiRefPtr &device,
mukesh agrawal7ec71312011-11-10 02:08:26 +000030 const std::vector<uint8_t> &ssid,
Chris Masone092df3e2011-08-22 09:41:39 -070031 const std::string &mode,
Paul Stewartced6a0b2011-11-08 15:32:04 -080032 const std::string &security,
33 bool hidden_ssid);
mukesh agrawalb54601c2011-06-07 17:39:22 -070034 ~WiFiService();
Darin Petkov4d6d9412011-08-24 13:19:54 -070035
36 // Inherited from Service.
mukesh agrawal8a3188d2011-12-01 20:56:44 +000037 virtual void AutoConnect();
Darin Petkov4d6d9412011-08-24 13:19:54 -070038 virtual void Connect(Error *error);
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +000039 virtual void Disconnect(Error *error);
Chris Masone34af2182011-08-22 11:59:36 -070040
Paul Stewart22aa71b2011-09-16 12:15:11 -070041 virtual bool TechnologyIs(const Technology::Identifier type) const;
mukesh agrawal8a3188d2011-12-01 20:56:44 +000042 virtual bool IsConnecting() const;
Paul Stewart22aa71b2011-09-16 12:15:11 -070043
mukesh agrawal261daca2011-12-02 18:56:56 +000044 virtual void AddEndpoint(WiFiEndpointConstRefPtr endpoint);
45 virtual void RemoveEndpoint(WiFiEndpointConstRefPtr endpoint);
mukesh agrawal4eb4d782011-12-05 17:34:37 +000046 bool NumEndpoints() const { return endpoints_.size(); }
mukesh agrawal261daca2011-12-02 18:56:56 +000047
Chris Masone34af2182011-08-22 11:59:36 -070048 // wifi_<MAC>_<BSSID>_<mode_string>_<security_string>
Chris Masone6515aab2011-10-12 16:19:09 -070049 std::string GetStorageIdentifier() const;
Paul Stewarta41e38d2011-11-11 07:47:29 -080050 static bool ParseStorageIdentifier(const std::string &storage_name,
51 std::string *address,
52 std::string *mode,
53 std::string *security);
Chris Masone34af2182011-08-22 11:59:36 -070054
Thieu Le48e6d6d2011-12-06 00:40:27 +000055 const std::string &mode() const { return mode_; }
56 const std::string &key_management() const { return GetEAPKeyManagement(); }
57 const std::vector<uint8_t> &ssid() const { return ssid_; }
mukesh agrawalb54601c2011-06-07 17:39:22 -070058
mukesh agrawal1a056262011-10-05 14:36:54 -070059 void SetPassphrase(const std::string &passphrase, Error *error);
60
Paul Stewartd08f4432011-11-04 07:48:20 -070061 // Overrride Load and Save from parent Service class. We will call
62 // the parent method.
63 virtual bool IsLoadableFrom(StoreInterface *storage) const;
64 virtual bool Load(StoreInterface *storage);
65 virtual bool Save(StoreInterface *storage);
Paul Stewartd8ad3c42012-01-09 12:39:38 -080066 virtual void Unload();
Paul Stewartd08f4432011-11-04 07:48:20 -070067
mukesh agrawal8a3188d2011-12-01 20:56:44 +000068 virtual bool HasEndpoints() const { return !endpoints_.empty(); }
Paul Stewarta41e38d2011-11-11 07:47:29 -080069 virtual bool IsVisible() const;
Paul Stewart6ab23a92011-11-09 17:17:47 -080070 bool IsSecurityMatch(const std::string &security) const;
Paul Stewartced6a0b2011-11-08 15:32:04 -080071 bool hidden_ssid() const { return hidden_ssid_; }
72
Thieu Le48e6d6d2011-12-06 00:40:27 +000073 virtual void InitializeCustomMetrics() const;
74 virtual void SendPostReadyStateMetrics() const;
75
mukesh agrawal8a3188d2011-12-01 20:56:44 +000076 protected:
77 virtual bool IsAutoConnectable() const;
78
mukesh agrawalb54601c2011-06-07 17:39:22 -070079 private:
Paul Stewartd08f4432011-11-04 07:48:20 -070080 friend class WiFiServiceSecurityTest;
Thieu Lead1ec2c2012-01-05 23:39:48 +000081 FRIEND_TEST(MetricsTest, WiFiServicePostReady);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000082 FRIEND_TEST(WiFiServiceTest, AutoConnect);
mukesh agrawalf2fd7452011-10-03 16:38:47 -070083 FRIEND_TEST(WiFiServiceTest, ConnectTaskRSN);
84 FRIEND_TEST(WiFiServiceTest, ConnectTaskWPA);
Gaurav Shahf8721ee2011-11-07 09:12:46 -080085 FRIEND_TEST(WiFiServiceTest, ConnectTaskPSK);
Thieu Lef4cbda92011-11-10 23:41:24 +000086 FRIEND_TEST(WiFiServiceTest, ConnectTaskWEP);
mukesh agrawal8a3188d2011-12-01 20:56:44 +000087 FRIEND_TEST(WiFiServiceTest, IsAutoConnectable);
Paul Stewartd08f4432011-11-04 07:48:20 -070088 FRIEND_TEST(WiFiServiceTest, LoadHidden);
Paul Stewartd8ad3c42012-01-09 12:39:38 -080089 FRIEND_TEST(WiFiServiceTest, LoadAndUnloadPassphrase);
Paul Stewartd08f4432011-11-04 07:48:20 -070090
91 static const char kStorageHiddenSSID[];
Paul Stewart2706aaf2011-12-14 16:44:04 -080092 static const char kStorageMode[];
93 static const char kStoragePassphrase[];
94 static const char kStorageSecurity[];
95 static const char kStorageSSID[];
mukesh agrawal6e277772011-09-29 15:04:23 -070096
Thieu Lef7709452011-11-15 01:13:19 +000097 void HelpRegisterDerivedString(
98 PropertyStore *store,
99 const std::string &name,
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800100 std::string(WiFiService::*get)(Error *error),
101 void(WiFiService::*set)(const std::string &value, Error *error));
Thieu Lef7709452011-11-15 01:13:19 +0000102
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700103 void ConnectTask();
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000104 void DisconnectTask();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700105
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800106 std::string GetDeviceRpcId(Error *error);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000107 void UpdateConnectable();
Chris Masone95207da2011-06-29 16:50:49 -0700108
Thieu Lef4cbda92011-11-10 23:41:24 +0000109 static void ValidateWEPPassphrase(const std::string &passphrase,
110 Error *error);
111 static void ValidateWPAPassphrase(const std::string &passphrase,
112 Error *error);
113 static void ParseWEPPassphrase(const std::string &passphrase,
114 int *key_index,
115 std::vector<uint8> *password_bytes,
116 Error *error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700117 static bool CheckWEPIsHex(const std::string &passphrase, Error *error);
118 static bool CheckWEPKeyIndex(const std::string &passphrase, Error *error);
119 static bool CheckWEPPrefix(const std::string &passphrase, Error *error);
120
mukesh agrawald835b202011-10-07 15:26:47 -0700121 // replace non-ASCII characters with '?'. return true if one or more
122 // characters were changed
123 static bool SanitizeSSID(std::string *ssid);
124
Paul Stewart6ab23a92011-11-09 17:17:47 -0800125 // "wpa", "rsn" and "psk" are equivalent from a configuration perspective.
126 // This function maps them all into "psk".
127 static std::string GetSecurityClass(const std::string &security);
128
Paul Stewartd08f4432011-11-04 07:48:20 -0700129 // Profile data for a WPA/RSN service can be stored under a number of
130 // different names. These functions create different storage identifiers
131 // based on whether they are referred to by their generic "psk" name or
132 // if they use the (legacy) specific "wpa" or "rsn" names.
133 std::string GetGenericStorageIdentifier() const;
134 std::string GetSpecificStorageIdentifier() const;
135 std::string GetStorageIdentifierForSecurity(
136 const std::string &security) const;
137
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700138 // Properties
139 std::string passphrase_;
140 bool need_passphrase_;
141 std::string security_;
142 uint8 strength_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700143 // TODO(cmasone): see if the below can be pulled from the endpoint associated
144 // with this service instead.
Chris Masone092df3e2011-08-22 09:41:39 -0700145 const std::string mode_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146 std::string auth_mode_;
147 bool hidden_ssid_;
148 uint16 frequency_;
149 uint16 physical_mode_;
mukesh agrawal32399322011-09-01 10:53:43 -0700150 std::string hex_ssid_;
Paul Stewartd08f4432011-11-04 07:48:20 -0700151 std::string storage_identifier_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700152
mukesh agrawalb54601c2011-06-07 17:39:22 -0700153 ScopedRunnableMethodFactory<WiFiService> task_factory_;
Chris Masone2b105542011-06-22 10:58:09 -0700154 WiFiRefPtr wifi_;
mukesh agrawal261daca2011-12-02 18:56:56 +0000155 std::set<WiFiEndpointConstRefPtr> endpoints_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700156 const std::vector<uint8_t> ssid_;
mukesh agrawalb54601c2011-06-07 17:39:22 -0700157 DISALLOW_COPY_AND_ASSIGN(WiFiService);
158};
159
160} // namespace shill
161
162#endif // SHILL_WIFI_SERVICE_