Mike Lockwood | 56118b5 | 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 | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 23 | #include "MtpEventPacket.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 24 | #include "mtp.h" |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 25 | #include "MtpUtils.h" |
| 26 | |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 27 | #include <utils/threads.h> |
| 28 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 31 | class MtpDatabase; |
Mike Lockwood | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 32 | class MtpStorage; |
Mike Lockwood | 56118b5 | 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 | d21eac9 | 2010-07-03 00:44:05 -0400 | [diff] [blame] | 40 | MtpDatabase* mDatabase; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 41 | |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame^] | 42 | // appear as a PTP device |
| 43 | bool mPtp; |
| 44 | |
Mike Lockwood | dad6927 | 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 | 56118b5 | 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 | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 59 | MtpEventPacket mEvent; |
Mike Lockwood | 56118b5 | 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 | d815f79 | 2010-07-12 08:49:01 -0400 | [diff] [blame] | 65 | MtpObjectFormat mSendObjectFormat; |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 66 | MtpString mSendObjectFilePath; |
| 67 | size_t mSendObjectFileSize; |
| 68 | |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 69 | Mutex mMutex; |
| 70 | |
Mike Lockwood | 9df53fae | 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 | 1003199 | 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 | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 89 | }; |
| 90 | Vector<ObjectEdit*> mObjectEditList; |
| 91 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 92 | public: |
Mike Lockwood | 7d40d42 | 2011-06-21 08:27:06 -0400 | [diff] [blame^] | 93 | MtpServer(int fd, MtpDatabase* database, bool ptp, |
Mike Lockwood | dad6927 | 2010-07-02 15:15:07 -0400 | [diff] [blame] | 94 | int fileGroup, int filePerm, int directoryPerm); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 95 | virtual ~MtpServer(); |
| 96 | |
Mike Lockwood | b239b683 | 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 | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 100 | void addStorage(MtpStorage* storage); |
| 101 | void removeStorage(MtpStorage* storage); |
| 102 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 103 | void run(); |
| 104 | |
Mike Lockwood | be125a5 | 2010-07-12 18:54:16 -0400 | [diff] [blame] | 105 | void sendObjectAdded(MtpObjectHandle handle); |
| 106 | void sendObjectRemoved(MtpObjectHandle handle); |
| 107 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 108 | private: |
Mike Lockwood | 467ca0d | 2011-02-18 09:07:14 -0500 | [diff] [blame] | 109 | void sendStoreAdded(MtpStorageID id); |
| 110 | void sendStoreRemoved(MtpStorageID id); |
| 111 | void sendEvent(MtpEventCode code, uint32_t param1); |
| 112 | |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 113 | void addEditObject(MtpObjectHandle handle, MtpString& path, |
| 114 | uint64_t size, MtpObjectFormat format, int fd); |
| 115 | ObjectEdit* getEditObject(MtpObjectHandle handle); |
| 116 | void removeEditObject(MtpObjectHandle handle); |
| 117 | void commitEdit(ObjectEdit* edit); |
| 118 | |
Mike Lockwood | a82d3c5 | 2010-06-04 09:49:21 -0400 | [diff] [blame] | 119 | bool handleRequest(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 120 | |
| 121 | MtpResponseCode doGetDeviceInfo(); |
| 122 | MtpResponseCode doOpenSession(); |
| 123 | MtpResponseCode doCloseSession(); |
| 124 | MtpResponseCode doGetStorageIDs(); |
| 125 | MtpResponseCode doGetStorageInfo(); |
| 126 | MtpResponseCode doGetObjectPropsSupported(); |
| 127 | MtpResponseCode doGetObjectHandles(); |
Mike Lockwood | 7a047c8 | 2010-08-02 10:52:20 -0400 | [diff] [blame] | 128 | MtpResponseCode doGetNumObjects(); |
Mike Lockwood | 9a2046f | 2010-08-03 15:30:09 -0400 | [diff] [blame] | 129 | MtpResponseCode doGetObjectReferences(); |
| 130 | MtpResponseCode doSetObjectReferences(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 131 | MtpResponseCode doGetObjectPropValue(); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 132 | MtpResponseCode doSetObjectPropValue(); |
| 133 | MtpResponseCode doGetDevicePropValue(); |
| 134 | MtpResponseCode doSetDevicePropValue(); |
| 135 | MtpResponseCode doResetDevicePropValue(); |
Mike Lockwood | e2ad6ec | 2010-10-14 18:03:25 -0400 | [diff] [blame] | 136 | MtpResponseCode doGetObjectPropList(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 137 | MtpResponseCode doGetObjectInfo(); |
| 138 | MtpResponseCode doGetObject(); |
Mike Lockwood | c89f222 | 2011-04-24 18:40:17 -0700 | [diff] [blame] | 139 | MtpResponseCode doGetThumb(); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 140 | MtpResponseCode doGetPartialObject(MtpOperationCode operation); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 141 | MtpResponseCode doSendObjectInfo(); |
| 142 | MtpResponseCode doSendObject(); |
| 143 | MtpResponseCode doDeleteObject(); |
| 144 | MtpResponseCode doGetObjectPropDesc(); |
Mike Lockwood | 828d19d | 2010-08-10 15:20:35 -0400 | [diff] [blame] | 145 | MtpResponseCode doGetDevicePropDesc(); |
Mike Lockwood | 9df53fae | 2011-04-21 17:05:55 -0700 | [diff] [blame] | 146 | MtpResponseCode doSendPartialObject(); |
| 147 | MtpResponseCode doTruncateObject(); |
| 148 | MtpResponseCode doBeginEditObject(); |
| 149 | MtpResponseCode doEndEditObject(); |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 150 | }; |
| 151 | |
Mike Lockwood | 8d3257a | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 152 | }; // namespace android |
| 153 | |
Mike Lockwood | 56118b5 | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 154 | #endif // _MTP_SERVER_H |