• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MtpEventPacket.h"
21 #include "MtpDataPacket.h"
22 #include "MtpRequestPacket.h"
23 #include "MtpResponsePacket.h"
24 #include "MtpTypes.h"
25 
26 #include <mutex>
27 
28 struct usb_device;
29 struct usb_request;
30 struct usb_endpoint_descriptor;
31 
32 namespace android {
33 
34 class MtpDeviceInfo;
35 class MtpEventPacket;
36 class MtpObjectInfo;
37 class MtpStorageInfo;
38 
39 class MtpDevice {
40 private:
41     struct usb_device*      mDevice;
42     int                     mInterface;
43     struct usb_request*     mRequestIn1;
44     struct usb_request*     mRequestIn2;
45     struct usb_request*     mRequestOut;
46     struct usb_request*     mRequestIntr;
47     MtpDeviceInfo*          mDeviceInfo;
48     MtpPropertyList         mDeviceProperties;
49 
50     // current session ID
51     MtpSessionID            mSessionID;
52     // current transaction ID
53     MtpTransactionID        mTransactionID;
54 
55     MtpRequestPacket        mRequest;
56     MtpDataPacket           mData;
57     MtpResponsePacket       mResponse;
58     MtpEventPacket          mEventPacket;
59 
60     // set to true if we received a response packet instead of a data packet
61     bool                    mReceivedResponse;
62     bool                    mProcessingEvent;
63     int                     mCurrentEventHandle;
64 
65     // to check if a sendObject request follows the last sendObjectInfo request.
66     MtpTransactionID        mLastSendObjectInfoTransactionID;
67     MtpObjectHandle         mLastSendObjectInfoObjectHandle;
68 
69     // to ensure only one MTP transaction at a time
70     std::mutex              mMutex;
71     std::mutex              mEventMutex;
72     std::mutex              mEventMutexForInterrupt;
73 
74     // Remember the device's packet division mode.
75     UrbPacketDivisionMode   mPacketDivisionMode;
76 
77 public:
78     typedef bool (*ReadObjectCallback)
79             (void* data, uint32_t offset, uint32_t length, void* clientData);
80 
81     MtpDevice(struct usb_device* device,
82               int interface,
83               const struct usb_endpoint_descriptor *ep_in,
84               const struct usb_endpoint_descriptor *ep_out,
85               const struct usb_endpoint_descriptor *ep_intr);
86 
87     static MtpDevice*       open(const char* deviceName, int fd);
88 
89     virtual                 ~MtpDevice();
90 
91     void                    initialize();
92     void                    close();
93     void                    print();
94     const char*             getDeviceName();
95 
96     bool                    openSession();
97     bool                    closeSession();
98 
99     MtpDeviceInfo*          getDeviceInfo();
100     MtpStorageIDList*       getStorageIDs();
101     MtpStorageInfo*         getStorageInfo(MtpStorageID storageID);
102     MtpObjectHandleList*    getObjectHandles(MtpStorageID storageID, MtpObjectFormat format,
103                                     MtpObjectHandle parent);
104     MtpObjectInfo*          getObjectInfo(MtpObjectHandle handle);
105     void*                   getThumbnail(MtpObjectHandle handle, int& outLength);
106     MtpObjectHandle         sendObjectInfo(MtpObjectInfo* info);
107     bool                    sendObject(MtpObjectHandle handle, uint32_t size, int srcFD);
108     bool                    deleteObject(MtpObjectHandle handle);
109     MtpObjectHandle         getParent(MtpObjectHandle handle);
110     MtpStorageID            getStorageID(MtpObjectHandle handle);
111 
112     MtpObjectPropertyList*  getObjectPropsSupported(MtpObjectFormat format);
113 
114     MtpProperty*            getDevicePropDesc(MtpDeviceProperty code);
115     bool                    setDevicePropValueStr(MtpProperty* property);
116     MtpProperty*            getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format);
117 
118     // Reads value of |property| for |handle|. Returns true on success.
119     bool                    getObjectPropValue(MtpObjectHandle handle, MtpProperty* property);
120 
121     bool                    readObject(MtpObjectHandle handle, ReadObjectCallback callback,
122                                     uint32_t objectSize, void* clientData);
123     bool                    readObject(MtpObjectHandle handle, const char* destPath, int group,
124                                     int perm);
125     bool                    readObject(MtpObjectHandle handle, int fd);
126     bool                    readPartialObject(MtpObjectHandle handle,
127                                               uint32_t offset,
128                                               uint32_t size,
129                                               uint32_t *writtenSize,
130                                               ReadObjectCallback callback,
131                                               void* clientData);
132     bool                    readPartialObject64(MtpObjectHandle handle,
133                                                 uint64_t offset,
134                                                 uint32_t size,
135                                                 uint32_t *writtenSize,
136                                                 ReadObjectCallback callback,
137                                                 void* clientData);
138     // Starts a request to read MTP event from MTP device. It returns a request handle that
139     // can be used for blocking read or cancel. If other thread has already been processing an
140     // event returns -1.
141     int                     submitEventRequest();
142     // Waits for MTP event from the device and returns MTP event code. It blocks the current thread
143     // until it receives an event from the device. |handle| should be a request handle returned
144     // by |submitEventRequest|. The function writes event parameters to |parameters|. Returns 0 for
145     // cancellations. Returns -1 for errors.
146     int                     reapEventRequest(int handle, uint32_t (*parameters)[3]);
147     // Cancels an event request. |handle| should be request handle returned by
148     // |submitEventRequest|. If there is a thread blocked by |reapEventRequest| with the same
149     // |handle|, the thread will resume.
150     void                    discardEventRequest(int handle);
151 
152 private:
153     // If |objectSize| is not NULL, it checks object size before reading data bytes.
154     bool                    readObjectInternal(MtpObjectHandle handle,
155                                                ReadObjectCallback callback,
156                                                const uint32_t* objectSize,
157                                                void* clientData);
158     // If |objectSize| is not NULL, it checks object size before reading data bytes.
159     bool                    readData(ReadObjectCallback callback,
160                                      const uint32_t* objectSize,
161                                      uint32_t* writtenData,
162                                      void* clientData);
163     bool                    sendRequest(MtpOperationCode operation);
164     bool                    sendData();
165     bool                    readData();
166     bool                    writeDataHeader(MtpOperationCode operation, int dataLength);
167     MtpResponseCode         readResponse();
168 };
169 
170 }; // namespace android
171 
172 #endif // _MTP_DEVICE_H
173