blob: b3a11e0a540376da7f1798c23f90a259c2c27593 [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 Lockwood3d1d7762011-06-21 08:27:06 -040042 // appear as a PTP device
43 bool mPtp;
44
Mike Lockwood8e2a2802010-07-02 15:15:07 -040045 // group to own new files and folders
46 int mFileGroup;
47 // permissions for new files and directories
48 int mFilePermission;
49 int mDirectoryPermission;
50
Mike Lockwood16864ba2010-05-11 17:16:59 -040051 // current session ID
52 MtpSessionID mSessionID;
53 // true if we have an open session and mSessionID is valid
54 bool mSessionOpen;
55
56 MtpRequestPacket mRequest;
57 MtpDataPacket mData;
58 MtpResponsePacket mResponse;
Mike Lockwood873871f2010-07-12 18:54:16 -040059 MtpEventPacket mEvent;
Mike Lockwood16864ba2010-05-11 17:16:59 -040060
61 MtpStorageList mStorages;
62
63 // handle for new object, set by SendObjectInfo and used by SendObject
64 MtpObjectHandle mSendObjectHandle;
Mike Lockwood4714b072010-07-12 08:49:01 -040065 MtpObjectFormat mSendObjectFormat;
Mike Lockwood16864ba2010-05-11 17:16:59 -040066 MtpString mSendObjectFilePath;
67 size_t mSendObjectFileSize;
68
Mike Lockwooda8494402011-02-18 09:07:14 -050069 Mutex mMutex;
70
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070071 // represents an MTP object that is being edited using the android extensions
72 // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
Mike Lockwoodc3f16e52011-04-25 12:56:21 -070073 class ObjectEdit {
74 public:
75 MtpObjectHandle mHandle;
76 MtpString mPath;
77 uint64_t mSize;
78 MtpObjectFormat mFormat;
79 int mFD;
80
81 ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
82 MtpObjectFormat format, int fd)
83 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
84 }
85
86 virtual ~ObjectEdit() {
87 close(mFD);
88 }
Mike Lockwood7d77dcf2011-04-21 17:05:55 -070089 };
90 Vector<ObjectEdit*> mObjectEditList;
91
Mike Lockwood16864ba2010-05-11 17:16:59 -040092public:
Mike Lockwood3d1d7762011-06-21 08:27:06 -040093 MtpServer(int fd, MtpDatabase* database, bool ptp,
Mike Lockwood8e2a2802010-07-02 15:15:07 -040094 int fileGroup, int filePerm, int directoryPerm);
Mike Lockwood16864ba2010-05-11 17:16:59 -040095 virtual ~MtpServer();
96
Mike Lockwood30adaaf2011-04-05 10:21:27 -040097 MtpStorage* getStorage(MtpStorageID id);
98 inline bool hasStorage() { return mStorages.size() > 0; }
99 bool hasStorage(MtpStorageID id);
Mike Lockwooda8494402011-02-18 09:07:14 -0500100 void addStorage(MtpStorage* storage);
101 void removeStorage(MtpStorage* storage);
102
Mike Lockwood16864ba2010-05-11 17:16:59 -0400103 void run();
104
Mike Lockwood873871f2010-07-12 18:54:16 -0400105 void sendObjectAdded(MtpObjectHandle handle);
106 void sendObjectRemoved(MtpObjectHandle handle);
Mike Lockwood0fa848d2014-03-07 13:29:59 -0800107 void sendDevicePropertyChanged(MtpDeviceProperty property);
Mike Lockwood873871f2010-07-12 18:54:16 -0400108
Mike Lockwood16864ba2010-05-11 17:16:59 -0400109private:
Mike Lockwooda8494402011-02-18 09:07:14 -0500110 void sendStoreAdded(MtpStorageID id);
111 void sendStoreRemoved(MtpStorageID id);
112 void sendEvent(MtpEventCode code, uint32_t param1);
113
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700114 void addEditObject(MtpObjectHandle handle, MtpString& path,
115 uint64_t size, MtpObjectFormat format, int fd);
116 ObjectEdit* getEditObject(MtpObjectHandle handle);
117 void removeEditObject(MtpObjectHandle handle);
118 void commitEdit(ObjectEdit* edit);
119
Mike Lockwood916076c2010-06-04 09:49:21 -0400120 bool handleRequest();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400121
122 MtpResponseCode doGetDeviceInfo();
123 MtpResponseCode doOpenSession();
124 MtpResponseCode doCloseSession();
125 MtpResponseCode doGetStorageIDs();
126 MtpResponseCode doGetStorageInfo();
127 MtpResponseCode doGetObjectPropsSupported();
128 MtpResponseCode doGetObjectHandles();
Mike Lockwood343af4e2010-08-02 10:52:20 -0400129 MtpResponseCode doGetNumObjects();
Mike Lockwood438344f2010-08-03 15:30:09 -0400130 MtpResponseCode doGetObjectReferences();
131 MtpResponseCode doSetObjectReferences();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400132 MtpResponseCode doGetObjectPropValue();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400133 MtpResponseCode doSetObjectPropValue();
134 MtpResponseCode doGetDevicePropValue();
135 MtpResponseCode doSetDevicePropValue();
136 MtpResponseCode doResetDevicePropValue();
Mike Lockwoodb6da06e2010-10-14 18:03:25 -0400137 MtpResponseCode doGetObjectPropList();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400138 MtpResponseCode doGetObjectInfo();
139 MtpResponseCode doGetObject();
Mike Lockwood64000782011-04-24 18:40:17 -0700140 MtpResponseCode doGetThumb();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700141 MtpResponseCode doGetPartialObject(MtpOperationCode operation);
Mike Lockwood16864ba2010-05-11 17:16:59 -0400142 MtpResponseCode doSendObjectInfo();
143 MtpResponseCode doSendObject();
144 MtpResponseCode doDeleteObject();
145 MtpResponseCode doGetObjectPropDesc();
Mike Lockwood8277cec2010-08-10 15:20:35 -0400146 MtpResponseCode doGetDevicePropDesc();
Mike Lockwood7d77dcf2011-04-21 17:05:55 -0700147 MtpResponseCode doSendPartialObject();
148 MtpResponseCode doTruncateObject();
149 MtpResponseCode doBeginEditObject();
150 MtpResponseCode doEndEditObject();
Mike Lockwood16864ba2010-05-11 17:16:59 -0400151};
152
Mike Lockwood7850ef92010-05-14 10:10:36 -0400153}; // namespace android
154
Mike Lockwood16864ba2010-05-11 17:16:59 -0400155#endif // _MTP_SERVER_H