Home | History | Annotate | Download | only in apmanager
      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 //
     16 
     17 #ifndef APMANAGER_CONFIG_H_
     18 #define APMANAGER_CONFIG_H_
     19 
     20 #include <memory>
     21 #include <string>
     22 
     23 #include <base/macros.h>
     24 #include <base/memory/ref_counted.h>
     25 #include <brillo/errors/error.h>
     26 
     27 #include "apmanager/config_adaptor_interface.h"
     28 
     29 namespace apmanager {
     30 
     31 class Error;
     32 class Device;
     33 class Manager;
     34 
     35 class Config {
     36  public:
     37   Config(Manager* manager, int service_identifier);
     38   virtual ~Config();
     39 
     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);
     46 
     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 
     51   // Generate a config file string for a hostapd instance. Populate
     52   // |error| when encounter invalid configuration. Return true if success,
     53   // false otherwise.
     54   virtual bool GenerateConfigFile(Error* error, std::string* config_str);
     55 
     56   // Claim and release the device needed for this configuration.
     57   virtual bool ClaimDevice();
     58   virtual bool ReleaseDevice();
     59 
     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 
     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 
     89   const std::string& selected_interface() const { return selected_interface_; }
     90 
     91   ConfigAdaptorInterface* adaptor() const { return adaptor_.get(); }
     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[];
     98   static const char kHostapdConfigKeyControlInterfaceGroup[];
     99   static const char kHostapdConfigKeyDriver[];
    100   static const char kHostapdConfigKeyFragmThreshold[];
    101   static const char kHostapdConfigKeyHTCapability[];
    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 
    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 
    142   static const int kSsidMinLength;
    143   static const int kSsidMaxLength;
    144   static const int kPassphraseMinLength;
    145   static const int kPassphraseMaxLength;
    146 
    147   // Append default hostapd configurations to the config file.
    148   bool AppendHostapdDefaults(Error* error, std::string* config_str);
    149 
    150   // Append hardware mode related configurations to the config file.
    151   bool AppendHwMode(Error* error, std::string* config_str);
    152 
    153   // Determine/append interface configuration to the config file.
    154   bool AppendInterface(Error* error, std::string* config_str);
    155 
    156   // Append security related configurations to the config file.
    157   bool AppendSecurityMode(Error* error, std::string* config_str);
    158 
    159   Manager* manager_;
    160   std::string control_interface_;
    161   // Interface selected for hostapd.
    162   std::string selected_interface_;
    163   scoped_refptr<Device> device_;
    164 
    165   std::unique_ptr<ConfigAdaptorInterface> adaptor_;
    166 
    167   DISALLOW_COPY_AND_ASSIGN(Config);
    168 };
    169 
    170 }  // namespace apmanager
    171 
    172 #endif  // APMANAGER_CONFIG_H_
    173