blob: f09db5ed98d457cc18613caf7fcb4af78b4266dc [file] [log] [blame]
Mikhail Naganov1b444a52020-10-29 13:08:05 -07001/*
2 * Copyright (C) 2020 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 <stdio.h>
18#include <string.h>
19
20#define LOG_TAG "HidlUtils"
21#include <log/log.h>
22
23#include <android_audio_policy_configuration_V7_0-enums.h>
Mikhail Naganov3f1457b2020-12-17 15:01:54 -080024#include <common/all-versions/HidlSupport.h>
Mikhail Naganov1b444a52020-10-29 13:08:05 -070025#include <common/all-versions/VersionUtils.h>
26
27#include "HidlUtils.h"
28
29namespace android {
30namespace hardware {
31namespace audio {
32namespace common {
33namespace CPP_VERSION {
34namespace implementation {
35
36namespace xsd {
37using namespace ::android::audio::policy::configuration::V7_0;
38}
39
40#define CONVERT_CHECKED(expr, result) \
41 if (status_t status = (expr); status != NO_ERROR) { \
42 result = status; \
43 }
44
45status_t HidlUtils::audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
46 AudioChannelMask* channelMask) {
47 *channelMask = audio_channel_index_mask_to_string(halChannelMask);
48 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
49 return NO_ERROR;
50 }
51 ALOGE("Unknown index channel mask value 0x%X", halChannelMask);
52 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
53 return BAD_VALUE;
54}
55
56status_t HidlUtils::audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
57 AudioChannelMask* channelMask) {
58 *channelMask = audio_channel_in_mask_to_string(halChannelMask);
59 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
60 return NO_ERROR;
61 }
62 ALOGE("Unknown input channel mask value 0x%X", halChannelMask);
63 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
64 return BAD_VALUE;
65}
66
67status_t HidlUtils::audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
68 AudioChannelMask* channelMask) {
69 *channelMask = audio_channel_out_mask_to_string(halChannelMask);
70 if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
71 return NO_ERROR;
72 }
73 ALOGE("Unknown output channel mask value 0x%X", halChannelMask);
74 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
75 return BAD_VALUE;
76}
77
78status_t HidlUtils::audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
79 AudioChannelMask* channelMask) {
80 if (halChannelMask != AUDIO_CHANNEL_NONE) {
81 if (audio_channel_mask_is_valid(halChannelMask)) {
82 switch (audio_channel_mask_get_representation(halChannelMask)) {
83 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
84 return isInput ? audioInputChannelMaskFromHal(halChannelMask, channelMask)
85 : audioOutputChannelMaskFromHal(halChannelMask, channelMask);
86 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
87 // Index masks do not have direction.
88 return audioIndexChannelMaskFromHal(halChannelMask, channelMask);
89 // no default
90 }
91 }
92 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
93 return BAD_VALUE;
94 }
95 *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
96 return NO_ERROR;
97}
98
Mikhail Naganovb52e93f2020-12-10 16:10:08 -080099status_t HidlUtils::audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
100 hidl_vec<AudioChannelMask>* channelMasks) {
101 hidl_vec<AudioChannelMask> tempChannelMasks;
102 tempChannelMasks.resize(halChannelMasks.size());
103 size_t tempPos = 0;
104 for (const auto& halChannelMask : halChannelMasks) {
105 if (!halChannelMask.empty() && !xsd::isUnknownAudioChannelMask(halChannelMask)) {
106 tempChannelMasks[tempPos++] = halChannelMask;
107 }
108 }
109 if (tempPos == tempChannelMasks.size()) {
110 *channelMasks = std::move(tempChannelMasks);
111 } else {
112 *channelMasks = hidl_vec<AudioChannelMask>(tempChannelMasks.begin(),
113 tempChannelMasks.begin() + tempPos);
114 }
115 return halChannelMasks.size() == channelMasks->size() ? NO_ERROR : BAD_VALUE;
116}
117
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700118status_t HidlUtils::audioChannelMaskToHal(const AudioChannelMask& channelMask,
119 audio_channel_mask_t* halChannelMask) {
120 if (!xsd::isUnknownAudioChannelMask(channelMask) &&
121 audio_channel_mask_from_string(channelMask.c_str(), halChannelMask)) {
122 return NO_ERROR;
123 }
124 ALOGE("Unknown channel mask \"%s\"", channelMask.c_str());
125 *halChannelMask = AUDIO_CHANNEL_NONE;
126 return BAD_VALUE;
127}
128
129status_t HidlUtils::audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
130 AudioConfigBase* configBase) {
131 status_t result = NO_ERROR;
132 configBase->sampleRateHz = halConfigBase.sample_rate;
133 CONVERT_CHECKED(
134 audioChannelMaskFromHal(halConfigBase.channel_mask, isInput, &configBase->channelMask),
135 result);
136 CONVERT_CHECKED(audioFormatFromHal(halConfigBase.format, &configBase->format), result);
137 return result;
138}
139
140status_t HidlUtils::audioConfigBaseToHal(const AudioConfigBase& configBase,
141 audio_config_base_t* halConfigBase) {
142 status_t result = NO_ERROR;
143 halConfigBase->sample_rate = configBase.sampleRateHz;
144 CONVERT_CHECKED(audioChannelMaskToHal(configBase.channelMask, &halConfigBase->channel_mask),
145 result);
146 CONVERT_CHECKED(audioFormatToHal(configBase.format, &halConfigBase->format), result);
147 return result;
148}
149
Mikhail Naganovff611982021-01-27 02:16:53 +0000150status_t HidlUtils::audioConfigBaseOptionalFromHal(const audio_config_base_t& halConfigBase,
151 bool isInput, bool formatSpecified,
152 bool sampleRateSpecified,
153 bool channelMaskSpecified,
154 AudioConfigBaseOptional* configBase) {
155 status_t result = NO_ERROR;
156 if (formatSpecified) {
157 AudioFormat value;
158 CONVERT_CHECKED(audioFormatFromHal(halConfigBase.format, &value), result);
159 configBase->format.value(std::move(value));
160 } else {
161 configBase->format.unspecified({});
162 }
163 if (sampleRateSpecified) {
164 configBase->sampleRateHz.value(halConfigBase.sample_rate);
165 } else {
166 configBase->sampleRateHz.unspecified({});
167 }
168 if (channelMaskSpecified) {
169 AudioChannelMask value;
170 CONVERT_CHECKED(audioChannelMaskFromHal(halConfigBase.channel_mask, isInput, &value),
171 result);
172 configBase->channelMask.value(std::move(value));
173 }
174 return result;
175}
176
177status_t HidlUtils::audioConfigBaseOptionalToHal(const AudioConfigBaseOptional& configBase,
178 audio_config_base_t* halConfigBase,
179 bool* formatSpecified, bool* sampleRateSpecified,
180 bool* channelMaskSpecified) {
181 status_t result = NO_ERROR;
182 *formatSpecified = configBase.format.getDiscriminator() ==
183 AudioConfigBaseOptional::Format::hidl_discriminator::value;
184 if (*formatSpecified) {
185 CONVERT_CHECKED(audioFormatToHal(configBase.format.value(), &halConfigBase->format),
186 result);
187 }
188 *sampleRateSpecified = configBase.sampleRateHz.getDiscriminator() ==
189 AudioConfigBaseOptional::SampleRate::hidl_discriminator::value;
190 if (*sampleRateSpecified) {
191 halConfigBase->sample_rate = configBase.sampleRateHz.value();
192 }
193 *channelMaskSpecified = configBase.channelMask.getDiscriminator() ==
194 AudioConfigBaseOptional::ChannelMask::hidl_discriminator::value;
195 if (*channelMaskSpecified) {
196 CONVERT_CHECKED(
197 audioChannelMaskToHal(configBase.channelMask.value(), &halConfigBase->channel_mask),
198 result);
199 }
200 return result;
201}
202
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800203status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
204 AudioContentType* contentType) {
205 *contentType = audio_content_type_to_string(halContentType);
206 if (!contentType->empty() && !xsd::isUnknownAudioContentType(*contentType)) {
207 return NO_ERROR;
208 }
209 ALOGE("Unknown audio content type value 0x%X", halContentType);
210 *contentType = toString(xsd::AudioContentType::AUDIO_CONTENT_TYPE_UNKNOWN);
211 return BAD_VALUE;
212}
213
214status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
215 audio_content_type_t* halContentType) {
216 if (!xsd::isUnknownAudioContentType(contentType) &&
217 audio_content_type_from_string(contentType.c_str(), halContentType)) {
218 return NO_ERROR;
219 }
220 ALOGE("Unknown audio content type \"%s\"", contentType.c_str());
221 *halContentType = AUDIO_CONTENT_TYPE_UNKNOWN;
222 return BAD_VALUE;
223}
224
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700225status_t HidlUtils::audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device) {
226 *device = audio_device_to_string(halDevice);
227 if (!device->empty() && !xsd::isUnknownAudioDevice(*device)) {
228 return NO_ERROR;
229 }
230 ALOGE("Unknown audio device value 0x%X", halDevice);
231 *device = toString(xsd::AudioDevice::AUDIO_DEVICE_NONE);
232 return BAD_VALUE;
233}
234
235status_t HidlUtils::audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice) {
236 if (!xsd::isUnknownAudioDevice(device) && audio_device_from_string(device.c_str(), halDevice)) {
237 return NO_ERROR;
238 }
239 ALOGE("Unknown audio device \"%s\"", device.c_str());
240 *halDevice = AUDIO_DEVICE_NONE;
241 return BAD_VALUE;
242}
243
244status_t HidlUtils::audioFormatFromHal(audio_format_t halFormat, AudioFormat* format) {
245 *format = audio_format_to_string(halFormat);
246 if (!format->empty() && !xsd::isUnknownAudioFormat(*format)) {
247 return NO_ERROR;
248 }
249 ALOGE("Unknown audio format value 0x%X", halFormat);
250 return BAD_VALUE;
251}
252
Mikhail Naganovb52e93f2020-12-10 16:10:08 -0800253status_t HidlUtils::audioFormatsFromHal(const std::vector<std::string>& halFormats,
254 hidl_vec<AudioFormat>* formats) {
255 hidl_vec<AudioFormat> tempFormats;
256 tempFormats.resize(halFormats.size());
257 size_t tempPos = 0;
258 for (const auto& halFormat : halFormats) {
259 if (!halFormat.empty() && !xsd::isUnknownAudioFormat(halFormat)) {
260 tempFormats[tempPos++] = halFormat;
261 }
262 }
263 if (tempPos == tempFormats.size()) {
264 *formats = std::move(tempFormats);
265 } else {
266 *formats = hidl_vec<AudioFormat>(tempFormats.begin(), tempFormats.begin() + tempPos);
267 }
268 return halFormats.size() == formats->size() ? NO_ERROR : BAD_VALUE;
269}
270
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700271status_t HidlUtils::audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat) {
272 if (!xsd::isUnknownAudioFormat(format) && audio_format_from_string(format.c_str(), halFormat)) {
273 return NO_ERROR;
274 }
275 ALOGE("Unknown audio format \"%s\"", format.c_str());
276 *halFormat = AUDIO_FORMAT_DEFAULT;
277 return BAD_VALUE;
278}
279
280status_t HidlUtils::audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
281 hidl_vec<AudioGainMode>* gainModeMask) {
282 status_t status = NO_ERROR;
283 std::vector<AudioGainMode> result;
284 for (uint32_t bit = 0; bit < sizeof(audio_gain_mode_t) * 8; ++bit) {
285 audio_gain_mode_t flag = static_cast<audio_gain_mode_t>(1u << bit);
286 if ((flag & halGainModeMask) == flag) {
287 AudioGainMode flagStr = audio_gain_mode_to_string(flag);
288 if (!flagStr.empty() && !xsd::isUnknownAudioGainMode(flagStr)) {
289 result.push_back(flagStr);
290 } else {
291 ALOGE("Unknown audio gain mode value 0x%X", flag);
292 status = BAD_VALUE;
293 }
294 }
295 }
296 *gainModeMask = result;
297 return status;
298}
299
300status_t HidlUtils::audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask,
301 audio_gain_mode_t* halGainModeMask) {
302 status_t status = NO_ERROR;
303 *halGainModeMask = {};
304 for (const auto& gainMode : gainModeMask) {
305 audio_gain_mode_t halGainMode;
306 if (!xsd::isUnknownAudioGainMode(gainMode) &&
307 audio_gain_mode_from_string(gainMode.c_str(), &halGainMode)) {
308 *halGainModeMask = static_cast<audio_gain_mode_t>(*halGainModeMask | halGainMode);
309 } else {
310 ALOGE("Unknown audio gain mode \"%s\"", gainMode.c_str());
311 status = BAD_VALUE;
312 }
313 }
314 return status;
315}
316
317status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
318 *source = audio_source_to_string(halSource);
319 if (!source->empty() && !xsd::isUnknownAudioSource(*source)) {
320 return NO_ERROR;
321 }
322 ALOGE("Unknown audio source value 0x%X", halSource);
323 *source = toString(xsd::AudioSource::AUDIO_SOURCE_DEFAULT);
324 return BAD_VALUE;
325}
326
327status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
328 if (!xsd::isUnknownAudioSource(source) && audio_source_from_string(source.c_str(), halSource)) {
329 return NO_ERROR;
330 }
331 ALOGE("Unknown audio source \"%s\"", source.c_str());
332 *halSource = AUDIO_SOURCE_DEFAULT;
333 return BAD_VALUE;
334}
335
336status_t HidlUtils::audioStreamTypeFromHal(audio_stream_type_t halStreamType,
337 AudioStreamType* streamType) {
338 *streamType = audio_stream_type_to_string(halStreamType);
339 if (!streamType->empty() && !xsd::isUnknownAudioStreamType(*streamType)) {
340 return NO_ERROR;
341 }
342 ALOGE("Unknown audio stream type value 0x%X", halStreamType);
343 return BAD_VALUE;
344}
345
346status_t HidlUtils::audioStreamTypeToHal(const AudioStreamType& streamType,
347 audio_stream_type_t* halStreamType) {
348 if (!xsd::isUnknownAudioStreamType(streamType) &&
349 audio_stream_type_from_string(streamType.c_str(), halStreamType)) {
350 return NO_ERROR;
351 }
352 ALOGE("Unknown audio stream type \"%s\"", streamType.c_str());
353 *halStreamType = AUDIO_STREAM_DEFAULT;
354 return BAD_VALUE;
355}
356
357status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
358 AudioConfig* config) {
359 status_t result = NO_ERROR;
360 audio_config_base_t halConfigBase = {halConfig.sample_rate, halConfig.channel_mask,
361 halConfig.format};
362 CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, isInput, &config->base), result);
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800363 if (halConfig.offload_info.sample_rate != 0) {
364 config->offloadInfo.info({});
365 CONVERT_CHECKED(
366 audioOffloadInfoFromHal(halConfig.offload_info, &config->offloadInfo.info()),
367 result);
368 }
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700369 config->frameCount = halConfig.frame_count;
370 return result;
371}
372
373status_t HidlUtils::audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig) {
374 status_t result = NO_ERROR;
375 *halConfig = AUDIO_CONFIG_INITIALIZER;
376 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
377 CONVERT_CHECKED(audioConfigBaseToHal(config.base, &halConfigBase), result);
378 halConfig->sample_rate = halConfigBase.sample_rate;
379 halConfig->channel_mask = halConfigBase.channel_mask;
380 halConfig->format = halConfigBase.format;
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800381 if (config.offloadInfo.getDiscriminator() ==
382 AudioConfig::OffloadInfo::hidl_discriminator::info) {
383 CONVERT_CHECKED(audioOffloadInfoToHal(config.offloadInfo.info(), &halConfig->offload_info),
384 result);
385 }
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700386 halConfig->frame_count = config.frameCount;
387 return result;
388}
389
390status_t HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
391 AudioGainConfig* config) {
392 status_t result = NO_ERROR;
393 config->index = halConfig.index;
394 CONVERT_CHECKED(audioGainModeMaskFromHal(halConfig.mode, &config->mode), result);
395 CONVERT_CHECKED(audioChannelMaskFromHal(halConfig.channel_mask, isInput, &config->channelMask),
396 result);
397 if (halConfig.mode & AUDIO_GAIN_MODE_JOINT) {
398 config->values.resize(1);
399 config->values[0] = halConfig.values[0];
400 }
401 if (halConfig.mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
402 config->values.resize(__builtin_popcount(halConfig.channel_mask));
403 for (size_t i = 0; i < config->values.size(); ++i) {
404 config->values[i] = halConfig.values[i];
405 }
406 }
407 config->rampDurationMs = halConfig.ramp_duration_ms;
408 return result;
409}
410
411status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
412 struct audio_gain_config* halConfig) {
413 status_t result = NO_ERROR;
414 halConfig->index = config.index;
415 CONVERT_CHECKED(audioGainModeMaskToHal(config.mode, &halConfig->mode), result);
416 CONVERT_CHECKED(audioChannelMaskToHal(config.channelMask, &halConfig->channel_mask), result);
417 memset(halConfig->values, 0, sizeof(halConfig->values));
418 if (halConfig->mode & AUDIO_GAIN_MODE_JOINT) {
419 if (config.values.size() > 0) {
420 halConfig->values[0] = config.values[0];
421 } else {
422 ALOGE("Empty values vector in AudioGainConfig");
423 result = BAD_VALUE;
424 }
425 }
426 if (halConfig->mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
427 size_t channelCount = __builtin_popcount(halConfig->channel_mask);
428 size_t valuesCount = config.values.size();
429 if (channelCount != valuesCount) {
430 ALOGE("Wrong number of values in AudioGainConfig, expected: %zu, found: %zu",
431 channelCount, valuesCount);
432 result = BAD_VALUE;
433 if (channelCount < valuesCount) {
434 valuesCount = channelCount;
435 }
436 }
437 for (size_t i = 0; i < valuesCount; ++i) {
438 halConfig->values[i] = config.values[i];
439 }
440 }
441 halConfig->ramp_duration_ms = config.rampDurationMs;
442 return result;
443}
444
445status_t HidlUtils::audioGainFromHal(const struct audio_gain& halGain, bool isInput,
446 AudioGain* gain) {
447 status_t result = NO_ERROR;
448 CONVERT_CHECKED(audioGainModeMaskFromHal(halGain.mode, &gain->mode), result);
449 CONVERT_CHECKED(audioChannelMaskFromHal(halGain.channel_mask, isInput, &gain->channelMask),
450 result);
451 gain->minValue = halGain.min_value;
452 gain->maxValue = halGain.max_value;
453 gain->defaultValue = halGain.default_value;
454 gain->stepValue = halGain.step_value;
455 gain->minRampMs = halGain.min_ramp_ms;
456 gain->maxRampMs = halGain.max_ramp_ms;
457 return result;
458}
459
460status_t HidlUtils::audioGainToHal(const AudioGain& gain, struct audio_gain* halGain) {
461 status_t result = NO_ERROR;
462 CONVERT_CHECKED(audioGainModeMaskToHal(gain.mode, &halGain->mode), result);
463 CONVERT_CHECKED(audioChannelMaskToHal(gain.channelMask, &halGain->channel_mask), result);
464 halGain->min_value = gain.minValue;
465 halGain->max_value = gain.maxValue;
466 halGain->default_value = gain.defaultValue;
467 halGain->step_value = gain.stepValue;
468 halGain->min_ramp_ms = gain.minRampMs;
469 halGain->max_ramp_ms = gain.maxRampMs;
470 return result;
471}
472
473status_t HidlUtils::audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage) {
474 if (halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST ||
475 halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT ||
476 halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED ||
477 halUsage == AUDIO_USAGE_NOTIFICATION_EVENT) {
478 halUsage = AUDIO_USAGE_NOTIFICATION;
479 }
480 *usage = audio_usage_to_string(halUsage);
481 if (!usage->empty() && !xsd::isUnknownAudioUsage(*usage)) {
482 return NO_ERROR;
483 }
484 ALOGE("Unknown audio usage %d", halUsage);
485 *usage = toString(xsd::AudioUsage::AUDIO_USAGE_UNKNOWN);
486 return BAD_VALUE;
487}
488
489status_t HidlUtils::audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage) {
490 if (!xsd::isUnknownAudioUsage(usage) && audio_usage_from_string(usage.c_str(), halUsage)) {
491 return NO_ERROR;
492 }
493 ALOGE("Unknown audio usage \"%s\"", usage.c_str());
494 *halUsage = AUDIO_USAGE_UNKNOWN;
495 return BAD_VALUE;
496}
497
498status_t HidlUtils::audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
499 AudioOffloadInfo* offload) {
500 status_t result = NO_ERROR;
501 audio_config_base_t halConfigBase = {halOffload.sample_rate, halOffload.channel_mask,
502 halOffload.format};
503 CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, false /*isInput*/, &offload->base),
504 result);
505 CONVERT_CHECKED(audioStreamTypeFromHal(halOffload.stream_type, &offload->streamType), result);
506 offload->bitRatePerSecond = halOffload.bit_rate;
507 offload->durationMicroseconds = halOffload.duration_us;
508 offload->hasVideo = halOffload.has_video;
509 offload->isStreaming = halOffload.is_streaming;
510 offload->bitWidth = halOffload.bit_width;
511 offload->bufferSize = halOffload.offload_buffer_size;
512 CONVERT_CHECKED(audioUsageFromHal(halOffload.usage, &offload->usage), result);
513 if (halOffload.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
514 offload->encapsulationMode =
515 static_cast<AudioEncapsulationMode>(halOffload.encapsulation_mode);
516 offload->contentId = halOffload.content_id;
517 offload->syncId = halOffload.sync_id;
518 } else {
519 offload->encapsulationMode = AudioEncapsulationMode::NONE;
520 offload->contentId = 0;
521 offload->syncId = 0;
522 }
523 return result;
524}
525
526status_t HidlUtils::audioOffloadInfoToHal(const AudioOffloadInfo& offload,
527 audio_offload_info_t* halOffload) {
528 status_t result = NO_ERROR;
529 *halOffload = AUDIO_INFO_INITIALIZER;
530 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
531 CONVERT_CHECKED(audioConfigBaseToHal(offload.base, &halConfigBase), result);
532 halOffload->sample_rate = halConfigBase.sample_rate;
533 halOffload->channel_mask = halConfigBase.channel_mask;
534 halOffload->format = halConfigBase.format;
535 CONVERT_CHECKED(audioStreamTypeToHal(offload.streamType, &halOffload->stream_type), result);
536 halOffload->bit_rate = offload.bitRatePerSecond;
537 halOffload->duration_us = offload.durationMicroseconds;
538 halOffload->has_video = offload.hasVideo;
539 halOffload->is_streaming = offload.isStreaming;
540 halOffload->bit_width = offload.bitWidth;
541 halOffload->offload_buffer_size = offload.bufferSize;
542 CONVERT_CHECKED(audioUsageToHal(offload.usage, &halOffload->usage), result);
543 halOffload->encapsulation_mode =
544 static_cast<audio_encapsulation_mode_t>(offload.encapsulationMode);
545 halOffload->content_id = offload.contentId;
546 halOffload->sync_id = offload.syncId;
547 return result;
548}
549
550status_t HidlUtils::audioPortConfigFromHal(const struct audio_port_config& halConfig,
551 AudioPortConfig* config) {
552 status_t result = NO_ERROR;
553 bool isInput = false;
554 config->id = halConfig.id;
555 CONVERT_CHECKED(audioPortExtendedInfoFromHal(halConfig.role, halConfig.type,
556 halConfig.ext.device, halConfig.ext.mix,
557 halConfig.ext.session, &config->ext, &isInput),
558 result);
559 if (audio_port_config_has_input_direction(&halConfig) != isInput) {
560 ALOGE("Inconsistent port config direction data, is input: %d (hal) != %d (converter)",
561 audio_port_config_has_input_direction(&halConfig), isInput);
562 result = BAD_VALUE;
563 }
Mikhail Naganovff611982021-01-27 02:16:53 +0000564 audio_config_base_t halConfigBase = {halConfig.sample_rate, halConfig.channel_mask,
565 halConfig.format};
566 CONVERT_CHECKED(
567 audioConfigBaseOptionalFromHal(
568 halConfigBase, isInput, halConfig.config_mask & AUDIO_PORT_CONFIG_FORMAT,
569 halConfig.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE,
570 halConfig.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK, &config->base),
571 result);
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700572 if (halConfig.config_mask & AUDIO_PORT_CONFIG_GAIN) {
573 config->gain.config({});
574 CONVERT_CHECKED(audioGainConfigFromHal(halConfig.gain, isInput, &config->gain.config()),
575 result);
576 } else {
577 config->gain.unspecified({});
578 }
579 return result;
580}
581
582status_t HidlUtils::audioPortConfigToHal(const AudioPortConfig& config,
583 struct audio_port_config* halConfig) {
584 status_t result = NO_ERROR;
585 memset(halConfig, 0, sizeof(audio_port_config));
586 halConfig->id = config.id;
Mikhail Naganovff611982021-01-27 02:16:53 +0000587 halConfig->config_mask = 0;
588 audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
589 bool formatSpecified = false, sRateSpecified = false, channelMaskSpecified = false;
590 CONVERT_CHECKED(audioConfigBaseOptionalToHal(config.base, &halConfigBase, &formatSpecified,
591 &sRateSpecified, &channelMaskSpecified),
592 result);
593 if (sRateSpecified) {
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700594 halConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
Mikhail Naganovff611982021-01-27 02:16:53 +0000595 halConfig->sample_rate = halConfigBase.sample_rate;
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700596 }
Mikhail Naganovff611982021-01-27 02:16:53 +0000597 if (channelMaskSpecified) {
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700598 halConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
Mikhail Naganovff611982021-01-27 02:16:53 +0000599 halConfig->channel_mask = halConfigBase.channel_mask;
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700600 }
Mikhail Naganovff611982021-01-27 02:16:53 +0000601 if (formatSpecified) {
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700602 halConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
Mikhail Naganovff611982021-01-27 02:16:53 +0000603 halConfig->format = halConfigBase.format;
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700604 }
605 if (config.gain.getDiscriminator() ==
606 AudioPortConfig::OptionalGain::hidl_discriminator::config) {
607 halConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
608 CONVERT_CHECKED(audioGainConfigToHal(config.gain.config(), &halConfig->gain), result);
609 }
610 CONVERT_CHECKED(audioPortExtendedInfoToHal(config.ext, &halConfig->role, &halConfig->type,
611 &halConfig->ext.device, &halConfig->ext.mix,
612 &halConfig->ext.session),
613 result);
614 return result;
615}
616
617status_t HidlUtils::audioPortExtendedInfoFromHal(
618 audio_port_role_t role, audio_port_type_t type,
619 const struct audio_port_config_device_ext& device,
620 const struct audio_port_config_mix_ext& mix,
621 const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext,
622 bool* isInput) {
623 status_t result = NO_ERROR;
624 *isInput = false;
625 switch (type) {
626 case AUDIO_PORT_TYPE_NONE:
627 ext->unspecified({});
628 break;
629 case AUDIO_PORT_TYPE_DEVICE: {
630 *isInput = role == AUDIO_PORT_ROLE_SOURCE;
631 ext->device({});
632 CONVERT_CHECKED(deviceAddressFromHal(device.type, device.address, &ext->device()),
633 result);
634 break;
635 }
636 case AUDIO_PORT_TYPE_MIX: {
637 *isInput = role == AUDIO_PORT_ROLE_SINK;
638 ext->mix({});
639 ext->mix().ioHandle = mix.handle;
640 if (role == AUDIO_PORT_ROLE_SOURCE) {
641 ext->mix().useCase.stream({});
642 CONVERT_CHECKED(
643 audioStreamTypeFromHal(mix.usecase.stream, &ext->mix().useCase.stream()),
644 result);
645 } else if (role == AUDIO_PORT_ROLE_SINK) {
646 ext->mix().useCase.source({});
647 CONVERT_CHECKED(
648 audioSourceFromHal(mix.usecase.source, &ext->mix().useCase.source()),
649 result);
650 }
651 break;
652 }
653 case AUDIO_PORT_TYPE_SESSION: {
654 ext->session(session.session);
655 break;
656 }
657 }
658 return result;
659}
660
661status_t HidlUtils::audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext,
662 audio_port_role_t* role, audio_port_type_t* type,
663 struct audio_port_config_device_ext* device,
664 struct audio_port_config_mix_ext* mix,
665 struct audio_port_config_session_ext* session) {
666 status_t result = NO_ERROR;
667 switch (ext.getDiscriminator()) {
668 case AudioPortExtendedInfo::hidl_discriminator::unspecified:
669 *role = AUDIO_PORT_ROLE_NONE;
670 *type = AUDIO_PORT_TYPE_NONE;
671 break;
672 case AudioPortExtendedInfo::hidl_discriminator::device:
673 *role = xsd::isOutputDevice(ext.device().deviceType) ? AUDIO_PORT_ROLE_SINK
674 : AUDIO_PORT_ROLE_SOURCE;
675 *type = AUDIO_PORT_TYPE_DEVICE;
676 CONVERT_CHECKED(deviceAddressToHal(ext.device(), &device->type, device->address),
677 result);
678 break;
679 case AudioPortExtendedInfo::hidl_discriminator::mix:
680 *type = AUDIO_PORT_TYPE_MIX;
681 switch (ext.mix().useCase.getDiscriminator()) {
682 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::stream:
683 *role = AUDIO_PORT_ROLE_SOURCE;
684 CONVERT_CHECKED(
685 audioStreamTypeToHal(ext.mix().useCase.stream(), &mix->usecase.stream),
686 result);
687 break;
688 case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::source:
689 *role = AUDIO_PORT_ROLE_SINK;
690 CONVERT_CHECKED(
691 audioSourceToHal(ext.mix().useCase.source(), &mix->usecase.source),
692 result);
693 break;
694 }
695 mix->handle = ext.mix().ioHandle;
696 break;
697 case AudioPortExtendedInfo::hidl_discriminator::session:
698 *role = AUDIO_PORT_ROLE_NONE;
699 *type = AUDIO_PORT_TYPE_SESSION;
700 session->session = static_cast<audio_session_t>(ext.session());
701 break;
702 }
703 return result;
704}
705
706status_t HidlUtils::audioPortFromHal(const struct audio_port& halPort, AudioPort* port) {
707 struct audio_port_v7 halPortV7 = {};
708 audio_populate_audio_port_v7(&halPort, &halPortV7);
709 return audioPortFromHal(halPortV7, port);
710}
711
712status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port* halPort) {
713 status_t result = NO_ERROR;
714 struct audio_port_v7 halPortV7 = {};
715 CONVERT_CHECKED(audioPortToHal(port, &halPortV7), result);
716 if (!audio_populate_audio_port(&halPortV7, halPort)) {
717 result = BAD_VALUE;
718 }
719 return result;
720}
721
722status_t HidlUtils::audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port) {
723 status_t result = NO_ERROR;
724 bool isInput = false;
725 port->id = halPort.id;
726 port->name.setToExternal(halPort.name, strlen(halPort.name));
727 // HAL uses slightly different but convertible structures for the extended info in port
728 // and port config structures.
729 struct audio_port_config_device_ext halDevice = {};
730 struct audio_port_config_mix_ext halMix = {};
731 struct audio_port_config_session_ext halSession = {};
732 switch (halPort.type) {
733 case AUDIO_PORT_TYPE_NONE:
734 break;
735 case AUDIO_PORT_TYPE_DEVICE:
736 halDevice.type = halPort.ext.device.type;
737 memcpy(halDevice.address, halPort.ext.device.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
738 break;
739 case AUDIO_PORT_TYPE_MIX:
740 halMix.handle = halPort.ext.mix.handle;
741 break;
742 case AUDIO_PORT_TYPE_SESSION:
743 halSession.session = halPort.ext.session.session;
744 break;
745 }
746 CONVERT_CHECKED(audioPortExtendedInfoFromHal(halPort.role, halPort.type, halDevice, halMix,
747 halSession, &port->ext, &isInput),
748 result);
749 port->profiles.resize(halPort.num_audio_profiles);
750 for (size_t i = 0; i < halPort.num_audio_profiles; ++i) {
751 CONVERT_CHECKED(audioProfileFromHal(halPort.audio_profiles[i], isInput, &port->profiles[i]),
752 result);
753 }
754 port->gains.resize(halPort.num_gains);
755 for (size_t i = 0; i < halPort.num_gains; ++i) {
756 CONVERT_CHECKED(audioGainFromHal(halPort.gains[i], isInput, &port->gains[i]), result);
757 }
758 CONVERT_CHECKED(audioPortConfigFromHal(halPort.active_config, &port->activeConfig), result);
759 return result;
760}
761
762status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort) {
763 status_t result = NO_ERROR;
764 halPort->id = port.id;
765 strncpy(halPort->name, port.name.c_str(), AUDIO_PORT_MAX_NAME_LEN);
766 halPort->name[AUDIO_PORT_MAX_NAME_LEN - 1] = '\0';
767 if (port.name.size() >= AUDIO_PORT_MAX_NAME_LEN) {
768 ALOGE("HIDL Audio Port name is too long: %zu", port.name.size());
769 result = BAD_VALUE;
770 }
771 halPort->num_audio_profiles = port.profiles.size();
772 if (halPort->num_audio_profiles > AUDIO_PORT_MAX_AUDIO_PROFILES) {
773 ALOGE("HIDL Audio Port has too many profiles: %u", halPort->num_audio_profiles);
774 halPort->num_audio_profiles = AUDIO_PORT_MAX_AUDIO_PROFILES;
775 result = BAD_VALUE;
776 }
777 for (size_t i = 0; i < halPort->num_audio_profiles; ++i) {
778 CONVERT_CHECKED(audioProfileToHal(port.profiles[i], &halPort->audio_profiles[i]), result);
779 }
780 halPort->num_gains = port.gains.size();
781 if (halPort->num_gains > AUDIO_PORT_MAX_GAINS) {
782 ALOGE("HIDL Audio Port has too many gains: %u", halPort->num_gains);
783 halPort->num_gains = AUDIO_PORT_MAX_GAINS;
784 result = BAD_VALUE;
785 }
786 for (size_t i = 0; i < halPort->num_gains; ++i) {
787 CONVERT_CHECKED(audioGainToHal(port.gains[i], &halPort->gains[i]), result);
788 }
789 // HAL uses slightly different but convertible structures for the extended info in port
790 // and port config structures.
791 struct audio_port_config_device_ext halDevice = {};
792 struct audio_port_config_mix_ext halMix = {};
793 struct audio_port_config_session_ext halSession = {};
794 CONVERT_CHECKED(audioPortExtendedInfoToHal(port.ext, &halPort->role, &halPort->type, &halDevice,
795 &halMix, &halSession),
796 result);
797 switch (halPort->type) {
798 case AUDIO_PORT_TYPE_NONE:
799 break;
800 case AUDIO_PORT_TYPE_DEVICE:
801 halPort->ext.device.type = halDevice.type;
802 memcpy(halPort->ext.device.address, halDevice.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
803 break;
804 case AUDIO_PORT_TYPE_MIX:
805 halPort->ext.mix.handle = halMix.handle;
806 break;
807 case AUDIO_PORT_TYPE_SESSION:
808 halPort->ext.session.session = halSession.session;
809 break;
810 }
811 CONVERT_CHECKED(audioPortConfigToHal(port.activeConfig, &halPort->active_config), result);
812 return result;
813}
814
815status_t HidlUtils::audioProfileFromHal(const struct audio_profile& halProfile, bool isInput,
816 AudioProfile* profile) {
817 status_t result = NO_ERROR;
818 CONVERT_CHECKED(audioFormatFromHal(halProfile.format, &profile->format), result);
819 profile->sampleRates.resize(halProfile.num_sample_rates);
820 for (size_t i = 0; i < halProfile.num_sample_rates; ++i) {
821 profile->sampleRates[i] = halProfile.sample_rates[i];
822 }
823 profile->channelMasks.resize(halProfile.num_channel_masks);
824 for (size_t i = 0; i < halProfile.num_channel_masks; ++i) {
825 CONVERT_CHECKED(audioChannelMaskFromHal(halProfile.channel_masks[i], isInput,
826 &profile->channelMasks[i]),
827 result);
828 }
829 return result;
830}
831
832status_t HidlUtils::audioProfileToHal(const AudioProfile& profile,
833 struct audio_profile* halProfile) {
834 status_t result = NO_ERROR;
835 CONVERT_CHECKED(audioFormatToHal(profile.format, &halProfile->format), result);
836 memset(halProfile->sample_rates, 0, sizeof(halProfile->sample_rates));
837 halProfile->num_sample_rates = profile.sampleRates.size();
838 if (halProfile->num_sample_rates > AUDIO_PORT_MAX_SAMPLING_RATES) {
839 ALOGE("HIDL Audio profile has too many sample rates: %u", halProfile->num_sample_rates);
840 halProfile->num_sample_rates = AUDIO_PORT_MAX_SAMPLING_RATES;
841 result = BAD_VALUE;
842 }
843 for (size_t i = 0; i < halProfile->num_sample_rates; ++i) {
844 halProfile->sample_rates[i] = profile.sampleRates[i];
845 }
846 memset(halProfile->channel_masks, 0, sizeof(halProfile->channel_masks));
847 halProfile->num_channel_masks = profile.channelMasks.size();
848 if (halProfile->num_channel_masks > AUDIO_PORT_MAX_CHANNEL_MASKS) {
849 ALOGE("HIDL Audio profile has too many channel masks: %u", halProfile->num_channel_masks);
850 halProfile->num_channel_masks = AUDIO_PORT_MAX_CHANNEL_MASKS;
851 result = BAD_VALUE;
852 }
853 for (size_t i = 0; i < halProfile->num_channel_masks; ++i) {
854 CONVERT_CHECKED(
855 audioChannelMaskToHal(profile.channelMasks[i], &halProfile->channel_masks[i]),
856 status);
857 }
858 return result;
859}
860
Mikhail Naganov3f1457b2020-12-17 15:01:54 -0800861status_t HidlUtils::audioTagsFromHal(const char* halTags, hidl_vec<AudioTag>* tags) {
862 std::vector<std::string> strTags = utils::splitString(halTags, sAudioTagSeparator);
863 status_t result = NO_ERROR;
864 tags->resize(strTags.size());
865 size_t to = 0;
866 for (size_t from = 0; from < strTags.size(); ++from) {
867 if (xsd::isVendorExtension(strTags[from])) {
868 (*tags)[to++] = strTags[from];
869 } else {
870 result = BAD_VALUE;
871 }
872 }
873 if (to != strTags.size()) {
874 tags->resize(to);
875 }
876 return result;
877}
878
879status_t HidlUtils::audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags) {
880 memset(halTags, 0, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
881 status_t result = NO_ERROR;
882 std::ostringstream halTagsBuffer;
883 bool hasValue = false;
884 for (const auto& tag : tags) {
885 if (hasValue) {
886 halTagsBuffer << sAudioTagSeparator;
887 }
888 if (xsd::isVendorExtension(tag) && strchr(tag.c_str(), sAudioTagSeparator) == nullptr) {
889 halTagsBuffer << tag;
890 hasValue = true;
891 } else {
892 result = BAD_VALUE;
893 }
894 }
895 std::string fullHalTags{std::move(halTagsBuffer.str())};
896 strncpy(halTags, fullHalTags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
897 CONVERT_CHECKED(fullHalTags.length() <= AUDIO_ATTRIBUTES_TAGS_MAX_SIZE ? NO_ERROR : BAD_VALUE,
898 result);
899 return result;
900}
901
Mikhail Naganov1b444a52020-10-29 13:08:05 -0700902status_t HidlUtils::deviceAddressFromHal(audio_devices_t halDeviceType,
903 const char* halDeviceAddress, DeviceAddress* device) {
904 status_t result = NO_ERROR;
905 CONVERT_CHECKED(audioDeviceTypeFromHal(halDeviceType, &device->deviceType), result);
906 if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
907 device->address.mac({});
908 if (halDeviceAddress != nullptr) {
909 auto& mac = device->address.mac();
910 int status = sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &mac[0], &mac[1],
911 &mac[2], &mac[3], &mac[4], &mac[5]);
912 if (status != 6) {
913 ALOGE("BT A2DP device \"%s\" MAC address \"%s\" is invalid",
914 device->deviceType.c_str(), halDeviceAddress);
915 result = BAD_VALUE;
916 }
917 } else {
918 ALOGE("BT A2DP device \"%s\" does not have a MAC address", halDeviceAddress);
919 result = BAD_VALUE;
920 }
921 } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
922 device->address.ipv4({});
923 if (halDeviceAddress != nullptr) {
924 auto& ipv4 = device->address.ipv4();
925 int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &ipv4[0], &ipv4[1],
926 &ipv4[2], &ipv4[3]);
927 if (status != 4) {
928 ALOGE("IP device \"%s\" IPv4 address \"%s\" is invalid", device->deviceType.c_str(),
929 halDeviceAddress);
930 result = BAD_VALUE;
931 }
932 } else {
933 ALOGE("IP device \"%s\" does not have an IPv4 address", device->deviceType.c_str());
934 result = BAD_VALUE;
935 }
936 } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
937 device->address.alsa({});
938 if (halDeviceAddress != nullptr) {
939 auto& alsa = device->address.alsa();
940 int status = sscanf(halDeviceAddress, "card=%d;device=%d", &alsa.card, &alsa.device);
941 if (status != 2) {
942 ALOGE("USB device \"%s\" ALSA address \"%s\" is invalid",
943 device->deviceType.c_str(), halDeviceAddress);
944 result = BAD_VALUE;
945 }
946 } else {
947 ALOGE("USB device \"%s\" does not have ALSA address", device->deviceType.c_str());
948 result = BAD_VALUE;
949 }
950 } else {
951 // Any other device type uses the 'id' field.
952 device->address.id(halDeviceAddress != nullptr ? halDeviceAddress : "");
953 }
954 return result;
955}
956
957status_t HidlUtils::deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
958 char* halDeviceAddress) {
959 status_t result = NO_ERROR;
960 CONVERT_CHECKED(audioDeviceTypeToHal(device.deviceType, halDeviceType), result);
961 memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
962 if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
963 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::mac) {
964 const auto& mac = device.address.mac();
965 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN,
966 "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4],
967 mac[5]);
968 } else {
969 ALOGE("BT A2DP device \"%s\" does not have MAC address set", device.deviceType.c_str());
970 result = BAD_VALUE;
971 }
972 } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
973 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::ipv4) {
974 const auto& ipv4 = device.address.ipv4();
975 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d", ipv4[0],
976 ipv4[1], ipv4[2], ipv4[3]);
977 } else {
978 ALOGE("IP device \"%s\" does not have IPv4 address set", device.deviceType.c_str());
979 result = BAD_VALUE;
980 }
981 } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
982 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::alsa) {
983 const auto& alsa = device.address.alsa();
984 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d", alsa.card,
985 alsa.device);
986 } else {
987 ALOGE("USB device \"%s\" does not have ALSA address set", device.deviceType.c_str());
988 result = BAD_VALUE;
989 }
990 } else {
991 // Any other device type uses the 'id' field.
992 if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::id) {
993 snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
994 device.address.id().c_str());
995 }
996 }
997 return result;
998}
999
1000} // namespace implementation
1001} // namespace CPP_VERSION
1002} // namespace common
1003} // namespace audio
1004} // namespace hardware
1005} // namespace android