blob: 859a18e63b6659146ec339debe662f10f752c616 [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"
26
Mike Lockwooda8494402011-02-18 09:07:14 -050027#include <utils/threads.h>
28
Mike Lockwood7850ef92010-05-14 10:10:36 -040029namespace android {
30
Mike Lockwood1865a5d2010-07-03 00:44:05 -040031class MtpDatabase;
Mike Lockwood1865a5d2010-07-03 00:44:05 -040032class MtpStorage;
Mike Lockwood16864ba2010-05-11 17:16:59 -040033
34class MtpServer {
35
36private:
37 // file descriptor for MTP kernel driver
38 int mFD;
39
Mike Lockwood1865a5d2010-07-03 00:44:05 -040040 MtpDatabase* mDatabase;
Mike Lockwood16864ba2010-05-11 17:16:59 -040041
Mike Lockwood8e2a2802010-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 Lockwood16864ba2010-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 Lockwood873871f2010-07-12 18:54:16 -040056 MtpEventPacket mEvent;
Mike Lockwood16864ba2010-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 Lockwood4714b072010-07-12 08:49:01 -040062 MtpObjectFormat mSendObjectFormat;
Mike Lockwood16864ba2010-05-11 17:16:59 -040063 MtpString mSendObjectFilePath;
64 size_t mSendObjectFileSize;
65
Mike Lockwooda8494402011-02-18 09:07:14 -050066 Mutex mMutex;
67
Mike Lockwood7d77dcf2011-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)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -070070 class ObjectEdit {
71 public:
72 MtpObjectHandle mHandle;
73 MtpString mPath;
74 uint64_t mSize;
75 MtpObjectFormat mFormat;
76 int mFD;
77
78 ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
79 MtpObjectFormat format, int fd)
80 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
81 }
82
83 virtual ~ObjectEdit() {
84 close(mFD);
85 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070086 };
87 Vector<ObjectEdit*> mObjectEditList;
88
Mike Lockwood16864ba2010-05-11 17:16:59 -040089public:
Mike Lockwood1865a5d2010-07-03 00:44:05 -040090 MtpServer(int fd, MtpDatabase* database,
Mike Lockwood8e2a2802010-07-02 15:15:07 -040091 int fileGroup, int filePerm, int directoryPerm);
Mike Lockwood16864ba2010-05-11 17:16:59 -040092 virtual ~MtpServer();
93
Mike Lockwood30adaaf2011-04-05 10:21:27 -040094 MtpStorage* getStorage(MtpStorageID id);
95 inline bool hasStorage() { return mStorages.size() > 0; }
96 bool hasStorage(MtpStorageID id);
Mike Lockwooda8494402011-02-18 09:07:14 -050097 void addStorage(MtpStorage* storage);
98 void removeStorage(MtpStorage* storage);
99
Mike Lockwood16864ba2010-05-11 17:16:59 -0400100 void run();
101
Mike Lockwood873871f2010-07-12 18:54:16 -0400102 void sendObjectAdded(MtpObjectHandle handle);
103 void sendObjectRemoved(MtpObjectHandle handle);
104
Mike Lockwood16864ba2010-05-11 17:16:59 -0400105private:
Mike Lockwooda8494402011-02-18 09:07:14 -0500106 void sendStoreAdded(MtpStorageID id);
107 void sendStoreRemoved(MtpStorageID id);
108 void sendEvent(MtpEventCode code, uint32_t param1);
109
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700110 void addEditObject(MtpObjectHandle handle, MtpString& path,
111 uint64_t size, MtpObjectFormat format, int fd);
112 ObjectEdit* getEditObject(MtpObjectHandle handle);
113 void removeEditObject(MtpObjectHandle handle);
114 void commitEdit(ObjectEdit* edit);
115
Mike Lockwood916076c2010-06-04 09:49:21 -0400116 bool handleRequest();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400117
118 MtpResponseCode doGetDeviceInfo();
119 MtpResponseCode doOpenSession();
120 MtpResponseCode doCloseSession();
121 MtpResponseCode doGetStorageIDs();
122 MtpResponseCode doGetStorageInfo();
123 MtpResponseCode doGetObjectPropsSupported();
124 MtpResponseCode doGetObjectHandles();
Mike Lockwood343af4e2010-08-02 10:52:20 -0400125 MtpResponseCode doGetNumObjects();
Mike Lockwood438344f2010-08-03 15:30:09 -0400126 MtpResponseCode doGetObjectReferences();
127 MtpResponseCode doSetObjectReferences();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400128 MtpResponseCode doGetObjectPropValue();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400129 MtpResponseCode doSetObjectPropValue();
130 MtpResponseCode doGetDevicePropValue();
131 MtpResponseCode doSetDevicePropValue();
132 MtpResponseCode doResetDevicePropValue();
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400133 MtpResponseCode doGetObjectPropList();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400134 MtpResponseCode doGetObjectInfo();
135 MtpResponseCode doGetObject();
Mike Lockwood64000782011-04-24 18:40:17 -0700136 MtpResponseCode doGetThumb();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700137 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400138 MtpResponseCode doSendObjectInfo();
139 MtpResponseCode doSendObject();
140 MtpResponseCode doDeleteObject();
141 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400142 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700143 MtpResponseCode doSendPartialObject();
144 MtpResponseCode doTruncateObject();
145 MtpResponseCode doBeginEditObject();
146 MtpResponseCode doEndEditObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400147};
148
Mike Lockwood7850ef92010-05-14 10:10:36 -0400149}; // namespace android
150
Mike Lockwood16864ba2010-05-11 17:16:59 -0400151#endif // _MTP_SERVER_H