1/*
2 * Copyright (C) 2015 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#define LOG_TAG "APM::Devices"
18//#define LOG_NDEBUG 0
19
20#include <audio_utils/string.h>
21#include "DeviceDescriptor.h"
22#include "TypeConverter.h"
23#include "AudioGain.h"
24#include "HwModule.h"
25
26namespace android {
27
28DeviceDescriptor::DeviceDescriptor(audio_devices_t type, const String8 &tagName) :
29    AudioPort(String8(""), AUDIO_PORT_TYPE_DEVICE,
30              audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
31                                             AUDIO_PORT_ROLE_SOURCE),
32    mAddress(""), mTagName(tagName), mDeviceType(type), mId(0)
33{
34    if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX || type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) {
35        mAddress = String8("0");
36    }
37}
38
39audio_port_handle_t DeviceDescriptor::getId() const
40{
41    return mId;
42}
43
44void DeviceDescriptor::attach(const sp<HwModule>& module)
45{
46    AudioPort::attach(module);
47    mId = getNextUniqueId();
48}
49
50bool DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
51{
52    // Devices are considered equal if they:
53    // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
54    // - have the same address
55    if (other == 0) {
56        return false;
57    }
58    return (mDeviceType == other->mDeviceType) && (mAddress == other->mAddress);
59}
60
61void DeviceVector::refreshTypes()
62{
63    mDeviceTypes = AUDIO_DEVICE_NONE;
64    for (size_t i = 0; i < size(); i++) {
65        mDeviceTypes |= itemAt(i)->type();
66    }
67    ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
68}
69
70ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
71{
72    for (size_t i = 0; i < size(); i++) {
73        if (item->equals(itemAt(i))) {
74            return i;
75        }
76    }
77    return -1;
78}
79
80void DeviceVector::add(const DeviceVector &devices)
81{
82    bool added = false;
83    for (const auto& device : devices) {
84        if (indexOf(device) < 0 && SortedVector::add(device) >= 0) {
85            added = true;
86        }
87    }
88    if (added) {
89        refreshTypes();
90    }
91}
92
93ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item)
94{
95    ssize_t ret = indexOf(item);
96
97    if (ret < 0) {
98        ret = SortedVector::add(item);
99        if (ret >= 0) {
100            refreshTypes();
101        }
102    } else {
103        ALOGW("DeviceVector::add device %08x already in", item->type());
104        ret = -1;
105    }
106    return ret;
107}
108
109ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item)
110{
111    ssize_t ret = indexOf(item);
112
113    if (ret < 0) {
114        ALOGW("DeviceVector::remove device %08x not in", item->type());
115    } else {
116        ret = SortedVector::removeAt(ret);
117        if (ret >= 0) {
118            refreshTypes();
119        }
120    }
121    return ret;
122}
123
124audio_devices_t DeviceVector::getDevicesFromHwModule(audio_module_handle_t moduleHandle) const
125{
126    audio_devices_t devices = AUDIO_DEVICE_NONE;
127    for (size_t i = 0; i < size(); i++) {
128        if (itemAt(i)->getModuleHandle() == moduleHandle) {
129            devices |= itemAt(i)->type();
130        }
131    }
132    return devices;
133}
134
135sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, const String8& address) const
136{
137    sp<DeviceDescriptor> device;
138    for (size_t i = 0; i < size(); i++) {
139        if (itemAt(i)->type() == type) {
140            if (address == "" || itemAt(i)->mAddress == address) {
141                device = itemAt(i);
142                if (itemAt(i)->mAddress == address) {
143                    break;
144                }
145            }
146        }
147    }
148    ALOGV("DeviceVector::getDevice() for type %08x address %s found %p",
149          type, address.string(), device.get());
150    return device;
151}
152
153sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const
154{
155    for (const auto& device : *this) {
156        if (device->getId() == id) {
157            return device;
158        }
159    }
160    return nullptr;
161}
162
163DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const
164{
165    DeviceVector devices;
166    bool isOutput = audio_is_output_devices(type);
167    type &= ~AUDIO_DEVICE_BIT_IN;
168    for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
169        bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType);
170        audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN;
171        if ((isOutput == curIsOutput) && ((type & curType) != 0)) {
172            devices.add(itemAt(i));
173            type &= ~curType;
174            ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
175                  itemAt(i)->type(), itemAt(i).get());
176        }
177    }
178    return devices;
179}
180
181DeviceVector DeviceVector::getDevicesFromTypeAddr(
182        audio_devices_t type, const String8& address) const
183{
184    DeviceVector devices;
185    for (const auto& device : *this) {
186        if (device->type() == type && device->mAddress == address) {
187            devices.add(device);
188        }
189    }
190    return devices;
191}
192
193sp<DeviceDescriptor> DeviceVector::getDeviceFromTagName(const String8 &tagName) const
194{
195    for (const auto& device : *this) {
196        if (device->getTagName() == tagName) {
197            return device;
198        }
199    }
200    return nullptr;
201}
202
203status_t DeviceVector::dump(int fd, const String8 &tag, int spaces, bool verbose) const
204{
205    if (isEmpty()) {
206        return NO_ERROR;
207    }
208    const size_t SIZE = 256;
209    char buffer[SIZE];
210
211    snprintf(buffer, SIZE, "%*s- %s devices:\n", spaces, "", tag.string());
212    write(fd, buffer, strlen(buffer));
213    for (size_t i = 0; i < size(); i++) {
214        itemAt(i)->dump(fd, spaces + 2, i, verbose);
215    }
216    return NO_ERROR;
217}
218
219void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
220                                         const struct audio_port_config *srcConfig) const
221{
222    dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN;
223    if (mSamplingRate != 0) {
224        dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
225    }
226    if (mChannelMask != AUDIO_CHANNEL_NONE) {
227        dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
228    }
229    if (mFormat != AUDIO_FORMAT_INVALID) {
230        dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
231    }
232
233    if (srcConfig != NULL) {
234        dstConfig->config_mask |= srcConfig->config_mask;
235    }
236
237    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
238
239    dstConfig->id = mId;
240    dstConfig->role = audio_is_output_device(mDeviceType) ?
241                        AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
242    dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
243    dstConfig->ext.device.type = mDeviceType;
244
245    //TODO Understand why this test is necessary. i.e. why at boot time does it crash
246    // without the test?
247    // This has been demonstrated to NOT be true (at start up)
248    // ALOG_ASSERT(mModule != NULL);
249    dstConfig->ext.device.hw_module =
250            mModule != 0 ? mModule->getHandle() : AUDIO_MODULE_HANDLE_NONE;
251    (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mAddress.string());
252}
253
254void DeviceDescriptor::toAudioPort(struct audio_port *port) const
255{
256    ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType);
257    AudioPort::toAudioPort(port);
258    port->id = mId;
259    toAudioPortConfig(&port->active_config);
260    port->ext.device.type = mDeviceType;
261    port->ext.device.hw_module = mModule->getHandle();
262    (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mAddress.string());
263}
264
265void DeviceDescriptor::importAudioPort(const sp<AudioPort>& port, bool force) {
266    if (!force && !port->hasDynamicAudioProfile()) {
267        return;
268    }
269    AudioPort::importAudioPort(port);
270    port->pickAudioProfile(mSamplingRate, mChannelMask, mFormat);
271}
272
273status_t DeviceDescriptor::dump(int fd, int spaces, int index, bool verbose) const
274{
275    const size_t SIZE = 256;
276    char buffer[SIZE];
277    String8 result;
278
279    snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
280    result.append(buffer);
281    if (mId != 0) {
282        snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
283        result.append(buffer);
284    }
285    if (!mTagName.isEmpty()) {
286        snprintf(buffer, SIZE, "%*s- tag name: %s\n", spaces, "", mTagName.string());
287        result.append(buffer);
288    }
289    std::string deviceLiteral;
290    if (deviceToString(mDeviceType, deviceLiteral)) {
291        snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "", deviceLiteral.c_str());
292        result.append(buffer);
293    }
294    if (mAddress.size() != 0) {
295        snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
296        result.append(buffer);
297    }
298    write(fd, result.string(), result.size());
299    AudioPort::dump(fd, spaces, verbose);
300
301    return NO_ERROR;
302}
303
304void DeviceDescriptor::log() const
305{
306    std::string device;
307    deviceToString(mDeviceType, device);
308    ALOGI("Device id:%d type:0x%X:%s, addr:%s", mId,  mDeviceType, device.c_str(),
309          mAddress.string());
310
311    AudioPort::log("  ");
312}
313
314} // namespace android
315