/*Function: SYS_FS_HANDLE SYS_FS_FileOpen_Wrapper(const char *fname, SYS_FS_FILE_OPEN_ATTRIBUTES attributes); ***************************************************************************/ SYS_FS_HANDLE SYS_FS_FileOpen_Wrapper(const char *fname, SYS_FS_FILE_OPEN_ATTRIBUTES attributes) { char localSitePath[SYS_FS_MAX_PATH + 1]; const char *fnameBuf = SYS_FS_FileNameFormat(fname, localSitePath, sizeof(localSitePath)); return (fnameBuf == 0) ? SYS_FS_HANDLE_INVALID : SYS_FS_FileOpen(fnameBuf, attributes); }
/*Function: SYS_FS_HANDLE SYS_FS_FileOpen_Wrapper(const char *fname, SYS_FS_FILE_OPEN_ATTRIBUTES attributes); ***************************************************************************/ SYS_FS_HANDLE SYS_FS_FileOpen_Wrapper(const char *fname, SYS_FS_FILE_OPEN_ATTRIBUTES attributes) { uint32_t compareResult, strCopyLoop; SYS_FS_HANDLE fileOpenResult; uint8_t localSitePath[WEBSITE_PATH_LENGTH+12]; uint8_t fnameString[WEBSITE_PATH_LENGTH]; const char *fnameBuf; for (strCopyLoop=0; strCopyLoop <WEBSITE_PATH_LENGTH; strCopyLoop++) fnameString[strCopyLoop] = 0; for (strCopyLoop=0; strCopyLoop <WEBSITE_PATH_LENGTH+12; strCopyLoop++) localSitePath[strCopyLoop] = 0; fnameBuf = fname; /* Copy fname into a string */ for (strCopyLoop=0; strCopyLoop <WEBSITE_PATH_LENGTH; fnameBuf++, strCopyLoop++) fnameString[strCopyLoop] = *fnameBuf; /* Copy the local website path to to a string*/ strncpy((char*)localSitePath,(const char*)LOCAL_WEBSITE_PATH, strlen((const char*)LOCAL_WEBSITE_PATH)); /* Check web path name to see if it already contains local website name*/ compareResult = strncmp((const char *)fnameString,(const char *)localSitePath,strlen((char *)localSitePath)); /* If it does*/ if (compareResult == 0) { /* Pass the fname directly to the FileOpen function*/ fileOpenResult = SYS_FS_FileOpen(fname, attributes); } /* If not*/ else { /*Append local path to web path*/ strncat((char*)localSitePath,(const char*)fname,WEBSITE_PATH_LENGTH+12); fileOpenResult = SYS_FS_FileOpen((const char *)localSitePath, attributes); } return fileOpenResult; }
void APP_Tasks ( void ) { /* The application task state machine */ switch(appData.state) { case APP_MOUNT_DISK: if(SYS_FS_Mount("/dev/nvma1", "/mnt/myDrive", FAT, 0, NULL) != 0) { /* The disk could not be mounted. Try * mounting again untill success. */ appData.state = APP_MOUNT_DISK; } else { /* Mount was successful. Open a file. * Let the switch case fall through. */ appData.state = APP_OPEN_FILE; } break; case APP_OPEN_FILE: appData.fileHandle = SYS_FS_FileOpen("/mnt/myDrive/FILE.TXT", SYS_FS_FILE_OPEN_READ_PLUS); if(appData.fileHandle == SYS_FS_HANDLE_INVALID) { /* Could not open the file. Error out*/ appData.state = APP_ERROR; } else { /* Check the file status */ appData.state = APP_READ_FILE_STAT; } break; case APP_READ_FILE_STAT: if( SYS_FS_FileStat("/mnt/myDrive/FILE.TXT", &appData.fileStatus ) == SYS_FS_RES_FAILURE ) { /* Reading file status was a failure */ appData.state = APP_ERROR; } else { /* Read file size */ appData.state = APP_READ_FILE_SIZE; } break; case APP_READ_FILE_SIZE: appData.fileSize = SYS_FS_FileSize( appData.fileHandle ); if(appData.fileSize == -1) { /* Reading file size was a failure */ appData.state = APP_ERROR; } else { if(appData.fileSize == appData.fileStatus.fsize) { appData.state = APP_DO_FILE_SEEK; } else appData.state = APP_ERROR; } break; case APP_DO_FILE_SEEK: if(SYS_FS_FileSeek( appData.fileHandle, 4, SYS_FS_SEEK_SET ) == -1) { /* File seek caused an error */ appData.state = APP_ERROR; } else { /* Check for End of file */ appData.state = APP_CHECK_EOF; } break; case APP_CHECK_EOF: if(SYS_FS_FileEOF( appData.fileHandle ) == false ) { /* Either, EOF is not reached or there was an error In any case, for the application, its an error condition */ appData.state = APP_ERROR; } else { appData.state = APP_DO_ANOTHER_FILE_SEEK; } break; case APP_DO_ANOTHER_FILE_SEEK: /* Move file pointer to begining of file */ if(SYS_FS_FileSeek( appData.fileHandle, (-1 * appData.fileSize), SYS_FS_SEEK_END ) == -1) { /* File seek caused an error */ appData.state = APP_ERROR; } else { /* Check for original file content */ appData.state = APP_READ_ORIGINAL_FILE_CONTENT; } break; case APP_READ_ORIGINAL_FILE_CONTENT: if(SYS_FS_FileRead((void *)appData.data, 4, appData.fileHandle) == -1) { /* There was an error while reading the file. * Close the file and error out. */ SYS_FS_FileClose(appData.fileHandle); appData.state = APP_ERROR; } else { if(memcmp(appData.data, originalData, 4) != 0) { /* The written and the read data dont * match. */ appData.state = APP_ERROR; } else { /* The test was successful. Lets do a file seek to move to begin of file . */ appData.state = APP_FINAL_FILE_SEEK; } break; } case APP_FINAL_FILE_SEEK: /* Move file pointer to begining of file */ if(SYS_FS_FileSeek( appData.fileHandle, (-1 * appData.fileSize), SYS_FS_SEEK_END ) == -1) { /* File seek caused an error */ appData.state = APP_ERROR; } else { /* Do a file write now */ appData.state = APP_WRITE_TO_FILE; } break; case APP_WRITE_TO_FILE: if(SYS_FS_FileWrite((const void *)writeData, 13, appData.fileHandle) == -1) { /* Write was not successful. Close the file * and error out.*/ SYS_FS_FileClose(appData.fileHandle); appData.state = APP_ERROR; } else { /* Write was successful. Close the file and * open again for read. */ SYS_FS_FileClose(appData.fileHandle); appData.state = APP_OPEN_FOR_READ; } break; case APP_OPEN_FOR_READ: appData.fileHandle = SYS_FS_FileOpen("/mnt/myDrive/FILE.TXT", SYS_FS_FILE_OPEN_READ); if(appData.fileHandle == SYS_FS_HANDLE_INVALID) { /* Could not open the file. Error out*/ appData.state = APP_ERROR; } else { /* Try wrting to the file. Let the * switch case fall through. */ appData.state = APP_READ_VERIFY_FILE; } break; case APP_READ_VERIFY_FILE: if(SYS_FS_FileRead((void *)appData.data, 13, appData.fileHandle) == -1) { /* There was an error while reading the file. * Close the file and error out. */ SYS_FS_FileClose(appData.fileHandle); appData.state = APP_ERROR; } else { if(strcmp((const char *)appData.data, (const char *)writeData) != 0) { /* The written and the read data dont * match. */ appData.state = APP_ERROR; } else { /* The test was successful. Lets idle. */ appData.state = APP_IDLE; } SYS_FS_FileClose(appData.fileHandle); break; } case APP_IDLE: /* The appliction comes here when the demo * has completed successfully. Switch on * green LED. */ BSP_SwitchONSuccessLED(); break; case APP_ERROR: /* The appliction comes here when the demo * has failed. Switch on the red LED.*/ BSP_SwitchONFailureLED(); break; default: break; } } //End of APP_Tasks
void APP_Tasks ( void ) { /* The application task state machine */ switch(appData.state) { case APP_MOUNT_DISK: if(SYS_FS_Mount("/dev/nvma1", "/mnt/myDrive", MPFS2, 0, NULL) != 0) { /* The disk could not be mounted. Try * mounting again untill success. */ appData.state = APP_MOUNT_DISK; } else { /* Mount was successful. opens * the first file */ appData.state = APP_OPEN_FILE_1; } break; case APP_OPEN_FILE_1: appData.fileHandle_1 = SYS_FS_FileOpen("/mnt/myDrive/FILE.TXT", SYS_FS_FILE_OPEN_READ); if(appData.fileHandle_1 == SYS_FS_HANDLE_INVALID) { /* Could not open the file. Error out*/ appData.state = APP_ERROR; } else { /* If first file was opened successfully, open the second file */ appData.state = APP_OPEN_FILE_2; } break; case APP_OPEN_FILE_2: appData.fileHandle_2 = SYS_FS_FileOpen("/mnt/myDrive/TEST.txt", SYS_FS_FILE_OPEN_READ); if(appData.fileHandle_2 == SYS_FS_HANDLE_INVALID) { /* Could not open the file. Error out*/ appData.state = APP_ERROR; } else { /* If first file was opened successfully, check status of 3rd file */ appData.state = APP_CHECK_FILE_3_STAT; } break; case APP_CHECK_FILE_3_STAT: if(SYS_FS_FileStat("/mnt/myDrive/ABC.txt", &appData.fileStatus) == SYS_FS_RES_FAILURE) { /* Could not read the file staus. Error out*/ appData.state = APP_ERROR; } else { /* While creating the MPFS disk image, the file ABC.txt was chose to be of the following size */ if(appData.fileStatus.fsize != 31744) { /* reading file status had been wrong somewhere */ appData.state = APP_ERROR; } else { /* If status read was success, then do a file size check */ appData.state = APP_DO_FILE_SIZE_CHECK; } } break; case APP_DO_FILE_SIZE_CHECK: if(SYS_FS_FileSize(appData.fileHandle_1) != 11) { /* File size check went wrong somewhere */ appData.state = APP_ERROR; } else { /* If status read was success, then do a file seek */ appData.state = APP_DO_FILE_SEEK; } /* We are done with this file, hence close it */ SYS_FS_FileClose(appData.fileHandle_1); break; case APP_DO_FILE_SEEK: if(SYS_FS_FileSeek(appData.fileHandle_2, -10, SYS_FS_SEEK_END) != -10) { /* File seek went wrong somewhere */ appData.state = APP_ERROR; } else { /* Compare the remaining file content with a known string */ appData.state = APP_READ_VERIFY_CONTENT; } break; case APP_READ_VERIFY_CONTENT: if(SYS_FS_FileRead((void *)appData.data, 10, appData.fileHandle_2) == -1) { /* There was an error while reading the file. * Close the file and error out. */ SYS_FS_FileClose(appData.fileHandle_2); appData.state = APP_ERROR; break; } else { if(strncmp((const char *)appData.data, (const char *)compareString, 10) != 0) { /* The written and the read data dont * match. */ appData.state = APP_ERROR; } else { /* The test was successful. Lets idle. */ appData.state = APP_CHECK_EOF; } break; } case APP_CHECK_EOF: /* By now, we should have reached end of file */ if(SYS_FS_FileEOF(appData.fileHandle_2) != true) { /* Error */ appData.state = APP_ERROR; } else { /* We have completed all tests. Hence go to idle state */ appData.state = APP_IDLE; } case APP_IDLE: /* The appliction comes here when the demo * has completed successfully. Switch on * green LED. */ SYS_FS_FileClose(appData.fileHandle_2); BSP_SwitchONSuccessLED(); break; case APP_ERROR: /* The appliction comes here when the demo * has failed. Switch on the red LED.*/ BSP_SwitchONFailureLED(); break; default: break; } } //End of APP_Tasks