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