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