Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 23 | namespace android { |
| 24 | |
Mike Lockwood | 5cdceca | 2010-07-20 09:47:41 -0400 | [diff] [blame] | 25 | class MtpStringBuffer; |
| 26 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 27 | class MtpDataPacket : public MtpPacket { |
| 28 | private: |
| 29 | // current offset for get/put methods |
| 30 | int mOffset; |
| 31 | |
| 32 | public: |
| 33 | MtpDataPacket(); |
| 34 | virtual ~MtpDataPacket(); |
| 35 | |
| 36 | virtual void reset(); |
| 37 | |
| 38 | void setOperationCode(MtpOperationCode code); |
| 39 | void setTransactionID(MtpTransactionID id); |
| 40 | |
| 41 | inline uint8_t getUInt8() { return (uint8_t)mBuffer[mOffset++]; } |
| 42 | inline int8_t getInt8() { return (int8_t)mBuffer[mOffset++]; } |
| 43 | uint16_t getUInt16(); |
| 44 | inline int16_t getInt16() { return (int16_t)getUInt16(); } |
| 45 | uint32_t getUInt32(); |
| 46 | inline int32_t getInt32() { return (int32_t)getUInt32(); } |
| 47 | uint64_t getUInt64(); |
| 48 | inline int64_t getInt64() { return (int64_t)getUInt64(); } |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 49 | void getUInt128(uint128_t& value); |
| 50 | inline void getInt128(int128_t& value) { getUInt128((uint128_t&)value); } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 51 | void getString(MtpStringBuffer& string); |
| 52 | |
Mike Lockwood | 335dd2b | 2010-05-19 10:33:39 -0400 | [diff] [blame] | 53 | Int8List* getAInt8(); |
| 54 | UInt8List* getAUInt8(); |
| 55 | Int16List* getAInt16(); |
| 56 | UInt16List* getAUInt16(); |
| 57 | Int32List* getAInt32(); |
| 58 | UInt32List* getAUInt32(); |
| 59 | Int64List* getAInt64(); |
| 60 | UInt64List* getAUInt64(); |
| 61 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 62 | void putInt8(int8_t value); |
| 63 | void putUInt8(uint8_t value); |
| 64 | void putInt16(int16_t value); |
| 65 | void putUInt16(uint16_t value); |
| 66 | void putInt32(int32_t value); |
| 67 | void putUInt32(uint32_t value); |
| 68 | void putInt64(int64_t value); |
| 69 | void putUInt64(uint64_t value); |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 70 | void putInt128(const int128_t& value); |
| 71 | void putUInt128(const uint128_t& value); |
Mike Lockwood | 8277cec | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 72 | void putInt128(int64_t value); |
| 73 | void putUInt128(uint64_t value); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 74 | |
| 75 | void putAInt8(const int8_t* values, int count); |
| 76 | void putAUInt8(const uint8_t* values, int count); |
| 77 | void putAInt16(const int16_t* values, int count); |
| 78 | void putAUInt16(const uint16_t* values, int count); |
Mike Lockwood | 782aef1 | 2010-08-10 07:37:50 -0400 | [diff] [blame] | 79 | void putAUInt16(const UInt16List* values); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 80 | void putAInt32(const int32_t* values, int count); |
| 81 | void putAUInt32(const uint32_t* values, int count); |
| 82 | void putAUInt32(const UInt32List* list); |
| 83 | void putAInt64(const int64_t* values, int count); |
| 84 | void putAUInt64(const uint64_t* values, int count); |
| 85 | void putString(const MtpStringBuffer& string); |
| 86 | void putString(const char* string); |
Mike Lockwood | 1865a5d | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 87 | void putString(const uint16_t* string); |
Mike Lockwood | de1e37a | 2010-08-18 12:31:09 -0400 | [diff] [blame] | 88 | inline void putEmptyString() { putUInt8(0); } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 89 | inline void putEmptyArray() { putUInt32(0); } |
| 90 | |
| 91 | |
| 92 | #ifdef MTP_DEVICE |
| 93 | // fill our buffer with data from the given file descriptor |
| 94 | int read(int fd); |
| 95 | int readDataHeader(int fd); |
| 96 | |
| 97 | // write our data to the given file descriptor |
| 98 | int write(int fd); |
| 99 | int writeDataHeader(int fd, uint32_t length); |
| 100 | #endif |
| 101 | |
| 102 | #ifdef MTP_HOST |
| 103 | int read(struct usb_endpoint *ep); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 104 | int readData(struct usb_endpoint *ep, void* buffer, int length); |
Mike Lockwood | b9ff444 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 105 | int readDataAsync(struct usb_endpoint *ep, void* buffer, int length); |
| 106 | int readDataWait(struct usb_endpoint *ep); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 107 | int readDataHeader(struct usb_endpoint *ep); |
| 108 | |
| 109 | int writeDataHeader(struct usb_endpoint *ep, uint32_t length); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 110 | int write(struct usb_endpoint *ep); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 111 | int write(struct usb_endpoint *ep, void* buffer, uint32_t length); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 112 | #endif |
| 113 | |
| 114 | inline bool hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; } |
Mike Lockwood | b9ff444 | 2010-11-19 11:20:19 -0500 | [diff] [blame] | 115 | inline uint32_t getContainerLength() const { return MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); } |
Mike Lockwood | 3e072b3 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 116 | void* getData(int& outLength) const; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 119 | }; // namespace android |
| 120 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 121 | #endif // _MTP_DATA_PACKET_H |