/*!
 * @file        menu.c
 *
 * @brief       Display a usart menu to user and include IAP download and upload and area jump operation
 *
 * @version     V1.0.0
 *
 * @date        2026-02-26
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "menu.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup IAP_Examples
  @{
*/

/** @addtogroup IAP_BootLoader
  @{
  */

/** @defgroup IAP_BootLoader_Variables Variables
  @{
*/

static uint8_t filek[1024] __attribute__((aligned(4))) = {0U};
uint8_t FileN[FILE_NAME_MAX] = {0U};

/**@} end of group IAP_BootLoader_Variables */

/** @defgroup IAP_BootLoader_Functions Functions
  @{
*/

/*!
 * @brief       Download a file via serial port
 *
 * @param       Application : APP1 or APP2
 *
 * @retval      None
 *
 * @note
 */
void Iap_Download(App_DefType Application)
{
    uint8_t Number[10] = {0U}, i = 0U;
    uint32_t timeout = (uint32_t)0xFFFFU;
    int32_t Size = 0;

    SendString(">> Waiting for the file to be sent ... (press 'a' to abort)\r\n");
    Size = Iap_ReceiveFile(&filek[0], (uint8_t)Application);
    while (timeout > 0U)
    {
        timeout -= 1U;
    }

    switch (Size)
    {
        case -5:
            SendString(">> Operation failed!.\r\n");
            break;

        case -3:
            SendString(">> Operation aborted!.\r\n");
            break;

        case -2:
            SendString(">> Verification failed!\r\n");
            break;

        case -1:
            SendString(">> The file is too large!\r\n");
            break;

        default:
            Iap_Delay(10000U);
            SendString(">> Programming Completed Successfully!\r\n");

            SendString("***********");

            for (i = 0U; (i < 255U) && (FileN[i] != 0x00U); i++)
            {
                SendString("*");
            }

            SendString("\n\r   Name: ");
            SendString(FileN);
            Iap_IntConStr(Number, (uint32_t)Size);
            SendString("\n\r   Size: ");
            SendString(Number);
            SendString(" Bytes\r\n");

            SendString("***********");

            for (i = 0U; (i < 255U) && (FileN[i] != 0x00U); i++)
            {
                SendString("*");
            }

            if(APP1 == Application)
            {
                SendString("\r\n>> Jump to user application 1 \r\n");
                /* Wait all the string output */
                Iap_Delay(1000U);
                Iap_JumpToApp((uint8_t)APP1);
            }
            else if(APP2 == Application)
            {
                SendString("\r\n>> Jump to user application 2 \r\n");
                /* Wait all the string output */
                Iap_Delay(1000U);
                Iap_JumpToApp((uint8_t)APP2);
            }
            else
            {
                /* do nothing */
            }

            break;
    }
}

/**@} end of group IAP_BootLoader_Functions */
/**@} end of group IAP_BootLoader */
/**@} end of group IAP_Examples */
/**@} end of group Examples */
