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