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