blob: fc74542106c0cbc4c29d1905cdc3d4cab69683cd [file] [log] [blame]
Mike Lockwoodbe125a52010-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>
22#include <sys/ioctl.h>
23
Mike Lockwoodbbd9f362010-07-19 09:01:57 -040024#ifdef MTP_DEVICE
Mike Lockwood9c7fdf52010-07-15 13:36:52 -040025#include <linux/usb/f_mtp.h>
Mike Lockwoodbbd9f362010-07-19 09:01:57 -040026#endif
Mike Lockwoodbe125a52010-07-12 18:54:16 -040027
28#include "MtpEventPacket.h"
29
30namespace android {
31
32MtpEventPacket::MtpEventPacket()
33 : MtpPacket(512)
34{
35}
36
37MtpEventPacket::~MtpEventPacket() {
38}
39
40#ifdef MTP_DEVICE
41int MtpEventPacket::write(int fd) {
42 struct mtp_event event;
43
44 putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
45 putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_EVENT);
46
47 event.data = mBuffer;
48 event.length = mPacketSize;
49 int ret = ::ioctl(fd, MTP_SEND_EVENT, (unsigned long)&event);
50 return (ret < 0 ? ret : 0);
51}
52#endif
53
54#ifdef MTP_HOST
55 // read our buffer from the given endpoint
56int MtpEventPacket::read(struct usb_endpoint *ep) {
57 int ret = transfer(ep, mBuffer, mBufferSize);
58 if (ret >= 0)
59 mPacketSize = ret;
60 else
61 mPacketSize = 0;
62 return ret;
63}
64#endif
65
66} // namespace android
67