blob: 24a8bd54593e7185b902aa63aea4559d6125ebfb [file] [log] [blame]
Jerry Zhang487be612016-10-24 12:10:41 -07001/*
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>
Jerry Zhangdf69dd32017-05-03 17:17:49 -070021#include <linux/aio_abi.h>
22#include <mutex>
23#include <sys/poll.h>
24#include <time.h>
25#include <thread>
26#include <vector>
27
Jerry Zhang487be612016-10-24 12:10:41 -070028#include <IMtpHandle.h>
29
30namespace android {
31
Jerry Zhangdf69dd32017-05-03 17:17:49 -070032constexpr int NUM_IO_BUFS = 2;
33
34struct 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
42template <class T> class MtpFfsHandleTest;
Jerry Zhang487be612016-10-24 12:10:41 -070043
44class MtpFfsHandle : public IMtpHandle {
Jerry Zhang69b74502017-10-02 16:26:37 -070045 template <class T> friend class MtpFfsHandleTest;
Jerry Zhangdf69dd32017-05-03 17:17:49 -070046protected:
Jerry Zhang487be612016-10-24 12:10:41 -070047 void closeConfig();
48 void closeEndpoints();
Jerry Zhangdf69dd32017-05-03 17:17:49 -070049 void advise(int fd);
50 int handleControlRequest(const struct usb_ctrlrequest *request);
51 int doAsync(void* data, size_t len, bool read);
52 int handleEvent();
53 void cancelTransaction();
Jerry Zhang94ef0ea2017-07-26 11:37:23 -070054 void doSendEvent(mtp_event me);
Jerry Zhang63dac452017-12-06 15:19:36 -080055 bool openEndpoints(bool ptp);
Jerry Zhangdf69dd32017-05-03 17:17:49 -070056
57 static int getPacketSize(int ffs_fd);
Jerry Zhang487be612016-10-24 12:10:41 -070058
Jerry Zhangdf69dd32017-05-03 17:17:49 -070059 bool mCanceled;
Jerry Zhang487be612016-10-24 12:10:41 -070060
Jerry Zhang487be612016-10-24 12:10:41 -070061 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
Jerry Zhangdf69dd32017-05-03 17:17:49 -070068 aio_context_t mCtx;
Jerry Zhang487be612016-10-24 12:10:41 -070069
Jerry Zhangdf69dd32017-05-03 17:17:49 -070070 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);
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -080084
Jerry Zhang487be612016-10-24 12:10:41 -070085public:
Jerry Zhangdf69dd32017-05-03 17:17:49 -070086 int read(void *data, size_t len) override;
87 int write(const void *data, size_t len) override;
Jerry Zhang487be612016-10-24 12:10:41 -070088
Jerry Zhangdf69dd32017-05-03 17:17:49 -070089 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;
Jerry Zhang487be612016-10-24 12:10:41 -070092
Jerry Zhang63dac452017-12-06 15:19:36 -080093 int start(bool ptp) override;
Jerry Zhangdf69dd32017-05-03 17:17:49 -070094 void close() override;
Jerry Zhang487be612016-10-24 12:10:41 -070095
Jerry Zhang63dac452017-12-06 15:19:36 -080096 bool writeDescriptors(bool ptp);
Jerry Zhang487be612016-10-24 12:10:41 -070097
Jerry Zhang63dac452017-12-06 15:19:36 -080098 MtpFfsHandle(int controlFd);
Jerry Zhang487be612016-10-24 12:10:41 -070099 ~MtpFfsHandle();
100};
101
102struct 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
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700115#endif // _MTP_FFS_HANDLE_H
Jerry Zhang487be612016-10-24 12:10:41 -0700116