aboutsummaryrefslogtreecommitdiff
path: root/include/weave/provider/config_store.h
blob: 128ecccd0a4f27447689bd72cd31f11aee6b91c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_
#define LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_

#include <map>
#include <set>
#include <string>
#include <vector>

#include <base/callback.h>
#include <base/time/time.h>
#include <weave/enum_to_string.h>
#include <weave/error.h>
#include <weave/settings.h>

namespace weave {
namespace provider {

// This interface should be implemented by the user of libweave and
// provided during device creation in Device::Create(...)
// libweave will use this interface to get default settings and load / save
// settings to a persistent storage.
//
// Implementation of the LoadDefaults(...) method may load settings from
// a file or just hardcode defaults for this device.
// For example:
//   bool FileConfigStore::LoadDefaults(Settings* settings) {
//     settings->name = "My device";
//     settings->pairing_modes.insert(kPinCode);
//     // set all other required settings, see include/weave/settings.h
//     return true;
//   }
//
// Implementation of LoadSettings() method should load previously
// stored settings from the persistent storage (file, flash, etc).
// For example:
//   std::string FileConfigStore::LoadSettings(const std::string& name) {
//     std::ifstream str("/var/lib/weave/weave_" + name + ".json");
//     return std::string(std::istreambuf_iterator<char>(str),
//                        std::istreambuf_iterator<char>());
//   }
// If data stored encrypted (highly recommended), LoadSettings()
// implementation should decrypt the data before returning it to libweave.
//
// Implementation of SaveSettings(...) method should store data in the
// persistent storage (file, flash, etc).
// For example:
//   void FileConfigStore::SaveSettings(const std::string& name,
//                                      const std::string& settings,
//                                      const DoneCallback& callback) {
//     std::ofstream str("/var/lib/weave/weave_" + name + ".json");
//     str << settings;
//     if (!callback.is_null())
//       task_runner_->PostDelayedTask(FROM_HERE, base::Bind(callback, nullptr),
//                                     {});
//   }
// It is highly recommended to protected data using encryption with
// hardware backed key.
//
// See libweave/examples/provider/file_config_store.cc for a complete
// example.

// Interface with methods to read/write libweave settings, device state and
// commands definitions.
class ConfigStore {
 public:
  // Returns default settings. This settings used for a new device or after
  // a factory reset.
  virtual bool LoadDefaults(Settings* settings) = 0;

  // Returns settings saved by SaveSettings during last run of libweave.
  // Implementation should return data as-is without parsing or modifications.
  // |name| is the name of settings blob. Could be used as filename.
  virtual std::string LoadSettings(const std::string& name) = 0;

  // Saves settings. Implementation should save data as-is without parsing or
  // modifications. Data stored in settings can be sensitive, so it's highly
  // recommended to protect data, e.g. using encryption.
  // |name| is the name of settings blob. Could be used as filename.
  // Implementation must call or post callback
  virtual void SaveSettings(const std::string& name,
                            const std::string& settings,
                            const DoneCallback& callback) = 0;

  // Deprecated: only for migration of old configs to version with |name|.
  virtual std::string LoadSettings() = 0;

 protected:
  virtual ~ConfigStore() {}
};

}  // namespace provider
}  // namespace weave

#endif  // LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_