blob: 58d70d38db2204831474c4c6195c276f31b50e47 [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,
28 const base::FilePath& test_definitions_path)
Christopher Wileye0fdeee2015-02-07 18:29:32 -080029 : DBusServiceDaemon(kServiceName, kRootServicePath),
30 config_path_{config_path},
Christopher Wileybb5b8482015-02-12 13:42:16 -080031 state_path_{state_path},
32 test_definitions_path_{test_definitions_path} {}
Chris Sosaababc5c2014-04-09 15:42:01 -070033
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070034 protected:
35 void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
36 manager_.reset(new buffet::Manager(object_manager_->AsWeakPtr()));
37 manager_->RegisterAsync(
Christopher Wileye0fdeee2015-02-07 18:29:32 -080038 config_path_,
39 state_path_,
Christopher Wileybb5b8482015-02-12 13:42:16 -080040 test_definitions_path_,
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_;
Chris Sosa45d9f102014-03-24 11:18:54 -070050
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070051 DISALLOW_COPY_AND_ASSIGN(Daemon);
52};
Chris Sosa45d9f102014-03-24 11:18:54 -070053
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070054} // namespace buffet
Chris Sosa45d9f102014-03-24 11:18:54 -070055
Christopher Wileye0fdeee2015-02-07 18:29:32 -080056namespace {
57
58const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf";
59const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info";
60
61} // namespace
62
Chris Sosa45d9f102014-03-24 11:18:54 -070063int main(int argc, char* argv[]) {
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070064 DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080065 DEFINE_string(config_path, kDefaultConfigFilePath,
66 "Path to file containing config information.");
67 DEFINE_string(state_path, kDefaultStateFilePath,
68 "Path to file containing state information.");
Christopher Wileybb5b8482015-02-12 13:42:16 -080069 DEFINE_string(test_definitions_path, "",
70 "Path to directory containing additional command "
71 "and state definitions. For use in test only.");
Aaron Kemp292cc842015-02-11 08:00:06 -080072 chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon");
Christopher Wileye0fdeee2015-02-07 18:29:32 -080073 if (FLAGS_config_path.empty())
74 FLAGS_config_path = kDefaultConfigFilePath;
75 if (FLAGS_state_path.empty())
76 FLAGS_state_path = kDefaultStateFilePath;
Vitaly Bukac5e9b4d2015-03-13 23:54:21 -070077 int flags = chromeos::kLogToSyslog | chromeos::kLogHeader;
78 if (FLAGS_log_to_stderr)
79 flags |= chromeos::kLogToStderr;
80 chromeos::InitLog(flags);
81
Christopher Wileye0fdeee2015-02-07 18:29:32 -080082 buffet::Daemon daemon{base::FilePath{FLAGS_config_path},
Christopher Wileybb5b8482015-02-12 13:42:16 -080083 base::FilePath{FLAGS_state_path},
84 base::FilePath{FLAGS_test_definitions_path}};
Alex Vakulenko74bd01b2014-09-08 17:07:19 -070085 return daemon.Run();
Chris Sosa45d9f102014-03-24 11:18:54 -070086}