• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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();
36     void doSendEvent(mtp_event me);
37 
38     void controlLoop();
39     int handleEvent();
40     int handleControlRequest(const struct usb_ctrlrequest *setup);
41 
42     bool mPtp;
43 
44     std::timed_mutex mLock;
45 
46     android::base::unique_fd mControl;
47     // "in" from the host's perspective => sink for mtp server
48     android::base::unique_fd mBulkIn;
49     // "out" from the host's perspective => source for mtp server
50     android::base::unique_fd mBulkOut;
51     android::base::unique_fd mIntr;
52 
53     int mMaxWrite;
54     int mMaxRead;
55 
56     std::vector<char> mBuffer1;
57     std::vector<char> mBuffer2;
58 
59 public:
60     int read(void *data, int len);
61     int write(const void *data, int len);
62 
63     int receiveFile(mtp_file_range mfr, bool zero_packet);
64     int sendFile(mtp_file_range mfr);
65     int sendEvent(mtp_event me);
66 
67     /**
68      * Open ffs endpoints and allocate necessary kernel and user memory.
69      * Will sleep until endpoints are enabled, for up to 1 second.
70      */
71     int start();
72     void close();
73 
74     int configure(bool ptp);
75 
76     MtpFfsHandle();
77     ~MtpFfsHandle();
78 };
79 
80 struct mtp_data_header {
81     /* length of packet, including this header */
82     __le32 length;
83     /* container type (2 for data packet) */
84     __le16 type;
85     /* MTP command code */
86     __le16 command;
87     /* MTP transaction ID */
88     __le32 transaction_id;
89 };
90 
91 } // namespace android
92 
93 #endif // _MTP_FF_HANDLE_H
94 
95