MtpDevice.h revision 0cf89f2e622aa53f31fa5762ca4bc805bb509ed3
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#ifndef _MTP_DEVICE_H
18#define _MTP_DEVICE_H
19
20#include "MtpRequestPacket.h"
21#include "MtpDataPacket.h"
22#include "MtpResponsePacket.h"
23#include "MtpTypes.h"
24
25#include <utils/threads.h>
26
27struct usb_device;
28
29namespace android {
30
31class MtpDeviceInfo;
32class MtpObjectInfo;
33class MtpStorageInfo;
34
35class MtpDevice {
36private:
37    struct usb_device*      mDevice;
38    int                     mInterface;
39    struct usb_endpoint*    mEndpointIn;
40    struct usb_endpoint*    mEndpointOut;
41    struct usb_endpoint*    mEndpointIntr;
42    MtpDeviceInfo*          mDeviceInfo;
43    MtpPropertyList         mDeviceProperties;
44
45    // a unique ID for the device
46    int                     mID;
47
48    // current session ID
49    MtpSessionID            mSessionID;
50    // current transaction ID
51    MtpTransactionID        mTransactionID;
52
53    MtpRequestPacket        mRequest;
54    MtpDataPacket           mData;
55    MtpResponsePacket       mResponse;
56
57    // to ensure only one MTP transaction at a time
58    Mutex                   mMutex;
59
60public:
61                            MtpDevice(struct usb_device* device, int interface,
62                                    struct usb_endpoint *ep_in, struct usb_endpoint *ep_out,
63                                    struct usb_endpoint *ep_intr);
64    virtual                 ~MtpDevice();
65
66    inline int              getID() const { return mID; }
67
68    void                    initialize();
69    void                    close();
70    const char*             getDeviceName();
71
72    bool                    openSession();
73    bool                    closeSession();
74
75    MtpDeviceInfo*          getDeviceInfo();
76    MtpStorageIDList*       getStorageIDs();
77    MtpStorageInfo*         getStorageInfo(MtpStorageID storageID);
78    MtpObjectHandleList*    getObjectHandles(MtpStorageID storageID, MtpObjectFormat format, MtpObjectHandle parent);
79    MtpObjectInfo*          getObjectInfo(MtpObjectHandle handle);
80    void*                   getThumbnail(MtpObjectHandle handle, int& outLength);
81    MtpObjectHandle         sendObjectInfo(MtpObjectInfo* info);
82    bool                    sendObject(MtpObjectInfo* info, int srcFD);
83    bool                    deleteObject(MtpObjectHandle handle);
84    MtpObjectHandle         getParent(MtpObjectHandle handle);
85    MtpObjectHandle         getStorageID(MtpObjectHandle handle);
86
87    MtpProperty*            getDevicePropDesc(MtpDeviceProperty code);
88
89    // returns the file descriptor for a pipe to read the object's data
90    int                     readObject(MtpObjectHandle handle, int objectSize);
91
92private:
93    friend class ReadObjectThread;
94
95    bool                    sendRequest(MtpOperationCode operation);
96    bool                    sendData();
97    bool                    readData();
98    bool                    writeDataHeader(MtpOperationCode operation, int dataLength);
99    MtpResponseCode         readResponse();
100
101};
102
103}; // namespace android
104
105#endif // _MTP_DEVICE_H
106