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 "DeviceHAL"
18 
19 #include "core/default/Device.h"
20 #include <HidlUtils.h>
21 #include "common/all-versions/default/EffectMap.h"
22 #include "core/default/Conversions.h"
23 #include "core/default/StreamIn.h"
24 #include "core/default/StreamOut.h"
25 #include "core/default/Util.h"
26 
27 //#define LOG_NDEBUG 0
28 
29 #include <inttypes.h>
30 #include <memory.h>
31 #include <string.h>
32 #include <algorithm>
33 
34 #include <android/log.h>
35 
36 namespace android {
37 namespace hardware {
38 namespace audio {
39 namespace CPP_VERSION {
40 namespace implementation {
41 
42 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
43 
Device(audio_hw_device_t * device)44 Device::Device(audio_hw_device_t* device) : mIsClosed(false), mDevice(device) {}
45 
~Device()46 Device::~Device() {
47     (void)doClose();
48     mDevice = nullptr;
49 }
50 
analyzeStatus(const char * funcName,int status,const std::vector<int> & ignoreErrors)51 Result Device::analyzeStatus(const char* funcName, int status,
52                              const std::vector<int>& ignoreErrors) {
53     return util::analyzeStatus("Device", funcName, status, ignoreErrors);
54 }
55 
closeInputStream(audio_stream_in_t * stream)56 void Device::closeInputStream(audio_stream_in_t* stream) {
57     mDevice->close_input_stream(mDevice, stream);
58     LOG_ALWAYS_FATAL_IF(mOpenedStreamsCount == 0, "mOpenedStreamsCount is already 0");
59     --mOpenedStreamsCount;
60 }
61 
closeOutputStream(audio_stream_out_t * stream)62 void Device::closeOutputStream(audio_stream_out_t* stream) {
63     mDevice->close_output_stream(mDevice, stream);
64     LOG_ALWAYS_FATAL_IF(mOpenedStreamsCount == 0, "mOpenedStreamsCount is already 0");
65     --mOpenedStreamsCount;
66 }
67 
halGetParameters(const char * keys)68 char* Device::halGetParameters(const char* keys) {
69     return mDevice->get_parameters(mDevice, keys);
70 }
71 
halSetParameters(const char * keysAndValues)72 int Device::halSetParameters(const char* keysAndValues) {
73     return mDevice->set_parameters(mDevice, keysAndValues);
74 }
75 
76 // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow.
initCheck()77 Return<Result> Device::initCheck() {
78     return analyzeStatus("init_check", mDevice->init_check(mDevice));
79 }
80 
setMasterVolume(float volume)81 Return<Result> Device::setMasterVolume(float volume) {
82     if (mDevice->set_master_volume == NULL) {
83         return Result::NOT_SUPPORTED;
84     }
85     if (!isGainNormalized(volume)) {
86         ALOGW("Can not set a master volume (%f) outside [0,1]", volume);
87         return Result::INVALID_ARGUMENTS;
88     }
89     return analyzeStatus("set_master_volume", mDevice->set_master_volume(mDevice, volume),
90                          {ENOSYS} /*ignore*/);
91 }
92 
getMasterVolume(getMasterVolume_cb _hidl_cb)93 Return<void> Device::getMasterVolume(getMasterVolume_cb _hidl_cb) {
94     Result retval(Result::NOT_SUPPORTED);
95     float volume = 0;
96     if (mDevice->get_master_volume != NULL) {
97         retval = analyzeStatus("get_master_volume", mDevice->get_master_volume(mDevice, &volume),
98                                {ENOSYS} /*ignore*/);
99     }
100     _hidl_cb(retval, volume);
101     return Void();
102 }
103 
setMicMute(bool mute)104 Return<Result> Device::setMicMute(bool mute) {
105     return analyzeStatus("set_mic_mute", mDevice->set_mic_mute(mDevice, mute), {ENOSYS} /*ignore*/);
106 }
107 
getMicMute(getMicMute_cb _hidl_cb)108 Return<void> Device::getMicMute(getMicMute_cb _hidl_cb) {
109     bool mute = false;
110     Result retval = analyzeStatus("get_mic_mute", mDevice->get_mic_mute(mDevice, &mute),
111                                   {ENOSYS} /*ignore*/);
112     _hidl_cb(retval, mute);
113     return Void();
114 }
115 
setMasterMute(bool mute)116 Return<Result> Device::setMasterMute(bool mute) {
117     Result retval(Result::NOT_SUPPORTED);
118     if (mDevice->set_master_mute != NULL) {
119         retval = analyzeStatus("set_master_mute", mDevice->set_master_mute(mDevice, mute),
120                                {ENOSYS} /*ignore*/);
121     }
122     return retval;
123 }
124 
getMasterMute(getMasterMute_cb _hidl_cb)125 Return<void> Device::getMasterMute(getMasterMute_cb _hidl_cb) {
126     Result retval(Result::NOT_SUPPORTED);
127     bool mute = false;
128     if (mDevice->get_master_mute != NULL) {
129         retval = analyzeStatus("get_master_mute", mDevice->get_master_mute(mDevice, &mute),
130                                {ENOSYS} /*ignore*/);
131     }
132     _hidl_cb(retval, mute);
133     return Void();
134 }
135 
getInputBufferSize(const AudioConfig & config,getInputBufferSize_cb _hidl_cb)136 Return<void> Device::getInputBufferSize(const AudioConfig& config, getInputBufferSize_cb _hidl_cb) {
137     audio_config_t halConfig;
138     HidlUtils::audioConfigToHal(config, &halConfig);
139     size_t halBufferSize = mDevice->get_input_buffer_size(mDevice, &halConfig);
140     Result retval(Result::INVALID_ARGUMENTS);
141     uint64_t bufferSize = 0;
142     if (halBufferSize != 0) {
143         retval = Result::OK;
144         bufferSize = halBufferSize;
145     }
146     _hidl_cb(retval, bufferSize);
147     return Void();
148 }
149 
openOutputStreamImpl(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,AudioConfig * suggestedConfig)150 std::tuple<Result, sp<IStreamOut>> Device::openOutputStreamImpl(int32_t ioHandle,
151                                                                 const DeviceAddress& device,
152                                                                 const AudioConfig& config,
153                                                                 AudioOutputFlagBitfield flags,
154                                                                 AudioConfig* suggestedConfig) {
155     audio_config_t halConfig;
156     HidlUtils::audioConfigToHal(config, &halConfig);
157     audio_stream_out_t* halStream;
158     ALOGV(
159         "open_output_stream handle: %d devices: %x flags: %#x "
160         "srate: %d format %#x channels %x address %s",
161         ioHandle, static_cast<audio_devices_t>(device.device),
162         static_cast<audio_output_flags_t>(flags), halConfig.sample_rate, halConfig.format,
163         halConfig.channel_mask, deviceAddressToHal(device).c_str());
164     int status =
165         mDevice->open_output_stream(mDevice, ioHandle, static_cast<audio_devices_t>(device.device),
166                                     static_cast<audio_output_flags_t>(flags), &halConfig,
167                                     &halStream, deviceAddressToHal(device).c_str());
168     ALOGV("open_output_stream status %d stream %p", status, halStream);
169     sp<IStreamOut> streamOut;
170     if (status == OK) {
171         streamOut = new StreamOut(this, halStream);
172         ++mOpenedStreamsCount;
173     }
174     HidlUtils::audioConfigFromHal(halConfig, suggestedConfig);
175     return {analyzeStatus("open_output_stream", status, {EINVAL} /*ignore*/), streamOut};
176 }
177 
openInputStreamImpl(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,AudioSource source,AudioConfig * suggestedConfig)178 std::tuple<Result, sp<IStreamIn>> Device::openInputStreamImpl(
179     int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config,
180     AudioInputFlagBitfield flags, AudioSource source, AudioConfig* suggestedConfig) {
181     audio_config_t halConfig;
182     HidlUtils::audioConfigToHal(config, &halConfig);
183     audio_stream_in_t* halStream;
184     ALOGV(
185         "open_input_stream handle: %d devices: %x flags: %#x "
186         "srate: %d format %#x channels %x address %s source %d",
187         ioHandle, static_cast<audio_devices_t>(device.device),
188         static_cast<audio_input_flags_t>(flags), halConfig.sample_rate, halConfig.format,
189         halConfig.channel_mask, deviceAddressToHal(device).c_str(),
190         static_cast<audio_source_t>(source));
191     int status = mDevice->open_input_stream(
192         mDevice, ioHandle, static_cast<audio_devices_t>(device.device), &halConfig, &halStream,
193         static_cast<audio_input_flags_t>(flags), deviceAddressToHal(device).c_str(),
194         static_cast<audio_source_t>(source));
195     ALOGV("open_input_stream status %d stream %p", status, halStream);
196     sp<IStreamIn> streamIn;
197     if (status == OK) {
198         streamIn = new StreamIn(this, halStream);
199         ++mOpenedStreamsCount;
200     }
201     HidlUtils::audioConfigFromHal(halConfig, suggestedConfig);
202     return {analyzeStatus("open_input_stream", status, {EINVAL} /*ignore*/), streamIn};
203 }
204 
205 #if MAJOR_VERSION == 2
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,openOutputStream_cb _hidl_cb)206 Return<void> Device::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
207                                       const AudioConfig& config, AudioOutputFlagBitfield flags,
208                                       openOutputStream_cb _hidl_cb) {
209     AudioConfig suggestedConfig;
210     auto [result, streamOut] =
211         openOutputStreamImpl(ioHandle, device, config, flags, &suggestedConfig);
212     _hidl_cb(result, streamOut, suggestedConfig);
213     return Void();
214 }
215 
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,AudioSource source,openInputStream_cb _hidl_cb)216 Return<void> Device::openInputStream(int32_t ioHandle, const DeviceAddress& device,
217                                      const AudioConfig& config, AudioInputFlagBitfield flags,
218                                      AudioSource source, openInputStream_cb _hidl_cb) {
219     AudioConfig suggestedConfig;
220     auto [result, streamIn] =
221         openInputStreamImpl(ioHandle, device, config, flags, source, &suggestedConfig);
222     _hidl_cb(result, streamIn, suggestedConfig);
223     return Void();
224 }
225 
226 #elif MAJOR_VERSION >= 4
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioOutputFlagBitfield flags,const SourceMetadata & sourceMetadata,openOutputStream_cb _hidl_cb)227 Return<void> Device::openOutputStream(int32_t ioHandle, const DeviceAddress& device,
228                                       const AudioConfig& config, AudioOutputFlagBitfield flags,
229                                       const SourceMetadata& sourceMetadata,
230                                       openOutputStream_cb _hidl_cb) {
231     AudioConfig suggestedConfig;
232     auto [result, streamOut] =
233         openOutputStreamImpl(ioHandle, device, config, flags, &suggestedConfig);
234     if (streamOut) {
235         streamOut->updateSourceMetadata(sourceMetadata);
236     }
237     _hidl_cb(result, streamOut, suggestedConfig);
238     return Void();
239 }
240 
openInputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,AudioInputFlagBitfield flags,const SinkMetadata & sinkMetadata,openInputStream_cb _hidl_cb)241 Return<void> Device::openInputStream(int32_t ioHandle, const DeviceAddress& device,
242                                      const AudioConfig& config, AudioInputFlagBitfield flags,
243                                      const SinkMetadata& sinkMetadata,
244                                      openInputStream_cb _hidl_cb) {
245     if (sinkMetadata.tracks.size() == 0) {
246         // This should never happen, the framework must not create as stream
247         // if there is no client
248         ALOGE("openInputStream called without tracks connected");
249         _hidl_cb(Result::INVALID_ARGUMENTS, nullptr, AudioConfig());
250         return Void();
251     }
252     // Pick the first one as the main.
253     AudioSource source = sinkMetadata.tracks[0].source;
254     AudioConfig suggestedConfig;
255     auto [result, streamIn] =
256         openInputStreamImpl(ioHandle, device, config, flags, source, &suggestedConfig);
257     if (streamIn) {
258         streamIn->updateSinkMetadata(sinkMetadata);
259     }
260     _hidl_cb(result, streamIn, suggestedConfig);
261     return Void();
262 }
263 #endif /* MAJOR_VERSION */
264 
supportsAudioPatches()265 Return<bool> Device::supportsAudioPatches() {
266     return version() >= AUDIO_DEVICE_API_VERSION_3_0;
267 }
268 
createAudioPatch(const hidl_vec<AudioPortConfig> & sources,const hidl_vec<AudioPortConfig> & sinks,createAudioPatch_cb _hidl_cb)269 Return<void> Device::createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
270                                       const hidl_vec<AudioPortConfig>& sinks,
271                                       createAudioPatch_cb _hidl_cb) {
272     Result retval(Result::NOT_SUPPORTED);
273     AudioPatchHandle patch = 0;
274     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
275         std::unique_ptr<audio_port_config[]> halSources(HidlUtils::audioPortConfigsToHal(sources));
276         std::unique_ptr<audio_port_config[]> halSinks(HidlUtils::audioPortConfigsToHal(sinks));
277         audio_patch_handle_t halPatch = AUDIO_PATCH_HANDLE_NONE;
278         retval = analyzeStatus("create_audio_patch",
279                                mDevice->create_audio_patch(mDevice, sources.size(), &halSources[0],
280                                                            sinks.size(), &halSinks[0], &halPatch));
281         if (retval == Result::OK) {
282             patch = static_cast<AudioPatchHandle>(halPatch);
283         }
284     }
285     _hidl_cb(retval, patch);
286     return Void();
287 }
288 
releaseAudioPatch(int32_t patch)289 Return<Result> Device::releaseAudioPatch(int32_t patch) {
290     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
291         return analyzeStatus(
292             "release_audio_patch",
293             mDevice->release_audio_patch(mDevice, static_cast<audio_patch_handle_t>(patch)));
294     }
295     return Result::NOT_SUPPORTED;
296 }
297 
getAudioPort(const AudioPort & port,getAudioPort_cb _hidl_cb)298 Return<void> Device::getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) {
299     audio_port halPort;
300     HidlUtils::audioPortToHal(port, &halPort);
301     Result retval = analyzeStatus("get_audio_port", mDevice->get_audio_port(mDevice, &halPort));
302     AudioPort resultPort = port;
303     if (retval == Result::OK) {
304         HidlUtils::audioPortFromHal(halPort, &resultPort);
305     }
306     _hidl_cb(retval, resultPort);
307     return Void();
308 }
309 
setAudioPortConfig(const AudioPortConfig & config)310 Return<Result> Device::setAudioPortConfig(const AudioPortConfig& config) {
311     if (version() >= AUDIO_DEVICE_API_VERSION_3_0) {
312         struct audio_port_config halPortConfig;
313         HidlUtils::audioPortConfigToHal(config, &halPortConfig);
314         return analyzeStatus("set_audio_port_config",
315                              mDevice->set_audio_port_config(mDevice, &halPortConfig));
316     }
317     return Result::NOT_SUPPORTED;
318 }
319 
320 #if MAJOR_VERSION == 2
getHwAvSync()321 Return<AudioHwSync> Device::getHwAvSync() {
322     int halHwAvSync;
323     Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
324     return retval == Result::OK ? halHwAvSync : AUDIO_HW_SYNC_INVALID;
325 }
326 #elif MAJOR_VERSION >= 4
getHwAvSync(getHwAvSync_cb _hidl_cb)327 Return<void> Device::getHwAvSync(getHwAvSync_cb _hidl_cb) {
328     int halHwAvSync;
329     Result retval = getParam(AudioParameter::keyHwAvSync, &halHwAvSync);
330     _hidl_cb(retval, halHwAvSync);
331     return Void();
332 }
333 #endif
334 
setScreenState(bool turnedOn)335 Return<Result> Device::setScreenState(bool turnedOn) {
336     return setParam(AudioParameter::keyScreenState, turnedOn);
337 }
338 
339 #if MAJOR_VERSION == 2
getParameters(const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)340 Return<void> Device::getParameters(const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
341     getParametersImpl({}, keys, _hidl_cb);
342     return Void();
343 }
344 
setParameters(const hidl_vec<ParameterValue> & parameters)345 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& parameters) {
346     return setParametersImpl({} /* context */, parameters);
347 }
348 #elif MAJOR_VERSION >= 4
getParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,getParameters_cb _hidl_cb)349 Return<void> Device::getParameters(const hidl_vec<ParameterValue>& context,
350                                    const hidl_vec<hidl_string>& keys, getParameters_cb _hidl_cb) {
351     getParametersImpl(context, keys, _hidl_cb);
352     return Void();
353 }
setParameters(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)354 Return<Result> Device::setParameters(const hidl_vec<ParameterValue>& context,
355                                      const hidl_vec<ParameterValue>& parameters) {
356     return setParametersImpl(context, parameters);
357 }
358 #endif
359 
360 #if MAJOR_VERSION == 2
debugDump(const hidl_handle & fd)361 Return<void> Device::debugDump(const hidl_handle& fd) {
362     return debug(fd, {});
363 }
364 #endif
365 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> &)366 Return<void> Device::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
367     if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
368         analyzeStatus("dump", mDevice->dump(mDevice, fd->data[0]));
369     }
370     return Void();
371 }
372 
373 #if MAJOR_VERSION >= 4
getMicrophones(getMicrophones_cb _hidl_cb)374 Return<void> Device::getMicrophones(getMicrophones_cb _hidl_cb) {
375     Result retval = Result::NOT_SUPPORTED;
376     size_t actual_mics = AUDIO_MICROPHONE_MAX_COUNT;
377     audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
378 
379     hidl_vec<MicrophoneInfo> microphones;
380     if (mDevice->get_microphones != NULL &&
381         mDevice->get_microphones(mDevice, &mic_array[0], &actual_mics) == 0) {
382         microphones.resize(actual_mics);
383         for (size_t i = 0; i < actual_mics; ++i) {
384             halToMicrophoneCharacteristics(&microphones[i], mic_array[i]);
385         }
386         retval = Result::OK;
387     }
388     _hidl_cb(retval, microphones);
389     return Void();
390 }
391 
setConnectedState(const DeviceAddress & address,bool connected)392 Return<Result> Device::setConnectedState(const DeviceAddress& address, bool connected) {
393     auto key = connected ? AudioParameter::keyDeviceConnect : AudioParameter::keyDeviceDisconnect;
394     return setParam(key, address);
395 }
396 #endif
397 
doClose()398 Result Device::doClose() {
399     if (mIsClosed || mOpenedStreamsCount != 0) return Result::INVALID_STATE;
400     mIsClosed = true;
401     return analyzeStatus("close", audio_hw_device_close(mDevice));
402 }
403 
404 #if MAJOR_VERSION >= 6
close()405 Return<Result> Device::close() {
406     return doClose();
407 }
408 
addDeviceEffect(AudioPortHandle device,uint64_t effectId)409 Return<Result> Device::addDeviceEffect(AudioPortHandle device, uint64_t effectId) {
410     if (version() < AUDIO_DEVICE_API_VERSION_3_1 || mDevice->add_device_effect == nullptr) {
411         return Result::NOT_SUPPORTED;
412     }
413 
414     effect_handle_t halEffect = EffectMap::getInstance().get(effectId);
415     if (halEffect != NULL) {
416         return analyzeStatus("add_device_effect",
417                              mDevice->add_device_effect(
418                                      mDevice, static_cast<audio_port_handle_t>(device), halEffect));
419     } else {
420         ALOGW("%s Invalid effect ID passed from client: %" PRIu64 "", __func__, effectId);
421         return Result::INVALID_ARGUMENTS;
422     }
423 }
424 
removeDeviceEffect(AudioPortHandle device,uint64_t effectId)425 Return<Result> Device::removeDeviceEffect(AudioPortHandle device, uint64_t effectId) {
426     if (version() < AUDIO_DEVICE_API_VERSION_3_1 || mDevice->remove_device_effect == nullptr) {
427         return Result::NOT_SUPPORTED;
428     }
429 
430     effect_handle_t halEffect = EffectMap::getInstance().get(effectId);
431     if (halEffect != NULL) {
432         return analyzeStatus("remove_device_effect",
433                              mDevice->remove_device_effect(
434                                      mDevice, static_cast<audio_port_handle_t>(device), halEffect));
435     } else {
436         ALOGW("%s Invalid effect ID passed from client: %" PRIu64 "", __func__, effectId);
437         return Result::INVALID_ARGUMENTS;
438     }
439 }
440 
441 #endif
442 
443 }  // namespace implementation
444 }  // namespace CPP_VERSION
445 }  // namespace audio
446 }  // namespace hardware
447 }  // namespace android
448