struct stFW_RequestDownload
{
    struct can_frame frame;
    stFW_RequestDownload(unsigned long int  fw_sizw)
    {
        memset(&frame.data[0], 0,8);

        frame.data[0] = 0x10;
        frame.data[1] = 0x0B;
        frame.data[2] = 0x34;
        frame.data[3] = 0x00;
        frame.data[4] = 0x44;
        frame.data[5] = 0x1D;
        frame.data[6] = 0x00;
        frame.data[7] = 0x00;

        frame.can_id  =  0x00000700;
        frame.can_dlc = 8;
    }
};

struct stFW_RequestDownloadFWSize
{
    struct can_frame frame;
    stFW_RequestDownloadFWSize(uint32_t fw_size)
    {
        memset(&frame.data[0], 0, 8);

        frame.data[0] = 0x21;  // CF #1

        // Put FW size (big-endian) into bytes 1..4
        frame.data[1] = (fw_size >> 24) & 0xFF;  // MSB
        frame.data[2] = (fw_size >> 16) & 0xFF;
        frame.data[3] = (fw_size >> 8)  & 0xFF;
        frame.data[4] = (fw_size)       & 0xFF;  // LSB

        // Padding
        frame.data[5] = 0x55;
        frame.data[6] = 0x55;
        frame.data[7] = 0x55;

        frame.can_id  = 0x00000700;
        frame.can_dlc = 8;
    }
};


uint32_t dcComboPLC::Read_FW_File_Size()
{
    QString fwPath = "/home/root/GQ_FW_1.gqf";
    qint64 fileSize = 0;

    QFile file(fwPath);
    if (file.open(QIODevice::ReadOnly))
    {
        fileSize = file.size();
        qDebug() << "Firmware file size = " << fileSize;
        file.close();
    }
    else
    {
        qDebug() << "Unable to open firmware file!";
        return 0;  // error
    }

    if (fileSize <= 0 || fileSize > 0xFFFFFFFF)
    {
        qDebug() << "Firmware size out of range!";
        return 0;
    }

    uint32_t size32 = static_cast<uint32_t>(fileSize);

    uint8_t fileSizeBytes[4];
    fileSizeBytes[0] = (size32 >> 24) & 0xFF;
    fileSizeBytes[1] = (size32 >> 16) & 0xFF;
    fileSizeBytes[2] = (size32 >> 8)  & 0xFF;
    fileSizeBytes[3] = (size32)       & 0xFF;

    qDebug() << "fileSizeBytes[0]:" << fileSizeBytes[0];
    qDebug() << "fileSizeBytes[1]:" << fileSizeBytes[1];
    qDebug() << "fileSizeBytes[2]:" << fileSizeBytes[2];
    qDebug() << "fileSizeBytes[3]:" << fileSizeBytes[3];

    return size32;
}



void dcComboPLC::Request_Download(void)
{
    // 1) Read firmware size from file
    uint32_t fw_size = Read_FW_File_Size();
    if (fw_size == 0)
    {
        DebugString("Invalid firmware size, abort Request_Download");
        return;
    }

    // 2) First frame: 10 0B 34 00 44 1D 00 00
    stFW_RequestDownload *pFW_RequestDownload = new stFW_RequestDownload(0);
    if (write(can2_fd, &pFW_RequestDownload->frame, sizeof(struct can_frame)) 
        != sizeof(struct can_frame))
    {
        DebugString("Request Download FF send failed");
        SAFE_DELETE(pFW_RequestDownload);
        return;
    }
    SAFE_DELETE(pFW_RequestDownload);

    // 3) Consecutive frame: 21 <FileSize[0..3]> 55 55 55
    stFW_RequestDownloadFWSize *pFW_RequestDownloadFWSize =
            new stFW_RequestDownloadFWSize(fw_size);

    if (write(can2_fd, &pFW_RequestDownloadFWSize->frame, sizeof(struct can_frame)) 
        != sizeof(struct can_frame))
    {
        DebugString("Request Download CF (FW size) send failed");
        SAFE_DELETE(pFW_RequestDownloadFWSize);
        return;
    }
    SAFE_DELETE(pFW_RequestDownloadFWSize);
}
