コード例 #1
0
ファイル: osfileapi.c プロジェクト: TomCrowley-ME/me_sim_test
/* --------------------------------------------------------------------------------------
Name: OS_ShellOutputToFile

Purpose: Takes a shell command in and writes the output of that command to the specified file

Returns: OS_SUCCESS if success
         OS_FS_ERR_INVALID_FD if the file descriptor passed in is invalid
         OS_FS_ERROR if Error
---------------------------------------------------------------------------------------*/
int32 OS_ShellOutputToFile(char* Cmd, int32 OS_fd)
{
    char LocalCmd [OS_MAX_CMD_LEN];
    int32 Result;
    int32 ReturnCode = OS_FS_SUCCESS;
    int32 fdCmd;
    char * shellName;

    /*
    ** Check parameters
    */
    if (Cmd == NULL)
    {
        return(OS_FS_ERR_INVALID_POINTER);
    }

    /* Make sure the file descriptor is legit before using it */
    if (OS_fd < 0 || OS_fd >= OS_MAX_NUM_OPEN_FILES || OS_FDTable[OS_fd].IsValid == FALSE)
    {
        ReturnCode = OS_FS_ERR_INVALID_FD;
    }
    else
    {
        /* Create a file to write the command to (or write over the old one) */
        fdCmd = OS_creat(OS_SHELL_CMD_INPUT_FILE_NAME,OS_READ_WRITE);

        if (fdCmd < OS_FS_SUCCESS)
        {
            Result = OS_FS_ERROR;
        }

        else
        {
            /* copy the command to the file, and then seek back to the beginning of the file */

            strncpy(LocalCmd,Cmd, OS_MAX_CMD_LEN);
            OS_write(fdCmd,Cmd, strlen(LocalCmd));
            OS_lseek(fdCmd,0,OS_SEEK_SET);

            /* Create a shell task the will run the command in the file, push output to OS_fd */
            Result = shellGenericInit("INTERPRETER=Cmd",0,NULL, &shellName, FALSE, FALSE, OS_FDTable[fdCmd].OSfd,	OS_FDTable[OS_fd].OSfd, OS_FDTable[OS_fd].OSfd);

            /* Wait for the command to terminate */
           do{
              taskDelay(sysClkRateGet());
            }while (taskNameToId(shellName) != ERROR);

            /* Close the file descriptor */
            OS_close(fdCmd);

        } /* else */

        if (Result != OK)
        {
             ReturnCode =  OS_FS_ERROR;
        }

    }
    return ReturnCode;
}/* end OS_ShellOutputToFile */
コード例 #2
0
ファイル: fm_utest.c プロジェクト: CoreflightModeler/cfs-fm
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void CreateTestFile(char *Filename, int SizeInKs, boolean LeaveOpen)
{
    int FileHandle;
    int i;

    FileHandle = OS_creat(Filename, OS_READ_WRITE);
    if (FileHandle >= OS_SUCCESS)
    {
        if (FillBuffer[0] == 0)
        {
            CFE_PSP_MemSet(FillBuffer, 0xA5, sizeof(FillBuffer));
        }

        for (i = 0; i < SizeInKs; i++)
        {
            OS_write(FileHandle, FillBuffer, sizeof(FillBuffer));
        }

        if (LeaveOpen)
        {
            OpenFileHandle = FileHandle;
        }
        else
        {
            OS_close(FileHandle);
        }
    }
    else
    {
        UTF_put_text("\nERROR CREATING TEST FILE %s\n", Filename);
    }

    return;

} /* End of CreateTestFile() */
コード例 #3
0
ファイル: tblmgr.c プロジェクト: Open-Sat/ObjBased-App
/******************************************************************************
** Function: DumpTableToFile
**
*/
static boolean DumpTableToFile(const char* FileName, const char* FileDescr, DumpTableFuncPtr DumpTableFunc)
{

   CFE_FS_Header_t  CfeStdFileHeader;
   int32            FileHandle;
   int32            FileStatus;
   boolean          RetStatus = FALSE;

   /* Create a new dump file, overwriting anything that may have existed previously */
   FileHandle = OS_creat(FileName, OS_WRITE_ONLY);

   if (FileHandle >= OS_FS_SUCCESS)
   {
      /* Initialize the standard cFE File Header for the Dump File */
      CfeStdFileHeader.SubType = 0x74786574;
      strcpy(&CfeStdFileHeader.Description[0], FileDescr);

      /* Output the Standard cFE File Header to the Dump File */
      FileStatus = CFE_FS_WriteHeader(FileHandle, &CfeStdFileHeader);

      if (FileStatus == sizeof(CFE_FS_Header_t))
      {
         (DumpTableFunc)(FileHandle);
         RetStatus = TRUE;

      } /* End if successfully wrote file header */
      else
      {
          CFE_EVS_SendEvent(TBLMGR_WRITE_CFE_HDR_ERR_EID,
                            CFE_EVS_ERROR,
                            "Error writing cFE File Header to '%s', Status=0x%08X",
                            FileName, FileStatus);

      }
   } /* End if file create */
   else
   {

        CFE_EVS_SendEvent(TBLMGR_CREATE_MSG_DUMP_ERR_EID,
                          CFE_EVS_ERROR,
                          "Error creating CDS dump file '%s', Status=0x%08X",
                          FileName, FileHandle);

    }

   OS_close(FileHandle);

   return RetStatus;

} /* End of DumpTableToFile() */
コード例 #4
0
ファイル: sc_app.c プロジェクト: kingzappo/cfe
void SC_LoadDefaultTables(void)
{
    char    TableName[OS_MAX_PATH_LEN];
    int32   FileDesc;
    int32   RtsIndex;
    int32   RtsCount = 0;

    /*
    ** Currently, only RTS tables are loaded during initialization.
    **
    ** ATS and ATS Append tables must be loaded by command.
    */        
    for (RtsIndex = 0; RtsIndex < SC_NUMBER_OF_RTS; RtsIndex++)
    {
        /* Example filename: /cf/apps/sc_rts001.tbl */
        sprintf(TableName, "%s%03ld.tbl", SC_RTS_FILE_NAME, RtsIndex + 1);
        
        FileDesc = OS_open(TableName, OS_READ_ONLY, 0);
                   
        if (FileDesc >= 0)
        {
            OS_close(FileDesc);
            
            /* Only try to load table files that can be opened, ignore others */
            if(CFE_TBL_Load(SC_OperData.RtsTblHandle[RtsIndex], CFE_TBL_SRC_FILE, TableName) == CFE_SUCCESS){
               RtsCount++;
            }
        }
    }

    /* Display startup RTS load count */
    CFE_EVS_SendEvent(SC_RTS_LOAD_COUNT_INFO_EID, CFE_EVS_INFORMATION,
       "RTS table file load count = %ld", RtsCount);

    return;    

} /* end SC_LoadDefaultTables() */
コード例 #5
0
ファイル: test_tbl_api2.c プロジェクト: Spacecraft-Code/cFE
void Create_Input_File0(void)
{
/*	FILE *to; */
	int8 values[4] = {5,10,15,20};
	char tableName[30];
	CFE_FS_Header_t    StdFileHeader;
    CFE_TBL_File_Hdr_t TblFileHeader;
    int32              FileDescriptor;
    int32              Status;
    int32              EndianCheck = 0x01020304;

/*	to = fopen("/tt_table_values0.dat",  "wb");
	if (to == 0)
	{
		UTF_error("Error opening file /tt_table_initial_values.dat to write\n");
		UTF_exit();
	}

	fwrite(values, sizeof(int8), 4, to);	
	
	fclose(to); */

	strcpy (tableName, "TT.FourNumbers");

	/* Clear Header of any garbage before copying content */
    CFE_PSP_MemSet(&StdFileHeader, 0, sizeof(CFE_FS_Header_t));
    CFE_PSP_MemSet(&TblFileHeader, 0, sizeof(CFE_TBL_File_Hdr_t));

	/* Create a new dump file, overwriting anything that may have existed previously */
    FileDescriptor = OS_creat("/ram/tt_table_values0.dat", OS_WRITE_ONLY);

    if (FileDescriptor >= OS_FS_SUCCESS)
    {
        /* Initialize the standard cFE File Header for the Dump File */
       	StdFileHeader.SubType = CFE_FS_TBL_IMG_SUBTYPE;
       	strcpy(&StdFileHeader.Description[0], "Table Load File");

       	/* Output the Standard cFE File Header to the Dump File */
       	Status = CFE_FS_WriteHeader(FileDescriptor, &StdFileHeader);

        if (Status == sizeof(CFE_FS_Header_t))
       	{
           	/* Initialize the Table Image Header for the Dump File */
           	CFE_PSP_MemCpy(TblFileHeader.TableName, tableName, CFE_TBL_MAX_FULL_NAME_LEN);
           	TblFileHeader.Offset = 0;
            TblFileHeader.NumBytes = 4;
    	    TblFileHeader.Reserved = 0;
            
           	/* Determine if this is a little endian processor */
           	if ((*(char *)&EndianCheck) == 0x04)
           	{
	        	CFE_TBL_ByteSwapTblHeader(&TblFileHeader);
    	    }

            /* Output the Table Image Header to the Dump File */
           	Status = OS_write(FileDescriptor, &TblFileHeader, sizeof(CFE_TBL_File_Hdr_t));

           	/* Make sure the header was output completely */
           	if (Status == sizeof(CFE_TBL_File_Hdr_t))
           	{
	        	/* Output the requested data to the dump file */
    	        /* Output the active table image data to the dump file */
        	    Status = OS_write(FileDescriptor, values, 4);
            }
        }
        	    
        /* We are done writing the load file.  Close it. */
        OS_close(FileDescriptor);
    }
}
コード例 #6
0
ファイル: ds_file.c プロジェクト: CoreflightModeler/cfs-ds
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void DS_FileCloseDest(int32 FileIndex)
{
    DS_AppFileStatus_t *FileStatus = &DS_AppData.FileStatus[FileIndex];

    #if (DS_MOVE_FILES == TRUE)
    /*
    ** Move file from working directory to downlink directory...
    */
    int32 OS_result;
    int32 PathLength;
    char *FileName;
    char PathName[DS_TOTAL_FNAME_BUFSIZE];

    /*
    ** First, close the file...
    */
    OS_close(FileStatus->FileHandle);

    /*
    ** Move file only if table has a downlink directory name...
    */
    if (DS_AppData.DestFileTblPtr->File[FileIndex].Movename[0] != '\0')
    {
        /*
        ** Make sure directory name does not end with slash character...
        */
        strcpy(PathName, DS_AppData.DestFileTblPtr->File[FileIndex].Movename);
        PathLength = strlen(PathName);
        if (PathName[PathLength - 1] == '/')
        {
            PathName[PathLength - 1] = '\0';
            PathLength--;
        }

        /*
        ** Get a pointer to slash character before the filename...
        */
        FileName = strrchr(FileStatus->FileName, '/');

        /*
        ** Verify that directory name plus filename is not too large...
        */
        if ((PathLength + strlen(FileName)) < DS_TOTAL_FNAME_BUFSIZE)
        {
            /*
            ** Append the filename (with slash) to the directory name...
            */
            strcat(PathName, FileName);

            /*
            ** Use OS function to move/rename the file...
            */
            OS_result = OS_mv(FileStatus->FileName, PathName);
            /* OS_result = OS_rename(FileStatus->FileName, PathName); */

            if (OS_result != OS_SUCCESS)
            {
                /*
                ** Error - send event but leave destination enabled...
                */
                CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_ERROR,
                   "FILE MOVE error: src = '%s', tgt = '%s', result = %d",
                    FileStatus->FileName, PathName, OS_result);
            }
        }
        else
        {
            /*
            ** Error - send event but leave destination enabled...
            */
            CFE_EVS_SendEvent(DS_MOVE_FILE_ERR_EID, CFE_EVS_ERROR,
               "FILE MOVE error: dir name = '%s', filename = '%s'",
                PathName, FileName);
        }
    }
    #else
    /*
    ** Close the file...
    */
    OS_close(FileStatus->FileHandle);
    #endif

    /*
    ** Reset status for this destination file...
    */
    FileStatus->FileHandle = DS_CLOSED_FILE_HANDLE;
    FileStatus->FileAge  = 0;
    FileStatus->FileSize = 0;

    /*
    ** Remove previous filename from status data...
    */
    CFE_PSP_MemSet(FileStatus->FileName, 0, DS_TOTAL_FNAME_BUFSIZE);

    return;

} /* End of DS_FileCloseDest() */
コード例 #7
0
ファイル: osfileapi.c プロジェクト: felipeprov/osal
/* --------------------------------------------------------------------------------------
    Name: OS_ShellOutputToFile
    
    Purpose: Takes a shell command in and writes the output of that command to the specified file
    
    Returns: OS_FS_ERROR if the command was not executed properly
             OS_FS_ERR_INVALID_FD if the file descriptor passed in is invalid
             OS_SUCCESS if success
 ---------------------------------------------------------------------------------------*/
int32 OS_ShellOutputToFile(char* Cmd, int32 OS_fd)
{
    int32              cmdFd;
    int32              tmpFd;
    rtems_status_code  rtemsRc;;
    int32              ReturnCode = OS_SUCCESS;
    int32              fileStatus;
    int32              bytesRead;
    int32              bytesWritten;
    char               readBuffer[256];
    char               outputFileName[OS_MAX_PATH_LEN + OS_SHELL_TMP_FILE_EXT_LEN];
    char               localCmd[OS_MAX_CMD_LEN]; 

    /* Make sure the file descriptor is legit before using it */
    if (OS_fd < 0 || OS_fd >= OS_MAX_NUM_OPEN_FILES || OS_FDTable[OS_fd].IsValid == FALSE)
    {
        ReturnCode = OS_FS_ERR_INVALID_FD;
    }
    else
    {
        /* 
        ** Create a file to write the command to (or write over the old one) 
        */
        cmdFd = OS_creat(OS_SHELL_CMD_INPUT_FILE_NAME,OS_READ_WRITE);
        if (cmdFd < OS_FS_SUCCESS)
        {
            ReturnCode = OS_FS_ERROR;
        }
        else
        {
            /*
            ** Write the command to the buffer
            */
            strncpy(localCmd,Cmd,OS_MAX_CMD_LEN);
            strncat(localCmd,"\n",1);

            /*
            ** This function passes in an open file descriptor to write the shell
            **  command output. The RTEMS shell script API expects a filename, not
            **  a file descriptor. So in addition to the temporary file for the shell input,
            **  we need to create a temporary file for the command output, then it has
            **  to be copied to the open file.  
            */
            strncpy(outputFileName,OS_SHELL_CMD_INPUT_FILE_NAME,OS_MAX_PATH_LEN);           
            strncat(outputFileName,OS_SHELL_TMP_FILE_EXT,OS_SHELL_TMP_FILE_EXT_LEN);

            /* 
            ** copy the shell command buffer to the file 
            */
            fileStatus = OS_write(cmdFd, localCmd, strlen(localCmd));
            if ( fileStatus == strlen(localCmd) )
            {
               /*
               ** Close the file
               */
               OS_close(cmdFd);	

               /*
               ** Spawn a task to execute the shell command
               */
               rtemsRc =  rtems_shell_script ( 
                         "RSHL", 
                         OS_SHELL_CMD_TASK_STACK_SIZE, 
                         OS_SHELL_CMD_TASK_PRIORITY, 
                         OS_SHELL_CMD_INPUT_FILE_NAME,
                         outputFileName,
                         FALSE,       /* Do not append output to file */
                         TRUE,        /* Wait for shell task to complete */
                         FALSE        /* Echo output */ 
                        );
                
               /*
               ** Now we have the temporary file with the output 
               */
               if((tmpFd = OS_open(outputFileName, OS_READ_ONLY,0)) == OS_FS_ERROR)
               {
                  printf("OSAL:Could not open %s for reading\n",outputFileName);
                  ReturnCode = OS_FS_ERROR; 
               }
               else
               { 
                  while((bytesRead = OS_read(tmpFd, readBuffer, 256)) > 0)
                  {
                     bytesWritten = OS_write(OS_fd, readBuffer, bytesRead);
                  }
                  OS_close(tmpFd);
                  /*
                  ** Remove the temporary output file
                  */
                  fileStatus = OS_remove(outputFileName); 
               } 
            }
            else
            {
                 ReturnCode = OS_FS_ERROR;
            }
        } 
        /*
        ** Remove the temporary shell input file
        */
        fileStatus = OS_remove(OS_SHELL_CMD_INPUT_FILE_NAME); 
    }
 
    return ReturnCode;

}/* end OS_ShellOutputToFile */
コード例 #8
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void CFE_ES_PerfLogDump(void){

    int32               WriteStat;
    uint32              i;
    uint32              FileSize;
    CFE_FS_Header_t     FileHdr;

    CFE_ES_RegisterChildTask();


    /* Zero cFE header, then fill in fields */
    CFE_PSP_MemSet(&FileHdr, 0, sizeof(CFE_FS_Header_t));
    strcpy(&FileHdr.Description[0], CFE_ES_PERF_LOG_DESC);
    FileHdr.SubType = CFE_FS_ES_PERFDATA_SUBTYPE;

    /* write the cFE header to the file */
    WriteStat = CFE_FS_WriteHeader( CFE_ES_PerfLogDumpStatus.DataFileDescriptor, &FileHdr);
    if(WriteStat != sizeof(CFE_FS_Header_t))
    {
        CFE_ES_FileWriteByteCntErr(&CFE_ES_PerfLogDumpStatus.DataFileName[0],
                                   sizeof(CFE_FS_Header_t),WriteStat);
        
        OS_close(CFE_ES_PerfLogDumpStatus.DataFileDescriptor);
        CFE_ES_ExitChildTask();
    }/* end if */
    FileSize = WriteStat;

    /* write the performance metadata to the file */
    WriteStat = OS_write(CFE_ES_PerfLogDumpStatus.DataFileDescriptor,(uint8 *)&Perf->MetaData,sizeof(CFE_ES_PerfMetaData_t));
    if(WriteStat != sizeof(CFE_ES_PerfMetaData_t))
    {
        CFE_ES_FileWriteByteCntErr(&CFE_ES_PerfLogDumpStatus.DataFileName[0],
                                   sizeof(CFE_ES_PerfMetaData_t),WriteStat);
        OS_close(CFE_ES_PerfLogDumpStatus.DataFileDescriptor);
        CFE_ES_ExitChildTask();
    }/* end if */
    FileSize += WriteStat;

    CFE_ES_PerfLogDumpStatus.DataToWrite = Perf->MetaData.DataCount;

    /* write the collected data to the file */
    for(i=0; i < Perf->MetaData.DataCount; i++){
      WriteStat = OS_write (CFE_ES_PerfLogDumpStatus.DataFileDescriptor, &Perf->DataBuffer[i], sizeof(CFE_ES_PerfDataEntry_t));
      if(WriteStat != sizeof(CFE_ES_PerfDataEntry_t))
      {
        CFE_ES_FileWriteByteCntErr(&CFE_ES_PerfLogDumpStatus.DataFileName[0],
                                   sizeof(CFE_ES_PerfDataEntry_t),WriteStat);
        OS_close(CFE_ES_PerfLogDumpStatus.DataFileDescriptor);
        /* Reset the DataToWrite variable, so a new file can be written */
        CFE_ES_PerfLogDumpStatus.DataToWrite = 0;
        CFE_ES_ExitChildTask();
      }/* end if */
      FileSize += WriteStat;
      CFE_ES_PerfLogDumpStatus.DataToWrite--;
      if((i % CFE_ES_PERF_ENTRIES_BTWN_DLYS) == 0){
        OS_TaskDelay(CFE_ES_PERF_CHILD_MS_DELAY);
      }/* end if */

    }/* end for */

    OS_close(CFE_ES_PerfLogDumpStatus.DataFileDescriptor);

    CFE_EVS_SendEvent(CFE_ES_PERF_DATAWRITTEN_EID,CFE_EVS_DEBUG,
                      "%s written:Size=%d,EntryCount=%d",
                       &CFE_ES_PerfLogDumpStatus.DataFileName[0],FileSize,
                       Perf->MetaData.DataCount);

    CFE_ES_ExitChildTask();

}/* end CFE_ES_PerfLogDump */
コード例 #9
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void CFE_ES_PerfStopDataCmd(CFE_SB_MsgPtr_t Msg){

    CFE_ES_PerfStopCmd_t  *CmdPtr = (CFE_ES_PerfStopCmd_t *)Msg;
    uint16 ExpectedLength = sizeof(CFE_ES_PerfStopCmd_t);
    int32 Stat;
    
    /*
    ** Verify command packet length.
    */
    if (CFE_ES_VerifyCmdLength(Msg, ExpectedLength))
    {

      /* Ensure there is no file write in progress before proceeding */
      if(CFE_ES_PerfLogDumpStatus.DataToWrite == 0)
      {
          Perf->MetaData.State = CFE_ES_PERF_IDLE;

          if(CmdPtr->DataFileName[0]=='\0')
          {
              strncpy(&CFE_ES_PerfLogDumpStatus.DataFileName[0],
                      CFE_ES_DEFAULT_PERF_DUMP_FILENAME,OS_MAX_PATH_LEN);
          }
          else
          {
              CmdPtr->DataFileName[OS_MAX_PATH_LEN - 1] = '\0';
              strncpy(&CFE_ES_PerfLogDumpStatus.DataFileName[0], &CmdPtr->DataFileName[0],OS_MAX_PATH_LEN);
          }/* end if */
          
          
          
          /* Create the file to dump to */
          CFE_ES_PerfLogDumpStatus.DataFileDescriptor = OS_creat(&CFE_ES_PerfLogDumpStatus.DataFileName[0], OS_WRITE_ONLY);
          
          
          if(CFE_ES_PerfLogDumpStatus.DataFileDescriptor < 0)
          {
              CFE_ES_TaskData.ErrCounter++;
              CFE_EVS_SendEvent(CFE_ES_PERF_LOG_ERR_EID,CFE_EVS_ERROR,
                                "Error creating file %s, RC = 0x%08X",
                                &CFE_ES_PerfLogDumpStatus.DataFileName[0], CFE_ES_PerfLogDumpStatus.DataFileDescriptor);
          }
          else
          {
          
              /* Spawn a task to write the performance data to a file */
              Stat = CFE_ES_CreateChildTask(&CFE_ES_PerfLogDumpStatus.ChildID,
                                            CFE_ES_PERF_CHILD_NAME,
                                            CFE_ES_PerfLogDump,
                                            CFE_ES_PERF_CHILD_STACK_PTR,
                                            CFE_ES_PERF_CHILD_STACK_SIZE,
                                            CFE_ES_PERF_CHILD_PRIORITY,
                                            CFE_ES_PERF_CHILD_FLAGS);

              if(Stat == CFE_SUCCESS)
              {
                  /* Note: the file gets closed in the child task */
                  CFE_ES_TaskData.CmdCounter++;
                  CFE_EVS_SendEvent(CFE_ES_PERF_STOPCMD_EID,CFE_EVS_DEBUG,
                                    "Perf Stop Cmd Rcvd,%s will write %d entries.%dmS dly every %d entries",
                                    CFE_ES_PERF_CHILD_NAME,Perf->MetaData.DataCount,
                                    CFE_ES_PERF_CHILD_MS_DELAY,CFE_ES_PERF_ENTRIES_BTWN_DLYS);
              }
              else
              {
                  /* close the fd */
                  OS_close( CFE_ES_PerfLogDumpStatus.DataFileDescriptor);
                  CFE_ES_TaskData.ErrCounter++;
                  CFE_EVS_SendEvent(CFE_ES_PERF_STOPCMD_ERR1_EID, CFE_EVS_ERROR,
                                    "Stop performance data cmd,Error creating child task RC=0x%08X",Stat);
              }/* end if */

          }/* end if fd < 0 */
          
      }/* if data to write == 0 */
      else
      {

          CFE_ES_TaskData.ErrCounter++;
          CFE_EVS_SendEvent(CFE_ES_PERF_STOPCMD_ERR2_EID, CFE_EVS_ERROR,
                   "Stop performance data cmd ignored,perf data write in progress");
      }/* end if */

    }/* end VerifyCmdLength */

} /* End of CFE_ES_PerfStopDataCmd() */
コード例 #10
0
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int32 CFE_ES_ShellOutputCommand(char * CmdString, char *Filename)
{
    int32 Result;
    int32 ReturnCode = CFE_SUCCESS;
    int32 fd;
    int32 FileSize;
    int32 CurrFilePtr;
    uint32 i;
    
    /* the extra 1 added for the \0 char */
    char CheckCmd [CFE_ES_CHECKSIZE + 1];
    char Cmd [CFE_ES_MAX_SHELL_CMD];
    char OutputFilename [OS_MAX_PATH_LEN];

    /* Use default filename if not provided */
    if (Filename[0] == '\0')
    {
        strncpy(OutputFilename, CFE_ES_DEFAULT_SHELL_FILENAME, OS_MAX_PATH_LEN);
    }
    else
    {
        strncpy(OutputFilename, Filename, OS_MAX_PATH_LEN);
    }

    /* Make sure string is null terminated */
    OutputFilename[OS_MAX_PATH_LEN - 1] = '\0';

    /* Remove previous version of output file */
    OS_remove(OutputFilename); 

    fd = OS_creat(OutputFilename, OS_READ_WRITE);

    if (fd < OS_FS_SUCCESS)
    {
        Result = OS_FS_ERROR;
    }

    else
    {
        strncpy(CheckCmd,CmdString,CFE_ES_CHECKSIZE);
    
        CheckCmd[CFE_ES_CHECKSIZE]  = '\0';
    
        strncpy(Cmd,CmdString, CFE_ES_MAX_SHELL_CMD);
    
        /* We need to check if this command is directed at ES, or at the 
        operating system */
    
        if (strncmp(CheckCmd,"ES_",CFE_ES_CHECKSIZE) == 0)
        {
            /* This list can be expanded to include other ES functionality */
            if ( strncmp(Cmd,CFE_ES_LIST_APPS_CMD,strlen(CFE_ES_LIST_APPS_CMD) )== 0)
            {
                Result = CFE_ES_ListApplications(fd);
            }
            else if ( strncmp(Cmd,CFE_ES_LIST_TASKS_CMD,strlen(CFE_ES_LIST_TASKS_CMD) )== 0)
            {
                Result = CFE_ES_ListTasks(fd);
            }
            else if ( strncmp(Cmd,CFE_ES_LIST_RESOURCES_CMD,strlen(CFE_ES_LIST_RESOURCES_CMD) )== 0)
            {
                Result = CFE_ES_ListResources(fd);
            }

            /* default if there is not an ES command that matches */
            else
            {
                Result = CFE_ES_ERR_SHELL_CMD;
                CFE_ES_WriteToSysLog("There is no ES Shell command that matches %s \n",Cmd);
            }            

        }
        /* if the command is not directed at ES, pass it through to the 
        * underlying OS */
        else
        {
            Result = OS_ShellOutputToFile(Cmd,fd);
        }

        /* seek to the end of the file to get it's size */
        FileSize = OS_lseek(fd,0,OS_SEEK_END);

        if (FileSize == OS_FS_ERROR)
        {
            OS_close(fd);
            CFE_ES_WriteToSysLog("OS_lseek call failed from CFE_ES_ShellOutputCmd 1\n");
            Result =  OS_FS_ERROR;
        }



        /* We want to add 3 characters at the end of the telemetry,'\n','$','\0'.
         * To do this we need to make sure there are at least 3 empty characters at
         * the end of the last CFE_ES_MAX_SHELL_PKT so we don't over write any data. If 
         * the current file has only 0,1, or 2 free spaces at the end, we want to 
         * make the file longer to start a new tlm packet of size CFE_ES_MAX_SHELL_PKT.
         * This way we will get a 'blank' packet with the correct 3 characters at the end.
         */

        else
        {
            /* if we are within 2 bytes of the end of the packet*/
            if ( FileSize % CFE_ES_MAX_SHELL_PKT > (CFE_ES_MAX_SHELL_PKT - 3))
            {
                /* add enough bytes to start a new packet */
                for (i = 0; i < CFE_ES_MAX_SHELL_PKT - (FileSize % CFE_ES_MAX_SHELL_PKT) + 1 ; i++)
                {
                    OS_write(fd," ",1);
                }
            }
            else
            {
                /* we are exactly at the end */
                if( FileSize % CFE_ES_MAX_SHELL_PKT == 0)
                {
                    OS_write(fd," ",1);
                }
            }

            /* seek to the end of the file again to get it's new size */
            FileSize = OS_lseek(fd,0,OS_SEEK_END);

            if (FileSize == OS_FS_ERROR)
            {
                OS_close(fd);
                CFE_ES_WriteToSysLog("OS_lseek call failed from CFE_ES_ShellOutputCmd 2\n");
                Result =  OS_FS_ERROR;
            }


            else
            {
                /* set the file back to the beginning */
                OS_lseek(fd,0,OS_SEEK_SET);


                /* start processing the chunks. We want to have one packet left so we are sure this for loop
                * won't run over */
        
                for (CurrFilePtr=0; CurrFilePtr < (FileSize - CFE_ES_MAX_SHELL_PKT); CurrFilePtr += CFE_ES_MAX_SHELL_PKT)
                {
                    OS_read(fd, CFE_ES_TaskData.ShellPacket.ShellOutput, CFE_ES_MAX_SHELL_PKT);

                    /* Send the packet */
                    CFE_SB_TimeStampMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket);
                    CFE_SB_SendMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket);
                    /* delay to not flood the pipe on large messages */
                    OS_TaskDelay(200);
                }

                /* finish off the last portion of the file */
                /* over write the last packet with spaces, then it will get filled
               * in with the correct info below. This assures that the last non full
               * part of the packet will be spaces */
                for (i =0; i < CFE_ES_MAX_SHELL_PKT; i++)
                {
                    CFE_ES_TaskData.ShellPacket.ShellOutput[i] = ' ';
                }
  
                OS_read(fd, CFE_ES_TaskData.ShellPacket.ShellOutput, ( FileSize - CurrFilePtr));

                /* From our check above, we are assured that there are at least 3 free
                 * characters to write our data into at the end of this last packet 
                 * 
                 * The \n assures we are on a new line, the $ gives us our prompt, and the 
                 * \0 assures we are null terminalted.
                 */

        
                CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 3] = '\n';
                CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 2] = '$';
                CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 1] = '\0';

                /* Send the last packet */
                CFE_SB_TimeStampMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket);
                CFE_SB_SendMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket);
   
                /* Close the file descriptor */
                OS_close(fd);
            } /* if FilseSize == OS_FS_ERROR */
        } /* if FileSeize == OS_FS_ERROR */
    }/* if fd < OS_FS_SUCCESS */


    if (Result != OS_SUCCESS && Result != CFE_SUCCESS )
    {
        ReturnCode = CFE_ES_ERR_SHELL_CMD;
        CFE_ES_WriteToSysLog("OS_ShellOutputToFile call failed from CFE_ES_ShellOutputCommand\n");
    }
    else
    {
        ReturnCode = CFE_SUCCESS;
    }
    
    return ReturnCode;
}  
コード例 #11
0
ファイル: tblmgr.c プロジェクト: Open-Sat/ObjBased-App
/******************************************************************************
**
** ParseXmlFile
**
** Parse an XML file
*/
static boolean ParseXmlFile(const char* FilePathName,
                            XML_StartElementHandler StartElementHandler,
                            XML_EndElementHandler   EndElementHandler)
{

   int      FileHandle;
   int32    ReadStatus;
   boolean  Done;
   boolean  ParseErr  = FALSE;
   boolean  RetStatus = FALSE;

   XML_Parser XmlParser = XML_ParserCreate(NULL);
   if (! XmlParser) {
      CFE_EVS_SendEvent(TBLMGR_CREATE_ERR_EID, CFE_EVS_ERROR, "Failed to allocate memory for XML parser");
   }
   else {

      XML_SetElementHandler(XmlParser, StartElementHandler, EndElementHandler);

      FileHandle = OS_open(FilePathName, OS_READ_ONLY, 0);
      
      if (FileHandle >= 0) {

         Done = FALSE;

         while (!Done) {

            ReadStatus = OS_read(FileHandle, TblFileBuff, TBLMGR_BUFFSIZE);

            if ( ReadStatus == OS_FS_ERROR )
            {
               CFE_EVS_SendEvent(TBLMGR_FILE_READ_ERR_EID, CFE_EVS_ERROR, "File read error, EC = 0x%08X",ReadStatus);
               Done = TRUE;
               ParseErr = TRUE;
            }
            else if ( ReadStatus == 0 ) /* EOF reached */
            {
                Done = TRUE;
            }
            else {

               /* ReadStatus contains number of bytes read */
               if (XML_Parse(XmlParser, TblFileBuff, ReadStatus, Done) == XML_STATUS_ERROR) {

                  CFE_EVS_SendEvent(TBLMGR_PARSE_ERR_EID, CFE_EVS_ERROR, "Parse error at line %l, error code = %s",
                                    XML_GetCurrentLineNumber(XmlParser),
                                    XML_ErrorString(XML_GetErrorCode(XmlParser)));
                  Done = TRUE;
                  ParseErr = TRUE;

               } /* End if valid parse */
            } /* End if valid fread */
         } /* End file read loop */

         RetStatus = !ParseErr;
         
         OS_close(FileHandle);

      } /* End if file opened */
      else
      {
      
          CFE_EVS_SendEvent(TBLMGR_FILE_OPEN_ERR_EID, CFE_EVS_ERROR, "File open error for %s, Error = %d",
                            FilePathName, FileHandle );
      }

      XML_ParserFree(XmlParser);

   } /* end if parser allocated */

   return RetStatus;

} /* End ParseXmlFile() */