blob: 577cea152f6991e39772c188520691730e65e981 [file] [log] [blame]
Mike Lockwood56118b52010-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 Lockwood215b6822011-01-04 14:48:57 -050023struct usb_device;
24struct usb_request;
25
Mike Lockwood8d3257a2010-05-14 10:10:36 -040026namespace android {
27
Mike Lockwood9bbc2ea2010-07-20 09:47:41 -040028class MtpStringBuffer;
29
Mike Lockwood56118b52010-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
44 inline uint8_t getUInt8() { return (uint8_t)mBuffer[mOffset++]; }
45 inline int8_t getInt8() { return (int8_t)mBuffer[mOffset++]; }
46 uint16_t getUInt16();
47 inline int16_t getInt16() { return (int16_t)getUInt16(); }
48 uint32_t getUInt32();
49 inline int32_t getInt32() { return (int32_t)getUInt32(); }
50 uint64_t getUInt64();
51 inline int64_t getInt64() { return (int64_t)getUInt64(); }
Mike Lockwood90f48732010-06-05 22:45:01 -040052 void getUInt128(uint128_t& value);
53 inline void getInt128(int128_t& value) { getUInt128((uint128_t&)value); }
Mike Lockwood56118b52010-05-11 17:16:59 -040054 void getString(MtpStringBuffer& string);
55
Mike Lockwood5bae7f62010-05-19 10:33:39 -040056 Int8List* getAInt8();
57 UInt8List* getAUInt8();
58 Int16List* getAInt16();
59 UInt16List* getAUInt16();
60 Int32List* getAInt32();
61 UInt32List* getAUInt32();
62 Int64List* getAInt64();
63 UInt64List* getAUInt64();
64
Mike Lockwood56118b52010-05-11 17:16:59 -040065 void putInt8(int8_t value);
66 void putUInt8(uint8_t value);
67 void putInt16(int16_t value);
68 void putUInt16(uint16_t value);
69 void putInt32(int32_t value);
70 void putUInt32(uint32_t value);
71 void putInt64(int64_t value);
72 void putUInt64(uint64_t value);
Mike Lockwood90f48732010-06-05 22:45:01 -040073 void putInt128(const int128_t& value);
74 void putUInt128(const uint128_t& value);
Mike Lockwood828d19d2010-08-10 15:20:35 -040075 void putInt128(int64_t value);
76 void putUInt128(uint64_t value);
Mike Lockwood56118b52010-05-11 17:16:59 -040077
78 void putAInt8(const int8_t* values, int count);
79 void putAUInt8(const uint8_t* values, int count);
80 void putAInt16(const int16_t* values, int count);
81 void putAUInt16(const uint16_t* values, int count);
Mike Lockwood4b322ce2010-08-10 07:37:50 -040082 void putAUInt16(const UInt16List* values);
Mike Lockwood56118b52010-05-11 17:16:59 -040083 void putAInt32(const int32_t* values, int count);
84 void putAUInt32(const uint32_t* values, int count);
85 void putAUInt32(const UInt32List* list);
86 void putAInt64(const int64_t* values, int count);
87 void putAUInt64(const uint64_t* values, int count);
88 void putString(const MtpStringBuffer& string);
89 void putString(const char* string);
Mike Lockwoodd21eac92010-07-03 00:44:05 -040090 void putString(const uint16_t* string);
Mike Lockwood9e837862010-08-18 12:31:09 -040091 inline void putEmptyString() { putUInt8(0); }
Mike Lockwood56118b52010-05-11 17:16:59 -040092 inline void putEmptyArray() { putUInt32(0); }
93
94
95#ifdef MTP_DEVICE
96 // fill our buffer with data from the given file descriptor
97 int read(int fd);
98 int readDataHeader(int fd);
99
100 // write our data to the given file descriptor
101 int write(int fd);
102 int writeDataHeader(int fd, uint32_t length);
103#endif
104
105#ifdef MTP_HOST
Mike Lockwood215b6822011-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 Lockwoodbc4cb0b2010-07-26 20:40:45 -0400111
Mike Lockwood215b6822011-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 Lockwood56118b52010-05-11 17:16:59 -0400115#endif
116
117 inline bool hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; }
Mike Lockwood954c2672010-11-19 11:20:19 -0500118 inline uint32_t getContainerLength() const { return MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); }
Mike Lockwooddda56862010-06-10 16:34:20 -0400119 void* getData(int& outLength) const;
Mike Lockwood56118b52010-05-11 17:16:59 -0400120};
121
Mike Lockwood8d3257a2010-05-14 10:10:36 -0400122}; // namespace android
123
Mike Lockwood56118b52010-05-11 17:16:59 -0400124#endif // _MTP_DATA_PACKET_H