blob: e22ba55b2f7004a2aa660aaf0753a72fdcc3329f [file] [log] [blame]
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05001/*
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
34constexpr char mtp_dev_path[] = "/dev/mtp_usb";
35
dianlujitao95244dd2019-03-26 11:07:51 +080036MtpDevHandle::MtpDevHandle(int controlFd) {
37 mFd.reset(controlFd);
38}
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050039
40MtpDevHandle::~MtpDevHandle() {}
41
42int MtpDevHandle::read(void *data, size_t len) {
43 return ::read(mFd, data, len);
44}
45
46int MtpDevHandle::write(const void *data, size_t len) {
47 return ::write(mFd, data, len);
48}
49
50int MtpDevHandle::receiveFile(mtp_file_range mfr, bool) {
51 return ioctl(mFd, MTP_RECEIVE_FILE, reinterpret_cast<unsigned long>(&mfr));
52}
53
54int MtpDevHandle::sendFile(mtp_file_range mfr) {
55 return ioctl(mFd, MTP_SEND_FILE_WITH_HEADER, reinterpret_cast<unsigned long>(&mfr));
56}
57
58int MtpDevHandle::sendEvent(mtp_event me) {
59 return ioctl(mFd, MTP_SEND_EVENT, reinterpret_cast<unsigned long>(&me));
60}
61
62int MtpDevHandle::start(bool /* ptp */) {
63 mFd.reset(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
64 if (mFd == -1) return -1;
65 return 0;
66}
67
68void MtpDevHandle::close() {
69 mFd.reset();
70}
71
72bool MtpDevHandle::writeDescriptors(bool usePtp) { return usePtp; }