Last-Modified: Sun, 26 Jul 2026 05:02:54 GMT Expires: Wed, 23 Jul 2036 05:02:54 GMT FirmwareUpgrade.c « SWUpdate « Common « Embedded « Embedded_SW « Software - Tango - Twine softwares
aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Common/SWUpdate/FirmwareUpgrade.c
blob: 01a30c21a3ab3b378fde786f433856fbaa1484fb (plain)
1
2
3
4
5
6
7
8
generated by cgit v1.3.1 (git 2.54.0) at 2026-07-26 05:02:54 +0000
 


a id='n200' href='#n200'>200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * FirmwareUpgrade.c
 *
 *  Created on: Dec 24, 2018
 *      Author: shlomo
 */

#include "include.h"
#include "Communication/CommunicationTask.h"
#include <DataDef.h>

#include  <Communication/PMR/FirmwareUpgrade/ActivateVersionRequest.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ActivateVersionResponse.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ActivateVersionRequest.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ActivateVersionResponse.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ValidateVersionRequest.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ValidateVersionResponse.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ValidateVersionRequest.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/ValidateVersionResponse.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/VersionFileDestination.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/VersionFileDescriptor.pb-c.h>
#include  <Communication/PMR/FirmwareUpgrade/VersionPackageDescriptor.pb-c.h>

#include "drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"
#include "drivers/FPGA/Full_Vme/FPGA_Programming_Up.h"
#include "drivers/FPGA/Full_Vme/ispvme/vmopcode.h"
#include "Common/Utilities/Utils.h"

#include "Common/SWUpdate/FileSystem.h"
#include "Modules/Control/Control.h"

int NumberOfFiles = 0,CurrentRunningFile;
int CurrentFileSize = 0;
uint32_t ActivateVersionControlId = 0xFF;
char activateString[100];
char ActivateToken[36+1]={0};
bool Reboot = false;


bool SwUpgradeActive(void)
{
    if (ActivateVersionControlId != 0xFF)
    {
        Reboot = false;
        return true;
    }
    return false;
}
uint32_t ActivateVersionCallBackFunction(uint32_t IfIndex, uint32_t BusyFlag)
{
    //ActivateVersionResponse response = ACTIVATE_VERSION_RESPONSE__INIT;
    //MessageContainer responseContainer;

    usnprintf(activateString, 1000, "file %d of %d bytes %d of %d",CurrentRunningFile,NumberOfFiles,vme_index,CurrentFileSize);

    Report(activateString,__FILE__,__LINE__,(int)CurrentRunningFile,RpWarning,CurrentFileSize,0);

    if (CurrentRunningFile<NumberOfFiles)
        return OK;
    if (SafeRemoveControlCallback(ActivateVersionControlId, ActivateVersionCallBackFunction )==OK)
        ActivateVersionControlId = 0xFF;
    else
        LOG_ERROR(255,"ActivateVersionCallBackFunction");
    return OK;

    /*
    responseContainer = createContainer(MESSAGE_TYPE__ActivateVersionResponse,  ActivateToken, false, &response, &activate_version_response__pack, &activate_version_response__get_packed_size);
    responseContainer.continuous = true;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

    if (Reboot == true)
    {
        Power_Reset();
    }
*/
}
int File_i = 0;

uint32_t    ActivateVersionRequestFunc(MessageContainer* requestContainer)
{
    void* buffer = NULL;
    uint32_t Bytes = 0;
    char SWUpgradePath[100];
    char FullPath[50];
    FRESULT Fresult = FR_OK;
    FILINFO* fno = 0;

    fno = my_malloc(sizeof(FILINFO));
    if (fno == 0)
        return ERROR;
    MessageContainer responseContainer;

    ActivateVersionRequest* request = activate_version_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    ActivateVersionResponse response = ACTIVATE_VERSION_RESPONSE__INIT;
    VersionPackageDescriptor* VersionPackage;// = VERSION_PACKAGE_DESCRIPTOR__INIT;

    FPGA_ID FileDestinationToFPGAId[VERSION_FILE_DESTINATION__FPGA3+1] = {MAX_FPGA,FPGA1,FPGA2,FPGA3};

    ustrncpy (ActivateToken, requestContainer->token,36);

    strcpy (SWUpgradePath,request->path);
    strcat(SWUpgradePath,"/package.cfg");

    Fresult = FileRead(SWUpgradePath, &Bytes, &buffer);
    if (Fresult == FR_OK)
    {
        VersionPackage = version_package_descriptor__unpack(NULL,Bytes,buffer);
        NumberOfFiles = VersionPackage->n_filedescriptors;
        if (NumberOfFiles)
        {
            CurrentRunningFile = 0;
            File_i = 0;
            //add control 1 second
            ActivateVersionControlId = AddControlCallback( ActivateVersionCallBackFunction, eOneSecond, TemplateDataReadCBFunction,0,0, 0 );
        }
        for (File_i = 0;File_i<VersionPackage->n_filedescriptors;File_i++)
        {
            CurrentRunningFile = 0;
            CurrentFileSize = -1;
            vme_index = 0;
            switch(VersionPackage->filedescriptors[File_i]->destination)
            {
            case   VERSION_FILE_DESTINATION__MCU:
                break;
            case VERSION_FILE_DESTINATION__FPGA1:
            case VERSION_FILE_DESTINATION__FPGA2:
            case VERSION_FILE_DESTINATION__FPGA3:
                //validate
                //update
                usnprintf(FullPath, 50,  "%s%s%s", request->path,"/", VersionPackage->filedescriptors[File_i]->filename);
                Reboot = true;
                memset (fno,0,sizeof(FILINFO));
                Fresult = f_stat(FullPath,fno);
                CurrentFileSize = fno->fsize;
                Fresult = FPGA_Programming_Request( FileDestinationToFPGAId[VersionPackage->filedescriptors[File_i]->destination], FullPath, false);
                break;
            case VERSION_FILE_DESTINATION__ConfigParams:
                    usnprintf(FullPath, 50,  "%s%s%s", request->path,"/", VersionPackage->filedescriptors[File_i]->filename);
                    Fresult = f_unlink("//SYSINFO//EMBPARAM.CFG");
                    Fresult |= f_rename (FullPath, "//SYSINFO//EMBPARAM.CFG");
                break;
            case     VERSION_FILE_DESTINATION__ProcessParams:
                    usnprintf(FullPath, 50,  "%s%s%s", request->path,"/", VersionPackage->filedescriptors[File_i]->filename);
                    Fresult = f_unlink("//SYSINFO//PROCESSP.CFG");
                    Fresult |= f_rename (FullPath, "//SYSINFO//PROCESSP.CFG");
                break;
            case     VERSION_FILE_DESTINATION__AlarmParams:
                    usnprintf(FullPath, 50,  "%s%s%s", request->path,"/", VersionPackage->filedescriptors[File_i]->filename);
                    Fresult = f_unlink("//SYSINFO//ALARMPRM.CFG");
                    Fresult |= f_rename (FullPath, "//SYSINFO//ALARMPRM.CFG");
                break;
            case     VERSION_FILE_DESTINATION__GeneralHWConfigParams:
                    usnprintf(FullPath, 50,  "%s%s%s", request->path,"/", VersionPackage->filedescriptors[File_i]->filename);
                    Fresult = f_unlink("//SYSINFO//GENHWCFG.CFG");
                    Fresult |= f_rename (FullPath, "//SYSINFO//GENHWCFG.CFG");
                break;
            default:
                LOG_ERROR (VersionPackage->filedescriptors[File_i]->destination,"wrong file update Destination");
                Fresult = FR_INVALID_NAME;
                break;
            }
        }
    }
    responseContainer = createContainer(MESSAGE_TYPE__ActivateVersionResponse,  requestContainer->token, false, &response, &activate_version_response__pack, &activate_version_response__get_packed_size);
    if (Fresult!= OK)
    {
        responseContainer.error = FileError_to_ErrorCode[Fresult];
        responseContainer.errormessage = "Activate Version Request error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    activate_version_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    if (Reboot == true)
    {
        Power_Reset();
    }
    return OK;
}

uint32_t    ValidateVersionRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    ValidateVersionRequest* request = validate_version_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    ValidateVersionResponse response = VALIDATE_VERSION_RESPONSE__INIT;


    responseContainer = createContainer(MESSAGE_TYPE__ValidateVersionResponse,  requestContainer->token, false, &response, &validate_version_response__pack, &validate_version_response__get_packed_size);
    if (status!= OK)
    {
        responseContainer.error = ERROR_CODE__INVALID_UPLOAD_ID;
        responseContainer.errormessage = "Validate Version Request error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    validate_version_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}