Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 17 | #define LOG_TAG "MtpRequestPacket" |
| 18 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
| 22 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame^] | 23 | #include "IMtpHandle.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 24 | #include "MtpRequestPacket.h" |
| 25 | |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 26 | #include <usbhost/usbhost.h> |
| 27 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 30 | MtpRequestPacket::MtpRequestPacket() |
Mike Lockwood | ab06384 | 2014-11-12 14:20:06 -0800 | [diff] [blame] | 31 | : MtpPacket(512), |
| 32 | mParameterCount(0) |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | MtpRequestPacket::~MtpRequestPacket() { |
| 37 | } |
| 38 | |
| 39 | #ifdef MTP_DEVICE |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame^] | 40 | int MtpRequestPacket::read(IMtpHandle *h) { |
| 41 | int ret = h->read(mBuffer, mBufferSize); |
Mike Lockwood | ab06384 | 2014-11-12 14:20:06 -0800 | [diff] [blame] | 42 | if (ret < 0) { |
| 43 | // file read error |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | // request packet should have 12 byte header followed by 0 to 5 32-bit arguments |
Daichi Hirono | b3be006 | 2016-02-25 12:42:58 +0900 | [diff] [blame] | 48 | const size_t read_size = static_cast<size_t>(ret); |
| 49 | if (read_size >= MTP_CONTAINER_HEADER_SIZE |
| 50 | && read_size <= MTP_CONTAINER_HEADER_SIZE + 5 * sizeof(uint32_t) |
| 51 | && ((read_size - MTP_CONTAINER_HEADER_SIZE) & 3) == 0) { |
| 52 | mPacketSize = read_size; |
| 53 | mParameterCount = (read_size - MTP_CONTAINER_HEADER_SIZE) / sizeof(uint32_t); |
Mike Lockwood | ab06384 | 2014-11-12 14:20:06 -0800 | [diff] [blame] | 54 | } else { |
| 55 | ALOGE("Malformed MTP request packet"); |
| 56 | ret = -1; |
| 57 | } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #ifdef MTP_HOST |
| 63 | // write our buffer to the given endpoint (host mode) |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 64 | int MtpRequestPacket::write(struct usb_request *request) |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 65 | { |
| 66 | putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 67 | putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_COMMAND); |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 68 | request->buffer = mBuffer; |
| 69 | request->buffer_length = mPacketSize; |
| 70 | return transfer(request); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 71 | } |
| 72 | #endif |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 73 | |
| 74 | } // namespace android |