blob: 44f2c30cf1591ada5864d5a2e91f1438aa8fa648 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Johan Euphrosine3523fdd2015-10-14 20:46:05 -07005#include "examples/provider/file_config_store.h"
Vitaly Buka17b0a8a2015-08-31 19:12:35 -07006
7#include <sys/stat.h>
8#include <sys/utsname.h>
9
10#include <fstream>
11#include <map>
12#include <string>
13#include <vector>
14
15namespace weave {
16namespace examples {
17
18const char kSettingsDir[] = "/var/lib/weave/";
19const char kSettingsPath[] = "/var/lib/weave/weave_settings.json";
Vitaly Buka1496d542015-10-01 13:05:57 -070020const char kCategory[] = "example";
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070021
Vitaly Buka41a90d62015-09-29 16:58:39 -070022FileConfigStore::FileConfigStore(bool disable_security)
23 : disable_security_{disable_security} {}
24
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070025bool 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 Bukaa05eadb2015-09-29 16:38:24 -070041 settings->client_id = "58855907228.apps.googleusercontent.com";
42 settings->client_secret = "eHSAREAHrIqPsHBxCE9zPPBi";
43 settings->api_key = "AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA";
Vitaly Buka41a90d62015-09-29 16:58:39 -070044
45 settings->disable_security = disable_security_;
Vitaly Buka17b0a8a2015-08-31 19:12:35 -070046 return true;
47}
48
49std::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
56void 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 Buka17b0a8a2015-08-31 19:12:35 -070063} // namespace examples
64} // namespace weave