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 #pragma once
18 
19 #include "DeviceDescriptor.h"
20 #include "AudioRoute.h"
21 #include <hardware/audio.h>
22 #include <utils/RefBase.h>
23 #include <utils/String8.h>
24 #include <utils/Errors.h>
25 #include <utils/Vector.h>
26 #include <system/audio.h>
27 #include <cutils/config_utils.h>
28 #include <string>
29 
30 namespace android {
31 
32 class IOProfile;
33 class InputProfile;
34 class OutputProfile;
35 
36 typedef Vector<sp<IOProfile> > InputProfileCollection;
37 typedef Vector<sp<IOProfile> > OutputProfileCollection;
38 typedef Vector<sp<IOProfile> > IOProfileCollection;
39 
40 class HwModule : public RefBase
41 {
42 public:
43     HwModule(const char *name, uint32_t halVersion = AUDIO_DEVICE_API_VERSION_MIN);
44     ~HwModule();
45 
getName()46     const char *getName() const { return mName.string(); }
47 
48 
getDeclaredDevices()49     const DeviceVector &getDeclaredDevices() const { return mDeclaredDevices; }
50     void setDeclaredDevices(const DeviceVector &devices);
51 
getInputProfiles()52     const InputProfileCollection &getInputProfiles() const { return mInputProfiles; }
53 
getOutputProfiles()54     const OutputProfileCollection &getOutputProfiles() const { return mOutputProfiles; }
55 
56     void setProfiles(const IOProfileCollection &profiles);
57 
setHalVersion(uint32_t halVersion)58     void setHalVersion(uint32_t halVersion) { mHalVersion = halVersion; }
getHalVersion()59     uint32_t getHalVersion() const { return mHalVersion; }
60 
61     sp<DeviceDescriptor> getRouteSinkDevice(const sp<AudioRoute> &route) const;
62     DeviceVector getRouteSourceDevices(const sp<AudioRoute> &route) const;
63     void setRoutes(const AudioRouteVector &routes);
64 
65     status_t addOutputProfile(const sp<IOProfile> &profile);
66     status_t addInputProfile(const sp<IOProfile> &profile);
67     status_t addProfile(const sp<IOProfile> &profile);
68 
69     status_t addOutputProfile(String8 name, const audio_config_t *config,
70             audio_devices_t device, String8 address);
71     status_t removeOutputProfile(String8 name);
72     status_t addInputProfile(String8 name, const audio_config_t *config,
73             audio_devices_t device, String8 address);
74     status_t removeInputProfile(String8 name);
75 
getHandle()76     audio_module_handle_t getHandle() const { return mHandle; }
77 
findPortByTagName(const String8 & tagName)78     sp<AudioPort> findPortByTagName(const String8 &tagName) const
79     {
80         return mPorts.findByTagName(tagName);
81     }
82 
83     // TODO remove from here (split serialization)
84     void dump(int fd);
85 
86     const String8 mName; // base name of the audio HW module (primary, a2dp ...)
87     audio_module_handle_t mHandle;
88     OutputProfileCollection mOutputProfiles; // output profiles exposed by this module
89     InputProfileCollection mInputProfiles;  // input profiles exposed by this module
90 
91 private:
92     void refreshSupportedDevices();
93 
94     uint32_t mHalVersion; // audio HAL API version
95     DeviceVector mDeclaredDevices; // devices declared in audio_policy configuration file.
96     AudioRouteVector mRoutes;
97     AudioPortVector mPorts;
98 };
99 
100 class HwModuleCollection : public Vector<sp<HwModule> >
101 {
102 public:
103     sp<HwModule> getModuleFromName(const char *name) const;
104 
105     sp<HwModule> getModuleForDevice(audio_devices_t device) const;
106 
107     sp<DeviceDescriptor> getDeviceDescriptor(const audio_devices_t device,
108                                              const char *device_address,
109                                              const char *device_name,
110                                              bool matchAdress = true) const;
111 
112     status_t dump(int fd) const;
113 };
114 
115 }; // namespace android
116