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" 23 #include "MtpEventPacket.h" 24 #include "mtp.h" 25 #include "MtpUtils.h" 26 27 #include <utils/threads.h> 28 29 namespace android { 30 31 class MtpDatabase; 32 class MtpStorage; 33 34 class MtpServer { 35 36 private: 37 // file descriptor for MTP kernel driver 38 int mFD; 39 40 MtpDatabase* mDatabase; 41 42 // appear as a PTP device 43 bool mPtp; 44 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 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; 59 MtpEventPacket mEvent; 60 61 MtpStorageList mStorages; 62 63 // handle for new object, set by SendObjectInfo and used by SendObject 64 MtpObjectHandle mSendObjectHandle; 65 MtpObjectFormat mSendObjectFormat; 66 MtpString mSendObjectFilePath; 67 size_t mSendObjectFileSize; 68 69 Mutex mMutex; 70 71 // represents an MTP object that is being edited using the android extensions 72 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject) 73 class ObjectEdit { 74 public: 75 MtpObjectHandle mHandle; 76 MtpString mPath; 77 uint64_t mSize; 78 MtpObjectFormat mFormat; 79 int mFD; 80 ObjectEdit(MtpObjectHandle handle,const char * path,uint64_t size,MtpObjectFormat format,int fd)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 ~ObjectEdit()86 virtual ~ObjectEdit() { 87 close(mFD); 88 } 89 }; 90 Vector<ObjectEdit*> mObjectEditList; 91 92 public: 93 MtpServer(int fd, MtpDatabase* database, bool ptp, 94 int fileGroup, int filePerm, int directoryPerm); 95 virtual ~MtpServer(); 96 97 MtpStorage* getStorage(MtpStorageID id); hasStorage()98 inline bool hasStorage() { return mStorages.size() > 0; } 99 bool hasStorage(MtpStorageID id); 100 void addStorage(MtpStorage* storage); 101 void removeStorage(MtpStorage* storage); 102 103 void run(); 104 105 void sendObjectAdded(MtpObjectHandle handle); 106 void sendObjectRemoved(MtpObjectHandle handle); 107 108 private: 109 void sendStoreAdded(MtpStorageID id); 110 void sendStoreRemoved(MtpStorageID id); 111 void sendEvent(MtpEventCode code, uint32_t param1); 112 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 119 bool handleRequest(); 120 121 MtpResponseCode doGetDeviceInfo(); 122 MtpResponseCode doOpenSession(); 123 MtpResponseCode doCloseSession(); 124 MtpResponseCode doGetStorageIDs(); 125 MtpResponseCode doGetStorageInfo(); 126 MtpResponseCode doGetObjectPropsSupported(); 127 MtpResponseCode doGetObjectHandles(); 128 MtpResponseCode doGetNumObjects(); 129 MtpResponseCode doGetObjectReferences(); 130 MtpResponseCode doSetObjectReferences(); 131 MtpResponseCode doGetObjectPropValue(); 132 MtpResponseCode doSetObjectPropValue(); 133 MtpResponseCode doGetDevicePropValue(); 134 MtpResponseCode doSetDevicePropValue(); 135 MtpResponseCode doResetDevicePropValue(); 136 MtpResponseCode doGetObjectPropList(); 137 MtpResponseCode doGetObjectInfo(); 138 MtpResponseCode doGetObject(); 139 MtpResponseCode doGetThumb(); 140 MtpResponseCode doGetPartialObject(MtpOperationCode operation); 141 MtpResponseCode doSendObjectInfo(); 142 MtpResponseCode doSendObject(); 143 MtpResponseCode doDeleteObject(); 144 MtpResponseCode doGetObjectPropDesc(); 145 MtpResponseCode doGetDevicePropDesc(); 146 MtpResponseCode doSendPartialObject(); 147 MtpResponseCode doTruncateObject(); 148 MtpResponseCode doBeginEditObject(); 149 MtpResponseCode doEndEditObject(); 150 }; 151 152 }; // namespace android 153 154 #endif // _MTP_SERVER_H 155