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