2 // Copyright (C) 2015 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "service/ipc/binder/IBluetooth.h"
19 #include <base/logging.h>
20 #include <binder/IServiceManager.h>
21 #include <binder/Parcel.h>
23 using android::defaultServiceManager;
24 using android::IBinder;
25 using android::interface_cast;
26 using android::IServiceManager;
27 using android::Parcel;
29 using android::String16;
35 const char IBluetooth::kBluetoothServiceName[] = "bluetooth-service";
38 sp<IBluetooth> IBluetooth::getClientInterface() {
39 sp<IServiceManager> sm = defaultServiceManager();
41 LOG(ERROR) << "Failed to obtain a handle to the default Service Manager";
45 sp<IBinder> binder = sm->getService(String16(kBluetoothServiceName));
47 LOG(ERROR) << "Failed to obtain a handle to the Bluetooth service";
51 sp<IBluetooth> bt_iface = interface_cast<IBluetooth>(binder);
52 if (!bt_iface.get()) {
53 LOG(ERROR) << "Obtained invalid IBinder handle";
60 // BnBluetooth (server) implementation
61 // ========================================================
63 android::status_t BnBluetooth::onTransact(
68 VLOG(2) << "IBluetooth transaction: " << code;
69 if (!data.checkInterface(this))
70 return android::PERMISSION_DENIED;
73 case IS_ENABLED_TRANSACTION: {
74 bool is_enabled = IsEnabled();
75 reply->writeInt32(is_enabled);
76 return android::NO_ERROR;
78 case GET_STATE_TRANSACTION: {
79 int state = GetState();
80 reply->writeInt32(state);
81 return android::NO_ERROR;
83 case ENABLE_TRANSACTION: {
84 bool result = Enable();
85 reply->writeInt32(result);
86 return android::NO_ERROR;
88 case DISABLE_TRANSACTION: {
89 bool result = Disable();
90 reply->writeInt32(result);
91 return android::NO_ERROR;
93 case GET_ADDRESS_TRANSACTION: {
94 std::string address = GetAddress();
95 reply->writeCString(address.c_str());
96 return android::NO_ERROR;
98 case GET_UUIDS_TRANSACTION:
99 // TODO(armansito): Figure out how to handle a Java "ParcelUuid" natively.
100 // (see http://b/23316698).
101 return android::INVALID_OPERATION;
103 case SET_NAME_TRANSACTION: {
104 std::string name(data.readCString());
105 bool result = SetName(name);
106 reply->writeInt32(result);
107 return android::NO_ERROR;
109 case GET_NAME_TRANSACTION: {
110 std::string name = GetName();
111 reply->writeCString(name.c_str());
112 return android::NO_ERROR;
114 case REGISTER_CALLBACK_TRANSACTION: {
115 sp<IBinder> callback = data.readStrongBinder();
116 RegisterCallback(interface_cast<IBluetoothCallback>(callback));
117 return android::NO_ERROR;
119 case UNREGISTER_CALLBACK_TRANSACTION: {
120 sp<IBinder> callback = data.readStrongBinder();
121 UnregisterCallback(interface_cast<IBluetoothCallback>(callback));
122 return android::NO_ERROR;
124 case IS_MULTI_ADVERTISEMENT_SUPPORTED_TRANSACTION: {
125 bool result = IsMultiAdvertisementSupported();
126 reply->writeInt32(result);
127 return android::NO_ERROR;
130 return BBinder::onTransact(code, data, reply, flags);
134 // BpBluetooth (client) implementation
135 // ========================================================
137 BpBluetooth::BpBluetooth(const sp<IBinder>& impl)
138 : BpInterface<IBluetooth>(impl) {
141 bool BpBluetooth::IsEnabled() {
144 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
145 remote()->transact(IBluetooth::IS_ENABLED_TRANSACTION, data, &reply);
147 return reply.readInt32();
150 int BpBluetooth::GetState() {
153 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
154 remote()->transact(IBluetooth::GET_STATE_TRANSACTION, data, &reply);
156 return reply.readInt32();
159 bool BpBluetooth::Enable() {
162 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
163 remote()->transact(IBluetooth::ENABLE_TRANSACTION, data, &reply);
165 return reply.readInt32();
168 bool BpBluetooth::EnableNoAutoConnect() {
171 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
172 remote()->transact(IBluetooth::ENABLE_NO_AUTO_CONNECT_TRANSACTION,
175 return reply.readInt32();
178 bool BpBluetooth::Disable() {
181 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
182 remote()->transact(IBluetooth::DISABLE_TRANSACTION, data, &reply);
184 return reply.readInt32();
187 std::string BpBluetooth::GetAddress() {
190 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
191 remote()->transact(IBluetooth::GET_ADDRESS_TRANSACTION, data, &reply);
193 return reply.readCString();
196 std::vector<bluetooth::UUID> BpBluetooth::GetUUIDs() {
197 // TODO(armansito): need to deserialize a parceled java.util.ParcelUUID[] to
198 // std::vector<bluetooth::UUID> here (see http://b/23316698).
199 return std::vector<bluetooth::UUID>();
202 bool BpBluetooth::SetName(const std::string& name) {
205 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
206 data.writeCString(name.c_str());
207 remote()->transact(IBluetooth::SET_NAME_TRANSACTION, data, &reply);
209 return reply.readInt32();
212 std::string BpBluetooth::GetName() {
215 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
216 remote()->transact(IBluetooth::GET_NAME_TRANSACTION, data, &reply);
218 return reply.readCString();
221 void BpBluetooth::RegisterCallback(const sp<IBluetoothCallback>& callback) {
224 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
225 data.writeStrongBinder(IInterface::asBinder(callback.get()));
227 remote()->transact(IBluetooth::REGISTER_CALLBACK_TRANSACTION, data, &reply);
230 void BpBluetooth::UnregisterCallback(const sp<IBluetoothCallback>& callback) {
233 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
234 data.writeStrongBinder(IInterface::asBinder(callback.get()));
236 remote()->transact(IBluetooth::UNREGISTER_CALLBACK_TRANSACTION, data, &reply);
239 bool BpBluetooth::IsMultiAdvertisementSupported() {
242 data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
244 remote()->transact(IBluetooth::IS_MULTI_ADVERTISEMENT_SUPPORTED_TRANSACTION,
247 return reply.readInt32();
250 IMPLEMENT_META_INTERFACE(Bluetooth, IBluetooth::kBluetoothServiceName);
252 } // namespace binder