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" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 24 | #include "mtp.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 25 | #include "MtpUtils.h" |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 26 | #include "IMtpHandle.h" |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 27 | |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 28 | #include <utils/threads.h> |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 29 | #include <queue> |
| 30 | #include <memory> |
| 31 | #include <mutex> |
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 | |
Mike Lockwood | 1865a5d | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 35 | class MtpDatabase; |
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: |
Mike Lockwood | 1865a5d | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 41 | MtpDatabase* 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 | |
Mike Lockwood | 8e2a280 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 46 | // group to own new files and folders |
| 47 | int mFileGroup; |
| 48 | // permissions for new files and directories |
| 49 | int mFilePermission; |
| 50 | int mDirectoryPermission; |
| 51 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 52 | // current session ID |
| 53 | MtpSessionID mSessionID; |
| 54 | // true if we have an open session and mSessionID is valid |
| 55 | bool mSessionOpen; |
| 56 | |
| 57 | MtpRequestPacket mRequest; |
| 58 | MtpDataPacket mData; |
| 59 | MtpResponsePacket mResponse; |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 60 | |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 61 | MtpEventPacket mEvent; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 62 | |
| 63 | MtpStorageList mStorages; |
| 64 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 65 | static IMtpHandle* sHandle; |
| 66 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 67 | // handle for new object, set by SendObjectInfo and used by SendObject |
| 68 | MtpObjectHandle mSendObjectHandle; |
Mike Lockwood | 4714b07 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 69 | MtpObjectFormat mSendObjectFormat; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 70 | MtpString mSendObjectFilePath; |
| 71 | size_t mSendObjectFileSize; |
| 72 | |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 73 | Mutex mMutex; |
| 74 | |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 75 | // represents an MTP object that is being edited using the android extensions |
| 76 | // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject) |
Mike Lockwood | c3f16e5 | 2011-04-25 12:56:21 -0700 | [diff] [blame] | 77 | class ObjectEdit { |
| 78 | public: |
| 79 | MtpObjectHandle mHandle; |
| 80 | MtpString mPath; |
| 81 | uint64_t mSize; |
| 82 | MtpObjectFormat mFormat; |
| 83 | int mFD; |
| 84 | |
| 85 | ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size, |
| 86 | MtpObjectFormat format, int fd) |
| 87 | : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) { |
| 88 | } |
| 89 | |
| 90 | virtual ~ObjectEdit() { |
| 91 | close(mFD); |
| 92 | } |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 93 | }; |
| 94 | Vector<ObjectEdit*> mObjectEditList; |
| 95 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 96 | public: |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 97 | MtpServer(MtpDatabase* database, bool ptp, |
Mike Lockwood | 8e2a280 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 98 | int fileGroup, int filePerm, int directoryPerm); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 99 | virtual ~MtpServer(); |
| 100 | |
Mike Lockwood | 30adaaf | 2011-04-05 10:21:27 -0400 | [diff] [blame] | 101 | MtpStorage* getStorage(MtpStorageID id); |
| 102 | inline bool hasStorage() { return mStorages.size() > 0; } |
| 103 | bool hasStorage(MtpStorageID id); |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 104 | void addStorage(MtpStorage* storage); |
| 105 | void removeStorage(MtpStorage* storage); |
| 106 | |
Jerry Zhang | 487be61 | 2016-10-24 12:10:41 -0700 | [diff] [blame] | 107 | static int configure(bool usePtp); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 108 | void run(); |
| 109 | |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 110 | void sendObjectAdded(MtpObjectHandle handle); |
| 111 | void sendObjectRemoved(MtpObjectHandle handle); |
Mike Lockwood | 0fa848d | 2014-03-07 13:29:59 -0800 | [diff] [blame] | 112 | void sendDevicePropertyChanged(MtpDeviceProperty property); |
Mike Lockwood | 873871f | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 113 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 114 | private: |
Mike Lockwood | a849440 | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 115 | void sendStoreAdded(MtpStorageID id); |
| 116 | void sendStoreRemoved(MtpStorageID id); |
| 117 | void sendEvent(MtpEventCode code, uint32_t param1); |
| 118 | |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 119 | void addEditObject(MtpObjectHandle handle, MtpString& path, |
| 120 | uint64_t size, MtpObjectFormat format, int fd); |
| 121 | ObjectEdit* getEditObject(MtpObjectHandle handle); |
| 122 | void removeEditObject(MtpObjectHandle handle); |
| 123 | void commitEdit(ObjectEdit* edit); |
| 124 | |
Mike Lockwood | 916076c | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 125 | bool handleRequest(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 126 | |
| 127 | MtpResponseCode doGetDeviceInfo(); |
| 128 | MtpResponseCode doOpenSession(); |
| 129 | MtpResponseCode doCloseSession(); |
| 130 | MtpResponseCode doGetStorageIDs(); |
| 131 | MtpResponseCode doGetStorageInfo(); |
| 132 | MtpResponseCode doGetObjectPropsSupported(); |
| 133 | MtpResponseCode doGetObjectHandles(); |
Mike Lockwood | 343af4e | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 134 | MtpResponseCode doGetNumObjects(); |
Mike Lockwood | 438344f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 135 | MtpResponseCode doGetObjectReferences(); |
| 136 | MtpResponseCode doSetObjectReferences(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 137 | MtpResponseCode doGetObjectPropValue(); |
Mike Lockwood | 8277cec | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 138 | MtpResponseCode doSetObjectPropValue(); |
| 139 | MtpResponseCode doGetDevicePropValue(); |
| 140 | MtpResponseCode doSetDevicePropValue(); |
| 141 | MtpResponseCode doResetDevicePropValue(); |
Mike Lockwood | b6da06e | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 142 | MtpResponseCode doGetObjectPropList(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 143 | MtpResponseCode doGetObjectInfo(); |
| 144 | MtpResponseCode doGetObject(); |
Mike Lockwood | 6400078 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 145 | MtpResponseCode doGetThumb(); |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 146 | MtpResponseCode doGetPartialObject(MtpOperationCode operation); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 147 | MtpResponseCode doSendObjectInfo(); |
| 148 | MtpResponseCode doSendObject(); |
| 149 | MtpResponseCode doDeleteObject(); |
| 150 | MtpResponseCode doGetObjectPropDesc(); |
Mike Lockwood | 8277cec | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 151 | MtpResponseCode doGetDevicePropDesc(); |
Mike Lockwood | 7d77dcf | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 152 | MtpResponseCode doSendPartialObject(); |
| 153 | MtpResponseCode doTruncateObject(); |
| 154 | MtpResponseCode doBeginEditObject(); |
| 155 | MtpResponseCode doEndEditObject(); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 156 | }; |
| 157 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 158 | }; // namespace android |
| 159 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 160 | #endif // _MTP_SERVER_H |