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