1 /*
2 * Copyright (C) 2021 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 <gtest/gtest.h>
18
19 #define LOG_TAG "HidlUtils_Test"
20 #include <log/log.h>
21
22 #include <HidlUtils.h>
23 #include <system/audio.h>
24
25 using namespace android;
26 using namespace ::android::hardware::audio::common::CPP_VERSION;
27 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
28
29 // Not generated automatically because DeviceAddress contains
30 // an union.
31 //
32 // operator== must be defined in the same namespace as the data type.
33 namespace android::hardware::audio::common::CPP_VERSION {
34
operator ==(const DeviceAddress & lhs,const DeviceAddress & rhs)35 inline bool operator==(const DeviceAddress& lhs, const DeviceAddress& rhs) {
36 if (lhs.device != rhs.device) return false;
37 audio_devices_t halDeviceType = static_cast<audio_devices_t>(lhs.device);
38 if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
39 return lhs.address.mac == rhs.address.mac;
40 } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
41 return lhs.address.ipv4 == rhs.address.ipv4;
42 } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
43 return lhs.address.alsa == rhs.address.alsa;
44 } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
45 halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
46 return lhs.rSubmixAddress == rhs.rSubmixAddress;
47 }
48 // busAddress field can be used for types other than bus, e.g. for microphones.
49 return lhs.busAddress == rhs.busAddress;
50 }
51
52 } // namespace android::hardware::audio::common::CPP_VERSION
53
ConvertDeviceAddress(const DeviceAddress & device)54 static void ConvertDeviceAddress(const DeviceAddress& device) {
55 audio_devices_t halDeviceType;
56 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
57 EXPECT_EQ(NO_ERROR, HidlUtils::deviceAddressToHal(device, &halDeviceType, halDeviceAddress));
58 DeviceAddress deviceBack;
59 EXPECT_EQ(NO_ERROR,
60 HidlUtils::deviceAddressFromHal(halDeviceType, halDeviceAddress, &deviceBack));
61 EXPECT_EQ(device, deviceBack);
62 }
63
TEST(HidlUtils6,ConvertUniqueDeviceAddress)64 TEST(HidlUtils6, ConvertUniqueDeviceAddress) {
65 DeviceAddress speaker;
66 speaker.device = AudioDevice::OUT_SPEAKER;
67 ConvertDeviceAddress(speaker);
68
69 DeviceAddress micWithAddress;
70 micWithAddress.device = AudioDevice::IN_BUILTIN_MIC;
71 micWithAddress.busAddress = "bottom";
72 ConvertDeviceAddress(micWithAddress);
73 }
74
TEST(HidlUtils6,ConvertA2dpDeviceAddress)75 TEST(HidlUtils6, ConvertA2dpDeviceAddress) {
76 DeviceAddress a2dpSpeaker;
77 a2dpSpeaker.device = AudioDevice::OUT_BLUETOOTH_A2DP_SPEAKER;
78 a2dpSpeaker.address.mac = std::array<uint8_t, 6>{1, 2, 3, 4, 5, 6};
79 ConvertDeviceAddress(a2dpSpeaker);
80 }
81
TEST(HidlUtils6,ConvertIpv4DeviceAddress)82 TEST(HidlUtils6, ConvertIpv4DeviceAddress) {
83 DeviceAddress ipv4;
84 ipv4.device = AudioDevice::OUT_IP;
85 ipv4.address.ipv4 = std::array<uint8_t, 4>{1, 2, 3, 4};
86 ConvertDeviceAddress(ipv4);
87 }
88
TEST(HidlUtils6,ConvertUsbDeviceAddress)89 TEST(HidlUtils6, ConvertUsbDeviceAddress) {
90 DeviceAddress usbHeadset;
91 usbHeadset.device = AudioDevice::OUT_USB_HEADSET;
92 usbHeadset.address.alsa = {1, 2};
93 ConvertDeviceAddress(usbHeadset);
94 }
95
TEST(HidlUtils6,ConvertBusDeviceAddress)96 TEST(HidlUtils6, ConvertBusDeviceAddress) {
97 DeviceAddress bus;
98 bus.device = AudioDevice::OUT_BUS;
99 bus.busAddress = "bus_device";
100 ConvertDeviceAddress(bus);
101 }
102
TEST(HidlUtils6,ConvertRSubmixDeviceAddress)103 TEST(HidlUtils6, ConvertRSubmixDeviceAddress) {
104 DeviceAddress rSubmix;
105 rSubmix.device = AudioDevice::OUT_REMOTE_SUBMIX;
106 rSubmix.rSubmixAddress = AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS;
107 ConvertDeviceAddress(rSubmix);
108 }
109