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/common/bluetooth/binder/IBluetoothCallback.h"
18 
19 #include <base/logging.h>
20 #include <binder/Parcel.h>
21 
22 using android::IBinder;
23 using android::Parcel;
24 using android::sp;
25 using android::status_t;
26 
27 namespace ipc {
28 namespace binder {
29 
30 // static
31 const char IBluetoothCallback::kServiceName[] =
32     "bluetooth-callback-service";
33 
34 // BnBluetoothCallback (server) implementation
35 // ========================================================
36 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)37 status_t BnBluetoothCallback::onTransact(
38     uint32_t code,
39     const Parcel& data,
40     Parcel* reply,
41     uint32_t flags) {
42   VLOG(2) << "IBluetoothCallback transaction: " << code;
43   if (!data.checkInterface(this))
44     return android::PERMISSION_DENIED;
45 
46   switch (code) {
47     case ON_BLUETOOTH_STATE_CHANGE_TRANSACTION: {
48       int prev_state, new_state;
49       if (data.readInt32(&prev_state) != android::NO_ERROR ||
50           data.readInt32(&new_state) != android::NO_ERROR)
51         return android::NOT_ENOUGH_DATA;
52 
53       OnBluetoothStateChange(
54           static_cast<bluetooth::AdapterState>(prev_state),
55           static_cast<bluetooth::AdapterState>(new_state));
56       return android::NO_ERROR;
57     }
58     default:
59       return BBinder::onTransact(code, data, reply, flags);
60   }
61 }
62 
63 // BpBluetoothCallback (client) implementation
64 // ========================================================
65 
BpBluetoothCallback(const sp<IBinder> & impl)66 BpBluetoothCallback::BpBluetoothCallback(const sp<IBinder>& impl)
67   : BpInterface<IBluetoothCallback>(impl) {
68 }
69 
OnBluetoothStateChange(bluetooth::AdapterState prev_state,bluetooth::AdapterState new_state)70 void BpBluetoothCallback::OnBluetoothStateChange(
71     bluetooth::AdapterState prev_state,
72     bluetooth::AdapterState new_state) {
73   Parcel data, reply;
74 
75   data.writeInterfaceToken(IBluetoothCallback::getInterfaceDescriptor());
76   data.writeInt32(prev_state);
77   data.writeInt32(new_state);
78 
79   remote()->transact(IBluetoothCallback::ON_BLUETOOTH_STATE_CHANGE_TRANSACTION,
80                      data, &reply);
81 }
82 
83 IMPLEMENT_META_INTERFACE(BluetoothCallback, IBluetoothCallback::kServiceName);
84 
85 }  // namespace binder
86 }  // namespace ipc
87