blob: 40b11b0720614b5e3ccb36f700ccfbc6d010c414 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
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 Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "MtpRequestPacket"
18
Mike Lockwood16864ba2010-05-11 17:16:59 -040019#include <stdio.h>
20#include <sys/types.h>
21#include <fcntl.h>
22
23#include "MtpRequestPacket.h"
24
Mike Lockwood42d0b792011-01-04 14:48:57 -050025#include <usbhost/usbhost.h>
26
Mike Lockwood7850ef92010-05-14 10:10:36 -040027namespace android {
28
Mike Lockwood16864ba2010-05-11 17:16:59 -040029MtpRequestPacket::MtpRequestPacket()
Mike Lockwoodab063842014-11-12 14:20:06 -080030 : MtpPacket(512),
31 mParameterCount(0)
Mike Lockwood16864ba2010-05-11 17:16:59 -040032{
33}
34
35MtpRequestPacket::~MtpRequestPacket() {
36}
37
38#ifdef MTP_DEVICE
39int MtpRequestPacket::read(int fd) {
40 int ret = ::read(fd, mBuffer, mBufferSize);
Mike Lockwoodab063842014-11-12 14:20:06 -080041 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 if (ret >= MTP_CONTAINER_HEADER_SIZE
48 && ret <= MTP_CONTAINER_HEADER_SIZE + 5 * sizeof(uint32_t)
49 && ((ret - MTP_CONTAINER_HEADER_SIZE) & 3) == 0) {
Mike Lockwood16864ba2010-05-11 17:16:59 -040050 mPacketSize = ret;
Mike Lockwoodab063842014-11-12 14:20:06 -080051 mParameterCount = (ret - MTP_CONTAINER_HEADER_SIZE) / sizeof(uint32_t);
52 } else {
53 ALOGE("Malformed MTP request packet");
54 ret = -1;
55 }
Mike Lockwood16864ba2010-05-11 17:16:59 -040056 return ret;
57}
58#endif
59
60#ifdef MTP_HOST
61 // write our buffer to the given endpoint (host mode)
Mike Lockwood42d0b792011-01-04 14:48:57 -050062int MtpRequestPacket::write(struct usb_request *request)
Mike Lockwood16864ba2010-05-11 17:16:59 -040063{
64 putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
65 putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_COMMAND);
Mike Lockwood42d0b792011-01-04 14:48:57 -050066 request->buffer = mBuffer;
67 request->buffer_length = mPacketSize;
68 return transfer(request);
Mike Lockwood16864ba2010-05-11 17:16:59 -040069}
70#endif
Mike Lockwood7850ef92010-05-14 10:10:36 -040071
72} // namespace android