blob: e18f869c36004d18603836473c21c0c85fe1b8e1 [file] [log] [blame]
Paul Stewart1062d9d2012-04-27 10:42:27 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_STATIC_IP_PARAMETERS_
6#define SHILL_STATIC_IP_PARAMETERS_
7
8#include <string>
9#include <vector>
10
11#include <base/memory/ref_counted.h>
12
13#include "shill/ipconfig.h"
14#include "shill/key_value_store.h"
15#include "shill/property_store.h"
16
17namespace shill {
18class StoreInterface;
19
20// Holder for static IP parameters. Includes methods for reading and
21// displaying values over a control API, methods for loading and
22// storing this to a persistent store, as well as applying these
23// parameters to an IPConfig object.
24class StaticIPParameters {
25 public:
26 StaticIPParameters();
27 virtual ~StaticIPParameters();
28
29 // Take a property store and add static IP parameters to them.
30 void PlumbPropertyStore(PropertyStore *store);
31
32 // Load static IP parameters from a persistent store with id |storage_id|.
33 void Load(StoreInterface *storage, const std::string &storage_id);
34
35 // Save static IP parameters to a persistent store with id |storage_id|.
36 void Save(StoreInterface *storage, const std::string &storage_id);
37
Paul Stewartdef189e2012-08-02 20:12:09 -070038 // Apply static IP parameters to an IPConfig properties object, and save
39 // their original values.
40 void ApplyTo(IPConfig::Properties *props);
41
42 // Remove any saved parameters from a previous call to ApplyTo().
43 void ClearSavedParameters();
Paul Stewart1062d9d2012-04-27 10:42:27 -070044
45 // Return whether configuration parameters contain an address property.
46 bool ContainsAddress() const;
47
48 private:
49 friend class StaticIPParametersTest;
mukesh agrawalcc0fded2012-05-09 13:40:58 -070050 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic);
Paul Stewart1062d9d2012-04-27 10:42:27 -070051
52 struct Property {
53 enum Type {
54 kTypeInt32,
55 kTypeString,
56 // Properties of type "Strings" are stored as a comma-separated list
57 // in the control interface and in the profile, but are stored as a
58 // vector of strings in the IPConfig properties.
59 kTypeStrings
60 };
61
62 const char *name;
63 Type type;
64 };
65
66 static const char kConfigKeyPrefix[];
Paul Stewartdef189e2012-08-02 20:12:09 -070067 static const char kSavedConfigKeyPrefix[];
Paul Stewart1062d9d2012-04-27 10:42:27 -070068 static const Property kProperties[];
69
70 // These functions try to retrieve the argument |property| out of the
71 // KeyValueStore in |args_|. If that value exists, overwrite |value_out|
Paul Stewartdef189e2012-08-02 20:12:09 -070072 // with its contents, and save the previous value into |saved_args_|.
73 void ApplyInt(const std::string &property, int32 *value_out);
74 void ApplyString(const std::string &property, std::string *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070075 void ApplyStrings(const std::string &property,
Paul Stewartdef189e2012-08-02 20:12:09 -070076 std::vector<std::string> *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070077
78 void ClearMappedProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070079 void ClearMappedSavedProperty(const size_t &index, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070080 int32 GetMappedInt32Property(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070081 int32 GetMappedSavedInt32Property(const size_t &index, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070082 std::string GetMappedStringProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070083 std::string GetMappedSavedStringProperty(const size_t &index, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070084 bool SetMappedInt32Property(
Paul Stewart1062d9d2012-04-27 10:42:27 -070085 const size_t &index, const int32 &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070086 bool SetMappedSavedInt32Property(
Paul Stewartdef189e2012-08-02 20:12:09 -070087 const size_t &index, const int32 &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070088 bool SetMappedStringProperty(
Paul Stewart1062d9d2012-04-27 10:42:27 -070089 const size_t &index, const std::string &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070090 bool SetMappedSavedStringProperty(
Paul Stewartdef189e2012-08-02 20:12:09 -070091 const size_t &index, const std::string &value, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070092
93 KeyValueStore args_;
Paul Stewartdef189e2012-08-02 20:12:09 -070094 KeyValueStore saved_args_;
Paul Stewart1062d9d2012-04-27 10:42:27 -070095
96 DISALLOW_COPY_AND_ASSIGN(StaticIPParameters);
97};
98
99} // namespace shill
100
101#endif // SHILL_STATIC_IP_PARAMETERS_