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