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/IBluetoothLowEnergyCallback.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::Parcel;
27 using android::status_t;
29 using bluetooth::AdvertiseData;
30 using bluetooth::AdvertiseSettings;
36 const char IBluetoothLowEnergyCallback::kServiceName[] =
37 "bluetooth-low-energy-callback-service";
39 // BnBluetoothLowEnergyCallback (server) implementation
40 // ========================================================
42 status_t BnBluetoothLowEnergyCallback::onTransact(
47 VLOG(2) << "IBluetoothLowEnergyCallback: " << code;
48 if (!data.checkInterface(this))
49 return android::PERMISSION_DENIED;
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;
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();
64 OnConnectionState(status, client_id, address, connected);
65 return android::NO_ERROR;
67 case ON_SCAN_RESULT_TRANSACTION: {
68 auto scan_result = CreateScanResultFromParcel(data);
69 CHECK(scan_result.get());
70 OnScanResult(*scan_result);
71 return android::NO_ERROR;
73 case ON_MULTI_ADVERTISE_CALLBACK_TRANSACTION: {
74 int status = data.readInt32();
75 bool is_start = data.readInt32();
76 std::unique_ptr<AdvertiseSettings> settings =
77 CreateAdvertiseSettingsFromParcel(data);
78 OnMultiAdvertiseCallback(status, is_start, *settings);
79 return android::NO_ERROR;
82 return BBinder::onTransact(code, data, reply, flags);
86 // BpBluetoothLowEnergyCallback (client) implementation
87 // ========================================================
89 BpBluetoothLowEnergyCallback::BpBluetoothLowEnergyCallback(
90 const sp<IBinder>& impl)
91 : BpInterface<IBluetoothLowEnergyCallback>(impl) {
94 void BpBluetoothLowEnergyCallback::OnClientRegistered(
95 int status, int client_if) {
98 data.writeInterfaceToken(
99 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
100 data.writeInt32(status);
101 data.writeInt32(client_if);
104 IBluetoothLowEnergyCallback::ON_CLIENT_REGISTERED_TRANSACTION,
106 IBinder::FLAG_ONEWAY);
109 void BpBluetoothLowEnergyCallback::OnConnectionState(
110 int status, int client_id, const char* address, bool connected) {
114 data.writeInterfaceToken(
115 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
116 data.writeInt32(status);
117 data.writeInt32(client_id);
118 data.writeCString(address);
119 data.writeBool(connected);
122 IBluetoothLowEnergyCallback::ON_CONNECTION_STATE_TRANSACTION,
124 IBinder::FLAG_ONEWAY);
127 void BpBluetoothLowEnergyCallback::OnScanResult(
128 const bluetooth::ScanResult& scan_result) {
131 data.writeInterfaceToken(
132 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
133 WriteScanResultToParcel(scan_result, &data);
136 IBluetoothLowEnergyCallback::ON_SCAN_RESULT_TRANSACTION,
138 IBinder::FLAG_ONEWAY);
141 void BpBluetoothLowEnergyCallback::OnMultiAdvertiseCallback(
142 int status, bool is_start,
143 const AdvertiseSettings& settings) {
146 data.writeInterfaceToken(
147 IBluetoothLowEnergyCallback::getInterfaceDescriptor());
148 data.writeInt32(status);
149 data.writeInt32(is_start);
150 WriteAdvertiseSettingsToParcel(settings, &data);
153 IBluetoothLowEnergyCallback::ON_MULTI_ADVERTISE_CALLBACK_TRANSACTION,
155 IBinder::FLAG_ONEWAY);
158 IMPLEMENT_META_INTERFACE(BluetoothLowEnergyCallback,
159 IBluetoothLowEnergyCallback::kServiceName);
161 } // namespace binder