daemon.h revision ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18
13fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine// Copyright 2015 The Weave Authors. All rights reserved.
23fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine// Use of this source code is governed by a BSD-style license that can be
33fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine// found in the LICENSE file.
43fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
53fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include <weave/device.h>
63fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include <weave/error.h>
73fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
83fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include <base/bind.h>
93fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
103fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/avahi_client.h"
113fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/bluez_client.h"
123fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/curl_http_client.h"
133fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/event_http_server.h"
143fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/event_network.h"
153fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/event_task_runner.h"
163fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/file_config_store.h"
173fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine#include "examples/provider/wifi_manager.h"
183fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
193fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosineclass Daemon {
203fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine public:
213fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  struct Options {
22321dae66a2e2c6efbc85f990eaa2aefd192602a3Paul Westbrook    bool force_bootstrapping_{false};
23321dae66a2e2c6efbc85f990eaa2aefd192602a3Paul Westbrook    bool disable_security_{false};
24321dae66a2e2c6efbc85f990eaa2aefd192602a3Paul Westbrook    bool disable_privet_{false};
253fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    std::string registration_ticket_;
26321dae66a2e2c6efbc85f990eaa2aefd192602a3Paul Westbrook    std::string model_id_{"AAAAA"};
273fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
283fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    static void ShowUsage(const std::string& name) {
293fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(ERROR) << "\nUsage: " << name << " <option(s)>"
303fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\nOptions:\n"
313fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t-h,--help                    Show this help message\n"
323fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--v=LEVEL                    Logging level\n"
333fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t-b,--bootstrapping           Force WiFi bootstrapping\n"
343fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t-d,--disable_security        Disable privet security\n"
353fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--registration_ticket=TICKET Register device with the "
363fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                    "given ticket\n"
373fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--disable_privet             Disable local privet\n";
383fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
393fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
403fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    bool Parse(int argc, char** argv) {
413fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      for (int i = 1; i < argc; ++i) {
423fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        std::string arg = argv[i];
433fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        if (arg == "-h" || arg == "--help") {
443fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          return false;
453fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg == "-b" || arg == "--bootstrapping") {
463fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          force_bootstrapping_ = true;
473fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg == "-d" || arg == "--disable_security") {
483fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          disable_security_ = true;
493fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg == "--disable_privet") {
503fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          disable_privet_ = true;
513fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg.find("--registration_ticket") != std::string::npos) {
523fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          auto pos = arg.find("=");
533fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          if (pos == std::string::npos) {
543fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            return false;
553fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          }
563fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          registration_ticket_ = arg.substr(pos + 1);
573fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg.find("--v") != std::string::npos) {
583fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          auto pos = arg.find("=");
593fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          if (pos == std::string::npos) {
603fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            return false;
613fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          }
623fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          logging::SetMinLogLevel(-std::stoi(arg.substr(pos + 1)));
633fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else {
643fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          return false;
653fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        }
663fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      }
673fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      return true;
683fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
693fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  };
703fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
713fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  Daemon(const Options& opts)
72ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka      : task_runner_{new weave::examples::EventTaskRunner},
73ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka        config_store_{
74ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka            new weave::examples::FileConfigStore(opts.disable_security_,
75ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka                                                 opts.model_id_,
76ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka                                                 task_runner_.get())},
773fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        http_client_{new weave::examples::CurlHttpClient(task_runner_.get())},
783fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        network_{new weave::examples::EventNetworkImpl(task_runner_.get())},
793fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        bluetooth_{new weave::examples::BluetoothImpl} {
803fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (!opts.disable_privet_) {
813fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      network_->SetSimulateOffline(opts.force_bootstrapping_);
823fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
833fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      dns_sd_.reset(new weave::examples::AvahiClient);
843fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      http_server_.reset(
853fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          new weave::examples::HttpServerImpl{task_runner_.get()});
863fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      if (weave::examples::WifiImpl::HasWifiCapability())
873fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        wifi_.reset(
883fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            new weave::examples::WifiImpl{task_runner_.get(), network_.get()});
893fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
903fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    device_ = weave::Device::Create(config_store_.get(), task_runner_.get(),
913fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    http_client_.get(), network_.get(),
923fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    dns_sd_.get(), http_server_.get(),
933fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    wifi_.get(), bluetooth_.get());
943fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
953fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (!opts.registration_ticket_.empty()) {
963fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      device_->Register(opts.registration_ticket_,
973fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                        base::Bind(&OnRegisterDeviceDone, device_.get()));
983fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
993fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
1003fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1013fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  void Run() { task_runner_->Run(); }
1023fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1033fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  weave::Device* GetDevice() const { return device_.get(); }
1043fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1053fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  weave::examples::EventTaskRunner* GetTaskRunner() const {
1063fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    return task_runner_.get();
1073fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
1083fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1093fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine private:
1103fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  static void OnRegisterDeviceDone(weave::Device* device,
1113fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                   weave::ErrorPtr error) {
1123fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (error)
1133fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(ERROR) << "Fail to register device: " << error->GetMessage();
1143fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    else
1153fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
1163fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
1173fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1183fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
119ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka  std::unique_ptr<weave::examples::FileConfigStore> config_store_;
1203fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::CurlHttpClient> http_client_;
1213fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::EventNetworkImpl> network_;
1223fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::BluetoothImpl> bluetooth_;
1233fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::AvahiClient> dns_sd_;
1243fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::HttpServerImpl> http_server_;
1253fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::WifiImpl> wifi_;
1263fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::Device> device_;
1273fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine};
128