Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 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 | |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 20 | #include "MtpEventPacket.h" |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 21 | #include "MtpDataPacket.h" |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 22 | #include "MtpRequestPacket.h" |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 23 | #include "MtpResponsePacket.h" |
| 24 | #include "MtpTypes.h" |
| 25 | |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 26 | #include <utils/threads.h> |
| 27 | |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 28 | struct usb_device; |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 29 | struct usb_request; |
| 30 | struct usb_endpoint_descriptor; |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 31 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 32 | namespace android { |
| 33 | |
| 34 | class MtpDeviceInfo; |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 35 | class MtpEventPacket; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 36 | class MtpObjectInfo; |
| 37 | class MtpStorageInfo; |
| 38 | |
| 39 | class MtpDevice { |
| 40 | private: |
| 41 | struct usb_device* mDevice; |
| 42 | int mInterface; |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 43 | struct usb_request* mRequestIn1; |
| 44 | struct usb_request* mRequestIn2; |
| 45 | struct usb_request* mRequestOut; |
| 46 | struct usb_request* mRequestIntr; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 47 | MtpDeviceInfo* mDeviceInfo; |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 48 | MtpPropertyList mDeviceProperties; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 49 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 50 | // current session ID |
| 51 | MtpSessionID mSessionID; |
| 52 | // current transaction ID |
| 53 | MtpTransactionID mTransactionID; |
| 54 | |
| 55 | MtpRequestPacket mRequest; |
| 56 | MtpDataPacket mData; |
| 57 | MtpResponsePacket mResponse; |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 58 | MtpEventPacket mEventPacket; |
| 59 | |
Mike Lockwood | f745462 | 2010-12-09 18:34:18 -0800 | [diff] [blame] | 60 | // set to true if we received a response packet instead of a data packet |
| 61 | bool mReceivedResponse; |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 62 | bool mProcessingEvent; |
| 63 | int mCurrentEventHandle; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 64 | |
Daichi Hirono | 8d20945 | 2016-10-04 17:45:31 +0900 | [diff] [blame] | 65 | // to check if a sendObject request follows the last sendObjectInfo request. |
| 66 | MtpTransactionID mLastSendObjectInfoTransactionID; |
| 67 | MtpObjectHandle mLastSendObjectInfoObjectHandle; |
| 68 | |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 69 | // to ensure only one MTP transaction at a time |
| 70 | Mutex mMutex; |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 71 | Mutex mEventMutex; |
| 72 | Mutex mEventMutexForInterrupt; |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 73 | |
Daichi Hirono | d4b4296 | 2016-10-04 17:34:43 +0900 | [diff] [blame^] | 74 | // Remember the device's packet division mode. |
| 75 | UrbPacketDivisionMode mPacketDivisionMode; |
| 76 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 77 | public: |
Daichi Hirono | 4a7cea8 | 2015-12-11 15:49:43 +0900 | [diff] [blame] | 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); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 86 | |
Mike Lockwood | 23f1b33 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 87 | static MtpDevice* open(const char* deviceName, int fd); |
| 88 | |
| 89 | virtual ~MtpDevice(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 90 | |
| 91 | void initialize(); |
| 92 | void close(); |
Mike Lockwood | 0c7c7c7 | 2010-12-07 11:24:28 -0800 | [diff] [blame] | 93 | void print(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 94 | const char* getDeviceName(); |
| 95 | |
| 96 | bool openSession(); |
| 97 | bool closeSession(); |
| 98 | |
| 99 | MtpDeviceInfo* getDeviceInfo(); |
| 100 | MtpStorageIDList* getStorageIDs(); |
| 101 | MtpStorageInfo* getStorageInfo(MtpStorageID storageID); |
Mike Lockwood | 27afe3a | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 102 | MtpObjectHandleList* getObjectHandles(MtpStorageID storageID, MtpObjectFormat format, |
| 103 | MtpObjectHandle parent); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 104 | MtpObjectInfo* getObjectInfo(MtpObjectHandle handle); |
Mike Lockwood | 3e072b3 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 105 | void* getThumbnail(MtpObjectHandle handle, int& outLength); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 106 | MtpObjectHandle sendObjectInfo(MtpObjectInfo* info); |
Tomasz Mikolajewski | 532b4f2 | 2015-08-25 14:14:36 +0900 | [diff] [blame] | 107 | bool sendObject(MtpObjectHandle handle, int size, int srcFD); |
Mike Lockwood | 6afc41d | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 108 | bool deleteObject(MtpObjectHandle handle); |
| 109 | MtpObjectHandle getParent(MtpObjectHandle handle); |
Daichi Hirono | d722107 | 2016-01-22 14:22:28 +0900 | [diff] [blame] | 110 | MtpStorageID getStorageID(MtpObjectHandle handle); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 111 | |
Mike Lockwood | 98693f6 | 2010-12-07 10:58:56 -0800 | [diff] [blame] | 112 | MtpObjectPropertyList* getObjectPropsSupported(MtpObjectFormat format); |
| 113 | |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 114 | MtpProperty* getDevicePropDesc(MtpDeviceProperty code); |
Mike Lockwood | 99e393a | 2010-12-07 18:53:04 -0800 | [diff] [blame] | 115 | MtpProperty* getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format); |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 116 | |
Daichi Hirono | 66a9abe | 2016-03-24 20:56:13 +0900 | [diff] [blame] | 117 | // Reads value of |property| for |handle|. Returns true on success. |
| 118 | bool getObjectPropValue(MtpObjectHandle handle, MtpProperty* property); |
| 119 | |
Daichi Hirono | 4fd9a8b | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 120 | bool readObject(MtpObjectHandle handle, ReadObjectCallback callback, |
Daichi Hirono | 4a7cea8 | 2015-12-11 15:49:43 +0900 | [diff] [blame] | 121 | uint32_t objectSize, void* clientData); |
Mike Lockwood | 23f1b33 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 122 | bool readObject(MtpObjectHandle handle, const char* destPath, int group, |
Mike Lockwood | 27afe3a | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 123 | int perm); |
Tomasz Mikolajewski | 025ffd9 | 2015-08-04 18:38:31 +0900 | [diff] [blame] | 124 | bool readObject(MtpObjectHandle handle, int fd); |
Daichi Hirono | 4a7cea8 | 2015-12-11 15:49:43 +0900 | [diff] [blame] | 125 | bool readPartialObject(MtpObjectHandle handle, |
| 126 | uint32_t offset, |
| 127 | uint32_t size, |
| 128 | uint32_t *writtenSize, |
| 129 | ReadObjectCallback callback, |
| 130 | void* clientData); |
Daichi Hirono | 326edd9 | 2016-01-22 15:34:46 +0900 | [diff] [blame] | 131 | bool readPartialObject64(MtpObjectHandle handle, |
| 132 | uint64_t offset, |
| 133 | uint32_t size, |
| 134 | uint32_t *writtenSize, |
| 135 | ReadObjectCallback callback, |
| 136 | void* clientData); |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 137 | // Starts a request to read MTP event from MTP device. It returns a request handle that |
| 138 | // can be used for blocking read or cancel. If other thread has already been processing an |
| 139 | // event returns -1. |
| 140 | int submitEventRequest(); |
| 141 | // Waits for MTP event from the device and returns MTP event code. It blocks the current thread |
| 142 | // until it receives an event from the device. |handle| should be a request handle returned |
Daichi Hirono | 59a9060 | 2016-01-11 13:36:57 +0900 | [diff] [blame] | 143 | // by |submitEventRequest|. The function writes event parameters to |parameters|. Returns 0 for |
| 144 | // cancellations. Returns -1 for errors. |
| 145 | int reapEventRequest(int handle, uint32_t (*parameters)[3]); |
Daichi Hirono | 8a7ffae | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 146 | // Cancels an event request. |handle| should be request handle returned by |
| 147 | // |submitEventRequest|. If there is a thread blocked by |reapEventRequest| with the same |
| 148 | // |handle|, the thread will resume. |
| 149 | void discardEventRequest(int handle); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 150 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 151 | private: |
Daichi Hirono | 4fd9a8b | 2015-08-20 15:13:40 +0900 | [diff] [blame] | 152 | // If |objectSize| is not NULL, it checks object size before reading data bytes. |
Daichi Hirono | 4a7cea8 | 2015-12-11 15:49:43 +0900 | [diff] [blame] | 153 | bool readObjectInternal(MtpObjectHandle handle, |
| 154 | ReadObjectCallback callback, |
| 155 | const uint32_t* objectSize, |
| 156 | void* clientData); |
| 157 | // If |objectSize| is not NULL, it checks object size before reading data bytes. |
| 158 | bool readData(ReadObjectCallback callback, |
| 159 | const uint32_t* objectSize, |
| 160 | uint32_t* writtenData, |
| 161 | void* clientData); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 162 | bool sendRequest(MtpOperationCode operation); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 163 | bool sendData(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 164 | bool readData(); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 165 | bool writeDataHeader(MtpOperationCode operation, int dataLength); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 166 | MtpResponseCode readResponse(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | }; // namespace android |
| 170 | |
| 171 | #endif // _MTP_DEVICE_H |