blob: 2f90bd1c80724271ccc44efbc236aba5de68b628 [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 Zhang487be612016-10-24 12:10:41 -070045
46class MtpFfsHandle : public IMtpHandle {
Jerry Zhangdf69dd32017-05-03 17:17:49 -070047 template <class T> friend class android::MtpFfsHandleTest;
48protected:
Jerry Zhang487be612016-10-24 12:10:41 -070049 bool initFunctionfs();
50 void closeConfig();
51 void closeEndpoints();
Jerry Zhangdf69dd32017-05-03 17:17:49 -070052 void advise(int fd);
53 int handleControlRequest(const struct usb_ctrlrequest *request);
54 int doAsync(void* data, size_t len, bool read);
55 int handleEvent();
56 void cancelTransaction();
Jerry Zhang94ef0ea2017-07-26 11:37:23 -070057 void doSendEvent(mtp_event me);
Jerry Zhangdf69dd32017-05-03 17:17:49 -070058 bool openEndpoints();
59
60 static int getPacketSize(int ffs_fd);
Jerry Zhang487be612016-10-24 12:10:41 -070061
62 bool mPtp;
Jerry Zhangdf69dd32017-05-03 17:17:49 -070063 bool mCanceled;
Jerry Zhang487be612016-10-24 12:10:41 -070064
Jerry Zhangdf69dd32017-05-03 17:17:49 -070065 std::timed_mutex mLock; // protects configure() vs main loop
Jerry Zhang487be612016-10-24 12:10:41 -070066
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
Jerry Zhangdf69dd32017-05-03 17:17:49 -070074 aio_context_t mCtx;
Jerry Zhang487be612016-10-24 12:10:41 -070075
Jerry Zhangdf69dd32017-05-03 17:17:49 -070076 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
87 // Wait for at minimum the given number of events. Returns the amount of data in the returned
88 // events. Increments counter by the number of events returned.
89 int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter);
Jerry Zhangcc9d0fd2017-01-27 10:29:59 -080090
Jerry Zhang487be612016-10-24 12:10:41 -070091public:
Jerry Zhangdf69dd32017-05-03 17:17:49 -070092 int read(void *data, size_t len) override;
93 int write(const void *data, size_t len) override;
Jerry Zhang487be612016-10-24 12:10:41 -070094
Jerry Zhangdf69dd32017-05-03 17:17:49 -070095 int receiveFile(mtp_file_range mfr, bool zero_packet) override;
96 int sendFile(mtp_file_range mfr) override;
97 int sendEvent(mtp_event me) override;
Jerry Zhang487be612016-10-24 12:10:41 -070098
Jerry Zhangdf69dd32017-05-03 17:17:49 -070099 int start() override;
100 void close() override;
Jerry Zhang487be612016-10-24 12:10:41 -0700101
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700102 int configure(bool ptp) override;
Jerry Zhang487be612016-10-24 12:10:41 -0700103
104 MtpFfsHandle();
105 ~MtpFfsHandle();
106};
107
108struct mtp_data_header {
109 /* length of packet, including this header */
110 __le32 length;
111 /* container type (2 for data packet) */
112 __le16 type;
113 /* MTP command code */
114 __le16 command;
115 /* MTP transaction ID */
116 __le32 transaction_id;
117};
118
119} // namespace android
120
Jerry Zhangdf69dd32017-05-03 17:17:49 -0700121#endif // _MTP_FFS_HANDLE_H
Jerry Zhang487be612016-10-24 12:10:41 -0700122