1 //
2 //  Copyright 2016 The Android Open Source Project
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/IBluetoothLeScannerCallback.h>
24 #include "android/bluetooth/BnBluetoothLeScanner.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_scanner.h"
29 
30 using android::binder::Status;
31 using android::String16;
32 
33 using android::bluetooth::BnBluetoothLeScanner;
34 using android::bluetooth::IBluetoothLeScannerCallback;
35 
36 namespace bluetooth {
37 class Adapter;
38 }  // namespace bluetooth
39 
40 namespace ipc {
41 namespace binder {
42 
43 // Implements the server side of the IBluetoothLowEnergy interface.
44 class BluetoothLeScannerBinderServer
45     : public BnBluetoothLeScanner,
46       public InterfaceWithInstancesBase,
47       public bluetooth::LowEnergyScanner::Delegate {
48  public:
49   explicit BluetoothLeScannerBinderServer(bluetooth::Adapter* adapter);
50   ~BluetoothLeScannerBinderServer() override;
51 
52   // IBluetoothLowEnergy overrides:
53   Status RegisterScanner(
54       const android::sp<IBluetoothLeScannerCallback>& callback,
55       bool* _aidl_return) override;
56   Status UnregisterScanner(int scanner_id) override;
57   Status UnregisterAll() override;
58   Status StartScan(int scanner_id,
59                    const android::bluetooth::ScanSettings& settings,
60                    const std::vector<android::bluetooth::ScanFilter>& filters,
61                    bool* _aidl_return) override;
62   Status StopScan(int scanner_id, bool* _aidl_return) override;
63 
64   void OnScanResult(bluetooth::LowEnergyScanner* scanner,
65                     const bluetooth::ScanResult& result) override;
66 
67  private:
68   // Returns a pointer to the IBluetoothLowEnergyCallback instance associated
69   // with |scanner_id|. Returns NULL if such a callback cannot be found.
70   android::sp<IBluetoothLeScannerCallback> GetLECallback(int scanner_id);
71 
72   // Returns a pointer to the LowEnergyScanner instance associated with
73   // |scanner_id|. Returns NULL if such a scanner cannot be found.
74   std::shared_ptr<bluetooth::LowEnergyScanner> GetLEScanner(int scanner_id);
75 
76   // InterfaceWithInstancesBase override:
77   void OnRegisterInstanceImpl(bluetooth::BLEStatus status,
78                               android::sp<IInterface> callback,
79                               bluetooth::BluetoothInstance* instance) override;
80 
81   bluetooth::Adapter* adapter_;  // weak
82 
83   DISALLOW_COPY_AND_ASSIGN(BluetoothLeScannerBinderServer);
84 };
85 
86 }  // namespace binder
87 }  // namespace ipc
88