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#pragma once
18
19#include <memory>
20
21#include <base/macros.h>
22
23#include <android/bluetooth/IBluetoothLowEnergyCallback.h>
24#include "android/bluetooth/BnBluetoothLowEnergy.h"
25
26#include "service/common/bluetooth/low_energy_constants.h"
27#include "service/ipc/binder/interface_with_instances_base.h"
28#include "service/low_energy_client.h"
29
30using android::binder::Status;
31using android::String16;
32
33using android::bluetooth::BnBluetoothLowEnergy;
34using android::bluetooth::IBluetoothLowEnergyCallback;
35
36namespace bluetooth {
37class Adapter;
38}  // namespace bluetooth
39
40namespace ipc {
41namespace binder {
42
43// Implements the server side of the IBluetoothLowEnergy interface.
44class BluetoothLowEnergyBinderServer
45    : public BnBluetoothLowEnergy,
46      public InterfaceWithInstancesBase,
47      public bluetooth::LowEnergyClient::Delegate {
48 public:
49  explicit BluetoothLowEnergyBinderServer(bluetooth::Adapter* adapter);
50  ~BluetoothLowEnergyBinderServer() override;
51
52  // IBluetoothLowEnergy overrides:
53  Status RegisterClient(
54      const android::sp<IBluetoothLowEnergyCallback>& callback,
55      bool* _aidl_return) override;
56  Status UnregisterClient(int client_id) override;
57  Status UnregisterAll() override;
58  Status Connect(int client_id, const String16& address, bool is_direct,
59                 bool* _aidl_return) override;
60  Status Disconnect(int client_id, const String16& address,
61                    bool* _aidl_return) override;
62  Status SetMtu(int client_id, const String16& address, int mtu,
63                bool* _aidl_return) override;
64
65  // bluetooth::LowEnergyClient::Delegate overrides:
66  void OnConnectionState(bluetooth::LowEnergyClient* client, int status,
67                         const char* address, bool connected) override;
68  void OnMtuChanged(bluetooth::LowEnergyClient* client, int status,
69                    const char* address, int mtu) override;
70
71 private:
72  // Returns a pointer to the IBluetoothLowEnergyCallback instance associated
73  // with |client_id|. Returns NULL if such a callback cannot be found.
74  android::sp<IBluetoothLowEnergyCallback> GetLECallback(int client_id);
75
76  // Returns a pointer to the LowEnergyClient instance associated with
77  // |client_id|. Returns NULL if such a client cannot be found.
78  std::shared_ptr<bluetooth::LowEnergyClient> GetLEClient(int client_id);
79
80  // InterfaceWithInstancesBase override:
81  void OnRegisterInstanceImpl(bluetooth::BLEStatus status,
82                              android::sp<IInterface> callback,
83                              bluetooth::BluetoothInstance* instance) override;
84
85  bluetooth::Adapter* adapter_;  // weak
86
87  DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyBinderServer);
88};
89
90}  // namespace binder
91}  // namespace ipc
92