MtpFfsHandle.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2016 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_FFS_HANDLE_H
  17. #define _MTP_FFS_HANDLE_H
  18. #include <android-base/unique_fd.h>
  19. #include <linux/aio_abi.h>
  20. #include <mutex>
  21. #include <sys/poll.h>
  22. #include <time.h>
  23. #include <thread>
  24. #include <vector>
  25. #include <IMtpHandle.h>
  26. namespace android {
  27. constexpr int NUM_IO_BUFS = 2;
  28. struct io_buffer {
  29. std::vector<struct iocb> iocbs; // Holds memory for all iocbs. Not used directly.
  30. std::vector<struct iocb*> iocb; // Pointers to individual iocbs, for syscalls
  31. std::vector<unsigned char> bufs; // A large buffer, used with filesystem io
  32. std::vector<unsigned char*> buf; // Pointers within the larger buffer, for syscalls
  33. unsigned actual; // The number of buffers submitted for this request
  34. };
  35. template <class T> class MtpFfsHandleTest;
  36. class MtpFfsHandle : public IMtpHandle {
  37. template <class T> friend class MtpFfsHandleTest;
  38. protected:
  39. void closeConfig();
  40. void closeEndpoints();
  41. void advise(int fd);
  42. int handleControlRequest(const struct usb_ctrlrequest *request);
  43. int doAsync(void* data, size_t len, bool read, bool zero_packet);
  44. int handleEvent();
  45. void cancelTransaction();
  46. void doSendEvent(mtp_event me);
  47. bool openEndpoints(bool ptp);
  48. static int getPacketSize(int ffs_fd);
  49. bool mCanceled;
  50. android::base::unique_fd mControl;
  51. // "in" from the host's perspective => sink for mtp server
  52. android::base::unique_fd mBulkIn;
  53. // "out" from the host's perspective => source for mtp server
  54. android::base::unique_fd mBulkOut;
  55. android::base::unique_fd mIntr;
  56. aio_context_t mCtx;
  57. android::base::unique_fd mEventFd;
  58. struct pollfd mPollFds[2];
  59. struct io_buffer mIobuf[NUM_IO_BUFS];
  60. // Submit an io request of given length. Return amount submitted or -1.
  61. int iobufSubmit(struct io_buffer *buf, int fd, unsigned length, bool read);
  62. // Cancel submitted requests from start to end in the given array. Return 0 or -1.
  63. int cancelEvents(struct iocb **iocb, struct io_event *events, unsigned start, unsigned end);
  64. // Wait for at minimum the given number of events. Returns the amount of data in the returned
  65. // events. Increments counter by the number of events returned.
  66. int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter);
  67. public:
  68. int read(void *data, size_t len) override;
  69. int write(const void *data, size_t len) override;
  70. int receiveFile(mtp_file_range mfr, bool zero_packet) override;
  71. int sendFile(mtp_file_range mfr) override;
  72. int sendEvent(mtp_event me) override;
  73. int start(bool ptp) override;
  74. void close() override;
  75. bool writeDescriptors(bool ptp);
  76. MtpFfsHandle(int controlFd);
  77. ~MtpFfsHandle();
  78. };
  79. struct mtp_data_header {
  80. /* length of packet, including this header */
  81. __le32 length;
  82. /* container type (2 for data packet) */
  83. __le16 type;
  84. /* MTP command code */
  85. __le16 command;
  86. /* MTP transaction ID */
  87. __le32 transaction_id;
  88. };
  89. } // namespace android
  90. #endif // _MTP_FFS_HANDLE_H