DeviceDescriptor.cpp revision 493404d8c396e15ed73455acd39aa0f1940996e2
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 "DeviceDescriptor.h"
21#include "AudioGain.h"
22#include "HwModule.h"
23#include "ConfigParsingUtils.h"
24
25namespace android {
26
27String8 DeviceDescriptor::emptyNameStr = String8("");
28
29DeviceDescriptor::DeviceDescriptor(const String8& name, audio_devices_t type) :
30    AudioPort(name, AUDIO_PORT_TYPE_DEVICE,
31              audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
32                                             AUDIO_PORT_ROLE_SOURCE),
33    mAddress(""), mDeviceType(type), mId(0)
34{
35
36}
37
38audio_port_handle_t DeviceDescriptor::getId() const
39{
40    return mId;
41}
42
43void DeviceDescriptor::attach(const sp<HwModule>& module)
44{
45    AudioPort::attach(module);
46    mId = getNextUniqueId();
47}
48
49bool DeviceDescriptor::equals(const sp<DeviceDescriptor>& other) const
50{
51    // Devices are considered equal if they:
52    // - are of the same type (a device type cannot be AUDIO_DEVICE_NONE)
53    // - have the same address or one device does not specify the address
54    // - have the same channel mask or one device does not specify the channel mask
55    if (other == 0) {
56        return false;
57    }
58    return (mDeviceType == other->mDeviceType) &&
59           (mAddress == "" || other->mAddress == "" || mAddress == other->mAddress) &&
60           (mChannelMask == 0 || other->mChannelMask == 0 ||
61                mChannelMask == other->mChannelMask);
62}
63
64void DeviceDescriptor::loadGains(cnode *root)
65{
66    AudioPort::loadGains(root);
67    if (mGains.size() > 0) {
68        mGains[0]->getDefaultConfig(&mGain);
69    }
70}
71
72void DeviceVector::refreshTypes()
73{
74    mDeviceTypes = AUDIO_DEVICE_NONE;
75    for(size_t i = 0; i < size(); i++) {
76        mDeviceTypes |= itemAt(i)->type();
77    }
78    ALOGV("DeviceVector::refreshTypes() mDeviceTypes %08x", mDeviceTypes);
79}
80
81ssize_t DeviceVector::indexOf(const sp<DeviceDescriptor>& item) const
82{
83    for(size_t i = 0; i < size(); i++) {
84        if (item->equals(itemAt(i))) {
85            return i;
86        }
87    }
88    return -1;
89}
90
91ssize_t DeviceVector::add(const sp<DeviceDescriptor>& item)
92{
93    ssize_t ret = indexOf(item);
94
95    if (ret < 0) {
96        ret = SortedVector::add(item);
97        if (ret >= 0) {
98            refreshTypes();
99        }
100    } else {
101        ALOGW("DeviceVector::add device %08x already in", item->type());
102        ret = -1;
103    }
104    return ret;
105}
106
107ssize_t DeviceVector::remove(const sp<DeviceDescriptor>& item)
108{
109    size_t i;
110    ssize_t ret = indexOf(item);
111
112    if (ret < 0) {
113        ALOGW("DeviceVector::remove device %08x not in", item->type());
114    } else {
115        ret = SortedVector::removeAt(ret);
116        if (ret >= 0) {
117            refreshTypes();
118        }
119    }
120    return ret;
121}
122
123audio_devices_t DeviceVector::getDevicesFromHwModule(audio_module_handle_t moduleHandle) const
124{
125    audio_devices_t devices = AUDIO_DEVICE_NONE;
126    for (size_t i = 0; i < size(); i++) {
127        if (itemAt(i)->getModuleHandle() == moduleHandle) {
128            devices |= itemAt(i)->type();
129        }
130    }
131    return devices;
132}
133
134void DeviceVector::loadDevicesFromType(audio_devices_t types)
135{
136    DeviceVector deviceList;
137
138    uint32_t role_bit = AUDIO_DEVICE_BIT_IN & types;
139    types &= ~role_bit;
140
141    while (types) {
142        uint32_t i = 31 - __builtin_clz(types);
143        uint32_t type = 1 << i;
144        types &= ~type;
145        add(new DeviceDescriptor(String8("device_type"), type | role_bit));
146    }
147}
148
149void DeviceVector::loadDevicesFromName(char *name,
150                                       const DeviceVector& declaredDevices)
151{
152    char *devName = strtok(name, "|");
153    while (devName != NULL) {
154        if (strlen(devName) != 0) {
155            audio_devices_t type = ConfigParsingUtils::stringToEnum(sDeviceTypeToEnumTable,
156                                 ARRAY_SIZE(sDeviceTypeToEnumTable),
157                                 devName);
158            if (type != AUDIO_DEVICE_NONE) {
159                devName = (char *)ConfigParsingUtils::enumToString(sDeviceNameToEnumTable,
160                                                           ARRAY_SIZE(sDeviceNameToEnumTable),
161                                                           type);
162                sp<DeviceDescriptor> dev = new DeviceDescriptor(String8(devName), type);
163                if (type == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
164                        type == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ) {
165                    dev->mAddress = String8("0");
166                }
167                add(dev);
168            } else {
169                sp<DeviceDescriptor> deviceDesc =
170                        declaredDevices.getDeviceFromName(String8(devName));
171                if (deviceDesc != 0) {
172                    add(deviceDesc);
173                }
174            }
175         }
176         devName = strtok(NULL, "|");
177     }
178}
179
180sp<DeviceDescriptor> DeviceVector::getDevice(audio_devices_t type, String8 address) const
181{
182    sp<DeviceDescriptor> device;
183    for (size_t i = 0; i < size(); i++) {
184        if (itemAt(i)->type() == type) {
185            if (address == "" || itemAt(i)->mAddress == address) {
186                device = itemAt(i);
187                if (itemAt(i)->mAddress == address) {
188                    break;
189                }
190            }
191        }
192    }
193    ALOGV("DeviceVector::getDevice() for type %08x address %s found %p",
194          type, address.string(), device.get());
195    return device;
196}
197
198sp<DeviceDescriptor> DeviceVector::getDeviceFromId(audio_port_handle_t id) const
199{
200    sp<DeviceDescriptor> device;
201    for (size_t i = 0; i < size(); i++) {
202        if (itemAt(i)->getId() == id) {
203            device = itemAt(i);
204            break;
205        }
206    }
207    return device;
208}
209
210DeviceVector DeviceVector::getDevicesFromType(audio_devices_t type) const
211{
212    DeviceVector devices;
213    bool isOutput = audio_is_output_devices(type);
214    type &= ~AUDIO_DEVICE_BIT_IN;
215    for (size_t i = 0; (i < size()) && (type != AUDIO_DEVICE_NONE); i++) {
216        bool curIsOutput = audio_is_output_devices(itemAt(i)->mDeviceType);
217        audio_devices_t curType = itemAt(i)->mDeviceType & ~AUDIO_DEVICE_BIT_IN;
218        if ((isOutput == curIsOutput) && ((type & curType) != 0)) {
219            devices.add(itemAt(i));
220            type &= ~curType;
221            ALOGV("DeviceVector::getDevicesFromType() for type %x found %p",
222                  itemAt(i)->type(), itemAt(i).get());
223        }
224    }
225    return devices;
226}
227
228DeviceVector DeviceVector::getDevicesFromTypeAddr(
229        audio_devices_t type, String8 address) const
230{
231    DeviceVector devices;
232    for (size_t i = 0; i < size(); i++) {
233        if (itemAt(i)->type() == type) {
234            if (itemAt(i)->mAddress == address) {
235                devices.add(itemAt(i));
236            }
237        }
238    }
239    return devices;
240}
241
242sp<DeviceDescriptor> DeviceVector::getDeviceFromName(const String8& name) const
243{
244    sp<DeviceDescriptor> device;
245    for (size_t i = 0; i < size(); i++) {
246        if (itemAt(i)->mName == name) {
247            device = itemAt(i);
248            break;
249        }
250    }
251    return device;
252}
253
254
255status_t DeviceVector::dump(int fd, const String8 &direction) const
256{
257    const size_t SIZE = 256;
258    char buffer[SIZE];
259
260    snprintf(buffer, SIZE, "\n Available %s devices:\n", direction.string());
261    write(fd, buffer, strlen(buffer));
262    for (size_t i = 0; i < size(); i++) {
263        itemAt(i)->dump(fd, 2, i);
264    }
265    return NO_ERROR;
266}
267
268audio_policy_dev_state_t DeviceVector::getDeviceConnectionState(const sp<DeviceDescriptor> &devDesc) const
269{
270    ssize_t index = indexOf(devDesc);
271    return index >= 0 ? AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
272}
273
274void DeviceDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
275                                         const struct audio_port_config *srcConfig) const
276{
277    dstConfig->config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK|AUDIO_PORT_CONFIG_GAIN;
278    if (srcConfig != NULL) {
279        dstConfig->config_mask |= srcConfig->config_mask;
280    }
281
282    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
283
284    dstConfig->id = mId;
285    dstConfig->role = audio_is_output_device(mDeviceType) ?
286                        AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
287    dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
288    dstConfig->ext.device.type = mDeviceType;
289
290    //TODO Understand why this test is necessary. i.e. why at boot time does it crash
291    // without the test?
292    // This has been demonstrated to NOT be true (at start up)
293    // ALOG_ASSERT(mModule != NULL);
294    dstConfig->ext.device.hw_module = mModule != 0 ? mModule->mHandle : AUDIO_IO_HANDLE_NONE;
295    strncpy(dstConfig->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
296}
297
298void DeviceDescriptor::toAudioPort(struct audio_port *port) const
299{
300    ALOGV("DeviceDescriptor::toAudioPort() handle %d type %x", mId, mDeviceType);
301    AudioPort::toAudioPort(port);
302    port->id = mId;
303    toAudioPortConfig(&port->active_config);
304    port->ext.device.type = mDeviceType;
305    port->ext.device.hw_module = mModule->mHandle;
306    strncpy(port->ext.device.address, mAddress.string(), AUDIO_DEVICE_MAX_ADDRESS_LEN);
307}
308
309status_t DeviceDescriptor::dump(int fd, int spaces, int index) const
310{
311    const size_t SIZE = 256;
312    char buffer[SIZE];
313    String8 result;
314
315    snprintf(buffer, SIZE, "%*sDevice %d:\n", spaces, "", index+1);
316    result.append(buffer);
317    if (mId != 0) {
318        snprintf(buffer, SIZE, "%*s- id: %2d\n", spaces, "", mId);
319        result.append(buffer);
320    }
321    snprintf(buffer, SIZE, "%*s- type: %-48s\n", spaces, "",
322            ConfigParsingUtils::enumToString(sDeviceTypeToEnumTable,
323                    ARRAY_SIZE(sDeviceTypeToEnumTable),
324                    mDeviceType));
325    result.append(buffer);
326    if (mAddress.size() != 0) {
327        snprintf(buffer, SIZE, "%*s- address: %-32s\n", spaces, "", mAddress.string());
328        result.append(buffer);
329    }
330    write(fd, result.string(), result.size());
331    AudioPort::dump(fd, spaces);
332
333    return NO_ERROR;
334}
335
336void DeviceDescriptor::log() const
337{
338    ALOGI("Device id:%d type:0x%X:%s, addr:%s",
339          mId,
340          mDeviceType,
341          ConfigParsingUtils::enumToString(
342             sDeviceNameToEnumTable, ARRAY_SIZE(sDeviceNameToEnumTable), mDeviceType),
343          mAddress.string());
344
345    AudioPort::log("  ");
346}
347
348}; // namespace android
349