blob: 64d1b72df21a595dd40a1462bf7e8217a8f0ad26 [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;
81
Mike Lockwooda8494402011-02-18 09:07:14 -050082 Mutex mMutex;
83
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070084 // represents an MTP object that is being edited using the android extensions
85 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -070086 class ObjectEdit {
87 public:
88 MtpObjectHandle mHandle;
89 MtpString mPath;
90 uint64_t mSize;
91 MtpObjectFormat mFormat;
92 int mFD;
93
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
99 virtual ~ObjectEdit() {
100 close(mFD);
101 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700102 };
103 Vector<ObjectEdit*> mObjectEditList;
104
Mike Lockwood16864ba2010-05-11 17:16:59 -0400105public:
Jerry Zhang487be612016-10-24 12:10:41 -0700106 MtpServer(MtpDatabase* database, bool ptp,
Alex Klyubin792298f2016-12-21 11:20:22 -0800107 int fileGroup, int filePerm, int directoryPerm,
108 const MtpString& deviceInfoManufacturer,
109 const MtpString& deviceInfoModel,
110 const MtpString& deviceInfoDeviceVersion,
111 const MtpString& deviceInfoSerialNumber);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400112 virtual ~MtpServer();
113
Mike Lockwood30adaaf2011-04-05 10:21:27 -0400114 MtpStorage* getStorage(MtpStorageID id);
115 inline bool hasStorage() { return mStorages.size() > 0; }
116 bool hasStorage(MtpStorageID id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500117 void addStorage(MtpStorage* storage);
118 void removeStorage(MtpStorage* storage);
119
Jerry Zhang487be612016-10-24 12:10:41 -0700120 static int configure(bool usePtp);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400121 void run();
122
Mike Lockwood873871f2010-07-12 18:54:16 -0400123 void sendObjectAdded(MtpObjectHandle handle);
124 void sendObjectRemoved(MtpObjectHandle handle);
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800125 void sendDevicePropertyChanged(MtpDeviceProperty property);
Mike Lockwood873871f2010-07-12 18:54:16 -0400126
Mike Lockwood16864ba2010-05-11 17:16:59 -0400127private:
Mike Lockwooda8494402011-02-18 09:07:14 -0500128 void sendStoreAdded(MtpStorageID id);
129 void sendStoreRemoved(MtpStorageID id);
130 void sendEvent(MtpEventCode code, uint32_t param1);
131
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700132 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
Mike Lockwood916076c2010-06-04 09:49:21 -0400138 bool handleRequest();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400139
140 MtpResponseCode doGetDeviceInfo();
141 MtpResponseCode doOpenSession();
142 MtpResponseCode doCloseSession();
143 MtpResponseCode doGetStorageIDs();
144 MtpResponseCode doGetStorageInfo();
145 MtpResponseCode doGetObjectPropsSupported();
146 MtpResponseCode doGetObjectHandles();
Mike Lockwood343af4e2010-08-02 10:52:20 -0400147 MtpResponseCode doGetNumObjects();
Mike Lockwood438344f2010-08-03 15:30:09 -0400148 MtpResponseCode doGetObjectReferences();
149 MtpResponseCode doSetObjectReferences();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400150 MtpResponseCode doGetObjectPropValue();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400151 MtpResponseCode doSetObjectPropValue();
152 MtpResponseCode doGetDevicePropValue();
153 MtpResponseCode doSetDevicePropValue();
154 MtpResponseCode doResetDevicePropValue();
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400155 MtpResponseCode doGetObjectPropList();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400156 MtpResponseCode doGetObjectInfo();
157 MtpResponseCode doGetObject();
Mike Lockwood64000782011-04-24 18:40:17 -0700158 MtpResponseCode doGetThumb();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700159 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400160 MtpResponseCode doSendObjectInfo();
161 MtpResponseCode doSendObject();
162 MtpResponseCode doDeleteObject();
163 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400164 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700165 MtpResponseCode doSendPartialObject();
166 MtpResponseCode doTruncateObject();
167 MtpResponseCode doBeginEditObject();
168 MtpResponseCode doEndEditObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400169};
170
Mike Lockwood7850ef92010-05-14 10:10:36 -0400171}; // namespace android
172
Mike Lockwood16864ba2010-05-11 17:16:59 -0400173#endif // _MTP_SERVER_H