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