MtpResponsePacket.cpp revision b14e588bec4d5e39e61b020b5b575f2ce555d316
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
17#define LOG_TAG "MtpResponsePacket"
18
19#include <stdio.h>
20#include <sys/types.h>
21#include <fcntl.h>
22
23#include "MtpResponsePacket.h"
24
25namespace android {
26
27MtpResponsePacket::MtpResponsePacket()
28    :   MtpPacket(512)
29{
30}
31
32MtpResponsePacket::~MtpResponsePacket() {
33}
34
35#ifdef MTP_DEVICE
36int MtpResponsePacket::write(int fd) {
37    putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
38    putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_RESPONSE);
39    int ret = ::write(fd, mBuffer, mPacketSize);
40    return (ret < 0 ? ret : 0);
41}
42#endif
43
44#ifdef MTP_HOST
45    // read our buffer from the given endpoint
46int MtpResponsePacket::read(struct usb_endpoint *ep) {
47    int ret = transfer(ep, mBuffer, mBufferSize);
48     if (ret >= 0)
49        mPacketSize = ret;
50    else
51        mPacketSize = 0;
52    return ret;
53}
54#endif
55
56}  // namespace android
57
58