summaryrefslogtreecommitdiff
path: root/audio/core/all-versions/vts/functional/DeviceManager.h
blob: 6bb39ed3ce16c07e55f13a9508c054f8895ea64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <unistd.h>

#include <map>

#include <android-base/logging.h>
#include <hwbinder/IPCThreadState.h>
#include <system/audio.h>

// clang-format off
#include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
#include PATH(android/hardware/audio/FILE_VERSION/IDevicesFactory.h)
#include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h)
#include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/types.h)
#include PATH(android/hardware/audio/common/COMMON_TYPES_FILE_VERSION/types.h)
// clang-format on

#include "utility/ReturnIn.h"

using ::android::sp;
using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::Result;
using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
using namespace ::android::hardware::audio::common::test::utility;
using namespace ::android::hardware::audio::CPP_VERSION;

template <class Derived, class Key, class Interface>
class InterfaceManager {
  public:
    sp<Interface> getExisting(const Key& name) {
        auto existing = instances.find(name);
        return existing != instances.end() ? existing->second : sp<Interface>();
    }

    sp<Interface> get(const Key& name) {
        auto existing = instances.find(name);
        if (existing != instances.end()) return existing->second;
        auto [inserted, _] = instances.emplace(name, Derived::createInterfaceInstance(name));
        return inserted->second;
    }

    // The test must check that reset was successful. Reset failure means that the test code
    // is holding a strong reference to the device.
    bool reset(const Key& name, bool waitForDestruction) __attribute__((warn_unused_result)) {
        auto iter = instances.find(name);
        if (iter == instances.end()) return true;
        ::android::wp<Interface> weak = iter->second;
        instances.erase(iter);
        if (weak.promote() != nullptr) return false;
        if (waitForDestruction) {
            waitForInstanceDestruction();
        }
        return true;
    }

    static void waitForInstanceDestruction() {
        // FIXME: there is no way to know when the remote IDevice is being destroyed
        //        Binder does not support testing if an object is alive, thus
        //        wait for 100ms to let the binder destruction propagates and
        //        the remote device has the time to be destroyed.
        //        flushCommand makes sure all local command are sent, thus should reduce
        //        the latency between local and remote destruction.
        ::android::hardware::IPCThreadState::self()->flushCommands();
        usleep(100 * 1000);
    }

  protected:
    std::map<Key, sp<Interface>> instances;
};

class DevicesFactoryManager
    : public InterfaceManager<DevicesFactoryManager, std::string, IDevicesFactory> {
  public:
    static DevicesFactoryManager& getInstance() {
        static DevicesFactoryManager instance;
        return instance;
    }
    static sp<IDevicesFactory> createInterfaceInstance(const std::string& name) {
        return IDevicesFactory::getService(name);
    }
};

namespace impl {

class PrimaryDeviceManager
    : public InterfaceManager<PrimaryDeviceManager, std::string, IPrimaryDevice> {
  public:
    static sp<IPrimaryDevice> createInterfaceInstance(const std::string& factoryName) {
        sp<IDevicesFactory> factory = DevicesFactoryManager::getInstance().get(factoryName);
        return openPrimaryDevice(factory);
    }

    bool reset(const std::string& factoryName) __attribute__((warn_unused_result)) {
#if MAJOR_VERSION <= 5
        return InterfaceManager::reset(factoryName, true);
#elif MAJOR_VERSION >= 6
        {
            sp<IPrimaryDevice> device = getExisting(factoryName);
            if (device != nullptr) {
                auto ret = device->close();
                ALOGE_IF(!ret.isOk(), "PrimaryDevice %s close failed: %s", factoryName.c_str(),
                         ret.description().c_str());
            }
        }
        return InterfaceManager::reset(factoryName, false);
#endif
    }

  private:
    static sp<IPrimaryDevice> openPrimaryDevice(const sp<IDevicesFactory>& factory) {
        if (factory == nullptr) return {};
        Result result;
        sp<IPrimaryDevice> primaryDevice;
#if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1)
        sp<IDevice> device;
#if MAJOR_VERSION == 2
        auto ret = factory->openDevice(IDevicesFactory::Device::PRIMARY, returnIn(result, device));
        if (ret.isOk() && result == Result::OK && device != nullptr) {
            primaryDevice = IPrimaryDevice::castFrom(device);
        }
#elif MAJOR_VERSION >= 4
        auto ret = factory->openPrimaryDevice(returnIn(result, device));
        if (ret.isOk() && result == Result::OK && device != nullptr) {
            primaryDevice = IPrimaryDevice::castFrom(device);
        }
#endif
        if (!ret.isOk() || result != Result::OK || primaryDevice == nullptr) {
            ALOGW("Primary device can not be opened, transaction: %s, result %d, device %p",
                  ret.description().c_str(), result, device.get());
            return nullptr;
        }
#else  // V7.1
        auto ret = factory->openPrimaryDevice_7_1(returnIn(result, primaryDevice));
        if (!ret.isOk() || result != Result::OK) {
            ALOGW("Primary device can not be opened, transaction: %s, result %d",
                  ret.description().c_str(), result);
            return nullptr;
        }
#endif
        return primaryDevice;
    }
};

using FactoryAndDevice = std::tuple<std::string, std::string>;
class RegularDeviceManager
    : public InterfaceManager<RegularDeviceManager, FactoryAndDevice, IDevice> {
  public:
    static sp<IDevice> createInterfaceInstance(const FactoryAndDevice& factoryAndDevice) {
        auto [factoryName, name] = factoryAndDevice;
        sp<IDevicesFactory> factory = DevicesFactoryManager::getInstance().get(factoryName);
        return openDevice(factory, name);
    }

    sp<IDevice> get(const std::string& factoryName, const std::string& name) {
        return InterfaceManager::get(std::make_tuple(factoryName, name));
    }

    bool reset(const std::string& factoryName, const std::string& name)
            __attribute__((warn_unused_result)) {
#if MAJOR_VERSION <= 5
        return InterfaceManager::reset(std::make_tuple(factoryName, name), true);
#elif MAJOR_VERSION >= 6
        {
            sp<IDevice> device = getExisting(std::make_tuple(factoryName, name));
            if (device != nullptr) {
                auto ret = device->close();
                ALOGE_IF(!ret.isOk(), "Device %s::%s close failed: %s", factoryName.c_str(),
                         name.c_str(), ret.description().c_str());
            }
        }
        return InterfaceManager::reset(std::make_tuple(factoryName, name), false);
#endif
    }

  private:
    static sp<IDevice> openDevice(const sp<IDevicesFactory>& factory, const std::string& name) {
        if (factory == nullptr) return nullptr;
        Result result;
        sp<IDevice> device;
#if MAJOR_VERSION == 2
        IDevicesFactory::Device dev = IDevicesFactory::IDevicesFactory::Device(-1);
        if (name == AUDIO_HARDWARE_MODULE_ID_A2DP) {
            dev = IDevicesFactory::Device::A2DP;
        } else if (name == AUDIO_HARDWARE_MODULE_ID_USB) {
            dev = IDevicesFactory::Device::USB;
        } else if (name == AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) {
            dev = IDevicesFactory::Device::R_SUBMIX;
        } else if (name == AUDIO_HARDWARE_MODULE_ID_STUB) {
            dev = IDevicesFactory::Device::STUB;
        }
        auto ret = factory->openDevice(dev, returnIn(result, device));
#elif MAJOR_VERSION >= 4 && (MAJOR_VERSION < 7 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0))
        auto ret = factory->openDevice(name, returnIn(result, device));
#elif MAJOR_VERSION == 7 && MINOR_VERSION == 1
        auto ret = factory->openDevice_7_1(name, returnIn(result, device));
#endif
        if (!ret.isOk() || result != Result::OK || device == nullptr) {
            ALOGW("Device %s can not be opened, transaction: %s, result %d, device %p",
                  name.c_str(), ret.description().c_str(), result, device.get());
            return nullptr;
        }
        return device;
    }
};

}  // namespace impl

class DeviceManager {
  public:
    static DeviceManager& getInstance() {
        static DeviceManager instance;
        return instance;
    }

    static constexpr const char* kPrimaryDevice = "primary";

    sp<IDevice> get(const std::string& factoryName, const std::string& name) {
        if (name == kPrimaryDevice) {
            auto primary = getPrimary(factoryName);
            return primary ? deviceFromPrimary(primary) : nullptr;
        }
        return mDevices.get(factoryName, name);
    }

    sp<IPrimaryDevice> getPrimary(const std::string& factoryName) {
        return mPrimary.get(factoryName);
    }

    bool reset(const std::string& factoryName, const std::string& name)
            __attribute__((warn_unused_result)) {
        return name == kPrimaryDevice ? resetPrimary(factoryName)
                                      : mDevices.reset(factoryName, name);
    }

    bool resetPrimary(const std::string& factoryName) __attribute__((warn_unused_result)) {
        return mPrimary.reset(factoryName);
    }

    static void waitForInstanceDestruction() {
        // Does not matter which device manager to use.
        impl::RegularDeviceManager::waitForInstanceDestruction();
    }

  private:
    sp<IDevice> deviceFromPrimary(const sp<IPrimaryDevice>& primary) {
#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
        auto ret = primary->getDevice();
        if (ret.isOk()) {
            return ret;
        } else {
            ALOGW("Error retrieving IDevice from primary: transaction: %s, primary %p",
                  ret.description().c_str(), primary.get());
            return nullptr;
        }
#else
        return primary;
#endif
    }

    impl::PrimaryDeviceManager mPrimary;
    impl::RegularDeviceManager mDevices;
};