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