Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -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_SERVER_H |
| 18 | #define _MTP_SERVER_H |
| 19 | |
| 20 | #include "MtpRequestPacket.h" |
| 21 | #include "MtpDataPacket.h" |
| 22 | #include "MtpResponsePacket.h" |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 23 | #include "MtpEventPacket.h" |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 24 | #include "MtpStringBuffer.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 25 | #include "mtp.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 26 | #include "MtpUtils.h" |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 27 | #include "IMtpHandle.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 28 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <mutex> |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 31 | #include <queue> |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 32 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Jerry Zhang | e5aa05d | 2017-10-13 12:14:42 -0700 | [diff] [blame] | 35 | class IMtpDatabase; |
Mike Lockwood | 1865a5d | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 36 | class MtpStorage; |
Grant Hernandez | 76fc7cb | 2019-07-01 15:01:43 -0700 | [diff] [blame] | 37 | class MtpMockServer; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 38 | |
| 39 | class MtpServer { |
Grant Hernandez | 76fc7cb | 2019-07-01 15:01:43 -0700 | [diff] [blame] | 40 | // libFuzzer testing |
| 41 | friend class MtpMockServer; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 42 | |
| 43 | private: |
Jerry Zhang | e5aa05d | 2017-10-13 12:14:42 -0700 | [diff] [blame] | 44 | IMtpDatabase* mDatabase; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 45 | |
Mike Lockwood | 3d1d776 | 2011-06-21 08:27:06 -0400 | [diff] [blame] | 46 | // appear as a PTP device |
| 47 | bool mPtp; |
| 48 | |
Alex Klyubin | 792298f | 2016-12-21 11:20:22 -0800 | [diff] [blame] | 49 | // Manufacturer to report in DeviceInfo |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 50 | MtpStringBuffer mDeviceInfoManufacturer; |
Alex Klyubin | 792298f | 2016-12-21 11:20:22 -0800 | [diff] [blame] | 51 | // Model to report in DeviceInfo |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 52 | MtpStringBuffer mDeviceInfoModel; |
Alex Klyubin | 792298f | 2016-12-21 11:20:22 -0800 | [diff] [blame] | 53 | // Device version to report in DeviceInfo |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 54 | MtpStringBuffer mDeviceInfoDeviceVersion; |
Alex Klyubin | 792298f | 2016-12-21 11:20:22 -0800 | [diff] [blame] | 55 | // Serial number to report in DeviceInfo |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 56 | MtpStringBuffer mDeviceInfoSerialNumber; |
Alex Klyubin | 792298f | 2016-12-21 11:20:22 -0800 | [diff] [blame] | 57 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 58 | // current session ID |
| 59 | MtpSessionID mSessionID; |
| 60 | // true if we have an open session and mSessionID is valid |
| 61 | bool mSessionOpen; |
| 62 | |
| 63 | MtpRequestPacket mRequest; |
| 64 | MtpDataPacket mData; |
| 65 | MtpResponsePacket mResponse; |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 66 | |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 67 | MtpEventPacket mEvent; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 68 | |
| 69 | MtpStorageList mStorages; |
| 70 | |
Jerry Zhang | 63dac45 | 2017-12-06 15:19:36 -0800 | [diff] [blame] | 71 | IMtpHandle* mHandle; |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 72 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 73 | // handle for new object, set by SendObjectInfo and used by SendObject |
| 74 | MtpObjectHandle mSendObjectHandle; |
Mike Lockwood | 4714b07 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 75 | MtpObjectFormat mSendObjectFormat; |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 76 | MtpStringBuffer mSendObjectFilePath; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 77 | size_t mSendObjectFileSize; |
caozhiyuan | 854cb17 | 2017-04-26 16:52:30 +0800 | [diff] [blame] | 78 | time_t mSendObjectModifiedTime; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 79 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 80 | std::mutex mMutex; |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 81 | |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 82 | // represents an MTP object that is being edited using the android extensions |
| 83 | // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject) |
Mike Lockwood | c3f16e5 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 84 | class ObjectEdit { |
| 85 | public: |
| 86 | MtpObjectHandle mHandle; |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 87 | MtpStringBuffer mPath; |
Mike Lockwood | c3f16e5 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 88 | uint64_t mSize; |
| 89 | MtpObjectFormat mFormat; |
| 90 | int mFD; |
| 91 | |
| 92 | ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size, |
| 93 | MtpObjectFormat format, int fd) |
| 94 | : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) { |
| 95 | } |
| 96 | |
| 97 | virtual ~ObjectEdit() { |
| 98 | close(mFD); |
| 99 | } |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 100 | }; |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 101 | std::vector<ObjectEdit*> mObjectEditList; |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 102 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 103 | public: |
Jerry Zhang | 63dac45 | 2017-12-06 15:19:36 -0800 | [diff] [blame] | 104 | MtpServer(IMtpDatabase* database, int controlFd, bool ptp, |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 105 | const char *deviceInfoManufacturer, |
| 106 | const char *deviceInfoModel, |
| 107 | const char *deviceInfoDeviceVersion, |
| 108 | const char *deviceInfoSerialNumber); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 109 | virtual ~MtpServer(); |
| 110 | |
Mike Lockwood | 30adaaf | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 111 | MtpStorage* getStorage(MtpStorageID id); |
| 112 | inline bool hasStorage() { return mStorages.size() > 0; } |
| 113 | bool hasStorage(MtpStorageID id); |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 114 | void addStorage(MtpStorage* storage); |
| 115 | void removeStorage(MtpStorage* storage); |
| 116 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 117 | void run(); |
| 118 | |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 119 | void sendObjectAdded(MtpObjectHandle handle); |
| 120 | void sendObjectRemoved(MtpObjectHandle handle); |
James | 5513843 | 2018-07-02 18:07:30 +0800 | [diff] [blame] | 121 | void sendObjectInfoChanged(MtpObjectHandle handle); |
Mike Lockwood | 0fa848d | 2014-03-07 13:29:59 -0800 | [diff] [blame] | 122 | void sendDevicePropertyChanged(MtpDeviceProperty property); |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 123 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 124 | private: |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 125 | void sendStoreAdded(MtpStorageID id); |
| 126 | void sendStoreRemoved(MtpStorageID id); |
| 127 | void sendEvent(MtpEventCode code, uint32_t param1); |
| 128 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 129 | void addEditObject(MtpObjectHandle handle, MtpStringBuffer& path, |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 130 | uint64_t size, MtpObjectFormat format, int fd); |
| 131 | ObjectEdit* getEditObject(MtpObjectHandle handle); |
| 132 | void removeEditObject(MtpObjectHandle handle); |
| 133 | void commitEdit(ObjectEdit* edit); |
| 134 | |
Mike Lockwood | 916076c | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 135 | bool handleRequest(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 136 | |
| 137 | MtpResponseCode doGetDeviceInfo(); |
| 138 | MtpResponseCode doOpenSession(); |
| 139 | MtpResponseCode doCloseSession(); |
| 140 | MtpResponseCode doGetStorageIDs(); |
| 141 | MtpResponseCode doGetStorageInfo(); |
| 142 | MtpResponseCode doGetObjectPropsSupported(); |
| 143 | MtpResponseCode doGetObjectHandles(); |
Mike Lockwood | 343af4e | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 144 | MtpResponseCode doGetNumObjects(); |
Mike Lockwood | 438344f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 145 | MtpResponseCode doGetObjectReferences(); |
| 146 | MtpResponseCode doSetObjectReferences(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 147 | MtpResponseCode doGetObjectPropValue(); |
Mike Lockwood | 8277cec | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 148 | MtpResponseCode doSetObjectPropValue(); |
| 149 | MtpResponseCode doGetDevicePropValue(); |
| 150 | MtpResponseCode doSetDevicePropValue(); |
| 151 | MtpResponseCode doResetDevicePropValue(); |
Mike Lockwood | b6da06e | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 152 | MtpResponseCode doGetObjectPropList(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 153 | MtpResponseCode doGetObjectInfo(); |
| 154 | MtpResponseCode doGetObject(); |
Mike Lockwood | 6400078 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 155 | MtpResponseCode doGetThumb(); |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 156 | MtpResponseCode doGetPartialObject(MtpOperationCode operation); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 157 | MtpResponseCode doSendObjectInfo(); |
| 158 | MtpResponseCode doSendObject(); |
| 159 | MtpResponseCode doDeleteObject(); |
Jerry Zhang | 708b3e0 | 2017-09-26 17:53:39 -0700 | [diff] [blame] | 160 | MtpResponseCode doMoveObject(); |
| 161 | MtpResponseCode doCopyObject(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 162 | MtpResponseCode doGetObjectPropDesc(); |
Mike Lockwood | 8277cec | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 163 | MtpResponseCode doGetDevicePropDesc(); |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 164 | MtpResponseCode doSendPartialObject(); |
| 165 | MtpResponseCode doTruncateObject(); |
| 166 | MtpResponseCode doBeginEditObject(); |
| 167 | MtpResponseCode doEndEditObject(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 168 | }; |
| 169 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 170 | }; // namespace android |
| 171 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 172 | #endif // _MTP_SERVER_H |