File Transfer Services: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(16 intermediate revisions by 3 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:
The STRIDE Framework 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
* open/close
* read/write
* read/write
Line 9: Line 8:
* error messaging related to any failures in these operations
* error messaging related to any failures in these operations


== Examples ==
'''Example'''
 
=== read entire contents of a host file ===


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.
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.
Line 17: Line 14:
<source lang="c">
<source lang="c">
srCHAR file[] = "/path/to/host/file";
srCHAR file[] = "/path/to/host/file";
srBYTE readBuffer[srFILE_DATA_CHUNK_SIZE] = {0};
srBYTE readBuffer[1024] = {0};
srFileHandle_t handle = 0;
srFileHandle_t handle = 0;
srDWORD result = 0;
srDWORD result = 0;
Line 29: Line 26:
{
{
   srDWORD actualBytesRead = 0;
   srDWORD actualBytesRead = 0;
   if (0 != (result = srFileRead(handle, readBuffer, srFILE_DATA_CHUNK_SIZE, &actualBytesRead)))
   if (0 != (result = srFileRead(handle, readBuffer, sizeof(readBuffer), &actualBytesRead)))
     goto QUIT;
     goto QUIT;


Line 41: Line 38:
}
}


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


QUIT:
QUIT:
Line 48: Line 45:
   srCHAR errorBuffer[srFILE_MAX_ERROR_LENGTH] = {0};
   srCHAR errorBuffer[srFILE_MAX_ERROR_LENGTH] = {0};
   srFileGetErrorMessage(result, errorBuffer, srFILE_MAX_ERROR_LENGTH);
   srFileGetErrorMessage(result, errorBuffer, srFILE_MAX_ERROR_LENGTH);
   srLOG_ERROR(errorBuffer);
   srNOTE_ERROR(errorBuffer);
}
}
    
    
Line 55: Line 52:
</source>
</source>


== Reference ==


The following sections document the srFile API.
== srFileOpen ==
 
=== srFileOpen ===
 
srFileOpen() opens a file on the host computer.


srFileOpen() opens a file on the host computer.
<source lang="c">
<source lang="c">
srDWORD srFileOpen(const srCHAR  * szHostPath,  
srDWORD srFileOpen(const srCHAR  * szHostPath,  
Line 76: 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 100: 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 127: 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 167: 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 199: 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 242: 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 285: Line 278:




=== srFileGets ===
== 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.
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.
Line 322: Line 315:




=== srFilePuts ===
== 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.
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.
Line 354: 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 381: Line 374:




=== 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 408: Line 401:




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


srFileClearErr() clears the error flag for the remote file
srFileClearErr() clears the error flag for the remote file
Line 429: Line 422:




=== 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 466: Line 459:




=== srFileGetTempName ===
== srFileGetTempName ==


srFileGetTempName() gets an unique name for a temporary file on the host.
srFileGetTempName() gets an unique name for a temporary file on the host.
Line 482: Line 475:
| szHostPath
| szHostPath
| Output
| Output
| Pointer to a character buffer to populate with the file path name.
| Pointer to a character buffer to populate with the file path name.).<ref name="env"/>
|-
|-
| dwSize
| dwSize
Line 498: Line 491:




=== srFileRemove ===
== srFileRemove ==


srFileRemove() removes a file on the host.
srFileRemove() removes a file on the host.
Line 513: Line 506:
| 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"/>
|}
|}
<br>
<br>
Line 525: Line 518:




=== srFileRename===
== srFileRename==


srFileRename() moves/renames a file on the host.
srFileRename() moves/renames a file on the host.
Line 541: Line 534:
| szHostPathOld
| szHostPathOld
| Input
| Input
| Full path to the old remote file (located on the host computer).
| Full path to the old remote file (located on the host computer).<ref name="env"/>
|-
|-
| szHostPathNew
| szHostPathNew
| Input
| Input
| Full path to the new remote file (located on the host computer).
| Full path to the new remote file (located on the host computer).<ref name="env"/>


|}
|}
Line 557: Line 550:
|}
|}


[[Category: Reference]]
 
== srPrompt==
 
srPrompt() provides interactive console prompt on the host.
 
<source lang="c">
srDWORD srPrompt(const srCHAR * szQuestion,
                srCHAR * szAnswer,
                srDWORD dwSize,
                srWORD wTimeout);
</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}