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 
23 #include "IMtpHandle.h"
24 #include "MtpEventPacket.h"
25 
26 #include <usbhost/usbhost.h>
27 
28 namespace android {
29 
MtpEventPacket()30 MtpEventPacket::MtpEventPacket()
31     :   MtpPacket(512)
32 {
33 }
34 
~MtpEventPacket()35 MtpEventPacket::~MtpEventPacket() {
36 }
37 
38 #ifdef MTP_DEVICE
write(IMtpHandle * h)39 int MtpEventPacket::write(IMtpHandle *h) {
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;
47     int ret = h->sendEvent(event);
48     return (ret < 0 ? ret : 0);
49 }
50 #endif
51 
52 #ifdef MTP_HOST
sendRequest(struct usb_request * request)53 int MtpEventPacket::sendRequest(struct usb_request *request) {
54     request->buffer = mBuffer;
55     request->buffer_length = mBufferSize;
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 
readResponse(struct usb_device * device)64 int MtpEventPacket::readResponse(struct usb_device *device) {
65     struct usb_request* const req = usb_request_wait(device, -1);
66     if (req) {
67         mPacketSize = req->actual_length;
68         return req->actual_length;
69     } else {
70         return -1;
71     }
72 }
73 #endif
74 
75 }  // namespace android
76