Home | History | Annotate | Download | only in dbus
      1 // Copyright 2016 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 <brillo/dbus/dbus_connection.h>
      6 
      7 #include <sysexits.h>
      8 
      9 #include <base/bind.h>
     10 #include <brillo/dbus/async_event_sequencer.h>
     11 #include <brillo/dbus/exported_object_manager.h>
     12 
     13 using brillo::dbus_utils::AsyncEventSequencer;
     14 using brillo::dbus_utils::ExportedObjectManager;
     15 
     16 namespace brillo {
     17 
     18 DBusConnection::DBusConnection() {
     19 }
     20 
     21 DBusConnection::~DBusConnection() {
     22   if (bus_)
     23     bus_->ShutdownAndBlock();
     24 }
     25 
     26 scoped_refptr<dbus::Bus> DBusConnection::Connect() {
     27   return ConnectWithTimeout(base::TimeDelta());
     28 }
     29 
     30 scoped_refptr<dbus::Bus> DBusConnection::ConnectWithTimeout(
     31     base::TimeDelta timeout) {
     32   if (bus_)
     33     return bus_;
     34 
     35   base::TimeTicks deadline = base::TimeTicks::Now() + timeout;
     36 
     37   dbus::Bus::Options options;
     38   options.bus_type = dbus::Bus::SYSTEM;
     39 
     40   scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
     41 
     42   do {
     43     if (bus->Connect()) {
     44       bus_ = bus;
     45       return bus_;
     46     }
     47     LOG(WARNING) << "Failed to get system bus.";
     48     // Wait 1 second to prevent trashing the device while waiting for the
     49     // dbus-daemon to start.
     50     sleep(1);
     51   } while (base::TimeTicks::Now() < deadline);
     52 
     53   LOG(ERROR) << "Failed to get system bus after " << timeout.InSeconds()
     54              << " seconds.";
     55   return nullptr;
     56 }
     57 
     58 }  // namespace brillo
     59