Home | History | Annotate | Download | only in mtp
      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 "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 
     33 namespace android {
     34 
     35 class IMtpDatabase;
     36 class MtpStorage;
     37 
     38 class MtpServer {
     39 
     40 private:
     41     IMtpDatabase*       mDatabase;
     42 
     43     // appear as a PTP device
     44     bool                mPtp;
     45 
     46     // Manufacturer to report in DeviceInfo
     47     MtpStringBuffer     mDeviceInfoManufacturer;
     48     // Model to report in DeviceInfo
     49     MtpStringBuffer     mDeviceInfoModel;
     50     // Device version to report in DeviceInfo
     51     MtpStringBuffer     mDeviceInfoDeviceVersion;
     52     // Serial number to report in DeviceInfo
     53     MtpStringBuffer     mDeviceInfoSerialNumber;
     54 
     55     // current session ID
     56     MtpSessionID        mSessionID;
     57     // true if we have an open session and mSessionID is valid
     58     bool                mSessionOpen;
     59 
     60     MtpRequestPacket    mRequest;
     61     MtpDataPacket       mData;
     62     MtpResponsePacket   mResponse;
     63 
     64     MtpEventPacket      mEvent;
     65 
     66     MtpStorageList      mStorages;
     67 
     68     IMtpHandle*         mHandle;
     69 
     70     // handle for new object, set by SendObjectInfo and used by SendObject
     71     MtpObjectHandle     mSendObjectHandle;
     72     MtpObjectFormat     mSendObjectFormat;
     73     MtpStringBuffer     mSendObjectFilePath;
     74     size_t              mSendObjectFileSize;
     75     time_t              mSendObjectModifiedTime;
     76 
     77     std::mutex          mMutex;
     78 
     79     // represents an MTP object that is being edited using the android extensions
     80     // for direct editing (BeginEditObject, SendPartialObject, TruncateObject and EndEditObject)
     81     class ObjectEdit {
     82         public:
     83         MtpObjectHandle     mHandle;
     84         MtpStringBuffer           mPath;
     85         uint64_t            mSize;
     86         MtpObjectFormat     mFormat;
     87         int                 mFD;
     88 
     89         ObjectEdit(MtpObjectHandle handle, const char* path, uint64_t size,
     90             MtpObjectFormat format, int fd)
     91                 : mHandle(handle), mPath(path), mSize(size), mFormat(format), mFD(fd) {
     92             }
     93 
     94         virtual ~ObjectEdit() {
     95             close(mFD);
     96         }
     97     };
     98     std::vector<ObjectEdit*>  mObjectEditList;
     99 
    100 public:
    101                         MtpServer(IMtpDatabase* database, int controlFd, bool ptp,
    102                                     const char *deviceInfoManufacturer,
    103                                     const char *deviceInfoModel,
    104                                     const char *deviceInfoDeviceVersion,
    105                                     const char *deviceInfoSerialNumber);
    106     virtual             ~MtpServer();
    107 
    108     MtpStorage*         getStorage(MtpStorageID id);
    109     inline bool         hasStorage() { return mStorages.size() > 0; }
    110     bool                hasStorage(MtpStorageID id);
    111     void                addStorage(MtpStorage* storage);
    112     void                removeStorage(MtpStorage* storage);
    113 
    114     void                run();
    115 
    116     void                sendObjectAdded(MtpObjectHandle handle);
    117     void                sendObjectRemoved(MtpObjectHandle handle);
    118     void                sendDevicePropertyChanged(MtpDeviceProperty property);
    119 
    120 private:
    121     void                sendStoreAdded(MtpStorageID id);
    122     void                sendStoreRemoved(MtpStorageID id);
    123     void                sendEvent(MtpEventCode code, uint32_t param1);
    124 
    125     void                addEditObject(MtpObjectHandle handle, MtpStringBuffer& path,
    126                                 uint64_t size, MtpObjectFormat format, int fd);
    127     ObjectEdit*         getEditObject(MtpObjectHandle handle);
    128     void                removeEditObject(MtpObjectHandle handle);
    129     void                commitEdit(ObjectEdit* edit);
    130 
    131     bool                handleRequest();
    132 
    133     MtpResponseCode     doGetDeviceInfo();
    134     MtpResponseCode     doOpenSession();
    135     MtpResponseCode     doCloseSession();
    136     MtpResponseCode     doGetStorageIDs();
    137     MtpResponseCode     doGetStorageInfo();
    138     MtpResponseCode     doGetObjectPropsSupported();
    139     MtpResponseCode     doGetObjectHandles();
    140     MtpResponseCode     doGetNumObjects();
    141     MtpResponseCode     doGetObjectReferences();
    142     MtpResponseCode     doSetObjectReferences();
    143     MtpResponseCode     doGetObjectPropValue();
    144     MtpResponseCode     doSetObjectPropValue();
    145     MtpResponseCode     doGetDevicePropValue();
    146     MtpResponseCode     doSetDevicePropValue();
    147     MtpResponseCode     doResetDevicePropValue();
    148     MtpResponseCode     doGetObjectPropList();
    149     MtpResponseCode     doGetObjectInfo();
    150     MtpResponseCode     doGetObject();
    151     MtpResponseCode     doGetThumb();
    152     MtpResponseCode     doGetPartialObject(MtpOperationCode operation);
    153     MtpResponseCode     doSendObjectInfo();
    154     MtpResponseCode     doSendObject();
    155     MtpResponseCode     doDeleteObject();
    156     MtpResponseCode     doMoveObject();
    157     MtpResponseCode     doCopyObject();
    158     MtpResponseCode     doGetObjectPropDesc();
    159     MtpResponseCode     doGetDevicePropDesc();
    160     MtpResponseCode     doSendPartialObject();
    161     MtpResponseCode     doTruncateObject();
    162     MtpResponseCode     doBeginEditObject();
    163     MtpResponseCode     doEndEditObject();
    164 };
    165 
    166 }; // namespace android
    167 
    168 #endif // _MTP_SERVER_H
    169