1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/binder/binder_control.h"
18
19 #include <base/bind.h>
20 #include <binder/IServiceManager.h>
21 #include <binderwrapper/binder_wrapper.h>
22 #include <brillo/binder_watcher.h>
23
24 // TODO(samueltan): remove when shill is no longer dependent on DBus proxies.
25 #include <dbus/service_constants.h>
26
27 #include "shill/manager.h"
28
29 #include "shill/binder/device_binder_adaptor.h"
30 #include "shill/binder/manager_binder_adaptor.h"
31 #include "shill/binder/service_binder_adaptor.h"
32 #include "shill/dbus/chromeos_dhcpcd_listener.h"
33 #include "shill/dbus/chromeos_dhcpcd_proxy.h"
34 #include "shill/ipconfig_adaptor_stub.h"
35 #include "shill/profile_adaptor_stub.h"
36 #include "shill/rpc_task_adaptor_stub.h"
37 #include "shill/third_party_vpn_adaptor_stub.h"
38 #include "shill/dbus/chromeos_firewalld_proxy.h"
39 #include "shill/power_manager_proxy_stub.h"
40 #include "shill/upstart/upstart_proxy_stub.h"
41 #if !defined(DISABLE_WIFI)
42 #include "shill/dbus/chromeos_supplicant_bss_proxy.h"
43 #endif // DISABLE_WIFI
44 #if !defined(DISABLE_WIFI) || !defined(DISABLE_WIRED_8021X)
45 #include "shill/dbus/chromeos_supplicant_interface_proxy.h"
46 #include "shill/dbus/chromeos_supplicant_network_proxy.h"
47 #include "shill/dbus/chromeos_supplicant_process_proxy.h"
48 #endif // DISABLE_WIFI || DISABLE_WIRED_8021X
49
50 using android::BinderWrapper;
51 using android::defaultServiceManager;
52 using std::string;
53 using std::to_string;
54
55 namespace shill {
56
57 // static.
58 const char BinderControl::kNullRpcIdentifier[] = "-1";
59
BinderControl(EventDispatcher * dispatcher)60 BinderControl::BinderControl(EventDispatcher* dispatcher)
61 : next_unique_binder_adaptor_id_(0),
62 dispatcher_(dispatcher),
63 null_identifier_(kNullRpcIdentifier) {
64 BinderWrapper::Create();
65 // Watch Binder events in the main loop
66 brillo::BinderWatcher binder_watcher;
67 CHECK(binder_watcher.Init()) << "Binder FD watcher init failed";
68
69 // Also initialize D-Bus, which we will use alongside Binder for IPC with
70 // daemons that do not yet support Binder.
71 // TODO(samueltan): remove when shill is no longer dependent on DBus proxies.
72 dbus::Bus::Options options;
73 options.bus_type = dbus::Bus::SYSTEM;
74 proxy_bus_ = new dbus::Bus(options);
75 CHECK(proxy_bus_->Connect());
76 }
77
~BinderControl()78 BinderControl::~BinderControl() {
79 // TODO(samueltan): remove when shill is no longer dependent on DBus proxies.
80 if (proxy_bus_) {
81 proxy_bus_->ShutdownAndBlock();
82 }
83 }
84
NullRPCIdentifier()85 const string& BinderControl::NullRPCIdentifier() { return null_identifier_; }
86
RegisterManagerObject(Manager * manager,const base::Closure & registration_done_callback)87 void BinderControl::RegisterManagerObject(
88 Manager* manager, const base::Closure& registration_done_callback) {
89 // Binder manager object registration is performed synchronously, and
90 // ManagerBinderAdaptor::RegisterAsync does not
91 // actually use the callback passed to it. However, since the caller of this
92 // function expects |registration_done_callback| to be called asynchronously,
93 // post the callback to the message loop ourselves.
94 manager->RegisterAsync(base::Callback<void(bool)>());
95 dispatcher_->PostTask(registration_done_callback);
96 }
97
CreateDeviceAdaptor(Device * device)98 DeviceAdaptorInterface* BinderControl::CreateDeviceAdaptor(Device* device) {
99 return CreateAdaptor<Device, DeviceAdaptorInterface, DeviceBinderAdaptor>(
100 device);
101 }
102
CreateIPConfigAdaptor(IPConfig * config)103 IPConfigAdaptorInterface* BinderControl::CreateIPConfigAdaptor(
104 IPConfig* config) {
105 return new IPConfigAdaptorStub(to_string(next_unique_binder_adaptor_id_++));
106 }
107
CreateManagerAdaptor(Manager * manager)108 ManagerAdaptorInterface* BinderControl::CreateManagerAdaptor(Manager* manager) {
109 return CreateAdaptor<Manager, ManagerAdaptorInterface, ManagerBinderAdaptor>(
110 manager);
111 }
112
CreateProfileAdaptor(Profile * profile)113 ProfileAdaptorInterface* BinderControl::CreateProfileAdaptor(Profile* profile) {
114 return new ProfileAdaptorStub(to_string(next_unique_binder_adaptor_id_++));
115 }
116
CreateRPCTaskAdaptor(RPCTask * task)117 RPCTaskAdaptorInterface* BinderControl::CreateRPCTaskAdaptor(RPCTask* task) {
118 return new RPCTaskAdaptorStub(to_string(next_unique_binder_adaptor_id_++));
119 }
120
CreateServiceAdaptor(Service * service)121 ServiceAdaptorInterface* BinderControl::CreateServiceAdaptor(Service* service) {
122 return CreateAdaptor<Service, ServiceAdaptorInterface, ServiceBinderAdaptor>(
123 service);
124 }
125
126 #ifndef DISABLE_VPN
CreateThirdPartyVpnAdaptor(ThirdPartyVpnDriver * driver)127 ThirdPartyVpnAdaptorInterface* BinderControl::CreateThirdPartyVpnAdaptor(
128 ThirdPartyVpnDriver* driver) {
129 return new ThirdPartyVpnAdaptorStub(
130 to_string(next_unique_binder_adaptor_id_++));
131 }
132 #endif
133
CreatePowerManagerProxy(PowerManagerProxyDelegate * delegate,const base::Closure & service_appeared_callback,const base::Closure & service_vanished_callback)134 PowerManagerProxyInterface* BinderControl::CreatePowerManagerProxy(
135 PowerManagerProxyDelegate* delegate,
136 const base::Closure& service_appeared_callback,
137 const base::Closure& service_vanished_callback) {
138 return new PowerManagerProxyStub();
139 }
140
141 #if !defined(DISABLE_WIFI) || !defined(DISABLE_WIRED_8021X)
CreateSupplicantProcessProxy(const base::Closure & service_appeared_callback,const base::Closure & service_vanished_callback)142 SupplicantProcessProxyInterface* BinderControl::CreateSupplicantProcessProxy(
143 const base::Closure& service_appeared_callback,
144 const base::Closure& service_vanished_callback) {
145 return new ChromeosSupplicantProcessProxy(dispatcher_, proxy_bus_,
146 service_appeared_callback,
147 service_vanished_callback);
148 }
149
150 SupplicantInterfaceProxyInterface*
CreateSupplicantInterfaceProxy(SupplicantEventDelegateInterface * delegate,const string & object_path)151 BinderControl::CreateSupplicantInterfaceProxy(
152 SupplicantEventDelegateInterface* delegate, const string& object_path) {
153 return new ChromeosSupplicantInterfaceProxy(proxy_bus_, object_path,
154 delegate);
155 }
156
CreateSupplicantNetworkProxy(const string & object_path)157 SupplicantNetworkProxyInterface* BinderControl::CreateSupplicantNetworkProxy(
158 const string& object_path) {
159 return new ChromeosSupplicantNetworkProxy(proxy_bus_, object_path);
160 }
161 #endif // DISABLE_WIFI || DISABLE_WIRED_8021X
162
163 #if !defined(DISABLE_WIFI)
CreateSupplicantBSSProxy(WiFiEndpoint * wifi_endpoint,const string & object_path)164 SupplicantBSSProxyInterface* BinderControl::CreateSupplicantBSSProxy(
165 WiFiEndpoint* wifi_endpoint, const string& object_path) {
166 return new ChromeosSupplicantBSSProxy(proxy_bus_, object_path, wifi_endpoint);
167 }
168 #endif // DISABLE_WIFI
169
CreateDHCPCDListener(DHCPProvider * provider)170 DHCPCDListenerInterface* BinderControl::CreateDHCPCDListener(
171 DHCPProvider* provider) {
172 return new ChromeosDHCPCDListener(proxy_bus_, dispatcher_, provider);
173 }
174
CreateDHCPProxy(const string & service)175 DHCPProxyInterface* BinderControl::CreateDHCPProxy(const string& service) {
176 return new ChromeosDHCPCDProxy(proxy_bus_, service);
177 }
178
CreateUpstartProxy()179 UpstartProxyInterface* BinderControl::CreateUpstartProxy() {
180 return new UpstartProxyStub();
181 }
182
CreateFirewallProxy()183 FirewallProxyInterface* BinderControl::CreateFirewallProxy() {
184 return new ChromeosFirewalldProxy(proxy_bus_);
185 }
186
187 template <typename Object, typename AdaptorInterface, typename Adaptor>
CreateAdaptor(Object * object)188 AdaptorInterface* BinderControl::CreateAdaptor(Object* object) {
189 return new Adaptor(object, to_string(next_unique_binder_adaptor_id_++));
190 }
191
192 } // namespace shill
193