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