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 #ifndef _MTP_PROPERTY_H
18 #define _MTP_PROPERTY_H
19 
20 #include "MtpTypes.h"
21 
22 #include <string>
23 
24 namespace android {
25 
26 class MtpDataPacket;
27 
28 struct MtpPropertyValue {
29     union {
30         int8_t          i8;
31         uint8_t         u8;
32         int16_t         i16;
33         uint16_t        u16;
34         int32_t         i32;
35         uint32_t        u32;
36         int64_t         i64;
37         uint64_t        u64;
38         int128_t        i128;
39         uint128_t       u128;
40     } u;
41     // string in UTF8 format
42     char*               str;
43 };
44 
45 class MtpProperty {
46 public:
47     MtpPropertyCode     mCode;
48     MtpDataType         mType;
49     bool                mWriteable;
50     MtpPropertyValue    mDefaultValue;
51     MtpPropertyValue    mCurrentValue;
52 
53     // for array types
54     uint32_t            mDefaultArrayLength;
55     MtpPropertyValue*   mDefaultArrayValues;
56     uint32_t            mCurrentArrayLength;
57     MtpPropertyValue*   mCurrentArrayValues;
58 
59     enum {
60         kFormNone = 0,
61         kFormRange = 1,
62         kFormEnum = 2,
63         kFormDateTime = 3,
64     };
65 
66     uint32_t            mGroupCode;
67     uint8_t             mFormFlag;
68 
69     // for range form
70     MtpPropertyValue    mMinimumValue;
71     MtpPropertyValue    mMaximumValue;
72     MtpPropertyValue    mStepSize;
73 
74     // for enum form
75     uint16_t            mEnumLength;
76     MtpPropertyValue*   mEnumValues;
77 
78 public:
79                         MtpProperty();
80                         MtpProperty(MtpPropertyCode propCode,
81                                      MtpDataType type,
82                                      bool writeable = false,
83                                      int defaultValue = 0);
84     virtual             ~MtpProperty();
85 
getPropertyCode()86     MtpPropertyCode getPropertyCode() const { return mCode; }
getDataType()87     MtpDataType getDataType() const { return mType; }
88 
89     bool                read(MtpDataPacket& packet);
90     void                write(MtpDataPacket& packet);
91 
92     void                setDefaultValue(const uint16_t* string);
93     void                setCurrentValue(const uint16_t* string);
94     void                setCurrentValue(MtpDataPacket& packet);
getCurrentValue()95     const MtpPropertyValue& getCurrentValue() { return mCurrentValue; }
96 
97     void                setFormRange(int min, int max, int step);
98     void                setFormEnum(const int* values, int count);
99     void                setFormDateTime();
100 
101     void                print();
102 
isDeviceProperty()103     inline bool         isDeviceProperty() const {
104                             return (   ((mCode & 0xF000) == 0x5000)
105                                     || ((mCode & 0xF800) == 0xD000));
106                         }
107 
108 private:
109     bool                readValue(MtpDataPacket& packet, MtpPropertyValue& value);
110     void                writeValue(MtpDataPacket& packet, MtpPropertyValue& value);
111     MtpPropertyValue*   readArrayValues(MtpDataPacket& packet, uint32_t& length);
112     void                writeArrayValues(MtpDataPacket& packet,
113                                             MtpPropertyValue* values, uint32_t length);
114     void                print(MtpPropertyValue& value, std::string& buffer);
115 };
116 
117 }; // namespace android
118 
119 #endif // _MTP_PROPERTY_H
120