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