blob: 2b810633a27a915b6a0ce6136d147168080dd333 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
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
Mike Lockwood42d0b792011-01-04 14:48:57 -050023struct usb_device;
24struct usb_request;
25
Mike Lockwood7850ef92010-05-14 10:10:36 -040026namespace android {
27
Mike Lockwood5cdceca2010-07-20 09:47:41 -040028class MtpStringBuffer;
29
Mike Lockwood16864ba2010-05-11 17:16:59 -040030class MtpDataPacket : public MtpPacket {
31private:
32 // current offset for get/put methods
33 int mOffset;
34
35public:
36 MtpDataPacket();
37 virtual ~MtpDataPacket();
38
39 virtual void reset();
40
41 void setOperationCode(MtpOperationCode code);
42 void setTransactionID(MtpTransactionID id);
43
Mike Lockwoodef441d92011-07-14 21:00:02 -040044 inline const uint8_t* getData() const { return mBuffer + MTP_CONTAINER_HEADER_SIZE; }
Mike Lockwood16864ba2010-05-11 17:16:59 -040045 inline uint8_t getUInt8() { return (uint8_t)mBuffer[mOffset++]; }
46 inline int8_t getInt8() { return (int8_t)mBuffer[mOffset++]; }
47 uint16_t getUInt16();
48 inline int16_t getInt16() { return (int16_t)getUInt16(); }
49 uint32_t getUInt32();
50 inline int32_t getInt32() { return (int32_t)getUInt32(); }
51 uint64_t getUInt64();
52 inline int64_t getInt64() { return (int64_t)getUInt64(); }
Mike Lockwooda6c490b2010-06-05 22:45:01 -040053 void getUInt128(uint128_t& value);
54 inline void getInt128(int128_t& value) { getUInt128((uint128_t&)value); }
Mike Lockwood16864ba2010-05-11 17:16:59 -040055 void getString(MtpStringBuffer& string);
56
Mike Lockwood335dd2b2010-05-19 10:33:39 -040057 Int8List* getAInt8();
58 UInt8List* getAUInt8();
59 Int16List* getAInt16();
60 UInt16List* getAUInt16();
61 Int32List* getAInt32();
62 UInt32List* getAUInt32();
63 Int64List* getAInt64();
64 UInt64List* getAUInt64();
65
Mike Lockwood16864ba2010-05-11 17:16:59 -040066 void putInt8(int8_t value);
67 void putUInt8(uint8_t value);
68 void putInt16(int16_t value);
69 void putUInt16(uint16_t value);
70 void putInt32(int32_t value);
71 void putUInt32(uint32_t value);
72 void putInt64(int64_t value);
73 void putUInt64(uint64_t value);
Mike Lockwooda6c490b2010-06-05 22:45:01 -040074 void putInt128(const int128_t& value);
75 void putUInt128(const uint128_t& value);
Mike Lockwood8277cec2010-08-10 15:20:35 -040076 void putInt128(int64_t value);
77 void putUInt128(uint64_t value);
Mike Lockwood16864ba2010-05-11 17:16:59 -040078
79 void putAInt8(const int8_t* values, int count);
80 void putAUInt8(const uint8_t* values, int count);
81 void putAInt16(const int16_t* values, int count);
82 void putAUInt16(const uint16_t* values, int count);
Mike Lockwood782aef12010-08-10 07:37:50 -040083 void putAUInt16(const UInt16List* values);
Mike Lockwood16864ba2010-05-11 17:16:59 -040084 void putAInt32(const int32_t* values, int count);
85 void putAUInt32(const uint32_t* values, int count);
86 void putAUInt32(const UInt32List* list);
87 void putAInt64(const int64_t* values, int count);
88 void putAUInt64(const uint64_t* values, int count);
89 void putString(const MtpStringBuffer& string);
90 void putString(const char* string);
Mike Lockwood1865a5d2010-07-03 00:44:05 -040091 void putString(const uint16_t* string);
Mike Lockwoodde1e37a2010-08-18 12:31:09 -040092 inline void putEmptyString() { putUInt8(0); }
Mike Lockwood16864ba2010-05-11 17:16:59 -040093 inline void putEmptyArray() { putUInt32(0); }
94
95
96#ifdef MTP_DEVICE
97 // fill our buffer with data from the given file descriptor
98 int read(int fd);
Mike Lockwood16864ba2010-05-11 17:16:59 -040099
100 // write our data to the given file descriptor
101 int write(int fd);
Mike Lockwood64000782011-04-24 18:40:17 -0700102 int writeData(int fd, void* data, uint32_t length);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400103#endif
104
105#ifdef MTP_HOST
Mike Lockwood42d0b792011-01-04 14:48:57 -0500106 int read(struct usb_request *request);
107 int readData(struct usb_request *request, void* buffer, int length);
108 int readDataAsync(struct usb_request *req);
109 int readDataWait(struct usb_device *device);
110 int readDataHeader(struct usb_request *ep);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400111
Mike Lockwood42d0b792011-01-04 14:48:57 -0500112 int writeDataHeader(struct usb_request *ep, uint32_t length);
113 int write(struct usb_request *ep);
114 int write(struct usb_request *ep, void* buffer, uint32_t length);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400115#endif
116
117 inline bool hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; }
Mike Lockwoodb9ff4442010-11-19 11:20:19 -0500118 inline uint32_t getContainerLength() const { return MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); }
Mike Lockwood3e072b32010-06-10 16:34:20 -0400119 void* getData(int& outLength) const;
Mike Lockwood16864ba2010-05-11 17:16:59 -0400120};
121
Mike Lockwood7850ef92010-05-14 10:10:36 -0400122}; // namespace android
123
Mike Lockwood16864ba2010-05-11 17:16:59 -0400124#endif // _MTP_DATA_PACKET_H