blob: 8c3725ec3071f8735a1c08770fa11ea8eaf53dd7 [file] [log] [blame]
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05001/*
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
Ray Chif5aa84d2021-06-01 16:40:33 +080020#include <android-base/properties.h>
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050021#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
31constexpr int NUM_IO_BUFS = 2;
32
33struct io_buffer {
34 std::vector<struct iocb> iocbs; // Holds memory for all iocbs. Not used directly.
35 std::vector<struct iocb*> iocb; // Pointers to individual iocbs, for syscalls
36 std::vector<unsigned char> bufs; // A large buffer, used with filesystem io
37 std::vector<unsigned char*> buf; // Pointers within the larger buffer, for syscalls
38 unsigned actual; // The number of buffers submitted for this request
39};
40
41template <class T> class MtpFfsHandleTest;
42
43class MtpFfsHandle : public IMtpHandle {
44 template <class T> friend class MtpFfsHandleTest;
45protected:
46 void closeConfig();
47 void closeEndpoints();
48 void advise(int fd);
49 int handleControlRequest(const struct usb_ctrlrequest *request);
50 int doAsync(void* data, size_t len, bool read, bool zero_packet);
51 int handleEvent();
52 void cancelTransaction();
53 void doSendEvent(mtp_event me);
54 bool openEndpoints(bool ptp);
55
56 static int getPacketSize(int ffs_fd);
57
58 bool mCanceled;
Ray Chif5aa84d2021-06-01 16:40:33 +080059 bool mBatchCancel;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050060
61 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
68 aio_context_t mCtx;
69
70 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.
Ray Chif5aa84d2021-06-01 16:40:33 +080079 int cancelEvents(struct iocb **iocb, struct io_event *events, unsigned start, unsigned end,
80 bool is_batch_cancel);
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050081
82 // Wait for at minimum the given number of events. Returns the amount of data in the returned
83 // events. Increments counter by the number of events returned.
84 int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter);
85
86public:
87 int read(void *data, size_t len) override;
88 int write(const void *data, size_t len) override;
89
90 int receiveFile(mtp_file_range mfr, bool zero_packet) override;
91 int sendFile(mtp_file_range mfr) override;
92 int sendEvent(mtp_event me) override;
93
94 int start(bool ptp) override;
95 void close() override;
96
97 bool writeDescriptors(bool ptp);
98
99 MtpFfsHandle(int controlFd);
100 ~MtpFfsHandle();
101};
102
103struct mtp_data_header {
104 /* length of packet, including this header */
105 __le32 length;
106 /* container type (2 for data packet) */
107 __le16 type;
108 /* MTP command code */
109 __le16 command;
110 /* MTP transaction ID */
111 __le32 transaction_id;
112};
113
114#endif // _MTP_FFS_HANDLE_H
115