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_PACKET_H
18 #define _MTP_PACKET_H
19 
20 #include <android-base/macros.h>
21 
22 #include "MtpTypes.h"
23 
24 struct usb_device;
25 struct usb_request;
26 
27 namespace android {
28 
29 class MtpPacket {
30 
31 protected:
32     uint8_t*            mBuffer;
33     // current size of the buffer
34     size_t              mBufferSize;
35     // number of bytes to add when resizing the buffer
36     size_t              mAllocationIncrement;
37     // size of the data in the packet
38     size_t              mPacketSize;
39 
40 public:
41     explicit            MtpPacket(int bufferSize);
42     virtual             ~MtpPacket();
43 
44     // sets packet size to the default container size and sets buffer to zero
45     virtual void        reset();
46 
47     void                allocate(size_t length);
48     void                dump();
49     void                copyFrom(const MtpPacket& src);
50 
51     uint16_t            getContainerCode() const;
52     void                setContainerCode(uint16_t code);
53 
54     uint16_t            getContainerType() const;
55 
56     MtpTransactionID    getTransactionID() const;
57     void                setTransactionID(MtpTransactionID id);
58 
59     uint32_t            getParameter(int index) const;
60     void                setParameter(int index, uint32_t value);
61 
62 #ifdef MTP_HOST
63     int                 transfer(struct usb_request* request);
64 #endif
65 
66 protected:
67     uint16_t            getUInt16(int offset) const;
68     uint32_t            getUInt32(int offset) const;
69     void                putUInt16(int offset, uint16_t value);
70     void                putUInt32(int offset, uint32_t value);
71 
72     DISALLOW_COPY_AND_ASSIGN(MtpPacket);
73 };
74 
75 }; // namespace android
76 
77 #endif // _MTP_PACKET_H
78