aboutsummaryrefslogtreecommitdiff
path: root/service/ipc/dbus/bluetooth_adapter.cc
blob: 6831c1b9320f58606c1ae11e4f804e74aab9d8f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
//  Copyright 2016 The Android Open Source Project
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#include "service/ipc/dbus/bluetooth_adapter.h"
#include <base/files/file_util.h>
#include <base/logging.h>
#include "service/hal/bluetooth_interface.h"

using ::dbus::Bus;
using ::dbus::ExportedObject;
using ::dbus::MethodCall;
using ::dbus::MessageWriter;
using ::dbus::Response;
using ::dbus::ObjectPath;
using ::dbus::ErrorResponse;

namespace {

const std::string kBluetoothAdapterInterface = "org.fluoride.BluetoothAdapter";
const std::string kEnable = "Enable";
const std::string kDisable = "Disable";
const std::string kBluetoothAdapter = "org.fluoride.BluetoothAdapter";
const std::string kBluetoothAdapterPath = "/org/fluoride/BluetoothAdapter";

// TODO(jpawlowski): right now xml interface files are in service/ipc/dbus/
// folder.  Make a script to move them into /usr/share/dbus-1/interfaces
const char kBindingsPath[] =
    "/usr/share/dbus-1/interfaces/org.fluoride.BluetoothAdapter.xml";
const char kDBusIntrospectMethod[] = "Introspect";

}  // namespace

namespace ipc {
namespace dbus {

#if defined(BASE_VER) && BASE_VER > 780000
// New libchrome treats ResponseSender as base::OnceCallback so we need to move
// ownership before calling ::Run()
inline void RunResponse(std::unique_ptr<Response> response,
                        ExportedObject::ResponseSender& response_sender) {
  std::move(response_sender).Run(std::move(response));
}
#else
inline void RunResponse(std::unique_ptr<Response> response,
                        ExportedObject::ResponseSender& response_sender) {
  response_sender.Run(std::move(response));
}
#endif

BluetoothAdapter::BluetoothAdapter(scoped_refptr<Bus> bus,
                                   bluetooth::Adapter* adapter)
    : adapter_(adapter) {
  exported_object_ = bus->GetExportedObject(ObjectPath(kBluetoothAdapterPath));

  CHECK(exported_object_->ExportMethodAndBlock(
      kBluetoothAdapterInterface, kEnable,
      base::Bind(&BluetoothAdapter::Enable, base::Unretained(this))));

  CHECK(exported_object_->ExportMethodAndBlock(
      kBluetoothAdapterInterface, kDisable,
      base::Bind(&BluetoothAdapter::Disable, base::Unretained(this))));

  CHECK(exported_object_->ExportMethodAndBlock(
      DBUS_INTERFACE_INTROSPECTABLE, kDBusIntrospectMethod,
      base::Bind(&BluetoothAdapter::Introspect, base::Unretained(this))));

  CHECK(bus->RequestOwnershipAndBlock(kBluetoothAdapter, Bus::REQUIRE_PRIMARY))
      << "Unable to take ownership of " << kBluetoothAdapter
      << ". Make sure you have proper busconfig file "
         "/etc/dbus-1/system.d/org.fluoride.conf";
}

void BluetoothAdapter::Enable(MethodCall* method_call,
                              ExportedObject::ResponseSender response_sender) {
  VLOG(1) << __func__;
  adapter_->Enable();
  RunResponse(Response::FromMethodCall(method_call), response_sender);
}

void BluetoothAdapter::Disable(MethodCall* method_call,
                               ExportedObject::ResponseSender response_sender) {
  VLOG(1) << __func__;
  adapter_->Disable();
  RunResponse(Response::FromMethodCall(method_call), response_sender);
}

void BluetoothAdapter::Introspect(
    MethodCall* method_call, ExportedObject::ResponseSender response_sender) {
  VLOG(1) << __func__;

  std::string output;
  if (!base::ReadFileToString(base::FilePath(kBindingsPath), &output)) {
    PLOG(ERROR) << "Can't read XML bindings from disk:";
    RunResponse(ErrorResponse::FromMethodCall(
                    method_call, "Can't read XML bindings from disk.", ""),
                response_sender);
  }

  std::unique_ptr<Response> response(Response::FromMethodCall(method_call));
  MessageWriter writer(response.get());
  writer.AppendString(output);

  RunResponse(std::move(response), response_sender);
}

BluetoothAdapter::~BluetoothAdapter() {}

}  // namespace dbus
}  // namespace ipc