MtpEventPacket.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "MtpEventPacket"
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <fcntl.h>
  20. #include "IMtpHandle.h"
  21. #include "MtpEventPacket.h"
  22. #include <usbhost/usbhost.h>
  23. namespace android {
  24. MtpEventPacket::MtpEventPacket()
  25. : MtpPacket(512)
  26. {
  27. }
  28. MtpEventPacket::~MtpEventPacket() {
  29. }
  30. #ifdef MTP_DEVICE
  31. int MtpEventPacket::write(IMtpHandle *h) {
  32. struct mtp_event event;
  33. putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
  34. putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_EVENT);
  35. event.data = mBuffer;
  36. event.length = mPacketSize;
  37. int ret = h->sendEvent(event);
  38. return (ret < 0 ? ret : 0);
  39. }
  40. #endif
  41. #ifdef MTP_HOST
  42. int MtpEventPacket::sendRequest(struct usb_request *request) {
  43. request->buffer = mBuffer;
  44. request->buffer_length = mBufferSize;
  45. mPacketSize = 0;
  46. if (usb_request_queue(request)) {
  47. ALOGE("usb_endpoint_queue failed, errno: %d", errno);
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. int MtpEventPacket::readResponse(struct usb_device *device) {
  53. struct usb_request* const req = usb_request_wait(device, -1);
  54. if (req) {
  55. mPacketSize = req->actual_length;
  56. return req->actual_length;
  57. } else {
  58. return -1;
  59. }
  60. }
  61. #endif
  62. } // namespace android