Skip to content

file ota.h

Types

Name
structota_target_handler_t
structota_image_version_t
Represents the version number information of an OTA image.
structota_server_t
Represents an OTA (Over-The-Air) server.
structota_t
Represents an Over-The-Air (OTA) update.
structota_downloader_t
structota_custom_t
Represents an Over-The-Air (OTA) update using a custom download mechanism.
structcw_ota_status_t
Live snapshot of the HW-slot-0 (Coldwave SW-OTA) firmware update.

Source code

cpp
/*******************************************************************************
 * @file       ota.c
 * @brief
 * @copyright  Copyright (c) 2023. ImagineOn GmbH. www.imagineon.de
 *             All rights reserved.
 ******************************************************************************/

#ifndef OTA_H
#define OTA_H

#include <inttypes.h>
#include <stdlib.h>
#include "checksum.h"

#if defined(__cplusplus)
extern "C"
{
#endif

#define OTA_MAX_UPDATE_TARGETS (16) 

    typedef int (*ota_begin_t)(void* p);
    typedef int (*ota_append_t)(void* p, uint8_t const* const data, size_t const size);
    typedef int (*ota_finalize_t)(void* p);
    typedef int (*ota_abort_t)(void* p);

    typedef void* ota_context_t;

    typedef struct ota_target_handler_t
    {
        ota_context_t ctx;                 
        ota_begin_t on_update_begin;       
        ota_append_t on_update_append;     
        ota_finalize_t on_update_finalize; 
        ota_abort_t on_update_abort;       
    } ota_target_handler_t;

    typedef struct ota_image_version_t
    {
        uint8_t maj;    
        uint8_t min;    
        uint16_t patch; 
        uint32_t build; 
    } ota_image_version_t;

    typedef enum ota_protocol_t
    {
        OTA_PROTO_PLAIN = 0,     
        OTA_PROTO_TFTP = 1,      
        OTA_PROTO_HTTP = 2,      
        OTA_PROTO_PLAIN_TLS = 3, 
        OTA_PROTO_HTTPS = 4,     
        OTA_PROTO_CUSTOM = 5     
    } ota_protocol_t;

    typedef struct ota_server_t
    {
        ota_protocol_t protocol; 
        char* hostname;          
        uint16_t port;           
        struct
        {
            char* filename; 
        } ota_http_params;
        struct
        {
            unsigned char* cert;   
            unsigned int cert_len; 
            uint8_t pin_pubkey_sha256[32];
        } ota_tls_params;
    } ota_server_t;

    typedef struct ota_t
    {
        int target;                       
        ota_server_t server;              
        uint32_t size_bytes;              
        ota_checksum_algorithm_t cs_algo; 
        ota_checksum_t cs;                
        uint32_t start_offset;            
    } ota_t;

    typedef struct ota_downloader_t
    {
        int (*connect)(void* ctx);
        int (*recv)(void* ctx, char* data, int data_len);
        int (*send)(void* ctx, char* data, int data_len);
        int (*close)(void* ctx);
    } ota_downloader_t;

    typedef struct ota_custom_t
    {
        int target;                       
        ota_downloader_t downloader;      
        uint32_t size_bytes;              
        ota_checksum_algorithm_t cs_algo; 
        ota_checksum_t cs;                
        void* ctx;                        
        uint32_t start_offset;            
    } ota_custom_t;

#define OTA_FLG_IDLE (0)                    
#define OTA_FLG_SUCCEEDED (1)               
#define OTA_FLG_FAILED (2)                  
#define OTA_FLG_FAILED_CONNECT (5)          
#define OTA_FLG_FAILED_UPDATE_RUNNING (6)   
#define OTA_FLG_FAILED_FLASH (7)            
#define OTA_FLG_FAILED_IMAGE_TOO_BIG (8)    
#define OTA_FLG_FAILED_UNSUP_ALGO (9)       
#define OTA_FLG_FAILED_CHECKSUM (10)        
#define OTA_FLG_FAILED_REMAINING_BYTES (11) 
#define OTA_FLG_FAILED_TIMEOUT (12)         
#define OTA_FLG_FAILED_SIGNATURE (13)       
#define OTA_FLG_FAILED_TARGET (14)          
#define OTA_FLG_FAILED_RESOLVE_HOST (15)    
#define OTA_FLG_FAILED_METADATA (16)        
#define OTA_FLG_FAILED_ABORTED (17)         
#define OTA_FLG_UPDATE_STARTED (20)         
#define OTA_FLG_PENDING_UPDATE (0xAA)       

    int
    ota_download(ota_t* ota);

    int
    ota_register_target_handler(int update_target_id, ota_target_handler_t* hnd);

    int
    ota_download_custom(ota_custom_t* ota);

#define CW_OTA_HAS_RESUME_API 1

    int
    ota_running(void);

    int
    cw_ota_resume_point(uint32_t* size_bytes, uint32_t* committed);

    int
    cw_ota_resume_matches(const uint8_t* cs, uint16_t cs_len, uint32_t size_bytes);

    int
    cw_ota_confirm(void);

    int
    cw_ota_is_confirmed(void);

    typedef struct cw_ota_status_t
    {
        uint32_t code;        
        uint32_t bytes_done;  
        uint32_t bytes_total; 
    } cw_ota_status_t;

    int
    cw_ota_status_get(cw_ota_status_t* out);

#if defined(__cplusplus)
}
#endif

#endif // OTA_H