blob: 3335b18885249778040373e912fd7c1499bbe204 [file] [log] [blame]
Darin Petkova9b1fed2012-02-29 11:49:05 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkova9b1fed2012-02-29 11:49:05 +01005#include "shill/dbus_control.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <string>
8
Chris Masoned7732e42011-05-20 11:08:56 -07009#include <dbus-c++/glib-integration.h>
10#include <dbus-c++/util.h>
11
Chris Masoned7732e42011-05-20 11:08:56 -070012#include "shill/device_dbus_adaptor.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070013#include "shill/ipconfig_dbus_adaptor.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Chris Masoned7732e42011-05-20 11:08:56 -070015#include "shill/manager_dbus_adaptor.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070016#include "shill/profile_dbus_adaptor.h"
Darin Petkova9b1fed2012-02-29 11:49:05 +010017#include "shill/rpc_task_dbus_adaptor.h"
Chris Masoned7732e42011-05-20 11:08:56 -070018#include "shill/service_dbus_adaptor.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019
20namespace shill {
Darin Petkova9b1fed2012-02-29 11:49:05 +010021
Chris Masoned7732e42011-05-20 11:08:56 -070022DBusControl::DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070023
Chris Masoned7732e42011-05-20 11:08:56 -070024DBusControl::~DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070025
Ben Chan3d0e8d22014-07-10 23:40:47 -070026template <typename Object, typename AdaptorInterface, typename Adaptor>
27AdaptorInterface *DBusControl::CreateAdaptor(Object *object) {
28 AdaptorInterface *adaptor = NULL;
Wade Guthriee2e29242012-05-08 08:58:56 -070029 try {
Ben Chan3d0e8d22014-07-10 23:40:47 -070030 adaptor = new Adaptor(connection_.get(), object);
31 } catch(const DBus::ErrorObjectPathInUse &error) {
32 LOG(FATAL) << error.message() << " (object path in use)";
33 } catch(const DBus::ErrorNoMemory &error) {
34 LOG(FATAL) << error.message() << " (no memory)";
Wade Guthriee2e29242012-05-08 08:58:56 -070035 } catch(const DBus::Error &error) {
Ben Chan7fe43b82014-03-06 15:36:33 -080036 LOG(FATAL) << error.message();
Wade Guthriee2e29242012-05-08 08:58:56 -070037 }
Ben Chan3d0e8d22014-07-10 23:40:47 -070038 return adaptor;
39}
40
41DeviceAdaptorInterface *DBusControl::CreateDeviceAdaptor(Device *device) {
42 return CreateAdaptor<Device, DeviceAdaptorInterface, DeviceDBusAdaptor>(
43 device);
Paul Stewart75897df2011-04-27 09:05:53 -070044}
45
Chris Masonec6c6c132011-06-30 11:29:52 -070046IPConfigAdaptorInterface *DBusControl::CreateIPConfigAdaptor(IPConfig *config) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070047 return CreateAdaptor<IPConfig, IPConfigAdaptorInterface, IPConfigDBusAdaptor>(
48 config);
Chris Masonec6c6c132011-06-30 11:29:52 -070049}
50
51ManagerAdaptorInterface *DBusControl::CreateManagerAdaptor(Manager *manager) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070052 return CreateAdaptor<Manager, ManagerAdaptorInterface, ManagerDBusAdaptor>(
53 manager);
Chris Masonec6c6c132011-06-30 11:29:52 -070054}
55
Chris Masone52cd19b2011-06-29 17:23:04 -070056ProfileAdaptorInterface *DBusControl::CreateProfileAdaptor(Profile *profile) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070057 return CreateAdaptor<Profile, ProfileAdaptorInterface, ProfileDBusAdaptor>(
58 profile);
Chris Masone52cd19b2011-06-29 17:23:04 -070059}
60
Darin Petkova9b1fed2012-02-29 11:49:05 +010061RPCTaskAdaptorInterface *DBusControl::CreateRPCTaskAdaptor(RPCTask *task) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070062 return CreateAdaptor<RPCTask, RPCTaskAdaptorInterface, RPCTaskDBusAdaptor>(
63 task);
Darin Petkova9b1fed2012-02-29 11:49:05 +010064}
65
Chris Masonec6c6c132011-06-30 11:29:52 -070066ServiceAdaptorInterface *DBusControl::CreateServiceAdaptor(Service *service) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070067 return CreateAdaptor<Service, ServiceAdaptorInterface, ServiceDBusAdaptor>(
68 service);
Chris Masonec6c6c132011-06-30 11:29:52 -070069}
70
Darin Petkovd1b715b2011-06-02 21:21:22 -070071void DBusControl::Init() {
72 CHECK(!connection_.get());
73 dispatcher_.reset(new(std::nothrow) DBus::Glib::BusDispatcher());
74 CHECK(dispatcher_.get()) << "Failed to create a dbus-dispatcher";
75 DBus::default_dispatcher = dispatcher_.get();
76 dispatcher_->attach(NULL);
77 connection_.reset(new DBus::Connection(DBus::Connection::SystemBus()));
mukesh agrawal4041a1c2014-04-15 14:49:59 -070078 if (!connection_->acquire_name(SHILL_INTERFACE)) {
79 LOG(FATAL) << "Failed to acquire D-Bus name " << SHILL_INTERFACE << ". "
80 << "Is another shill running?";
81 }
Chris Masoned7732e42011-05-20 11:08:56 -070082}
Paul Stewart75897df2011-04-27 09:05:53 -070083
84} // namespace shill