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/IBluetoothLowEnergyCallback.h"
18
19 #include <base/logging.h>
20 #include <binder/Parcel.h>
21
22 #include "service/common/bluetooth/binder/parcel_helpers.h"
23
24 using android::IBinder;
25 using android::Parcel;
26 using android::sp;
27 using android::status_t;
28
29 using bluetooth::AdvertiseData;
30 using bluetooth::AdvertiseSettings;
31
32 namespace ipc {
33 namespace binder {
34
35 // static
36 const char IBluetoothLowEnergyCallback::kServiceName[] =
37 "bluetooth-low-energy-callback-service";
38
39 // BnBluetoothLowEnergyCallback (server) implementation
40 // ========================================================
41
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)42 status_t BnBluetoothLowEnergyCallback::onTransact(
43 uint32_t code,
44 const Parcel& data,
45 Parcel* reply,
46 uint32_t flags) {
47 VLOG(2) << "IBluetoothLowEnergyCallback: " << code;
48 if (!data.checkInterface(this))
49 return android::PERMISSION_DENIED;
50
51 switch (code) {
52 case ON_CLIENT_REGISTERED_TRANSACTION: {
53 int status = data.readInt32();
54 int client_if = data.readInt32();
55 OnClientRegistered(status, client_if);
56 return android::NO_ERROR;
57 }
58 case ON_CONNECTION_STATE_TRANSACTION: {
59 int status = data.readInt32();
60 int client_id = data.readInt32();
61 const char* address = data.readCString();
62 bool connected = data.readBool();
63
64 OnConnectionState(status, client_id, address, connected);
65 return android::NO_ERROR;
66 }
67 case ON_MTU_CHANGED_TRANSACTION: {
68 int status = data.readInt32();
69 const char *address = data.readCString();
70 int mtu = data.readInt32();
71
72 OnMtuChanged(status, address, mtu);
73 return android::NO_ERROR;
74 }
75 case ON_SCAN_RESULT_TRANSACTION: {
76 auto scan_result = CreateScanResultFromParcel(data);
77 CHECK(scan_result.get());
78 OnScanResult(*scan_result);
79 return android::NO_ERROR;
80 }
81 case ON_MULTI_ADVERTISE_CALLBACK_TRANSACTION: {
82 int status = data.readInt32();
83 bool is_start = data.readInt32();
84 std::unique_ptr<AdvertiseSettings> settings =
85 CreateAdvertiseSettingsFromParcel(data);
86 OnMultiAdvertiseCallback(status, is_start, *settings);
87 return android::NO_ERROR;
88 }
89 default:
90 return BBinder::onTransact(code, data, reply, flags);
91 }
92 }
93
94 // BpBluetoothLowEnergyCallback (client) implementation
95 // ========================================================
96
BpBluetoothLowEnergyCallback(const sp<IBinder> & impl)97 BpBluetoothLowEnergyCallback::BpBluetoothLowEnergyCallback(
98 const sp<IBinder>& impl)
99 : BpInterface<IBluetoothLowEnergyCallback>(impl) {
100 }
101
OnClientRegistered(int status,int client_if)102 void BpBluetoothLowEnergyCallback::OnClientRegistered(
103 int status, int client_if) {
104 Parcel data, reply;
105
106 data.writeInterfaceToken(
107 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
108 data.writeInt32(status);
109 data.writeInt32(client_if);
110
111 remote()->transact(
112 IBluetoothLowEnergyCallback::ON_CLIENT_REGISTERED_TRANSACTION,
113 data, &reply,
114 IBinder::FLAG_ONEWAY);
115 }
116
OnConnectionState(int status,int client_id,const char * address,bool connected)117 void BpBluetoothLowEnergyCallback::OnConnectionState(
118 int status, int client_id, const char* address, bool connected) {
119
120 Parcel data;
121
122 data.writeInterfaceToken(
123 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
124 data.writeInt32(status);
125 data.writeInt32(client_id);
126 data.writeCString(address);
127 data.writeBool(connected);
128
129 remote()->transact(
130 IBluetoothLowEnergyCallback::ON_CONNECTION_STATE_TRANSACTION,
131 data, NULL,
132 IBinder::FLAG_ONEWAY);
133 }
134
OnMtuChanged(int status,const char * address,int mtu)135 void BpBluetoothLowEnergyCallback::OnMtuChanged(
136 int status, const char* address, int mtu) {
137 Parcel data;
138
139 data.writeInterfaceToken(
140 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
141 data.writeInt32(status);
142 data.writeCString(address);
143 data.writeInt32(mtu);
144
145 remote()->transact(
146 IBluetoothLowEnergyCallback::ON_MTU_CHANGED_TRANSACTION,
147 data, NULL,
148 IBinder::FLAG_ONEWAY);
149 }
150
OnScanResult(const bluetooth::ScanResult & scan_result)151 void BpBluetoothLowEnergyCallback::OnScanResult(
152 const bluetooth::ScanResult& scan_result) {
153 Parcel data, reply;
154
155 data.writeInterfaceToken(
156 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
157 WriteScanResultToParcel(scan_result, &data);
158
159 remote()->transact(
160 IBluetoothLowEnergyCallback::ON_SCAN_RESULT_TRANSACTION,
161 data, &reply,
162 IBinder::FLAG_ONEWAY);
163 }
164
OnMultiAdvertiseCallback(int status,bool is_start,const AdvertiseSettings & settings)165 void BpBluetoothLowEnergyCallback::OnMultiAdvertiseCallback(
166 int status, bool is_start,
167 const AdvertiseSettings& settings) {
168 Parcel data, reply;
169
170 data.writeInterfaceToken(
171 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
172 data.writeInt32(status);
173 data.writeInt32(is_start);
174 WriteAdvertiseSettingsToParcel(settings, &data);
175
176 remote()->transact(
177 IBluetoothLowEnergyCallback::ON_MULTI_ADVERTISE_CALLBACK_TRANSACTION,
178 data, &reply,
179 IBinder::FLAG_ONEWAY);
180 }
181
182 IMPLEMENT_META_INTERFACE(BluetoothLowEnergyCallback,
183 IBluetoothLowEnergyCallback::kServiceName);
184
185 } // namespace binder
186 } // namespace ipc
187