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