Mike Lockwood | 873871f | 2010-07-12 18:54:16 -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 | #define LOG_TAG "MtpEventPacket" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 22 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 23 | #include "IMtpHandle.h" |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 24 | #include "MtpEventPacket.h" |
| 25 | |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 26 | #include <usbhost/usbhost.h> |
| 27 | |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | MtpEventPacket::MtpEventPacket() |
| 31 | : MtpPacket(512) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | MtpEventPacket::~MtpEventPacket() { |
| 36 | } |
| 37 | |
| 38 | #ifdef MTP_DEVICE |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 39 | int MtpEventPacket::write(IMtpHandle *h) { |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 40 | struct mtp_event event; |
| 41 | |
| 42 | putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 43 | putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_EVENT); |
| 44 | |
| 45 | event.data = mBuffer; |
| 46 | event.length = mPacketSize; |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 47 | int ret = h->sendEvent(event); |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 48 | return (ret < 0 ? ret : 0); |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | #ifdef MTP_HOST |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 53 | int MtpEventPacket::sendRequest(struct usb_request *request) { |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 54 | request->buffer = mBuffer; |
| 55 | request->buffer_length = mBufferSize; |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 56 | mPacketSize = 0; |
| 57 | if (usb_request_queue(request)) { |
| 58 | ALOGE("usb_endpoint_queue failed, errno: %d", errno); |
| 59 | return -1; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int MtpEventPacket::readResponse(struct usb_device *device) { |
Philip P. Moltmann | cb8d8af | 2016-10-19 10:11:22 -0700 | [diff] [blame] | 65 | struct usb_request* const req = usb_request_wait(device, -1); |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 66 | if (req) { |
| 67 | mPacketSize = req->actual_length; |
| 68 | return req->actual_length; |
| 69 | } else { |
| 70 | return -1; |
| 71 | } |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 72 | } |
| 73 | #endif |
| 74 | |
| 75 | } // namespace android |