Library to allo USB PTP device to be hosted by the mbed platform

Dependents:   class_project_main

USBHostPTP.h

Committer:
jakowisp
Date:
2013-08-23
Revision:
0:98cf19bcd828
Child:
1:71c0e9dc153d

File content as of revision 0:98cf19bcd828:

/* mbed USBHost Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef USBHOSTPTP_H
#define USBHOSTPTP_H

#include "USBHostConf.h"
#include "USBHost.h"
#include "PIMA15740_types.h"

//#define USDBPTPDEBUG 1

    #ifdef USDBPTPDEBUG
    #define PTPTRACE(s) (Notify((s))
    #define PTPTRACE2(s,r) (Message((s),(r))
    #else
    #define PTPTRACE(s) ((void)0)
    #define PTPTRACE2(s,r) (wait_us(1))
    #endif
    
/** 
 * USB PTP definitions
 *    
 */
#define USB_PTP_CLASS_CODE 0x06
#define USB_PTP_CLASS_REQUEST_CANCELREQEUST           0x64
#define USB_PTP_CLASS_REQUEST_GET_EXTENDED_EVENT_DATA 0x65
#define USB_PTP_CLASS_REQUEST_DEVICE_RESET            0x66
#define USB_PTP_CLASS_REQUEST_GET_DEVICE_STATUS       0x67


typedef void (*DataHandler)(void *object, uint8_t *bufffer, uint16_t length);

/**
 * USBHostPTP class
 *
 */        
class USBHostPTP : public IUSBEnumerator {
public:
   struct OperFlags
        {
                uint16_t        opParams        :       3;                      // 7    - maximum number of operation parameters
                uint16_t        rsParams        :       3;                      // 7    - maximum number of response parameters
                uint16_t        txOperation     :       1;                      // I->R operation if the flag is set
                uint16_t        dataStage       :       1;                      // operation has data stage if the flag is set
                uint16_t        typeOfVoid      :       2;                      // 0 - NULL, 1 - PTPReadParser/PTPDataSupplyer, 2 - WRITEPARSER, 3 - buffer pointer
                uint16_t        dataSize        :       6;                      // size of data buffer (64 bytes maximum)               
        };     

    /**
    * Constructor
    *
    * @param None
    */
    USBHostPTP(void);

    /**
    * Check if a PTP device is connected
    *
    * @return true if a PTP device is connected
    */
    bool connected();

    /**
     * Try to connect to a PTP device
     *
     * @return true if connection was successful
     */
    bool connect();

    bool CancelRequest(unsigned int TransactionID);
    bool DeviceResetRequest(void);
    bool GetDeviceStatus(void);
    bool GetExtendedEventData(void);
    
    uint16_t Transaction(uint16_t operationCode, OperFlags *flags, uint32_t *params = NULL, void *pVoid = NULL);
    uint32_t transactionCnt;
    uint32_t sessionID;
    
    DeviceInfoStruct deviceInfo;
    ObjectInfoStruct objectInfo;
    
    uint32_t dataLeftToTransfer;
    uint32_t totalDataToTransfer;
    
    // Simple PTP operation which has no data stage. Any number of uint32_t params can be supplied.
    uint16_t Operation(uint16_t opcode, uint8_t nparams = 0, uint32_t *params = NULL);
    uint16_t OpenSession();
    uint16_t CloseSession();
    uint16_t GetDeviceInfo();

    uint16_t PowerDown();
    uint16_t SelfTest(uint16_t type = 0);
    uint16_t GetNumObjects(uint32_t *retval, uint32_t storage_id=0xffffffff, uint16_t format=0x0000, uint32_t assoc=0x0000);
    uint16_t GetObjectHandles(uint32_t storage_id=0xffffffff, uint16_t format=0x0000, uint16_t assoc=0x0000, void *parser=NULL);
    uint16_t GetObjectInfo(uint32_t handle);
    uint16_t GetThumb(uint32_t handle, void *parser=NULL);
    uint16_t GetObject(uint32_t handle, void *parser=NULL);
    uint16_t GetStorageIDs(void *parser=NULL);
    uint16_t GetStorageInfo(uint32_t storage_id, void *parser=NULL);
    uint16_t CopyObject(uint32_t handle, uint32_t storage_id, uint32_t parent, uint32_t *new_handle);
    uint16_t DeleteObject(uint32_t handle, uint16_t format);
    uint16_t SetObjectProtection(uint32_t handle, uint16_t attrib);
    uint16_t MoveObject(uint32_t handle, uint32_t storage_id, uint32_t parent);
    uint16_t GetDevicePropDesc(const uint16_t pcode, void *parser=NULL);
    uint16_t GetDevicePropValue(const uint16_t pcode, void *parser=NULL);
    uint16_t SetDevicePropValue(uint16_t pcode, uint32_t val);
    uint16_t ResetDevicePropValue(const uint16_t pcode);
    uint16_t FormatStore(uint32_t storage_id, uint32_t fsformat);
    uint16_t InitiateCapture(uint32_t storage_id, uint16_t format);
    uint16_t InitiateOpenCapture(uint32_t storage_id, uint16_t format);
    uint16_t TerminateOpenCapture(uint32_t trans_id);
    void DumpDeviceInfo(void);
    void DumpObjectInfo(void);

protected:
    //From IUSBEnumerator
    virtual void setVidPid(uint16_t vid, uint16_t pid);
    virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
    virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used

    //This is a generic function should belong to some other class or namespance.
    void DumpBuffer(uint8_t *buffer,uint32_t length);
    
private:
    USBHost            * host;
    USBDeviceConnected * pointerToDevice;
    bool               deviceFound;
    bool               deviceConnected;
    USBEndpoint        * bulk_in;
    USBEndpoint        * bulk_out;
    USBEndpoint        * int_in;
    uint8_t            numberOfEndpoints;
    
    PIMAContainer      commandContainer;
    PIMAContainer      responseContainer;
    PIMAContainer      dataContainer;
    PIMAContainer      eventContainer;
    uint8_t            buffer[1024];
    int                ptp_intf;
    
    void initializeClassData();
    int  checkResult(uint8_t res, USBEndpoint * ep);
    bool IsCommandSupported(uint16_t opcode);
    bool IsPropertySupported(uint16_t code);
    bool CheckValueInArray(PIMAArray *array, uint16_t code);
    bool CheckEvent(void);
    static void ParseDeviceInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length); 
    static void ParseObjectInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length);
};

#endif