summaryrefslogtreecommitdiff
path: root/audio/common/all-versions/default/tests/hidlutils6_tests.cpp
blob: ca59b9de6c0163d2f78f408ba599982ce1596694 (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
/*
 * Copyright (C) 2021 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 <gtest/gtest.h>

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

#include <HidlUtils.h>
#include <system/audio.h>

using namespace android;
using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;

// Not generated automatically because DeviceAddress contains
// an union.
//
// operator== must be defined in the same namespace as the data type.
namespace android::hardware::audio::common::COMMON_TYPES_CPP_VERSION {

inline bool operator==(const DeviceAddress& lhs, const DeviceAddress& rhs) {
    if (lhs.device != rhs.device) return false;
    audio_devices_t halDeviceType = static_cast<audio_devices_t>(lhs.device);
    if (audio_is_a2dp_out_device(halDeviceType) || audio_is_a2dp_in_device(halDeviceType)) {
        return lhs.address.mac == rhs.address.mac;
    } else if (halDeviceType == AUDIO_DEVICE_OUT_IP || halDeviceType == AUDIO_DEVICE_IN_IP) {
        return lhs.address.ipv4 == rhs.address.ipv4;
    } else if (audio_is_usb_out_device(halDeviceType) || audio_is_usb_in_device(halDeviceType)) {
        return lhs.address.alsa == rhs.address.alsa;
    } else if (halDeviceType == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
               halDeviceType == AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
        return lhs.rSubmixAddress == rhs.rSubmixAddress;
    }
    // busAddress field can be used for types other than bus, e.g. for microphones.
    return lhs.busAddress == rhs.busAddress;
}

}  // namespace android::hardware::audio::common::COMMON_TYPES_CPP_VERSION

static void ConvertDeviceAddress(const DeviceAddress& device) {
    audio_devices_t halDeviceType;
    char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
    EXPECT_EQ(NO_ERROR, HidlUtils::deviceAddressToHal(device, &halDeviceType, halDeviceAddress));
    DeviceAddress deviceBack;
    EXPECT_EQ(NO_ERROR,
              HidlUtils::deviceAddressFromHal(halDeviceType, halDeviceAddress, &deviceBack));
    EXPECT_EQ(device, deviceBack);
}

TEST(HidlUtils6, ConvertUniqueDeviceAddress) {
    DeviceAddress speaker;
    speaker.device = AudioDevice::OUT_SPEAKER;
    ConvertDeviceAddress(speaker);

    DeviceAddress micWithAddress;
    micWithAddress.device = AudioDevice::IN_BUILTIN_MIC;
    micWithAddress.busAddress = "bottom";
    ConvertDeviceAddress(micWithAddress);
}

TEST(HidlUtils6, ConvertA2dpDeviceAddress) {
    DeviceAddress a2dpSpeaker;
    a2dpSpeaker.device = AudioDevice::OUT_BLUETOOTH_A2DP_SPEAKER;
    a2dpSpeaker.address.mac = std::array<uint8_t, 6>{1, 2, 3, 4, 5, 6};
    ConvertDeviceAddress(a2dpSpeaker);
}

TEST(HidlUtils6, ConvertIpv4DeviceAddress) {
    DeviceAddress ipv4;
    ipv4.device = AudioDevice::OUT_IP;
    ipv4.address.ipv4 = std::array<uint8_t, 4>{1, 2, 3, 4};
    ConvertDeviceAddress(ipv4);
}

TEST(HidlUtils6, ConvertUsbDeviceAddress) {
    DeviceAddress usbHeadset;
    usbHeadset.device = AudioDevice::OUT_USB_HEADSET;
    usbHeadset.address.alsa = {1, 2};
    ConvertDeviceAddress(usbHeadset);
}

TEST(HidlUtils6, ConvertBusDeviceAddress) {
    DeviceAddress bus;
    bus.device = AudioDevice::OUT_BUS;
    bus.busAddress = "bus_device";
    ConvertDeviceAddress(bus);
}

TEST(HidlUtils6, ConvertRSubmixDeviceAddress) {
    DeviceAddress rSubmix;
    rSubmix.device = AudioDevice::OUT_REMOTE_SUBMIX;
    rSubmix.rSubmixAddress = AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS;
    ConvertDeviceAddress(rSubmix);
}