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