Read a File
118
SWRU455A – February 2017 – Revised March 2017
Copyright © 2017, Texas Instruments Incorporated
File System
SignatureLen =
sizeof
(Signature);
Status = sl_FsClose(FileHdl, CeritificateFileName, Signature, SignatureLen);
if
( Status < 0 )
{
/* error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}
8.5
Read a File
To read a file, it should first be opened for read. The following functions are involved in the read file
procedure:
•
Open-read a file, function sl_FsOpen()
•
Read the file, function sl_FsRead()
•
Close the file, function sl_FsClose()
8.5.1 Open a File for Read
Open a file for read only succeeds if the file has been closed or aborted. The open-for-read function does
not involve any SFLASH updates, and it has no effect on the SFLASH endurance.
The open-for-read function returns a negative value in case of an error.
The following is a partial list of errors that might be returned by the open-for-read function:
•
SL_ERROR_FS_FILE_IS_ALREADY_OPENED: The file is already opened for read or write.
•
SL_ERROR_FS_INVALID_TOKEN_SECURITY_ALERT: For a secure file, the input token is not valid;
this triggers security alerts if the device is secured.
•
SL_ERROR_FS_FILE_NOT_EXISTS: The file does not exist.
•
SL_ERROR_FS_DEVICE_NOT_SECURED: Reading the secure file can only be done in a secure
device type.
•
SL_ERROR_FS_WRONG_SIGNATURE_SECURITY_ALERT: For secure-signed files, each time the
file is opened for read, the file integrity is tested. If the test is failed, an error and a security alert is
raised.
Example:
_i32
FileHdl;
_u8
DeviceFileName[180];
_u32
MasterToken;
FileHdl =
sl_FsOpen(
unsigned char
*)DeviceFileName, SL_FS_READ, &MasterToken);
if
( FileHdl < 0 )
{
/*error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}
8.5.2 Read an Opened File
To read a file, the host requires the file handle, the offset to read, and the output buffer. A file can be read
from random offsets.
The sl_FsRead returns the actual bytes read, or a negative value which represents an error.
The following is a partial list of errors that might be returned by the open-for-read function:
•
SL_ERROR_FS_OFFSET_OUT_OF_RANGE: The file system set the actual file size as a higher offset
than was written. Trying to read a file from an offset which is higher than the actual file size results in
an error.
•
SL_ERROR_FS_NO_MEMORY: The read operation requires a system resource (RX-socket). The
system may return that if there are not available resources (sockets) for the operation; in this case, the
host can repeat the read operation after a while (or reduce the traffic overload while reading a file).