blob: d6a8b820de8dc9a8bdd05ad258e3c305abb353fa [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
36MtpDevHandle::MtpDevHandle()
37 : mFd(-1) {};
38
39MtpDevHandle::~MtpDevHandle() {}
40
41int MtpDevHandle::read(void *data, size_t len) {
42 return ::read(mFd, data, len);
43}
44
45int MtpDevHandle::write(const void *data, size_t len) {
46 return ::write(mFd, data, len);
47}
48
49int MtpDevHandle::receiveFile(mtp_file_range mfr, bool) {
50 return ioctl(mFd, MTP_RECEIVE_FILE, reinterpret_cast<unsigned long>(&mfr));
51}
52
53int MtpDevHandle::sendFile(mtp_file_range mfr) {
54 return ioctl(mFd, MTP_SEND_FILE_WITH_HEADER, reinterpret_cast<unsigned long>(&mfr));
55}
56
57int MtpDevHandle::sendEvent(mtp_event me) {
58 return ioctl(mFd, MTP_SEND_EVENT, reinterpret_cast<unsigned long>(&me));
59}
60
61int MtpDevHandle::start(bool /* ptp */) {
62 mFd.reset(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
63 if (mFd == -1) return -1;
64 return 0;
65}
66
67void MtpDevHandle::close() {
68 mFd.reset();
69}
70
71bool MtpDevHandle::writeDescriptors(bool usePtp) { return usePtp; }