Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [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 <IMtpHandle.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | class MtpFfsHandleTest; |
| 26 | |
| 27 | class MtpFfsHandle : public IMtpHandle { |
| 28 | friend class android::MtpFfsHandleTest; |
| 29 | private: |
| 30 | int writeHandle(int fd, const void *data, int len); |
| 31 | int readHandle(int fd, void *data, int len); |
| 32 | int spliceReadHandle(int fd, int fd_out, int len); |
| 33 | bool initFunctionfs(); |
| 34 | void closeConfig(); |
| 35 | void closeEndpoints(); |
Jerry Zhang | 94ef0ea | 2017-07-26 11:37:23 -0700 | [diff] [blame^] | 36 | void doSendEvent(mtp_event me); |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 37 | |
| 38 | bool mPtp; |
| 39 | |
Jerry Zhang | 0475d91 | 2017-04-03 11:24:48 -0700 | [diff] [blame] | 40 | std::timed_mutex mLock; |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 41 | |
| 42 | android::base::unique_fd mControl; |
| 43 | // "in" from the host's perspective => sink for mtp server |
| 44 | android::base::unique_fd mBulkIn; |
| 45 | // "out" from the host's perspective => source for mtp server |
| 46 | android::base::unique_fd mBulkOut; |
| 47 | android::base::unique_fd mIntr; |
| 48 | |
| 49 | int mMaxWrite; |
| 50 | int mMaxRead; |
| 51 | |
Jerry Zhang | cc9d0fd | 2017-01-27 10:29:59 -0800 | [diff] [blame] | 52 | std::vector<char> mBuffer1; |
| 53 | std::vector<char> mBuffer2; |
| 54 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 55 | public: |
| 56 | int read(void *data, int len); |
| 57 | int write(const void *data, int len); |
| 58 | |
Jerry Zhang | 5410756 | 2017-05-15 11:54:19 -0700 | [diff] [blame] | 59 | int receiveFile(mtp_file_range mfr, bool zero_packet); |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 60 | int sendFile(mtp_file_range mfr); |
| 61 | int sendEvent(mtp_event me); |
| 62 | |
Jerry Zhang | b4f5426 | 2017-02-02 18:14:33 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Open ffs endpoints and allocate necessary kernel and user memory. |
| 65 | * Will sleep until endpoints are enabled, for up to 1 second. |
| 66 | */ |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 67 | int start(); |
| 68 | void close(); |
| 69 | |
| 70 | int configure(bool ptp); |
| 71 | |
| 72 | MtpFfsHandle(); |
| 73 | ~MtpFfsHandle(); |
| 74 | }; |
| 75 | |
| 76 | struct mtp_data_header { |
| 77 | /* length of packet, including this header */ |
| 78 | __le32 length; |
| 79 | /* container type (2 for data packet) */ |
| 80 | __le16 type; |
| 81 | /* MTP command code */ |
| 82 | __le16 command; |
| 83 | /* MTP transaction ID */ |
| 84 | __le32 transaction_id; |
| 85 | }; |
| 86 | |
| 87 | } // namespace android |
| 88 | |
| 89 | #endif // _MTP_FF_HANDLE_H |
| 90 | |