bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 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 | constexpr char mtp_dev_path[] = "/dev/mtp_usb"; |
| 35 | |
dianlujitao | 95244dd | 2019-03-26 11:07:51 +0800 | [diff] [blame] | 36 | MtpDevHandle::MtpDevHandle(int controlFd) { |
| 37 | mFd.reset(controlFd); |
| 38 | } |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 39 | |
| 40 | MtpDevHandle::~MtpDevHandle() {} |
| 41 | |
| 42 | int MtpDevHandle::read(void *data, size_t len) { |
| 43 | return ::read(mFd, data, len); |
| 44 | } |
| 45 | |
| 46 | int MtpDevHandle::write(const void *data, size_t len) { |
| 47 | return ::write(mFd, data, len); |
| 48 | } |
| 49 | |
| 50 | int MtpDevHandle::receiveFile(mtp_file_range mfr, bool) { |
| 51 | return ioctl(mFd, MTP_RECEIVE_FILE, reinterpret_cast<unsigned long>(&mfr)); |
| 52 | } |
| 53 | |
| 54 | int MtpDevHandle::sendFile(mtp_file_range mfr) { |
| 55 | return ioctl(mFd, MTP_SEND_FILE_WITH_HEADER, reinterpret_cast<unsigned long>(&mfr)); |
| 56 | } |
| 57 | |
| 58 | int MtpDevHandle::sendEvent(mtp_event me) { |
| 59 | return ioctl(mFd, MTP_SEND_EVENT, reinterpret_cast<unsigned long>(&me)); |
| 60 | } |
| 61 | |
| 62 | int 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 | |
| 68 | void MtpDevHandle::close() { |
| 69 | mFd.reset(); |
| 70 | } |
| 71 | |
| 72 | bool MtpDevHandle::writeDescriptors(bool usePtp) { return usePtp; } |