1 //
2 //  Copyright 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 "service/ipc/dbus/bluetooth_adapter.h"
18 #include <base/files/file_util.h>
19 #include <base/logging.h>
20 #include "service/hal/bluetooth_interface.h"
21 
22 using ::dbus::Bus;
23 using ::dbus::ExportedObject;
24 using ::dbus::MethodCall;
25 using ::dbus::MessageWriter;
26 using ::dbus::Response;
27 using ::dbus::ObjectPath;
28 using ::dbus::ErrorResponse;
29 
30 namespace {
31 
32 const std::string kBluetoothAdapterInterface = "org.fluoride.BluetoothAdapter";
33 const std::string kEnable = "Enable";
34 const std::string kDisable = "Disable";
35 const std::string kBluetoothAdapter = "org.fluoride.BluetoothAdapter";
36 const std::string kBluetoothAdapterPath = "/org/fluoride/BluetoothAdapter";
37 
38 // TODO(jpawlowski): right now xml interface files are in service/ipc/dbus/
39 // folder.  Make a script to move them into /usr/share/dbus-1/interfaces
40 const char kBindingsPath[] =
41     "/usr/share/dbus-1/interfaces/org.fluoride.BluetoothAdapter.xml";
42 const char kDBusIntrospectMethod[] = "Introspect";
43 
44 }  // namespace
45 
46 namespace ipc {
47 namespace dbus {
48 
BluetoothAdapter(scoped_refptr<Bus> bus,bluetooth::Adapter * adapter)49 BluetoothAdapter::BluetoothAdapter(scoped_refptr<Bus> bus,
50                                    bluetooth::Adapter* adapter)
51     : adapter_(adapter) {
52   exported_object_ = bus->GetExportedObject(ObjectPath(kBluetoothAdapterPath));
53 
54   CHECK(exported_object_->ExportMethodAndBlock(
55       kBluetoothAdapterInterface, kEnable,
56       base::Bind(&BluetoothAdapter::Enable, base::Unretained(this))));
57 
58   CHECK(exported_object_->ExportMethodAndBlock(
59       kBluetoothAdapterInterface, kDisable,
60       base::Bind(&BluetoothAdapter::Disable, base::Unretained(this))));
61 
62   CHECK(exported_object_->ExportMethodAndBlock(
63       DBUS_INTERFACE_INTROSPECTABLE, kDBusIntrospectMethod,
64       base::Bind(&BluetoothAdapter::Introspect, base::Unretained(this))));
65 
66   CHECK(bus->RequestOwnershipAndBlock(kBluetoothAdapter, Bus::REQUIRE_PRIMARY))
67       << "Unable to take ownership of " << kBluetoothAdapter
68       << ". Make sure you have proper busconfig file "
69          "/etc/dbus-1/system.d/org.fluoride.conf";
70 }
71 
Enable(MethodCall * method_call,ExportedObject::ResponseSender response_sender)72 void BluetoothAdapter::Enable(MethodCall* method_call,
73                               ExportedObject::ResponseSender response_sender) {
74   VLOG(1) << __func__;
75   adapter_->Enable(false);
76   response_sender.Run(Response::FromMethodCall(method_call));
77 }
78 
Disable(MethodCall * method_call,ExportedObject::ResponseSender response_sender)79 void BluetoothAdapter::Disable(MethodCall* method_call,
80                                ExportedObject::ResponseSender response_sender) {
81   VLOG(1) << __func__;
82   adapter_->Disable();
83   response_sender.Run(Response::FromMethodCall(method_call));
84 }
85 
Introspect(MethodCall * method_call,ExportedObject::ResponseSender response_sender)86 void BluetoothAdapter::Introspect(
87     MethodCall* method_call, ExportedObject::ResponseSender response_sender) {
88   VLOG(1) << __func__;
89 
90   std::string output;
91   if (!base::ReadFileToString(base::FilePath(kBindingsPath), &output)) {
92     PLOG(ERROR) << "Can't read XML bindings from disk:";
93     response_sender.Run(ErrorResponse::FromMethodCall(
94         method_call, "Can't read XML bindings from disk.", ""));
95   }
96   std::unique_ptr<Response> response(Response::FromMethodCall(method_call));
97   MessageWriter writer(response.get());
98   writer.AppendString(output);
99 
100   response_sender.Run(std::move(response));
101 }
102 
~BluetoothAdapter()103 BluetoothAdapter::~BluetoothAdapter() {}
104 
105 }  // namespace dbus
106 }  // namespace ipc
107