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