/***********************************************************************
*                    SEGGER Microcontroller GmbH                       *
*                        The Embedded Experts                          *
************************************************************************
*                                                                      *
*                  (c) SEGGER Microcontroller GmbH                     *
*                        All rights reserved                           *
*                          www.segger.com                              *
*                                                                      *
************************************************************************
*                                                                      *
************************************************************************
*                                                                      *
*                                                                      *
*  Licensing terms                                                     *
*                                                                      *
* This software may be distributed to your customers free of charge.   *
* This grant of redistribution does not entitle YOU or enduser to      *
* receive from SEGGER hard-copy documentation, technical support,      *
* phone assistance, or enhancements or updates to the Software unless  *
* a specific agreement clearly states otherwise.                       *
*                                                                      *
*                                                                      *
* THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY        *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   *
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE        *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,     *
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,             *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  *
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    *
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     *
* DAMAGE.                                                              *
*                                                                      *
************************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : Template_FileRead_SYS_FILE.JLinkScript
Purpose : Writes and verifies file data using the SYS_FILE API.

Notes:
  (1) C:\Temp\TestFile.bin must already exist. Its first 8 bytes are overwritten.
  (2) The script is supposed to be used with the Device Provisioner (DevPro.exe).
      DevPro.exe -operation TestFunc -if SWD -speed 4000 -scriptfile C:\Temp\Template_FileRead_SYS_FILE.JLinkScript

*/

/*********************************************************************
*
*       Constants, fixed
*
**********************************************************************
*/

__constant U32 _NUM_DATA_BYTES = 8;

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

static U8 _aData[8] = {
  0x12, 0x34, 0x56, 0x78, 0x9A, 0xAB, 0xCD, 0xEF
};

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       TestFunc()
*
*  Function description
*    Writes data to an existing file and verifies the readback using SYS_FILE.
*
*  Return value
*     0: OK
*    <0: Error
*/
int TestFunc(void) {
  SYS_FILE_HANDLE hFile;
  int r;
  int Result;
  U32 i;
  U8 aReadBack[8];
  //
  // Open existing TestFile.bin for reading and writing
  //
  hFile = SYS_FILE_Open("C:\\Temp\\TestFile.bin", SYS_FILE_FLAG_READ | SYS_FILE_FLAG_WRITE);
  if (hFile < 0) {
    JLINK_SYS_Report("Error: Failed to open TestFile!");
    return -1;
  }
  Result = -1;
  //
  // Write data to TestFile.bin
  //
  r = SYS_FILE_Write(hFile, &_aData[0], _NUM_DATA_BYTES);
  if (r <= 0) {
    JLINK_SYS_Report("Error: Failed to write to TestFile!");
    goto CloseFile;
  }
  if (r != _NUM_DATA_BYTES) {
    JLINK_SYS_Report("Error: Number of bytes written is smaller than size of <_aData>!");
    goto CloseFile;
  }
  //
  // Rewind before reading back data from TestFile.bin
  //
  r = SYS_FILE_SetPos(hFile, 0, SYS_FILE_ORIGIN_BEGIN);
  if (r < 0) {
    JLINK_SYS_Report("Error: Failed to set file position!");
    goto CloseFile;
  }
  r = SYS_FILE_Read(hFile, &aReadBack[0], _NUM_DATA_BYTES);
  if (r < 0) {
    JLINK_SYS_Report("Error: Failed to read from TestFile!");
    goto CloseFile;
  }
  JLINK_SYS_Report1("NumBytes read back from TestFile: ", r);
  if (r != _NUM_DATA_BYTES) {
    JLINK_SYS_Report("Error: TestFile smaller than expected!");
    goto CloseFile;
  }
  JLINK_SYS_Report("TestFile data:");
  i = 0;
  while (i < _NUM_DATA_BYTES) {
    JLINK_SYS_Report1("", aReadBack[i]);
    if (aReadBack[i] != _aData[i]) {
      JLINK_SYS_Report("Error: Data does not match!");
      goto CloseFile;
    }
    i += 1;
  }
  Result = 0;
  //
  // Close the file on success and on every error after opening
  //
CloseFile:
  r = SYS_FILE_Close(hFile);
  if (r < 0) {
    JLINK_SYS_Report("Error: Failed to close TestFile!");
    return -1;
  }
  return Result;
}

/*************************** End of file ****************************/
