MtpDevHandle.cpp 1.9 KB

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