Peter Qiu | 326b6cf | 2015-09-02 11:11:42 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef APMANAGER_CONFIG_H_ |
| 18 | #define APMANAGER_CONFIG_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
| 23 | #include <base/macros.h> |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 24 | #include <base/memory/ref_counted.h> |
Alex Vakulenko | 8d0c31b | 2015-10-13 09:14:24 -0700 | [diff] [blame] | 25 | #include <brillo/errors/error.h> |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 26 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 27 | #include "apmanager/config_adaptor_interface.h" |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 28 | |
| 29 | namespace apmanager { |
| 30 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 31 | class Error; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 32 | class Device; |
| 33 | class Manager; |
| 34 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 35 | class Config { |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 36 | public: |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 37 | Config(Manager* manager, int service_identifier); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 38 | virtual ~Config(); |
| 39 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 40 | bool ValidateSsid(Error* error, const std::string& value); |
| 41 | bool ValidateSecurityMode(Error* error, const std::string& value); |
| 42 | bool ValidatePassphrase(Error* error, const std::string& value); |
| 43 | bool ValidateHwMode(Error* error, const std::string& value); |
| 44 | bool ValidateOperationMode(Error* error, const std::string& value); |
| 45 | bool ValidateChannel(Error* error, const uint16_t& value); |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 46 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 47 | // Calculate the frequency based on the given |channel|. Return true and set |
| 48 | // the output |frequency| if is valid channel, false otherwise. |
| 49 | static bool GetFrequencyFromChannel(uint16_t channel, uint32_t* freq); |
| 50 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 51 | // Generate a config file string for a hostapd instance. Populate |
| 52 | // |error| when encounter invalid configuration. Return true if success, |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 53 | // false otherwise. |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 54 | virtual bool GenerateConfigFile(Error* error, std::string* config_str); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 55 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 56 | // Claim and release the device needed for this configuration. |
| 57 | virtual bool ClaimDevice(); |
| 58 | virtual bool ReleaseDevice(); |
| 59 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 60 | // Getter and setter for configuration properties. |
| 61 | void SetSsid(const std::string& ssid); |
| 62 | std::string GetSsid() const; |
| 63 | void SetInterfaceName(const std::string& interface_name); |
| 64 | std::string GetInterfaceName() const; |
| 65 | void SetSecurityMode(const std::string& security_mode); |
| 66 | std::string GetSecurityMode() const; |
| 67 | void SetPassphrase(const std::string& passphrase); |
| 68 | std::string GetPassphrase() const; |
| 69 | void SetHwMode(const std::string& hw_mode); |
| 70 | std::string GetHwMode() const; |
| 71 | void SetOperationMode(const std::string& op_mode); |
| 72 | std::string GetOperationMode() const; |
| 73 | void SetChannel(uint16_t channel); |
| 74 | uint16_t GetChannel() const; |
| 75 | void SetHiddenNetwork(bool hidden); |
| 76 | bool GetHiddenNetwork() const; |
| 77 | void SetBridgeInterface(const std::string& interface_name); |
| 78 | std::string GetBridgeInterface() const; |
| 79 | void SetServerAddressIndex(uint16_t); |
| 80 | uint16_t GetServerAddressIndex() const; |
| 81 | void SetFullDeviceControl(bool full_control); |
| 82 | bool GetFullDeviceControl() const; |
| 83 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 84 | const std::string& control_interface() const { return control_interface_; } |
| 85 | void set_control_interface(const std::string& control_interface) { |
| 86 | control_interface_ = control_interface; |
| 87 | } |
| 88 | |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 89 | const std::string& selected_interface() const { return selected_interface_; } |
| 90 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 91 | ConfigAdaptorInterface* adaptor() const { return adaptor_.get(); } |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | // Keys used in hostapd config file. |
| 95 | static const char kHostapdConfigKeyBridgeInterface[]; |
| 96 | static const char kHostapdConfigKeyChannel[]; |
| 97 | static const char kHostapdConfigKeyControlInterface[]; |
Peter Qiu | bfd410e | 2015-01-09 15:14:20 -0800 | [diff] [blame] | 98 | static const char kHostapdConfigKeyControlInterfaceGroup[]; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 99 | static const char kHostapdConfigKeyDriver[]; |
| 100 | static const char kHostapdConfigKeyFragmThreshold[]; |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 101 | static const char kHostapdConfigKeyHTCapability[]; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 102 | static const char kHostapdConfigKeyHwMode[]; |
| 103 | static const char kHostapdConfigKeyIeee80211ac[]; |
| 104 | static const char kHostapdConfigKeyIeee80211n[]; |
| 105 | static const char kHostapdConfigKeyIgnoreBroadcastSsid[]; |
| 106 | static const char kHostapdConfigKeyInterface[]; |
| 107 | static const char kHostapdConfigKeyRsnPairwise[]; |
| 108 | static const char kHostapdConfigKeyRtsThreshold[]; |
| 109 | static const char kHostapdConfigKeySsid[]; |
| 110 | static const char kHostapdConfigKeyWepDefaultKey[]; |
| 111 | static const char kHostapdConfigKeyWepKey0[]; |
| 112 | static const char kHostapdConfigKeyWpa[]; |
| 113 | static const char kHostapdConfigKeyWpaKeyMgmt[]; |
| 114 | static const char kHostapdConfigKeyWpaPassphrase[]; |
| 115 | |
| 116 | // Hardware mode value for hostapd config file. |
| 117 | static const char kHostapdHwMode80211a[]; |
| 118 | static const char kHostapdHwMode80211b[]; |
| 119 | static const char kHostapdHwMode80211g[]; |
| 120 | |
| 121 | // Default hostapd configuration values. User will not be able to configure |
| 122 | // these. |
| 123 | static const char kHostapdDefaultDriver[]; |
| 124 | static const char kHostapdDefaultRsnPairwise[]; |
| 125 | static const char kHostapdDefaultWpaKeyMgmt[]; |
| 126 | static const int kHostapdDefaultFragmThreshold; |
| 127 | static const int kHostapdDefaultRtsThreshold; |
| 128 | |
| 129 | // Default config property values. |
| 130 | static const uint16_t kPropertyDefaultChannel;; |
| 131 | static const bool kPropertyDefaultHiddenNetwork; |
| 132 | static const uint16_t kPropertyDefaultServerAddressIndex; |
| 133 | |
Peter Qiu | 8e785b9 | 2014-11-24 10:01:08 -0800 | [diff] [blame] | 134 | // Constants use for converting channel to frequency. |
| 135 | static const uint16_t kBand24GHzChannelLow; |
| 136 | static const uint16_t kBand24GHzChannelHigh; |
| 137 | static const uint32_t kBand24GHzBaseFrequency; |
| 138 | static const uint16_t kBand5GHzChannelLow; |
| 139 | static const uint16_t kBand5GHzChannelHigh; |
| 140 | static const uint16_t kBand5GHzBaseFrequency; |
| 141 | |
Peter Qiu | 6830329 | 2014-12-10 10:42:13 -0800 | [diff] [blame] | 142 | static const int kSsidMinLength; |
| 143 | static const int kSsidMaxLength; |
| 144 | static const int kPassphraseMinLength; |
| 145 | static const int kPassphraseMaxLength; |
| 146 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 147 | // Append default hostapd configurations to the config file. |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 148 | bool AppendHostapdDefaults(Error* error, std::string* config_str); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 149 | |
| 150 | // Append hardware mode related configurations to the config file. |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 151 | bool AppendHwMode(Error* error, std::string* config_str); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 152 | |
| 153 | // Determine/append interface configuration to the config file. |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 154 | bool AppendInterface(Error* error, std::string* config_str); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 155 | |
| 156 | // Append security related configurations to the config file. |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 157 | bool AppendSecurityMode(Error* error, std::string* config_str); |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 158 | |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 159 | Manager* manager_; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 160 | std::string control_interface_; |
Peter Qiu | bf8e36c | 2014-12-03 22:59:45 -0800 | [diff] [blame] | 161 | // Interface selected for hostapd. |
| 162 | std::string selected_interface_; |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 163 | scoped_refptr<Device> device_; |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 164 | |
Peter Qiu | d9c79aa | 2015-11-19 15:36:00 -0800 | [diff] [blame] | 165 | std::unique_ptr<ConfigAdaptorInterface> adaptor_; |
| 166 | |
Peter Qiu | f073173 | 2014-11-11 09:46:41 -0800 | [diff] [blame] | 167 | DISALLOW_COPY_AND_ASSIGN(Config); |
| 168 | }; |
| 169 | |
| 170 | } // namespace apmanager |
| 171 | |
| 172 | #endif // APMANAGER_CONFIG_H_ |