blob: 461680b323a3ba3472a4f67171d545be8545ecbf [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>
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070012#include <chromeos/syslog_logging.h>
Chris Sosa45d9f102014-03-24 11:18:54 -070013
Alex Vakulenko420e49f2014-12-01 17:53:27 -080014#include "buffet/dbus_constants.h"
Christopher Wileya4915c42014-03-27 14:45:37 -070015#include "buffet/manager.h"
Chris Sosa45d9f102014-03-24 11:18:54 -070016
Christopher Wiley2d2d92b2014-07-29 14:07:10 -070017using chromeos::dbus_utils::AsyncEventSequencer;
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070018using chromeos::DBusServiceDaemon;
19using buffet::dbus_constants::kServiceName;
20using buffet::dbus_constants::kRootServicePath;
Christopher Wiley90016242014-04-01 17:33:29 -070021
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070022namespace buffet {
Chris Sosa45d9f102014-03-24 11:18:54 -070023
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070024class Daemon : public DBusServiceDaemon {
25 public:
Christopher Wileye0fdeee2015-02-07 18:29:32 -080026 Daemon(const base::FilePath& config_path,
Christopher Wileybb5b8482015-02-12 13:42:16 -080027 const base::FilePath& state_path,
Christopher Wileyd732bd02015-04-07 11:11:18 -070028 const base::FilePath& test_definitions_path,
29 bool enable_xmpp)
Christopher Wileye0fdeee2015-02-07 18:29:32 -080030 : DBusServiceDaemon(kServiceName, kRootServicePath),
31 config_path_{config_path},
Christopher Wileybb5b8482015-02-12 13:42:16 -080032 state_path_{state_path},
Christopher Wileyd732bd02015-04-07 11:11:18 -070033 test_definitions_path_{test_definitions_path},
34 enable_xmpp_{enable_xmpp} {}
Chris Sosaababc5c2014-04-09 15:42:01 -070035
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070036 protected:
37 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
38 manager_.reset(new buffet::Manager(object_manager_->AsWeakPtr()));
39 manager_->RegisterAsync(
Christopher Wileye0fdeee2015-02-07 18:29:32 -080040 config_path_,
41 state_path_,
Christopher Wileybb5b8482015-02-12 13:42:16 -080042 test_definitions_path_,
Christopher Wileyd732bd02015-04-07 11:11:18 -070043 enable_xmpp_,
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070044 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
Chris Sosa45d9f102014-03-24 11:18:54 -070045 }
Chris Sosa45d9f102014-03-24 11:18:54 -070046
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070047 private:
Christopher Wiley583d64b2015-03-24 14:30:17 -070048 BuffetConfig config_;
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070049 std::unique_ptr<buffet::Manager> manager_;
Christopher Wileye0fdeee2015-02-07 18:29:32 -080050 const base::FilePath config_path_;
51 const base::FilePath state_path_;
Christopher Wileybb5b8482015-02-12 13:42:16 -080052 const base::FilePath test_definitions_path_;
Christopher Wileyd732bd02015-04-07 11:11:18 -070053 const bool enable_xmpp_;
Chris Sosa45d9f102014-03-24 11:18:54 -070054
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070055 DISALLOW_COPY_AND_ASSIGN(Daemon);
56};
Chris Sosa45d9f102014-03-24 11:18:54 -070057
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070058} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070059
Christopher Wileye0fdeee2015-02-07 18:29:32 -080060namespace {
61
62const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf";
63const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info";
64
65} // namespace
66
Chris Sosa45d9f102014-03-24 11:18:54 -070067int main(int argc, char* argv[]) {
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070068 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080069 DEFINE_string(config_path, kDefaultConfigFilePath,
70 "Path to file containing config information.");
71 DEFINE_string(state_path, kDefaultStateFilePath,
72 "Path to file containing state information.");
Christopher Wileybb5b8482015-02-12 13:42:16 -080073 DEFINE_string(test_definitions_path, "",
74 "Path to directory containing additional command "
75 "and state definitions. For use in test only.");
Christopher Wileyd732bd02015-04-07 11:11:18 -070076 DEFINE_bool(enable_xmpp, true,
77 "Connect to GCD via a persistent XMPP connection.");
Aaron Kemp292cc842015-02-11 08:00:06 -080078 chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080079 if (FLAGS_config_path.empty())
80 FLAGS_config_path = kDefaultConfigFilePath;
81 if (FLAGS_state_path.empty())
82 FLAGS_state_path = kDefaultStateFilePath;
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070083 int flags = chromeos::kLogToSyslog | chromeos::kLogHeader;
84 if (FLAGS_log_to_stderr)
85 flags |= chromeos::kLogToStderr;
86 chromeos::InitLog(flags);
87
Christopher Wileye0fdeee2015-02-07 18:29:32 -080088 buffet::Daemon daemon{base::FilePath{FLAGS_config_path},
Christopher Wileybb5b8482015-02-12 13:42:16 -080089 base::FilePath{FLAGS_state_path},
Christopher Wileyd732bd02015-04-07 11:11:18 -070090 base::FilePath{FLAGS_test_definitions_path},
91 FLAGS_enable_xmpp};
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070092 return daemon.Run();
Chris Sosa45d9f102014-03-24 11:18:54 -070093}