blob: 4cccff32aed59003fc2cef86c2d470f849e6fbc5 [file] [log] [blame]
Johan Euphrosine3fb474e2015-10-29 15:23:53 -07001// 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#include <weave/device.h>
6#include <weave/error.h>
7
8#include <base/bind.h>
9
10#include "examples/provider/avahi_client.h"
11#include "examples/provider/bluez_client.h"
12#include "examples/provider/curl_http_client.h"
13#include "examples/provider/event_http_server.h"
14#include "examples/provider/event_network.h"
15#include "examples/provider/event_task_runner.h"
16#include "examples/provider/file_config_store.h"
17#include "examples/provider/wifi_manager.h"
18
19class Daemon {
20 public:
21 struct Options {
Paul Westbrook321dae62015-11-16 09:31:35 -080022 bool force_bootstrapping_{false};
23 bool disable_security_{false};
24 bool disable_privet_{false};
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070025 std::string registration_ticket_;
Paul Westbrook321dae62015-11-16 09:31:35 -080026 std::string model_id_{"AAAAA"};
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070027
28 static void ShowUsage(const std::string& name) {
29 LOG(ERROR) << "\nUsage: " << name << " <option(s)>"
30 << "\nOptions:\n"
31 << "\t-h,--help Show this help message\n"
32 << "\t--v=LEVEL Logging level\n"
33 << "\t-b,--bootstrapping Force WiFi bootstrapping\n"
34 << "\t-d,--disable_security Disable privet security\n"
35 << "\t--registration_ticket=TICKET Register device with the "
36 "given ticket\n"
37 << "\t--disable_privet Disable local privet\n";
38 }
39
40 bool Parse(int argc, char** argv) {
41 for (int i = 1; i < argc; ++i) {
42 std::string arg = argv[i];
43 if (arg == "-h" || arg == "--help") {
44 return false;
45 } else if (arg == "-b" || arg == "--bootstrapping") {
46 force_bootstrapping_ = true;
47 } else if (arg == "-d" || arg == "--disable_security") {
48 disable_security_ = true;
49 } else if (arg == "--disable_privet") {
50 disable_privet_ = true;
51 } else if (arg.find("--registration_ticket") != std::string::npos) {
52 auto pos = arg.find("=");
53 if (pos == std::string::npos) {
54 return false;
55 }
56 registration_ticket_ = arg.substr(pos + 1);
57 } else if (arg.find("--v") != std::string::npos) {
58 auto pos = arg.find("=");
59 if (pos == std::string::npos) {
60 return false;
61 }
62 logging::SetMinLogLevel(-std::stoi(arg.substr(pos + 1)));
63 } else {
64 return false;
65 }
66 }
67 return true;
68 }
69 };
70
71 Daemon(const Options& opts)
72 : config_store_{new weave::examples::FileConfigStore(
Vitaly Buka34668e72015-12-15 14:46:47 -080073 opts.disable_security_,
74 opts.model_id_)},
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070075 task_runner_{new weave::examples::EventTaskRunner},
76 http_client_{new weave::examples::CurlHttpClient(task_runner_.get())},
77 network_{new weave::examples::EventNetworkImpl(task_runner_.get())},
78 bluetooth_{new weave::examples::BluetoothImpl} {
79 if (!opts.disable_privet_) {
80 network_->SetSimulateOffline(opts.force_bootstrapping_);
81
82 dns_sd_.reset(new weave::examples::AvahiClient);
83 http_server_.reset(
84 new weave::examples::HttpServerImpl{task_runner_.get()});
85 if (weave::examples::WifiImpl::HasWifiCapability())
86 wifi_.reset(
87 new weave::examples::WifiImpl{task_runner_.get(), network_.get()});
88 }
89 device_ = weave::Device::Create(config_store_.get(), task_runner_.get(),
90 http_client_.get(), network_.get(),
91 dns_sd_.get(), http_server_.get(),
92 wifi_.get(), bluetooth_.get());
93
94 if (!opts.registration_ticket_.empty()) {
95 device_->Register(opts.registration_ticket_,
96 base::Bind(&OnRegisterDeviceDone, device_.get()));
97 }
98 }
99
100 void Run() { task_runner_->Run(); }
101
102 weave::Device* GetDevice() const { return device_.get(); }
103
104 weave::examples::EventTaskRunner* GetTaskRunner() const {
105 return task_runner_.get();
106 }
107
108 private:
109 static void OnRegisterDeviceDone(weave::Device* device,
110 weave::ErrorPtr error) {
111 if (error)
112 LOG(ERROR) << "Fail to register device: " << error->GetMessage();
113 else
114 LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
115 }
116
117 std::unique_ptr<weave::examples::FileConfigStore> config_store_;
118 std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
119 std::unique_ptr<weave::examples::CurlHttpClient> http_client_;
120 std::unique_ptr<weave::examples::EventNetworkImpl> network_;
121 std::unique_ptr<weave::examples::BluetoothImpl> bluetooth_;
122 std::unique_ptr<weave::examples::AvahiClient> dns_sd_;
123 std::unique_ptr<weave::examples::HttpServerImpl> http_server_;
124 std::unique_ptr<weave::examples::WifiImpl> wifi_;
125 std::unique_ptr<weave::Device> device_;
126};