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