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