Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 1 | // 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 Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 7 | #include <base/files/file_path.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/async_event_sequencer.h> |
| 9 | #include <chromeos/dbus/exported_object_manager.h> |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 10 | #include <chromeos/daemons/dbus_daemon.h> |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 11 | #include <chromeos/flag_helper.h> |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 12 | #include <chromeos/syslog_logging.h> |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 14 | #include "buffet/dbus_constants.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 15 | #include "buffet/manager.h" |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 16 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 17 | using chromeos::dbus_utils::AsyncEventSequencer; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 18 | using chromeos::DBusServiceDaemon; |
| 19 | using buffet::dbus_constants::kServiceName; |
| 20 | using buffet::dbus_constants::kRootServicePath; |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 21 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 22 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 23 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 24 | class Daemon : public DBusServiceDaemon { |
| 25 | public: |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 26 | Daemon(const base::FilePath& config_path, |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 27 | const base::FilePath& state_path, |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 28 | const base::FilePath& test_definitions_path, |
| 29 | bool enable_xmpp) |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 30 | : DBusServiceDaemon(kServiceName, kRootServicePath), |
| 31 | config_path_{config_path}, |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 32 | state_path_{state_path}, |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 33 | test_definitions_path_{test_definitions_path}, |
| 34 | enable_xmpp_{enable_xmpp} {} |
Chris Sosa | ababc5c | 2014-04-09 15:42:01 -0700 | [diff] [blame] | 35 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 36 | protected: |
| 37 | void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override { |
| 38 | manager_.reset(new buffet::Manager(object_manager_->AsWeakPtr())); |
Vitaly Buka | 76e7059 | 2015-04-16 11:39:02 -0700 | [diff] [blame] | 39 | manager_->Start( |
| 40 | config_path_, state_path_, test_definitions_path_, enable_xmpp_, |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 41 | sequencer->GetHandler("Manager.RegisterAsync() failed.", true)); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 42 | } |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 43 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 44 | private: |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 45 | BuffetConfig config_; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 46 | std::unique_ptr<buffet::Manager> manager_; |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 47 | const base::FilePath config_path_; |
| 48 | const base::FilePath state_path_; |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 49 | const base::FilePath test_definitions_path_; |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 50 | const bool enable_xmpp_; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 51 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 53 | }; |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 54 | |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 55 | } // namespace buffet |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 56 | |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 57 | namespace { |
| 58 | |
| 59 | const char kDefaultConfigFilePath[] = "/etc/buffet/buffet.conf"; |
| 60 | const char kDefaultStateFilePath[] = "/var/lib/buffet/device_reg_info"; |
| 61 | |
| 62 | } // namespace |
| 63 | |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 64 | int main(int argc, char* argv[]) { |
Vitaly Buka | c5e9b4d | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 65 | DEFINE_bool(log_to_stderr, false, "log trace messages to stderr as well"); |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 66 | 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 Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 70 | DEFINE_string(test_definitions_path, "", |
| 71 | "Path to directory containing additional command " |
| 72 | "and state definitions. For use in test only."); |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 73 | DEFINE_bool(enable_xmpp, true, |
| 74 | "Connect to GCD via a persistent XMPP connection."); |
Aaron Kemp | 292cc84 | 2015-02-11 08:00:06 -0800 | [diff] [blame] | 75 | chromeos::FlagHelper::Init(argc, argv, "Privet protocol handler daemon"); |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 76 | if (FLAGS_config_path.empty()) |
| 77 | FLAGS_config_path = kDefaultConfigFilePath; |
| 78 | if (FLAGS_state_path.empty()) |
| 79 | FLAGS_state_path = kDefaultStateFilePath; |
Vitaly Buka | c5e9b4d | 2015-03-13 23:54:21 -0700 | [diff] [blame] | 80 | int flags = chromeos::kLogToSyslog | chromeos::kLogHeader; |
| 81 | if (FLAGS_log_to_stderr) |
| 82 | flags |= chromeos::kLogToStderr; |
| 83 | chromeos::InitLog(flags); |
| 84 | |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 85 | buffet::Daemon daemon{base::FilePath{FLAGS_config_path}, |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 86 | base::FilePath{FLAGS_state_path}, |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 87 | base::FilePath{FLAGS_test_definitions_path}, |
| 88 | FLAGS_enable_xmpp}; |
Alex Vakulenko | 74bd01b | 2014-09-08 17:07:19 -0700 | [diff] [blame] | 89 | return daemon.Run(); |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 90 | } |