• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "PrimaryDeviceHAL"
18 
19 #include "core/default/PrimaryDevice.h"
20 #include "core/default/Util.h"
21 
22 #if MAJOR_VERSION >= 4
23 #include <cmath>
24 #endif
25 
26 namespace android {
27 namespace hardware {
28 namespace audio {
29 namespace CPP_VERSION {
30 namespace implementation {
31 
PrimaryDevice(audio_hw_device_t * device)32 PrimaryDevice::PrimaryDevice(audio_hw_device_t* device) : mDevice(new Device(device)) {}
33 
~PrimaryDevice()34 PrimaryDevice::~PrimaryDevice() {}
35 
36 // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow.
initCheck()37 Return<Result> PrimaryDevice::initCheck() {
38     return mDevice->initCheck();
39 }
40 
setMasterVolume(float volume)41 Return<Result> PrimaryDevice::setMasterVolume(float volume) {
42     return mDevice->setMasterVolume(volume);
43 }
44 
getMasterVolume(getMasterVolume_cb _hidl_cb)45 Return<void> PrimaryDevice::getMasterVolume(getMasterVolume_cb _hidl_cb) {
46     return mDevice->getMasterVolume(_hidl_cb);
47 }
48 
setMicMute(bool mute)49 Return<Result> PrimaryDevice::setMicMute(bool mute) {
50     return mDevice->setMicMute(mute);
51 }
52 
getMicMute(getMicMute_cb _hidl_cb)53 Return<void> PrimaryDevice::getMicMute(getMicMute_cb _hidl_cb) {
54     return mDevice->getMicMute(_hidl_cb);
55 }
56 
setMasterMute(bool mute)57 Return<Result> PrimaryDevice::setMasterMute(bool mute) {
58     return mDevice->setMasterMute(mute);
59 }
60 
getMasterMute(getMasterMute_cb _hidl_cb)61 Return<void> PrimaryDevice::getMasterMute(getMasterMute_cb _hidl_cb) {
62     return mDevice->getMasterMute(_hidl_cb);
63 }
64 
getInputBufferSize(const AudioConfig & config,getInputBufferSize_cb _hidl_cb)65 Return<void> PrimaryDevice::getInputBufferSize(const AudioConfig& config,
66                                                getInputBufferSize_cb _hidl_cb) {
67     return mDevice->getInputBufferSize(config, _hidl_cb);
68 }
69 
70 #if MAJOR_VERSION == 2
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,openOutputStream_cb _hidl_cb)71 Return<void> PrimaryDevice::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
72                                              const AudioConfig& config,
73                                              AudioOutputFlagBitfield flags,
74                                              openOutputStream_cb _hidl_cb) {
75     return mDevice->openOutputStream(ioHandle, device, config, flags, _hidl_cb);
76 }
77 
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,AudioSource source,openInputStream_cb _hidl_cb)78 Return<void> PrimaryDevice::openInputStream(int32_t ioHandle, const DeviceAddress& device,
79                                             const AudioConfig& config, AudioInputFlagBitfield flags,
80                                             AudioSource source, openInputStream_cb _hidl_cb) {
81     return mDevice->openInputStream(ioHandle, device, config, flags, source, _hidl_cb);
82 }
83 #elif MAJOR_VERSION >= 4
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,const SourceMetadata & sourceMetadata,openOutputStream_cb _hidl_cb)84 Return<void> PrimaryDevice::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
85                                              const AudioConfig& config,
86                                              AudioOutputFlagBitfield flags,
87                                              const SourceMetadata& sourceMetadata,
88                                              openOutputStream_cb _hidl_cb) {
89     return mDevice->openOutputStream(ioHandle, device, config, flags, sourceMetadata, _hidl_cb);
90 }
91 
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,const SinkMetadata & sinkMetadata,openInputStream_cb _hidl_cb)92 Return<void> PrimaryDevice::openInputStream(int32_t ioHandle, const DeviceAddress& device,
93                                             const AudioConfig& config, AudioInputFlagBitfield flags,
94                                             const SinkMetadata& sinkMetadata,
95                                             openInputStream_cb _hidl_cb) {
96     return mDevice->openInputStream(ioHandle, device, config, flags, sinkMetadata, _hidl_cb);
97 }
98 #endif
99 
supportsAudioPatches()100 Return<bool> PrimaryDevice::supportsAudioPatches() {
101     return mDevice->supportsAudioPatches();
102 }
103 
createAudioPatch(const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,createAudioPatch_cb _hidl_cb)104 Return<void> PrimaryDevice::createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
105                                              const hidl_vec<AudioPortConfig>& sinks,
106                                              createAudioPatch_cb _hidl_cb) {
107     return mDevice->createAudioPatch(sources, sinks, _hidl_cb);
108 }
109 
releaseAudioPatch(int32_t patch)110 Return<Result> PrimaryDevice::releaseAudioPatch(int32_t patch) {
111     return mDevice->releaseAudioPatch(patch);
112 }
113 
getAudioPort(const AudioPort & port,getAudioPort_cb _hidl_cb)114 Return<void> PrimaryDevice::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
115     return mDevice->getAudioPort(port, _hidl_cb);
116 }
117 
setAudioPortConfig(const AudioPortConfig & config)118 Return<Result> PrimaryDevice::setAudioPortConfig(const AudioPortConfig& config) {
119     return mDevice->setAudioPortConfig(config);
120 }
121 
setScreenState(bool turnedOn)122 Return<Result> PrimaryDevice::setScreenState(bool turnedOn) {
123     return mDevice->setScreenState(turnedOn);
124 }
125 
126 #if MAJOR_VERSION == 2
getHwAvSync()127 Return<AudioHwSync> PrimaryDevice::getHwAvSync() {
128     return mDevice->getHwAvSync();
129 }
130 
getParameters(const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)131 Return<void> PrimaryDevice::getParameters(const hidl_vec<hidl_string>& keys,
132                                           getParameters_cb _hidl_cb) {
133     return mDevice->getParameters(keys, _hidl_cb);
134 }
135 
setParameters(const hidl_vec<ParameterValue> & parameters)136 Return<Result> PrimaryDevice::setParameters(const hidl_vec<ParameterValue>& parameters) {
137     return mDevice->setParameters(parameters);
138 }
139 
debugDump(const hidl_handle & fd)140 Return<void> PrimaryDevice::debugDump(const hidl_handle& fd) {
141     return mDevice->debugDump(fd);
142 }
143 #elif MAJOR_VERSION >= 4
getHwAvSync(getHwAvSync_cb _hidl_cb)144 Return<void> PrimaryDevice::getHwAvSync(getHwAvSync_cb _hidl_cb) {
145     return mDevice->getHwAvSync(_hidl_cb);
146 }
getParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)147 Return<void> PrimaryDevice::getParameters(const hidl_vec<ParameterValue>& context,
148                                           const hidl_vec<hidl_string>& keys,
149                                           getParameters_cb _hidl_cb) {
150     return mDevice->getParameters(context, keys, _hidl_cb);
151 }
setParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)152 Return<Result> PrimaryDevice::setParameters(const hidl_vec<ParameterValue>& context,
153                                             const hidl_vec<ParameterValue>& parameters) {
154     return mDevice->setParameters(context, parameters);
155 }
getMicrophones(getMicrophones_cb _hidl_cb)156 Return<void> PrimaryDevice::getMicrophones(getMicrophones_cb _hidl_cb) {
157     return mDevice->getMicrophones(_hidl_cb);
158 }
setConnectedState(const DeviceAddress & address,bool connected)159 Return<Result> PrimaryDevice::setConnectedState(const DeviceAddress& address, bool connected) {
160     return mDevice->setConnectedState(address, connected);
161 }
162 #endif
163 
164 // Methods from ::android::hardware::audio::CPP_VERSION::IPrimaryDevice follow.
setVoiceVolume(float volume)165 Return<Result> PrimaryDevice::setVoiceVolume(float volume) {
166     if (!isGainNormalized(volume)) {
167         ALOGW("Can not set a voice volume (%f) outside [0,1]", volume);
168         return Result::INVALID_ARGUMENTS;
169     }
170     return mDevice->analyzeStatus("set_voice_volume",
171                                   mDevice->device()->set_voice_volume(mDevice->device(), volume));
172 }
173 
setMode(AudioMode mode)174 Return<Result> PrimaryDevice::setMode(AudioMode mode) {
175     // INVALID, CURRENT, CNT, MAX are reserved for internal use.
176     // TODO: remove the values from the HIDL interface
177     switch (mode) {
178         case AudioMode::NORMAL:
179         case AudioMode::RINGTONE:
180         case AudioMode::IN_CALL:
181         case AudioMode::IN_COMMUNICATION:
182             break;  // Valid values
183         default:
184             return Result::INVALID_ARGUMENTS;
185     };
186 
187     return mDevice->analyzeStatus(
188         "set_mode",
189         mDevice->device()->set_mode(mDevice->device(), static_cast<audio_mode_t>(mode)));
190 }
191 
getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb)192 Return<void> PrimaryDevice::getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb) {
193     bool enabled;
194     Result retval = mDevice->getParam(AudioParameter::keyBtNrec, &enabled);
195     _hidl_cb(retval, enabled);
196     return Void();
197 }
198 
setBtScoNrecEnabled(bool enabled)199 Return<Result> PrimaryDevice::setBtScoNrecEnabled(bool enabled) {
200     return mDevice->setParam(AudioParameter::keyBtNrec, enabled);
201 }
202 
getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb)203 Return<void> PrimaryDevice::getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb) {
204     bool enabled;
205     Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_BT_SCO_WB, &enabled);
206     _hidl_cb(retval, enabled);
207     return Void();
208 }
209 
setBtScoWidebandEnabled(bool enabled)210 Return<Result> PrimaryDevice::setBtScoWidebandEnabled(bool enabled) {
211     return mDevice->setParam(AUDIO_PARAMETER_KEY_BT_SCO_WB, enabled);
212 }
213 
convertTtyModeFromHIDL(IPrimaryDevice::TtyMode mode)214 static const char* convertTtyModeFromHIDL(IPrimaryDevice::TtyMode mode) {
215     switch (mode) {
216         case IPrimaryDevice::TtyMode::OFF:
217             return AUDIO_PARAMETER_VALUE_TTY_OFF;
218         case IPrimaryDevice::TtyMode::VCO:
219             return AUDIO_PARAMETER_VALUE_TTY_VCO;
220         case IPrimaryDevice::TtyMode::HCO:
221             return AUDIO_PARAMETER_VALUE_TTY_HCO;
222         case IPrimaryDevice::TtyMode::FULL:
223             return AUDIO_PARAMETER_VALUE_TTY_FULL;
224         default:
225             return nullptr;
226     }
227 }
convertTtyModeToHIDL(const char * halMode)228 static IPrimaryDevice::TtyMode convertTtyModeToHIDL(const char* halMode) {
229     if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
230         return IPrimaryDevice::TtyMode::OFF;
231     else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
232         return IPrimaryDevice::TtyMode::VCO;
233     else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
234         return IPrimaryDevice::TtyMode::HCO;
235     else if (strcmp(halMode, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
236         return IPrimaryDevice::TtyMode::FULL;
237     return IPrimaryDevice::TtyMode(-1);
238 }
239 
getTtyMode(getTtyMode_cb _hidl_cb)240 Return<void> PrimaryDevice::getTtyMode(getTtyMode_cb _hidl_cb) {
241     String8 halMode;
242     Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_TTY_MODE, &halMode);
243     if (retval != Result::OK) {
244         _hidl_cb(retval, TtyMode::OFF);
245         return Void();
246     }
247     TtyMode mode = convertTtyModeToHIDL(halMode);
248     if (mode == TtyMode(-1)) {
249         ALOGE("HAL returned invalid TTY value: %s", halMode.c_str());
250         _hidl_cb(Result::INVALID_STATE, TtyMode::OFF);
251         return Void();
252     }
253     _hidl_cb(Result::OK, mode);
254     return Void();
255 }
256 
setTtyMode(IPrimaryDevice::TtyMode mode)257 Return<Result> PrimaryDevice::setTtyMode(IPrimaryDevice::TtyMode mode) {
258     const char* modeStr = convertTtyModeFromHIDL(mode);
259     if (modeStr == nullptr) {
260         ALOGW("Can not set an invalid TTY value: %d", mode);
261         return Result::INVALID_ARGUMENTS;
262     }
263     return mDevice->setParam(AUDIO_PARAMETER_KEY_TTY_MODE, modeStr);
264 }
265 
getHacEnabled(getHacEnabled_cb _hidl_cb)266 Return<void> PrimaryDevice::getHacEnabled(getHacEnabled_cb _hidl_cb) {
267     bool enabled;
268     Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_HAC, &enabled);
269     _hidl_cb(retval, enabled);
270     return Void();
271 }
272 
setHacEnabled(bool enabled)273 Return<Result> PrimaryDevice::setHacEnabled(bool enabled) {
274     return mDevice->setParam(AUDIO_PARAMETER_KEY_HAC, enabled);
275 }
276 
277 #if MAJOR_VERSION >= 4
setBtScoHeadsetDebugName(const hidl_string & name)278 Return<Result> PrimaryDevice::setBtScoHeadsetDebugName(const hidl_string& name) {
279     return mDevice->setParam(AUDIO_PARAMETER_KEY_BT_SCO_HEADSET_NAME, name.c_str());
280 }
getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb)281 Return<void> PrimaryDevice::getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb) {
282     bool enabled;
283     Result retval = mDevice->getParam(AUDIO_PARAMETER_KEY_HFP_ENABLE, &enabled);
284     _hidl_cb(retval, enabled);
285     return Void();
286 }
setBtHfpEnabled(bool enabled)287 Return<Result> PrimaryDevice::setBtHfpEnabled(bool enabled) {
288     return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_ENABLE, enabled);
289 }
setBtHfpSampleRate(uint32_t sampleRateHz)290 Return<Result> PrimaryDevice::setBtHfpSampleRate(uint32_t sampleRateHz) {
291     return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_SET_SAMPLING_RATE, int(sampleRateHz));
292 }
setBtHfpVolume(float volume)293 Return<Result> PrimaryDevice::setBtHfpVolume(float volume) {
294     if (!isGainNormalized(volume)) {
295         ALOGW("Can not set BT HFP volume (%f) outside [0,1]", volume);
296         return Result::INVALID_ARGUMENTS;
297     }
298     // Map the normalized volume onto the range of [0, 15]
299     return mDevice->setParam(AUDIO_PARAMETER_KEY_HFP_VOLUME,
300                              static_cast<int>(std::round(volume * 15)));
301 }
updateRotation(IPrimaryDevice::Rotation rotation)302 Return<Result> PrimaryDevice::updateRotation(IPrimaryDevice::Rotation rotation) {
303     // legacy API expects the rotation in degree
304     return mDevice->setParam(AUDIO_PARAMETER_KEY_ROTATION, int(rotation) * 90);
305 }
306 #endif
307 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)308 Return<void> PrimaryDevice::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
309     return mDevice->debug(fd, options);
310 }
311 
312 }  // namespace implementation
313 }  // namespace CPP_VERSION
314 }  // namespace audio
315 }  // namespace hardware
316 }  // namespace android
317