/*********************************************************************
*            (c) 1995 - 2021 SEGGER Microcontroller GmbH             *
*                        The Embedded Experts                        *
*                           www.segger.com                           *
**********************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : Template_FileRead.JLinkScript
Purpose : This script shows how to use the JLINK_FILE_Read() function to read data from a file.

Notes:
  (1) 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.JLinkScript

*/

/*********************************************************************
*
*       Constants (similar to defines)
*
**********************************************************************
*/

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/


/*********************************************************************
*
*       Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       TestFunc()
*
*  Function description
*    Test JLINK_FILE_READ() functionality
*
*  Return value
*    >= 0  OK
*     < 0  Error
*/
int TestFunc(void) {
  int r;
  U32 i;
  U32 NumBytesKeyFile;
  U8 abKeys[32];
  //
  // Output test values for <Keyfile>
  //
  NumBytesKeyFile = 32;
  r = JLINK_FILE_Read("C:\\Temp\\Keyfile.bin", &abKeys[0], 0, NumBytesKeyFile, 0);
  if (r < 0) {
    JLINK_SYS_Report("Failed to read from keyfile.");
    return -1;
  }
  JLINK_SYS_Report1("NumBytes read from keyfile: ", r);
  if (r < NumBytesKeyFile) {
    JLINK_SYS_Report1("Keyfile smaller than expected. <NumBytes> reduced to: ", r);
    NumBytesKeyFile = r;
  }
  JLINK_SYS_Report("Keyfile data:");
  i = 0;
  do {
    JLINK_SYS_Report1("", abKeys[i]);
    i += 1;
  } while (i < NumBytesKeyFile);
  return 0;
}
