blob: ce3dfde051a43dbe71d1719ca0a0fda51b8c1625 [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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_STATIC_IP_PARAMETERS_H_
6#define SHILL_STATIC_IP_PARAMETERS_H_
Paul Stewart1062d9d2012-04-27 10:42:27 -07007
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);
Peter Qiu08bf5082014-09-08 15:57:12 -070057 FRIEND_TEST(StaticIpParametersTest, SavedParametersDict);
Paul Stewart1062d9d2012-04-27 10:42:27 -070058
59 struct Property {
60 enum Type {
61 kTypeInt32,
62 kTypeString,
63 // Properties of type "Strings" are stored as a comma-separated list
64 // in the control interface and in the profile, but are stored as a
65 // vector of strings in the IPConfig properties.
66 kTypeStrings
67 };
68
69 const char *name;
70 Type type;
71 };
72
73 static const char kConfigKeyPrefix[];
Paul Stewartdef189e2012-08-02 20:12:09 -070074 static const char kSavedConfigKeyPrefix[];
Paul Stewart1062d9d2012-04-27 10:42:27 -070075 static const Property kProperties[];
76
77 // These functions try to retrieve the argument |property| out of the
78 // KeyValueStore in |args_|. If that value exists, overwrite |value_out|
Paul Stewartdef189e2012-08-02 20:12:09 -070079 // with its contents, and save the previous value into |saved_args_|.
Ben Chan7fab8972014-08-10 17:14:46 -070080 void ApplyInt(const std::string &property, int32_t *value_out);
Paul Stewartdef189e2012-08-02 20:12:09 -070081 void ApplyString(const std::string &property, std::string *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070082 void ApplyStrings(const std::string &property,
Paul Stewartdef189e2012-08-02 20:12:09 -070083 std::vector<std::string> *value_out);
Paul Stewart1062d9d2012-04-27 10:42:27 -070084
85 void ClearMappedProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070086 void ClearMappedSavedProperty(const size_t &index, Error *error);
Ben Chan7fab8972014-08-10 17:14:46 -070087 int32_t GetMappedInt32Property(const size_t &index, Error *error);
88 int32_t GetMappedSavedInt32Property(const size_t &index, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -070089 std::string GetMappedStringProperty(const size_t &index, Error *error);
Paul Stewartdef189e2012-08-02 20:12:09 -070090 std::string GetMappedSavedStringProperty(const size_t &index, Error *error);
Peter Qiu08bf5082014-09-08 15:57:12 -070091 std::string GetMappedStringsProperty(const size_t &index, Error *error);
92 std::string GetMappedSavedStringsProperty(const size_t &index, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070093 bool SetMappedInt32Property(
Ben Chan7fab8972014-08-10 17:14:46 -070094 const size_t &index, const int32_t &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070095 bool SetMappedSavedInt32Property(
Ben Chan7fab8972014-08-10 17:14:46 -070096 const size_t &index, const int32_t &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070097 bool SetMappedStringProperty(
Paul Stewart1062d9d2012-04-27 10:42:27 -070098 const size_t &index, const std::string &value, Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070099 bool SetMappedSavedStringProperty(
Paul Stewartdef189e2012-08-02 20:12:09 -0700100 const size_t &index, const std::string &value, Error *error);
Peter Qiu08bf5082014-09-08 15:57:12 -0700101 bool SetMappedStringsProperty(
102 const size_t &index, const std::string &value, Error *error);
103 bool SetMappedSavedStringsProperty(
104 const size_t &index, const std::string &value, Error *error);
105
106 KeyValueStore GetSavedIPConfig(Error *error);
107 KeyValueStore GetStaticIPConfig(Error *error);
108 bool SetStaticIPConfig(const KeyValueStore &value, Error *error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700109
110 KeyValueStore args_;
Paul Stewartdef189e2012-08-02 20:12:09 -0700111 KeyValueStore saved_args_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700112
113 DISALLOW_COPY_AND_ASSIGN(StaticIPParameters);
114};
115
116} // namespace shill
117
Ben Chanc45688b2014-07-02 23:50:45 -0700118#endif // SHILL_STATIC_IP_PARAMETERS_H_