bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 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/unique_fd.h> |
| 21 | #include <linux/aio_abi.h> |
| 22 | #include <mutex> |
| 23 | #include <sys/poll.h> |
| 24 | #include <time.h> |
| 25 | #include <thread> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <IMtpHandle.h> |
| 29 | |
| 30 | constexpr int NUM_IO_BUFS = 2; |
| 31 | |
| 32 | struct io_buffer { |
| 33 | std::vector<struct iocb> iocbs; // Holds memory for all iocbs. Not used directly. |
| 34 | std::vector<struct iocb*> iocb; // Pointers to individual iocbs, for syscalls |
| 35 | std::vector<unsigned char> bufs; // A large buffer, used with filesystem io |
| 36 | std::vector<unsigned char*> buf; // Pointers within the larger buffer, for syscalls |
| 37 | unsigned actual; // The number of buffers submitted for this request |
| 38 | }; |
| 39 | |
| 40 | template <class T> class MtpFfsHandleTest; |
| 41 | |
| 42 | class MtpFfsHandle : public IMtpHandle { |
| 43 | template <class T> friend class MtpFfsHandleTest; |
| 44 | protected: |
| 45 | void closeConfig(); |
| 46 | void closeEndpoints(); |
| 47 | void advise(int fd); |
| 48 | int handleControlRequest(const struct usb_ctrlrequest *request); |
| 49 | int doAsync(void* data, size_t len, bool read, bool zero_packet); |
| 50 | int handleEvent(); |
| 51 | void cancelTransaction(); |
| 52 | void doSendEvent(mtp_event me); |
| 53 | bool openEndpoints(bool ptp); |
| 54 | |
| 55 | static int getPacketSize(int ffs_fd); |
| 56 | |
| 57 | bool mCanceled; |
| 58 | |
| 59 | android::base::unique_fd mControl; |
| 60 | // "in" from the host's perspective => sink for mtp server |
| 61 | android::base::unique_fd mBulkIn; |
| 62 | // "out" from the host's perspective => source for mtp server |
| 63 | android::base::unique_fd mBulkOut; |
| 64 | android::base::unique_fd mIntr; |
| 65 | |
| 66 | aio_context_t mCtx; |
| 67 | |
| 68 | android::base::unique_fd mEventFd; |
| 69 | struct pollfd mPollFds[2]; |
| 70 | |
| 71 | struct io_buffer mIobuf[NUM_IO_BUFS]; |
| 72 | |
| 73 | // Submit an io request of given length. Return amount submitted or -1. |
| 74 | int iobufSubmit(struct io_buffer *buf, int fd, unsigned length, bool read); |
| 75 | |
| 76 | // Cancel submitted requests from start to end in the given array. Return 0 or -1. |
| 77 | int cancelEvents(struct iocb **iocb, struct io_event *events, unsigned start, unsigned end); |
| 78 | |
| 79 | // Wait for at minimum the given number of events. Returns the amount of data in the returned |
| 80 | // events. Increments counter by the number of events returned. |
| 81 | int waitEvents(struct io_buffer *buf, int min_events, struct io_event *events, int *counter); |
| 82 | |
| 83 | public: |
| 84 | int read(void *data, size_t len) override; |
| 85 | int write(const void *data, size_t len) override; |
| 86 | |
| 87 | int receiveFile(mtp_file_range mfr, bool zero_packet) override; |
| 88 | int sendFile(mtp_file_range mfr) override; |
| 89 | int sendEvent(mtp_event me) override; |
| 90 | |
| 91 | int start(bool ptp) override; |
| 92 | void close() override; |
| 93 | |
| 94 | bool writeDescriptors(bool ptp); |
| 95 | |
| 96 | MtpFfsHandle(int controlFd); |
| 97 | ~MtpFfsHandle(); |
| 98 | }; |
| 99 | |
| 100 | struct mtp_data_header { |
| 101 | /* length of packet, including this header */ |
| 102 | __le32 length; |
| 103 | /* container type (2 for data packet) */ |
| 104 | __le16 type; |
| 105 | /* MTP command code */ |
| 106 | __le16 command; |
| 107 | /* MTP transaction ID */ |
| 108 | __le32 transaction_id; |
| 109 | }; |
| 110 | |
| 111 | #endif // _MTP_FFS_HANDLE_H |
| 112 | |