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 | |
| 20 | #include "MtpRequestPacket.h" |
| 21 | #include "MtpDataPacket.h" |
| 22 | #include "MtpResponsePacket.h" |
| 23 | #include "MtpTypes.h" |
| 24 | |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 25 | #include <utils/threads.h> |
| 26 | |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 27 | struct usb_device; |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 28 | struct usb_request; |
| 29 | struct usb_endpoint_descriptor; |
Mike Lockwood | b14e588 | 2010-06-29 18:11:52 -0400 | [diff] [blame] | 30 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | class MtpDeviceInfo; |
| 34 | class MtpObjectInfo; |
| 35 | class MtpStorageInfo; |
| 36 | |
| 37 | class MtpDevice { |
| 38 | private: |
| 39 | struct usb_device* mDevice; |
| 40 | int mInterface; |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 41 | struct usb_request* mRequestIn1; |
| 42 | struct usb_request* mRequestIn2; |
| 43 | struct usb_request* mRequestOut; |
| 44 | struct usb_request* mRequestIntr; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 45 | MtpDeviceInfo* mDeviceInfo; |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 46 | MtpPropertyList mDeviceProperties; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 47 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 48 | // current session ID |
| 49 | MtpSessionID mSessionID; |
| 50 | // current transaction ID |
| 51 | MtpTransactionID mTransactionID; |
| 52 | |
| 53 | MtpRequestPacket mRequest; |
| 54 | MtpDataPacket mData; |
| 55 | MtpResponsePacket mResponse; |
Mike Lockwood | f745462 | 2010-12-09 18:34:18 -0800 | [diff] [blame] | 56 | // set to true if we received a response packet instead of a data packet |
| 57 | bool mReceivedResponse; |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 58 | |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 59 | // to ensure only one MTP transaction at a time |
| 60 | Mutex mMutex; |
| 61 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 62 | public: |
Daichi Hirono | 4fd9a8b | 2015-08-20 15:13:40 +0900 | [diff] [blame^] | 63 | typedef bool (*ReadObjectCallback)(void* data, int offset, int length, void* clientData); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 64 | MtpDevice(struct usb_device* device, int interface, |
Mike Lockwood | 42d0b79 | 2011-01-04 14:48:57 -0500 | [diff] [blame] | 65 | const struct usb_endpoint_descriptor *ep_in, |
| 66 | const struct usb_endpoint_descriptor *ep_out, |
| 67 | const struct usb_endpoint_descriptor *ep_intr); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 68 | |
Mike Lockwood | 23f1b33 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 69 | static MtpDevice* open(const char* deviceName, int fd); |
| 70 | |
| 71 | virtual ~MtpDevice(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 72 | |
| 73 | void initialize(); |
| 74 | void close(); |
Mike Lockwood | 0c7c7c7 | 2010-12-07 11:24:28 -0800 | [diff] [blame] | 75 | void print(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 76 | const char* getDeviceName(); |
| 77 | |
| 78 | bool openSession(); |
| 79 | bool closeSession(); |
| 80 | |
| 81 | MtpDeviceInfo* getDeviceInfo(); |
| 82 | MtpStorageIDList* getStorageIDs(); |
| 83 | MtpStorageInfo* getStorageInfo(MtpStorageID storageID); |
Mike Lockwood | 27afe3a | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 84 | MtpObjectHandleList* getObjectHandles(MtpStorageID storageID, MtpObjectFormat format, |
| 85 | MtpObjectHandle parent); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 86 | MtpObjectInfo* getObjectInfo(MtpObjectHandle handle); |
Mike Lockwood | 3e072b3 | 2010-06-10 16:34:20 -0400 | [diff] [blame] | 87 | void* getThumbnail(MtpObjectHandle handle, int& outLength); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 88 | MtpObjectHandle sendObjectInfo(MtpObjectInfo* info); |
| 89 | bool sendObject(MtpObjectInfo* info, int srcFD); |
Mike Lockwood | 6afc41d | 2010-06-11 16:34:52 -0400 | [diff] [blame] | 90 | bool deleteObject(MtpObjectHandle handle); |
| 91 | MtpObjectHandle getParent(MtpObjectHandle handle); |
| 92 | MtpObjectHandle getStorageID(MtpObjectHandle handle); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 93 | |
Mike Lockwood | 98693f6 | 2010-12-07 10:58:56 -0800 | [diff] [blame] | 94 | MtpObjectPropertyList* getObjectPropsSupported(MtpObjectFormat format); |
| 95 | |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 96 | MtpProperty* getDevicePropDesc(MtpDeviceProperty code); |
Mike Lockwood | 99e393a | 2010-12-07 18:53:04 -0800 | [diff] [blame] | 97 | MtpProperty* getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format); |
Mike Lockwood | a6c490b | 2010-06-05 22:45:01 -0400 | [diff] [blame] | 98 | |
Daichi Hirono | 4fd9a8b | 2015-08-20 15:13:40 +0900 | [diff] [blame^] | 99 | bool readObject(MtpObjectHandle handle, ReadObjectCallback callback, |
Mike Lockwood | ab06384 | 2014-11-12 14:20:06 -0800 | [diff] [blame] | 100 | size_t objectSize, void* clientData); |
Mike Lockwood | 23f1b33 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 101 | bool readObject(MtpObjectHandle handle, const char* destPath, int group, |
Mike Lockwood | 27afe3a | 2010-11-19 13:52:20 -0500 | [diff] [blame] | 102 | int perm); |
Tomasz Mikolajewski | 025ffd9 | 2015-08-04 18:38:31 +0900 | [diff] [blame] | 103 | bool readObject(MtpObjectHandle handle, int fd); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 104 | |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 105 | private: |
Daichi Hirono | 4fd9a8b | 2015-08-20 15:13:40 +0900 | [diff] [blame^] | 106 | // If |objectSize| is not NULL, it checks object size before reading data bytes. |
| 107 | bool readObjectInternal(MtpObjectHandle handle, ReadObjectCallback callback, |
| 108 | const size_t* objectSize, void* clientData); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 109 | bool sendRequest(MtpOperationCode operation); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 110 | bool sendData(); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 111 | bool readData(); |
Mike Lockwood | 0cf89f2 | 2010-07-26 20:40:45 -0400 | [diff] [blame] | 112 | bool writeDataHeader(MtpOperationCode operation, int dataLength); |
Mike Lockwood | 5ed68d2 | 2010-05-25 19:08:48 -0400 | [diff] [blame] | 113 | MtpResponseCode readResponse(); |
| 114 | |
| 115 | }; |
| 116 | |
| 117 | }; // namespace android |
| 118 | |
| 119 | #endif // _MTP_DEVICE_H |