Example #1
0
void schMoveService(void)
{
	char	logBuffer[LOG_BUFFER_SIZE];
	char	fileStr[200];
	char	folderStr[200];
	static	char	schMoveActualFileName[128];
	static	char	schMoveNewFileName[128];
	int	fileNumber = 0;
	char	fileNumberStr[10];
	int	totalFileCount = 0, i = 0;
	TYPE_PlayInfo	CurrentPlaybackInfo;
	char	playInfoOverRun[512];
	TYPE_RecInfo	CurrentRecordInfo0;
	char	rec0InfoOverRun[512];
	TYPE_RecInfo	CurrentRecordInfo1;
	char	rec1InfoOverRun[512];
	char	CurrentPlaybackFileName[TS_FILE_NAME_SIZE];
	char	CurrentRecord0FileName[SCH_REC_FILE_NAME_SIZE];
	char	CurrentRecord1FileName[SCH_REC_FILE_NAME_SIZE];
	bool	schFileFound = FALSE;
	static	TYPE_File	tempFile;
	static	dword	schMoveCurrentTime = 0;
	static	byte	schMoveIndex = 0;
	static	bool	schMoveExpired = FALSE;
	static	bool	schMoveSuccessful = FALSE;
	static	bool	moveFailed = FALSE;
	int	result = 0;
	bool	schMoveResult = FALSE;
	dword	moveStartTimeInMins = 0, fileStartTimeInMins = 0;

	char	buffer1[256];

	switch(schMoveServiceSV)
	{
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_INITIALISE:

		if(schFileRetreiveMoveData() > 0)
		{
		}
		else
		{
		}

		schMoveServiceSV = SCH_MOVE_SERVICE_WAIT_TO_CHECK;

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_WAIT_TO_CHECK:

		if
		(
			(schTimeSec == 40)
			&&
			(schMainTotalValidMoves > 0)
			&&
			(schStartUpCounter > SCH_MAIN_DELAY_MOVE_ALARM)
			&&
			(schKeepServiceSV == SCH_KEEP_SERVICE_WAIT_TO_CHECK)
		)
		{

			schMoveIndex = schMainTotalValidMoves - 1;

			schMoveCurrentTime = (schTimeMjd << 16) + (schTimeHour << 8) + schTimeMin;

			schMoveExpired = FALSE;

			moveFailed = FALSE;

			schMoveServiceSV = SCH_MOVE_SERVICE_CHECK_MOVE;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_CHECK_MOVE:

		if (schMoveCurrentTime >= schMoveData[schMoveIndex].moveEndTime)
		{
			schMoveSuccessful = FALSE;

			schMoveServiceSV = SCH_MOVE_SERVICE_CHECK_ARCHIVE;
		}
		else
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_NEXT_MOVE;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_CHECK_ARCHIVE:

		memset(schMoveActualFileName, 0 , sizeof(schMoveActualFileName));

		if(GotoDataFiles() == TRUE)
		{
			moveStartTimeInMins = schMainConvertTimeToMins(schMoveData[schMoveIndex].moveStartTime);

			totalFileCount = TAP_Hdd_FindFirst(&tempFile); 

			schFileFound = FALSE;
			for ( i=1; ((i <= totalFileCount) && (schFileFound == FALSE)) ; i++ )
			{
				fileStartTimeInMins = (tempFile.mjd * 24 * 60) + (tempFile.hour * 60) + tempFile.min;

				if
				(
					((moveStartTimeInMins - 1) <= fileStartTimeInMins)
					&&
					(tempFile.attr == ATTR_TS)
					&&
					(strlen(schMoveData[schMoveIndex].moveFileName) > 4)
					&&
					(strncmp(tempFile.name, schMoveData[schMoveIndex].moveFileName, (strlen(schMoveData[schMoveIndex].moveFileName) - 4)) == 0)
				)
				{
					strcpy(schMoveActualFileName, tempFile.name);

					schFileFound = TRUE;			
				}
				else
				{
					TAP_Hdd_FindNext(&tempFile);
				}
			}

			if(schFileFound == TRUE)
			{
				schMoveServiceSV = SCH_MOVE_SERVICE_CHECK_TARGET_FOLDER;
			}
			else
			{
				schMoveServiceSV = SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL;
			}
		}
		else
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_CHECK_TARGET_FOLDER:

		memset(schMoveNewFileName, 0 , sizeof(schMoveNewFileName));

		sprintf( folderStr, "DataFiles/%s", schMoveData[schMoveIndex].moveFolder );
		if(GotoPath(folderStr) == TRUE)
		{
			if (TAP_Hdd_Exist( schMoveActualFileName ) == FALSE)
			{
				schMoveServiceSV = SCH_MOVE_SERVICE_PERFORM_MOVE;
			}
			else
			{
				fileNumber = 2;

				sprintf(fileNumberStr, "-%d", fileNumber);

				if(strlen(schMoveActualFileName) > 4)
				{
					strncpy(schMoveNewFileName, schMoveActualFileName,(strlen(schMoveActualFileName) - 4));
					strcat(schMoveNewFileName, fileNumberStr);
					strcat(schMoveNewFileName, ".rec");
			
					while
					(
						(TAP_Hdd_Exist( schMoveNewFileName ) == TRUE)
						&&
						(fileNumber < 100)
					)
					{
						fileNumber++;

						memset(fileNumberStr, 0 , sizeof(fileNumberStr));
						sprintf(fileNumberStr, "-%d", fileNumber);
						
						memset(schMoveNewFileName, 0 , sizeof(schMoveNewFileName));
						strncpy(schMoveNewFileName, schMoveActualFileName,(strlen(schMoveActualFileName) - 4));
						strcat(schMoveNewFileName, fileNumberStr);
						strcat(schMoveNewFileName, ".rec");
					}
				}
				else
				{
					fileNumber = 100;
				}

				if(fileNumber < 100)
				{
					schMoveServiceSV = SCH_MOVE_SERVICE_PERFORM_MOVE;
				}
				else
				{
					schMoveServiceSV = SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL;
				}
			}
		}
		else
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_PERFORM_MOVE:

		schMoveSuccessful = FALSE;

		if(GotoDataFiles() == TRUE)
		{
			TAP_Hdd_GetPlayInfo (&CurrentPlaybackInfo);
			TAP_Hdd_GetRecInfo (0, &CurrentRecordInfo0);
			TAP_Hdd_GetRecInfo (1, &CurrentRecordInfo1);

			memset(CurrentPlaybackFileName,0,TS_FILE_NAME_SIZE);
			if(CurrentPlaybackInfo.playMode == PLAYMODE_Playing)
			{
				strcpy(CurrentPlaybackFileName,CurrentPlaybackInfo.file->name);
			}

			memset(CurrentRecord0FileName,0,SCH_REC_FILE_NAME_SIZE);
			if
			(
				(CurrentRecordInfo0.recType == RECTYPE_Normal)
				&&
				(schMoveData[schMoveIndex].moveStartTime == CurrentRecordInfo0.startTime)
			)
			{
				strcpy(CurrentRecord0FileName,CurrentRecordInfo0.fileName);
			}

			memset(CurrentRecord1FileName,0,SCH_REC_FILE_NAME_SIZE);
			if
			(
				(CurrentRecordInfo1.recType == RECTYPE_Normal)
				&&
				(schMoveData[schMoveIndex].moveStartTime == CurrentRecordInfo1.startTime)
			)
			{
				strcpy(CurrentRecord1FileName,CurrentRecordInfo1.fileName);
			}

			if
			(
				(strcmp(CurrentPlaybackFileName, schMoveActualFileName) != 0)
				&&
				(strcmp(CurrentRecord0FileName, schMoveActualFileName) != 0)
				&&
				(strcmp(CurrentRecord1FileName, schMoveActualFileName) != 0)
			)
			{
				if(strlen(schMoveNewFileName) > 0)
				{
					if(TAP_Hdd_Rename(schMoveActualFileName, schMoveNewFileName) == TRUE)
					{
						memset(schMoveActualFileName, 0 , sizeof(schMoveActualFileName));
						strcpy(schMoveActualFileName, schMoveNewFileName);
					}
				}

				sprintf( folderStr, "/DataFiles/%s", schMoveData[schMoveIndex].moveFolder );
#ifndef WIN32
				if(schMainApiMoveAvailable == TRUE)
				{
					schMoveResult = TAP_Hdd_ApiMove( "/DataFiles", folderStr, schMoveActualFileName );
				}
				else if(schMainDebugMoveAvailable == TRUE)
				{
					sprintf( fileStr, "/DataFiles/%s", schMoveActualFileName );

					schMoveResult = TAP_Hdd_DebugMove( fileStr, folderStr );
				}
				else
				{
				}

#else
				schMoveResult = TRUE;	// For the SDK
#endif
				if(schMoveResult == TRUE)
				{
					schMoveSuccessful = TRUE;

					schMoveData[schMoveIndex].moveEnabled = FALSE;

					schMoveExpired = TRUE;				// Delete this move when all moves checked
				}
			}
		}

		if(schMoveSuccessful == TRUE)
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_NEXT_MOVE;
		}
		else
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_MOVE_NOT_SUCCESSFUL:

		schMoveData[schMoveIndex].moveFailedCount++;

		if(schMoveData[schMoveIndex].moveFailedCount >= 3)
		{
			schMoveData[schMoveIndex].moveEnabled = FALSE;

			schMoveExpired = TRUE;				// Delete this move when all moves checked

			moveFailed = TRUE;

//			memset(logBuffer,0,LOG_BUFFER_SIZE);
//			sprintf( logBuffer, "Failed (Move to be deleted): %s    Count: %d", schMoveData[schMoveIndex].moveFileName, schMoveData[schMoveIndex].moveFailedCount  );
//			logStoreEvent(logBuffer);		
		}
		else
		{
//			memset(logBuffer,0,LOG_BUFFER_SIZE);
//			sprintf( logBuffer, "Failed: %s    Count: %d", schMoveData[schMoveIndex].moveFileName, schMoveData[schMoveIndex].moveFailedCount  );
//			logStoreEvent(logBuffer);		
		}

		schMoveServiceSV = SCH_MOVE_SERVICE_NEXT_MOVE;

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_NEXT_MOVE:

		if(schMoveIndex > 0)
		{
			schMoveIndex--;

			schMoveServiceSV = SCH_MOVE_SERVICE_CHECK_MOVE;
		}
		else
		{
			if(schMoveExpired == TRUE)
			{
				schMoveServiceSV = SCH_MOVE_SERVICE_DELETE_EXPIRED_MOVES;
			}
			else
			{
				schMoveServiceSV = SCH_MOVE_SERVICE_COMPLETE;
			}
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_DELETE_EXPIRED_MOVES:

		schWriteMoveList();

//		memset(logBuffer,0,LOG_BUFFER_SIZE);
//		sprintf( logBuffer, "Move list updated" );
//		logStoreEvent(logBuffer);		

		if (moveFailed == FALSE)
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_COMPLETE;
		}
		else
		{
			schMoveServiceSV = SCH_MOVE_LOG_ARCHIVE;
		}

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_LOG_ARCHIVE:

		logArchive();

		schMoveServiceSV = SCH_MOVE_SERVICE_COMPLETE;

		break;
	/*--------------------------------------------------*/
	case SCH_MOVE_SERVICE_COMPLETE:

		if(schTimeSec != 40)
		{
			schMoveServiceSV = SCH_MOVE_SERVICE_WAIT_TO_CHECK;
		}

		break;
	/*--------------------------------------------------*/
	default:

		break;
	/*--------------------------------------------------*/
	}
}
tFileInUse HDD_isFileInUse(char *FileName)
{
  TRACEENTER();

  TYPE_PlayInfo         PlayInfo;
  int                   i, NrRecSlots;
  char                  WorkingFileName[FBLIB_DIR_SIZE];
  char                  AbsFileName[FBLIB_DIR_SIZE];
  TYPE_File            *RecFile;

  if(!FileName || !*FileName)
  {
    TRACEEXIT();
    return FIU_No;
  }

  //Convert to an Linux path and cut away any .inf or .nav
  ConvertPathType(FileName, AbsFileName, PF_FullLinuxPath);
  if(StringEndsWith(AbsFileName, ".inf")) AbsFileName[strlen(AbsFileName) - 4] = '\0';

  //Is any Playback running
  TAP_Hdd_GetPlayInfo(&PlayInfo);
  if(PlayInfo.playMode && PlayInfo.totalBlock > 0)
  {
    //Compare the full path of both files
    HDD_GetAbsolutePathByTypeFile(PlayInfo.file, WorkingFileName);
    if(StringEndsWith(WorkingFileName, ".inf") || StringEndsWith(WorkingFileName, ".nav")) WorkingFileName[strlen(WorkingFileName) - 4] = '\0';

    if(!strcmp(AbsFileName, WorkingFileName))
    {
      if(PlayInfo.playMode == PLAYMODE_Mp3)
      {
        TRACEEXIT();
        return FIU_PlayMP3;
      }
      else
      {
        TRACEEXIT();
        return FIU_Playback;
      }
    }
  }

  //Loop through all recording slots
  NrRecSlots = (int)HDD_NumberOfRECSlots();
  for(i = 0; i < NrRecSlots; i++)
  {
    //Get the full path of the rec file
    if(HDD_GetRecSlotFiles(i, &RecFile, NULL, NULL) && HDD_GetAbsolutePathByTypeFile(RecFile, WorkingFileName))
    {
      //Check if both paths are equal
      if(!strcmp(AbsFileName, WorkingFileName))
      {
        TRACEEXIT();
        return (FIU_RecSlot1 + i);
      }
    }
  }

  TRACEEXIT();
  return FIU_No;
}
void CheckPlaybackStatus( void )
{
   bool         matchFound;
   int          i, l, l1, l2;
  
     
   // Check for Playbackmode
   iOverRunCheck = ApiOverRunCheck;   // Set flag to catch any buffer overrun from API call.
   TAP_Hdd_GetPlayInfo (&CurrentPlaybackInfo);

   if ( iOverRunCheck != ApiOverRunCheck )  // TAP_Hdd_GetPlayInfo V1.22 has a buffer overrun issue, so check if it has overrun by more than 128 bytes.
   {
      ShowMessageWin( rgn, "Archive:  Error retrieving Playback Info", "TAP_Hdd_GetPlayInfo buffer overrun", "in 'CheckPlaybackStatus'.", 500 );
      return;
   }

   // Check the various playmodes to see if we are playing a file back.
   // PLAYMODE_None               Nothing playing
   // PLAYMODE_Playing            Playing a file
   // PLAYMODE_RecPlaying         Playing a currently recording file
   //
   // If we're not in playback then just return.
   if (CurrentPlaybackInfo.playMode != PLAYMODE_Playing)   // We're not in playback mode  
   {
       if (inPlaybackMode) // We were in playback mode, but now we've stopped.
       {
           SaveDatToFile(); // Write the information (particularly the last played file details) to disk
       }
       inPlaybackMode = FALSE;
       return;
   }    
   
   inPlaybackMode = TRUE;

   // Assign variable to the file entry for easier use.
   CurrentPlaybackFile = CurrentPlaybackInfo.file;
   
   // Copy the current playing info to the 'last' variable so we can restart the very last playback.
   playedFiles[0]->startCluster = CurrentPlaybackFile->startCluster;   // Save the disk start cluster.
   playedFiles[0]->currentBlock = CurrentPlaybackInfo.currentBlock;   // Save the current block position.
   playedFiles[0]->totalBlock   = CurrentPlaybackInfo.totalBlock;     // Save the total block size.
   strcpy(playedFiles[0]->name,CurrentPlaybackFile->name);        // Save the file name.
   
   matchFound = FALSE;
   
   // Search through the existing played file list to see if we have a match.
   for (i=1; i<=numberOfPlayedFiles; i++)
   {
        l1 = strlen(playedFiles[i]->name);
        l2 = strlen(CurrentPlaybackFile->name);
        l = max(l1, l2);
        if ((playedFiles[i]->startCluster == CurrentPlaybackFile->startCluster) && (strncmp(playedFiles[i]->name,CurrentPlaybackFile->name,TS_FILE_NAME_SIZE)==0))
        {
              // If we match an existing entry in our playedFiles array, update the currentBlock position information.
              playedFiles[i]->currentBlock = CurrentPlaybackInfo.currentBlock;
              matchFound = TRUE;
              break; // We've found a match, so don't bother checking other playback entries.
        }
   }

   // If we didn't find a match in our existing list of played files, so add to our list if we have space.   
   if ((!matchFound) && (numberOfPlayedFiles < MAX_FILES) && (strcmp(CurrentPlaybackFile->name,"__temprec__.ts")!=0))
   {
         numberOfPlayedFiles++;  // Increase the number of Played Files.
         playedFiles[numberOfPlayedFiles] = TAP_MemAlloc( sizeof (*playedFiles[numberOfPlayedFiles]));
         playedFiles[numberOfPlayedFiles]->startCluster = CurrentPlaybackFile->startCluster;   // Save the disk start cluster.
         playedFiles[numberOfPlayedFiles]->currentBlock = CurrentPlaybackInfo.currentBlock;   // Save the current block position.
         playedFiles[numberOfPlayedFiles]->totalBlock   = CurrentPlaybackInfo.totalBlock;     // Save the total block size.
         strcpy(playedFiles[numberOfPlayedFiles]->name,CurrentPlaybackFile->name);        // Save the file name.
         playinfoChanged = TRUE;  // Set flag so that playback info will get written to disk.
   }     
        
                
     
}