1 //
2 // Copyright 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/ipc/binder/bluetooth_gatt_client_binder_server.h"
18
19 #include <base/logging.h>
20
21 #include "service/adapter.h"
22
23 using android::bluetooth::IBluetoothGattClientCallback;
24
25 namespace ipc {
26 namespace binder {
27
28 namespace {
29 const int kInvalidInstanceId = -1;
30 } // namespace
31
BluetoothGattClientBinderServer(bluetooth::Adapter * adapter)32 BluetoothGattClientBinderServer::BluetoothGattClientBinderServer(
33 bluetooth::Adapter* adapter)
34 : adapter_(adapter) {
35 CHECK(adapter_);
36 }
37
RegisterClient(const android::sp<IBluetoothGattClientCallback> & callback,bool * _aidl_return)38 Status BluetoothGattClientBinderServer::RegisterClient(
39 const android::sp<IBluetoothGattClientCallback>& callback,
40 bool* _aidl_return) {
41 VLOG(2) << __func__;
42
43 bluetooth::GattClientFactory* gatt_client_factory =
44 adapter_->GetGattClientFactory();
45
46 *_aidl_return = RegisterInstanceBase(callback, gatt_client_factory);
47 return Status::ok();
48 }
49
UnregisterClient(int client_id)50 Status BluetoothGattClientBinderServer::UnregisterClient(int client_id) {
51 VLOG(2) << __func__;
52 UnregisterInstanceBase(client_id);
53 return Status::ok();
54 }
55
UnregisterAll()56 Status BluetoothGattClientBinderServer::UnregisterAll() {
57 VLOG(2) << __func__;
58 UnregisterAllBase();
59 return Status::ok();
60 }
61
62 android::sp<IBluetoothGattClientCallback>
GetGattClientCallback(int client_id)63 BluetoothGattClientBinderServer::GetGattClientCallback(int client_id) {
64 auto cb = GetCallback(client_id);
65 return android::sp<IBluetoothGattClientCallback>(
66 static_cast<IBluetoothGattClientCallback*>(cb.get()));
67 }
68
69 std::shared_ptr<bluetooth::GattClient>
GetGattClient(int client_id)70 BluetoothGattClientBinderServer::GetGattClient(int client_id) {
71 return std::static_pointer_cast<bluetooth::GattClient>(
72 GetInstance(client_id));
73 }
74
OnRegisterInstanceImpl(bluetooth::BLEStatus status,android::sp<IInterface> callback,bluetooth::BluetoothInstance * instance)75 void BluetoothGattClientBinderServer::OnRegisterInstanceImpl(
76 bluetooth::BLEStatus status, android::sp<IInterface> callback,
77 bluetooth::BluetoothInstance* instance) {
78 VLOG(1) << __func__ << " client ID: " << instance->GetInstanceId()
79 << " status: " << status;
80
81 android::sp<IBluetoothGattClientCallback> cb(
82 static_cast<IBluetoothGattClientCallback*>(callback.get()));
83 cb->OnClientRegistered(status, (status == bluetooth::BLE_STATUS_SUCCESS)
84 ? instance->GetInstanceId()
85 : kInvalidInstanceId);
86 }
87
88 } // namespace binder
89 } // namespace ipc
90