blob: bedd92dd53c9ac85b1908c2b75e46983ac46d970 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
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 Qiuf0731732014-11-11 09:46:41 -080016
17#ifndef APMANAGER_CONFIG_H_
18#define APMANAGER_CONFIG_H_
19
20#include <memory>
21#include <string>
22
23#include <base/macros.h>
Peter Qiud9c79aa2015-11-19 15:36:00 -080024#include <base/memory/ref_counted.h>
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070025#include <brillo/errors/error.h>
Peter Qiuf0731732014-11-11 09:46:41 -080026
Peter Qiud9c79aa2015-11-19 15:36:00 -080027#include "apmanager/config_adaptor_interface.h"
Peter Qiuf0731732014-11-11 09:46:41 -080028
29namespace apmanager {
30
Peter Qiud9c79aa2015-11-19 15:36:00 -080031class Error;
Peter Qiufb39ba42014-11-21 09:09:59 -080032class Device;
33class Manager;
34
Peter Qiud9c79aa2015-11-19 15:36:00 -080035class Config {
Peter Qiuf0731732014-11-11 09:46:41 -080036 public:
Peter Qiud9c79aa2015-11-19 15:36:00 -080037 Config(Manager* manager, int service_identifier);
Peter Qiuf0731732014-11-11 09:46:41 -080038 virtual ~Config();
39
Peter Qiud9c79aa2015-11-19 15:36:00 -080040 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 Qiubf8e36c2014-12-03 22:59:45 -080046
Peter Qiu8e785b92014-11-24 10:01:08 -080047 // 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 Qiud9c79aa2015-11-19 15:36:00 -080051 // Generate a config file string for a hostapd instance. Populate
52 // |error| when encounter invalid configuration. Return true if success,
Peter Qiuf0731732014-11-11 09:46:41 -080053 // false otherwise.
Peter Qiud9c79aa2015-11-19 15:36:00 -080054 virtual bool GenerateConfigFile(Error* error, std::string* config_str);
Peter Qiuf0731732014-11-11 09:46:41 -080055
Peter Qiufb39ba42014-11-21 09:09:59 -080056 // Claim and release the device needed for this configuration.
57 virtual bool ClaimDevice();
58 virtual bool ReleaseDevice();
59
Peter Qiud9c79aa2015-11-19 15:36:00 -080060 // 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 Qiuf0731732014-11-11 09:46:41 -080084 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 Qiubf8e36c2014-12-03 22:59:45 -080089 const std::string& selected_interface() const { return selected_interface_; }
90
Peter Qiud9c79aa2015-11-19 15:36:00 -080091 ConfigAdaptorInterface* adaptor() const { return adaptor_.get(); }
Peter Qiuf0731732014-11-11 09:46:41 -080092
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 Qiubfd410e2015-01-09 15:14:20 -080098 static const char kHostapdConfigKeyControlInterfaceGroup[];
Peter Qiuf0731732014-11-11 09:46:41 -080099 static const char kHostapdConfigKeyDriver[];
100 static const char kHostapdConfigKeyFragmThreshold[];
Peter Qiu8e785b92014-11-24 10:01:08 -0800101 static const char kHostapdConfigKeyHTCapability[];
Peter Qiuf0731732014-11-11 09:46:41 -0800102 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 Qiu8e785b92014-11-24 10:01:08 -0800134 // 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 Qiu68303292014-12-10 10:42:13 -0800142 static const int kSsidMinLength;
143 static const int kSsidMaxLength;
144 static const int kPassphraseMinLength;
145 static const int kPassphraseMaxLength;
146
Peter Qiuf0731732014-11-11 09:46:41 -0800147 // Append default hostapd configurations to the config file.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800148 bool AppendHostapdDefaults(Error* error, std::string* config_str);
Peter Qiuf0731732014-11-11 09:46:41 -0800149
150 // Append hardware mode related configurations to the config file.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800151 bool AppendHwMode(Error* error, std::string* config_str);
Peter Qiuf0731732014-11-11 09:46:41 -0800152
153 // Determine/append interface configuration to the config file.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800154 bool AppendInterface(Error* error, std::string* config_str);
Peter Qiuf0731732014-11-11 09:46:41 -0800155
156 // Append security related configurations to the config file.
Peter Qiud9c79aa2015-11-19 15:36:00 -0800157 bool AppendSecurityMode(Error* error, std::string* config_str);
Peter Qiuf0731732014-11-11 09:46:41 -0800158
Peter Qiufb39ba42014-11-21 09:09:59 -0800159 Manager* manager_;
Peter Qiuf0731732014-11-11 09:46:41 -0800160 std::string control_interface_;
Peter Qiubf8e36c2014-12-03 22:59:45 -0800161 // Interface selected for hostapd.
162 std::string selected_interface_;
Peter Qiufb39ba42014-11-21 09:09:59 -0800163 scoped_refptr<Device> device_;
Peter Qiuf0731732014-11-11 09:46:41 -0800164
Peter Qiud9c79aa2015-11-19 15:36:00 -0800165 std::unique_ptr<ConfigAdaptorInterface> adaptor_;
166
Peter Qiuf0731732014-11-11 09:46:41 -0800167 DISALLOW_COPY_AND_ASSIGN(Config);
168};
169
170} // namespace apmanager
171
172#endif // APMANAGER_CONFIG_H_