1 // Copyright 2015 The Weave 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 LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_ 6 #define LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include <base/callback.h> 14 #include <base/time/time.h> 15 #include <weave/enum_to_string.h> 16 #include <weave/error.h> 17 #include <weave/settings.h> 18 19 namespace weave { 20 namespace provider { 21 22 // This interface should be implemented by the user of libweave and 23 // provided during device creation in Device::Create(...) 24 // libweave will use this interface to get default settings and load / save 25 // settings to a persistent storage. 26 // 27 // Implementation of the LoadDefaults(...) method may load settings from 28 // a file or just hardcode defaults for this device. 29 // For example: 30 // bool FileConfigStore::LoadDefaults(Settings* settings) { 31 // settings->name = "My device"; 32 // settings->pairing_modes.insert(kPinCode); 33 // // set all other required settings, see include/weave/settings.h 34 // return true; 35 // } 36 // 37 // Implementation of LoadSettings() method should load previously 38 // stored settings from the persistent storage (file, flash, etc). 39 // For example: 40 // std::string FileConfigStore::LoadSettings(const std::string& name) { 41 // std::ifstream str("/var/lib/weave/weave_" + name + ".json"); 42 // return std::string(std::istreambuf_iterator<char>(str), 43 // std::istreambuf_iterator<char>()); 44 // } 45 // If data stored encrypted (highly recommended), LoadSettings() 46 // implementation should decrypt the data before returning it to libweave. 47 // 48 // Implementation of SaveSettings(...) method should store data in the 49 // persistent storage (file, flash, etc). 50 // For example: 51 // void FileConfigStore::SaveSettings(const std::string& name, 52 // const std::string& settings, 53 // const DoneCallback& callback) { 54 // std::ofstream str("/var/lib/weave/weave_" + name + ".json"); 55 // str << settings; 56 // if (!callback.is_null()) 57 // task_runner_->PostDelayedTask(FROM_HERE, base::Bind(callback, nullptr), 58 // {}); 59 // } 60 // It is highly recommended to protected data using encryption with 61 // hardware backed key. 62 // 63 // See libweave/examples/provider/file_config_store.cc for a complete 64 // example. 65 66 // Interface with methods to read/write libweave settings, device state and 67 // commands definitions. 68 class ConfigStore { 69 public: 70 // Returns default settings. This settings used for a new device or after 71 // a factory reset. 72 virtual bool LoadDefaults(Settings* settings) = 0; 73 74 // Returns settings saved by SaveSettings during last run of libweave. 75 // Implementation should return data as-is without parsing or modifications. 76 // |name| is the name of settings blob. Could be used as filename. 77 virtual std::string LoadSettings(const std::string& name) = 0; 78 79 // Saves settings. Implementation should save data as-is without parsing or 80 // modifications. Data stored in settings can be sensitive, so it's highly 81 // recommended to protect data, e.g. using encryption. 82 // |name| is the name of settings blob. Could be used as filename. 83 // Implementation must call or post callback 84 virtual void SaveSettings(const std::string& name, 85 const std::string& settings, 86 const DoneCallback& callback) = 0; 87 88 // Deprecated: only for migration of old configs to version with |name|. 89 virtual std::string LoadSettings() = 0; 90 91 protected: 92 virtual ~ConfigStore() {} 93 }; 94 95 } // namespace provider 96 } // namespace weave 97 98 #endif // LIBWEAVE_INCLUDE_WEAVE_PROVIDER_CONFIG_STORE_H_ 99