MtpPacket.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 "MtpPacket"
18
19#include "MtpDebug.h"
20#include "MtpPacket.h"
21#include "mtp.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <stdio.h>
26
27#include <usbhost/usbhost.h>
28
29namespace android {
30
31MtpPacket::MtpPacket(int bufferSize)
32    :   mBuffer(NULL),
33        mBufferSize(bufferSize),
34        mAllocationIncrement(bufferSize),
35        mPacketSize(0)
36{
37    mBuffer = (uint8_t *)malloc(bufferSize);
38    if (!mBuffer) {
39        LOGE("out of memory!");
40        abort();
41    }
42}
43
44MtpPacket::~MtpPacket() {
45    if (mBuffer)
46        free(mBuffer);
47}
48
49void MtpPacket::reset() {
50    allocate(MTP_CONTAINER_HEADER_SIZE);
51    mPacketSize = MTP_CONTAINER_HEADER_SIZE;
52    memset(mBuffer, 0, mBufferSize);
53}
54
55void MtpPacket::allocate(int length) {
56    if (length > mBufferSize) {
57        int newLength = length + mAllocationIncrement;
58        mBuffer = (uint8_t *)realloc(mBuffer, newLength);
59        if (!mBuffer) {
60            LOGE("out of memory!");
61            abort();
62        }
63        mBufferSize = newLength;
64    }
65}
66
67void MtpPacket::dump() {
68#define DUMP_BYTES_PER_ROW  16
69    char buffer[500];
70    char* bufptr = buffer;
71
72    for (int i = 0; i < mPacketSize; i++) {
73        sprintf(bufptr, "%02X ", mBuffer[i]);
74        bufptr += strlen(bufptr);
75        if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
76            LOGV("%s", buffer);
77            bufptr = buffer;
78        }
79    }
80    if (bufptr != buffer) {
81        // print last line
82        LOGV("%s", buffer);
83    }
84    LOGV("\n");
85}
86
87uint16_t MtpPacket::getUInt16(int offset) const {
88    return ((uint16_t)mBuffer[offset + 1] << 8) | (uint16_t)mBuffer[offset];
89}
90
91uint32_t MtpPacket::getUInt32(int offset) const {
92    return ((uint32_t)mBuffer[offset + 3] << 24) | ((uint32_t)mBuffer[offset + 2] << 16) |
93           ((uint32_t)mBuffer[offset + 1] << 8)  | (uint32_t)mBuffer[offset];
94}
95
96void MtpPacket::putUInt16(int offset, uint16_t value) {
97    mBuffer[offset++] = (uint8_t)(value & 0xFF);
98    mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
99}
100
101void MtpPacket::putUInt32(int offset, uint32_t value) {
102    mBuffer[offset++] = (uint8_t)(value & 0xFF);
103    mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
104    mBuffer[offset++] = (uint8_t)((value >> 16) & 0xFF);
105    mBuffer[offset++] = (uint8_t)((value >> 24) & 0xFF);
106}
107
108uint16_t MtpPacket::getContainerCode() const {
109    return getUInt16(MTP_CONTAINER_CODE_OFFSET);
110}
111
112void MtpPacket::setContainerCode(uint16_t code) {
113    putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
114}
115
116MtpTransactionID MtpPacket::getTransactionID() const {
117    return getUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET);
118}
119
120void MtpPacket::setTransactionID(MtpTransactionID id) {
121    putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
122}
123
124uint32_t MtpPacket::getParameter(int index) const {
125    if (index < 1 || index > 5) {
126        LOGE("index %d out of range in MtpRequestPacket::getParameter", index);
127        return 0;
128    }
129    return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
130}
131
132void MtpPacket::setParameter(int index, uint32_t value) {
133    if (index < 1 || index > 5) {
134        LOGE("index %d out of range in MtpResponsePacket::setParameter", index);
135        return;
136    }
137    int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
138    if (mPacketSize < offset + sizeof(uint32_t))
139        mPacketSize = offset + sizeof(uint32_t);
140    putUInt32(offset, value);
141}
142
143#ifdef MTP_HOST
144int MtpPacket::transfer(struct usb_endpoint *ep, void* buffer, int length) {
145    if (usb_endpoint_queue(ep, buffer, length)) {
146        LOGE("usb_endpoint_queue failed, errno: %d", errno);
147        return -1;
148    }
149    int ep_num;
150    return usb_endpoint_wait(usb_endpoint_get_device(ep), &ep_num);
151}
152#endif
153
154}  // namespace android
155