/**
 * @file user_interface.c
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2022-11-09
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "user_interface.h"
#include "bs_type.h"
//#include "flash_ep.h"

/**
 * @brief 将差分文件写入用户的flash,用户自己决定是否在写之前擦除
 *
 * @param addr
 * @param p
 * @param len
 * @return int
 */
int bs_flash_write(uint32_t addr, const void *p, uint32_t len)
{
    #if 0
    if (xmq25qxx_write(addr, (uint8_t *)p, len))
    {
        printf("xmq25qxx_write err");
        return 1;  /* 写入flash失败返回错误码 */
    }
    #else

    





    #endif

    return 0;
}



#if 0

 1#ifdef BSDIFF_UPGRADE
  2        image_header_t recv_head;
  3        uint32_t recv_hcrc;  /* 接收到的文件头CRC */
  4        uint32_t calculation_crc;  /* 计算出来的文件头CRC */
  5        uint32_t spi_flash_addr = UPGRADE_PROGRAM_ADDR;
  6
  7        memcpy(&recv_head, (uint8_t *)APPLICATION_A, sizeof(image_header_t));
  8        recv_hcrc = BigtoLittle32(recv_head.ih_hcrc);
  9        recv_head.ih_hcrc = 0;
 10        calculation_crc = crc32((uint8_t *)&recv_head, sizeof(image_header_t));
 11
 12        if (recv_hcrc == calculation_crc)
 13        {
 14            recv_head.ih_hcrc = recv_hcrc;
 15            recv_head.ih_time = BigtoLittle32(recv_head.ih_time);
 16            recv_head.ih_size = BigtoLittle32(recv_head.ih_size);
 17            recv_head.ih_dcrc = BigtoLittle32(recv_head.ih_dcrc);
 18            recv_head.ih_ocrc = BigtoLittle32(recv_head.ih_ocrc);
 19            /* 差分升级包 */
 20            recv_head.ih_hcrc = calculation_crc;
 21            if (crc32((uint8_t *)APPLICATION_RUN, recv_head.ih_load) != recv_head.ih_ocrc)
 22            {
 23                APP_ERR_PRINT("file oldcrc err,calcrc:0X%08X, ih_oldbin_crc:0X%08X,", 
 24                                          crc32((uint8_t *)APPLICATION_RUN, 
 25                                          recv_head.ih_load), recv_head.ih_ocrc);
 26                goto bsdiff_out;
 27            }
 28            RTOS_LOCK();
 29            disable_task_monitoring(ALL_TASK_RUNFLAG_BIT, true);
 30            // flash_erase_sector(UPGRADE_PROGRAM_ADDR, UPGRADE_PROGRAM_PAGE);
 31            recv_hcrc = iap_patch((uint8_t *)APPLICATION_RUN, recv_head.ih_load, 
 32                                 (uint8_t *)(APPLICATION_A + sizeof(image_header_t)),  
 33                                   recv_head.ih_size, UPGRADE_PROGRAM_ADDR);
 34            if (recv_hcrc != recv_head.ih_ep)
 35            {
 36                APP_ERR_PRINT("iap_patch len err.");
 37                APP_ERR_PRINT("iap_patch len: %lu, new_len: %lu", recv_hcrc, recv_head.ih_ep);
 38                goto bsdiff_out;
 39            }
 40            if (erase_program(APPLICATION_A))
 41            {
 42                APP_ERR_PRINT("I erase program failed.");
 43                goto bsdiff_out;
 44            }
 45
 46            current_flash_write_addr = APPLICATION_A;
 47            for (uint32_t i = 0; i < (recv_head.ih_ep / 1024); i++)
 48            {
 49                xmq25qxx_read(spi_flash_addr, spi_read_cache, 1024);
 50                if (xflash_write(current_flash_write_addr, spi_read_cache, 1024))
 51                {
 52                    APP_ERR_PRINT("I write program failed.");
 53                    goto bsdiff_out;
 54                }
 55                spi_flash_addr += 1024;
 56                current_flash_write_addr += 1024;
 57                APP_PRINT("current_flash_write_addr: 0X%08X", current_flash_write_addr);
 58            }
 59            if (recv_head.ih_ep % 1024 != 0)
 60            {
 61                memset(spi_read_cache, 0XFF, 1024);
 62                xmq25qxx_read(spi_flash_addr, spi_read_cache, recv_head.ih_ep % 1024);
 63
 64                if (xflash_write(current_flash_write_addr, spi_read_cache, 1024))
 65                {
 66                    APP_ERR_PRINT("I write program failed.");
 67                    goto bsdiff_out;
 68                }
 69            }
 70            if (crc32((uint8_t *)APPLICATION_A, recv_head.ih_ep) != recv_head.ih_dcrc)
 71            {
 72                APP_ERR_PRINT("file newcrc err,calcrc:0X%08X, newcrc:0X%08X, len: %lu", 
 73                                         crc32((uint8_t *)APPLICATION_A, recv_head.ih_ep), 
 74                                         recv_head.ih_load, recv_head.ih_dcrc);
 75                goto bsdiff_out;
 76            }
 77            /* 下载成功,开始升级 */
 78            if (check_bin_file((bin_info_t *)(APPLICATION_A + BIN_INFO_OFFSET))) /* bin文件非法 */
 79            {
 80                APP_ERR_PRINT("check_bin_file err.");
 81                goto bsdiff_out;
 82            }
 83            recv_head.ih_dcrc = CRT_CRC16_check(0, (uint8_t *)APPLICATION_A, 
 84                                                recv_head.ih_ep);
 85            readwrite_app_run_bin_info(0, &recv_head.ih_ep, (uint16_t *)&recv_head.ih_dcrc);
 86            /* 整体校验成功,确认升级 */
 87            if (switch_program_stage(STAGE_1))
 88            {
 89                APP_ERR_PRINT("I write switch_program_stage STAGE_0 failed.");  /* 置位升级标志写失败 */
 90                goto bsdiff_out;
 91            }
 92            APP_PRINT("upgrade success.");
 93  bsdiff_out:
 94            SYSTEM_RESET();
 95        }


#endif




