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:
1:71c0e9dc153d
Parent:
0:98cf19bcd828
Child:
4:9c6f5867f050

File content as of revision 1:71c0e9dc153d:

/* mbed USBHostPTP Library
 * Copyright (c) 2013 Dwayne Dilbeck
 * This software is distributed under the terms of the GNU Lesser General Public License
 */


#ifndef USBHOSTPTP_H
#define USBHOSTPTP_H

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

//#define USDBPTPDEBUG 1

 
    
/** 
 * 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

//Define a data handler function pointer type to allow handler function pointeers to be readable in function definitions.
typedef void (*DataHandler)(void *object, uint8_t *bufffer, uint16_t length);

/**
 * USBHostPTP class
 *
 */        
class USBHostPTP : public IUSBEnumerator {
public:

   /**
    * Define a set of flags to make calls to execute Operations a simple wrapper.  
    * This copied from the Circuits At Home Arduino Library to match thier format.
    */
   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;                      // NOT USED
                uint16_t        dataSize        :       6;                      // NOT USED - This is meant to be used when sending data to the PTP device               
        };     

    /**
    * 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();

    /**
     *  Cancel Request
     *  USB PTP class specific operation to allow cancelation of a bulk transaction
     *
     *  @param TransactionID  The idnumber of the transaction to cancel
     *
     *  @return true if the operation was succesful
     */
    bool CancelRequest(unsigned int TransactionID);
    bool DeviceResetRequest(void);
    bool GetDeviceStatus(void);
    bool GetExtendedEventData(void);
    
    
    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;
    uint32_t           transactionCnt;
    uint32_t           sessionID;
    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);
    uint16_t Transaction(uint16_t operationCode, OperFlags *flags, uint32_t *params = NULL, void *pVoid = NULL);

};

#endif