blob: 5a90988cb39edb2614d400e283c4f10ad521711e [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(
Paul Westbrook321dae62015-11-16 09:31:35 -080073 opts.disable_security_, opts.model_id_)},
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070074 task_runner_{new weave::examples::EventTaskRunner},
75 http_client_{new weave::examples::CurlHttpClient(task_runner_.get())},
76 network_{new weave::examples::EventNetworkImpl(task_runner_.get())},
77 bluetooth_{new weave::examples::BluetoothImpl} {
78 if (!opts.disable_privet_) {
79 network_->SetSimulateOffline(opts.force_bootstrapping_);
80
81 dns_sd_.reset(new weave::examples::AvahiClient);
82 http_server_.reset(
83 new weave::examples::HttpServerImpl{task_runner_.get()});
84 if (weave::examples::WifiImpl::HasWifiCapability())
85 wifi_.reset(
86 new weave::examples::WifiImpl{task_runner_.get(), network_.get()});
87 }
88 device_ = weave::Device::Create(config_store_.get(), task_runner_.get(),
89 http_client_.get(), network_.get(),
90 dns_sd_.get(), http_server_.get(),
91 wifi_.get(), bluetooth_.get());
92
93 if (!opts.registration_ticket_.empty()) {
94 device_->Register(opts.registration_ticket_,
95 base::Bind(&OnRegisterDeviceDone, device_.get()));
96 }
97 }
98
99 void Run() { task_runner_->Run(); }
100
101 weave::Device* GetDevice() const { return device_.get(); }
102
103 weave::examples::EventTaskRunner* GetTaskRunner() const {
104 return task_runner_.get();
105 }
106
107 private:
108 static void OnRegisterDeviceDone(weave::Device* device,
109 weave::ErrorPtr error) {
110 if (error)
111 LOG(ERROR) << "Fail to register device: " << error->GetMessage();
112 else
113 LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
114 }
115
116 std::unique_ptr<weave::examples::FileConfigStore> config_store_;
117 std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
118 std::unique_ptr<weave::examples::CurlHttpClient> http_client_;
119 std::unique_ptr<weave::examples::EventNetworkImpl> network_;
120 std::unique_ptr<weave::examples::BluetoothImpl> bluetooth_;
121 std::unique_ptr<weave::examples::AvahiClient> dns_sd_;
122 std::unique_ptr<weave::examples::HttpServerImpl> http_server_;
123 std::unique_ptr<weave::examples::WifiImpl> wifi_;
124 std::unique_ptr<weave::Device> device_;
125};