summaryrefslogtreecommitdiff
path: root/config.h
blob: bedd92dd53c9ac85b1908c2b75e46983ac46d970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef APMANAGER_CONFIG_H_
#define APMANAGER_CONFIG_H_

#include <memory>
#include <string>

#include <base/macros.h>
#include <base/memory/ref_counted.h>
#include <brillo/errors/error.h>

#include "apmanager/config_adaptor_interface.h"

namespace apmanager {

class Error;
class Device;
class Manager;

class Config {
 public:
  Config(Manager* manager, int service_identifier);
  virtual ~Config();

  bool ValidateSsid(Error* error, const std::string& value);
  bool ValidateSecurityMode(Error* error, const std::string& value);
  bool ValidatePassphrase(Error* error, const std::string& value);
  bool ValidateHwMode(Error* error, const std::string& value);
  bool ValidateOperationMode(Error* error, const std::string& value);
  bool ValidateChannel(Error* error, const uint16_t& value);

  // Calculate the frequency based on the given |channel|. Return true and set
  // the output |frequency| if is valid channel, false otherwise.
  static bool GetFrequencyFromChannel(uint16_t channel, uint32_t* freq);

  // Generate a config file string for a hostapd instance. Populate
  // |error| when encounter invalid configuration. Return true if success,
  // false otherwise.
  virtual bool GenerateConfigFile(Error* error, std::string* config_str);

  // Claim and release the device needed for this configuration.
  virtual bool ClaimDevice();
  virtual bool ReleaseDevice();

  // Getter and setter for configuration properties.
  void SetSsid(const std::string& ssid);
  std::string GetSsid() const;
  void SetInterfaceName(const std::string& interface_name);
  std::string GetInterfaceName() const;
  void SetSecurityMode(const std::string& security_mode);
  std::string GetSecurityMode() const;
  void SetPassphrase(const std::string& passphrase);
  std::string GetPassphrase() const;
  void SetHwMode(const std::string& hw_mode);
  std::string GetHwMode() const;
  void SetOperationMode(const std::string& op_mode);
  std::string GetOperationMode() const;
  void SetChannel(uint16_t channel);
  uint16_t GetChannel() const;
  void SetHiddenNetwork(bool hidden);
  bool GetHiddenNetwork() const;
  void SetBridgeInterface(const std::string& interface_name);
  std::string GetBridgeInterface() const;
  void SetServerAddressIndex(uint16_t);
  uint16_t GetServerAddressIndex() const;
  void SetFullDeviceControl(bool full_control);
  bool GetFullDeviceControl() const;

  const std::string& control_interface() const { return control_interface_; }
  void set_control_interface(const std::string& control_interface) {
    control_interface_ = control_interface;
  }

  const std::string& selected_interface() const { return selected_interface_; }

  ConfigAdaptorInterface* adaptor() const { return adaptor_.get(); }

 private:
  // Keys used in hostapd config file.
  static const char kHostapdConfigKeyBridgeInterface[];
  static const char kHostapdConfigKeyChannel[];
  static const char kHostapdConfigKeyControlInterface[];
  static const char kHostapdConfigKeyControlInterfaceGroup[];
  static const char kHostapdConfigKeyDriver[];
  static const char kHostapdConfigKeyFragmThreshold[];
  static const char kHostapdConfigKeyHTCapability[];
  static const char kHostapdConfigKeyHwMode[];
  static const char kHostapdConfigKeyIeee80211ac[];
  static const char kHostapdConfigKeyIeee80211n[];
  static const char kHostapdConfigKeyIgnoreBroadcastSsid[];
  static const char kHostapdConfigKeyInterface[];
  static const char kHostapdConfigKeyRsnPairwise[];
  static const char kHostapdConfigKeyRtsThreshold[];
  static const char kHostapdConfigKeySsid[];
  static const char kHostapdConfigKeyWepDefaultKey[];
  static const char kHostapdConfigKeyWepKey0[];
  static const char kHostapdConfigKeyWpa[];
  static const char kHostapdConfigKeyWpaKeyMgmt[];
  static const char kHostapdConfigKeyWpaPassphrase[];

  // Hardware mode value for hostapd config file.
  static const char kHostapdHwMode80211a[];
  static const char kHostapdHwMode80211b[];
  static const char kHostapdHwMode80211g[];

  // Default hostapd configuration values. User will not be able to configure
  // these.
  static const char kHostapdDefaultDriver[];
  static const char kHostapdDefaultRsnPairwise[];
  static const char kHostapdDefaultWpaKeyMgmt[];
  static const int kHostapdDefaultFragmThreshold;
  static const int kHostapdDefaultRtsThreshold;

  // Default config property values.
  static const uint16_t kPropertyDefaultChannel;;
  static const bool kPropertyDefaultHiddenNetwork;
  static const uint16_t kPropertyDefaultServerAddressIndex;

  // Constants use for converting channel to frequency.
  static const uint16_t kBand24GHzChannelLow;
  static const uint16_t kBand24GHzChannelHigh;
  static const uint32_t kBand24GHzBaseFrequency;
  static const uint16_t kBand5GHzChannelLow;
  static const uint16_t kBand5GHzChannelHigh;
  static const uint16_t kBand5GHzBaseFrequency;

  static const int kSsidMinLength;
  static const int kSsidMaxLength;
  static const int kPassphraseMinLength;
  static const int kPassphraseMaxLength;

  // Append default hostapd configurations to the config file.
  bool AppendHostapdDefaults(Error* error, std::string* config_str);

  // Append hardware mode related configurations to the config file.
  bool AppendHwMode(Error* error, std::string* config_str);

  // Determine/append interface configuration to the config file.
  bool AppendInterface(Error* error, std::string* config_str);

  // Append security related configurations to the config file.
  bool AppendSecurityMode(Error* error, std::string* config_str);

  Manager* manager_;
  std::string control_interface_;
  // Interface selected for hostapd.
  std::string selected_interface_;
  scoped_refptr<Device> device_;

  std::unique_ptr<ConfigAdaptorInterface> adaptor_;

  DISALLOW_COPY_AND_ASSIGN(Config);
};

}  // namespace apmanager

#endif  // APMANAGER_CONFIG_H_