blob: fa7db8c26645c65223205752a43c10be66270d59 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -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 *
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05008 * http://www.apache.org/licenses/LICENSE-2.0
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04009 *
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 bigbiffc7eee6f2014-09-02 18:59:01 -040015 */
16
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050017#define LOG_TAG "MtpEventPacket"
18
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040019#include <stdio.h>
20#include <sys/types.h>
21#include <fcntl.h>
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040022
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050023#include "IMtpHandle.h"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040024#include "MtpEventPacket.h"
25
26#include <usbhost/usbhost.h>
27
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040028MtpEventPacket::MtpEventPacket()
29 : MtpPacket(512)
30{
31}
32
33MtpEventPacket::~MtpEventPacket() {
34}
35
36#ifdef MTP_DEVICE
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050037int MtpEventPacket::write(IMtpHandle *h) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040038 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 bigbiffaf32bb92018-12-18 18:39:53 -050045 int ret = h->sendEvent(event);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040046 return (ret < 0 ? ret : 0);
47}
48#endif
49
50#ifdef MTP_HOST
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050051int MtpEventPacket::sendRequest(struct usb_request *request) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040052 request->buffer = mBuffer;
53 request->buffer_length = mBufferSize;
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050054 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
62int 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 bigbiffc7eee6f2014-09-02 18:59:01 -040070}
71#endif