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