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 <android-base/logging.h>
18 #include <cutils/properties.h>
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <linux/usb/ch9.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/endian.h>
30 #include <unistd.h>
31
32 #include "MtpDevHandle.h"
33
34 namespace android {
35
36 constexpr char mtp_dev_path[] = "/dev/mtp_usb";
37
MtpDevHandle()38 MtpDevHandle::MtpDevHandle()
39 : mFd(-1) {};
40
~MtpDevHandle()41 MtpDevHandle::~MtpDevHandle() {}
42
read(void * data,size_t len)43 int MtpDevHandle::read(void *data, size_t len) {
44 return ::read(mFd, data, len);
45 }
46
write(const void * data,size_t len)47 int MtpDevHandle::write(const void *data, size_t len) {
48 return ::write(mFd, data, len);
49 }
50
receiveFile(mtp_file_range mfr,bool)51 int MtpDevHandle::receiveFile(mtp_file_range mfr, bool) {
52 return ioctl(mFd, MTP_RECEIVE_FILE, reinterpret_cast<unsigned long>(&mfr));
53 }
54
sendFile(mtp_file_range mfr)55 int MtpDevHandle::sendFile(mtp_file_range mfr) {
56 return ioctl(mFd, MTP_SEND_FILE_WITH_HEADER, reinterpret_cast<unsigned long>(&mfr));
57 }
58
sendEvent(mtp_event me)59 int MtpDevHandle::sendEvent(mtp_event me) {
60 return ioctl(mFd, MTP_SEND_EVENT, reinterpret_cast<unsigned long>(&me));
61 }
62
start(bool)63 int MtpDevHandle::start(bool /* ptp */) {
64 mFd.reset(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
65 if (mFd == -1) return -1;
66 return 0;
67 }
68
close()69 void MtpDevHandle::close() {
70 mFd.reset();
71 }
72
73 } // namespace android
74