Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "MtpProperty" |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 18 | |
| 19 | #include "MtpDataPacket.h" |
| 20 | #include "MtpProperty.h" |
| 21 | #include "MtpStringBuffer.h" |
| 22 | #include "MtpUtils.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | MtpProperty::MtpProperty() |
| 27 | : mCode(0), |
| 28 | mType(0), |
| 29 | mWriteable(false), |
| 30 | mDefaultArrayLength(0), |
| 31 | mDefaultArrayValues(NULL), |
| 32 | mCurrentArrayLength(0), |
| 33 | mCurrentArrayValues(NULL), |
| 34 | mFormFlag(kFormNone), |
| 35 | mEnumLength(0), |
| 36 | mEnumValues(NULL) |
| 37 | { |
| 38 | mDefaultValue.str = NULL; |
| 39 | mCurrentValue.str = NULL; |
| 40 | mMinimumValue.str = NULL; |
| 41 | mMaximumValue.str = NULL; |
| 42 | } |
| 43 | |
| 44 | MtpProperty::~MtpProperty() { |
| 45 | if (mType == MTP_TYPE_STR) { |
| 46 | // free all strings |
| 47 | free(mDefaultValue.str); |
| 48 | free(mCurrentValue.str); |
| 49 | free(mMinimumValue.str); |
| 50 | free(mMaximumValue.str); |
| 51 | if (mDefaultArrayValues) { |
| 52 | for (int i = 0; i < mDefaultArrayLength; i++) |
| 53 | free(mDefaultArrayValues[i].str); |
| 54 | } |
| 55 | if (mCurrentArrayValues) { |
| 56 | for (int i = 0; i < mCurrentArrayLength; i++) |
| 57 | free(mCurrentArrayValues[i].str); |
| 58 | } |
| 59 | if (mEnumValues) { |
| 60 | for (int i = 0; i < mEnumLength; i++) |
| 61 | free(mEnumValues[i].str); |
| 62 | } |
| 63 | } |
| 64 | delete[] mDefaultArrayValues; |
| 65 | delete[] mCurrentArrayValues; |
| 66 | delete[] mEnumValues; |
| 67 | } |
| 68 | |
| 69 | void MtpProperty::read(MtpDataPacket& packet) { |
| 70 | MtpStringBuffer string; |
| 71 | |
| 72 | mCode = packet.getUInt16(); |
| 73 | mType = packet.getUInt16(); |
| 74 | mWriteable = (packet.getUInt8() == 1); |
| 75 | switch (mType) { |
| 76 | case MTP_TYPE_AINT8: |
| 77 | case MTP_TYPE_AUINT8: |
| 78 | case MTP_TYPE_AINT16: |
| 79 | case MTP_TYPE_AUINT16: |
| 80 | case MTP_TYPE_AINT32: |
| 81 | case MTP_TYPE_AUINT32: |
| 82 | case MTP_TYPE_AINT64: |
| 83 | case MTP_TYPE_AUINT64: |
| 84 | case MTP_TYPE_AINT128: |
| 85 | case MTP_TYPE_AUINT128: |
| 86 | mDefaultArrayValues = readArrayValues(packet, mDefaultArrayLength); |
| 87 | mCurrentArrayValues = readArrayValues(packet, mCurrentArrayLength); |
| 88 | break; |
| 89 | default: |
| 90 | readValue(packet, mDefaultValue); |
| 91 | readValue(packet, mCurrentValue); |
| 92 | } |
| 93 | mFormFlag = packet.getUInt8(); |
| 94 | |
| 95 | if (mFormFlag == kFormRange) { |
| 96 | readValue(packet, mMinimumValue); |
| 97 | readValue(packet, mMaximumValue); |
| 98 | readValue(packet, mStepSize); |
| 99 | } else if (mFormFlag == kFormEnum) { |
| 100 | mEnumLength = packet.getUInt16(); |
| 101 | mEnumValues = new MtpPropertyValue[mEnumLength]; |
| 102 | for (int i = 0; i < mEnumLength; i++) |
| 103 | readValue(packet, mEnumValues[i]); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void MtpProperty::print() { |
| 108 | LOGD("MtpProperty %04X\n", mCode); |
| 109 | LOGD(" type %04X\n", mType); |
| 110 | LOGD(" writeable %s\n", (mWriteable ? "true" : "false")); |
| 111 | } |
| 112 | |
| 113 | void MtpProperty::readValue(MtpDataPacket& packet, MtpPropertyValue& value) { |
| 114 | switch (mType) { |
| 115 | case MTP_TYPE_INT8: |
| 116 | value.i8 = packet.getInt8(); |
| 117 | break; |
| 118 | case MTP_TYPE_UINT8: |
| 119 | value.u8 = packet.getUInt8(); |
| 120 | break; |
| 121 | case MTP_TYPE_INT16: |
| 122 | value.i16 = packet.getInt16(); |
| 123 | break; |
| 124 | case MTP_TYPE_UINT16: |
| 125 | value.u16 = packet.getUInt16(); |
| 126 | break; |
| 127 | case MTP_TYPE_INT32: |
| 128 | value.i32 = packet.getInt32(); |
| 129 | break; |
| 130 | case MTP_TYPE_UINT32: |
| 131 | value.u32 = packet.getUInt32(); |
| 132 | break; |
| 133 | case MTP_TYPE_INT64: |
| 134 | value.i64 = packet.getInt64(); |
| 135 | break; |
| 136 | case MTP_TYPE_UINT64: |
| 137 | value.u64 = packet.getUInt64(); |
| 138 | break; |
| 139 | case MTP_TYPE_INT128: |
| 140 | packet.getInt128(value.i128); |
| 141 | break; |
| 142 | case MTP_TYPE_UINT128: |
| 143 | packet.getUInt128(value.u128); |
| 144 | break; |
| 145 | default: |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 146 | LOGE("unknown type %d in MtpProperty::readValue", mType); |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | MtpPropertyValue* MtpProperty::readArrayValues(MtpDataPacket& packet, int& length) { |
| 151 | length = packet.getUInt32(); |
| 152 | if (length == 0) |
| 153 | return NULL; |
| 154 | MtpPropertyValue* result = new MtpPropertyValue[length]; |
| 155 | for (int i = 0; i < length; i++) |
| 156 | readValue(packet, result[i]); |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | } // namespace android |