blob: 7d70ffdf678949359f4c224e03a0deecc0b3df65 [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()));
Vitaly Buka76e70592015-04-16 11:39:02 -070039 manager_->Start(
40 config_path_, state_path_, test_definitions_path_, enable_xmpp_,
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070041 sequencer->GetHandler("Manager.RegisterAsync() failed.", true));
Chris Sosa45d9f102014-03-24 11:18:54 -070042 }
Chris Sosa45d9f102014-03-24 11:18:54 -070043
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070044 private:
Christopher Wiley583d64b2015-03-24 14:30:17 -070045 BuffetConfig config_;
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070046 std::unique_ptr<buffet::Manager> manager_;
Christopher Wileye0fdeee2015-02-07 18:29:32 -080047 const base::FilePath config_path_;
48 const base::FilePath state_path_;
Christopher Wileybb5b8482015-02-12 13:42:16 -080049 const base::FilePath test_definitions_path_;
Christopher Wileyd732bd02015-04-07 11:11:18 -070050 const bool enable_xmpp_;
Chris Sosa45d9f102014-03-24 11:18:54 -070051
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070052 DISALLOW_COPY_AND_ASSIGN(Daemon);
53};
Chris Sosa45d9f102014-03-24 11:18:54 -070054
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070055} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070056
Christopher Wileye0fdeee2015-02-07 18:29:32 -080057namespace {
58
59const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf";
60const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info";
61
62} // namespace
63
Chris Sosa45d9f102014-03-24 11:18:54 -070064int main(int argc, char* argv[]) {
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070065 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080066 DEFINE_string(config_path, kDefaultConfigFilePath,
67 "Path to file containing config information.");
68 DEFINE_string(state_path, kDefaultStateFilePath,
69 "Path to file containing state information.");
Christopher Wileybb5b8482015-02-12 13:42:16 -080070 DEFINE_string(test_definitions_path, "",
71 "Path to directory containing additional command "
72 "and state definitions. For use in test only.");
Christopher Wileyd732bd02015-04-07 11:11:18 -070073 DEFINE_bool(enable_xmpp, true,
74 "Connect to GCD via a persistent XMPP connection.");
Aaron Kemp292cc842015-02-11 08:00:06 -080075 chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080076 if (FLAGS_config_path.empty())
77 FLAGS_config_path = kDefaultConfigFilePath;
78 if (FLAGS_state_path.empty())
79 FLAGS_state_path = kDefaultStateFilePath;
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070080 int flags = chromeos::kLogToSyslog | chromeos::kLogHeader;
81 if (FLAGS_log_to_stderr)
82 flags |= chromeos::kLogToStderr;
83 chromeos::InitLog(flags);
84
Christopher Wileye0fdeee2015-02-07 18:29:32 -080085 buffet::Daemon daemon{base::FilePath{FLAGS_config_path},
Christopher Wileybb5b8482015-02-12 13:42:16 -080086 base::FilePath{FLAGS_state_path},
Christopher Wileyd732bd02015-04-07 11:11:18 -070087 base::FilePath{FLAGS_test_definitions_path},
88 FLAGS_enable_xmpp};
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070089 return daemon.Run();
Chris Sosa45d9f102014-03-24 11:18:54 -070090}