blob: 35a38e79d89894e0bb623805a3ab7692ea4f4e89 [file] [log] [blame]
Mike Lockwood56118b52010-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 Lockwoodbe125a52010-07-12 18:54:16 -040023#include "MtpEventPacket.h"
Mike Lockwood56118b52010-05-11 17:16:59 -040024#include "mtp.h"
Mike Lockwood56118b52010-05-11 17:16:59 -040025#include "MtpUtils.h"
26
Mike Lockwood467ca0d2011-02-18 09:07:14 -050027#include <utils/threads.h>
28
Mike Lockwood8d3257a2010-05-14 10:10:36 -040029namespace android {
30
Mike Lockwoodd21eac92010-07-03 00:44:05 -040031class MtpDatabase;
Mike Lockwoodd21eac92010-07-03 00:44:05 -040032class MtpStorage;
Mike Lockwood56118b52010-05-11 17:16:59 -040033
34class MtpServer {
35
36private:
37 // file descriptor for MTP kernel driver
38 int mFD;
39
Mike Lockwoodd21eac92010-07-03 00:44:05 -040040 MtpDatabase* mDatabase;
Mike Lockwood56118b52010-05-11 17:16:59 -040041
Mike Lockwooddad69272010-07-02 15:15:07 -040042 // group to own new files and folders
43 int mFileGroup;
44 // permissions for new files and directories
45 int mFilePermission;
46 int mDirectoryPermission;
47
Mike Lockwood56118b52010-05-11 17:16:59 -040048 // current session ID
49 MtpSessionID mSessionID;
50 // true if we have an open session and mSessionID is valid
51 bool mSessionOpen;
52
53 MtpRequestPacket mRequest;
54 MtpDataPacket mData;
55 MtpResponsePacket mResponse;
Mike Lockwoodbe125a52010-07-12 18:54:16 -040056 MtpEventPacket mEvent;
Mike Lockwood56118b52010-05-11 17:16:59 -040057
58 MtpStorageList mStorages;
59
60 // handle for new object, set by SendObjectInfo and used by SendObject
61 MtpObjectHandle mSendObjectHandle;
Mike Lockwoodd815f792010-07-12 08:49:01 -040062 MtpObjectFormat mSendObjectFormat;
Mike Lockwood56118b52010-05-11 17:16:59 -040063 MtpString mSendObjectFilePath;
64 size_t mSendObjectFileSize;
65
Mike Lockwood467ca0d2011-02-18 09:07:14 -050066 Mutex mMutex;
67
Mike Lockwood9df53fae2011-04-21 17:05:55 -070068 // represents an MTP object that is being edited using the android extensions
69 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
70 struct ObjectEdit {
71 MtpObjectHandle handle;
72 MtpString path;
73 uint64_t size;
74 MtpObjectFormat format;
75 int fd;
76 };
77 Vector<ObjectEdit*> mObjectEditList;
78
Mike Lockwood56118b52010-05-11 17:16:59 -040079public:
Mike Lockwoodd21eac92010-07-03 00:44:05 -040080 MtpServer(int fd, MtpDatabase* database,
Mike Lockwooddad69272010-07-02 15:15:07 -040081 int fileGroup, int filePerm, int directoryPerm);
Mike Lockwood56118b52010-05-11 17:16:59 -040082 virtual ~MtpServer();
83
Mike Lockwoodb239b6832011-04-05 10:21:27 -040084 MtpStorage* getStorage(MtpStorageID id);
85 inline bool hasStorage() { return mStorages.size() > 0; }
86 bool hasStorage(MtpStorageID id);
Mike Lockwood467ca0d2011-02-18 09:07:14 -050087 void addStorage(MtpStorage* storage);
88 void removeStorage(MtpStorage* storage);
89
Mike Lockwood56118b52010-05-11 17:16:59 -040090 void run();
91
Mike Lockwoodbe125a52010-07-12 18:54:16 -040092 void sendObjectAdded(MtpObjectHandle handle);
93 void sendObjectRemoved(MtpObjectHandle handle);
94
Mike Lockwood56118b52010-05-11 17:16:59 -040095private:
Mike Lockwood467ca0d2011-02-18 09:07:14 -050096 void sendStoreAdded(MtpStorageID id);
97 void sendStoreRemoved(MtpStorageID id);
98 void sendEvent(MtpEventCode code, uint32_t param1);
99
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700100 void addEditObject(MtpObjectHandle handle, MtpString& path,
101 uint64_t size, MtpObjectFormat format, int fd);
102 ObjectEdit* getEditObject(MtpObjectHandle handle);
103 void removeEditObject(MtpObjectHandle handle);
104 void commitEdit(ObjectEdit* edit);
105
Mike Lockwooda82d3c52010-06-04 09:49:21 -0400106 bool handleRequest();
Mike Lockwood56118b52010-05-11 17:16:59 -0400107
108 MtpResponseCode doGetDeviceInfo();
109 MtpResponseCode doOpenSession();
110 MtpResponseCode doCloseSession();
111 MtpResponseCode doGetStorageIDs();
112 MtpResponseCode doGetStorageInfo();
113 MtpResponseCode doGetObjectPropsSupported();
114 MtpResponseCode doGetObjectHandles();
Mike Lockwood7a047c82010-08-02 10:52:20 -0400115 MtpResponseCode doGetNumObjects();
Mike Lockwood9a2046f2010-08-03 15:30:09 -0400116 MtpResponseCode doGetObjectReferences();
117 MtpResponseCode doSetObjectReferences();
Mike Lockwood56118b52010-05-11 17:16:59 -0400118 MtpResponseCode doGetObjectPropValue();
Mike Lockwood828d19d2010-08-10 15:20:35 -0400119 MtpResponseCode doSetObjectPropValue();
120 MtpResponseCode doGetDevicePropValue();
121 MtpResponseCode doSetDevicePropValue();
122 MtpResponseCode doResetDevicePropValue();
Mike Lockwoode2ad6ec2010-10-14 18:03:25 -0400123 MtpResponseCode doGetObjectPropList();
Mike Lockwood56118b52010-05-11 17:16:59 -0400124 MtpResponseCode doGetObjectInfo();
125 MtpResponseCode doGetObject();
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700126 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood56118b52010-05-11 17:16:59 -0400127 MtpResponseCode doSendObjectInfo();
128 MtpResponseCode doSendObject();
129 MtpResponseCode doDeleteObject();
130 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood828d19d2010-08-10 15:20:35 -0400131 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood9df53fae2011-04-21 17:05:55 -0700132 MtpResponseCode doSendPartialObject();
133 MtpResponseCode doTruncateObject();
134 MtpResponseCode doBeginEditObject();
135 MtpResponseCode doEndEditObject();
Mike Lockwood56118b52010-05-11 17:16:59 -0400136};
137
Mike Lockwood8d3257a2010-05-14 10:10:36 -0400138}; // namespace android
139
Mike Lockwood56118b52010-05-11 17:16:59 -0400140#endif // _MTP_SERVER_H