• 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 #include <utils/Log.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <cutils/properties.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <linux/usb/ch9.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/endian.h>
30 #include <unistd.h>
31 
32 #include <android-base/logging.h>
33 #include <android-base/unique_fd.h>
34 #include "IMtpHandle.h"
35 
36 constexpr char mtp_dev_path[] = "/dev/mtp_usb";
37 
38 class MtpDevHandle : public IMtpHandle {
39 private:
40     android::base::unique_fd mFd;
41 
42 public:
43     MtpDevHandle();
44     ~MtpDevHandle();
45     int read(void *data, int len);
46     int write(const void *data, int len);
47 
48     int receiveFile(mtp_file_range mfr, bool);
49     int sendFile(mtp_file_range mfr);
50     int sendEvent(mtp_event me);
51 
52     int start();
53     void close();
54 
55     int configure(bool ptp);
56 };
57 
MtpDevHandle()58 MtpDevHandle::MtpDevHandle()
59     : mFd(-1) {};
60 
~MtpDevHandle()61 MtpDevHandle::~MtpDevHandle() {}
62 
read(void * data,int len)63 int MtpDevHandle::read(void *data, int len) {
64     return ::read(mFd, data, len);
65 }
66 
write(const void * data,int len)67 int MtpDevHandle::write(const void *data, int len) {
68     return ::write(mFd, data, len);
69 }
70 
receiveFile(mtp_file_range mfr,bool)71 int MtpDevHandle::receiveFile(mtp_file_range mfr, bool) {
72     return ioctl(mFd, MTP_RECEIVE_FILE, reinterpret_cast<unsigned long>(&mfr));
73 }
74 
sendFile(mtp_file_range mfr)75 int MtpDevHandle::sendFile(mtp_file_range mfr) {
76     return ioctl(mFd, MTP_SEND_FILE_WITH_HEADER, reinterpret_cast<unsigned long>(&mfr));
77 }
78 
sendEvent(mtp_event me)79 int MtpDevHandle::sendEvent(mtp_event me) {
80     return ioctl(mFd, MTP_SEND_EVENT, reinterpret_cast<unsigned long>(&me));
81 }
82 
start()83 int MtpDevHandle::start() {
84     mFd = android::base::unique_fd(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
85     if (mFd == -1) return -1;
86     return 0;
87 }
88 
close()89 void MtpDevHandle::close() {
90     mFd.reset();
91 }
92 
configure(bool)93 int MtpDevHandle::configure(bool) {
94     // Nothing to do, driver can configure itself
95     return 0;
96 }
97 
get_mtp_handle()98 IMtpHandle *get_mtp_handle() {
99     return new MtpDevHandle();
100 }
101