Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df69dd33 authored by Jerry Zhang's avatar Jerry Zhang
Browse files

Switch MtpFfsHandle to aio, add control functions.

MtpFfsHandle now uses kaio to handle usb data.
This achieves better performance without using
the endpoint alloc ioctl.

This also allows ep0 events to be handled without
race conditions. Events will also include control
requests, which will allow both host and device
initiated cancellation.

Bug: 37916658
Bug: 36802721
Test: Transfer various size files, run MtpFfsHandleTest
Test: Cancel transfer on Windows
Test: Allow device to cancel transfer on Windows
Change-Id: Ib3ce996f00782ce7f68f29b2510dbc17f09fdf14
parent 0fdeb09e
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -17,13 +17,13 @@
cc_library_shared {
    name: "libmtp",
    srcs: [
        "AsyncIO.cpp",
        "MtpDataPacket.cpp",
        "MtpDebug.cpp",
        "MtpDevHandle.cpp",
        "MtpDevice.cpp",
        "MtpDeviceInfo.cpp",
        "MtpEventPacket.cpp",
        "MtpFfsCompatHandle.cpp",
        "MtpFfsHandle.cpp",
        "MtpObjectInfo.cpp",
        "MtpPacket.cpp",
@@ -35,6 +35,7 @@ cc_library_shared {
        "MtpStorageInfo.cpp",
        "MtpStringBuffer.cpp",
        "MtpUtils.cpp",
        "PosixAsyncIO.cpp",
    ],
    export_include_dirs: ["."],
    cflags: [
@@ -45,6 +46,7 @@ cc_library_shared {
        "-Werror",
    ],
    shared_libs: [
        "libasyncio",
        "libbase",
        "libutils",
        "liblog",
+4 −5
Original line number Diff line number Diff line
@@ -18,13 +18,13 @@

#include <linux/usb/f_mtp.h>

constexpr char FFS_MTP_EP0[] = "/dev/usb-ffs/mtp/ep0";
namespace android {

class IMtpHandle {
public:
    // Return number of bytes read/written, or -1 and errno is set
    virtual int read(void *data, int len) = 0;
    virtual int write(const void *data, int len) = 0;
    virtual int read(void *data, size_t len) = 0;
    virtual int write(const void *data, size_t len) = 0;

    // Return 0 if send/receive is successful, or -1 and errno is set
    virtual int receiveFile(mtp_file_range mfr, bool zero_packet) = 0;
@@ -40,8 +40,7 @@ public:
    virtual ~IMtpHandle() {}
};

IMtpHandle *get_ffs_handle();
IMtpHandle *get_mtp_handle();
}

#endif // _IMTP_HANDLE_H
+1 −1
Original line number Diff line number Diff line
@@ -20,12 +20,12 @@
#include "MtpPacket.h"
#include "mtp.h"

class IMtpHandle;
struct usb_device;
struct usb_request;

namespace android {

class IMtpHandle;
class MtpStringBuffer;

class MtpDataPacket : public MtpPacket {
+10 −32
Original line number Diff line number Diff line
@@ -14,57 +14,37 @@
 * limitations under the License.
 */

#include <utils/Log.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <android-base/logging.h>
#include <cutils/properties.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/usb/ch9.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/endian.h>
#include <unistd.h>

#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include "IMtpHandle.h"
#include "MtpDevHandle.h"

constexpr char mtp_dev_path[] = "/dev/mtp_usb";
namespace android {

class MtpDevHandle : public IMtpHandle {
private:
    android::base::unique_fd mFd;

public:
    MtpDevHandle();
    ~MtpDevHandle();
    int read(void *data, int len);
    int write(const void *data, int len);

    int receiveFile(mtp_file_range mfr, bool);
    int sendFile(mtp_file_range mfr);
    int sendEvent(mtp_event me);

    int start();
    void close();

    int configure(bool ptp);
};
constexpr char mtp_dev_path[] = "/dev/mtp_usb";

MtpDevHandle::MtpDevHandle()
    : mFd(-1) {};

MtpDevHandle::~MtpDevHandle() {}

int MtpDevHandle::read(void *data, int len) {
int MtpDevHandle::read(void *data, size_t len) {
    return ::read(mFd, data, len);
}

int MtpDevHandle::write(const void *data, int len) {
int MtpDevHandle::write(const void *data, size_t len) {
    return ::write(mFd, data, len);
}

@@ -81,7 +61,7 @@ int MtpDevHandle::sendEvent(mtp_event me) {
}

int MtpDevHandle::start() {
    mFd = android::base::unique_fd(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
    mFd.reset(TEMP_FAILURE_RETRY(open(mtp_dev_path, O_RDWR)));
    if (mFd == -1) return -1;
    return 0;
}
@@ -95,6 +75,4 @@ int MtpDevHandle::configure(bool) {
    return 0;
}

IMtpHandle *get_mtp_handle() {
    return new MtpDevHandle();
}
} // namespace android
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _MTP_DEV_HANDLE_H
#define _MTP_DEV_HANDLE_H

#include <android-base/unique_fd.h>
#include "IMtpHandle.h"

namespace android {

class MtpDevHandle : public IMtpHandle {
private:
    android::base::unique_fd mFd;

public:
    MtpDevHandle();
    ~MtpDevHandle();
    int read(void *data, size_t len);
    int write(const void *data, size_t len);

    int receiveFile(mtp_file_range mfr, bool);
    int sendFile(mtp_file_range mfr);
    int sendEvent(mtp_event me);

    int start();
    void close();

    int configure(bool ptp);
};

} // namespace android

#endif // _MTP_FFS_HANDLE_H
Loading