MtpServer.h revision c3f16e5620c090aeb75c0836572a8b913a4ef864
1/*
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 "mtp.h"
25#include "MtpUtils.h"
26
27#include <utils/threads.h>
28
29namespace android {
30
31class MtpDatabase;
32class MtpStorage;
33
34class MtpServer {
35
36private:
37    // file descriptor for MTP kernel driver
38    int                 mFD;
39
40    MtpDatabase*        mDatabase;
41
42    // group to own new files and folders
43    int                 mFileGroup;
44    // permissions for new files and directories
45    int                 mFilePermission;
46    int                 mDirectoryPermission;
47
48    // 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;
56    MtpEventPacket      mEvent;
57
58    MtpStorageList      mStorages;
59
60    // handle for new object, set by SendObjectInfo and used by SendObject
61    MtpObjectHandle     mSendObjectHandle;
62    MtpObjectFormat     mSendObjectFormat;
63    MtpString           mSendObjectFilePath;
64    size_t              mSendObjectFileSize;
65
66    Mutex               mMutex;
67
68    // represents an MTP object that is being edited using the android extensions
69    // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
70    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        }
86    };
87    Vector<ObjectEdit*>  mObjectEditList;
88
89public:
90                        MtpServer(int fd, MtpDatabase* database,
91                                    int fileGroup, int filePerm, int directoryPerm);
92    virtual             ~MtpServer();
93
94    MtpStorage*         getStorage(MtpStorageID id);
95    inline bool         hasStorage() { return mStorages.size() > 0; }
96    bool                hasStorage(MtpStorageID id);
97    void                addStorage(MtpStorage* storage);
98    void                removeStorage(MtpStorage* storage);
99
100    void                run();
101
102    void                sendObjectAdded(MtpObjectHandle handle);
103    void                sendObjectRemoved(MtpObjectHandle handle);
104
105private:
106    void                sendStoreAdded(MtpStorageID id);
107    void                sendStoreRemoved(MtpStorageID id);
108    void                sendEvent(MtpEventCode code, uint32_t param1);
109
110    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
116    bool                handleRequest();
117
118    MtpResponseCode     doGetDeviceInfo();
119    MtpResponseCode     doOpenSession();
120    MtpResponseCode     doCloseSession();
121    MtpResponseCode     doGetStorageIDs();
122    MtpResponseCode     doGetStorageInfo();
123    MtpResponseCode     doGetObjectPropsSupported();
124    MtpResponseCode     doGetObjectHandles();
125    MtpResponseCode     doGetNumObjects();
126    MtpResponseCode     doGetObjectReferences();
127    MtpResponseCode     doSetObjectReferences();
128    MtpResponseCode     doGetObjectPropValue();
129    MtpResponseCode     doSetObjectPropValue();
130    MtpResponseCode     doGetDevicePropValue();
131    MtpResponseCode     doSetDevicePropValue();
132    MtpResponseCode     doResetDevicePropValue();
133    MtpResponseCode     doGetObjectPropList();
134    MtpResponseCode     doGetObjectInfo();
135    MtpResponseCode     doGetObject();
136    MtpResponseCode     doGetPartialObject(MtpOperationCode operation);
137    MtpResponseCode     doSendObjectInfo();
138    MtpResponseCode     doSendObject();
139    MtpResponseCode     doDeleteObject();
140    MtpResponseCode     doGetObjectPropDesc();
141    MtpResponseCode     doGetDevicePropDesc();
142    MtpResponseCode     doSendPartialObject();
143    MtpResponseCode     doTruncateObject();
144    MtpResponseCode     doBeginEditObject();
145    MtpResponseCode     doEndEditObject();
146};
147
148}; // namespace android
149
150#endif // _MTP_SERVER_H
151