blob: 4bc07cdef2151f4ce888cf27331932c66693eb6b [file] [log] [blame]
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05001/*
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
33class IMtpDatabase;
34class MtpStorage;
35
36class MtpServer {
37
38private:
39 IMtpDatabase* mDatabase;
40
41 // appear as a PTP device
42 bool mPtp;
43
44 // Manufacturer to report in DeviceInfo
45 MtpStringBuffer mDeviceInfoManufacturer;
46 // Model to report in DeviceInfo
47 MtpStringBuffer mDeviceInfoModel;
48 // Device version to report in DeviceInfo
49 MtpStringBuffer mDeviceInfoDeviceVersion;
50 // Serial number to report in DeviceInfo
51 MtpStringBuffer mDeviceInfoSerialNumber;
52
53 // current session ID
54 MtpSessionID mSessionID;
55 // true if we have an open session and mSessionID is valid
56 bool mSessionOpen;
57
58 MtpRequestPacket mRequest;
59 MtpDataPacket mData;
60 MtpResponsePacket mResponse;
61
62 MtpEventPacket mEvent;
63
64 MtpStorageList mStorages;
65
66 IMtpHandle* mHandle;
67
68 // handle for new object, set by SendObjectInfo and used by SendObject
69 MtpObjectHandle mSendObjectHandle;
70 MtpObjectFormat mSendObjectFormat;
71 MtpStringBuffer mSendObjectFilePath;
72 size_t mSendObjectFileSize;
73 time_t mSendObjectModifiedTime;
74
75 std::mutex mMutex;
76
77 // represents an MTP object that is being edited using the android extensions
78 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
79 class ObjectEdit {
80 public:
81 MtpObjectHandle mHandle;
82 MtpStringBuffer mPath;
83 uint64_t mSize;
84 MtpObjectFormat mFormat;
85 int mFD;
86
87 ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
88 MtpObjectFormat format, int fd)
89 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
90 }
91
92 virtual ~ObjectEdit() {
93 close(mFD);
94 }
95 };
96 std::vector<ObjectEdit*> mObjectEditList;
97
98public:
99 MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
100 const char *deviceInfoManufacturer,
101 const char *deviceInfoModel,
102 const char *deviceInfoDeviceVersion,
103 const char *deviceInfoSerialNumber);
104 virtual ~MtpServer();
105
106 MtpStorage* getStorage(MtpStorageID id);
107 inline bool hasStorage() { return mStorages.size() > 0; }
108 bool hasStorage(MtpStorageID id);
109 void addStorage(MtpStorage* storage);
110 void removeStorage(MtpStorage* storage);
111
112 void run();
113
114 void sendObjectAdded(MtpObjectHandle handle);
115 void sendObjectRemoved(MtpObjectHandle handle);
116 void sendObjectUpdated(MtpObjectHandle handle);
117 void sendDevicePropertyChanged(MtpDeviceProperty property);
118 void sendObjectInfoChanged(MtpObjectHandle handle);
119
120
121private:
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#endif // _MTP_SERVER_H