• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <media/AudioDeviceTypeAddr.h>
18 #include <arpa/inet.h>
19 #include <iostream>
20 #include <regex>
21 #include <set>
22 #include <sstream>
23 
24 #include <media/AidlConversion.h>
25 
26 namespace android {
27 
28 namespace {
29 
30 static const std::string SUPPRESSED = "SUPPRESSED";
31 static const std::regex MAC_ADDRESS_REGEX("([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}");
32 
isSenstiveAddress(const std::string & address)33 bool isSenstiveAddress(const std::string &address) {
34     if (std::regex_match(address, MAC_ADDRESS_REGEX)) {
35         return true;
36     }
37 
38     sockaddr_storage ss4;
39     if (inet_pton(AF_INET, address.c_str(), &ss4) > 0) {
40         return true;
41     }
42 
43     sockaddr_storage ss6;
44     if (inet_pton(AF_INET6, address.c_str(), &ss6) > 0) {
45         return true;
46     }
47 
48     return false;
49 }
50 
51 } // namespace
52 
AudioDeviceTypeAddr(audio_devices_t type,const std::string & address)53 AudioDeviceTypeAddr::AudioDeviceTypeAddr(audio_devices_t type, const std::string &address) :
54         mType(type), mAddress(address) {
55     mIsAddressSensitive = isSenstiveAddress(mAddress);
56 }
57 
getAddress() const58 const char* AudioDeviceTypeAddr::getAddress() const {
59     return mAddress.c_str();
60 }
61 
address() const62 const std::string& AudioDeviceTypeAddr::address() const {
63     return mAddress;
64 }
65 
setAddress(const std::string & address)66 void AudioDeviceTypeAddr::setAddress(const std::string& address) {
67     mAddress = address;
68     mIsAddressSensitive = isSenstiveAddress(mAddress);
69 }
70 
equals(const AudioDeviceTypeAddr & other) const71 bool AudioDeviceTypeAddr::equals(const AudioDeviceTypeAddr& other) const {
72     return mType == other.mType && mAddress == other.mAddress;
73 }
74 
operator <(const AudioDeviceTypeAddr & other) const75 bool AudioDeviceTypeAddr::operator<(const AudioDeviceTypeAddr& other) const {
76     if (mType < other.mType)  return true;
77     if (mType > other.mType)  return false;
78 
79     if (mAddress < other.mAddress)  return true;
80     // if (mAddress > other.mAddress)  return false;
81 
82     return false;
83 }
84 
operator ==(const AudioDeviceTypeAddr & rhs) const85 bool AudioDeviceTypeAddr::operator==(const AudioDeviceTypeAddr &rhs) const {
86     return equals(rhs);
87 }
88 
operator !=(const AudioDeviceTypeAddr & rhs) const89 bool AudioDeviceTypeAddr::operator!=(const AudioDeviceTypeAddr &rhs) const {
90     return !operator==(rhs);
91 }
92 
reset()93 void AudioDeviceTypeAddr::reset() {
94     mType = AUDIO_DEVICE_NONE;
95     setAddress("");
96 }
97 
toString(bool includeSensitiveInfo) const98 std::string AudioDeviceTypeAddr::toString(bool includeSensitiveInfo) const {
99     std::stringstream sstream;
100     sstream << "type:0x" << std::hex << mType;
101     // IP and MAC address are sensitive information. The sensitive information will be suppressed
102     // is `includeSensitiveInfo` is false.
103     sstream << ",@:"
104             << (!includeSensitiveInfo && mIsAddressSensitive ? SUPPRESSED : mAddress);
105     return sstream.str();
106 }
107 
readFromParcel(const Parcel * parcel)108 status_t AudioDeviceTypeAddr::readFromParcel(const Parcel *parcel) {
109     status_t status;
110     uint32_t rawDeviceType;
111     if ((status = parcel->readUint32(&rawDeviceType)) != NO_ERROR) return status;
112     mType = static_cast<audio_devices_t>(rawDeviceType);
113     status = parcel->readUtf8FromUtf16(&mAddress);
114     return status;
115 }
116 
writeToParcel(Parcel * parcel) const117 status_t AudioDeviceTypeAddr::writeToParcel(Parcel *parcel) const {
118     status_t status;
119     if ((status = parcel->writeUint32(mType)) != NO_ERROR) return status;
120     status = parcel->writeUtf8AsUtf16(mAddress);
121     return status;
122 }
123 
124 
getAudioDeviceTypes(const AudioDeviceTypeAddrVector & deviceTypeAddrs)125 DeviceTypeSet getAudioDeviceTypes(const AudioDeviceTypeAddrVector& deviceTypeAddrs) {
126     DeviceTypeSet deviceTypes;
127     for (const auto& deviceTypeAddr : deviceTypeAddrs) {
128         deviceTypes.insert(deviceTypeAddr.mType);
129     }
130     return deviceTypes;
131 }
132 
excludeDeviceTypeAddrsFrom(const AudioDeviceTypeAddrVector & devices,const AudioDeviceTypeAddrVector & devicesToExclude)133 AudioDeviceTypeAddrVector excludeDeviceTypeAddrsFrom(
134         const AudioDeviceTypeAddrVector& devices,
135         const AudioDeviceTypeAddrVector& devicesToExclude) {
136     std::set<AudioDeviceTypeAddr> devicesToExcludeSet(
137             devicesToExclude.begin(), devicesToExclude.end());
138     AudioDeviceTypeAddrVector remainedDevices;
139     for (const auto& device : devices) {
140         if (devicesToExcludeSet.count(device) == 0) {
141             remainedDevices.push_back(device);
142         }
143     }
144     return remainedDevices;
145 }
146 
dumpAudioDeviceTypeAddrVector(const AudioDeviceTypeAddrVector & deviceTypeAddrs,bool includeSensitiveInfo)147 std::string dumpAudioDeviceTypeAddrVector(const AudioDeviceTypeAddrVector& deviceTypeAddrs,
148                                           bool includeSensitiveInfo) {
149     std::stringstream stream;
150     for (auto it = deviceTypeAddrs.begin(); it != deviceTypeAddrs.end(); ++it) {
151         if (it != deviceTypeAddrs.begin()) {
152             stream << " ";
153         }
154         stream << it->toString(includeSensitiveInfo);
155     }
156     return stream.str();
157 }
158 
159 ConversionResult<AudioDeviceTypeAddr>
aidl2legacy_AudioDeviceTypeAddress(const media::AudioDevice & aidl)160 aidl2legacy_AudioDeviceTypeAddress(const media::AudioDevice& aidl) {
161     audio_devices_t type = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_devices_t(aidl.type));
162     return AudioDeviceTypeAddr(type, aidl.address);
163 }
164 
165 ConversionResult<media::AudioDevice>
legacy2aidl_AudioDeviceTypeAddress(const AudioDeviceTypeAddr & legacy)166 legacy2aidl_AudioDeviceTypeAddress(const AudioDeviceTypeAddr& legacy) {
167     media::AudioDevice aidl;
168     aidl.type = VALUE_OR_RETURN(legacy2aidl_audio_devices_t_int32_t(legacy.mType));
169     aidl.address = legacy.getAddress();
170     return aidl;
171 }
172 
173 } // namespace android
174