blob: 578ee7499c22f84db34ebe43262147b41573c5d2 [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"
Prabhu Kaliamoorthi127b3412014-10-16 13:00:25 +020019#include "shill/third_party_vpn_dbus_adaptor.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020
21namespace shill {
Darin Petkova9b1fed2012-02-29 11:49:05 +010022
Chris Masoned7732e42011-05-20 11:08:56 -070023DBusControl::DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070024
Chris Masoned7732e42011-05-20 11:08:56 -070025DBusControl::~DBusControl() {}
Paul Stewart75897df2011-04-27 09:05:53 -070026
Ben Chan3d0e8d22014-07-10 23:40:47 -070027template <typename Object, typename AdaptorInterface, typename Adaptor>
28AdaptorInterface *DBusControl::CreateAdaptor(Object *object) {
Ben Chancc225ef2014-09-30 13:26:51 -070029 AdaptorInterface *adaptor = nullptr;
Wade Guthriee2e29242012-05-08 08:58:56 -070030 try {
Ben Chan3d0e8d22014-07-10 23:40:47 -070031 adaptor = new Adaptor(connection_.get(), object);
32 } catch(const DBus::ErrorObjectPathInUse &error) {
33 LOG(FATAL) << error.message() << " (object path in use)";
34 } catch(const DBus::ErrorNoMemory &error) {
35 LOG(FATAL) << error.message() << " (no memory)";
Wade Guthriee2e29242012-05-08 08:58:56 -070036 } catch(const DBus::Error &error) {
Ben Chan7fe43b82014-03-06 15:36:33 -080037 LOG(FATAL) << error.message();
Wade Guthriee2e29242012-05-08 08:58:56 -070038 }
Ben Chan3d0e8d22014-07-10 23:40:47 -070039 return adaptor;
40}
41
42DeviceAdaptorInterface *DBusControl::CreateDeviceAdaptor(Device *device) {
43 return CreateAdaptor<Device, DeviceAdaptorInterface, DeviceDBusAdaptor>(
44 device);
Paul Stewart75897df2011-04-27 09:05:53 -070045}
46
Chris Masonec6c6c132011-06-30 11:29:52 -070047IPConfigAdaptorInterface *DBusControl::CreateIPConfigAdaptor(IPConfig *config) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070048 return CreateAdaptor<IPConfig, IPConfigAdaptorInterface, IPConfigDBusAdaptor>(
49 config);
Chris Masonec6c6c132011-06-30 11:29:52 -070050}
51
52ManagerAdaptorInterface *DBusControl::CreateManagerAdaptor(Manager *manager) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070053 return CreateAdaptor<Manager, ManagerAdaptorInterface, ManagerDBusAdaptor>(
54 manager);
Chris Masonec6c6c132011-06-30 11:29:52 -070055}
56
Chris Masone52cd19b2011-06-29 17:23:04 -070057ProfileAdaptorInterface *DBusControl::CreateProfileAdaptor(Profile *profile) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070058 return CreateAdaptor<Profile, ProfileAdaptorInterface, ProfileDBusAdaptor>(
59 profile);
Chris Masone52cd19b2011-06-29 17:23:04 -070060}
61
Darin Petkova9b1fed2012-02-29 11:49:05 +010062RPCTaskAdaptorInterface *DBusControl::CreateRPCTaskAdaptor(RPCTask *task) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070063 return CreateAdaptor<RPCTask, RPCTaskAdaptorInterface, RPCTaskDBusAdaptor>(
64 task);
Darin Petkova9b1fed2012-02-29 11:49:05 +010065}
66
Chris Masonec6c6c132011-06-30 11:29:52 -070067ServiceAdaptorInterface *DBusControl::CreateServiceAdaptor(Service *service) {
Ben Chan3d0e8d22014-07-10 23:40:47 -070068 return CreateAdaptor<Service, ServiceAdaptorInterface, ServiceDBusAdaptor>(
69 service);
Chris Masonec6c6c132011-06-30 11:29:52 -070070}
71
Prabhu Kaliamoorthi127b3412014-10-16 13:00:25 +020072#ifndef DISABLE_VPN
73ThirdPartyVpnAdaptorInterface *DBusControl::CreateThirdPartyVpnAdaptor(
74 ThirdPartyVpnDriver *driver) {
75 return CreateAdaptor<ThirdPartyVpnDriver, ThirdPartyVpnAdaptorInterface,
76 ThirdPartyVpnAdaptor>(driver);
77}
78#endif
79
Darin Petkovd1b715b2011-06-02 21:21:22 -070080void DBusControl::Init() {
81 CHECK(!connection_.get());
82 dispatcher_.reset(new(std::nothrow) DBus::Glib::BusDispatcher());
83 CHECK(dispatcher_.get()) << "Failed to create a dbus-dispatcher";
84 DBus::default_dispatcher = dispatcher_.get();
Ben Chancc225ef2014-09-30 13:26:51 -070085 dispatcher_->attach(nullptr);
Darin Petkovd1b715b2011-06-02 21:21:22 -070086 connection_.reset(new DBus::Connection(DBus::Connection::SystemBus()));
mukesh agrawal4041a1c2014-04-15 14:49:59 -070087 if (!connection_->acquire_name(SHILL_INTERFACE)) {
88 LOG(FATAL) << "Failed to acquire D-Bus name " << SHILL_INTERFACE << ". "
89 << "Is another shill running?";
90 }
Chris Masoned7732e42011-05-20 11:08:56 -070091}
Paul Stewart75897df2011-04-27 09:05:53 -070092
93} // namespace shill