blob: 08a9e4a4b4c60af97d7362724750c82ccd34bba2 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
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"
Mike Lockwood873871f2010-07-12 18:54:16 -040023#include "MtpEventPacket.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040024#include "mtp.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040025#include "MtpUtils.h"
Jerry Zhang487be612016-10-24 12:10:41 -070026#include "IMtpHandle.h"
Mike Lockwood16864ba2010-05-11 17:16:59 -040027
Mike Lockwooda8494402011-02-18 09:07:14 -050028#include <utils/threads.h>
Jerry Zhang487be612016-10-24 12:10:41 -070029#include <queue>
30#include <memory>
31#include <mutex>
Mike Lockwooda8494402011-02-18 09:07:14 -050032
Mike Lockwood7850ef92010-05-14 10:10:36 -040033namespace android {
34
Mike Lockwood1865a5d2010-07-03 00:44:05 -040035class MtpDatabase;
Mike Lockwood1865a5d2010-07-03 00:44:05 -040036class MtpStorage;
Mike Lockwood16864ba2010-05-11 17:16:59 -040037
38class MtpServer {
39
40private:
Mike Lockwood1865a5d2010-07-03 00:44:05 -040041 MtpDatabase* mDatabase;
Mike Lockwood16864ba2010-05-11 17:16:59 -040042
Mike Lockwood3d1d7762011-06-21 08:27:06 -040043 // appear as a PTP device
44 bool mPtp;
45
Mike Lockwood8e2a2802010-07-02 15:15:07 -040046 // group to own new files and folders
47 int mFileGroup;
48 // permissions for new files and directories
49 int mFilePermission;
50 int mDirectoryPermission;
51
Alex Klyubin792298f2016-12-21 11:20:22 -080052 // 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
Mike Lockwood16864ba2010-05-11 17:16:59 -040061 // 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;
Jerry Zhang487be612016-10-24 12:10:41 -070069
Mike Lockwood873871f2010-07-12 18:54:16 -040070 MtpEventPacket mEvent;
Mike Lockwood16864ba2010-05-11 17:16:59 -040071
72 MtpStorageList mStorages;
73
Jerry Zhang487be612016-10-24 12:10:41 -070074 static IMtpHandle* sHandle;
75
Mike Lockwood16864ba2010-05-11 17:16:59 -040076 // handle for new object, set by SendObjectInfo and used by SendObject
77 MtpObjectHandle mSendObjectHandle;
Mike Lockwood4714b072010-07-12 08:49:01 -040078 MtpObjectFormat mSendObjectFormat;
Mike Lockwood16864ba2010-05-11 17:16:59 -040079 MtpString mSendObjectFilePath;
80 size_t mSendObjectFileSize;
caozhiyuan854cb172017-04-26 16:52:30 +080081 time_t mSendObjectModifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -040082
Mike Lockwooda8494402011-02-18 09:07:14 -050083 Mutex mMutex;
84
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070085 // represents an MTP object that is being edited using the android extensions
86 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -070087 class ObjectEdit {
88 public:
89 MtpObjectHandle mHandle;
90 MtpString mPath;
91 uint64_t mSize;
92 MtpObjectFormat mFormat;
93 int mFD;
94
95 ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
96 MtpObjectFormat format, int fd)
97 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
98 }
99
100 virtual ~ObjectEdit() {
101 close(mFD);
102 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700103 };
104 Vector<ObjectEdit*> mObjectEditList;
105
Mike Lockwood16864ba2010-05-11 17:16:59 -0400106public:
Jerry Zhang487be612016-10-24 12:10:41 -0700107 MtpServer(MtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800108 int fileGroup, int filePerm, int directoryPerm,
109 const MtpString& deviceInfoManufacturer,
110 const MtpString& deviceInfoModel,
111 const MtpString& deviceInfoDeviceVersion,
112 const MtpString& deviceInfoSerialNumber);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400113 virtual ~MtpServer();
114
Mike Lockwood30adaaf2011-04-05 10:21:27 -0400115 MtpStorage* getStorage(MtpStorageID id);
116 inline bool hasStorage() { return mStorages.size() > 0; }
117 bool hasStorage(MtpStorageID id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500118 void addStorage(MtpStorage* storage);
119 void removeStorage(MtpStorage* storage);
120
Jerry Zhang487be612016-10-24 12:10:41 -0700121 static int configure(bool usePtp);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400122 void run();
123
Mike Lockwood873871f2010-07-12 18:54:16 -0400124 void sendObjectAdded(MtpObjectHandle handle);
125 void sendObjectRemoved(MtpObjectHandle handle);
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800126 void sendDevicePropertyChanged(MtpDeviceProperty property);
Mike Lockwood873871f2010-07-12 18:54:16 -0400127
Mike Lockwood16864ba2010-05-11 17:16:59 -0400128private:
Mike Lockwooda8494402011-02-18 09:07:14 -0500129 void sendStoreAdded(MtpStorageID id);
130 void sendStoreRemoved(MtpStorageID id);
131 void sendEvent(MtpEventCode code, uint32_t param1);
132
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700133 void addEditObject(MtpObjectHandle handle, MtpString& path,
134 uint64_t size, MtpObjectFormat format, int fd);
135 ObjectEdit* getEditObject(MtpObjectHandle handle);
136 void removeEditObject(MtpObjectHandle handle);
137 void commitEdit(ObjectEdit* edit);
138
Mike Lockwood916076c2010-06-04 09:49:21 -0400139 bool handleRequest();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400140
141 MtpResponseCode doGetDeviceInfo();
142 MtpResponseCode doOpenSession();
143 MtpResponseCode doCloseSession();
144 MtpResponseCode doGetStorageIDs();
145 MtpResponseCode doGetStorageInfo();
146 MtpResponseCode doGetObjectPropsSupported();
147 MtpResponseCode doGetObjectHandles();
Mike Lockwood343af4e2010-08-02 10:52:20 -0400148 MtpResponseCode doGetNumObjects();
Mike Lockwood438344f2010-08-03 15:30:09 -0400149 MtpResponseCode doGetObjectReferences();
150 MtpResponseCode doSetObjectReferences();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400151 MtpResponseCode doGetObjectPropValue();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400152 MtpResponseCode doSetObjectPropValue();
153 MtpResponseCode doGetDevicePropValue();
154 MtpResponseCode doSetDevicePropValue();
155 MtpResponseCode doResetDevicePropValue();
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400156 MtpResponseCode doGetObjectPropList();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400157 MtpResponseCode doGetObjectInfo();
158 MtpResponseCode doGetObject();
Mike Lockwood64000782011-04-24 18:40:17 -0700159 MtpResponseCode doGetThumb();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700160 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400161 MtpResponseCode doSendObjectInfo();
162 MtpResponseCode doSendObject();
163 MtpResponseCode doDeleteObject();
164 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400165 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700166 MtpResponseCode doSendPartialObject();
167 MtpResponseCode doTruncateObject();
168 MtpResponseCode doBeginEditObject();
169 MtpResponseCode doEndEditObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400170};
171
Mike Lockwood7850ef92010-05-14 10:10:36 -0400172}; // namespace android
173
Mike Lockwood16864ba2010-05-11 17:16:59 -0400174#endif // _MTP_SERVER_H