bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -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 | * |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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. |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 15 | */ |
| 16 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 17 | #define LOG_TAG "MtpRequestPacket" |
| 18 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
| 22 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 23 | #include "IMtpHandle.h" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 24 | #include "MtpDebug.h" |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 25 | #include "MtpRequestPacket.h" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 26 | |
| 27 | #include <usbhost/usbhost.h> |
| 28 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 29 | MtpRequestPacket::MtpRequestPacket() |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 30 | : MtpPacket(512), |
| 31 | mParameterCount(0) |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
| 35 | MtpRequestPacket::~MtpRequestPacket() { |
| 36 | } |
| 37 | |
| 38 | #ifdef MTP_DEVICE |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 39 | int MtpRequestPacket::read(IMtpHandle *h) { |
| 40 | int ret = h->read(mBuffer, mBufferSize); |
| 41 | if (ret < 0) { |
| 42 | // file read error |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | // request packet should have 12 byte header followed by 0 to 5 32-bit arguments |
| 47 | const size_t read_size = static_cast<size_t>(ret); |
| 48 | if (read_size >= MTP_CONTAINER_HEADER_SIZE |
| 49 | && read_size <= MTP_CONTAINER_HEADER_SIZE + 5 * sizeof(uint32_t) |
| 50 | && ((read_size - MTP_CONTAINER_HEADER_SIZE) & 3) == 0) { |
| 51 | mPacketSize = read_size; |
| 52 | mParameterCount = (read_size - MTP_CONTAINER_HEADER_SIZE) / sizeof(uint32_t); |
| 53 | } else { |
| 54 | MTPE("Malformed MTP request packet"); |
| 55 | ret = -1; |
| 56 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 57 | return ret; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #ifdef MTP_HOST |
| 62 | // write our buffer to the given endpoint (host mode) |
| 63 | int MtpRequestPacket::write(struct usb_request *request) |
| 64 | { |
| 65 | putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 66 | putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_COMMAND); |
| 67 | request->buffer = mBuffer; |
| 68 | request->buffer_length = mPacketSize; |
| 69 | return transfer(request); |
| 70 | } |
| 71 | #endif |