/*****************************************************************************
 * file    include/pd_upgrade.h 
 * author  Wangbo
 * version 1.0.0
 * date    07-Feb-2023
 * brief   This file provides all the headers of the softwave upgrade functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2024 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
 *
 ******************************************************************************/

#ifndef __PD_UPGRADE_H__
#define __PD_UPGRADE_H__

#ifdef CFG_DEV_TYPE_LAND_PD
/* Includes ------------------------------------------------------------------*/

/* Define --------------------------------------------------------------------*/
#define PD_UPG_MAGICNUM 0xEACFA6A6         
#define PD_UPG_AREA_MAX 2               // 最多升级区域
#define PD_UPG_VERSION_LEN 16
#define PD_UPG_BLOCK_SIZE (128*1024)
#define PD_UPG_SOFTWARE "upgrade.sw"
#define PD_UPG_CMU_FILE "PDMonitor"
#define PD_UPG_CMU_FILE_BAK "PDMonitor.bak"
#define PD_UPG_DAU_FILE "system_wrapper.bin"
#define PD_UPG_DAU_FILE_BAK "system_wrapper.bak"

#define PD_UPG_SEL_ALL 0
#define PD_UPG_SEL_CMU 1
#define PD_UPG_SEL_DAU 2
#define PD_UPG_SEL_MAX 3

/* Exported types ------------------------------------------------------------*/
enum PD_UPG_TYPE
{
    PD_UPG_TYPE_CMU,
    PD_UPG_TYPE_DAU,
    PD_UPG_TYPE_MAX,
};

enum PD_UPG_FROM
{
    PD_UPG_FROM_CSG,
    PD_UPG_FROM_DBG,
    PD_UPG_FROM_MAX,
};

/*升级文件头*/
typedef struct
{
    uint32_t magic;                     // 结构体幻数 PD_UPG_MAGICNUM
    uint32_t crc;                       // 这里的 CRC 值只包括程序数据的 CRC 校验值 (不包含头)
    uint32_t version;                   // 结构体版本
    char hard_ver[PD_UPG_VERSION_LEN];  // hardware version 
    struct
    {
        char version[PD_UPG_VERSION_LEN];
        uint32_t type;                  // PD_UPG_TYPE_XXX
        uint32_t start;                 // 文件内的偏移量
        uint32_t len;                   // 升级数据的长度
    } arr_info[PD_UPG_AREA_MAX];        // 分段信息
} pd_upg_head_t;

typedef struct
{
    uint8_t is_start;           // 是否开始升级
    uint8_t upg_from;           // 是谁在升级 PD_UPG_FROM_xxx
    uint8_t upg_type;           // 升级类型 PD_UPG_TYPE_xxx
    pd_upg_head_t head;         // 报文头信息
    void (*upgrade_result)(int32_t rv, char *msg);  // 升级结果回调函数
    char msg[128];              // 返回结果字符串
} pd_upg_ctrl_t;

/* Exported macro ------------------------------------------------------------*/

/* Extern global variables ---------------------------------------------------*/

/* Extern functions ----------------------------------------------------------*/
extern int pd_upg_start(uint8_t from, uint8_t type);
#endif
#endif
