File Transfer Services: Difference between revisions
No edit summary |
|||
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
__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 | * 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 | ||
'''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. | 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[ | 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, | if (0 != (result = srFileRead(handle, readBuffer, sizeof(readBuffer), &actualBytesRead))) | ||
goto QUIT; | goto QUIT; | ||
Line 41: | Line 38: | ||
} | } | ||
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); | ||
srNOTE_ERROR(errorBuffer); | |||
} | } | ||
Line 55: | Line 52: | ||
</source> | </source> | ||
== srFileOpen == | |||
srFileOpen() opens a file on the host computer. | srFileOpen() opens a file on the host computer. | ||
Line 98: | Line 92: | ||
|} | |} | ||
== srFileClose == | |||
srFileClose () closes a remote file associated with the specified handle. | srFileClose () closes a remote file associated with the specified handle. | ||
Line 125: | Line 120: | ||
== 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 165: | Line 160: | ||
== 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 197: | Line 192: | ||
== 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 240: | Line 235: | ||
== 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 283: | Line 278: | ||
== 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 320: | Line 315: | ||
== 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 352: | Line 347: | ||
== 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 379: | Line 374: | ||
== 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 406: | Line 401: | ||
== srFileClearErr == | |||
srFileClearErr() clears the error flag for the remote file | srFileClearErr() clears the error flag for the remote file | ||
Line 427: | Line 422: | ||
== 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 464: | Line 459: | ||
== 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 480: | 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 495: | Line 490: | ||
|} | |} | ||
== srFileRemove == | |||
srFileRemove() removes a file on the host. | srFileRemove() removes a file on the host. | ||
Line 521: | Line 517: | ||
|} | |} | ||
== srFileRename== | |||
srFileRename() moves/renames a file on the host. | srFileRename() moves/renames a file on the host. | ||
Line 552: | Line 549: | ||
| 0 on success, error code otherwise. | | 0 on success, error code otherwise. | ||
|} | |} | ||
== 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 == | == Notes == | ||
<references/> | <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:
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:
|
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. |