MtpDataPacket.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef _MTP_DATA_PACKET_H
  17. #define _MTP_DATA_PACKET_H
  18. #include "MtpPacket.h"
  19. #include "mtp.h"
  20. struct usb_device;
  21. struct usb_request;
  22. namespace android {
  23. class IMtpHandle;
  24. class MtpStringBuffer;
  25. class MtpDataPacket : public MtpPacket {
  26. private:
  27. // current offset for get/put methods
  28. size_t mOffset;
  29. public:
  30. MtpDataPacket();
  31. virtual ~MtpDataPacket();
  32. virtual void reset();
  33. void setOperationCode(MtpOperationCode code);
  34. void setTransactionID(MtpTransactionID id);
  35. inline const uint8_t* getData() const { return mBuffer + MTP_CONTAINER_HEADER_SIZE; }
  36. bool getUInt8(uint8_t& value);
  37. inline bool getInt8(int8_t& value) { return getUInt8((uint8_t&)value); }
  38. bool getUInt16(uint16_t& value);
  39. inline bool getInt16(int16_t& value) { return getUInt16((uint16_t&)value); }
  40. bool getUInt32(uint32_t& value);
  41. inline bool getInt32(int32_t& value) { return getUInt32((uint32_t&)value); }
  42. bool getUInt64(uint64_t& value);
  43. inline bool getInt64(int64_t& value) { return getUInt64((uint64_t&)value); }
  44. bool getUInt128(uint128_t& value);
  45. inline bool getInt128(int128_t& value) { return getUInt128((uint128_t&)value); }
  46. bool getString(MtpStringBuffer& string);
  47. Int8List* getAInt8();
  48. UInt8List* getAUInt8();
  49. Int16List* getAInt16();
  50. UInt16List* getAUInt16();
  51. Int32List* getAInt32();
  52. UInt32List* getAUInt32();
  53. Int64List* getAInt64();
  54. UInt64List* getAUInt64();
  55. void putInt8(int8_t value);
  56. void putUInt8(uint8_t value);
  57. void putInt16(int16_t value);
  58. void putUInt16(uint16_t value);
  59. void putInt32(int32_t value);
  60. void putUInt32(uint32_t value);
  61. void putInt64(int64_t value);
  62. void putUInt64(uint64_t value);
  63. void putInt128(const int128_t& value);
  64. void putUInt128(const uint128_t& value);
  65. void putInt128(int64_t value);
  66. void putUInt128(uint64_t value);
  67. void putAInt8(const int8_t* values, int count);
  68. void putAUInt8(const uint8_t* values, int count);
  69. void putAInt16(const int16_t* values, int count);
  70. void putAUInt16(const uint16_t* values, int count);
  71. void putAUInt16(const UInt16List* values);
  72. void putAInt32(const int32_t* values, int count);
  73. void putAUInt32(const uint32_t* values, int count);
  74. void putAUInt32(const UInt32List* list);
  75. void putAInt64(const int64_t* values, int count);
  76. void putAUInt64(const uint64_t* values, int count);
  77. void putString(const MtpStringBuffer& string);
  78. void putString(const char* string);
  79. void putString(const uint16_t* string);
  80. inline void putEmptyString() { putUInt8(0); }
  81. inline void putEmptyArray() { putUInt32(0); }
  82. #ifdef MTP_DEVICE
  83. // fill our buffer with data from the given usb handle
  84. int read(IMtpHandle *h);
  85. // write our data to the given usb handle
  86. int write(IMtpHandle *h);
  87. int writeData(IMtpHandle *h, void* data, uint32_t length);
  88. #endif
  89. #ifdef MTP_HOST
  90. int read(struct usb_request *request);
  91. int readData(struct usb_request *request, void* buffer, int length);
  92. int readDataAsync(struct usb_request *req);
  93. int readDataWait(struct usb_device *device);
  94. int readDataHeader(struct usb_request *ep);
  95. // Write a whole data packet with payload to the end point given by a request. |divisionMode|
  96. // specifies whether to divide header and payload. See |UrbPacketDivisionMode| for meanings of
  97. // each value. Return the number of bytes (including header size) sent to the device on success.
  98. // Otherwise -1.
  99. int write(struct usb_request *request, UrbPacketDivisionMode divisionMode);
  100. // Similar to previous write method but it reads the payload from |fd|. If |size| is larger than
  101. // MTP_BUFFER_SIZE, the data will be sent by multiple bulk transfer requests.
  102. // Return type (int64_t) is used to handle the case that the size can be larger than 2GB.
  103. int64_t write(struct usb_request *request, UrbPacketDivisionMode divisionMode,
  104. int fd, size_t size);
  105. #endif
  106. inline bool hasData() const { return mPacketSize > MTP_CONTAINER_HEADER_SIZE; }
  107. inline uint32_t getContainerLength() const { return MtpPacket::getUInt32(MTP_CONTAINER_LENGTH_OFFSET); }
  108. void* getData(int* outLength) const;
  109. };
  110. }; // namespace android
  111. #endif // _MTP_DATA_PACKET_H