blob: e173269dc75f3b217f0b8029beef0ec63c948e59 [file] [log] [blame]
Chris Sosa45d9f102014-03-24 11:18:54 -07001// Copyright 2014 The Chromium OS 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 <string>
6
Christopher Wileye0fdeee2015-02-07 18:29:32 -08007#include <base/files/file_path.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -07008#include <chromeos/dbus/async_event_sequencer.h>
9#include <chromeos/dbus/exported_object_manager.h>
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070010#include <chromeos/daemons/dbus_daemon.h>
Christopher Wileye0fdeee2015-02-07 18:29:32 -080011#include <chromeos/flag_helper.h>
Vitaly Bukaae96cc32015-06-09 17:22:18 -070012#include <chromeos/strings/string_utils.h>
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070013#include <chromeos/syslog_logging.h>
Chris Sosa45d9f102014-03-24 11:18:54 -070014
Alex Vakulenko420e49f2014-12-01 17:53:27 -080015#include "buffet/dbus_constants.h"
Christopher Wileya4915c42014-03-27 14:45:37 -070016#include "buffet/manager.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070017
Christopher Wiley2d2d92b2014-07-29 14:07:10 -070018using chromeos::dbus_utils::AsyncEventSequencer;
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070019using chromeos::DBusServiceDaemon;
20using buffet::dbus_constants::kServiceName;
21using buffet::dbus_constants::kRootServicePath;
Christopher Wiley90016242014-04-01 17:33:29 -070022
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070023namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070024
Alex Vakulenko534a3122015-05-22 15:48:53 -070025class Daemon final : public DBusServiceDaemon {
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070026 public:
Vitaly Bukaae96cc32015-06-09 17:22:18 -070027 explicit Daemon(const buffet::Manager::Options& options)
28 : DBusServiceDaemon(kServiceName, kRootServicePath), options_{options} {}
Chris Sosaababc5c2014-04-09 15:42:01 -070029
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070030 protected:
31 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
32 manager_.reset(new buffet::Manager(object_manager_->AsWeakPtr()));
Vitaly Bukaae96cc32015-06-09 17:22:18 -070033 manager_->Start(options_, sequencer);
Chris Sosa45d9f102014-03-24 11:18:54 -070034 }
Chris Sosa45d9f102014-03-24 11:18:54 -070035
Vitaly Bukaae96cc32015-06-09 17:22:18 -070036 void OnShutdown(int* return_code) override { manager_->Stop(); }
Chris Sosa45d9f102014-03-24 11:18:54 -070037
Vitaly Bukaae96cc32015-06-09 17:22:18 -070038 private:
39 buffet::Manager::Options options_;
40 std::unique_ptr<buffet::Manager> manager_;
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070041 DISALLOW_COPY_AND_ASSIGN(Daemon);
42};
Chris Sosa45d9f102014-03-24 11:18:54 -070043
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070044} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070045
Christopher Wileye0fdeee2015-02-07 18:29:32 -080046namespace {
47
48const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf";
49const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info";
50
51} // namespace
52
Chris Sosa45d9f102014-03-24 11:18:54 -070053int main(int argc, char* argv[]) {
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070054 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080055 DEFINE_string(config_path, kDefaultConfigFilePath,
56 "Path to file containing config information.");
57 DEFINE_string(state_path, kDefaultStateFilePath,
58 "Path to file containing state information.");
Christopher Wileybb5b8482015-02-12 13:42:16 -080059 DEFINE_string(test_definitions_path, "",
60 "Path to directory containing additional command "
61 "and state definitions. For use in test only.");
Christopher Wileyd732bd02015-04-07 11:11:18 -070062 DEFINE_bool(enable_xmpp, true,
63 "Connect to GCD via a persistent XMPP connection.");
Vitaly Bukaae96cc32015-06-09 17:22:18 -070064 DEFINE_bool(disable_privet, false, "disable Privet protocol");
65 DEFINE_bool(disable_security, false, "disable Privet security for tests");
66 DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping");
67 DEFINE_string(device_whitelist, "",
68 "Comma separated list of network interfaces to monitor for "
69 "connectivity (an empty list enables all interfaces).");
Aaron Kemp292cc842015-02-11 08:00:06 -080070 chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080071 if (FLAGS_config_path.empty())
72 FLAGS_config_path = kDefaultConfigFilePath;
73 if (FLAGS_state_path.empty())
74 FLAGS_state_path = kDefaultStateFilePath;
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070075 int flags = chromeos::kLogToSyslog | chromeos::kLogHeader;
76 if (FLAGS_log_to_stderr)
77 flags |= chromeos::kLogToStderr;
78 chromeos::InitLog(flags);
79
Vitaly Bukaae96cc32015-06-09 17:22:18 -070080 auto device_whitelist =
81 chromeos::string_utils::Split(FLAGS_device_whitelist, ",", true, true);
82
83 buffet::Manager::Options options;
84 options.config_path = base::FilePath{FLAGS_config_path};
85 options.state_path = base::FilePath{FLAGS_state_path};
86 options.test_definitions_path = base::FilePath{FLAGS_test_definitions_path};
87 options.xmpp_enabled = FLAGS_enable_xmpp;
Vitaly Buka63cc3d22015-06-23 20:11:36 -070088 options.device_whitelist.insert(device_whitelist.begin(),
89 device_whitelist.end());
Vitaly Buka3d2cd662015-06-16 16:23:03 -070090 options.privet.config_path = base::FilePath{FLAGS_config_path};
Vitaly Bukaae96cc32015-06-09 17:22:18 -070091 options.privet.disable_privet = FLAGS_disable_privet;
92 options.privet.disable_security = FLAGS_disable_security;
93 options.privet.enable_ping = FLAGS_enable_ping;
Vitaly Bukaae96cc32015-06-09 17:22:18 -070094
95 buffet::Daemon daemon{options};
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070096 return daemon.Run();
Chris Sosa45d9f102014-03-24 11:18:54 -070097}