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