blob: 2347000b5f5afcfe1edec0fa67e4e05686b85b94 [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 char FFS_MTP_EP0[] = "/dev/usb-ffs/mtp/ep0";
33
34constexpr int NUM_IO_BUFS = 2;
35
36struct io_buffer {
37 std::vector<struct iocb> iocbs; // Holds memory for all iocbs. Not used directly.
38 std::vector<struct iocb*> iocb; // Pointers to individual iocbs, for syscalls
39 std::vector<unsigned char> bufs; // A large buffer, used with filesystem io
40 std::vector<unsigned char*> buf; // Pointers within the larger buffer, for syscalls
41 unsigned actual; // The number of buffers submitted for this request
42};
43
44template <class T> class MtpFfsHandleTest;
Jerry Zhang69b74502017-10-02 16:26:37 -070045template <class T> class MtpFfsHandleTest_testControl_Test;
Jerry Zhang487be612016-10-24 12:10:41 -070046
47class MtpFfsHandle : public IMtpHandle {
Jerry Zhang69b74502017-10-02 16:26:37 -070048 template <class T> friend class MtpFfsHandleTest;
49 template <class T> friend class MtpFfsHandleTest_testControl_Test;
Jerry Zhangdf69dd32017-05-03 17:17:49 -070050protected:
Jerry Zhang487be612016-10-24 12:10:41 -070051 bool initFunctionfs();
Jerry Zhang69b74502017-10-02 16:26:37 -070052 bool writeDescriptors();
Jerry Zhang487be612016-10-24 12:10:41 -070053 void closeConfig();
54 void closeEndpoints();
Jerry Zhangdf69dd32017-05-03 17:17:49 -070055 void advise(int fd);
56 int handleControlRequest(const struct usb_ctrlrequest *request);
57 int doAsync(void* data, size_t len, bool read);
58 int handleEvent();
59 void cancelTransaction();
Jerry Zhang94ef0ea2017-07-26 11:37:23 -070060 void doSendEvent(mtp_event me);
Jerry Zhangdf69dd32017-05-03 17:17:49 -070061 bool openEndpoints();
62
63 static int getPacketSize(int ffs_fd);
Jerry Zhang487be612016-10-24 12:10:41 -070064
65 bool mPtp;
Jerry Zhangdf69dd32017-05-03 17:17:49 -070066 bool mCanceled;
Jerry Zhang487be612016-10-24 12:10:41 -070067
Jerry Zhangdf69dd32017-05-03 17:17:49 -070068 std::timed_mutex mLock; // protects configure() vs main loop
Jerry Zhang487be612016-10-24 12:10:41 -070069
70 android::base::unique_fd mControl;
71 // "in" from the host's perspective => sink for mtp server
72 android::base::unique_fd mBulkIn;
73 // "out" from the host's perspective => source for mtp server
74 android::base::unique_fd mBulkOut;
75 android::base::unique_fd mIntr;
76
Jerry Zhangdf69dd32017-05-03 17:17:49 -070077 aio_context_t mCtx;
Jerry Zhang487be612016-10-24 12:10:41 -070078
Jerry Zhangdf69dd32017-05-03 17:17:49 -070079 android::base::unique_fd mEventFd;
80 struct pollfd mPollFds[2];
81
82 struct io_buffer mIobuf[NUM_IO_BUFS];
83
84 // Submit an io request of given length. Return amount submitted or -1.
85 int iobufSubmit(struct io_buffer *buf, int fd, unsigned length, bool read);
86
87 // Cancel submitted requests from start to end in the given array. Return 0 or -1.
88 int cancelEvents(struct iocb **iocb, struct io_event *events, unsigned start, unsigned end);
89
90 // Wait for at minimum the given number of events. Returns the amount of data in the returned
91 // events. Increments counter by the number of events returned.
92 int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter);
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -080093
Jerry Zhang487be612016-10-24 12:10:41 -070094public:
Jerry Zhangdf69dd32017-05-03 17:17:49 -070095 int read(void *data, size_t len) override;
96 int write(const void *data, size_t len) override;
Jerry Zhang487be612016-10-24 12:10:41 -070097
Jerry Zhangdf69dd32017-05-03 17:17:49 -070098 int receiveFile(mtp_file_range mfr, bool zero_packet) override;
99 int sendFile(mtp_file_range mfr) override;
100 int sendEvent(mtp_event me) override;
Jerry Zhang487be612016-10-24 12:10:41 -0700101
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700102 int start() override;
103 void close() override;
Jerry Zhang487be612016-10-24 12:10:41 -0700104
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700105 int configure(bool ptp) override;
Jerry Zhang487be612016-10-24 12:10:41 -0700106
107 MtpFfsHandle();
108 ~MtpFfsHandle();
109};
110
111struct mtp_data_header {
112 /* length of packet, including this header */
113 __le32 length;
114 /* container type (2 for data packet) */
115 __le16 type;
116 /* MTP command code */
117 __le16 command;
118 /* MTP transaction ID */
119 __le32 transaction_id;
120};
121
122} // namespace android
123
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700124#endif // _MTP_FFS_HANDLE_H
Jerry Zhang487be612016-10-24 12:10:41 -0700125