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 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 } 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); 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 void sendDevicePropertyChanged(MtpDeviceProperty property); 108 109 private: 110 void sendStoreAdded(MtpStorageID id); 111 void sendStoreRemoved(MtpStorageID id); 112 void sendEvent(MtpEventCode code, uint32_t param1); 113 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 120 bool handleRequest(); 121 122 MtpResponseCode doGetDeviceInfo(); 123 MtpResponseCode doOpenSession(); 124 MtpResponseCode doCloseSession(); 125 MtpResponseCode doGetStorageIDs(); 126 MtpResponseCode doGetStorageInfo(); 127 MtpResponseCode doGetObjectPropsSupported(); 128 MtpResponseCode doGetObjectHandles(); 129 MtpResponseCode doGetNumObjects(); 130 MtpResponseCode doGetObjectReferences(); 131 MtpResponseCode doSetObjectReferences(); 132 MtpResponseCode doGetObjectPropValue(); 133 MtpResponseCode doSetObjectPropValue(); 134 MtpResponseCode doGetDevicePropValue(); 135 MtpResponseCode doSetDevicePropValue(); 136 MtpResponseCode doResetDevicePropValue(); 137 MtpResponseCode doGetObjectPropList(); 138 MtpResponseCode doGetObjectInfo(); 139 MtpResponseCode doGetObject(); 140 MtpResponseCode doGetThumb(); 141 MtpResponseCode doGetPartialObject(MtpOperationCode operation); 142 MtpResponseCode doSendObjectInfo(); 143 MtpResponseCode doSendObject(); 144 MtpResponseCode doDeleteObject(); 145 MtpResponseCode doGetObjectPropDesc(); 146 MtpResponseCode doGetDevicePropDesc(); 147 MtpResponseCode doSendPartialObject(); 148 MtpResponseCode doTruncateObject(); 149 MtpResponseCode doBeginEditObject(); 150 MtpResponseCode doEndEditObject(); 151 }; 152 153 }; // namespace android 154 155 #endif // _MTP_SERVER_H 156