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 #include "IMtpHandle.h"
27 
28 #include <utils/threads.h>
29 #include <queue>
30 #include <memory>
31 #include <mutex>
32 
33 namespace android {
34 
35 class MtpDatabase;
36 class MtpStorage;
37 
38 class MtpServer {
39 
40 private:
41     MtpDatabase*        mDatabase;
42 
43     // appear as a PTP device
44     bool                mPtp;
45 
46     // group to own new files and folders
47     int                 mFileGroup;
48     // permissions for new files and directories
49     int                 mFilePermission;
50     int                 mDirectoryPermission;
51 
52     // Manufacturer to report in DeviceInfo
53     MtpString           mDeviceInfoManufacturer;
54     // Model to report in DeviceInfo
55     MtpString           mDeviceInfoModel;
56     // Device version to report in DeviceInfo
57     MtpString           mDeviceInfoDeviceVersion;
58     // Serial number to report in DeviceInfo
59     MtpString           mDeviceInfoSerialNumber;
60 
61     // current session ID
62     MtpSessionID        mSessionID;
63     // true if we have an open session and mSessionID is valid
64     bool                mSessionOpen;
65 
66     MtpRequestPacket    mRequest;
67     MtpDataPacket       mData;
68     MtpResponsePacket   mResponse;
69 
70     MtpEventPacket      mEvent;
71 
72     MtpStorageList      mStorages;
73 
74     static IMtpHandle*  sHandle;
75 
76     // handle for new object, set by SendObjectInfo and used by SendObject
77     MtpObjectHandle     mSendObjectHandle;
78     MtpObjectFormat     mSendObjectFormat;
79     MtpString           mSendObjectFilePath;
80     size_t              mSendObjectFileSize;
81 
82     Mutex               mMutex;
83 
84     // represents an MTP object that is being edited using the android extensions
85     // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
86     class ObjectEdit {
87         public:
88         MtpObjectHandle     mHandle;
89         MtpString           mPath;
90         uint64_t            mSize;
91         MtpObjectFormat     mFormat;
92         int                 mFD;
93 
ObjectEdit(MtpObjectHandle handle,const char * path,uint64_t size,MtpObjectFormat format,int fd)94         ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
95             MtpObjectFormat format, int fd)
96                 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
97             }
98 
~ObjectEdit()99         virtual ~ObjectEdit() {
100             close(mFD);
101         }
102     };
103     Vector<ObjectEdit*>  mObjectEditList;
104 
105 public:
106                         MtpServer(MtpDatabase* database, bool ptp,
107                                     int fileGroup, int filePerm, int directoryPerm,
108                                     const MtpString& deviceInfoManufacturer,
109                                     const MtpString& deviceInfoModel,
110                                     const MtpString& deviceInfoDeviceVersion,
111                                     const MtpString& deviceInfoSerialNumber);
112     virtual             ~MtpServer();
113 
114     MtpStorage*         getStorage(MtpStorageID id);
hasStorage()115     inline bool         hasStorage() { return mStorages.size() > 0; }
116     bool                hasStorage(MtpStorageID id);
117     void                addStorage(MtpStorage* storage);
118     void                removeStorage(MtpStorage* storage);
119 
120     static int          configure(bool usePtp);
121     void                run();
122 
123     void                sendObjectAdded(MtpObjectHandle handle);
124     void                sendObjectRemoved(MtpObjectHandle handle);
125     void                sendDevicePropertyChanged(MtpDeviceProperty property);
126 
127 private:
128     void                sendStoreAdded(MtpStorageID id);
129     void                sendStoreRemoved(MtpStorageID id);
130     void                sendEvent(MtpEventCode code, uint32_t param1);
131 
132     void                addEditObject(MtpObjectHandle handle, MtpString& path,
133                                 uint64_t size, MtpObjectFormat format, int fd);
134     ObjectEdit*         getEditObject(MtpObjectHandle handle);
135     void                removeEditObject(MtpObjectHandle handle);
136     void                commitEdit(ObjectEdit* edit);
137 
138     bool                handleRequest();
139 
140     MtpResponseCode     doGetDeviceInfo();
141     MtpResponseCode     doOpenSession();
142     MtpResponseCode     doCloseSession();
143     MtpResponseCode     doGetStorageIDs();
144     MtpResponseCode     doGetStorageInfo();
145     MtpResponseCode     doGetObjectPropsSupported();
146     MtpResponseCode     doGetObjectHandles();
147     MtpResponseCode     doGetNumObjects();
148     MtpResponseCode     doGetObjectReferences();
149     MtpResponseCode     doSetObjectReferences();
150     MtpResponseCode     doGetObjectPropValue();
151     MtpResponseCode     doSetObjectPropValue();
152     MtpResponseCode     doGetDevicePropValue();
153     MtpResponseCode     doSetDevicePropValue();
154     MtpResponseCode     doResetDevicePropValue();
155     MtpResponseCode     doGetObjectPropList();
156     MtpResponseCode     doGetObjectInfo();
157     MtpResponseCode     doGetObject();
158     MtpResponseCode     doGetThumb();
159     MtpResponseCode     doGetPartialObject(MtpOperationCode operation);
160     MtpResponseCode     doSendObjectInfo();
161     MtpResponseCode     doSendObject();
162     MtpResponseCode     doDeleteObject();
163     MtpResponseCode     doGetObjectPropDesc();
164     MtpResponseCode     doGetDevicePropDesc();
165     MtpResponseCode     doSendPartialObject();
166     MtpResponseCode     doTruncateObject();
167     MtpResponseCode     doBeginEditObject();
168     MtpResponseCode     doEndEditObject();
169 };
170 
171 }; // namespace android
172 
173 #endif // _MTP_SERVER_H
174