MtpPacket.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #define LOG_TAG "MtpPacket"
  17. #include "MtpDebug.h"
  18. #include "MtpPacket.h"
  19. #include "mtp.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <usbhost/usbhost.h>
  24. namespace android {
  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. ALOGE("out of memory!");
  34. abort();
  35. }
  36. }
  37. MtpPacket::~MtpPacket() {
  38. if (mBuffer)
  39. free(mBuffer);
  40. }
  41. void MtpPacket::reset() {
  42. allocate(MTP_CONTAINER_HEADER_SIZE);
  43. mPacketSize = MTP_CONTAINER_HEADER_SIZE;
  44. memset(mBuffer, 0, mBufferSize);
  45. }
  46. void MtpPacket::allocate(size_t length) {
  47. if (length > mBufferSize) {
  48. int newLength = length + mAllocationIncrement;
  49. mBuffer = (uint8_t *)realloc(mBuffer, newLength);
  50. if (!mBuffer) {
  51. ALOGE("out of memory!");
  52. abort();
  53. }
  54. mBufferSize = newLength;
  55. }
  56. }
  57. void MtpPacket::dump() {
  58. #define DUMP_BYTES_PER_ROW 16
  59. char buffer[500];
  60. char* bufptr = buffer;
  61. for (size_t i = 0; i < mPacketSize; i++) {
  62. bufptr += snprintf(bufptr, sizeof(buffer) - (bufptr - buffer), "%02X ",
  63. mBuffer[i]);
  64. if (i % DUMP_BYTES_PER_ROW == (DUMP_BYTES_PER_ROW - 1)) {
  65. ALOGV("%s", buffer);
  66. bufptr = buffer;
  67. }
  68. }
  69. if (bufptr != buffer) {
  70. // print last line
  71. ALOGV("%s", buffer);
  72. }
  73. ALOGV("\n");
  74. }
  75. void MtpPacket::copyFrom(const MtpPacket& src) {
  76. int length = src.mPacketSize;
  77. allocate(length);
  78. mPacketSize = length;
  79. memcpy(mBuffer, src.mBuffer, length);
  80. }
  81. uint16_t MtpPacket::getUInt16(int offset) const {
  82. return ((uint16_t)mBuffer[offset + 1] << 8) | (uint16_t)mBuffer[offset];
  83. }
  84. uint32_t MtpPacket::getUInt32(int offset) const {
  85. return ((uint32_t)mBuffer[offset + 3] << 24) | ((uint32_t)mBuffer[offset + 2] << 16) |
  86. ((uint32_t)mBuffer[offset + 1] << 8) | (uint32_t)mBuffer[offset];
  87. }
  88. void MtpPacket::putUInt16(int offset, uint16_t value) {
  89. mBuffer[offset++] = (uint8_t)(value & 0xFF);
  90. mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
  91. }
  92. void MtpPacket::putUInt32(int offset, uint32_t value) {
  93. mBuffer[offset++] = (uint8_t)(value & 0xFF);
  94. mBuffer[offset++] = (uint8_t)((value >> 8) & 0xFF);
  95. mBuffer[offset++] = (uint8_t)((value >> 16) & 0xFF);
  96. mBuffer[offset++] = (uint8_t)((value >> 24) & 0xFF);
  97. }
  98. uint16_t MtpPacket::getContainerCode() const {
  99. return getUInt16(MTP_CONTAINER_CODE_OFFSET);
  100. }
  101. void MtpPacket::setContainerCode(uint16_t code) {
  102. putUInt16(MTP_CONTAINER_CODE_OFFSET, code);
  103. }
  104. uint16_t MtpPacket::getContainerType() const {
  105. return getUInt16(MTP_CONTAINER_TYPE_OFFSET);
  106. }
  107. MtpTransactionID MtpPacket::getTransactionID() const {
  108. return getUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET);
  109. }
  110. void MtpPacket::setTransactionID(MtpTransactionID id) {
  111. putUInt32(MTP_CONTAINER_TRANSACTION_ID_OFFSET, id);
  112. }
  113. uint32_t MtpPacket::getParameter(int index) const {
  114. if (index < 1 || index > 5) {
  115. ALOGE("index %d out of range in MtpPacket::getParameter", index);
  116. return 0;
  117. }
  118. return getUInt32(MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t));
  119. }
  120. void MtpPacket::setParameter(int index, uint32_t value) {
  121. if (index < 1 || index > 5) {
  122. ALOGE("index %d out of range in MtpPacket::setParameter", index);
  123. return;
  124. }
  125. int offset = MTP_CONTAINER_PARAMETER_OFFSET + (index - 1) * sizeof(uint32_t);
  126. if (mPacketSize < offset + sizeof(uint32_t))
  127. mPacketSize = offset + sizeof(uint32_t);
  128. putUInt32(offset, value);
  129. }
  130. #ifdef MTP_HOST
  131. int MtpPacket::transfer(struct usb_request* request) {
  132. int result = usb_device_bulk_transfer(request->dev,
  133. request->endpoint,
  134. request->buffer,
  135. request->buffer_length,
  136. 1000);
  137. request->actual_length = result;
  138. return result;
  139. }
  140. #endif
  141. } // namespace android