/* ** Host Query (read) for information */ void OnFlashImgBeginUpload( Dci_Context* pctxt) { int ec; Dci_BinaryImageTransferQuery* pbitq = (Dci_BinaryImageTransferQuery*)pctxt->pMsg; // Get flash header from flash. ec = Flash_Hdr_Read(&s_flashHdr); //If we were successful in reading header initiate transfer. if( ec == 0x0 && Flash_Hdr_Validate( &s_flashHdr) ) { //Send back the bit params we stored previously. Dciproc_SendMessage1((byte*) &(s_flashHdr.bitParams), sizeof( Dci_BinaryImageTransfer), false, pctxt); //Initialize the frame id so we can begin sending. s_idFrame = 0; } else // error reply { byte buff[MAX_MSG_SIZE]; int lenMsg = Dci_BinaryImageTransferStatus_Init( buff, WCACOMP_FLASH, pbitq->idTransfer, s_idFrame, 0, BSE_ReadError); Dciproc_SendMessage1(buff, lenMsg, false, pctxt); ResetTransfer(); } pctxt->bHandled = true; }
/*----------------------------------------------------------------------------* * NAME * SetImageFileName * * DESCRIPTION * The file name of the image to be transferred. *---------------------------------------------------------------------------*/ bool ImageFileHandler::OpenImageFile() { bool fileOpened = false; if(mImgFile != NULL) { fclose(mImgFile); mImgFile = NULL; } FILE *imgFile = NULL; // The image file to transfer // Try to open the specified file if(0 == fopen_s(&imgFile, mAsciiFileName, "rb+")) { fclose(imgFile); //if(fopen_s(&mImgFile, "OTATemp.img", "w+")) == 0) DeleteFileA(mTempFileName); CopyFileA(mAsciiFileName, mTempFileName, FALSE); if(0 == fopen_s(&mImgFile, mTempFileName, "rb+")) { fileOpened = true; // Try to determine the length of the specified file mImgFileSize = readFileLength(mImgFile); ResetTransfer(); } } return (fileOpened && (mImgFileSize > 0)); }
/* ** Receive data from the Host and save to flash */ void OnFlashImgDowloadFrame( Dci_Context* pctxt) { byte buff[MAX_MSG_SIZE]; byte *pdata; int lenMsg, i, nErr, ec = 0; byte idStatus; long addr; ushort ctPad = 0; Dci_BinaryImageTransferFrame* pBitf = (Dci_BinaryImageTransferFrame*) pctxt->pMsg; // check transfer id if (!IsTransferring( pBitf->idTransfer ) ) return; // Check frame id and set up status. idStatus = (pBitf->idFrame != s_idFrame) ? BSE_FrameError : ( s_idFrame < (s_flashHdr.bitParams.ctFrames-1)) ? BSE_ReadyNextFrame : BSE_TransferComplete; /*---- write frame to EEPROM ----*/ addr = FLASH_IMAGE_OFFSET + (pBitf->idFrame * s_flashHdr.bitParams.sizeFrame); pdata = Dci_BinaryImageTransferFrame_GetData(pBitf); //Pad the frame with zero so we have writing on even boundaries. if( pBitf->ctBytes < s_flashHdr.bitParams.sizeFrame) { //Calculate the number of bytes to pad out to boundary. ctPad = FLASH_FRAME_SIZE - (pBitf->ctBytes & (FLASH_FRAME_SIZE-1)); memset( pdata + pBitf->ctBytes, 0, ctPad); //zero out remaining elements. } //Write the data and/or any padded bytes to the flash. ec = Flash_Write( addr, pBitf->ctBytes + ctPad, pdata); if( ec ) { idStatus = BSE_WriteError; ResetTransfer(); } //Send the current status so process can continue. lenMsg = Dci_BinaryImageTransferStatus_Init( buff, WCACOMP_FLASH, pBitf->idTransfer, s_idFrame, pBitf->ctBytes, idStatus); Dciproc_SendMessage1(buff, lenMsg, false, pctxt); if( !ec ) ++s_idFrame; // If transfer complete finish writing whole block and header. if( idStatus == BSE_TransferComplete ) { //Write Flash Header with calculated check sum value. s_flashHdr.crc = 0x0; //TODO Calculate Checksum Flash_Hdr_Write(&s_flashHdr); } pctxt->bHandled = true; }
/* ** Handle Bit Status Message to report Flash Data to the Host. */ void OnFlashImgUploadFrame(Dci_Context* pctxt) { byte buff[MAX_MSG_SIZE]; uint32 addr; int lenMsg; int ec = 0x0; int i; int nErr = 0; //Switch on the Status Message ID. Dci_BinaryImageTransferStatus *pMsg = (Dci_BinaryImageTransferStatus *) pctxt->pMsg; //Do not proceed if we are not actually doing a binary transfer upload. if( !IsTransferring( pMsg->idTransfer)) return; switch(pMsg->idStatus) { case(BSE_InitiatingTransfer): // Ready to send. // Host indicates we can start sending now (Send Enable). s_ctBytesRemaining = s_flashHdr.bitParams.sizeImg; // Continue to respond with a frame. case(BSE_ReadyNextFrame): // Send next frame. // Send a frame of data. { byte *pdata; uint16 ctBytes; // Compute the Flash read address. addr = FLASH_IMAGE_OFFSET + ((uint32)s_idFrame * (uint32)s_flashHdr.bitParams.sizeFrame); ctBytes = (s_ctBytesRemaining > s_flashHdr.bitParams.sizeFrame) ? s_flashHdr.bitParams.sizeFrame : (uint16) s_ctBytesRemaining; // Create Frame message and get point to read data into it. lenMsg = Dci_BinaryImageTransferFrame_Init( buff, WCACOMP_FLASH, pMsg->idTransfer, 0, s_idFrame, ctBytes, NULL); pdata = Dci_BinaryImageTransferFrame_GetData( (Dci_BinaryImageTransferFrame *)buff ); ec = Flash_Read(addr, (int)ctBytes, pdata); // Verify. #if defined(CJC_VERIFIED_THIS_GOOD) for(i=0; i<256; i++) { if( *(pdata+i) != (byte)i ) ++nErr; } #endif //If everything ok, send it. if( ec == 0x0 ) { Dciproc_SendMessage1(buff, lenMsg, false, pctxt); ++s_idFrame; s_ctBytesRemaining -= ctBytes; if( s_idFrame < s_flashHdr.bitParams.ctFrames) { break; //Done get out. } } else // Send the error message and then quit. { // If we get here, a read error of some sort ocurred, drop out // and kill the transfer operation. lenMsg = Dci_BinaryImageTransferStatus_Init( buff, WCACOMP_FLASH, pMsg->idTransfer, s_idFrame, 0, BSE_ReadError); Dciproc_SendMessage1(buff, lenMsg, false, pctxt); } //if we get here, we failed ResetTransfer(); } break; // Default conditions hault transfer case(BSE_FrameError): case(BSE_OperationNotAvailable): case(BSE_WriteError): default: ResetTransfer(); break; } pctxt->bHandled = true; }