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