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_DATA_PACKET_H
18 #define _MTP_DATA_PACKET_H
19 
20 #include "MtpPacket.h"
21 #include "mtp.h"
22 
23 struct usb_device;
24 struct usb_request;
25 
26 namespace android {
27 
28 class MtpStringBuffer;
29 
30 class MtpDataPacket : public MtpPacket {
31 private:
32     // current offset for get/put methods
33     size_t              mOffset;
34 
35 public:
36                         MtpDataPacket();
37     virtual             ~MtpDataPacket();
38 
39     virtual void        reset();
40 
41     void                setOperationCode(MtpOperationCode code);
42     void                setTransactionID(MtpTransactionID id);
43 
getData()44     inline const uint8_t*     getData() const { return mBuffer + MTP_CONTAINER_HEADER_SIZE; }
45 
46     bool                getUInt8(uint8_t& value);
getInt8(int8_t & value)47     inline bool         getInt8(int8_t& value) { return getUInt8((uint8_t&)value); }
48     bool                getUInt16(uint16_t& value);
getInt16(int16_t & value)49     inline bool         getInt16(int16_t& value) { return getUInt16((uint16_t&)value); }
50     bool                getUInt32(uint32_t& value);
getInt32(int32_t & value)51     inline bool         getInt32(int32_t& value) { return getUInt32((uint32_t&)value); }
52     bool                getUInt64(uint64_t& value);
getInt64(int64_t & value)53     inline bool         getInt64(int64_t& value) { return getUInt64((uint64_t&)value); }
54     bool                getUInt128(uint128_t& value);
getInt128(int128_t & value)55     inline bool         getInt128(int128_t& value) { return getUInt128((uint128_t&)value); }
56     bool                getString(MtpStringBuffer& string);
57 
58     Int8List*           getAInt8();
59     UInt8List*          getAUInt8();
60     Int16List*          getAInt16();
61     UInt16List*         getAUInt16();
62     Int32List*          getAInt32();
63     UInt32List*         getAUInt32();
64     Int64List*          getAInt64();
65     UInt64List*         getAUInt64();
66 
67     void                putInt8(int8_t value);
68     void                putUInt8(uint8_t value);
69     void                putInt16(int16_t value);
70     void                putUInt16(uint16_t value);
71     void                putInt32(int32_t value);
72     void                putUInt32(uint32_t value);
73     void                putInt64(int64_t value);
74     void                putUInt64(uint64_t value);
75     void                putInt128(const int128_t& value);
76     void                putUInt128(const uint128_t& value);
77     void                putInt128(int64_t value);
78     void                putUInt128(uint64_t value);
79 
80     void                putAInt8(const int8_t* values, int count);
81     void                putAUInt8(const uint8_t* values, int count);
82     void                putAInt16(const int16_t* values, int count);
83     void                putAUInt16(const uint16_t* values, int count);
84     void                putAUInt16(const UInt16List* values);
85     void                putAInt32(const int32_t* values, int count);
86     void                putAUInt32(const uint32_t* values, int count);
87     void                putAUInt32(const UInt32List* list);
88     void                putAInt64(const int64_t* values, int count);
89     void                putAUInt64(const uint64_t* values, int count);
90     void                putString(const MtpStringBuffer& string);
91     void                putString(const char* string);
92     void                putString(const uint16_t* string);
putEmptyString()93     inline void         putEmptyString() { putUInt8(0); }
putEmptyArray()94     inline void         putEmptyArray() { putUInt32(0); }
95 
96 
97 #ifdef MTP_DEVICE
98     // fill our buffer with data from the given file descriptor
99     int                 read(int fd);
100 
101     // write our data to the given file descriptor
102     int                 write(int fd);
103     int                 writeData(int fd, void* data, uint32_t length);
104 #endif
105 
106 #ifdef MTP_HOST
107     int                 read(struct usb_request *request);
108     int                 readData(struct usb_request *request, void* buffer, int length);
109     int                 readDataAsync(struct usb_request *req);
110     int                 readDataWait(struct usb_device *device);
111     int                 readDataHeader(struct usb_request *ep);
112 
113     int                 writeDataHeader(struct usb_request *ep, uint32_t length);
114     int                 write(struct usb_request *ep);
115     int                 write(struct usb_request *ep, void* buffer, uint32_t length);
116 #endif
117 
hasData()118     inline bool         hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; }
getContainerLength()119     inline uint32_t     getContainerLength() const { return MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); }
120     void*               getData(int& outLength) const;
121 };
122 
123 }; // namespace android
124 
125 #endif // _MTP_DATA_PACKET_H
126