• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unordered_map>
21 
22 #include "bluetooth/uuid.h"
23 #include "service/bluetooth_instance.h"
24 #include "service/ipc/binder/remote_callback_map.h"
25 
26 namespace ipc {
27 namespace binder {
28 
29 // InterfaceWithInstancesBase provides a common base class for Binder interface
30 // servers that involve instance callback Binders registered with an integer
31 // instance ID over an asynchronous lower-level stack API. This class abstracts
32 // away the common procedures of managing pending callbacks, listening to death
33 // notifications, and maintaining multiple internal maps in one common base
34 // class.
35 // TODO: add code example here.
36 class InterfaceWithInstancesBase
37     : public RemoteCallbackMap<int, android::IInterface>::Delegate,
38       virtual public android::RefBase {
39  public:
40   InterfaceWithInstancesBase() = default;
41   InterfaceWithInstancesBase(const InterfaceWithInstancesBase&) = delete;
42   InterfaceWithInstancesBase& operator=(const InterfaceWithInstancesBase&) =
43       delete;
44 
45   ~InterfaceWithInstancesBase() override = default;
46 
47  protected:
48   // The initial entry point for registering a instance. Invoke this from the
49   // registration API to add a instance/Uuid pair to the pending list and set up
50   // the generic asynchronous callback handler and initiate the process with the
51   // given |factory| instance. Returns false, if there were any errors that
52   // could be synchronously reported.
53   bool RegisterInstanceBase(const android::sp<IInterface>& callback,
54                             bluetooth::BluetoothInstanceFactory* factory);
55 
56   // Unregister the instance with the given ID, if it was registered before.
57   void UnregisterInstanceBase(int instance_id);
58 
59   // Unregisters all registered instances.
60   void UnregisterAllBase();
61 
62   void ForEachCallback(const std::function<void(IInterface*)>& func);
63 
64   // Returns a handle to the lock used to synchronize access to the internal
65   // data structures. Subclasses should acquire this before accessing the maps.
maps_lock()66   std::mutex* maps_lock() { return &maps_lock_; }
67 
68   // Returns the callback interface binder that is assigned to the given
69   // instance ID |instance_id|. The returned pointer will contain NULL if an
70   // entry for the given ID cannot be found.
71   android::sp<IInterface> GetCallback(int instance_id);
72 
73   // Returns the instance instance that is assigned to the given instance ID
74   // |instance_id|. The returned pointer will contain NULL if an entry for the
75   // given ID cannot be found.
76   std::shared_ptr<bluetooth::BluetoothInstance> GetInstance(int instance_id);
77 
78  private:
79   // Base implementation of the register callback.
80   void OnRegisterInstance(
81       bluetooth::BLEStatus status, const bluetooth::Uuid& uuid,
82       std::unique_ptr<bluetooth::BluetoothInstance> instance);
83 
84   // Called when the callback registration has completed. |instance| is owned by
85   // the base class and should not be deleted by the implementation. If the
86   // operation failed, nullptr will be passed for |instance|.
87   virtual void OnRegisterInstanceImpl(
88       bluetooth::BLEStatus status, android::sp<IInterface> callback,
89       bluetooth::BluetoothInstance* instance) = 0;
90 
91   // RemoteCallbackMap<int, IBluetoothLowEnergyCallback>::Delegate override:
92   void OnRemoteCallbackRemoved(const int& key) override;
93 
94   // Instances that are pending registration. Once their registration is
95   // complete, the entry will be removed from this map.
96   RemoteCallbackMap<bluetooth::Uuid, android::IInterface> pending_callbacks_;
97 
98   // We keep two maps here: one from instance_id IDs to callback Binders and one
99   // from instance_id IDs to the BluetoothInstance structures themselves.
100   std::mutex maps_lock_;  // Needed for |id_to_instance_|.
101   RemoteCallbackMap<int, IInterface> id_to_cb_;
102   std::unordered_map<int, std::shared_ptr<bluetooth::BluetoothInstance>>
103       id_to_instance_;
104 };
105 
106 }  // namespace binder
107 }  // namespace ipc
108