blob: 921ecbd0d2de9aca3621fadf31f77f1c531c7d0d [file] [log] [blame]
Mike Lockwood873871f2010-07-12 18:54:16 -04001/*
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 Lockwood873871f2010-07-12 18:54:16 -040022
Jerry Zhang487be612016-10-24 12:10:41 -070023#include "IMtpHandle.h"
Mike Lockwood873871f2010-07-12 18:54:16 -040024#include "MtpEventPacket.h"
25
Mike Lockwood42d0b792011-01-04 14:48:57 -050026#include <usbhost/usbhost.h>
27
Mike Lockwood873871f2010-07-12 18:54:16 -040028namespace android {
29
30MtpEventPacket::MtpEventPacket()
31 : MtpPacket(512)
32{
33}
34
35MtpEventPacket::~MtpEventPacket() {
36}
37
38#ifdef MTP_DEVICE
Jerry Zhang487be612016-10-24 12:10:41 -070039int MtpEventPacket::write(IMtpHandle *h) {
Mike Lockwood873871f2010-07-12 18:54:16 -040040 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 Zhang487be612016-10-24 12:10:41 -070047 int ret = h->sendEvent(event);
Mike Lockwood873871f2010-07-12 18:54:16 -040048 return (ret < 0 ? ret : 0);
49}
50#endif
51
52#ifdef MTP_HOST
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090053int MtpEventPacket::sendRequest(struct usb_request *request) {
Mike Lockwood42d0b792011-01-04 14:48:57 -050054 request->buffer = mBuffer;
55 request->buffer_length = mBufferSize;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090056 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
64int MtpEventPacket::readResponse(struct usb_device *device) {
Philip P. Moltmanncb8d8af2016-10-19 10:11:22 -070065 struct usb_request* const req = usb_request_wait(device, -1);
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090066 if (req) {
67 mPacketSize = req->actual_length;
68 return req->actual_length;
69 } else {
70 return -1;
71 }
Mike Lockwood873871f2010-07-12 18:54:16 -040072}
73#endif
74
75} // namespace android