File Transfer Services: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(26 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== Introduction ==
__NOTOC__
Stride provides an API for performing file operations on the host from test code executing on the device. In order for the file APIs to be operational, your test code must be executed in the context of the [[STRIDE Runner | Stride Runner]]. The host filesystem operations provided by this APIs encompass the following functionality:
* open/close
* read/write
* seek/tell
* tempfile name creation
* move/deletion
* error messaging related to any failures in these operations


== Examples ==
'''Example'''


== Reference ==
The following code sample shows how to read the entire contents of a file on the host. The code currently just discards the buffer contents with each read operation, but actual code would likely store or process the file contents.


=== srFileOpen ===
<source lang="c">
srCHAR file[] = "/path/to/host/file";
srBYTE readBuffer[1024] = {0};
srFileHandle_t handle = 0;
srDWORD result = 0;
srDWORD totalBytesRead = 0;


srFileOpen() opens a file on the host computer.
/* open host file */
if (0 != (result = srFileOpen(file , "r", &handle)))
  goto QUIT;


while (srFALSE == srFileEOF(handle))
{
  srDWORD actualBytesRead = 0;
  if (0 != (result = srFileRead(handle, readBuffer, sizeof(readBuffer), &actualBytesRead)))
    goto QUIT;
  if (actualBytesRead > 0)
  {
    totalBytesRead += actualBytesRead;
    /* TODO - do something with the contents of the readBuffer */
  }
  else
    break;
}
srNOTE_INFO("read %lu bytes from %s", totalBytesRead , file);
QUIT:
if (result != 0)
{
  srCHAR errorBuffer[srFILE_MAX_ERROR_LENGTH] = {0};
  srFileGetErrorMessage(result, errorBuffer, srFILE_MAX_ERROR_LENGTH);
  srNOTE_ERROR(errorBuffer);
}
 
if (handle !=0)
  srFileClose(handle);
</source>
== srFileOpen ==
srFileOpen() opens a file on the host computer.
<source lang="c">
<source lang="c">
srDWORD srTestPointExpect(const srCHAR * szHostPath,  
srDWORD srFileOpen(const srCHAR   * szHostPath,  
                          const srCHAR * szMode,
                  const srCHAR   * szMode,
                          srFileHandle_t * phFile);
                  srFileHandle_t * phFile);
</source>
</source>


Line 22: Line 69:
| szHostPath
| szHostPath
| Input
| Input
| Full path to the remote file (located on the host computer)
| Full path to the remote file (located on the host computer) <ref name="env">A full path to the remote file (located on the host computer) could reference host environment variables (e.g. ''$VAR/path/file'') using syntax of your choice - %VAR%, $VAR, $(VAR), or ${VAR}</ref>
|-
|-
| szMode
| szMode
| Input
| Input
| Mode string for opening the file - can be one of the following:
| Mode string for opening the file - can be one of the following:
* r
* <b>r</b> - open for reading
* w
* <b>w</b> - open for writing (creates file if it doesn't exist). Deletes content and overwrites the file.
* a
* <b>a</b> - open for appending (creates file if it doesn't exist)
A '+' sign can be added to any of these mode characters to request simultaneous read/write access. Files are always opened in binary mode, so no text character translation is done.
A '+' sign can be added to any of these mode characters to request simultaneous read/write access. Files are always opened in binary mode, so no text character translation is done.
|-
|-
Line 46: Line 93:




=== srFileClose ===
== srFileClose ==


srFileClose () closes a remote file associated with the specified handle.
srFileClose () closes a remote file associated with the specified handle.
Line 73: Line 120:




=== srFileSeek===
== srFileSeek ==


srFileSeek() seeks to the specified position within an open remote file stream by moving ''lOffset'' bytes relative to a position specified by ''eOrigin''.
srFileSeek() seeks to the specified position within an open remote file stream by moving ''lOffset'' bytes relative to a position specified by ''eOrigin''.
Line 113: Line 160:




=== srFileTell===
== srFileTell ==


srFileTell() returns the current position of <i>hFile</i> relative to the beginning of the file.
srFileTell() returns the current position of <i>hFile</i> relative to the beginning of the file.
Line 145: Line 192:




=== srFileRead===
== srFileRead ==


srFileRead() reads ''dwBytesToRead'' bytes from an open remote file stream and stores the data in the memory pointed to by ''pvData''. The current position of the stream is advanced by the number of bytes read.
srFileRead() reads ''dwBytesToRead'' bytes from an open remote file stream and stores the data in the memory pointed to by ''pvData''. The current position of the stream is advanced by the number of bytes read.
Line 188: Line 235:




=== srFileWrite===
== srFileWrite ==


srFileWrite() writes ''dwBytesToWrite'' bytes from the memory location specified in ''pvData'' to an open remote file stream. The current position of the stream is advanced by the number of bytes written.
srFileWrite() writes ''dwBytesToWrite'' bytes from the memory location specified in ''pvData'' to an open remote file stream. The current position of the stream is advanced by the number of bytes written.
Line 231: Line 278:




=== srFileFlush===
== srFileGets ==


srFileFlush() flushes the content of the remote file.
srFileGets () gets a string from an open remote file stream and stores the data in the memory pointed to by ''szBuff''. Reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to ''dwSize – 1'', whichever comes first.


<source lang="c">
<source lang="c">
srDWORD srFileFlush(srFileHandle_t hFile);
srDWORD srFileGets(srFileHandle_t hFile,
                  srCHAR * szBuff,
                  srDWORD dwSize);
</source>
</source>


Line 244: Line 293:
| '''Description'''
| '''Description'''
|-
|-
| hFile   
| hFile
| Input
| Handle specifying an open file stream
|-
| szBuff
| Output
| Pointer to a block of memory with a maximum size of ''dwSize'' chars
|-
| dwSize
| Input
| Size in chars of the buffer pointed to by ''szBuff''
|}
<br>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"
| '''Return Value'''
| ''' Description'''
|-
| srDWORD
| 0 on success, error code otherwise.
|}
 
 
== srFilePuts ==
 
srFilePuts() puts a null-terminated string specified in ''szBuff'' to an open remote file stream. The current position of the stream is advanced by the number of bytes written.
 
<source lang="c">
srDWORD srFilePuts(srFileHandle_t hFile,
                  const srCHAR * szBuff);
</source>
 
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"  
| '''Parameters'''
| '''Type'''
| '''Description'''
|-
| hFile
| Input
| Input
| Handle specifying an open file stream
| Handle specifying an open file stream
|-
| szBuff
| Output
| Pointer to a null-terminated string
|}
|}
<br>
<br>
Line 258: Line 347:




=== srFileEOF===
== srFileEOF ==


srFileEOF() checks whether the remote file's end of file flag is set.
srFileEOF() checks whether the remote file's end of file flag is set.
Line 280: Line 369:
| ''' Description'''
| ''' Description'''
|-
|-
| srDWORD
| srBOOL
| srTRUE if the EOF flag is set for the remote file, srFALSE otherwise.
| srTRUE if the EOF flag is set for the remote file, srFALSE otherwise.
|}
|}




=== srFileError===
== srFileError ==


srFileError() checks whether the remote file error flag has been set by a preceeding operation.
srFileError() checks whether the remote file error flag has been set by a preceeding operation.
Line 307: Line 396:
| ''' Description'''
| ''' Description'''
|-
|-
| srDWORD
| srBOOL
| srTRUE if the error flag is set for the remote file, srFALSE otherwise.
| srTRUE if the error flag is set for the remote file, srFALSE otherwise.
|}
|}




=== srFileClearErr===
== srFileClearErr ==


srFileClearErr() clears the error flag for the remote file
srFileClearErr() clears the error flag for the remote file
Line 329: Line 418:
| Handle specifying an open file stream
| Handle specifying an open file stream
|}
|}
no return value.




=== srFileGetErrorMessage===
== srFileGetErrorMessage ==


srFileGetErrorMessage() gets the last error message for the remote file, if any.
srFileGetErrorMessage() gets the last error message for the remote file, if any.
Line 368: Line 459:




== srFileGetTempName ==
srFileGetTempName() gets an unique name for a temporary file on the host.
<source lang="c">
srDWORD srFileGetTempName(srCHAR * szHostPath,
                          srDWORD dwSize);
</source>


=== TBD ===
{| border="1" cellspacing="0" cellpadding="10" style="align:left;" 
| '''Parameters'''
| '''Type'''
| '''Description'''
|-
| szHostPath
| Output
| Pointer to a character buffer to populate with the file path name.).<ref name="env"/>
|-
| dwSize
| Input
| Size in bytes of the buffer pointed to by szHostPath.
|}
<br>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"
| '''Return Value'''
| ''' Description'''
|-
| srDWORD
| 0 on success, error code otherwise.
|}


== srFileRemove ==
srFileRemove() removes a file on the host.
<source lang="c">
srDWORD srFileRemove(const srCHAR * szHostPath);
</source>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;" 
| '''Parameters'''
| '''Type'''
| '''Description'''
|-
| szHostPath
| Input
| Full path to the remote file (located on the host computer).<ref name="env"/>
|}
<br>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"
| '''Return Value'''
| ''' Description'''
|-
| srDWORD
| 0 on success, error code otherwise.
|}
== srFileRename==
srFileRename() moves/renames a file on the host.


<source lang="c">
<source lang="c">
srDWORD srFileRename(const srCHAR * szHostPathOld,
                    const srCHAR * szHostPathNew);
</source>


{| border="1" cellspacing="0" cellpadding="10" style="align:left;" 
| '''Parameters'''
| '''Type'''
| '''Description'''
|-
| szHostPathOld
| Input
| Full path to the old remote file (located on the host computer).<ref name="env"/>
|-
| szHostPathNew
| Input
| Full path to the new remote file (located on the host computer).<ref name="env"/>


/**
|}
* Gets an unique name for a temporary file.
<br>
*
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"
* @param szHostPath    Pointer to a character buffer to populate with the file path name.
| '''Return Value'''
* @param dwSize        Size in bytes of the buffer pointed to by szHostPath
| ''' Description'''
*
|-
* @return Zero on success, error code otherwise.
| srDWORD
*       
| 0 on success, error code otherwise.
*/
|}
srEXPORT srDWORD srFileGetTempName(srCHAR * szHostPath, srDWORD dwSize);


/**
* Remove a file.
*
* @param szHostPath  Full path to the remote file (located on the host computer).
*
* @return Zero on success, error code otherwise.
*       
*/
srEXPORT srDWORD srFileRemove(const srCHAR * szHostPath);


== srPrompt==


/**
srPrompt() provides interactive console prompt on the host.
* Rename/Move a file.
*
* @param szHostPathOld  Full path to the old remote file (located on the host computer).
* @param szHostPathNew  Full path to the new remote file (located on the host computer).
*
* @return Zero on success, error code otherwise.
*       
*/
srEXPORT srDWORD srFileRename(const srCHAR * szHostPathOld, const srCHAR * szHostPathNew);


<source lang="c">
srDWORD srPrompt(const srCHAR * szQuestion,
                srCHAR * szAnswer,
                srDWORD dwSize,
                srWORD wTimeout);
</source>
</source>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;" 
| '''Parameters'''
| '''Type'''
| '''Description'''
|-
| szQuestion
| Input
| Question to the user.
|-
| szAnswer
| Input/Output
| Default answer as input and actual answer as output
|-
| dwSize
| Input
|  Size in chars of the buffer pointed to by szAnswer
|-
| wTimeout
| Input
|  Timeout in seconds, where 0 means no timeout. In case of timeout the default answer will be used.
|}
<br>
{| border="1" cellspacing="0" cellpadding="10" style="align:left;"
| '''Return Value'''
| ''' Description'''
|-
| srDWORD
| 0 on success, error code otherwise.
|}
== Notes ==
<references/>

Latest revision as of 15:41, 6 July 2015

Stride provides an API for performing file operations on the host from test code executing on the device. In order for the file APIs to be operational, your test code must be executed in the context of the Stride Runner. The host filesystem operations provided by this APIs encompass the following functionality:

  • open/close
  • read/write
  • seek/tell
  • tempfile name creation
  • move/deletion
  • error messaging related to any failures in these operations

Example

The following code sample shows how to read the entire contents of a file on the host. The code currently just discards the buffer contents with each read operation, but actual code would likely store or process the file contents.

srCHAR file[] = "/path/to/host/file";
srBYTE readBuffer[1024] = {0};
srFileHandle_t handle = 0;
srDWORD result = 0;
srDWORD totalBytesRead = 0;

/* open host file */
if (0 != (result = srFileOpen(file , "r", &handle)))
  goto QUIT;

while (srFALSE == srFileEOF(handle))
{
  srDWORD actualBytesRead = 0;
  if (0 != (result = srFileRead(handle, readBuffer, sizeof(readBuffer), &actualBytesRead)))
    goto QUIT;

  if (actualBytesRead > 0)
  {
    totalBytesRead += actualBytesRead;
    /* TODO - do something with the contents of the readBuffer */
  }
  else 
    break;
}

srNOTE_INFO("read %lu bytes from %s", totalBytesRead , file);

QUIT:
if (result != 0)
{
  srCHAR errorBuffer[srFILE_MAX_ERROR_LENGTH] = {0};
  srFileGetErrorMessage(result, errorBuffer, srFILE_MAX_ERROR_LENGTH);
  srNOTE_ERROR(errorBuffer);
}
  
if (handle !=0)
  srFileClose(handle);


srFileOpen

srFileOpen() opens a file on the host computer.

srDWORD srFileOpen(const srCHAR   * szHostPath, 
                   const srCHAR   * szMode,
                   srFileHandle_t * phFile);
Parameters Type Description
szHostPath Input Full path to the remote file (located on the host computer) [1]
szMode Input Mode string for opening the file - can be one of the following:
  • r - open for reading
  • w - open for writing (creates file if it doesn't exist). Deletes content and overwrites the file.
  • a - open for appending (creates file if it doesn't exist)

A '+' sign can be added to any of these mode characters to request simultaneous read/write access. Files are always opened in binary mode, so no text character translation is done.

phFile Output Pointer to store a handle to be used to specify this file in subsequent operations


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileClose

srFileClose () closes a remote file associated with the specified handle.

srDWORD srFileClose(srFileHandle_t hFile);
Parameters Type Description
hFile Input Handle specifying an open file stream


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileSeek

srFileSeek() seeks to the specified position within an open remote file stream by moving lOffset bytes relative to a position specified by eOrigin.

srDWORD srFileSeek(srFileHandle_t hFile, 
                   srLONG lOffset, 
                   srFileSeekOrigin_e eOrigin);
Parameters Type Description
hFile Input Handle specifying an open file stream
lOffset Input Number of bytes to offset from eOrigin
eOrigin Input Position to which lOffset is added. It is specified by one of the following enumerations:
  • srSEEK_BEGIN
  • srSEEK_CUR
  • srSEEL_END


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileTell

srFileTell() returns the current position of hFile relative to the beginning of the file.

srDWORD srFileTell(srFileHandle_t hFile, 
                   srLONG* plOffset);
Parameters Type Description
hFile Input Handle specifying an open file stream
plOffset Input Pointer to store the current position in the file


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileRead

srFileRead() reads dwBytesToRead bytes from an open remote file stream and stores the data in the memory pointed to by pvData. The current position of the stream is advanced by the number of bytes read.

srDWORD srFileRead(srFileHandle_t hFile, 
                   void * pvData, 
                   srDWORD dwBytesToRead, 
                   srDWORD * pdwBytesRead);
Parameters Type Description
hFile Input Handle specifying an open file stream
pvData Output Pointer to a block of memory with a minimum size of dwBytesToRead bytes
dwBytesToRead Input Number of bytes to be read
pdwBytesRead Output Pointer to store the number of bytes successfully read. If this number differs from dwBytesToRead

either an error has occurred or the end-of-file was reached.


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileWrite

srFileWrite() writes dwBytesToWrite bytes from the memory location specified in pvData to an open remote file stream. The current position of the stream is advanced by the number of bytes written.

srDWORD srFileWrite(srFileHandle_t hFile, 
                    const void * pvData,
                    srDWORD dwBytesToWrite, 
                    srDWORD * pdwBytesWritten);
Parameters Type Description
hFile Input Handle specifying an open file stream
pvData Input Pointer to a block of memory to read from - must contain dwBytesToWrite bytes of data.
dwBytesToWrite Input Number of bytes to be written
pdwBytesWritten Output Pointer to store the number of bytes successfully written. If this number differs from dwBytesToWrite

an error has occurred.


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileGets

srFileGets () gets a string from an open remote file stream and stores the data in the memory pointed to by szBuff. Reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to dwSize – 1, whichever comes first.

srDWORD srFileGets(srFileHandle_t hFile, 
                   srCHAR * szBuff, 
                   srDWORD dwSize);
Parameters Type Description
hFile Input Handle specifying an open file stream
szBuff Output Pointer to a block of memory with a maximum size of dwSize chars
dwSize Input Size in chars of the buffer pointed to by szBuff


Return Value Description
srDWORD 0 on success, error code otherwise.


srFilePuts

srFilePuts() puts a null-terminated string specified in szBuff to an open remote file stream. The current position of the stream is advanced by the number of bytes written.

srDWORD srFilePuts(srFileHandle_t hFile, 
                   const srCHAR * szBuff);
Parameters Type Description
hFile Input Handle specifying an open file stream
szBuff Output Pointer to a null-terminated string


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileEOF

srFileEOF() checks whether the remote file's end of file flag is set.

srBOOL srFileEOF(srFileHandle_t hFile);
Parameters Type Description
hFile Input Handle specifying an open file stream


Return Value Description
srBOOL srTRUE if the EOF flag is set for the remote file, srFALSE otherwise.


srFileError

srFileError() checks whether the remote file error flag has been set by a preceeding operation.

srBOOL srFileError(srFileHandle_t hFile);
Parameters Type Description
hFile Input Handle specifying an open file stream


Return Value Description
srBOOL srTRUE if the error flag is set for the remote file, srFALSE otherwise.


srFileClearErr

srFileClearErr() clears the error flag for the remote file

void srFileClearErr(srFileHandle_t hFile);
Parameters Type Description
hFile Input Handle specifying an open file stream

no return value.


srFileGetErrorMessage

srFileGetErrorMessage() gets the last error message for the remote file, if any.

srDWORD srFileGetErrorMessage(srDWORD dwError, 
                              srCHAR * szBuff, 
                              srDWORD dwSize);
Parameters Type Description
dwError Input Error code
szBuff Output Pointer to a character buffer to populate with the error message.
dwSize Input Size in bytes of the buffer pointed to by szBuff.


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileGetTempName

srFileGetTempName() gets an unique name for a temporary file on the host.

srDWORD srFileGetTempName(srCHAR * szHostPath, 
                          srDWORD dwSize);
Parameters Type Description
szHostPath Output Pointer to a character buffer to populate with the file path name.).[1]
dwSize Input Size in bytes of the buffer pointed to by szHostPath.


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileRemove

srFileRemove() removes a file on the host.

srDWORD srFileRemove(const srCHAR * szHostPath);
Parameters Type Description
szHostPath Input Full path to the remote file (located on the host computer).[1]


Return Value Description
srDWORD 0 on success, error code otherwise.


srFileRename

srFileRename() moves/renames a file on the host.

srDWORD srFileRename(const srCHAR * szHostPathOld, 
                     const srCHAR * szHostPathNew);
Parameters Type Description
szHostPathOld Input Full path to the old remote file (located on the host computer).[1]
szHostPathNew Input Full path to the new remote file (located on the host computer).[1]


Return Value Description
srDWORD 0 on success, error code otherwise.


srPrompt

srPrompt() provides interactive console prompt on the host.

srDWORD srPrompt(const srCHAR * szQuestion, 
                 srCHAR * szAnswer, 
                 srDWORD dwSize, 
                 srWORD wTimeout);
Parameters Type Description
szQuestion Input Question to the user.
szAnswer Input/Output Default answer as input and actual answer as output
dwSize Input Size in chars of the buffer pointed to by szAnswer
wTimeout Input Timeout in seconds, where 0 means no timeout. In case of timeout the default answer will be used.


Return Value Description
srDWORD 0 on success, error code otherwise.


Notes

  1. 1.0 1.1 1.2 1.3 1.4 A full path to the remote file (located on the host computer) could reference host environment variables (e.g. $VAR/path/file) using syntax of your choice - %VAR%, $VAR, $(VAR), or ${VAR}