summaryrefslogtreecommitdiff
path: root/audio/common/all-versions/default/7.0/HidlUtils.cpp
blob: af29da61f3c0824e5f579bddb7ce3545efa954c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <algorithm>

#define LOG_TAG "HidlUtils"
#include <log/log.h>

#include <android_audio_policy_configuration_V7_0-enums.h>
#include <common/all-versions/HidlSupport.h>
#include <common/all-versions/VersionUtils.h>

#include "HidlUtils.h"

namespace android {
namespace hardware {
namespace audio {
namespace common {
namespace CPP_VERSION {
namespace implementation {

namespace xsd {
using namespace ::android::audio::policy::configuration::V7_0;
}

#define CONVERT_CHECKED(expr, result)                   \
    if (status_t status = (expr); status != NO_ERROR) { \
        result = status;                                \
    }

status_t HidlUtils::audioIndexChannelMaskFromHal(audio_channel_mask_t halChannelMask,
                                                 AudioChannelMask* channelMask) {
    *channelMask = audio_channel_index_mask_to_string(halChannelMask);
    if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
        return NO_ERROR;
    }
    ALOGE("Unknown index channel mask value 0x%X", halChannelMask);
    *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
    return BAD_VALUE;
}

status_t HidlUtils::audioInputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
                                                 AudioChannelMask* channelMask) {
    *channelMask = audio_channel_in_mask_to_string(halChannelMask);
    if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
        return NO_ERROR;
    }
    ALOGE("Unknown input channel mask value 0x%X", halChannelMask);
    *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
    return BAD_VALUE;
}

status_t HidlUtils::audioOutputChannelMaskFromHal(audio_channel_mask_t halChannelMask,
                                                  AudioChannelMask* channelMask) {
    *channelMask = audio_channel_out_mask_to_string(halChannelMask);
    if (!channelMask->empty() && !xsd::isUnknownAudioChannelMask(*channelMask)) {
        return NO_ERROR;
    }
    ALOGE("Unknown output channel mask value 0x%X", halChannelMask);
    *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
    return BAD_VALUE;
}

status_t HidlUtils::audioChannelMaskFromHal(audio_channel_mask_t halChannelMask, bool isInput,
                                            AudioChannelMask* channelMask) {
    if (halChannelMask != AUDIO_CHANNEL_NONE) {
        if (audio_channel_mask_is_valid(halChannelMask)) {
            switch (audio_channel_mask_get_representation(halChannelMask)) {
                case AUDIO_CHANNEL_REPRESENTATION_POSITION:
                    return isInput ? audioInputChannelMaskFromHal(halChannelMask, channelMask)
                                   : audioOutputChannelMaskFromHal(halChannelMask, channelMask);
                case AUDIO_CHANNEL_REPRESENTATION_INDEX:
                    // Index masks do not have direction.
                    return audioIndexChannelMaskFromHal(halChannelMask, channelMask);
                    // no default
            }
        }
        *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
        return BAD_VALUE;
    }
    *channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
    return NO_ERROR;
}

status_t HidlUtils::audioChannelMasksFromHal(const std::vector<std::string>& halChannelMasks,
                                             hidl_vec<AudioChannelMask>* channelMasks) {
    hidl_vec<AudioChannelMask> tempChannelMasks;
    tempChannelMasks.resize(halChannelMasks.size());
    size_t tempPos = 0;
    for (const auto& halChannelMask : halChannelMasks) {
        if (!halChannelMask.empty() && !xsd::isUnknownAudioChannelMask(halChannelMask)) {
            tempChannelMasks[tempPos++] = halChannelMask;
        }
    }
    if (tempPos == tempChannelMasks.size()) {
        *channelMasks = std::move(tempChannelMasks);
    } else {
        *channelMasks = hidl_vec<AudioChannelMask>(tempChannelMasks.begin(),
                                                   tempChannelMasks.begin() + tempPos);
    }
    return halChannelMasks.size() == channelMasks->size() ? NO_ERROR : BAD_VALUE;
}

status_t HidlUtils::audioChannelMaskToHal(const AudioChannelMask& channelMask,
                                          audio_channel_mask_t* halChannelMask) {
    if (!xsd::isUnknownAudioChannelMask(channelMask) &&
        audio_channel_mask_from_string(channelMask.c_str(), halChannelMask)) {
        return NO_ERROR;
    }
    ALOGE("Unknown channel mask \"%s\"", channelMask.c_str());
    *halChannelMask = AUDIO_CHANNEL_NONE;
    return BAD_VALUE;
}

status_t HidlUtils::audioConfigBaseFromHal(const audio_config_base_t& halConfigBase, bool isInput,
                                           AudioConfigBase* configBase) {
    status_t result = NO_ERROR;
    configBase->sampleRateHz = halConfigBase.sample_rate;
    CONVERT_CHECKED(
            audioChannelMaskFromHal(halConfigBase.channel_mask, isInput, &configBase->channelMask),
            result);
    CONVERT_CHECKED(audioFormatFromHal(halConfigBase.format, &configBase->format), result);
    return result;
}

status_t HidlUtils::audioConfigBaseToHal(const AudioConfigBase& configBase,
                                         audio_config_base_t* halConfigBase) {
    status_t result = NO_ERROR;
    halConfigBase->sample_rate = configBase.sampleRateHz;
    CONVERT_CHECKED(audioChannelMaskToHal(configBase.channelMask, &halConfigBase->channel_mask),
                    result);
    CONVERT_CHECKED(audioFormatToHal(configBase.format, &halConfigBase->format), result);
    return result;
}

status_t HidlUtils::audioConfigBaseOptionalFromHal(const audio_config_base_t& halConfigBase,
                                                   bool isInput, bool formatSpecified,
                                                   bool sampleRateSpecified,
                                                   bool channelMaskSpecified,
                                                   AudioConfigBaseOptional* configBase) {
    status_t result = NO_ERROR;
    if (formatSpecified) {
        AudioFormat value;
        CONVERT_CHECKED(audioFormatFromHal(halConfigBase.format, &value), result);
        configBase->format.value(std::move(value));
    } else {
        configBase->format.unspecified({});
    }
    if (sampleRateSpecified) {
        configBase->sampleRateHz.value(halConfigBase.sample_rate);
    } else {
        configBase->sampleRateHz.unspecified({});
    }
    if (channelMaskSpecified) {
        AudioChannelMask value;
        CONVERT_CHECKED(audioChannelMaskFromHal(halConfigBase.channel_mask, isInput, &value),
                        result);
        configBase->channelMask.value(std::move(value));
    }
    return result;
}

status_t HidlUtils::audioConfigBaseOptionalToHal(const AudioConfigBaseOptional& configBase,
                                                 audio_config_base_t* halConfigBase,
                                                 bool* formatSpecified, bool* sampleRateSpecified,
                                                 bool* channelMaskSpecified) {
    status_t result = NO_ERROR;
    *formatSpecified = configBase.format.getDiscriminator() ==
                       AudioConfigBaseOptional::Format::hidl_discriminator::value;
    if (*formatSpecified) {
        CONVERT_CHECKED(audioFormatToHal(configBase.format.value(), &halConfigBase->format),
                        result);
    }
    *sampleRateSpecified = configBase.sampleRateHz.getDiscriminator() ==
                           AudioConfigBaseOptional::SampleRate::hidl_discriminator::value;
    if (*sampleRateSpecified) {
        halConfigBase->sample_rate = configBase.sampleRateHz.value();
    }
    *channelMaskSpecified = configBase.channelMask.getDiscriminator() ==
                            AudioConfigBaseOptional::ChannelMask::hidl_discriminator::value;
    if (*channelMaskSpecified) {
        CONVERT_CHECKED(
                audioChannelMaskToHal(configBase.channelMask.value(), &halConfigBase->channel_mask),
                result);
    }
    return result;
}

status_t HidlUtils::audioContentTypeFromHal(const audio_content_type_t halContentType,
                                            AudioContentType* contentType) {
    *contentType = audio_content_type_to_string(halContentType);
    if (!contentType->empty() && !xsd::isUnknownAudioContentType(*contentType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio content type value 0x%X", halContentType);
    *contentType = toString(xsd::AudioContentType::AUDIO_CONTENT_TYPE_UNKNOWN);
    return BAD_VALUE;
}

status_t HidlUtils::audioContentTypeToHal(const AudioContentType& contentType,
                                          audio_content_type_t* halContentType) {
    if (!xsd::isUnknownAudioContentType(contentType) &&
        audio_content_type_from_string(contentType.c_str(), halContentType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio content type \"%s\"", contentType.c_str());
    *halContentType = AUDIO_CONTENT_TYPE_UNKNOWN;
    return BAD_VALUE;
}

status_t HidlUtils::audioDeviceTypeFromHal(audio_devices_t halDevice, AudioDevice* device) {
    *device = audio_device_to_string(halDevice);
    if (!device->empty() && !xsd::isUnknownAudioDevice(*device)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio device value 0x%X", halDevice);
    *device = toString(xsd::AudioDevice::AUDIO_DEVICE_NONE);
    return BAD_VALUE;
}

status_t HidlUtils::audioDeviceTypeToHal(const AudioDevice& device, audio_devices_t* halDevice) {
    if (!xsd::isUnknownAudioDevice(device) && audio_device_from_string(device.c_str(), halDevice)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio device \"%s\"", device.c_str());
    *halDevice = AUDIO_DEVICE_NONE;
    return BAD_VALUE;
}

status_t HidlUtils::audioFormatFromHal(audio_format_t halFormat, AudioFormat* format) {
    *format = audio_format_to_string(halFormat);
    if (!format->empty() && !xsd::isUnknownAudioFormat(*format)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio format value 0x%X", halFormat);
    return BAD_VALUE;
}

status_t HidlUtils::audioFormatsFromHal(const std::vector<std::string>& halFormats,
                                        hidl_vec<AudioFormat>* formats) {
    hidl_vec<AudioFormat> tempFormats;
    tempFormats.resize(halFormats.size());
    size_t tempPos = 0;
    for (const auto& halFormat : halFormats) {
        if (!halFormat.empty() && !xsd::isUnknownAudioFormat(halFormat)) {
            tempFormats[tempPos++] = halFormat;
        }
    }
    if (tempPos == tempFormats.size()) {
        *formats = std::move(tempFormats);
    } else {
        *formats = hidl_vec<AudioFormat>(tempFormats.begin(), tempFormats.begin() + tempPos);
    }
    return halFormats.size() == formats->size() ? NO_ERROR : BAD_VALUE;
}

status_t HidlUtils::audioFormatToHal(const AudioFormat& format, audio_format_t* halFormat) {
    if (!xsd::isUnknownAudioFormat(format) && audio_format_from_string(format.c_str(), halFormat)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio format \"%s\"", format.c_str());
    *halFormat = AUDIO_FORMAT_DEFAULT;
    return BAD_VALUE;
}

status_t HidlUtils::audioGainModeMaskFromHal(audio_gain_mode_t halGainModeMask,
                                             hidl_vec<AudioGainMode>* gainModeMask) {
    status_t status = NO_ERROR;
    std::vector<AudioGainMode> result;
    for (uint32_t bit = 0; halGainModeMask != 0 && bit < sizeof(audio_gain_mode_t) * 8; ++bit) {
        audio_gain_mode_t flag = static_cast<audio_gain_mode_t>(1u << bit);
        if ((flag & halGainModeMask) == flag) {
            AudioGainMode flagStr = audio_gain_mode_to_string(flag);
            if (!flagStr.empty() && !xsd::isUnknownAudioGainMode(flagStr)) {
                result.push_back(flagStr);
            } else {
                ALOGE("Unknown audio gain mode value 0x%X", flag);
                status = BAD_VALUE;
            }
            halGainModeMask = static_cast<audio_gain_mode_t>(halGainModeMask & ~flag);
        }
    }
    *gainModeMask = result;
    return status;
}

status_t HidlUtils::audioGainModeMaskToHal(const hidl_vec<AudioGainMode>& gainModeMask,
                                           audio_gain_mode_t* halGainModeMask) {
    status_t status = NO_ERROR;
    *halGainModeMask = {};
    for (const auto& gainMode : gainModeMask) {
        audio_gain_mode_t halGainMode;
        if (!xsd::isUnknownAudioGainMode(gainMode) &&
            audio_gain_mode_from_string(gainMode.c_str(), &halGainMode)) {
            *halGainModeMask = static_cast<audio_gain_mode_t>(*halGainModeMask | halGainMode);
        } else {
            ALOGE("Unknown audio gain mode \"%s\"", gainMode.c_str());
            status = BAD_VALUE;
        }
    }
    return status;
}

status_t HidlUtils::audioSourceFromHal(audio_source_t halSource, AudioSource* source) {
    *source = audio_source_to_string(halSource);
    if (!source->empty() && !xsd::isUnknownAudioSource(*source)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio source value 0x%X", halSource);
    *source = toString(xsd::AudioSource::AUDIO_SOURCE_DEFAULT);
    return BAD_VALUE;
}

status_t HidlUtils::audioSourceToHal(const AudioSource& source, audio_source_t* halSource) {
    if (!xsd::isUnknownAudioSource(source) && audio_source_from_string(source.c_str(), halSource)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio source \"%s\"", source.c_str());
    *halSource = AUDIO_SOURCE_DEFAULT;
    return BAD_VALUE;
}

// The "default" value of audio_stream_type_t is represented by an empty string.
status_t HidlUtils::audioStreamTypeFromHal(audio_stream_type_t halStreamType,
                                           AudioStreamType* streamType) {
    if (halStreamType != AUDIO_STREAM_DEFAULT) {
        *streamType = audio_stream_type_to_string(halStreamType);
        if (!streamType->empty() && !xsd::isUnknownAudioStreamType(*streamType)) {
            return NO_ERROR;
        }
        ALOGE("Unknown audio stream type value 0x%X", halStreamType);
        return BAD_VALUE;
    } else {
        *streamType = "";
        return NO_ERROR;
    }
}

status_t HidlUtils::audioStreamTypeToHal(const AudioStreamType& streamType,
                                         audio_stream_type_t* halStreamType) {
    if (!streamType.empty()) {
        if (!xsd::isUnknownAudioStreamType(streamType) &&
            audio_stream_type_from_string(streamType.c_str(), halStreamType)) {
            return NO_ERROR;
        }
        ALOGE("Unknown audio stream type \"%s\"", streamType.c_str());
        return BAD_VALUE;
    } else {
        *halStreamType = AUDIO_STREAM_DEFAULT;
        return NO_ERROR;
    }
}

status_t HidlUtils::audioConfigFromHal(const audio_config_t& halConfig, bool isInput,
                                       AudioConfig* config) {
    status_t result = NO_ERROR;
    audio_config_base_t halConfigBase = {halConfig.sample_rate, halConfig.channel_mask,
                                         halConfig.format};
    CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, isInput, &config->base), result);
    if (halConfig.offload_info.sample_rate != 0) {
        config->offloadInfo.info({});
        CONVERT_CHECKED(
                audioOffloadInfoFromHal(halConfig.offload_info, &config->offloadInfo.info()),
                result);
    }
    config->frameCount = halConfig.frame_count;
    return result;
}

status_t HidlUtils::audioConfigToHal(const AudioConfig& config, audio_config_t* halConfig) {
    status_t result = NO_ERROR;
    *halConfig = AUDIO_CONFIG_INITIALIZER;
    audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
    CONVERT_CHECKED(audioConfigBaseToHal(config.base, &halConfigBase), result);
    halConfig->sample_rate = halConfigBase.sample_rate;
    halConfig->channel_mask = halConfigBase.channel_mask;
    halConfig->format = halConfigBase.format;
    if (config.offloadInfo.getDiscriminator() ==
        AudioConfig::OffloadInfo::hidl_discriminator::info) {
        CONVERT_CHECKED(audioOffloadInfoToHal(config.offloadInfo.info(), &halConfig->offload_info),
                        result);
    }
    halConfig->frame_count = config.frameCount;
    return result;
}

status_t HidlUtils::audioGainConfigFromHal(const struct audio_gain_config& halConfig, bool isInput,
                                           AudioGainConfig* config) {
    status_t result = NO_ERROR;
    config->index = halConfig.index;
    CONVERT_CHECKED(audioGainModeMaskFromHal(halConfig.mode, &config->mode), result);
    CONVERT_CHECKED(audioChannelMaskFromHal(halConfig.channel_mask, isInput, &config->channelMask),
                    result);
    if (halConfig.mode & AUDIO_GAIN_MODE_JOINT) {
        config->values.resize(1);
        config->values[0] = halConfig.values[0];
    }
    if (halConfig.mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
        config->values.resize(__builtin_popcount(halConfig.channel_mask));
        for (size_t i = 0; i < config->values.size(); ++i) {
            config->values[i] = halConfig.values[i];
        }
    }
    config->rampDurationMs = halConfig.ramp_duration_ms;
    return result;
}

status_t HidlUtils::audioGainConfigToHal(const AudioGainConfig& config,
                                         struct audio_gain_config* halConfig) {
    status_t result = NO_ERROR;
    halConfig->index = config.index;
    CONVERT_CHECKED(audioGainModeMaskToHal(config.mode, &halConfig->mode), result);
    CONVERT_CHECKED(audioChannelMaskToHal(config.channelMask, &halConfig->channel_mask), result);
    memset(halConfig->values, 0, sizeof(halConfig->values));
    if (halConfig->mode & AUDIO_GAIN_MODE_JOINT) {
        if (config.values.size() > 0) {
            halConfig->values[0] = config.values[0];
        } else {
            ALOGE("Empty values vector in AudioGainConfig");
            result = BAD_VALUE;
        }
    }
    if (halConfig->mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
        size_t channelCount = __builtin_popcount(halConfig->channel_mask);
        size_t valuesCount = config.values.size();
        if (channelCount != valuesCount) {
            ALOGE("Wrong number of values in AudioGainConfig, expected: %zu, found: %zu",
                  channelCount, valuesCount);
            result = BAD_VALUE;
            if (channelCount < valuesCount) {
                valuesCount = channelCount;
            }
        }
        for (size_t i = 0; i < valuesCount; ++i) {
            halConfig->values[i] = config.values[i];
        }
    }
    halConfig->ramp_duration_ms = config.rampDurationMs;
    return result;
}

status_t HidlUtils::audioGainFromHal(const struct audio_gain& halGain, bool isInput,
                                     AudioGain* gain) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioGainModeMaskFromHal(halGain.mode, &gain->mode), result);
    CONVERT_CHECKED(audioChannelMaskFromHal(halGain.channel_mask, isInput, &gain->channelMask),
                    result);
    gain->minValue = halGain.min_value;
    gain->maxValue = halGain.max_value;
    gain->defaultValue = halGain.default_value;
    gain->stepValue = halGain.step_value;
    gain->minRampMs = halGain.min_ramp_ms;
    gain->maxRampMs = halGain.max_ramp_ms;
    return result;
}

status_t HidlUtils::audioGainToHal(const AudioGain& gain, struct audio_gain* halGain) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioGainModeMaskToHal(gain.mode, &halGain->mode), result);
    CONVERT_CHECKED(audioChannelMaskToHal(gain.channelMask, &halGain->channel_mask), result);
    halGain->min_value = gain.minValue;
    halGain->max_value = gain.maxValue;
    halGain->default_value = gain.defaultValue;
    halGain->step_value = gain.stepValue;
    halGain->min_ramp_ms = gain.minRampMs;
    halGain->max_ramp_ms = gain.maxRampMs;
    return result;
}

status_t HidlUtils::audioUsageFromHal(audio_usage_t halUsage, AudioUsage* usage) {
    if (halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST ||
        halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT ||
        halUsage == AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED ||
        halUsage == AUDIO_USAGE_NOTIFICATION_EVENT) {
        halUsage = AUDIO_USAGE_NOTIFICATION;
    }
    *usage = audio_usage_to_string(halUsage);
    if (!usage->empty() && !xsd::isUnknownAudioUsage(*usage)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio usage %d", halUsage);
    *usage = toString(xsd::AudioUsage::AUDIO_USAGE_UNKNOWN);
    return BAD_VALUE;
}

status_t HidlUtils::audioUsageToHal(const AudioUsage& usage, audio_usage_t* halUsage) {
    if (!xsd::isUnknownAudioUsage(usage) && audio_usage_from_string(usage.c_str(), halUsage)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio usage \"%s\"", usage.c_str());
    *halUsage = AUDIO_USAGE_UNKNOWN;
    return BAD_VALUE;
}

status_t HidlUtils::audioOffloadInfoFromHal(const audio_offload_info_t& halOffload,
                                            AudioOffloadInfo* offload) {
    status_t result = NO_ERROR;
    audio_config_base_t halConfigBase = {halOffload.sample_rate, halOffload.channel_mask,
                                         halOffload.format};
    CONVERT_CHECKED(audioConfigBaseFromHal(halConfigBase, false /*isInput*/, &offload->base),
                    result);
    CONVERT_CHECKED(audioStreamTypeFromHal(halOffload.stream_type, &offload->streamType), result);
    offload->bitRatePerSecond = halOffload.bit_rate;
    offload->durationMicroseconds = halOffload.duration_us;
    offload->hasVideo = halOffload.has_video;
    offload->isStreaming = halOffload.is_streaming;
    offload->bitWidth = halOffload.bit_width;
    offload->bufferSize = halOffload.offload_buffer_size;
    CONVERT_CHECKED(audioUsageFromHal(halOffload.usage, &offload->usage), result);
    if (halOffload.version >= AUDIO_OFFLOAD_INFO_VERSION_0_2) {
        offload->encapsulationMode =
                static_cast<AudioEncapsulationMode>(halOffload.encapsulation_mode);
        offload->contentId = halOffload.content_id;
        offload->syncId = halOffload.sync_id;
    } else {
        offload->encapsulationMode = AudioEncapsulationMode::NONE;
        offload->contentId = 0;
        offload->syncId = 0;
    }
    return result;
}

status_t HidlUtils::audioOffloadInfoToHal(const AudioOffloadInfo& offload,
                                          audio_offload_info_t* halOffload) {
    status_t result = NO_ERROR;
    *halOffload = AUDIO_INFO_INITIALIZER;
    audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
    CONVERT_CHECKED(audioConfigBaseToHal(offload.base, &halConfigBase), result);
    halOffload->sample_rate = halConfigBase.sample_rate;
    halOffload->channel_mask = halConfigBase.channel_mask;
    halOffload->format = halConfigBase.format;
    CONVERT_CHECKED(audioStreamTypeToHal(offload.streamType, &halOffload->stream_type), result);
    halOffload->bit_rate = offload.bitRatePerSecond;
    halOffload->duration_us = offload.durationMicroseconds;
    halOffload->has_video = offload.hasVideo;
    halOffload->is_streaming = offload.isStreaming;
    halOffload->bit_width = offload.bitWidth;
    halOffload->offload_buffer_size = offload.bufferSize;
    CONVERT_CHECKED(audioUsageToHal(offload.usage, &halOffload->usage), result);
    halOffload->encapsulation_mode =
            static_cast<audio_encapsulation_mode_t>(offload.encapsulationMode);
    halOffload->content_id = offload.contentId;
    halOffload->sync_id = offload.syncId;
    return result;
}

status_t HidlUtils::audioPortConfigFromHal(const struct audio_port_config& halConfig,
                                           AudioPortConfig* config) {
    status_t result = NO_ERROR;
    bool isInput = false;
    config->id = halConfig.id;
    CONVERT_CHECKED(audioPortExtendedInfoFromHal(halConfig.role, halConfig.type,
                                                 halConfig.ext.device, halConfig.ext.mix,
                                                 halConfig.ext.session, &config->ext, &isInput),
                    result);
    if (audio_port_config_has_input_direction(&halConfig) != isInput) {
        ALOGE("Inconsistent port config direction data, is input: %d (hal) != %d (converter)",
              audio_port_config_has_input_direction(&halConfig), isInput);
        result = BAD_VALUE;
    }
    audio_config_base_t halConfigBase = {halConfig.sample_rate, halConfig.channel_mask,
                                         halConfig.format};
    CONVERT_CHECKED(
            audioConfigBaseOptionalFromHal(
                    halConfigBase, isInput, halConfig.config_mask & AUDIO_PORT_CONFIG_FORMAT,
                    halConfig.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE,
                    halConfig.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK, &config->base),
            result);
    if (halConfig.config_mask & AUDIO_PORT_CONFIG_GAIN) {
        config->gain.config({});
        CONVERT_CHECKED(audioGainConfigFromHal(halConfig.gain, isInput, &config->gain.config()),
                        result);
    } else {
        config->gain.unspecified({});
    }
    return result;
}

status_t HidlUtils::audioPortConfigToHal(const AudioPortConfig& config,
                                         struct audio_port_config* halConfig) {
    status_t result = NO_ERROR;
    memset(halConfig, 0, sizeof(audio_port_config));
    halConfig->id = config.id;
    halConfig->config_mask = 0;
    audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
    bool formatSpecified = false, sRateSpecified = false, channelMaskSpecified = false;
    CONVERT_CHECKED(audioConfigBaseOptionalToHal(config.base, &halConfigBase, &formatSpecified,
                                                 &sRateSpecified, &channelMaskSpecified),
                    result);
    if (sRateSpecified) {
        halConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
        halConfig->sample_rate = halConfigBase.sample_rate;
    }
    if (channelMaskSpecified) {
        halConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
        halConfig->channel_mask = halConfigBase.channel_mask;
    }
    if (formatSpecified) {
        halConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
        halConfig->format = halConfigBase.format;
    }
    if (config.gain.getDiscriminator() ==
        AudioPortConfig::OptionalGain::hidl_discriminator::config) {
        halConfig->config_mask |= AUDIO_PORT_CONFIG_GAIN;
        CONVERT_CHECKED(audioGainConfigToHal(config.gain.config(), &halConfig->gain), result);
    }
    CONVERT_CHECKED(audioPortExtendedInfoToHal(config.ext, &halConfig->role, &halConfig->type,
                                               &halConfig->ext.device, &halConfig->ext.mix,
                                               &halConfig->ext.session),
                    result);
    return result;
}

status_t HidlUtils::audioPortExtendedInfoFromHal(
        audio_port_role_t role, audio_port_type_t type,
        const struct audio_port_config_device_ext& device,
        const struct audio_port_config_mix_ext& mix,
        const struct audio_port_config_session_ext& session, AudioPortExtendedInfo* ext,
        bool* isInput) {
    status_t result = NO_ERROR;
    *isInput = false;
    switch (type) {
        case AUDIO_PORT_TYPE_NONE:
            ext->unspecified({});
            break;
        case AUDIO_PORT_TYPE_DEVICE: {
            *isInput = role == AUDIO_PORT_ROLE_SOURCE;
            ext->device({});
            CONVERT_CHECKED(deviceAddressFromHal(device.type, device.address, &ext->device()),
                            result);
            break;
        }
        case AUDIO_PORT_TYPE_MIX: {
            *isInput = role == AUDIO_PORT_ROLE_SINK;
            ext->mix({});
            ext->mix().ioHandle = mix.handle;
            if (role == AUDIO_PORT_ROLE_SOURCE) {
                ext->mix().useCase.stream({});
                CONVERT_CHECKED(
                        audioStreamTypeFromHal(mix.usecase.stream, &ext->mix().useCase.stream()),
                        result);
            } else if (role == AUDIO_PORT_ROLE_SINK) {
                ext->mix().useCase.source({});
                CONVERT_CHECKED(
                        audioSourceFromHal(mix.usecase.source, &ext->mix().useCase.source()),
                        result);
            }
            break;
        }
        case AUDIO_PORT_TYPE_SESSION: {
            ext->session(session.session);
            break;
        }
    }
    return result;
}

status_t HidlUtils::audioPortExtendedInfoToHal(const AudioPortExtendedInfo& ext,
                                               audio_port_role_t* role, audio_port_type_t* type,
                                               struct audio_port_config_device_ext* device,
                                               struct audio_port_config_mix_ext* mix,
                                               struct audio_port_config_session_ext* session) {
    status_t result = NO_ERROR;
    switch (ext.getDiscriminator()) {
        case AudioPortExtendedInfo::hidl_discriminator::unspecified:
            *role = AUDIO_PORT_ROLE_NONE;
            *type = AUDIO_PORT_TYPE_NONE;
            break;
        case AudioPortExtendedInfo::hidl_discriminator::device:
            *role = xsd::isOutputDevice(ext.device().deviceType) ? AUDIO_PORT_ROLE_SINK
                                                                 : AUDIO_PORT_ROLE_SOURCE;
            *type = AUDIO_PORT_TYPE_DEVICE;
            CONVERT_CHECKED(deviceAddressToHal(ext.device(), &device->type, device->address),
                            result);
            break;
        case AudioPortExtendedInfo::hidl_discriminator::mix:
            *type = AUDIO_PORT_TYPE_MIX;
            switch (ext.mix().useCase.getDiscriminator()) {
                case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::stream:
                    *role = AUDIO_PORT_ROLE_SOURCE;
                    CONVERT_CHECKED(
                            audioStreamTypeToHal(ext.mix().useCase.stream(), &mix->usecase.stream),
                            result);
                    break;
                case AudioPortExtendedInfo::AudioPortMixExt::UseCase::hidl_discriminator::source:
                    *role = AUDIO_PORT_ROLE_SINK;
                    CONVERT_CHECKED(
                            audioSourceToHal(ext.mix().useCase.source(), &mix->usecase.source),
                            result);
                    break;
            }
            mix->handle = ext.mix().ioHandle;
            break;
        case AudioPortExtendedInfo::hidl_discriminator::session:
            *role = AUDIO_PORT_ROLE_NONE;
            *type = AUDIO_PORT_TYPE_SESSION;
            session->session = static_cast<audio_session_t>(ext.session());
            break;
    }
    return result;
}

status_t HidlUtils::encapsulationTypeFromHal(audio_encapsulation_type_t halEncapsulationType,
                                             AudioEncapsulationType* encapsulationType) {
    *encapsulationType = audio_encapsulation_type_to_string(halEncapsulationType);
    if (!encapsulationType->empty() && !xsd::isUnknownAudioEncapsulationType(*encapsulationType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio encapsulation type value 0x%X", halEncapsulationType);
    return BAD_VALUE;
}

status_t HidlUtils::encapsulationTypeToHal(const AudioEncapsulationType& encapsulationType,
                                           audio_encapsulation_type_t* halEncapsulationType) {
    if (!xsd::isUnknownAudioEncapsulationType(encapsulationType) &&
        audio_encapsulation_type_from_string(encapsulationType.c_str(), halEncapsulationType)) {
        return NO_ERROR;
    }
    ALOGE("Unknown audio encapsulation type \"%s\"", encapsulationType.c_str());
    *halEncapsulationType = AUDIO_ENCAPSULATION_TYPE_NONE;
    return BAD_VALUE;
}

status_t HidlUtils::audioPortFromHal(const struct audio_port& halPort, AudioPort* port) {
    struct audio_port_v7 halPortV7 = {};
    audio_populate_audio_port_v7(&halPort, &halPortV7);
    return audioPortFromHal(halPortV7, port);
}

status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port* halPort) {
    status_t result = NO_ERROR;
    struct audio_port_v7 halPortV7 = {};
    CONVERT_CHECKED(audioPortToHal(port, &halPortV7), result);
    if (!audio_populate_audio_port(&halPortV7, halPort)) {
        result = BAD_VALUE;
    }
    return result;
}

status_t HidlUtils::audioPortFromHal(const struct audio_port_v7& halPort, AudioPort* port) {
    status_t result = NO_ERROR;
    bool isInput = false;
    port->id = halPort.id;
    port->name.setToExternal(halPort.name, strlen(halPort.name));
    // HAL uses slightly different but convertible structures for the extended info in port
    // and port config structures.
    struct audio_port_config_device_ext halDevice = {};
    struct audio_port_config_mix_ext halMix = {};
    struct audio_port_config_session_ext halSession = {};
    switch (halPort.type) {
        case AUDIO_PORT_TYPE_NONE:
            break;
        case AUDIO_PORT_TYPE_DEVICE:
            halDevice.type = halPort.ext.device.type;
            memcpy(halDevice.address, halPort.ext.device.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
            break;
        case AUDIO_PORT_TYPE_MIX:
            halMix.handle = halPort.ext.mix.handle;
            break;
        case AUDIO_PORT_TYPE_SESSION:
            halSession.session = halPort.ext.session.session;
            break;
    }
    CONVERT_CHECKED(audioPortExtendedInfoFromHal(halPort.role, halPort.type, halDevice, halMix,
                                                 halSession, &port->ext, &isInput),
                    result);
    CONVERT_CHECKED(audioTransportsFromHal(halPort, isInput, &port->transports), result);
    port->gains.resize(halPort.num_gains);
    for (size_t i = 0; i < halPort.num_gains; ++i) {
        CONVERT_CHECKED(audioGainFromHal(halPort.gains[i], isInput, &port->gains[i]), result);
    }
    CONVERT_CHECKED(audioPortConfigFromHal(halPort.active_config, &port->activeConfig), result);
    return result;
}

status_t HidlUtils::audioPortToHal(const AudioPort& port, struct audio_port_v7* halPort) {
    status_t result = NO_ERROR;
    halPort->id = port.id;
    strncpy(halPort->name, port.name.c_str(), AUDIO_PORT_MAX_NAME_LEN);
    halPort->name[AUDIO_PORT_MAX_NAME_LEN - 1] = '\0';
    if (port.name.size() >= AUDIO_PORT_MAX_NAME_LEN) {
        ALOGE("HIDL Audio Port name is too long: %zu", port.name.size());
        result = BAD_VALUE;
    }
    CONVERT_CHECKED(audioTransportsToHal(port.transports, halPort), result);
    halPort->num_gains = port.gains.size();
    if (halPort->num_gains > AUDIO_PORT_MAX_GAINS) {
        ALOGE("HIDL Audio Port has too many gains: %u", halPort->num_gains);
        halPort->num_gains = AUDIO_PORT_MAX_GAINS;
        result = BAD_VALUE;
    }
    for (size_t i = 0; i < halPort->num_gains; ++i) {
        CONVERT_CHECKED(audioGainToHal(port.gains[i], &halPort->gains[i]), result);
    }
    // HAL uses slightly different but convertible structures for the extended info in port
    // and port config structures.
    struct audio_port_config_device_ext halDevice = {};
    struct audio_port_config_mix_ext halMix = {};
    struct audio_port_config_session_ext halSession = {};
    CONVERT_CHECKED(audioPortExtendedInfoToHal(port.ext, &halPort->role, &halPort->type, &halDevice,
                                               &halMix, &halSession),
                    result);
    switch (halPort->type) {
        case AUDIO_PORT_TYPE_NONE:
            break;
        case AUDIO_PORT_TYPE_DEVICE:
            halPort->ext.device.type = halDevice.type;
            memcpy(halPort->ext.device.address, halDevice.address, AUDIO_DEVICE_MAX_ADDRESS_LEN);
            break;
        case AUDIO_PORT_TYPE_MIX:
            halPort->ext.mix.handle = halMix.handle;
            break;
        case AUDIO_PORT_TYPE_SESSION:
            halPort->ext.session.session = halSession.session;
            break;
    }
    CONVERT_CHECKED(audioPortConfigToHal(port.activeConfig, &halPort->active_config), result);
    return result;
}

status_t HidlUtils::audioTransportsFromHal(const struct audio_port_v7& halPort, bool isInput,
                                           hidl_vec<AudioTransport>* transports) {
    if (halPort.num_audio_profiles > AUDIO_PORT_MAX_AUDIO_PROFILES ||
        halPort.num_extra_audio_descriptors > AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS) {
        ALOGE("%s, too many audio profiles(%u) or extra audio descriptors(%u)", __func__,
              halPort.num_audio_profiles, halPort.num_extra_audio_descriptors);
        return BAD_VALUE;
    }
    status_t result = NO_ERROR;
    transports->resize(halPort.num_audio_profiles + halPort.num_extra_audio_descriptors);
    size_t idx = 0;
    for (size_t i = 0; i < halPort.num_audio_profiles; ++i) {
        auto& transport = (*transports)[idx++];
        transport.audioCapability.profile({});
        CONVERT_CHECKED(audioProfileFromHal(halPort.audio_profiles[i], isInput,
                                            &transport.audioCapability.profile()),
                        result);
        CONVERT_CHECKED(encapsulationTypeFromHal(halPort.audio_profiles[i].encapsulation_type,
                                                 &transport.encapsulationType),
                        result);
    }
    for (size_t i = 0; i < halPort.num_extra_audio_descriptors; ++i) {
        switch (halPort.extra_audio_descriptors[i].standard) {
            case AUDIO_STANDARD_EDID: {
                const struct audio_extra_audio_descriptor* extraAudioDescriptor =
                        &halPort.extra_audio_descriptors[i];
                if (extraAudioDescriptor->descriptor_length <= EXTRA_AUDIO_DESCRIPTOR_SIZE) {
                    auto& transport = (*transports)[idx++];
                    transport.audioCapability.edid(
                            hidl_vec<uint8_t>(extraAudioDescriptor->descriptor,
                                              extraAudioDescriptor->descriptor +
                                                      extraAudioDescriptor->descriptor_length));
                    CONVERT_CHECKED(
                            encapsulationTypeFromHal(extraAudioDescriptor->encapsulation_type,
                                                     &transport.encapsulationType),
                            result);
                } else {
                    ALOGE("%s, invalid descriptor length %u", __func__,
                          extraAudioDescriptor->descriptor_length);
                    result = BAD_VALUE;
                }
            } break;
            case AUDIO_STANDARD_NONE:
            default:
                ALOGE("%s, invalid standard %u", __func__,
                      halPort.extra_audio_descriptors[i].standard);
                result = BAD_VALUE;
                break;
        }
    }
    return result;
}

status_t HidlUtils::audioTransportsToHal(const hidl_vec<AudioTransport>& transports,
                                         struct audio_port_v7* halPort) {
    status_t result = NO_ERROR;
    halPort->num_audio_profiles = 0;
    halPort->num_extra_audio_descriptors = 0;
    for (const auto& transport : transports) {
        switch (transport.audioCapability.getDiscriminator()) {
            case AudioTransport::AudioCapability::hidl_discriminator::profile:
                if (halPort->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) {
                    ALOGE("%s, too many audio profiles", __func__);
                    result = BAD_VALUE;
                    break;
                }
                CONVERT_CHECKED(
                        audioProfileToHal(transport.audioCapability.profile(),
                                          &halPort->audio_profiles[halPort->num_audio_profiles]),
                        result);
                CONVERT_CHECKED(encapsulationTypeToHal(
                                        transport.encapsulationType,
                                        &halPort->audio_profiles[halPort->num_audio_profiles++]
                                                 .encapsulation_type),
                                result);
                break;
            case AudioTransport::AudioCapability::hidl_discriminator::edid:
                if (halPort->num_extra_audio_descriptors >=
                    AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS) {
                    ALOGE("%s, too many extra audio descriptors", __func__);
                    result = BAD_VALUE;
                    break;
                }
                if (transport.audioCapability.edid().size() > EXTRA_AUDIO_DESCRIPTOR_SIZE) {
                    ALOGE("%s, wrong edid size %zu", __func__,
                          transport.audioCapability.edid().size());
                    result = BAD_VALUE;
                    break;
                }
                struct audio_extra_audio_descriptor* extraAudioDescriptor =
                        &halPort->extra_audio_descriptors[halPort->num_extra_audio_descriptors++];
                extraAudioDescriptor->standard = AUDIO_STANDARD_EDID;
                extraAudioDescriptor->descriptor_length = transport.audioCapability.edid().size();
                memcpy(extraAudioDescriptor->descriptor, transport.audioCapability.edid().data(),
                       transport.audioCapability.edid().size() * sizeof(uint8_t));

                CONVERT_CHECKED(encapsulationTypeToHal(transport.encapsulationType,
                                                       &extraAudioDescriptor->encapsulation_type),
                                result);
                break;
        }
    }
    return result;
}

status_t HidlUtils::audioProfileFromHal(const struct audio_profile& halProfile, bool isInput,
                                        AudioProfile* profile) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioFormatFromHal(halProfile.format, &profile->format), result);
    profile->sampleRates.resize(halProfile.num_sample_rates);
    for (size_t i = 0; i < halProfile.num_sample_rates; ++i) {
        profile->sampleRates[i] = halProfile.sample_rates[i];
    }
    profile->channelMasks.resize(halProfile.num_channel_masks);
    for (size_t i = 0; i < halProfile.num_channel_masks; ++i) {
        CONVERT_CHECKED(audioChannelMaskFromHal(halProfile.channel_masks[i], isInput,
                                                &profile->channelMasks[i]),
                        result);
    }
    return result;
}

status_t HidlUtils::audioProfileToHal(const AudioProfile& profile,
                                      struct audio_profile* halProfile) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioFormatToHal(profile.format, &halProfile->format), result);
    memset(halProfile->sample_rates, 0, sizeof(halProfile->sample_rates));
    halProfile->num_sample_rates = profile.sampleRates.size();
    if (halProfile->num_sample_rates > AUDIO_PORT_MAX_SAMPLING_RATES) {
        ALOGE("HIDL Audio profile has too many sample rates: %u", halProfile->num_sample_rates);
        halProfile->num_sample_rates = AUDIO_PORT_MAX_SAMPLING_RATES;
        result = BAD_VALUE;
    }
    for (size_t i = 0; i < halProfile->num_sample_rates; ++i) {
        halProfile->sample_rates[i] = profile.sampleRates[i];
    }
    memset(halProfile->channel_masks, 0, sizeof(halProfile->channel_masks));
    halProfile->num_channel_masks = profile.channelMasks.size();
    if (halProfile->num_channel_masks > AUDIO_PORT_MAX_CHANNEL_MASKS) {
        ALOGE("HIDL Audio profile has too many channel masks: %u", halProfile->num_channel_masks);
        halProfile->num_channel_masks = AUDIO_PORT_MAX_CHANNEL_MASKS;
        result = BAD_VALUE;
    }
    for (size_t i = 0; i < halProfile->num_channel_masks; ++i) {
        CONVERT_CHECKED(
                audioChannelMaskToHal(profile.channelMasks[i], &halProfile->channel_masks[i]),
                status);
    }
    return result;
}

status_t HidlUtils::audioTagsFromHal(const std::vector<std::string>& strTags,
                                     hidl_vec<AudioTag>* tags) {
    status_t result = NO_ERROR;
    tags->resize(strTags.size());
    size_t to = 0;
    for (size_t from = 0; from < strTags.size(); ++from) {
        const auto& tag = strTags[from];
        if (xsd::isVendorExtension(tag)) {
            (*tags)[to++] = tag;
        } else {
            ALOGE("Vendor extension tag is ill-formed: \"%s\"", tag.c_str());
            result = BAD_VALUE;
        }
    }
    if (to != strTags.size()) {
        tags->resize(to);
    }
    return result;
}

status_t HidlUtils::audioTagsToHal(const hidl_vec<AudioTag>& tags, char* halTags) {
    memset(halTags, 0, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
    status_t result = NO_ERROR;
    std::ostringstream halTagsBuffer;
    bool hasValue = false;
    for (const auto& tag : tags) {
        if (hasValue) {
            halTagsBuffer << sAudioTagSeparator;
        }
        if (xsd::isVendorExtension(tag) && strchr(tag.c_str(), sAudioTagSeparator) == nullptr) {
            halTagsBuffer << tag;
            hasValue = true;
        } else {
            ALOGE("Vendor extension tag is ill-formed: \"%s\"", tag.c_str());
            result = BAD_VALUE;
        }
    }
    std::string fullHalTags{std::move(halTagsBuffer.str())};
    strncpy(halTags, fullHalTags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
    CONVERT_CHECKED(fullHalTags.length() <= AUDIO_ATTRIBUTES_TAGS_MAX_SIZE ? NO_ERROR : BAD_VALUE,
                    result);
    return result;
}

hidl_vec<AudioTag> HidlUtils::filterOutNonVendorTags(const hidl_vec<AudioTag>& tags) {
    hidl_vec<AudioTag> result;
    result.resize(tags.size());
    size_t resultIdx = 0;
    for (const auto& tag : tags) {
        if (xsd::maybeVendorExtension(tag)) {
            result[resultIdx++] = tag;
        }
    }
    if (resultIdx != result.size()) {
        result.resize(resultIdx);
    }
    return result;
}

std::vector<std::string> HidlUtils::filterOutNonVendorTags(const std::vector<std::string>& tags) {
    std::vector<std::string> result;
    std::copy_if(tags.begin(), tags.end(), std::back_inserter(result), xsd::maybeVendorExtension);
    return result;
}

std::vector<std::string> HidlUtils::splitAudioTags(const char* halTags) {
    return utils::splitString(halTags, sAudioTagSeparator);
}

status_t HidlUtils::deviceAddressFromHal(audio_devices_t halDeviceType,
                                         const char* halDeviceAddress, DeviceAddress* device) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioDeviceTypeFromHal(halDeviceType, &device->deviceType), result);
    if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
        device->address.mac({});
        if (halDeviceAddress != nullptr) {
            auto& mac = device->address.mac();
            int status = sscanf(halDeviceAddress, "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX", &mac[0], &mac[1],
                                &mac[2], &mac[3], &mac[4], &mac[5]);
            if (status != 6) {
                ALOGE("BT A2DP device \"%s\" MAC address \"%s\" is invalid",
                      device->deviceType.c_str(), halDeviceAddress);
                result = BAD_VALUE;
            }
        } else {
            ALOGE("BT A2DP device \"%s\" does not have a MAC address", halDeviceAddress);
            result = BAD_VALUE;
        }
    } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
        device->address.ipv4({});
        if (halDeviceAddress != nullptr) {
            auto& ipv4 = device->address.ipv4();
            int status = sscanf(halDeviceAddress, "%hhu.%hhu.%hhu.%hhu", &ipv4[0], &ipv4[1],
                                &ipv4[2], &ipv4[3]);
            if (status != 4) {
                ALOGE("IP device \"%s\" IPv4 address \"%s\" is invalid", device->deviceType.c_str(),
                      halDeviceAddress);
                result = BAD_VALUE;
            }
        } else {
            ALOGE("IP device \"%s\" does not have an IPv4 address", device->deviceType.c_str());
            result = BAD_VALUE;
        }
    } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
        device->address.alsa({});
        if (halDeviceAddress != nullptr) {
            auto& alsa = device->address.alsa();
            int status = sscanf(halDeviceAddress, "card=%d;device=%d", &alsa.card, &alsa.device);
            if (status != 2) {
                ALOGE("USB device \"%s\" ALSA address \"%s\" is invalid",
                      device->deviceType.c_str(), halDeviceAddress);
                result = BAD_VALUE;
            }
        } else {
            ALOGE("USB device \"%s\" does not have ALSA address", device->deviceType.c_str());
            result = BAD_VALUE;
        }
    } else {
        // Any other device type uses the 'id' field.
        device->address.id(halDeviceAddress != nullptr ? halDeviceAddress : "");
    }
    return result;
}

status_t HidlUtils::deviceAddressToHal(const DeviceAddress& device, audio_devices_t* halDeviceType,
                                       char* halDeviceAddress) {
    status_t result = NO_ERROR;
    CONVERT_CHECKED(audioDeviceTypeToHal(device.deviceType, halDeviceType), result);
    memset(halDeviceAddress, 0, AUDIO_DEVICE_MAX_ADDRESS_LEN);
    if (audio_is_a2dp_out_device(*halDeviceType) || audio_is_a2dp_in_device(*halDeviceType)) {
        if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::mac) {
            const auto& mac = device.address.mac();
            snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN,
                     "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4],
                     mac[5]);
        } else {
            ALOGE("BT A2DP device \"%s\" does not have MAC address set", device.deviceType.c_str());
            result = BAD_VALUE;
        }
    } else if (*halDeviceType == AUDIO_DEVICE_OUT_IP || *halDeviceType == AUDIO_DEVICE_IN_IP) {
        if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::ipv4) {
            const auto& ipv4 = device.address.ipv4();
            snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%d.%d.%d.%d", ipv4[0],
                     ipv4[1], ipv4[2], ipv4[3]);
        } else {
            ALOGE("IP device \"%s\" does not have IPv4 address set", device.deviceType.c_str());
            result = BAD_VALUE;
        }
    } else if (audio_is_usb_out_device(*halDeviceType) || audio_is_usb_in_device(*halDeviceType)) {
        if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::alsa) {
            const auto& alsa = device.address.alsa();
            snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "card=%d;device=%d", alsa.card,
                     alsa.device);
        } else {
            ALOGE("USB device \"%s\" does not have ALSA address set", device.deviceType.c_str());
            result = BAD_VALUE;
        }
    } else {
        // Any other device type uses the 'id' field.
        if (device.address.getDiscriminator() == DeviceAddress::Address::hidl_discriminator::id) {
            snprintf(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN, "%s",
                     device.address.id().c_str());
        }
    }
    return result;
}

}  // namespace implementation
}  // namespace CPP_VERSION
}  // namespace common
}  // namespace audio
}  // namespace hardware
}  // namespace android