/***********************************************************************
*                    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_FileGetSize.JLinkScript
Purpose : This script shows how to use the JLINK_FILE_GetSize() and JLINK_FILE_Read() functions

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_FileGetSize.JLinkScript -SetConfigVal "DataFile=C:\Temp\TestFile.bin"
*/

/*********************************************************************
*
*       Constants (similar to defines)
*
**********************************************************************
*/

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static U8 _acFile[1024];
static U8 _acBuffer[512];

/*********************************************************************
*
*       Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       TestFunc()
*
*  Function description
*    Test JLINK_FILE_GetSize() JLINK_FILE_Read() functionality
*
*  Return value
*    >= 0  OK
*     < 0  Error
*/
int TestFunc(void) {
  int r;
  U32 MaxNumBytesAtOnce;
  U32 NumBytesAtOnce;
  U32 NumBytesRem;
  U32 FileOffset;
  //
  // Get the filename, stored in the ConfigVal "DataFile" via command line:
  // -SetConfigVal "DataFile=C\Temp\Test.bin"
  //
  r = JLINK_GetConfigData("DataFile", &_acFile[0], 1024);  // Get filename
  if (r < 0) {
    JLINK_SYS_Report("Error! DataFile not specified");
    return -1;
  }
  //
  // Get the size of the specified file
  //
  NumBytesRem = JLINK_FILE_GetSize(&_acFile[0]);
  if (NumBytesRem < 0) {
    JLINK_SYS_Report("Error! Could not get the size of the specified file.");
    return -1;
  }
  JLINK_SYS_Report1("FileSize: ", NumBytesRem);
  //
  // Read data from the file chunk-wise (512 bytes at once)
  //
  MaxNumBytesAtOnce = 512;
  FileOffset        = 0;
  do {
    if (NumBytesRem >= MaxNumBytesAtOnce) {
      NumBytesAtOnce = MaxNumBytesAtOnce;
    } else {
      NumBytesAtOnce = NumBytesRem;
    }
    r = JLINK_FILE_Read(&_acFile[0], &_acBuffer[0], FileOffset, NumBytesAtOnce, 0);
    if (r < 0) {
      JLINK_SYS_Report("Failed to read data from file.");
      return -1;
    } else {
      JLINK_SYS_Report1("NumBytesRead: ", r);
    }
    NumBytesRem -= NumBytesAtOnce;
    FileOffset  += NumBytesAtOnce;
  } while (NumBytesRem);
  return 0;
}
