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