xref: /aosp_15_r20/frameworks/av/media/mtp/MtpFfsHandle.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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::vector<std::thread> mChildThreads;
64 
65     android::base::unique_fd mControl;
66     // "in" from the host's perspective => sink for mtp server
67     android::base::unique_fd mBulkIn;
68     // "out" from the host's perspective => source for mtp server
69     android::base::unique_fd mBulkOut;
70     android::base::unique_fd mIntr;
71 
72     aio_context_t mCtx;
73 
74     android::base::unique_fd mEventFd;
75     struct pollfd mPollFds[2];
76 
77     struct io_buffer mIobuf[NUM_IO_BUFS];
78 
79     // Submit an io request of given length. Return amount submitted or -1.
80     int iobufSubmit(struct io_buffer *buf, int fd, unsigned length, bool read);
81 
82     // Cancel submitted requests from start to end in the given array. Return 0 or -1.
83     int cancelEvents(struct iocb **iocb, struct io_event *events, unsigned start, unsigned end,
84 		     bool is_batch_cancel);
85 
86     // Wait for at minimum the given number of events. Returns the amount of data in the returned
87     // events. Increments counter by the number of events returned.
88     int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter);
89 
90 public:
91     int read(void *data, size_t len) override;
92     int write(const void *data, size_t len) override;
93 
94     int receiveFile(mtp_file_range mfr, bool zero_packet) override;
95     int sendFile(mtp_file_range mfr) override;
96     int sendEvent(mtp_event me) override;
97 
98     int start(bool ptp) override;
99     void close() override;
100 
101     bool writeDescriptors(bool ptp);
102 
103     MtpFfsHandle(int controlFd);
104     ~MtpFfsHandle();
105 };
106 
107 struct mtp_data_header {
108     /* length of packet, including this header */
109     __le32 length;
110     /* container type (2 for data packet) */
111     __le16 type;
112     /* MTP command code */
113     __le16 command;
114     /* MTP transaction ID */
115     __le32 transaction_id;
116 };
117 
118 } // namespace android
119 
120 #endif // _MTP_FFS_HANDLE_H
121 
122