blob: 0b3c25afccd540e339fe4cc1f295f17bed933d77 [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 {
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080022 bool force_bootstrapping{false};
23 bool disable_privet{false};
24 std::string registration_ticket;
25 std::string model_id{"AAAAA"};
26 std::string service_url;
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"
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080034 << "\t-r,--registration_ticket=TICKET Register device with "
35 "the given ticket\n"
36 << "\t-s,--staging Use staging server. Use "
37 "only with -r.\n"
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070038 << "\t--disable_privet Disable local privet\n";
39 }
40
41 bool Parse(int argc, char** argv) {
42 for (int i = 1; i < argc; ++i) {
43 std::string arg = argv[i];
44 if (arg == "-h" || arg == "--help") {
45 return false;
46 } else if (arg == "-b" || arg == "--bootstrapping") {
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080047 force_bootstrapping = true;
48 } else if (arg == "-s" || arg == "--staging") {
49 service_url =
50 "https://www-googleapis-staging.sandbox.google.com/weave/v1/";
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070051 } else if (arg == "--disable_privet") {
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080052 disable_privet = true;
53 } else if (arg.find("--registration_ticket=") != std::string::npos ||
54 arg.find("-r=") != std::string::npos) {
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070055 auto pos = arg.find("=");
56 if (pos == std::string::npos) {
57 return false;
58 }
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080059 registration_ticket = arg.substr(pos + 1);
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070060 } else if (arg.find("--v") != std::string::npos) {
61 auto pos = arg.find("=");
62 if (pos == std::string::npos) {
63 return false;
64 }
65 logging::SetMinLogLevel(-std::stoi(arg.substr(pos + 1)));
66 } else {
67 return false;
68 }
69 }
70 return true;
71 }
72 };
73
74 Daemon(const Options& opts)
Vitaly Bukaac18fcf2016-01-15 14:48:54 -080075 : task_runner_{new weave::examples::EventTaskRunner},
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080076 config_store_{new weave::examples::FileConfigStore(opts.model_id,
77 task_runner_.get())},
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070078 http_client_{new weave::examples::CurlHttpClient(task_runner_.get())},
79 network_{new weave::examples::EventNetworkImpl(task_runner_.get())},
80 bluetooth_{new weave::examples::BluetoothImpl} {
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080081 if (!opts.disable_privet) {
82 network_->SetSimulateOffline(opts.force_bootstrapping);
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070083
84 dns_sd_.reset(new weave::examples::AvahiClient);
85 http_server_.reset(
86 new weave::examples::HttpServerImpl{task_runner_.get()});
87 if (weave::examples::WifiImpl::HasWifiCapability())
88 wifi_.reset(
89 new weave::examples::WifiImpl{task_runner_.get(), network_.get()});
90 }
91 device_ = weave::Device::Create(config_store_.get(), task_runner_.get(),
92 http_client_.get(), network_.get(),
93 dns_sd_.get(), http_server_.get(),
94 wifi_.get(), bluetooth_.get());
95
Vitaly Buka03ee8ac2016-02-05 11:40:33 -080096 if (!opts.registration_ticket.empty()) {
97 weave::RegistrationData data;
98 data.ticket_id = opts.registration_ticket;
99 data.service_url = opts.service_url;
100 device_->Register(data, base::Bind(&OnRegisterDeviceDone, device_.get()));
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700101 }
102 }
103
104 void Run() { task_runner_->Run(); }
105
106 weave::Device* GetDevice() const { return device_.get(); }
107
108 weave::examples::EventTaskRunner* GetTaskRunner() const {
109 return task_runner_.get();
110 }
111
112 private:
113 static void OnRegisterDeviceDone(weave::Device* device,
114 weave::ErrorPtr error) {
115 if (error)
116 LOG(ERROR) << "Fail to register device: " << error->GetMessage();
117 else
118 LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
119 }
120
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700121 std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
Vitaly Bukaac18fcf2016-01-15 14:48:54 -0800122 std::unique_ptr<weave::examples::FileConfigStore> config_store_;
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700123 std::unique_ptr<weave::examples::CurlHttpClient> http_client_;
124 std::unique_ptr<weave::examples::EventNetworkImpl> network_;
125 std::unique_ptr<weave::examples::BluetoothImpl> bluetooth_;
126 std::unique_ptr<weave::examples::AvahiClient> dns_sd_;
127 std::unique_ptr<weave::examples::HttpServerImpl> http_server_;
128 std::unique_ptr<weave::examples::WifiImpl> wifi_;
129 std::unique_ptr<weave::Device> device_;
130};