blob: 88f245f9e52498b1ffc922cd7fd98e70559bb9a9 [file] [log] [blame]
Mike Lockwood5ed68d22010-05-25 19:08:48 -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#ifndef _MTP_DEVICE_H
18#define _MTP_DEVICE_H
19
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090020#include "MtpEventPacket.h"
Mike Lockwood5ed68d22010-05-25 19:08:48 -040021#include "MtpDataPacket.h"
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090022#include "MtpRequestPacket.h"
Mike Lockwood5ed68d22010-05-25 19:08:48 -040023#include "MtpResponsePacket.h"
24#include "MtpTypes.h"
25
Mike Lockwood0cf89f22010-07-26 20:40:45 -040026#include <utils/threads.h>
27
Mike Lockwoodb14e5882010-06-29 18:11:52 -040028struct usb_device;
Mike Lockwood42d0b792011-01-04 14:48:57 -050029struct usb_request;
30struct usb_endpoint_descriptor;
Mike Lockwoodb14e5882010-06-29 18:11:52 -040031
Mike Lockwood5ed68d22010-05-25 19:08:48 -040032namespace android {
33
34class MtpDeviceInfo;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090035class MtpEventPacket;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040036class MtpObjectInfo;
37class MtpStorageInfo;
38
39class MtpDevice {
40private:
41 struct usb_device* mDevice;
42 int mInterface;
Mike Lockwood42d0b792011-01-04 14:48:57 -050043 struct usb_request* mRequestIn1;
44 struct usb_request* mRequestIn2;
45 struct usb_request* mRequestOut;
46 struct usb_request* mRequestIntr;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040047 MtpDeviceInfo* mDeviceInfo;
Mike Lockwooda6c490b2010-06-05 22:45:01 -040048 MtpPropertyList mDeviceProperties;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040049
Mike Lockwood5ed68d22010-05-25 19:08:48 -040050 // current session ID
51 MtpSessionID mSessionID;
52 // current transaction ID
53 MtpTransactionID mTransactionID;
54
55 MtpRequestPacket mRequest;
56 MtpDataPacket mData;
57 MtpResponsePacket mResponse;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090058 MtpEventPacket mEventPacket;
59
Mike Lockwoodf7454622010-12-09 18:34:18 -080060 // set to true if we received a response packet instead of a data packet
61 bool mReceivedResponse;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090062 bool mProcessingEvent;
63 int mCurrentEventHandle;
Mike Lockwood5ed68d22010-05-25 19:08:48 -040064
Daichi Hirono8d209452016-10-04 17:45:31 +090065 // to check if a sendObject request follows the last sendObjectInfo request.
66 MtpTransactionID mLastSendObjectInfoTransactionID;
67 MtpObjectHandle mLastSendObjectInfoObjectHandle;
68
Mike Lockwood0cf89f22010-07-26 20:40:45 -040069 // to ensure only one MTP transaction at a time
70 Mutex mMutex;
Daichi Hirono8a7ffae2015-08-20 15:13:40 +090071 Mutex mEventMutex;
72 Mutex mEventMutexForInterrupt;
Mike Lockwood0cf89f22010-07-26 20:40:45 -040073
Mike Lockwood5ed68d22010-05-25 19:08:48 -040074public:
Daichi Hirono4a7cea82015-12-11 15:49:43 +090075 typedef bool (*ReadObjectCallback)
76 (void* data, uint32_t offset, uint32_t length, void* clientData);
77
78 MtpDevice(struct usb_device* device,
79 int interface,
80 const struct usb_endpoint_descriptor *ep_in,
81 const struct usb_endpoint_descriptor *ep_out,
82 const struct usb_endpoint_descriptor *ep_intr);
Mike Lockwood5ed68d22010-05-25 19:08:48 -040083
Mike Lockwood23f1b332010-12-30 15:38:45 -050084 static MtpDevice* open(const char* deviceName, int fd);
85
86 virtual ~MtpDevice();
Mike Lockwood5ed68d22010-05-25 19:08:48 -040087
88 void initialize();
89 void close();
Mike Lockwood0c7c7c72010-12-07 11:24:28 -080090 void print();
Mike Lockwood5ed68d22010-05-25 19:08:48 -040091 const char* getDeviceName();
92
93 bool openSession();
94 bool closeSession();
95
96 MtpDeviceInfo* getDeviceInfo();
97 MtpStorageIDList* getStorageIDs();
98 MtpStorageInfo* getStorageInfo(MtpStorageID storageID);
Mike Lockwood27afe3a2010-11-19 13:52:20 -050099 MtpObjectHandleList* getObjectHandles(MtpStorageID storageID, MtpObjectFormat format,
100 MtpObjectHandle parent);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400101 MtpObjectInfo* getObjectInfo(MtpObjectHandle handle);
Mike Lockwood3e072b32010-06-10 16:34:20 -0400102 void* getThumbnail(MtpObjectHandle handle, int& outLength);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400103 MtpObjectHandle sendObjectInfo(MtpObjectInfo* info);
Tomasz Mikolajewski532b4f22015-08-25 14:14:36 +0900104 bool sendObject(MtpObjectHandle handle, int size, int srcFD);
Mike Lockwood6afc41d2010-06-11 16:34:52 -0400105 bool deleteObject(MtpObjectHandle handle);
106 MtpObjectHandle getParent(MtpObjectHandle handle);
Daichi Hironod7221072016-01-22 14:22:28 +0900107 MtpStorageID getStorageID(MtpObjectHandle handle);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400108
Mike Lockwood98693f62010-12-07 10:58:56 -0800109 MtpObjectPropertyList* getObjectPropsSupported(MtpObjectFormat format);
110
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400111 MtpProperty* getDevicePropDesc(MtpDeviceProperty code);
Mike Lockwood99e393a2010-12-07 18:53:04 -0800112 MtpProperty* getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format);
Mike Lockwooda6c490b2010-06-05 22:45:01 -0400113
Daichi Hirono66a9abe2016-03-24 20:56:13 +0900114 // Reads value of |property| for |handle|. Returns true on success.
115 bool getObjectPropValue(MtpObjectHandle handle, MtpProperty* property);
116
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900117 bool readObject(MtpObjectHandle handle, ReadObjectCallback callback,
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900118 uint32_t objectSize, void* clientData);
Mike Lockwood23f1b332010-12-30 15:38:45 -0500119 bool readObject(MtpObjectHandle handle, const char* destPath, int group,
Mike Lockwood27afe3a2010-11-19 13:52:20 -0500120 int perm);
Tomasz Mikolajewski025ffd92015-08-04 18:38:31 +0900121 bool readObject(MtpObjectHandle handle, int fd);
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900122 bool readPartialObject(MtpObjectHandle handle,
123 uint32_t offset,
124 uint32_t size,
125 uint32_t *writtenSize,
126 ReadObjectCallback callback,
127 void* clientData);
Daichi Hirono326edd92016-01-22 15:34:46 +0900128 bool readPartialObject64(MtpObjectHandle handle,
129 uint64_t offset,
130 uint32_t size,
131 uint32_t *writtenSize,
132 ReadObjectCallback callback,
133 void* clientData);
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900134 // Starts a request to read MTP event from MTP device. It returns a request handle that
135 // can be used for blocking read or cancel. If other thread has already been processing an
136 // event returns -1.
137 int submitEventRequest();
138 // Waits for MTP event from the device and returns MTP event code. It blocks the current thread
139 // until it receives an event from the device. |handle| should be a request handle returned
Daichi Hirono59a90602016-01-11 13:36:57 +0900140 // by |submitEventRequest|. The function writes event parameters to |parameters|. Returns 0 for
141 // cancellations. Returns -1 for errors.
142 int reapEventRequest(int handle, uint32_t (*parameters)[3]);
Daichi Hirono8a7ffae2015-08-20 15:13:40 +0900143 // Cancels an event request. |handle| should be request handle returned by
144 // |submitEventRequest|. If there is a thread blocked by |reapEventRequest| with the same
145 // |handle|, the thread will resume.
146 void discardEventRequest(int handle);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400147
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400148private:
Daichi Hirono4fd9a8b2015-08-20 15:13:40 +0900149 // If |objectSize| is not NULL, it checks object size before reading data bytes.
Daichi Hirono4a7cea82015-12-11 15:49:43 +0900150 bool readObjectInternal(MtpObjectHandle handle,
151 ReadObjectCallback callback,
152 const uint32_t* objectSize,
153 void* clientData);
154 // If |objectSize| is not NULL, it checks object size before reading data bytes.
155 bool readData(ReadObjectCallback callback,
156 const uint32_t* objectSize,
157 uint32_t* writtenData,
158 void* clientData);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400159 bool sendRequest(MtpOperationCode operation);
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400160 bool sendData();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400161 bool readData();
Mike Lockwood0cf89f22010-07-26 20:40:45 -0400162 bool writeDataHeader(MtpOperationCode operation, int dataLength);
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400163 MtpResponseCode readResponse();
Mike Lockwood5ed68d22010-05-25 19:08:48 -0400164};
165
166}; // namespace android
167
168#endif // _MTP_DEVICE_H