Library to allo USB PTP device to be hosted by the mbed platform
Dependents: class_project_main
Revision 5:728b5d58e135, committed 2013-08-29
- Comitter:
- jakowisp
- Date:
- Thu Aug 29 03:48:07 2013 +0000
- Parent:
- 4:9c6f5867f050
- Child:
- 6:124a01eaf569
- Commit message:
- Refactor Transaction to make the function simpler to understand.
Changed in this revision
USBHostPTP.cpp | Show annotated file Show diff for this revision Revisions of this file |
USBHostPTP.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/USBHostPTP.cpp Wed Aug 28 03:25:53 2013 +0000 +++ b/USBHostPTP.cpp Thu Aug 29 03:48:07 2013 +0000 @@ -21,19 +21,19 @@ } void USBHostPTP::initializeClassData() { - deviceConnected = false; - pointerToDevice = NULL; - bulk_in = NULL; - bulk_out = NULL; - int_in=NULL; - deviceFound = false; - deviceConnected = false; - ptp_intf = -1; - numberOfEndpoints = 0; - transactionCnt=0; - commandContainer.type=PIMA_CONTAINER_COMMAND; - sessionID=1; - dataLeftToTransfer=0; + deviceConnected = false; + pointerToDevice = NULL; + bulk_in = NULL; + bulk_out = NULL; + int_in = NULL; + deviceFound = false; + deviceConnected = false; + ptp_intf = -1; + numberOfEndpoints = 0; + transactionCnt = 0; + commandContainer.type = PIMA_CONTAINER_COMMAND; + sessionID = 1; + dataLeftToTransfer = 0; } @@ -169,7 +169,7 @@ unsigned char buffer[6]; int res = 0; - //TODO: DetermineBuffer size + ///TODO: DetermineBuffer size res = host->controlWrite( pointerToDevice, USB_RECIPIENT_ENDPOINT | USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_STANDARD, @@ -210,138 +210,158 @@ return 0; } +void USBHostPTP::PrepareControlContainer(uint16_t operationCode, + OperFlags *operationFlags, + uint32_t *parameters) { + + this->commandContainer.length = PIMA_CONTAINER_HEADER_LENGTH + operationFlags->opParams*4; + this->commandContainer.code = operationCode; + this->commandContainer.transactionID = this->transactionCnt++; + for (int i=0; i<operationFlags->opParams; i++) + this->commandContainer.payload[i]=parameters[i]; + + #ifdef USDBPTPDEBUG + printf("PTPCommandContainer:\r\n"); + DumpBuffer((uint8_t *)&(this->commandContainer),this->commandContainer.length); + #endif + +} + +bool USBHostPTP::RecieveDataContainer(void *dataHandlerFunctionCastToVoid, uint16_t *responseCode){ + int i=0; + int transferResult=0; + bool firstDataBlockReceived=true; + uint8_t *pointerToUint8tData; + uint32_t bytesReceived=0; + uint32_t bytesRemaining=0; + uint32_t dataToBeHandled=0; + + DataHandler dataHandler1 = (DataHandler) dataHandlerFunctionCastToVoid; + + if( responseCode!=NULL) + *responseCode= PIMA_RETURN_CODE_Undefined; + do { + ///TODO: Magic number needs to be replaced with DEFINE + transferResult = host->bulkRead(this->pointerToDevice, this->bulk_in, this->buffer, 1024); + if ( checkResult(transferResult, this->bulk_in)) + return PIMA_RETURN_CODE_IncompleteTransfer; + #ifdef USDBPTPDEBUG + printf("Data stage bulkRead result: %d\r\n",transferResult); + #endif + bytesReceived = bulk_in->getLengthTransferred(); + if(firstDataBlockReceived==true) { + pointerToUint8tData=(uint8_t *)&dataContainer; + for(i=0; i<PIMA_CONTAINER_HEADER_LENGTH; i++){ + pointerToUint8tData[i]=buffer[i]; + } + #ifdef USDBPTPDEBUG + DumpBuffer(pointerToUint8tData,PIMA_CONTAINER_HEADER_LENGTH); + #endif + firstDataBlockReceived = false; + pointerToUint8tData = buffer + PIMA_CONTAINER_HEADER_LENGTH; + bytesRemaining = dataContainer.length - PIMA_CONTAINER_HEADER_LENGTH; + this->totalDataToTransfer = bytesRemaining; + if(dataContainer.type==PIMA_CONTAINER_DATA) + bytesReceived -= PIMA_CONTAINER_HEADER_LENGTH; + #ifdef USDBPTPDEBUG + DumpBuffer(pointerToUint8tData,bytesReceived); + #endif + } else { + pointerToUint8tData=buffer; + } + + if(bytesReceived<=bytesRemaining) + dataToBeHandled=bytesReceived; + else { + dataToBeHandled=bytesRemaining; + } + + #ifdef USDBPTPDEBUG + printf("Calling Datahandler\r\n"); + printf("DataToBeHandled: %ld\r\n",dataToBeHandled); + #endif + if( dataHandler1!=NULL && dataToBeHandled !=0 ) + (*dataHandler1)(this,pointerToUint8tData,dataToBeHandled); + + #ifdef USDBPTPDEBUG + DumpBuffer(buffer,bytesReceived); + #endif + + //IF the Response got appended to Data block + if(bytesReceived<=bytesRemaining) { + bytesRemaining-=bytesReceived; + } else { + #ifdef USDBPTPDEBUG + printf("Handling Response Packet in data container\r\n"); + #endif + if(bytesReceived-bytesRemaining==responseContainer.length) { + pointerToUint8tData=(uint8_t *)&responseContainer; + for(i=0;i<responseContainer.length;i++){ + pointerToUint8tData[i]=buffer[bytesRemaining+i]; + } + *responseCode = responseContainer.code; + } else { + return false; + } + } + this->dataLeftToTransfer = bytesRemaining; + #ifdef USDBPTPDEBUG + printf("Loopcheck: %ld\r\n",bytesRemaining); + #endif + } while( bytesRemaining>0 ); + return true; +} uint16_t USBHostPTP::Transaction(uint16_t operationCode, OperFlags *operationFlags, uint32_t *parameters, void *dataHandlerFunctionCastToVoid){ - + + uint16_t responseCode=0; int transferResult=0; - int i=0; - uint8_t *pointerToUint8tData; - uint32_t bytesReceived=0; - uint32_t bytesRemaining=0; - uint32_t dataToBeHandled=0; - uint32_t responseLength=0; - DataHandler dataHandler1 = (DataHandler) dataHandlerFunctionCastToVoid; - bool firstDataBlockReceived=true; if(IsCommandSupported(operationCode)==false) return PIMA_RETURN_CODE_OperationNotSupported; - commandContainer.length = PIMA_CONTAINER_HEADER_LENGTH + operationFlags->opParams*4; - responseLength = PIMA_CONTAINER_HEADER_LENGTH + operationFlags->rsParams*4; - responseContainer.length = responseLength; - commandContainer.code = operationCode; - commandContainer.transactionID = transactionCnt++; - for (i=0; i<operationFlags->opParams; i++) - commandContainer.payload[i]=parameters[i]; - - #ifdef USDBPTPDEBUG - printf("PTPCommandContainer:\r\n"); - DumpBuffer((uint8_t *)&commandContainer,commandContainer.length); - #endif + responseContainer.length = PIMA_CONTAINER_HEADER_LENGTH + operationFlags->rsParams*4; + + PrepareControlContainer(operationCode,operationFlags,parameters); - transferResult = host->bulkWrite(pointerToDevice, bulk_out,(uint8_t *)&commandContainer, commandContainer.length); - if (checkResult(transferResult, bulk_out)) - return PIMA_RETURN_CODE_IncompleteTransfer; + transferResult = this->host->bulkWrite(this->pointerToDevice, this->bulk_out,(uint8_t *)&(this->commandContainer), this->commandContainer.length); + if (checkResult(transferResult, this->bulk_out)) + return PIMA_RETURN_CODE_GeneralError; #ifdef USDBPTPDEBUG printf("Command bulkWrite result: %d\r\n",transferResult); #endif - if(operationFlags->dataStage==1) { - if(operationFlags->txOperation==0) { - do { - transferResult = host->bulkRead(pointerToDevice, bulk_in, buffer, 1024); - #ifdef USDBPTPDEBUG - printf("Data stage bulkRead result: %d\r\n",transferResult); - #endif - if ( checkResult(transferResult, bulk_in)) - return PIMA_RETURN_CODE_IncompleteTransfer; - bytesReceived = bulk_in->getLengthTransferred(); - if(firstDataBlockReceived==true) { - pointerToUint8tData=(uint8_t *)&dataContainer; - for(i=0; i<PIMA_CONTAINER_HEADER_LENGTH; i++){ - pointerToUint8tData[i]=buffer[i]; - } - #ifdef USDBPTPDEBUG - DumpBuffer(pointerToUint8tData,PIMA_CONTAINER_HEADER_LENGTH); - #endif - firstDataBlockReceived = false; - pointerToUint8tData = buffer + PIMA_CONTAINER_HEADER_LENGTH; - bytesRemaining = dataContainer.length - PIMA_CONTAINER_HEADER_LENGTH; - this->totalDataToTransfer = bytesRemaining; - if(dataContainer.type==PIMA_CONTAINER_DATA) - bytesReceived -= PIMA_CONTAINER_HEADER_LENGTH; - #ifdef USDBPTPDEBUG - DumpBuffer(pointerToUint8tData,bytesReceived); - #endif - } else { - pointerToUint8tData=buffer; - } - - if(bytesReceived<=bytesRemaining) - dataToBeHandled=bytesReceived; - else { - dataToBeHandled=bytesRemaining; - } - - #ifdef USDBPTPDEBUG - printf("Calling Datahandler\r\n"); - printf("DataToBeHandled: %ld\r\n",dataToBeHandled); - #endif - if( dataHandler1!=NULL && dataToBeHandled !=0 ) - (*dataHandler1)(this,pointerToUint8tData,dataToBeHandled); - - #ifdef USDBPTPDEBUG - DumpBuffer(buffer,bytesReceived); - #endif - - //IF the Response got appended to Data block - if(bytesReceived<=bytesRemaining) { - bytesRemaining-=bytesReceived; - } else { - #ifdef USDBPTPDEBUG - printf("Handling Response Packet in data container\r\n"); - #endif - if(bytesReceived-bytesRemaining==responseLength) { - pointerToUint8tData=(uint8_t *)&responseContainer; - for(i=0;i<responseLength;i++){ - pointerToUint8tData[i]=buffer[bytesRemaining+i]; - } - - return responseContainer.code; - } else { - - return PIMA_RETURN_CODE_IncompleteTransfer; - } - } - this->dataLeftToTransfer = bytesRemaining; - #ifdef USDBPTPDEBUG - printf("Loopcheck: %ld\r\n",bytesRemaining); - #endif - } while( bytesRemaining>0 ); + + if (operationFlags->dataStage==1) { + if (operationFlags->txOperation==0) { + if (RecieveDataContainer(dataHandlerFunctionCastToVoid,&responseCode)!=true) + return PIMA_RETURN_CODE_GeneralError; + else + if (responseCode != PIMA_RETURN_CODE_Undefined) + return responseCode; } else { printf("DataOut not coded\r\n"); } } transferResult = host->bulkRead(pointerToDevice, bulk_in,(uint8_t *)&responseContainer, sizeof(responseContainer)); if (checkResult(transferResult, bulk_in)) - return PIMA_RETURN_CODE_IncompleteTransfer; + return PIMA_RETURN_CODE_GeneralError; #ifdef USDBPTPDEBUG - printf("Response bulkRead result: %d\r\n",transferResult); - printf("PTPResponseContainer:\r\n"); - DumpBuffer((uint8_t *)&responseContainer,responseContainer.len); - if(responseContainer.opcode==PIMA_RETURN_CODE_OK) { - printf("PIMA_RETURN_CODE_OK\r\n"); - } else { - printf("Response Length: %x\r\n",responseContainer.len); - printf("Response type: %x\r\n",responseContainer.type); - printf("Response code: %x\r\n",responseContainer.opcode); - printf("Response ID: %d\r\n",responseContainer.TransactionID); - } + printf("Response bulkRead result: %d\r\n",transferResult); + printf("PTPResponseContainer:\r\n"); + DumpBuffer((uint8_t *)&responseContainer,responseContainer.len); + if(responseContainer.opcode==PIMA_RETURN_CODE_OK) { + printf("PIMA_RETURN_CODE_OK\r\n"); + } else { + printf("Response Length: %x\r\n",responseContainer.len); + printf("Response type: %x\r\n",responseContainer.type); + printf("Response code: %x\r\n",responseContainer.opcode); + printf("Response ID: %d\r\n",responseContainer.TransactionID); + } #endif - return responseContainer.code; }
--- a/USBHostPTP.h Wed Aug 28 03:25:53 2013 +0000 +++ b/USBHostPTP.h Thu Aug 29 03:48:07 2013 +0000 @@ -383,6 +383,10 @@ static void ParseDeviceInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length); static void ParseObjectInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length); static void ParseStorageInfoDataBlock(void *ptp, uint8_t *buffer,uint16_t length); + void PrepareControlContainer(uint16_t operationCode, + OperFlags *operationFlags, + uint32_t *parameters); + bool RecieveDataContainer(void *dataHandlerFunctionCastToVoid, uint16_t *responseCode); uint16_t Transaction(uint16_t operationCode, OperFlags *flags, uint32_t *params = NULL, void *pVoid = NULL); };