Aggiornamento automatico FOTA dei sistemi

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

FOTA, o Firmware Over-The-Air , è una tecnologia che consente ai dispositivi connessi ad Internet di eseguire aggiornamenti delle loro versioni del firmware in remoto senza la necessità di intervento fisico nel dispositivo. La capacità di aggiornare i sistemi è essenziale per mantenere i dispositivi protetti, aggiungere nuove funzionalità e correggere bug.

Aggiornamento sistemi programmabili

Nel caso di sistemi programmabili, possono esistere più esigenze di aggiornamento automatico.

  • Aggiornamento del sistema operativo Firmware del prodotto per aggiungere nuove funzionalità e correggere bug di sistema.
  • Aggiornamento del programma PLC per per aggiungere nuove funzionalità richieste dal cliente e correggere eventuali errori di funzionamento.
  • Modifica memoria tampone per riconfigurare parametri di funzionamento.

Sui nostri sistemi programmabili grazie alle funzioni avanzate di backup & restore è possibile creare files di backup con l’immagine del sistema operativo, del programma utente e dei dati di backup. Creato il file è possibile hostarlo su di un server FTP nel cloud in modo da essere accessibile dai sistemi.

I sistemi possono leggere il file di backup e trasferirlo nel loro file system, poi eseguendo un reboot aggiornarsi in automatico.

Programma “FOTAUpdate”

Il programma FOTAUpdate scaricabile dal sito, riporta un esempio di come sia possibile implementare l’aggiornamento automatico sui nostri sistemi. Come si vede dal listato il programma ha una esecuzione a stati.

  • Attivando la variabile FOTAStart, si passa al case 10 dove si attiva il download dal server FTP del file indicato in FTP.RemoteFile che viene salvato sul file system del sistema nel file indicato in FTP.LocalFile. Il file deve avere come nome SYSTEM.UPL (Case sensitive).
  • Terminato il download viene eseguito un log sul file di log indicato in Logger.Filename, in questo modo è possibile verificare dal file di log l’avvenuto aggiornamento.
  • Si attende un tempo per consentire eventuale salvataggio di dati ritentivi, poi si forza il riavvio del sistema reboot.
  • Al riavvio il sistema controlla se nel file system vi è il file SYSTEM.UPL, lo controlla e se il file è compatibile con il sistema ne viene eseguito l’aggiornamento. Poi il sistema si riavvia.
  • Ad ogni avvio SysFirstLoop=TRUE, si controlla se vi è stato un update. In questo caso è già in esecuzione il nuovo programma, ne viene salvato nel file di log il nome. Se nel file system vi è il file SYSTEM.UPL, viene cancellato. La cancellazione del file è necessaria per evitare che ad ogni accensione del sistema venga controllato.
LogicLab (Ptp204, FOTAUpdate)

PROGRAM FOTAUpdate
VAR
    i : UDINT; (* Auxiliary variable *)
    FOTAStart : BOOL; (* FOTA start command *)
    Updated : BOOL; (* System has been updated *)
    CaseNr : USINT; (* Program case *)
    TimeBf : UDINT; (* Time buffer (mS) *)
    Header : STRING[ 32 ]; (* Header string *)
    FTP : FTPClient_v3; (* FTP client *)
    Logger : StringToLogFile_v3; (* Data logger *)
    LData : STRING[ 64 ]; (* Log data string *)
END_VAR

// *****************************************************************************
// PROGRAM "FOTAUpdate"
// *****************************************************************************
// By setting the FOTA command a new program file is loaded from a FTP server.
// After download system is rebooted and the new program replace the existing.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Program initializations.

    IF (SysFirstLoop) THEN

        // FTP client definitions.

        FTP.SpyOn:=TRUE; //Spy active
        FTP.FTPServer:=ADR('server'); //Server FTP
        FTP.FTPPort:=21; //Server port
        FTP.User:=ADR('username'); //User
        FTP.Password:=ADR('password'); //Password

        FTP.LocalFile:=ADR('D:/SYSTEM.UPL'); //Local file definition
        FTP.RemoteFile:=ADR('/Firmware.upl'); //Remote file definition
        FTP.Timeout:=15.0; //Timeout (S)

        // Data logger definitions.

        Logger.Mode:=1; //AsciiHex mode
        Logger.LDLength:=0; //Log data length
        Logger.Header:=ADR(Header); //Header string
        Logger.Filename:=ADR('D:/FOTALog.txt'); //Log file

        // On startup it's controlled if the system has been updated and the
        // SYSEM.UPL file is deleted to avoid to check it at every startup.

        i:=SysOSIDValue(FALSE, OSID_UPDATE_EXECUTED, ADR(Updated));
        IF (Updated) THEN
            i:=DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/y H\:i\:s\|'), ADR(Header), SIZEOF(Header));
            i:=SysVsnprintf(ADR(LData), SIZEOF(LData), ADR('System updated, program:%s$r$n'), STRING_TYPE, ADR(SysApllName));
            Logger(LData:=ADR(LData)); //Write data to file

            IF (SysGetFileLen(ADR('D:/SYSTEM.UPL')) <> eEOF) THEN
                i:=SysFileRemove(ADR('D:/SYSTEM.UPL')); //Delete file

                // Save a log report on file.

                i:=DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/y H\:i\:s\|'), ADR(Header), SIZEOF(Header));
                Logger(LData:=ADR('System updated, update file deleted$r$n')); //Write data to file
            END_IF;
        END_IF;
    END_IF;

    // -------------------------------------------------------------------------
    // FTP CLIENT
    // -------------------------------------------------------------------------
    // Executs the FTP client and resets the commands on execution fault.

    FTP(); //FTP client
    IF (FTP.Fault) THEN CaseNr:=0; END_IF;

    // -------------------------------------------------------------------------
    // PROGRAM CASES
    // -------------------------------------------------------------------------
    // Program cases management.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // EXECUTE FOTA COMMAND
        // ---------------------------------------------------------------------
        // Reset FTP commands and wait for FOTA command.

        0:
        FTP.Store:=FALSE; //Store command
        FTP.Retrieve:=FALSE; //Retrieve command
        FTP.Delete:=FALSE; //Delete command
        IF (FOTAStart) THEN CaseNr:=10; END_IF;

        // ---------------------------------------------------------------------
        // Connects to FTP server and read program file.

        10:
        FTP.Retrieve:=TRUE; //Retrieve command
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Retrieve:=FALSE; //Retrieve command
        FOTAStart:=FALSE; //FOTA start command

        // Save a log report on file.

        i:=DateTimeFormat(TO_LDATE_AND_TIME(SysDateGetNs()), ADR('^d/m/y H\:i\:s\|'), ADR(Header), SIZEOF(Header));
        Logger(LData:=ADR('Executed a FOTA update$r$n')); //Write data to file

        TimeBf:=SysTimeGetMs(); //Time buffer (mS)
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Waiting a while before reboot system. This is needed to allow data
        // retain.

        11:
        IF ((SysTimeGetMs()-TimeBf) < TO_UDINT(T#1m)) THEN RETURN; END_IF;
        i:=SysOSIDValue(TRUE, OSID_PLC_COMMAND, ADR('reboot'));
    END_CASE;

// [End of file]
Was this article helpful?