blob: 0e92010da3da845e7dc5d32aafb0227b8c0dec76 [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>
Paul Stewart82236532013-12-10 15:33:11 -080012#include <base/logging.h>
Paul Stewart1062d9d2012-04-27 10:42:27 -070013
14#include "shill/ipconfig.h"
15#include "shill/key_value_store.h"
16#include "shill/property_store.h"
17
18namespace shill {
19class StoreInterface;
20
21// Holder for static IP parameters. Includes methods for reading and
22// displaying values over a control API, methods for loading and
23// storing this to a persistent store, as well as applying these
24// parameters to an IPConfig object.
25class StaticIPParameters {
26 public:
27 StaticIPParameters();
28 virtual ~StaticIPParameters();
29
30 // Take a property store and add static IP parameters to them.
31 void PlumbPropertyStore(PropertyStore *store);
32
33 // Load static IP parameters from a persistent store with id |storage_id|.
34 void Load(StoreInterface *storage, const std::string &storage_id);
35
36 // Save static IP parameters to a persistent store with id |storage_id|.
37 void Save(StoreInterface *storage, const std::string &storage_id);
38
Paul Stewartdef189e2012-08-02 20:12:09 -070039 // Apply static IP parameters to an IPConfig properties object, and save
40 // their original values.
41 void ApplyTo(IPConfig::Properties *props);
42
Paul Stewart82236532013-12-10 15:33:11 -080043 // Restore IP parameters from |saved_args_| to |props|, then clear
44 // |saved_args_|.
45 void RestoreTo(IPConfig::Properties *props);
46
Paul Stewartdef189e2012-08-02 20:12:09 -070047 // Remove any saved parameters from a previous call to ApplyTo().
48 void ClearSavedParameters();
Paul Stewart1062d9d2012-04-27 10:42:27 -070049
50 // Return whether configuration parameters contain an address property.
51 bool ContainsAddress() const;
52
53 private:
54 friend class StaticIPParametersTest;
mukesh agrawalcc0fded2012-05-09 13:40:58 -070055 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic);
Paul Stewart82236532013-12-10 15:33:11 -080056 FRIEND_TEST(StaticIpParametersTest, SavedParameters);
Paul Stewart1062d9d2012-04-27 10:42:27 -070057
58 struct Property {
59 enum Type {
60 kTypeInt32,
61 kTypeString,
62 // Properties of type "Strings" are stored as a comma-separated list
63 // in the control interface and in the profile, but are stored as a
64 // vector of strings in the IPConfig properties.
65 kTypeStrings
66 };
67
68 const char *name;
69 Type type;
70 };
71
72 static const char kConfigKeyPrefix[];
Paul Stewartdef189e2012-08-02 20:12:09 -070073 static const char kSavedConfigKeyPrefix[];
Paul Stewart1062d9d2012-04-27 10:42:27 -070074 static const Property kProperties[];
75
76 // These functions try to retrieve the argument |property| out of the
77 // KeyValueStore in |args_|. If that value exists, overwrite |value_out|
Paul Stewartdef189e2012-08-02 20:12:09 -070078 // with its contents, and save the previous value into |saved_args_|.
79 void ApplyInt(const std::string &property, int32 *value_out);
80 void ApplyString(const std::string &property, std::string *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070081 void ApplyStrings(const std::string &property,
Paul Stewartdef189e2012-08-02 20:12:09 -070082 std::vector<std::string> *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070083
84 void ClearMappedProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070085 void ClearMappedSavedProperty(const size_t &index, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070086 int32 GetMappedInt32Property(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070087 int32 GetMappedSavedInt32Property(const size_t &index, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070088 std::string GetMappedStringProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070089 std::string GetMappedSavedStringProperty(const size_t &index, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070090 bool SetMappedInt32Property(
Paul Stewart1062d9d2012-04-27 10:42:27 -070091 const size_t &index, const int32 &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070092 bool SetMappedSavedInt32Property(
Paul Stewartdef189e2012-08-02 20:12:09 -070093 const size_t &index, const int32 &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070094 bool SetMappedStringProperty(
Paul Stewart1062d9d2012-04-27 10:42:27 -070095 const size_t &index, const std::string &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070096 bool SetMappedSavedStringProperty(
Paul Stewartdef189e2012-08-02 20:12:09 -070097 const size_t &index, const std::string &value, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070098
99 KeyValueStore args_;
Paul Stewartdef189e2012-08-02 20:12:09 -0700100 KeyValueStore saved_args_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700101
102 DISALLOW_COPY_AND_ASSIGN(StaticIPParameters);
103};
104
105} // namespace shill
106
107#endif // SHILL_STATIC_IP_PARAMETERS_