MtpDevice.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_DEVICE_H
  17. #define _MTP_DEVICE_H
  18. #include "MtpEventPacket.h"
  19. #include "MtpDataPacket.h"
  20. #include "MtpRequestPacket.h"
  21. #include "MtpResponsePacket.h"
  22. #include "MtpTypes.h"
  23. #include <mutex>
  24. struct usb_device;
  25. struct usb_request;
  26. struct usb_endpoint_descriptor;
  27. namespace android {
  28. class MtpDeviceInfo;
  29. class MtpEventPacket;
  30. class MtpObjectInfo;
  31. class MtpStorageInfo;
  32. class MtpDevice {
  33. private:
  34. struct usb_device* mDevice;
  35. int mInterface;
  36. struct usb_request* mRequestIn1;
  37. struct usb_request* mRequestIn2;
  38. struct usb_request* mRequestOut;
  39. struct usb_request* mRequestIntr;
  40. MtpDeviceInfo* mDeviceInfo;
  41. MtpPropertyList mDeviceProperties;
  42. // current session ID
  43. MtpSessionID mSessionID;
  44. // current transaction ID
  45. MtpTransactionID mTransactionID;
  46. MtpRequestPacket mRequest;
  47. MtpDataPacket mData;
  48. MtpResponsePacket mResponse;
  49. MtpEventPacket mEventPacket;
  50. // set to true if we received a response packet instead of a data packet
  51. bool mReceivedResponse;
  52. bool mProcessingEvent;
  53. int mCurrentEventHandle;
  54. // to check if a sendObject request follows the last sendObjectInfo request.
  55. MtpTransactionID mLastSendObjectInfoTransactionID;
  56. MtpObjectHandle mLastSendObjectInfoObjectHandle;
  57. // to ensure only one MTP transaction at a time
  58. std::mutex mMutex;
  59. std::mutex mEventMutex;
  60. std::mutex mEventMutexForInterrupt;
  61. // Remember the device's packet division mode.
  62. UrbPacketDivisionMode mPacketDivisionMode;
  63. public:
  64. typedef bool (*ReadObjectCallback)
  65. (void* data, uint32_t offset, uint32_t length, void* clientData);
  66. MtpDevice(struct usb_device* device,
  67. int interface,
  68. const struct usb_endpoint_descriptor *ep_in,
  69. const struct usb_endpoint_descriptor *ep_out,
  70. const struct usb_endpoint_descriptor *ep_intr);
  71. static MtpDevice* open(const char* deviceName, int fd);
  72. virtual ~MtpDevice();
  73. void initialize();
  74. void close();
  75. void print();
  76. const char* getDeviceName();
  77. bool openSession();
  78. bool closeSession();
  79. MtpDeviceInfo* getDeviceInfo();
  80. MtpStorageIDList* getStorageIDs();
  81. MtpStorageInfo* getStorageInfo(MtpStorageID storageID);
  82. MtpObjectHandleList* getObjectHandles(MtpStorageID storageID, MtpObjectFormat format,
  83. MtpObjectHandle parent);
  84. MtpObjectInfo* getObjectInfo(MtpObjectHandle handle);
  85. void* getThumbnail(MtpObjectHandle handle, int& outLength);
  86. MtpObjectHandle sendObjectInfo(MtpObjectInfo* info);
  87. bool sendObject(MtpObjectHandle handle, uint32_t size, int srcFD);
  88. bool deleteObject(MtpObjectHandle handle);
  89. MtpObjectHandle getParent(MtpObjectHandle handle);
  90. MtpStorageID getStorageID(MtpObjectHandle handle);
  91. MtpObjectPropertyList* getObjectPropsSupported(MtpObjectFormat format);
  92. MtpProperty* getDevicePropDesc(MtpDeviceProperty code);
  93. MtpProperty* getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format);
  94. // Reads value of |property| for |handle|. Returns true on success.
  95. bool getObjectPropValue(MtpObjectHandle handle, MtpProperty* property);
  96. bool readObject(MtpObjectHandle handle, ReadObjectCallback callback,
  97. uint32_t objectSize, void* clientData);
  98. bool readObject(MtpObjectHandle handle, const char* destPath, int group,
  99. int perm);
  100. bool readObject(MtpObjectHandle handle, int fd);
  101. bool readPartialObject(MtpObjectHandle handle,
  102. uint32_t offset,
  103. uint32_t size,
  104. uint32_t *writtenSize,
  105. ReadObjectCallback callback,
  106. void* clientData);
  107. bool readPartialObject64(MtpObjectHandle handle,
  108. uint64_t offset,
  109. uint32_t size,
  110. uint32_t *writtenSize,
  111. ReadObjectCallback callback,
  112. void* clientData);
  113. // Starts a request to read MTP event from MTP device. It returns a request handle that
  114. // can be used for blocking read or cancel. If other thread has already been processing an
  115. // event returns -1.
  116. int submitEventRequest();
  117. // Waits for MTP event from the device and returns MTP event code. It blocks the current thread
  118. // until it receives an event from the device. |handle| should be a request handle returned
  119. // by |submitEventRequest|. The function writes event parameters to |parameters|. Returns 0 for
  120. // cancellations. Returns -1 for errors.
  121. int reapEventRequest(int handle, uint32_t (*parameters)[3]);
  122. // Cancels an event request. |handle| should be request handle returned by
  123. // |submitEventRequest|. If there is a thread blocked by |reapEventRequest| with the same
  124. // |handle|, the thread will resume.
  125. void discardEventRequest(int handle);
  126. private:
  127. // If |objectSize| is not NULL, it checks object size before reading data bytes.
  128. bool readObjectInternal(MtpObjectHandle handle,
  129. ReadObjectCallback callback,
  130. const uint32_t* objectSize,
  131. void* clientData);
  132. // If |objectSize| is not NULL, it checks object size before reading data bytes.
  133. bool readData(ReadObjectCallback callback,
  134. const uint32_t* objectSize,
  135. uint32_t* writtenData,
  136. void* clientData);
  137. bool sendRequest(MtpOperationCode operation);
  138. bool sendData();
  139. bool readData();
  140. bool writeDataHeader(MtpOperationCode operation, int dataLength);
  141. MtpResponseCode readResponse();
  142. };
  143. }; // namespace android
  144. #endif // _MTP_DEVICE_H