Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 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 SHILL_STATIC_IP_PARAMETERS_H_
     18 #define SHILL_STATIC_IP_PARAMETERS_H_
     19 
     20 #include <string>
     21 #include <vector>
     22 
     23 #include <base/memory/ref_counted.h>
     24 #include <base/logging.h>
     25 
     26 #include "shill/ipconfig.h"
     27 #include "shill/key_value_store.h"
     28 #include "shill/property_store.h"
     29 
     30 namespace shill {
     31 class StoreInterface;
     32 
     33 // Holder for static IP parameters.  Includes methods for reading and
     34 // displaying values over a control API, methods for loading and
     35 // storing this to a persistent store, as well as applying these
     36 // parameters to an IPConfig object.
     37 class StaticIPParameters {
     38  public:
     39   StaticIPParameters();
     40   virtual ~StaticIPParameters();
     41 
     42   // Take a property store and add static IP parameters to them.
     43   void PlumbPropertyStore(PropertyStore* store);
     44 
     45   // Load static IP parameters from a persistent store with id |storage_id|.
     46   void Load(StoreInterface* storage, const std::string& storage_id);
     47 
     48   // Save static IP parameters to a persistent store with id |storage_id|.
     49   void Save(StoreInterface* storage, const std::string& storage_id);
     50 
     51   // Apply static IP parameters to an IPConfig properties object, and save
     52   // their original values.
     53   void ApplyTo(IPConfig::Properties* props);
     54 
     55   // Restore IP parameters from |saved_args_| to |props|, then clear
     56   // |saved_args_|.
     57   void RestoreTo(IPConfig::Properties* props);
     58 
     59   // Remove any saved parameters from a previous call to ApplyTo().
     60   void ClearSavedParameters();
     61 
     62   // Return whether configuration parameters contain an address property.
     63   bool ContainsAddress() const;
     64 
     65   // Return whether configuration parameters contain a namerservers property.
     66   bool ContainsNameServers() const;
     67 
     68  private:
     69   friend class StaticIPParametersTest;
     70   FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic);
     71   FRIEND_TEST(DeviceTest, PrependWithStaticConfiguration);
     72   FRIEND_TEST(StaticIpParametersTest, SavedParameters);
     73   FRIEND_TEST(StaticIpParametersTest, SavedParametersDict);
     74 
     75   struct Property {
     76     enum Type {
     77       kTypeInt32,
     78       kTypeString,
     79       // Properties of type "Strings" are stored as a comma-separated list
     80       // in the control interface and in the profile, but are stored as a
     81       // vector of strings in the IPConfig properties.
     82       kTypeStrings
     83     };
     84 
     85     const char* name;
     86     Type type;
     87   };
     88 
     89   static const char kConfigKeyPrefix[];
     90   static const char kSavedConfigKeyPrefix[];
     91   static const Property kProperties[];
     92 
     93   // These functions try to retrieve the argument |property| out of the
     94   // KeyValueStore in |args_|.  If that value exists, overwrite |value_out|
     95   // with its contents, and save the previous value into |saved_args_|.
     96   void ApplyInt(const std::string& property, int32_t* value_out);
     97   void ApplyString(const std::string& property, std::string* value_out);
     98   void ApplyStrings(const std::string& property,
     99                     std::vector<std::string>* value_out);
    100 
    101   void ClearMappedProperty(const size_t& index, Error* error);
    102   void ClearMappedSavedProperty(const size_t& index, Error* error);
    103   int32_t GetMappedInt32Property(const size_t& index, Error* error);
    104   int32_t GetMappedSavedInt32Property(const size_t& index, Error* error);
    105   std::string GetMappedStringProperty(const size_t& index, Error* error);
    106   std::string GetMappedSavedStringProperty(const size_t& index, Error* error);
    107   std::string GetMappedStringsProperty(const size_t& index, Error* error);
    108   std::string GetMappedSavedStringsProperty(const size_t& index, Error* error);
    109   bool SetMappedInt32Property(
    110       const size_t& index, const int32_t& value, Error* error);
    111   bool SetMappedSavedInt32Property(
    112       const size_t& index, const int32_t& value, Error* error);
    113   bool SetMappedStringProperty(
    114       const size_t& index, const std::string& value, Error* error);
    115   bool SetMappedSavedStringProperty(
    116       const size_t& index, const std::string& value, Error* error);
    117   bool SetMappedStringsProperty(
    118       const size_t& index, const std::string& value, Error* error);
    119   bool SetMappedSavedStringsProperty(
    120       const size_t& index, const std::string& value, Error* error);
    121 
    122   KeyValueStore GetSavedIPConfig(Error* error);
    123   KeyValueStore GetStaticIPConfig(Error* error);
    124   bool SetStaticIPConfig(const KeyValueStore& value, Error* error);
    125 
    126   KeyValueStore args_;
    127   KeyValueStore saved_args_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(StaticIPParameters);
    130 };
    131 
    132 }  // namespace shill
    133 
    134 #endif  // SHILL_STATIC_IP_PARAMETERS_H_
    135