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_DEVICE_H
     18 #define _MTP_DEVICE_H
     19 
     20 #include "MtpEventPacket.h"
     21 #include "MtpDataPacket.h"
     22 #include "MtpRequestPacket.h"
     23 #include "MtpResponsePacket.h"
     24 #include "MtpTypes.h"
     25 
     26 #include <utils/threads.h>
     27 
     28 struct usb_device;
     29 struct usb_request;
     30 struct usb_endpoint_descriptor;
     31 
     32 namespace android {
     33 
     34 class MtpDeviceInfo;
     35 class MtpEventPacket;
     36 class MtpObjectInfo;
     37 class MtpStorageInfo;
     38 
     39 class MtpDevice {
     40 private:
     41     struct usb_device*      mDevice;
     42     int                     mInterface;
     43     struct usb_request*     mRequestIn1;
     44     struct usb_request*     mRequestIn2;
     45     struct usb_request*     mRequestOut;
     46     struct usb_request*     mRequestIntr;
     47     MtpDeviceInfo*          mDeviceInfo;
     48     MtpPropertyList         mDeviceProperties;
     49 
     50     // current session ID
     51     MtpSessionID            mSessionID;
     52     // current transaction ID
     53     MtpTransactionID        mTransactionID;
     54 
     55     MtpRequestPacket        mRequest;
     56     MtpDataPacket           mData;
     57     MtpResponsePacket       mResponse;
     58     MtpEventPacket          mEventPacket;
     59 
     60     // set to true if we received a response packet instead of a data packet
     61     bool                    mReceivedResponse;
     62     bool                    mProcessingEvent;
     63     int                     mCurrentEventHandle;
     64 
     65     // to ensure only one MTP transaction at a time
     66     Mutex                   mMutex;
     67     Mutex                   mEventMutex;
     68     Mutex                   mEventMutexForInterrupt;
     69 
     70 public:
     71     typedef bool (*ReadObjectCallback)
     72             (void* data, uint32_t offset, uint32_t length, void* clientData);
     73 
     74     MtpDevice(struct usb_device* device,
     75               int interface,
     76               const struct usb_endpoint_descriptor *ep_in,
     77               const struct usb_endpoint_descriptor *ep_out,
     78               const struct usb_endpoint_descriptor *ep_intr);
     79 
     80     static MtpDevice*       open(const char* deviceName, int fd);
     81 
     82     virtual                 ~MtpDevice();
     83 
     84     void                    initialize();
     85     void                    close();
     86     void                    print();
     87     const char*             getDeviceName();
     88 
     89     bool                    openSession();
     90     bool                    closeSession();
     91 
     92     MtpDeviceInfo*          getDeviceInfo();
     93     MtpStorageIDList*       getStorageIDs();
     94     MtpStorageInfo*         getStorageInfo(MtpStorageID storageID);
     95     MtpObjectHandleList*    getObjectHandles(MtpStorageID storageID, MtpObjectFormat format,
     96                                     MtpObjectHandle parent);
     97     MtpObjectInfo*          getObjectInfo(MtpObjectHandle handle);
     98     void*                   getThumbnail(MtpObjectHandle handle, int& outLength);
     99     MtpObjectHandle         sendObjectInfo(MtpObjectInfo* info);
    100     bool                    sendObject(MtpObjectHandle handle, int size, int srcFD);
    101     bool                    deleteObject(MtpObjectHandle handle);
    102     MtpObjectHandle         getParent(MtpObjectHandle handle);
    103     MtpObjectHandle         getStorageID(MtpObjectHandle handle);
    104 
    105     MtpObjectPropertyList*  getObjectPropsSupported(MtpObjectFormat format);
    106 
    107     MtpProperty*            getDevicePropDesc(MtpDeviceProperty code);
    108     MtpProperty*            getObjectPropDesc(MtpObjectProperty code, MtpObjectFormat format);
    109 
    110     // Reads value of |property| for |handle|. Returns true on success.
    111     bool                    getObjectPropValue(MtpObjectHandle handle, MtpProperty* property);
    112 
    113     bool                    readObject(MtpObjectHandle handle, ReadObjectCallback callback,
    114                                     uint32_t objectSize, void* clientData);
    115     bool                    readObject(MtpObjectHandle handle, const char* destPath, int group,
    116                                     int perm);
    117     bool                    readObject(MtpObjectHandle handle, int fd);
    118     bool                    readPartialObject(MtpObjectHandle handle,
    119                                               uint32_t offset,
    120                                               uint32_t size,
    121                                               uint32_t *writtenSize,
    122                                               ReadObjectCallback callback,
    123                                               void* clientData);
    124     bool                    readPartialObject64(MtpObjectHandle handle,
    125                                                 uint64_t offset,
    126                                                 uint32_t size,
    127                                                 uint32_t *writtenSize,
    128                                                 ReadObjectCallback callback,
    129                                                 void* clientData);
    130     // Starts a request to read MTP event from MTP device. It returns a request handle that
    131     // can be used for blocking read or cancel. If other thread has already been processing an
    132     // event returns -1.
    133     int                     submitEventRequest();
    134     // Waits for MTP event from the device and returns MTP event code. It blocks the current thread
    135     // until it receives an event from the device. |handle| should be a request handle returned
    136     // by |submitEventRequest|. The function writes event parameters to |parameters|. Returns 0 for
    137     // cancellations. Returns -1 for errors.
    138     int                     reapEventRequest(int handle, uint32_t (*parameters)[3]);
    139     // Cancels an event request. |handle| should be request handle returned by
    140     // |submitEventRequest|. If there is a thread blocked by |reapEventRequest| with the same
    141     // |handle|, the thread will resume.
    142     void                    discardEventRequest(int handle);
    143 
    144 private:
    145     // If |objectSize| is not NULL, it checks object size before reading data bytes.
    146     bool                    readObjectInternal(MtpObjectHandle handle,
    147                                                ReadObjectCallback callback,
    148                                                const uint32_t* objectSize,
    149                                                void* clientData);
    150     // If |objectSize| is not NULL, it checks object size before reading data bytes.
    151     bool                    readData(ReadObjectCallback callback,
    152                                      const uint32_t* objectSize,
    153                                      uint32_t* writtenData,
    154                                      void* clientData);
    155     bool                    sendRequest(MtpOperationCode operation);
    156     bool                    sendData();
    157     bool                    readData();
    158     bool                    writeDataHeader(MtpOperationCode operation, int dataLength);
    159     MtpResponseCode         readResponse();
    160 };
    161 
    162 }; // namespace android
    163 
    164 #endif // _MTP_DEVICE_H
    165