OSDN Git Service

e2b729299fa65a91a19b11e8c17b0d6125f592ac
[android-x86/system-bt.git] / service / common / bluetooth / binder / IBluetoothLowEnergyCallback.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/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
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_SCAN_RESULT_TRANSACTION: {
68     auto scan_result = CreateScanResultFromParcel(data);
69     CHECK(scan_result.get());
70     OnScanResult(*scan_result);
71     return android::NO_ERROR;
72   }
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;
80   }
81   default:
82     return BBinder::onTransact(code, data, reply, flags);
83   }
84 }
85
86 // BpBluetoothLowEnergyCallback (client) implementation
87 // ========================================================
88
89 BpBluetoothLowEnergyCallback::BpBluetoothLowEnergyCallback(
90     const sp<IBinder>& impl)
91     : BpInterface<IBluetoothLowEnergyCallback>(impl) {
92 }
93
94 void BpBluetoothLowEnergyCallback::OnClientRegistered(
95     int status, int client_if) {
96   Parcel data, reply;
97
98   data.writeInterfaceToken(
99       IBluetoothLowEnergyCallback::getInterfaceDescriptor());
100   data.writeInt32(status);
101   data.writeInt32(client_if);
102
103   remote()->transact(
104       IBluetoothLowEnergyCallback::ON_CLIENT_REGISTERED_TRANSACTION,
105       data, &reply,
106       IBinder::FLAG_ONEWAY);
107 }
108
109 void BpBluetoothLowEnergyCallback::OnConnectionState(
110     int status, int client_id, const char* address, bool connected) {
111
112   Parcel data;
113
114   data.writeInterfaceToken(
115       IBluetoothLowEnergyCallback::getInterfaceDescriptor());
116   data.writeInt32(status);
117   data.writeInt32(client_id);
118   data.writeCString(address);
119   data.writeBool(connected);
120
121   remote()->transact(
122       IBluetoothLowEnergyCallback::ON_CONNECTION_STATE_TRANSACTION,
123       data, NULL,
124       IBinder::FLAG_ONEWAY);
125 }
126
127 void BpBluetoothLowEnergyCallback::OnScanResult(
128     const bluetooth::ScanResult& scan_result) {
129   Parcel data, reply;
130
131   data.writeInterfaceToken(
132       IBluetoothLowEnergyCallback::getInterfaceDescriptor());
133   WriteScanResultToParcel(scan_result, &data);
134
135   remote()->transact(
136       IBluetoothLowEnergyCallback::ON_SCAN_RESULT_TRANSACTION,
137       data, &reply,
138       IBinder::FLAG_ONEWAY);
139 }
140
141 void BpBluetoothLowEnergyCallback::OnMultiAdvertiseCallback(
142     int status, bool is_start,
143     const AdvertiseSettings& settings) {
144   Parcel data, reply;
145
146   data.writeInterfaceToken(
147       IBluetoothLowEnergyCallback::getInterfaceDescriptor());
148   data.writeInt32(status);
149   data.writeInt32(is_start);
150   WriteAdvertiseSettingsToParcel(settings, &data);
151
152   remote()->transact(
153       IBluetoothLowEnergyCallback::ON_MULTI_ADVERTISE_CALLBACK_TRANSACTION,
154       data, &reply,
155       IBinder::FLAG_ONEWAY);
156 }
157
158 IMPLEMENT_META_INTERFACE(BluetoothLowEnergyCallback,
159                          IBluetoothLowEnergyCallback::kServiceName);
160
161 }  // namespace binder
162 }  // namespace ipc