blob: af371eb5b6ad381947c9e3b9e9c1d68f468b06ae [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
Jerry Zhange5aa05d2017-10-13 12:14:42 -070035class IMtpDatabase;
Mike Lockwood1865a5d2010-07-03 00:44:05 -040036class MtpStorage;
Mike Lockwood16864ba2010-05-11 17:16:59 -040037
38class MtpServer {
39
40private:
Jerry Zhange5aa05d2017-10-13 12:14:42 -070041 IMtpDatabase* 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
Alex Klyubin792298f2016-12-21 11:20:22 -080046 // Manufacturer to report in DeviceInfo
47 MtpString mDeviceInfoManufacturer;
48 // Model to report in DeviceInfo
49 MtpString mDeviceInfoModel;
50 // Device version to report in DeviceInfo
51 MtpString mDeviceInfoDeviceVersion;
52 // Serial number to report in DeviceInfo
53 MtpString mDeviceInfoSerialNumber;
54
Mike Lockwood16864ba2010-05-11 17:16:59 -040055 // 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;
Jerry Zhang487be612016-10-24 12:10:41 -070063
Mike Lockwood873871f2010-07-12 18:54:16 -040064 MtpEventPacket mEvent;
Mike Lockwood16864ba2010-05-11 17:16:59 -040065
66 MtpStorageList mStorages;
67
Jerry Zhang487be612016-10-24 12:10:41 -070068 static IMtpHandle* sHandle;
69
Mike Lockwood16864ba2010-05-11 17:16:59 -040070 // handle for new object, set by SendObjectInfo and used by SendObject
71 MtpObjectHandle mSendObjectHandle;
Mike Lockwood4714b072010-07-12 08:49:01 -040072 MtpObjectFormat mSendObjectFormat;
Mike Lockwood16864ba2010-05-11 17:16:59 -040073 MtpString mSendObjectFilePath;
74 size_t mSendObjectFileSize;
caozhiyuan854cb172017-04-26 16:52:30 +080075 time_t mSendObjectModifiedTime;
Mike Lockwood16864ba2010-05-11 17:16:59 -040076
Mike Lockwooda8494402011-02-18 09:07:14 -050077 Mutex mMutex;
78
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070079 // represents an MTP object that is being edited using the android extensions
80 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -070081 class ObjectEdit {
82 public:
83 MtpObjectHandle mHandle;
84 MtpString mPath;
85 uint64_t mSize;
86 MtpObjectFormat mFormat;
87 int mFD;
88
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
94 virtual ~ObjectEdit() {
95 close(mFD);
96 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070097 };
98 Vector<ObjectEdit*> mObjectEditList;
99
Mike Lockwood16864ba2010-05-11 17:16:59 -0400100public:
Jerry Zhange5aa05d2017-10-13 12:14:42 -0700101 MtpServer(IMtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800102 const MtpString& deviceInfoManufacturer,
103 const MtpString& deviceInfoModel,
104 const MtpString& deviceInfoDeviceVersion,
105 const MtpString& deviceInfoSerialNumber);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400106 virtual ~MtpServer();
107
Mike Lockwood30adaaf2011-04-05 10:21:27 -0400108 MtpStorage* getStorage(MtpStorageID id);
109 inline bool hasStorage() { return mStorages.size() > 0; }
110 bool hasStorage(MtpStorageID id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500111 void addStorage(MtpStorage* storage);
112 void removeStorage(MtpStorage* storage);
113
Jerry Zhang487be612016-10-24 12:10:41 -0700114 static int configure(bool usePtp);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400115 void run();
116
Mike Lockwood873871f2010-07-12 18:54:16 -0400117 void sendObjectAdded(MtpObjectHandle handle);
118 void sendObjectRemoved(MtpObjectHandle handle);
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800119 void sendDevicePropertyChanged(MtpDeviceProperty property);
Mike Lockwood873871f2010-07-12 18:54:16 -0400120
Mike Lockwood16864ba2010-05-11 17:16:59 -0400121private:
Mike Lockwooda8494402011-02-18 09:07:14 -0500122 void sendStoreAdded(MtpStorageID id);
123 void sendStoreRemoved(MtpStorageID id);
124 void sendEvent(MtpEventCode code, uint32_t param1);
125
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700126 void addEditObject(MtpObjectHandle handle, MtpString& 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
Mike Lockwood916076c2010-06-04 09:49:21 -0400132 bool handleRequest();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400133
134 MtpResponseCode doGetDeviceInfo();
135 MtpResponseCode doOpenSession();
136 MtpResponseCode doCloseSession();
137 MtpResponseCode doGetStorageIDs();
138 MtpResponseCode doGetStorageInfo();
139 MtpResponseCode doGetObjectPropsSupported();
140 MtpResponseCode doGetObjectHandles();
Mike Lockwood343af4e2010-08-02 10:52:20 -0400141 MtpResponseCode doGetNumObjects();
Mike Lockwood438344f2010-08-03 15:30:09 -0400142 MtpResponseCode doGetObjectReferences();
143 MtpResponseCode doSetObjectReferences();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400144 MtpResponseCode doGetObjectPropValue();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400145 MtpResponseCode doSetObjectPropValue();
146 MtpResponseCode doGetDevicePropValue();
147 MtpResponseCode doSetDevicePropValue();
148 MtpResponseCode doResetDevicePropValue();
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400149 MtpResponseCode doGetObjectPropList();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400150 MtpResponseCode doGetObjectInfo();
151 MtpResponseCode doGetObject();
Mike Lockwood64000782011-04-24 18:40:17 -0700152 MtpResponseCode doGetThumb();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700153 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400154 MtpResponseCode doSendObjectInfo();
155 MtpResponseCode doSendObject();
156 MtpResponseCode doDeleteObject();
Jerry Zhang708b3e02017-09-26 17:53:39 -0700157 MtpResponseCode doMoveObject();
158 MtpResponseCode doCopyObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400159 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400160 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700161 MtpResponseCode doSendPartialObject();
162 MtpResponseCode doTruncateObject();
163 MtpResponseCode doBeginEditObject();
164 MtpResponseCode doEndEditObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400165};
166
Mike Lockwood7850ef92010-05-14 10:10:36 -0400167}; // namespace android
168
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169#endif // _MTP_SERVER_H