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