Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Johan Euphrosine | 3523fdd | 2015-10-14 20:46:05 -0700 | [diff] [blame] | 5 | #include "examples/provider/file_config_store.h" |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 6 | |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/utsname.h> |
| 9 | |
| 10 | #include <fstream> |
| 11 | #include <map> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace weave { |
| 16 | namespace examples { |
| 17 | |
| 18 | const char kSettingsDir[] = "/var/lib/weave/"; |
| 19 | const char kSettingsPath[] = "/var/lib/weave/weave_settings.json"; |
Vitaly Buka | 1496d54 | 2015-10-01 13:05:57 -0700 | [diff] [blame] | 20 | const char kCategory[] = "example"; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 21 | |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 22 | FileConfigStore::FileConfigStore(bool disable_security) |
| 23 | : disable_security_{disable_security} {} |
| 24 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 25 | bool FileConfigStore::LoadDefaults(Settings* settings) { |
| 26 | char host_name[HOST_NAME_MAX] = {}; |
| 27 | gethostname(host_name, HOST_NAME_MAX); |
| 28 | |
| 29 | settings->name = host_name; |
| 30 | settings->description = ""; |
| 31 | |
| 32 | utsname uname_data; |
| 33 | uname(&uname_data); |
| 34 | |
| 35 | settings->firmware_version = uname_data.sysname; |
| 36 | settings->oem_name = "Unknown"; |
| 37 | settings->model_name = "Unknown"; |
| 38 | settings->model_id = "AAAAA"; |
| 39 | settings->pairing_modes = {PairingType::kEmbeddedCode}; |
| 40 | settings->embedded_code = "0000"; |
Vitaly Buka | a05eadb | 2015-09-29 16:38:24 -0700 | [diff] [blame] | 41 | settings->client_id = "58855907228.apps.googleusercontent.com"; |
| 42 | settings->client_secret = "eHSAREAHrIqPsHBxCE9zPPBi"; |
| 43 | settings->api_key = "AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA"; |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 44 | |
| 45 | settings->disable_security = disable_security_; |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | |
| 49 | std::string FileConfigStore::LoadSettings() { |
| 50 | LOG(INFO) << "Loading settings from " << kSettingsPath; |
| 51 | std::ifstream str(kSettingsPath); |
| 52 | return std::string(std::istreambuf_iterator<char>(str), |
| 53 | std::istreambuf_iterator<char>()); |
| 54 | } |
| 55 | |
| 56 | void FileConfigStore::SaveSettings(const std::string& settings) { |
| 57 | CHECK(mkdir(kSettingsDir, S_IRWXU) == 0 || errno == EEXIST); |
| 58 | LOG(INFO) << "Saving settings to " << kSettingsPath; |
| 59 | std::ofstream str(kSettingsPath); |
| 60 | str << settings; |
| 61 | } |
| 62 | |
Vitaly Buka | 17b0a8a | 2015-08-31 19:12:35 -0700 | [diff] [blame] | 63 | } // namespace examples |
| 64 | } // namespace weave |