
#include "updata_hzk.h"


const char *HzkPath = "/app/lvgl_ui/display/GBhzk22.bin";
uint8_t *g_font_buf;
static int g_hzk = 0;
static int g_img = 0, g_imgsdram;
bool Hzkexist = false;
uint8_t updatabuffer[1025] = {0};

/**
 * @brief
 * @author zhl
 * @date 2025-03-26
 */
void init_displayfile(void)
{
    u8 i = 0;
    g_font_buf = malloc(2 * 1024 * 1024); // 2m分配给字库
    if (g_font_buf != NULL)
    {
        if (read_hzkfrom_file(3) != 0)
        {
            memset(g_font_buf, 0XFF, 2 * 1024 * 1024);
            Hzkexist = false;
        }
        else
            Hzkexist = true;
        
        printf("hzk exist:%d\r\n", Hzkexist);
    }

}

/**
 * @brief
 * @param dirpath
 * @param srcpath
 * @return int
 * @author zhl
 * @date 2025-03-26
 */
int copy_imgfilefrom_sys(char *dirpath, char *srcpath)
{
    uint32_t length = 0;
    uint32_t ret = 0;
    uint16_t i = 0;

    g_img = open(srcpath, O_CREAT | O_RDWR, 0);
    g_imgsdram = open(dirpath, O_CREAT | O_RDWR, 0);

    if (g_img < 0)
    {
        g_img = 0;
        return -1;
    }
    // 存在,检查长度
    length = lseek(g_img, 0, SEEK_END);
    if (length > 90) //>10K
    {
        for (i = 0; i < length / 1024; i++)
        {
            if (length < 1024)
            {
                return -1;
            }
            lseek(g_img, i * 1024, SEEK_SET);
            ret = read(g_img, updatabuffer, 1024);
            if (ret != 1024)
            {
                close(g_img);
                close(g_imgsdram);
                return 2;
            }

            lseek(g_imgsdram, i * 1024, SEEK_SET);
            ret = write(g_imgsdram, updatabuffer, 1024);
            if (ret != 1024)
            {
                close(g_img);
                close(g_imgsdram);
                return -2;
            }
        }
        if (i == length / 1024)
        {
            lseek(g_img, i * 1024, SEEK_SET);
            ret = read(g_img, updatabuffer, length - i * 1024);
            if (ret != (length - i * 1024))
            {
                close(g_img);
                close(g_imgsdram);
                return 2;
            }
            lseek(g_imgsdram, i * 1024, SEEK_SET);
            ret = write(g_imgsdram, updatabuffer, length - i * 1024);
            if (ret != (length - i * 1024))
            {
                close(g_img);
                close(g_imgsdram);
                return -2;
            }
        }
    }
    close(g_img);
    close(g_imgsdram);
    return 0;
}

int read_hzkfrom_file(uint8_t cmd)
{
    uint32_t length = 0;
    uint32_t ret = 0;
    uint16_t i = 0;

    memset(updatabuffer, 0, sizeof(updatabuffer));

    memset(g_font_buf, 0, 256 * 1024);
    g_hzk = open(HzkPath, O_CREAT | O_RDWR, 0);

    if (g_hzk < 0)
    {
        g_hzk = 0;
        close(g_hzk);
        return -1;
    }
    // 检查文件长度是否正确
    length = lseek(g_hzk, 0, SEEK_END);
    if (length > 100 * 1024) //>200K
    {
        for (i = 0; i < length / 1024; i++)
        {
            if (length < 1024)
            {
                close(g_hzk);
                return -1;
            }
            lseek(g_hzk, i * 1024, SEEK_SET);
            ret = read(g_hzk, updatabuffer, 1024);
            if (ret != 1024)
            {
                close(g_hzk);
                return 2;
            }

            if (cmd == 3)
                memcpy(&g_font_buf[i * 1024], updatabuffer, 1024);
        }
        if (i == length / 1024)
        {
            lseek(g_hzk, i * 1024, SEEK_SET);
            ret = read(g_hzk, updatabuffer, length - i * 1024);
            if (ret != (length - i * 1024))
            {
                close(g_hzk);
                return 2;
            }

            if (cmd == 3)
                memcpy(&g_font_buf[i * 1024], updatabuffer, length - i * 1024);
        }
    }
    else
    {
        close(g_hzk);
        return 3;
    }
    close(g_hzk);
    return 0;
}
