• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <mutex>
18 #include <vector>
19 
20 // clang-format off
21 #include PATH(device/google/atv/audio_proxy/FILE_VERSION/IBusDevice.h)
22 // clang-format on
23 
24 namespace audio_proxy {
25 namespace service {
26 
27 using ::android::sp;
28 using ::android::hardware::hidl_death_recipient;
29 using ::android::hardware::hidl_string;
30 using ::device::google::atv::audio_proxy::CPP_VERSION::IBusDevice;
31 
32 class BusDeviceProvider {
33  public:
34   BusDeviceProvider();
35   ~BusDeviceProvider();
36 
37   // Register bus device under `address`. The device is unregistered when its
38   // process exits.
39   bool add(const hidl_string& address, sp<IBusDevice> device);
40 
41   // Get IBusDevice with `address`. Caller should only keep the strong pointer
42   // shortly, a.k.a caller should release the strong pointer as soon as its
43   // function scope exits.
44   sp<IBusDevice> get(const hidl_string& address);
45 
46   // Remove all the added devices.
47   void removeAll();
48 
49  private:
50   class DeathRecipient;
51   struct BusDeviceHolder {
52     sp<IBusDevice> device;
53     hidl_string address;
54     uint64_t token;
55   };
56 
57   friend class DeathRecipient;
58 
59   // Called by DeathRecipient to remove device when the host process exits.
60   void removeByToken(uint64_t token);
61 
62   sp<hidl_death_recipient> mDeathRecipient;
63 
64   std::mutex mDevicesLock;
65 
66   // Use a vector since we don't have too much registered devices.
67   std::vector<BusDeviceHolder> mBusDevices;
68   uint64_t mNextToken = 0;
69 };
70 
71 }  // namespace service
72 }  // namespace audio_proxy