MtpServer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_SERVER_H
  17. #define _MTP_SERVER_H
  18. #include "MtpRequestPacket.h"
  19. #include "MtpDataPacket.h"
  20. #include "MtpResponsePacket.h"
  21. #include "MtpEventPacket.h"
  22. #include "MtpStringBuffer.h"
  23. #include "mtp.h"
  24. #include "MtpUtils.h"
  25. #include "IMtpHandle.h"
  26. #include <memory>
  27. #include <mutex>
  28. #include <queue>
  29. namespace android {
  30. class IMtpDatabase;
  31. class MtpStorage;
  32. class MtpServer {
  33. private:
  34. IMtpDatabase* mDatabase;
  35. // appear as a PTP device
  36. bool mPtp;
  37. // Manufacturer to report in DeviceInfo
  38. MtpStringBuffer mDeviceInfoManufacturer;
  39. // Model to report in DeviceInfo
  40. MtpStringBuffer mDeviceInfoModel;
  41. // Device version to report in DeviceInfo
  42. MtpStringBuffer mDeviceInfoDeviceVersion;
  43. // Serial number to report in DeviceInfo
  44. MtpStringBuffer mDeviceInfoSerialNumber;
  45. // current session ID
  46. MtpSessionID mSessionID;
  47. // true if we have an open session and mSessionID is valid
  48. bool mSessionOpen;
  49. MtpRequestPacket mRequest;
  50. MtpDataPacket mData;
  51. MtpResponsePacket mResponse;
  52. MtpEventPacket mEvent;
  53. MtpStorageList mStorages;
  54. IMtpHandle* mHandle;
  55. // handle for new object, set by SendObjectInfo and used by SendObject
  56. MtpObjectHandle mSendObjectHandle;
  57. MtpObjectFormat mSendObjectFormat;
  58. MtpStringBuffer mSendObjectFilePath;
  59. size_t mSendObjectFileSize;
  60. time_t mSendObjectModifiedTime;
  61. std::mutex mMutex;
  62. // represents an MTP object that is being edited using the android extensions
  63. // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
  64. class ObjectEdit {
  65. public:
  66. MtpObjectHandle mHandle;
  67. MtpStringBuffer mPath;
  68. uint64_t mSize;
  69. MtpObjectFormat mFormat;
  70. int mFD;
  71. ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
  72. MtpObjectFormat format, int fd)
  73. : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
  74. }
  75. virtual ~ObjectEdit() {
  76. close(mFD);
  77. }
  78. };
  79. std::vector<ObjectEdit*> mObjectEditList;
  80. public:
  81. MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
  82. const char *deviceInfoManufacturer,
  83. const char *deviceInfoModel,
  84. const char *deviceInfoDeviceVersion,
  85. const char *deviceInfoSerialNumber);
  86. virtual ~MtpServer();
  87. MtpStorage* getStorage(MtpStorageID id);
  88. inline bool hasStorage() { return mStorages.size() > 0; }
  89. bool hasStorage(MtpStorageID id);
  90. void addStorage(MtpStorage* storage);
  91. void removeStorage(MtpStorage* storage);
  92. void run();
  93. void sendObjectAdded(MtpObjectHandle handle);
  94. void sendObjectRemoved(MtpObjectHandle handle);
  95. void sendObjectInfoChanged(MtpObjectHandle handle);
  96. void sendDevicePropertyChanged(MtpDeviceProperty property);
  97. private:
  98. void sendStoreAdded(MtpStorageID id);
  99. void sendStoreRemoved(MtpStorageID id);
  100. void sendEvent(MtpEventCode code, uint32_t param1);
  101. void addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
  102. uint64_t size, MtpObjectFormat format, int fd);
  103. ObjectEdit* getEditObject(MtpObjectHandle handle);
  104. void removeEditObject(MtpObjectHandle handle);
  105. void commitEdit(ObjectEdit* edit);
  106. bool handleRequest();
  107. MtpResponseCode doGetDeviceInfo();
  108. MtpResponseCode doOpenSession();
  109. MtpResponseCode doCloseSession();
  110. MtpResponseCode doGetStorageIDs();
  111. MtpResponseCode doGetStorageInfo();
  112. MtpResponseCode doGetObjectPropsSupported();
  113. MtpResponseCode doGetObjectHandles();
  114. MtpResponseCode doGetNumObjects();
  115. MtpResponseCode doGetObjectReferences();
  116. MtpResponseCode doSetObjectReferences();
  117. MtpResponseCode doGetObjectPropValue();
  118. MtpResponseCode doSetObjectPropValue();
  119. MtpResponseCode doGetDevicePropValue();
  120. MtpResponseCode doSetDevicePropValue();
  121. MtpResponseCode doResetDevicePropValue();
  122. MtpResponseCode doGetObjectPropList();
  123. MtpResponseCode doGetObjectInfo();
  124. MtpResponseCode doGetObject();
  125. MtpResponseCode doGetThumb();
  126. MtpResponseCode doGetPartialObject(MtpOperationCode operation);
  127. MtpResponseCode doSendObjectInfo();
  128. MtpResponseCode doSendObject();
  129. MtpResponseCode doDeleteObject();
  130. MtpResponseCode doMoveObject();
  131. MtpResponseCode doCopyObject();
  132. MtpResponseCode doGetObjectPropDesc();
  133. MtpResponseCode doGetDevicePropDesc();
  134. MtpResponseCode doSendPartialObject();
  135. MtpResponseCode doTruncateObject();
  136. MtpResponseCode doBeginEditObject();
  137. MtpResponseCode doEndEditObject();
  138. };
  139. }; // namespace android
  140. #endif // _MTP_SERVER_H