MtpRequestPacket.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "MtpRequestPacket"
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <fcntl.h>
  20. #include "IMtpHandle.h"
  21. #include "MtpRequestPacket.h"
  22. #include <usbhost/usbhost.h>
  23. namespace android {
  24. MtpRequestPacket::MtpRequestPacket()
  25. : MtpPacket(512),
  26. mParameterCount(0)
  27. {
  28. }
  29. MtpRequestPacket::~MtpRequestPacket() {
  30. }
  31. #ifdef MTP_DEVICE
  32. int MtpRequestPacket::read(IMtpHandle *h) {
  33. int ret = h->read(mBuffer, mBufferSize);
  34. if (ret < 0) {
  35. // file read error
  36. return ret;
  37. }
  38. // request packet should have 12 byte header followed by 0 to 5 32-bit arguments
  39. const size_t read_size = static_cast<size_t>(ret);
  40. if (read_size >= MTP_CONTAINER_HEADER_SIZE
  41. && read_size <= MTP_CONTAINER_HEADER_SIZE + 5 * sizeof(uint32_t)
  42. && ((read_size - MTP_CONTAINER_HEADER_SIZE) & 3) == 0) {
  43. mPacketSize = read_size;
  44. mParameterCount = (read_size - MTP_CONTAINER_HEADER_SIZE) / sizeof(uint32_t);
  45. } else {
  46. ALOGE("Malformed MTP request packet");
  47. ret = -1;
  48. }
  49. return ret;
  50. }
  51. #endif
  52. #ifdef MTP_HOST
  53. // write our buffer to the given endpoint (host mode)
  54. int MtpRequestPacket::write(struct usb_request *request)
  55. {
  56. putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
  57. putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_COMMAND);
  58. request->buffer = mBuffer;
  59. request->buffer_length = mPacketSize;
  60. return transfer(request);
  61. }
  62. #endif
  63. } // namespace android