bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -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 | * |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 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. |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 15 | */ |
| 16 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 17 | #define LOG_TAG "MtpEventPacket" |
| 18 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 22 | |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 23 | #include "IMtpHandle.h" |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 24 | #include "MtpEventPacket.h" |
| 25 | |
| 26 | #include <usbhost/usbhost.h> |
| 27 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 28 | MtpEventPacket::MtpEventPacket() |
| 29 | : MtpPacket(512) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | MtpEventPacket::~MtpEventPacket() { |
| 34 | } |
| 35 | |
| 36 | #ifdef MTP_DEVICE |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 37 | int MtpEventPacket::write(IMtpHandle *h) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 38 | struct mtp_event event; |
| 39 | |
| 40 | putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize); |
| 41 | putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_EVENT); |
| 42 | |
| 43 | event.data = mBuffer; |
| 44 | event.length = mPacketSize; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 45 | int ret = h->sendEvent(event); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 46 | return (ret < 0 ? ret : 0); |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | #ifdef MTP_HOST |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 51 | int MtpEventPacket::sendRequest(struct usb_request *request) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 52 | request->buffer = mBuffer; |
| 53 | request->buffer_length = mBufferSize; |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 54 | mPacketSize = 0; |
| 55 | if (usb_request_queue(request)) { |
| 56 | MTPE("usb_endpoint_queue failed, errno: %d", errno); |
| 57 | return -1; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int MtpEventPacket::readResponse(struct usb_device *device) { |
| 63 | struct usb_request* const req = usb_request_wait(device, -1); |
| 64 | if (req) { |
| 65 | mPacketSize = req->actual_length; |
| 66 | return req->actual_length; |
| 67 | } else { |
| 68 | return -1; |
| 69 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 70 | } |
| 71 | #endif |