OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / service / ipc / binder / IBluetooth.cpp
1 //
2 //  Copyright (C) 2015 Google, Inc.
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/binder/IBluetooth.h"
18
19 #include <base/logging.h>
20 #include <binder/IServiceManager.h>
21 #include <binder/Parcel.h>
22
23 using android::defaultServiceManager;
24 using android::IBinder;
25 using android::interface_cast;
26 using android::IServiceManager;
27 using android::Parcel;
28 using android::sp;
29 using android::String16;
30
31 namespace ipc {
32 namespace binder {
33
34 // static
35 const char IBluetooth::kBluetoothServiceName[] = "bluetooth-service";
36
37 // static
38 sp<IBluetooth> IBluetooth::getClientInterface() {
39   sp<IServiceManager> sm = defaultServiceManager();
40   if (!sm.get()) {
41     LOG(ERROR) << "Failed to obtain a handle to the default Service Manager";
42     return nullptr;
43   }
44
45   sp<IBinder> binder = sm->getService(String16(kBluetoothServiceName));
46   if (!binder.get()) {
47     LOG(ERROR) << "Failed to obtain a handle to the Bluetooth service";
48     return nullptr;
49   }
50
51   sp<IBluetooth> bt_iface = interface_cast<IBluetooth>(binder);
52   if (!bt_iface.get()) {
53     LOG(ERROR) << "Obtained invalid IBinder handle";
54     return nullptr;
55   }
56
57   return bt_iface;
58 }
59
60 // BnBluetooth (server) implementation
61 // ========================================================
62
63 android::status_t BnBluetooth::onTransact(
64     uint32_t code,
65     const Parcel& data,
66     Parcel* reply,
67     uint32_t flags) {
68   VLOG(2) << "IBluetooth transaction: " << code;
69   if (!data.checkInterface(this))
70     return android::PERMISSION_DENIED;
71
72   switch (code) {
73     case IS_ENABLED_TRANSACTION: {
74       bool is_enabled = IsEnabled();
75       reply->writeInt32(is_enabled);
76       return android::NO_ERROR;
77     }
78     case GET_STATE_TRANSACTION: {
79       int state = GetState();
80       reply->writeInt32(state);
81       return android::NO_ERROR;
82     }
83     case ENABLE_TRANSACTION: {
84       bool result = Enable();
85       reply->writeInt32(result);
86       return android::NO_ERROR;
87     }
88     case DISABLE_TRANSACTION: {
89       bool result = Disable();
90       reply->writeInt32(result);
91       return android::NO_ERROR;
92     }
93     case GET_ADDRESS_TRANSACTION: {
94       std::string address = GetAddress();
95       reply->writeCString(address.c_str());
96       return android::NO_ERROR;
97     }
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;
102
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;
108     }
109     case GET_NAME_TRANSACTION: {
110       std::string name = GetName();
111       reply->writeCString(name.c_str());
112       return android::NO_ERROR;
113     }
114     case REGISTER_CALLBACK_TRANSACTION: {
115       sp<IBinder> callback = data.readStrongBinder();
116       RegisterCallback(interface_cast<IBluetoothCallback>(callback));
117       return android::NO_ERROR;
118     }
119     case UNREGISTER_CALLBACK_TRANSACTION: {
120       sp<IBinder> callback = data.readStrongBinder();
121       UnregisterCallback(interface_cast<IBluetoothCallback>(callback));
122       return android::NO_ERROR;
123     }
124     case IS_MULTI_ADVERTISEMENT_SUPPORTED_TRANSACTION: {
125       bool result = IsMultiAdvertisementSupported();
126       reply->writeInt32(result);
127       return android::NO_ERROR;
128     }
129     default:
130       return BBinder::onTransact(code, data, reply, flags);
131   }
132 }
133
134 // BpBluetooth (client) implementation
135 // ========================================================
136
137 BpBluetooth::BpBluetooth(const sp<IBinder>& impl)
138     : BpInterface<IBluetooth>(impl) {
139 }
140
141 bool BpBluetooth::IsEnabled() {
142   Parcel data, reply;
143
144   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
145   remote()->transact(IBluetooth::IS_ENABLED_TRANSACTION, data, &reply);
146
147   return reply.readInt32();
148 }
149
150 int BpBluetooth::GetState() {
151   Parcel data, reply;
152
153   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
154   remote()->transact(IBluetooth::GET_STATE_TRANSACTION, data, &reply);
155
156   return reply.readInt32();
157 }
158
159 bool BpBluetooth::Enable() {
160   Parcel data, reply;
161
162   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
163   remote()->transact(IBluetooth::ENABLE_TRANSACTION, data, &reply);
164
165   return reply.readInt32();
166 }
167
168 bool BpBluetooth::EnableNoAutoConnect() {
169   Parcel data, reply;
170
171   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
172   remote()->transact(IBluetooth::ENABLE_NO_AUTO_CONNECT_TRANSACTION,
173                      data, &reply);
174
175   return reply.readInt32();
176 }
177
178 bool BpBluetooth::Disable() {
179   Parcel data, reply;
180
181   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
182   remote()->transact(IBluetooth::DISABLE_TRANSACTION, data, &reply);
183
184   return reply.readInt32();
185 }
186
187 std::string BpBluetooth::GetAddress() {
188   Parcel data, reply;
189
190   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
191   remote()->transact(IBluetooth::GET_ADDRESS_TRANSACTION, data, &reply);
192
193   return reply.readCString();
194 }
195
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>();
200 }
201
202 bool BpBluetooth::SetName(const std::string& name) {
203   Parcel data, reply;
204
205   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
206   data.writeCString(name.c_str());
207   remote()->transact(IBluetooth::SET_NAME_TRANSACTION, data, &reply);
208
209   return reply.readInt32();
210 }
211
212 std::string BpBluetooth::GetName() {
213   Parcel data, reply;
214
215   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
216   remote()->transact(IBluetooth::GET_NAME_TRANSACTION, data, &reply);
217
218   return reply.readCString();
219 }
220
221 void BpBluetooth::RegisterCallback(const sp<IBluetoothCallback>& callback) {
222   Parcel data, reply;
223
224   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
225   data.writeStrongBinder(IInterface::asBinder(callback.get()));
226
227   remote()->transact(IBluetooth::REGISTER_CALLBACK_TRANSACTION, data, &reply);
228 }
229
230 void BpBluetooth::UnregisterCallback(const sp<IBluetoothCallback>& callback) {
231   Parcel data, reply;
232
233   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
234   data.writeStrongBinder(IInterface::asBinder(callback.get()));
235
236   remote()->transact(IBluetooth::UNREGISTER_CALLBACK_TRANSACTION, data, &reply);
237 }
238
239 bool BpBluetooth::IsMultiAdvertisementSupported() {
240   Parcel data, reply;
241
242   data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
243
244   remote()->transact(IBluetooth::IS_MULTI_ADVERTISEMENT_SUPPORTED_TRANSACTION,
245                      data, &reply);
246
247   return reply.readInt32();
248 }
249
250 IMPLEMENT_META_INTERFACE(Bluetooth, IBluetooth::kBluetoothServiceName);
251
252 }  // namespace binder
253 }  // namespace ipc