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_privet_{false};
243fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    std::string registration_ticket_;
25321dae66a2e2c6efbc85f990eaa2aefd192602a3Paul Westbrook    std::string model_id_{"AAAAA"};
263fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
273fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    static void ShowUsage(const std::string& name) {
283fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(ERROR) << "\nUsage: " << name << " <option(s)>"
293fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\nOptions:\n"
303fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t-h,--help                    Show this help message\n"
313fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--v=LEVEL                    Logging level\n"
323fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t-b,--bootstrapping           Force WiFi bootstrapping\n"
333fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--registration_ticket=TICKET Register device with the "
343fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                    "given ticket\n"
353fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                 << "\t--disable_privet             Disable local privet\n";
363fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
373fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
383fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    bool Parse(int argc, char** argv) {
393fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      for (int i = 1; i < argc; ++i) {
403fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        std::string arg = argv[i];
413fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        if (arg == "-h" || arg == "--help") {
423fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          return false;
433fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg == "-b" || arg == "--bootstrapping") {
443fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          force_bootstrapping_ = true;
453fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg == "--disable_privet") {
463fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          disable_privet_ = true;
473fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg.find("--registration_ticket") != std::string::npos) {
483fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          auto pos = arg.find("=");
493fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          if (pos == std::string::npos) {
503fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            return false;
513fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          }
523fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          registration_ticket_ = arg.substr(pos + 1);
533fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else if (arg.find("--v") != std::string::npos) {
543fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          auto pos = arg.find("=");
553fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          if (pos == std::string::npos) {
563fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            return false;
573fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          }
583fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          logging::SetMinLogLevel(-std::stoi(arg.substr(pos + 1)));
593fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        } else {
603fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          return false;
613fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        }
623fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      }
633fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      return true;
643fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
653fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  };
663fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
673fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  Daemon(const Options& opts)
68ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka      : task_runner_{new weave::examples::EventTaskRunner},
69ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka        config_store_{
70919a8a9908cd48271a0b3e6afa5a444d912459e2Vitaly Buka            new weave::examples::FileConfigStore(opts.model_id_,
71ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka                                                 task_runner_.get())},
723fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        http_client_{new weave::examples::CurlHttpClient(task_runner_.get())},
733fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        network_{new weave::examples::EventNetworkImpl(task_runner_.get())},
743fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        bluetooth_{new weave::examples::BluetoothImpl} {
753fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (!opts.disable_privet_) {
763fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      network_->SetSimulateOffline(opts.force_bootstrapping_);
773fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
783fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      dns_sd_.reset(new weave::examples::AvahiClient);
793fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      http_server_.reset(
803fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine          new weave::examples::HttpServerImpl{task_runner_.get()});
813fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      if (weave::examples::WifiImpl::HasWifiCapability())
823fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine        wifi_.reset(
833fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine            new weave::examples::WifiImpl{task_runner_.get(), network_.get()});
843fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
853fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    device_ = weave::Device::Create(config_store_.get(), task_runner_.get(),
863fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    http_client_.get(), network_.get(),
873fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    dns_sd_.get(), http_server_.get(),
883fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                    wifi_.get(), bluetooth_.get());
893fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
903fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (!opts.registration_ticket_.empty()) {
913fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      device_->Register(opts.registration_ticket_,
923fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                        base::Bind(&OnRegisterDeviceDone, device_.get()));
933fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    }
943fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
953fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
963fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  void Run() { task_runner_->Run(); }
973fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
983fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  weave::Device* GetDevice() const { return device_.get(); }
993fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1003fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  weave::examples::EventTaskRunner* GetTaskRunner() const {
1013fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    return task_runner_.get();
1023fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
1033fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1043fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine private:
1053fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  static void OnRegisterDeviceDone(weave::Device* device,
1063fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine                                   weave::ErrorPtr error) {
1073fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    if (error)
1083fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(ERROR) << "Fail to register device: " << error->GetMessage();
1093fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine    else
1103fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine      LOG(INFO) << "Device registered: " << device->GetSettings().cloud_id;
1113fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  }
1123fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine
1133fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::EventTaskRunner> task_runner_;
114ac18fcf3e15a74d9980ea6b09d2482a86d7fdf18Vitaly Buka  std::unique_ptr<weave::examples::FileConfigStore> config_store_;
1153fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::CurlHttpClient> http_client_;
1163fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::EventNetworkImpl> network_;
1173fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::BluetoothImpl> bluetooth_;
1183fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::AvahiClient> dns_sd_;
1193fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::HttpServerImpl> http_server_;
1203fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::examples::WifiImpl> wifi_;
1213fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine  std::unique_ptr<weave::Device> device_;
1223fb474e64c9ed199919313321e46da3a531ecc7dJohan Euphrosine};
123