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/common/bluetooth/binder/IBluetoothLowEnergy.h"
19 #include <base/logging.h>
20 #include <binder/Parcel.h>
22 #include "service/common/bluetooth/binder/parcel_helpers.h"
24 using android::IBinder;
25 using android::interface_cast;
26 using android::Parcel;
28 using android::status_t;
30 using bluetooth::AdvertiseData;
31 using bluetooth::AdvertiseSettings;
37 const char IBluetoothLowEnergy::kServiceName[] =
38 "bluetooth-low-energy-service";
40 // BnBluetoothLowEnergy (server) implementation
41 // ========================================================
43 status_t BnBluetoothLowEnergy::onTransact(
44 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
45 VLOG(2) << "IBluetoothLowEnergy: " << code;
46 if (!data.checkInterface(this))
47 return android::PERMISSION_DENIED;
50 case REGISTER_CLIENT_TRANSACTION: {
51 sp<IBinder> callback = data.readStrongBinder();
52 bool result = RegisterClient(
53 interface_cast<IBluetoothLowEnergyCallback>(callback));
55 reply->writeInt32(result);
57 return android::NO_ERROR;
59 case UNREGISTER_CLIENT_TRANSACTION: {
60 int client_id = data.readInt32();
61 UnregisterClient(client_id);
62 return android::NO_ERROR;
64 case UNREGISTER_ALL_TRANSACTION: {
66 return android::NO_ERROR;
68 case CONNECT_TRANSACTION: {
69 int client_id = data.readInt32();
70 const char* address = data.readCString();
71 bool is_direct = data.readBool();
73 bool result = Connect(client_id, address, is_direct);
74 reply->writeInt32(result);
76 return android::NO_ERROR;
78 case DISCONNECT_TRANSACTION: {
79 int client_id = data.readInt32();
80 const char* address = data.readCString();
82 bool result = Disconnect(client_id, address);
83 reply->writeInt32(result);
85 return android::NO_ERROR;
87 case SET_MTU_TRANSACTION: {
88 int client_id = data.readInt32();
89 const char* address = data.readCString();
90 int mtu = data.readInt32();
92 bool result = SetMtu(client_id, address, mtu);
93 reply->writeInt32(result);
95 return android::NO_ERROR;
97 case START_SCAN_TRANSACTION: {
98 int client_id = data.readInt32();
99 auto settings = CreateScanSettingsFromParcel(data);
101 std::vector<bluetooth::ScanFilter> filters;
103 int list_meta_data = data.readInt32();
104 CHECK(list_meta_data == kParcelValList);
106 int filter_count = data.readInt32();
107 if (filter_count >= 0) { // Make sure |filter_count| isn't negative.
108 for (int i = 0; i < filter_count; i++) {
109 auto filter = CreateScanFilterFromParcel(data);
111 filters.push_back(*filter);
115 bool result = StartScan(client_id, *settings, filters);
116 reply->writeInt32(result);
118 return android::NO_ERROR;
120 case STOP_SCAN_TRANSACTION: {
121 int client_id = data.readInt32();
122 bool result = StopScan(client_id);
123 reply->writeInt32(result);
124 return android::NO_ERROR;
126 case START_MULTI_ADVERTISING_TRANSACTION: {
127 int client_id = data.readInt32();
128 std::unique_ptr<AdvertiseData> adv_data =
129 CreateAdvertiseDataFromParcel(data);
130 std::unique_ptr<AdvertiseData> scan_rsp =
131 CreateAdvertiseDataFromParcel(data);
132 std::unique_ptr<AdvertiseSettings> adv_settings =
133 CreateAdvertiseSettingsFromParcel(data);
135 bool result = StartMultiAdvertising(
136 client_id, *adv_data, *scan_rsp, *adv_settings);
138 reply->writeInt32(result);
140 return android::NO_ERROR;
142 case STOP_MULTI_ADVERTISING_TRANSACTION: {
143 int client_id = data.readInt32();
144 bool result = StopMultiAdvertising(client_id);
146 reply->writeInt32(result);
148 return android::NO_ERROR;
151 return BBinder::onTransact(code, data, reply, flags);
155 // BpBluetoothLowEnergy (client) implementation
156 // ========================================================
158 BpBluetoothLowEnergy::BpBluetoothLowEnergy(const sp<IBinder>& impl)
159 : BpInterface<IBluetoothLowEnergy>(impl) {
162 bool BpBluetoothLowEnergy::RegisterClient(
163 const sp<IBluetoothLowEnergyCallback>& callback) {
166 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
167 data.writeStrongBinder(IInterface::asBinder(callback.get()));
169 remote()->transact(IBluetoothLowEnergy::REGISTER_CLIENT_TRANSACTION,
172 return reply.readInt32();
175 void BpBluetoothLowEnergy::UnregisterClient(int client_id) {
178 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
179 data.writeInt32(client_id);
181 remote()->transact(IBluetoothLowEnergy::UNREGISTER_CLIENT_TRANSACTION,
185 void BpBluetoothLowEnergy::UnregisterAll() {
188 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
190 remote()->transact(IBluetoothLowEnergy::UNREGISTER_ALL_TRANSACTION,
194 bool BpBluetoothLowEnergy::Connect(int client_id, const char* address,
198 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
199 data.writeInt32(client_id);
200 data.writeCString(address);
201 data.writeBool(is_direct);
203 remote()->transact(IBluetoothLowEnergy::CONNECT_TRANSACTION,
206 return reply.readInt32();
209 bool BpBluetoothLowEnergy::Disconnect(int client_id, const char* address) {
212 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
213 data.writeInt32(client_id);
214 data.writeCString(address);
216 remote()->transact(IBluetoothLowEnergy::DISCONNECT_TRANSACTION,
219 return reply.readInt32();
222 bool BpBluetoothLowEnergy::SetMtu(int client_id, const char* address, int mtu) {
225 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
226 data.writeInt32(client_id);
227 data.writeCString(address);
228 data.writeInt32(mtu);
230 remote()->transact(IBluetoothLowEnergy::SET_MTU_TRANSACTION, data, &reply);
231 return reply.readInt32();
234 bool BpBluetoothLowEnergy::StartScan(
236 const bluetooth::ScanSettings& settings,
237 const std::vector<bluetooth::ScanFilter>& filters) {
240 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
241 data.writeInt32(client_id);
242 WriteScanSettingsToParcel(settings, &data);
244 // The Java equivalent of |filters| is a List<ScanFilter>. Parcel.java inserts
245 // a metadata value of VAL_LIST (11) for this so I'm doing it here for
247 data.writeInt32(kParcelValList);
248 data.writeInt32(filters.size());
249 for (const auto& filter : filters)
250 WriteScanFilterToParcel(filter, &data);
252 remote()->transact(IBluetoothLowEnergy::START_SCAN_TRANSACTION,
255 return reply.readInt32();
258 bool BpBluetoothLowEnergy::StopScan(int client_id) {
261 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
262 data.writeInt32(client_id);
264 remote()->transact(IBluetoothLowEnergy::STOP_SCAN_TRANSACTION,
267 return reply.readInt32();
270 bool BpBluetoothLowEnergy::StartMultiAdvertising(
272 const AdvertiseData& advertise_data,
273 const AdvertiseData& scan_response,
274 const AdvertiseSettings& settings) {
277 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
278 data.writeInt32(client_id);
279 WriteAdvertiseDataToParcel(advertise_data, &data);
280 WriteAdvertiseDataToParcel(scan_response, &data);
281 WriteAdvertiseSettingsToParcel(settings, &data);
283 remote()->transact(IBluetoothLowEnergy::START_MULTI_ADVERTISING_TRANSACTION,
286 return reply.readInt32();
289 bool BpBluetoothLowEnergy::StopMultiAdvertising(int client_id) {
292 data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor());
293 data.writeInt32(client_id);
295 remote()->transact(IBluetoothLowEnergy::STOP_MULTI_ADVERTISING_TRANSACTION,
298 return reply.readInt32();
301 IMPLEMENT_META_INTERFACE(BluetoothLowEnergy, IBluetoothLowEnergy::kServiceName);
303 } // namespace binder