1 /*
2 * Copyright (C) 2016 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 #define LOG_TAG "MidiDeviceInfo"
18
19 #include <MidiDeviceInfo.h>
20
21 #include <binder/Parcel.h>
22 #include <log/log.h>
23 #include <utils/Errors.h>
24 #include <utils/String16.h>
25
26 namespace android {
27 namespace media {
28 namespace midi {
29
30 // The constant values need to be kept in sync with MidiDeviceInfo.java.
31 // static
32 const char* const MidiDeviceInfo::PROPERTY_NAME = "name";
33 const char* const MidiDeviceInfo::PROPERTY_MANUFACTURER = "manufacturer";
34 const char* const MidiDeviceInfo::PROPERTY_PRODUCT = "product";
35 const char* const MidiDeviceInfo::PROPERTY_VERSION = "version";
36 const char* const MidiDeviceInfo::PROPERTY_SERIAL_NUMBER = "serial_number";
37 const char* const MidiDeviceInfo::PROPERTY_ALSA_CARD = "alsa_card";
38 const char* const MidiDeviceInfo::PROPERTY_ALSA_DEVICE = "alsa_device";
39
getProperty(const char * propertyName)40 String16 MidiDeviceInfo::getProperty(const char* propertyName) {
41 String16 value;
42 if (mProperties.getString(String16(propertyName), &value)) {
43 return value;
44 } else {
45 return String16();
46 }
47 }
48
49 #define RETURN_IF_FAILED(calledOnce) \
50 { \
51 status_t returnStatus = calledOnce; \
52 if (returnStatus) { \
53 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
54 return returnStatus; \
55 } \
56 }
57
writeToParcel(Parcel * parcel) const58 status_t MidiDeviceInfo::writeToParcel(Parcel* parcel) const {
59 // Needs to be kept in sync with code in MidiDeviceInfo.java
60 RETURN_IF_FAILED(parcel->writeInt32(mType));
61 RETURN_IF_FAILED(parcel->writeInt32(mId));
62 RETURN_IF_FAILED(parcel->writeInt32((int32_t)mInputPortNames.size()));
63 RETURN_IF_FAILED(parcel->writeInt32((int32_t)mOutputPortNames.size()));
64 RETURN_IF_FAILED(writeStringVector(parcel, mInputPortNames));
65 RETURN_IF_FAILED(writeStringVector(parcel, mOutputPortNames));
66 RETURN_IF_FAILED(parcel->writeInt32(mIsPrivate ? 1 : 0));
67 RETURN_IF_FAILED(parcel->writeInt32(mDefaultProtocol));
68 RETURN_IF_FAILED(mProperties.writeToParcel(parcel));
69 // This corresponds to "extra" properties written by Java code
70 RETURN_IF_FAILED(mProperties.writeToParcel(parcel));
71 return OK;
72 }
73
readFromParcel(const Parcel * parcel)74 status_t MidiDeviceInfo::readFromParcel(const Parcel* parcel) {
75 // Needs to be kept in sync with code in MidiDeviceInfo.java
76 RETURN_IF_FAILED(parcel->readInt32(&mType));
77 RETURN_IF_FAILED(parcel->readInt32(&mId));
78 int32_t inputPortCount;
79 RETURN_IF_FAILED(parcel->readInt32(&inputPortCount));
80 int32_t outputPortCount;
81 RETURN_IF_FAILED(parcel->readInt32(&outputPortCount));
82 RETURN_IF_FAILED(readStringVector(parcel, &mInputPortNames, inputPortCount));
83 RETURN_IF_FAILED(readStringVector(parcel, &mOutputPortNames, outputPortCount));
84 int32_t isPrivate;
85 RETURN_IF_FAILED(parcel->readInt32(&isPrivate));
86 mIsPrivate = isPrivate == 1;
87 RETURN_IF_FAILED(parcel->readInt32(&mDefaultProtocol));
88 RETURN_IF_FAILED(mProperties.readFromParcel(parcel));
89 // Ignore "extra" properties as they may contain Java Parcelables
90 return OK;
91 }
92
readStringVector(const Parcel * parcel,Vector<String16> * vectorPtr,size_t defaultLength)93 status_t MidiDeviceInfo::readStringVector(
94 const Parcel* parcel, Vector<String16> *vectorPtr, size_t defaultLength) {
95 std::optional<std::vector<std::optional<String16>>> v;
96 status_t result = parcel->readString16Vector(&v);
97 if (result != OK) return result;
98 vectorPtr->clear();
99 if (v) {
100 for (const auto& iter : *v) {
101 if (iter) {
102 vectorPtr->push_back(*iter);
103 } else {
104 vectorPtr->push_back(String16());
105 }
106 }
107 } else {
108 vectorPtr->resize(defaultLength);
109 }
110 return OK;
111 }
112
writeStringVector(Parcel * parcel,const Vector<String16> & vector) const113 status_t MidiDeviceInfo::writeStringVector(Parcel* parcel, const Vector<String16>& vector) const {
114 std::vector<String16> v;
115 for (size_t i = 0; i < vector.size(); ++i) {
116 v.push_back(vector[i]);
117 }
118 return parcel->writeString16Vector(v);
119 }
120
121 // Vector does not define operator==
areVectorsEqual(const Vector<String16> & lhs,const Vector<String16> & rhs)122 static inline bool areVectorsEqual(const Vector<String16>& lhs, const Vector<String16>& rhs) {
123 if (lhs.size() != rhs.size()) return false;
124 for (size_t i = 0; i < lhs.size(); ++i) {
125 if (lhs[i] != rhs[i]) return false;
126 }
127 return true;
128 }
129
operator ==(const MidiDeviceInfo & lhs,const MidiDeviceInfo & rhs)130 bool operator==(const MidiDeviceInfo& lhs, const MidiDeviceInfo& rhs) {
131 return (lhs.mType == rhs.mType && lhs.mId == rhs.mId &&
132 areVectorsEqual(lhs.mInputPortNames, rhs.mInputPortNames) &&
133 areVectorsEqual(lhs.mOutputPortNames, rhs.mOutputPortNames) &&
134 lhs.mProperties == rhs.mProperties &&
135 lhs.mIsPrivate == rhs.mIsPrivate &&
136 lhs.mDefaultProtocol == rhs.mDefaultProtocol);
137 }
138
139 } // namespace midi
140 } // namespace media
141 } // namespace android
142