am: 82651d413e
Change-Id: I622346740d9310c9c9569ed5feb463f51cab6b5e
gugelfrei
commit
68ce743ef9
@ -1,182 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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.
|
||||
*/
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
#include "AsyncIO.h"
|
||||
|
||||
namespace {
|
||||
|
||||
void read_func(struct aiocb *aiocbp) {
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
void write_func(struct aiocb *aiocbp) {
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
void splice_read_func(struct aiocb *aiocbp) {
|
||||
loff_t long_offset = aiocbp->aio_offset;
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes,
|
||||
&long_offset, aiocbp->aio_sink,
|
||||
NULL, aiocbp->aio_nbytes, 0));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
void splice_write_func(struct aiocb *aiocbp) {
|
||||
loff_t long_offset = aiocbp->aio_offset;
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(splice(aiocbp->aio_fildes, NULL,
|
||||
aiocbp->aio_sink, &long_offset,
|
||||
aiocbp->aio_nbytes, 0));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
std::queue<std::unique_ptr<struct aiocb>> queue;
|
||||
std::mutex queue_lock;
|
||||
std::condition_variable queue_cond;
|
||||
std::condition_variable write_cond;
|
||||
int done = 1;
|
||||
void splice_write_pool_func(int) {
|
||||
while(1) {
|
||||
std::unique_lock<std::mutex> lk(queue_lock);
|
||||
queue_cond.wait(lk, []{return !queue.empty() || done;});
|
||||
if (queue.empty() && done) {
|
||||
return;
|
||||
}
|
||||
std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
|
||||
queue.pop();
|
||||
lk.unlock();
|
||||
write_cond.notify_one();
|
||||
splice_write_func(aiocbp.get());
|
||||
close(aiocbp->aio_fildes);
|
||||
}
|
||||
}
|
||||
|
||||
void write_pool_func(int) {
|
||||
while(1) {
|
||||
std::unique_lock<std::mutex> lk(queue_lock);
|
||||
queue_cond.wait(lk, []{return !queue.empty() || done;});
|
||||
if (queue.empty() && done) {
|
||||
return;
|
||||
}
|
||||
std::unique_ptr<struct aiocb> aiocbp = std::move(queue.front());
|
||||
queue.pop();
|
||||
lk.unlock();
|
||||
write_cond.notify_one();
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
|
||||
aiocbp->aio_pool_buf.get(), aiocbp->aio_nbytes, aiocbp->aio_offset));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr int NUM_THREADS = 1;
|
||||
constexpr int MAX_QUEUE_SIZE = 10;
|
||||
std::thread pool[NUM_THREADS];
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
aiocb::~aiocb() {
|
||||
CHECK(!thread.joinable());
|
||||
}
|
||||
|
||||
void aio_pool_init(void(f)(int)) {
|
||||
CHECK(done == 1);
|
||||
done = 0;
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
pool[i] = std::thread(f, i);
|
||||
}
|
||||
}
|
||||
|
||||
void aio_pool_splice_init() {
|
||||
aio_pool_init(splice_write_pool_func);
|
||||
}
|
||||
|
||||
void aio_pool_write_init() {
|
||||
aio_pool_init(write_pool_func);
|
||||
}
|
||||
|
||||
void aio_pool_end() {
|
||||
done = 1;
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
std::unique_lock<std::mutex> lk(queue_lock);
|
||||
lk.unlock();
|
||||
queue_cond.notify_one();
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
pool[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
// used for both writes and splices depending on which init was used before.
|
||||
int aio_pool_write(struct aiocb *aiocbp) {
|
||||
std::unique_lock<std::mutex> lk(queue_lock);
|
||||
write_cond.wait(lk, []{return queue.size() < MAX_QUEUE_SIZE;});
|
||||
queue.push(std::unique_ptr<struct aiocb>(aiocbp));
|
||||
lk.unlock();
|
||||
queue_cond.notify_one();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_read(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(read_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_write(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(write_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_splice_read(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(splice_read_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_splice_write(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(splice_write_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_error(const struct aiocb *aiocbp) {
|
||||
return aiocbp->error;
|
||||
}
|
||||
|
||||
ssize_t aio_return(struct aiocb *aiocbp) {
|
||||
return aiocbp->ret;
|
||||
}
|
||||
|
||||
int aio_suspend(struct aiocb *aiocbp[], int n,
|
||||
const struct timespec *) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
aiocbp[i]->thread.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_cancel(int, struct aiocb *) {
|
||||
// Not implemented
|
||||
return -1;
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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
|
@ -0,0 +1,342 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <android-base/properties.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/usb/ch9.h>
|
||||
#include <linux/usb/functionfs.h>
|
||||
#include <mutex>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/endian.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "PosixAsyncIO.h"
|
||||
#include "MtpFfsCompatHandle.h"
|
||||
#include "mtp.h"
|
||||
|
||||
#define FUNCTIONFS_ENDPOINT_ALLOC _IOR('g', 231, __u32)
|
||||
|
||||
namespace {
|
||||
|
||||
// Must be divisible by all max packet size values
|
||||
constexpr int MAX_FILE_CHUNK_SIZE = 3145728;
|
||||
|
||||
// Safe values since some devices cannot handle large DMAs
|
||||
// To get good performance, override these with
|
||||
// higher values per device using the properties
|
||||
// sys.usb.ffs.max_read and sys.usb.ffs.max_write
|
||||
constexpr int USB_FFS_MAX_WRITE = MTP_BUFFER_SIZE;
|
||||
constexpr int USB_FFS_MAX_READ = MTP_BUFFER_SIZE;
|
||||
|
||||
static_assert(USB_FFS_MAX_WRITE > 0, "Max r/w values must be > 0!");
|
||||
static_assert(USB_FFS_MAX_READ > 0, "Max r/w values must be > 0!");
|
||||
|
||||
constexpr unsigned int MAX_MTP_FILE_SIZE = 0xFFFFFFFF;
|
||||
|
||||
constexpr size_t ENDPOINT_ALLOC_RETRIES = 10;
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace android {
|
||||
|
||||
MtpFfsCompatHandle::MtpFfsCompatHandle() :
|
||||
mMaxWrite(USB_FFS_MAX_WRITE),
|
||||
mMaxRead(USB_FFS_MAX_READ) {}
|
||||
|
||||
MtpFfsCompatHandle::~MtpFfsCompatHandle() {}
|
||||
|
||||
int MtpFfsCompatHandle::writeHandle(int fd, const void* data, size_t len) {
|
||||
int ret = 0;
|
||||
const char* buf = static_cast<const char*>(data);
|
||||
while (len > 0) {
|
||||
int write_len = std::min(mMaxWrite, len);
|
||||
int n = TEMP_FAILURE_RETRY(::write(fd, buf, write_len));
|
||||
|
||||
if (n < 0) {
|
||||
PLOG(ERROR) << "write ERROR: fd = " << fd << ", n = " << n;
|
||||
return -1;
|
||||
} else if (n < write_len) {
|
||||
errno = EIO;
|
||||
PLOG(ERROR) << "less written than expected";
|
||||
return -1;
|
||||
}
|
||||
buf += n;
|
||||
len -= n;
|
||||
ret += n;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::readHandle(int fd, void* data, size_t len) {
|
||||
int ret = 0;
|
||||
char* buf = static_cast<char*>(data);
|
||||
while (len > 0) {
|
||||
int read_len = std::min(mMaxRead, len);
|
||||
int n = TEMP_FAILURE_RETRY(::read(fd, buf, read_len));
|
||||
if (n < 0) {
|
||||
PLOG(ERROR) << "read ERROR: fd = " << fd << ", n = " << n;
|
||||
return -1;
|
||||
}
|
||||
ret += n;
|
||||
if (n < read_len) // done reading early
|
||||
break;
|
||||
buf += n;
|
||||
len -= n;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::start() {
|
||||
mLock.lock();
|
||||
|
||||
if (!openEndpoints())
|
||||
return -1;
|
||||
|
||||
for (unsigned i = 0; i < NUM_IO_BUFS; i++) {
|
||||
mIobuf[i].bufs.resize(MAX_FILE_CHUNK_SIZE);
|
||||
posix_madvise(mIobuf[i].bufs.data(), MAX_FILE_CHUNK_SIZE,
|
||||
POSIX_MADV_SEQUENTIAL | POSIX_MADV_WILLNEED);
|
||||
}
|
||||
|
||||
// Get device specific r/w size
|
||||
mMaxWrite = android::base::GetIntProperty("sys.usb.ffs.max_write", USB_FFS_MAX_WRITE);
|
||||
mMaxRead = android::base::GetIntProperty("sys.usb.ffs.max_read", USB_FFS_MAX_READ);
|
||||
|
||||
size_t attempts = 0;
|
||||
while (mMaxWrite >= USB_FFS_MAX_WRITE && mMaxRead >= USB_FFS_MAX_READ &&
|
||||
attempts < ENDPOINT_ALLOC_RETRIES) {
|
||||
// If larger contiguous chunks of memory aren't available, attempt to try
|
||||
// smaller allocations.
|
||||
if (ioctl(mBulkIn, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(mMaxWrite)) ||
|
||||
ioctl(mBulkOut, FUNCTIONFS_ENDPOINT_ALLOC, static_cast<__u32>(mMaxRead))) {
|
||||
if (errno == ENODEV) {
|
||||
// Driver hasn't enabled endpoints yet.
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
attempts += 1;
|
||||
continue;
|
||||
}
|
||||
mMaxWrite /= 2;
|
||||
mMaxRead /=2;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// Try to start MtpServer anyway, with the smallest max r/w values
|
||||
mMaxWrite = USB_FFS_MAX_WRITE;
|
||||
mMaxRead = USB_FFS_MAX_READ;
|
||||
PLOG(ERROR) << "Functionfs could not allocate any memory!";
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::read(void* data, size_t len) {
|
||||
return readHandle(mBulkOut, data, len);
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::write(const void* data, size_t len) {
|
||||
return writeHandle(mBulkIn, data, len);
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::receiveFile(mtp_file_range mfr, bool zero_packet) {
|
||||
// When receiving files, the incoming length is given in 32 bits.
|
||||
// A >4G file is given as 0xFFFFFFFF
|
||||
uint32_t file_length = mfr.length;
|
||||
uint64_t offset = mfr.offset;
|
||||
int packet_size = getPacketSize(mBulkOut);
|
||||
|
||||
unsigned char *data = mIobuf[0].bufs.data();
|
||||
unsigned char *data2 = mIobuf[1].bufs.data();
|
||||
|
||||
struct aiocb aio;
|
||||
aio.aio_fildes = mfr.fd;
|
||||
aio.aio_buf = nullptr;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
int ret = -1;
|
||||
size_t length;
|
||||
bool read = false;
|
||||
bool write = false;
|
||||
|
||||
posix_fadvise(mfr.fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
|
||||
|
||||
// Break down the file into pieces that fit in buffers
|
||||
while (file_length > 0 || write) {
|
||||
if (file_length > 0) {
|
||||
length = std::min(static_cast<uint32_t>(MAX_FILE_CHUNK_SIZE), file_length);
|
||||
|
||||
// Read data from USB, handle errors after waiting for write thread.
|
||||
ret = readHandle(mBulkOut, data, length);
|
||||
|
||||
if (file_length != MAX_MTP_FILE_SIZE && ret < static_cast<int>(length)) {
|
||||
ret = -1;
|
||||
errno = EIO;
|
||||
}
|
||||
read = true;
|
||||
}
|
||||
|
||||
if (write) {
|
||||
// get the return status of the last write request
|
||||
aio_suspend(aiol, 1, nullptr);
|
||||
|
||||
int written = aio_return(&aio);
|
||||
if (written == -1) {
|
||||
errno = aio_error(&aio);
|
||||
return -1;
|
||||
}
|
||||
if (static_cast<size_t>(written) < aio.aio_nbytes) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
write = false;
|
||||
}
|
||||
|
||||
// If there was an error reading above
|
||||
if (ret == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (read) {
|
||||
if (file_length == MAX_MTP_FILE_SIZE) {
|
||||
// For larger files, receive until a short packet is received.
|
||||
if (static_cast<size_t>(ret) < length) {
|
||||
file_length = 0;
|
||||
}
|
||||
} else {
|
||||
file_length -= ret;
|
||||
}
|
||||
// Enqueue a new write request
|
||||
aio_prepare(&aio, data, length, offset);
|
||||
aio_write(&aio);
|
||||
|
||||
offset += ret;
|
||||
std::swap(data, data2);
|
||||
|
||||
write = true;
|
||||
read = false;
|
||||
}
|
||||
}
|
||||
// Receive an empty packet if size is a multiple of the endpoint size.
|
||||
if (ret % packet_size == 0 || zero_packet) {
|
||||
if (TEMP_FAILURE_RETRY(::read(mBulkOut, data, packet_size)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MtpFfsCompatHandle::sendFile(mtp_file_range mfr) {
|
||||
uint64_t file_length = mfr.length;
|
||||
uint32_t given_length = std::min(static_cast<uint64_t>(MAX_MTP_FILE_SIZE),
|
||||
file_length + sizeof(mtp_data_header));
|
||||
uint64_t offset = mfr.offset;
|
||||
int packet_size = getPacketSize(mBulkIn);
|
||||
|
||||
// If file_length is larger than a size_t, truncating would produce the wrong comparison.
|
||||
// Instead, promote the left side to 64 bits, then truncate the small result.
|
||||
int init_read_len = std::min(
|
||||
static_cast<uint64_t>(packet_size - sizeof(mtp_data_header)), file_length);
|
||||
|
||||
unsigned char *data = mIobuf[0].bufs.data();
|
||||
unsigned char *data2 = mIobuf[1].bufs.data();
|
||||
|
||||
posix_fadvise(mfr.fd, 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
|
||||
|
||||
struct aiocb aio;
|
||||
aio.aio_fildes = mfr.fd;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
int ret, length;
|
||||
int error = 0;
|
||||
bool read = false;
|
||||
bool write = false;
|
||||
|
||||
// Send the header data
|
||||
mtp_data_header *header = reinterpret_cast<mtp_data_header*>(data);
|
||||
header->length = htole32(given_length);
|
||||
header->type = htole16(2); /* data packet */
|
||||
header->command = htole16(mfr.command);
|
||||
header->transaction_id = htole32(mfr.transaction_id);
|
||||
|
||||
// Some hosts don't support header/data separation even though MTP allows it
|
||||
// Handle by filling first packet with initial file data
|
||||
if (TEMP_FAILURE_RETRY(pread(mfr.fd, reinterpret_cast<char*>(data) +
|
||||
sizeof(mtp_data_header), init_read_len, offset))
|
||||
!= init_read_len) return -1;
|
||||
if (writeHandle(mBulkIn, data, sizeof(mtp_data_header) + init_read_len) == -1) return -1;
|
||||
file_length -= init_read_len;
|
||||
offset += init_read_len;
|
||||
ret = init_read_len + sizeof(mtp_data_header);
|
||||
|
||||
// Break down the file into pieces that fit in buffers
|
||||
while (file_length > 0) {
|
||||
if (read) {
|
||||
// Wait for the previous read to finish
|
||||
aio_suspend(aiol, 1, nullptr);
|
||||
ret = aio_return(&aio);
|
||||
if (ret == -1) {
|
||||
errno = aio_error(&aio);
|
||||
return -1;
|
||||
}
|
||||
if (static_cast<size_t>(ret) < aio.aio_nbytes) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
file_length -= ret;
|
||||
offset += ret;
|
||||
std::swap(data, data2);
|
||||
read = false;
|
||||
write = true;
|
||||
}
|
||||
|
||||
if (error == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (file_length > 0) {
|
||||
length = std::min(static_cast<uint64_t>(MAX_FILE_CHUNK_SIZE), file_length);
|
||||
// Queue up another read
|
||||
aio_prepare(&aio, data, length, offset);
|
||||
aio_read(&aio);
|
||||
read = true;
|
||||
}
|
||||
|
||||
if (write) {
|
||||
if (writeHandle(mBulkIn, data2, ret) == -1) {
|
||||
error = -1;
|
||||
}
|
||||
write = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret % packet_size == 0) {
|
||||
// If the last packet wasn't short, send a final empty packet
|
||||
if (TEMP_FAILURE_RETRY(::write(mBulkIn, data, 0)) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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_FFS_COMPAT_HANDLE_H
|
||||
#define _MTP_FFS_COMPAT_HANDLE_H
|
||||
|
||||
#include <MtpFfsHandle.h>
|
||||
|
||||
namespace android {
|
||||
|
||||
template <class T> class MtpFfsHandleTest;
|
||||
|
||||
class MtpFfsCompatHandle : public MtpFfsHandle {
|
||||
template <class T> friend class android::MtpFfsHandleTest;
|
||||
private:
|
||||
int writeHandle(int fd, const void *data, size_t len);
|
||||
int readHandle(int fd, void *data, size_t len);
|
||||
|
||||
size_t mMaxWrite;
|
||||
size_t mMaxRead;
|
||||
|
||||
public:
|
||||
int read(void* data, size_t len) override;
|
||||
int write(const void* data, size_t len) override;
|
||||
int receiveFile(mtp_file_range mfr, bool zero_packet) override;
|
||||
int sendFile(mtp_file_range mfr) override;
|
||||
|
||||
/**
|
||||
* Open ffs endpoints and allocate necessary kernel and user memory.
|
||||
* Will sleep until endpoints are enabled, for up to 1 second.
|
||||
*/
|
||||
int start() override;
|
||||
|
||||
MtpFfsCompatHandle();
|
||||
~MtpFfsCompatHandle();
|
||||
};
|
||||
|
||||
} // namespace android
|
||||
|
||||
#endif // _MTP_FFS_COMPAT_HANDLE_H
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2016 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.
|
||||
*/
|
||||
|
||||
#include <android-base/logging.h>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "PosixAsyncIO.h"
|
||||
|
||||
namespace {
|
||||
|
||||
void read_func(struct aiocb *aiocbp) {
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(pread(aiocbp->aio_fildes,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
void write_func(struct aiocb *aiocbp) {
|
||||
aiocbp->ret = TEMP_FAILURE_RETRY(pwrite(aiocbp->aio_fildes,
|
||||
aiocbp->aio_buf, aiocbp->aio_nbytes, aiocbp->aio_offset));
|
||||
if (aiocbp->ret == -1) aiocbp->error = errno;
|
||||
}
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
aiocb::~aiocb() {
|
||||
CHECK(!thread.joinable());
|
||||
}
|
||||
|
||||
int aio_read(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(read_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_write(struct aiocb *aiocbp) {
|
||||
aiocbp->thread = std::thread(write_func, aiocbp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int aio_error(const struct aiocb *aiocbp) {
|
||||
return aiocbp->error;
|
||||
}
|
||||
|
||||
ssize_t aio_return(struct aiocb *aiocbp) {
|
||||
return aiocbp->ret;
|
||||
}
|
||||
|
||||
int aio_suspend(struct aiocb *aiocbp[], int n,
|
||||
const struct timespec *) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
aiocbp[i]->thread.join();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void aio_prepare(struct aiocb *aiocbp, void* buf, size_t count, off_t offset) {
|
||||
aiocbp->aio_buf = buf;
|
||||
aiocbp->aio_offset = offset;
|
||||
aiocbp->aio_nbytes = count;
|
||||
}
|
@ -1,192 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
*/
|
||||
#define LOG_TAG "AsyncIO_test.cpp"
|
||||
|
||||
#include <android-base/test_utils.h>
|
||||
#include <fcntl.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include "AsyncIO.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
constexpr int TEST_PACKET_SIZE = 512;
|
||||
constexpr int POOL_COUNT = 10;
|
||||
|
||||
static const std::string dummyDataStr =
|
||||
"/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
|
||||
"der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
|
||||
"e this file except in compliance with the License.\n * You may obtain a c"
|
||||
"opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
|
||||
"-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
|
||||
"oftware\n * distributed under the License is distributed on an \"AS IS\" "
|
||||
"BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
|
||||
"r implied.\n * Se";
|
||||
|
||||
|
||||
class AsyncIOTest : public ::testing::Test {
|
||||
protected:
|
||||
TemporaryFile dummy_file;
|
||||
|
||||
AsyncIOTest() {}
|
||||
~AsyncIOTest() {}
|
||||
};
|
||||
|
||||
TEST_F(AsyncIOTest, testRead) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = dummy_file.fd;
|
||||
aio.aio_buf = buf;
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_read(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testWrite) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = dummy_file.fd;
|
||||
aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_write(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testError) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = -1;
|
||||
aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_write(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), -1);
|
||||
EXPECT_EQ(aio_error(&aio), EBADF);
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testSpliceRead) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
int pipeFd[2];
|
||||
EXPECT_EQ(pipe(pipeFd), 0);
|
||||
EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = dummy_file.fd;
|
||||
aio.aio_sink = pipeFd[1];
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_splice_read(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
|
||||
EXPECT_EQ(read(pipeFd[0], buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testSpliceWrite) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
int pipeFd[2];
|
||||
EXPECT_EQ(pipe(pipeFd), 0);
|
||||
EXPECT_EQ(write(pipeFd[1], dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = pipeFd[0];
|
||||
aio.aio_sink = dummy_file.fd;
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_splice_write(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testPoolWrite) {
|
||||
aio_pool_write_init();
|
||||
char buf[TEST_PACKET_SIZE * POOL_COUNT + 1];
|
||||
buf[TEST_PACKET_SIZE * POOL_COUNT] = '\0';
|
||||
|
||||
for (int i = 0; i < POOL_COUNT; i++) {
|
||||
struct aiocb *aiop = new struct aiocb;
|
||||
aiop->aio_fildes = dummy_file.fd;
|
||||
aiop->aio_pool_buf = std::unique_ptr<char[]>(new char[TEST_PACKET_SIZE]);
|
||||
memcpy(aiop->aio_pool_buf.get(), dummyDataStr.c_str(), TEST_PACKET_SIZE);
|
||||
aiop->aio_offset = i * TEST_PACKET_SIZE;
|
||||
aiop->aio_nbytes = TEST_PACKET_SIZE;
|
||||
EXPECT_EQ(aio_pool_write(aiop), 0);
|
||||
}
|
||||
aio_pool_end();
|
||||
EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE * POOL_COUNT), TEST_PACKET_SIZE * POOL_COUNT);
|
||||
|
||||
std::stringstream ss;
|
||||
for (int i = 0; i < POOL_COUNT; i++)
|
||||
ss << dummyDataStr;
|
||||
|
||||
EXPECT_STREQ(buf, ss.str().c_str());
|
||||
}
|
||||
|
||||
TEST_F(AsyncIOTest, testSplicePoolWrite) {
|
||||
aio_pool_splice_init();
|
||||
char buf[TEST_PACKET_SIZE * POOL_COUNT + 1];
|
||||
buf[TEST_PACKET_SIZE * POOL_COUNT] = '\0';
|
||||
|
||||
for (int i = 0; i < POOL_COUNT; i++) {
|
||||
int pipeFd[2];
|
||||
EXPECT_EQ(pipe(pipeFd), 0);
|
||||
EXPECT_EQ(write(pipeFd[1], dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
struct aiocb *aiop = new struct aiocb;
|
||||
aiop->aio_fildes = pipeFd[0];
|
||||
aiop->aio_sink = dummy_file.fd;
|
||||
aiop->aio_offset = i * TEST_PACKET_SIZE;
|
||||
aiop->aio_nbytes = TEST_PACKET_SIZE;
|
||||
EXPECT_EQ(aio_pool_write(aiop), 0);
|
||||
}
|
||||
aio_pool_end();
|
||||
EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE * POOL_COUNT), TEST_PACKET_SIZE * POOL_COUNT);
|
||||
|
||||
std::stringstream ss;
|
||||
for (int i = 0; i < POOL_COUNT; i++)
|
||||
ss << dummyDataStr;
|
||||
|
||||
EXPECT_STREQ(buf, ss.str().c_str());
|
||||
}
|
||||
|
||||
} // namespace android
|
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2016 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.
|
||||
*/
|
||||
#define LOG_TAG "PosixAsyncIO_test.cpp"
|
||||
|
||||
#include <android-base/test_utils.h>
|
||||
#include <fcntl.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <utils/Log.h>
|
||||
|
||||
#include "PosixAsyncIO.h"
|
||||
|
||||
namespace android {
|
||||
|
||||
constexpr int TEST_PACKET_SIZE = 512;
|
||||
|
||||
static const std::string dummyDataStr =
|
||||
"/*\n * Copyright 2015 The Android Open Source Project\n *\n * Licensed un"
|
||||
"der the Apache License, Version 2.0 (the \"License\");\n * you may not us"
|
||||
"e this file except in compliance with the License.\n * You may obtain a c"
|
||||
"opy of the License at\n *\n * http://www.apache.org/licenses/LICENSE"
|
||||
"-2.0\n *\n * Unless required by applicable law or agreed to in writing, s"
|
||||
"oftware\n * distributed under the License is distributed on an \"AS IS\" "
|
||||
"BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express o"
|
||||
"r implied.\n * Se";
|
||||
|
||||
|
||||
class PosixAsyncIOTest : public ::testing::Test {
|
||||
protected:
|
||||
TemporaryFile dummy_file;
|
||||
|
||||
PosixAsyncIOTest() {}
|
||||
~PosixAsyncIOTest() {}
|
||||
};
|
||||
|
||||
TEST_F(PosixAsyncIOTest, testRead) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
EXPECT_EQ(write(dummy_file.fd, dummyDataStr.c_str(), TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = dummy_file.fd;
|
||||
aio.aio_buf = buf;
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_read(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(PosixAsyncIOTest, testWrite) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = dummy_file.fd;
|
||||
aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_write(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), TEST_PACKET_SIZE);
|
||||
EXPECT_EQ(read(dummy_file.fd, buf, TEST_PACKET_SIZE), TEST_PACKET_SIZE);
|
||||
EXPECT_STREQ(buf, dummyDataStr.c_str());
|
||||
}
|
||||
|
||||
TEST_F(PosixAsyncIOTest, testError) {
|
||||
char buf[TEST_PACKET_SIZE + 1];
|
||||
buf[TEST_PACKET_SIZE] = '\0';
|
||||
struct aiocb aio;
|
||||
struct aiocb *aiol[] = {&aio};
|
||||
aio.aio_fildes = -1;
|
||||
aio.aio_buf = const_cast<char*>(dummyDataStr.c_str());
|
||||
aio.aio_offset = 0;
|
||||
aio.aio_nbytes = TEST_PACKET_SIZE;
|
||||
|
||||
EXPECT_EQ(aio_write(&aio), 0);
|
||||
EXPECT_EQ(aio_suspend(aiol, 1, nullptr), 0);
|
||||
EXPECT_EQ(aio_return(&aio), -1);
|
||||
EXPECT_EQ(aio_error(&aio), EBADF);
|
||||
}
|
||||
|
||||
} // namespace android
|
Loading…
Reference in new issue