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