230 lines
4.9 KiB
C
230 lines
4.9 KiB
C
#ifndef __OTA_PROTOCOL_H_
|
|
#define __OTA_PROTOCOL_H_
|
|
|
|
#include "arch.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define PARAM_CNT 5
|
|
#define TRUES 1
|
|
#define FALSE 0
|
|
#define CRC32_INITIAL 0
|
|
#define DELAY_MS
|
|
#define FLASH_INIT
|
|
#define FLASH_WRITE_PAGE ble_flash_later_write_page
|
|
#define FLASH_READ_PAGE
|
|
#define FLASH_SECTOR_ERASE ble_flash_later_sector_erase
|
|
#define FlASH_WR_ENABLE
|
|
#define FLASH_WAIT_READY
|
|
#define FLASH_RELEASE_PD
|
|
#define FLASH_ENTER_PD
|
|
#define FLASH_PAGE_ERASE
|
|
#define CEILING(dividend, divisor) \
|
|
((dividend) / (divisor) + (((dividend) % (divisor)) ? 1 : 0))
|
|
#define FOTAS_NOTIFY ota_slave_send_data
|
|
// #define FOTA_PAGE_NUM 16
|
|
#define FOTA_PAGE_NUM 8
|
|
#define OTA_PARAM_ADDR ((8 * 1024) - 512)
|
|
enum fota_write_cmd
|
|
{
|
|
FOTAS_STA_CMD = 1,
|
|
FOTAS_STA_RSP_CMD,
|
|
FOTAS_INFO_CMD,
|
|
FOTAS_INFO_RSP_CMD,
|
|
FOTAS_DATA_CMD,
|
|
FOTAS_COM_CMD,
|
|
FOTAS_RES_CMD,
|
|
};
|
|
enum fota_result_rsp
|
|
{
|
|
FOTAS_SUCCEES,
|
|
FOTAS_FAIL,
|
|
};
|
|
enum fota_inf_stat
|
|
{
|
|
FOTAS_INF_OK,
|
|
FOTAS_INF_ERR,
|
|
};
|
|
enum fota_stat_rsp
|
|
{
|
|
FOTAS_STAT_OK,
|
|
FOTAS_SW_DIF,
|
|
FOTAS_ROM_DIF,
|
|
FOTAS_OVERLOUP,
|
|
};
|
|
|
|
enum fota_sn
|
|
{
|
|
FOTAS_RSP,
|
|
FOTAS_SENT,
|
|
};
|
|
|
|
enum fota_cmd_type
|
|
{
|
|
FOTAS_CMD,
|
|
FOTAS_ACK,
|
|
};
|
|
|
|
enum fota_ack_type
|
|
{
|
|
FOTAS_TRANS_SUC,
|
|
FOTAS_DEV_READY,
|
|
FOTAS_DEV_BUSY,
|
|
FOTAS_TANS_TIOUT,
|
|
FOTAS_TANS_CANCEL,
|
|
FOTAS_TANS_LOSE,
|
|
FOTAS_TANS_BIN_ACK,
|
|
FOTAS_STATE_MAX,
|
|
};
|
|
enum fota_rsp_stat
|
|
{
|
|
FOTAS_RSP_READY,
|
|
FOTAS_RCV_SECT_START,
|
|
FOTAS_RCV_SECT_CANCEL,
|
|
};
|
|
enum fota_rcv_rsp
|
|
{
|
|
FOTAS_RSP_START,
|
|
FOTAS_RSP_CMD,
|
|
FOTAS_RECV_START,
|
|
};
|
|
struct img_hdr_t
|
|
{
|
|
// Secure OTA uses the Signature for image validation instead of calculating
|
|
// a CRC, but the use of CRC==CRC-Shadow for quick boot-up determination of
|
|
// a validated image is still used. User-defined Image Version Number -
|
|
// default logic uses simple a '!=' comparison to start an OTA .
|
|
uint16_t sn;
|
|
uint8_t cmd;
|
|
uint16_t len;
|
|
uint16_t sw_ver;
|
|
uint16_t rom_ver; // Rom ver.
|
|
uint32_t bin_size; // Image fotas_write_env.length in 4-byte blocks ().
|
|
uint32_t crc; // CRC must not be 0x00000000 or 0xFFFFFFFF.
|
|
uint16_t pack_num;
|
|
} __attribute__((packed));
|
|
|
|
struct fota_data_att
|
|
{
|
|
uint16_t sn;
|
|
uint16_t len;
|
|
uint8_t cmd;
|
|
uint32_t segment_id;
|
|
uint32_t crc;
|
|
uint16_t sector_size;
|
|
} __attribute__((packed));
|
|
|
|
struct fota_data
|
|
{
|
|
uint16_t sn;
|
|
uint8_t cmd;
|
|
uint8_t data[256];
|
|
} __attribute__((packed));
|
|
|
|
/*
|
|
APP->DEV
|
|
sn,cmd,data,fnum
|
|
DEV->APP
|
|
sn,cmd,atype,para
|
|
*/
|
|
struct opencode_or_ack
|
|
{
|
|
uint16_t sn;
|
|
uint8_t cmd;
|
|
uint8_t data_or_atype; // data or ack type
|
|
uint16_t fnum_or_para; // frame num or parameter
|
|
uint16_t data;
|
|
} __attribute__((packed));
|
|
|
|
struct dev_send_buf
|
|
{
|
|
uint16_t sn;
|
|
uint16_t len;
|
|
uint8_t cmd;
|
|
uint16_t status;
|
|
} __attribute__((packed));
|
|
|
|
struct fotas_env
|
|
{
|
|
uint16_t sn;
|
|
uint32_t crc;
|
|
uint8_t status;
|
|
uint32_t bin_size;
|
|
uint8_t sector_buf[FOTA_PAGE_NUM][FLASH_PAGE_SIZE];
|
|
uint16_t pack_num;
|
|
struct opencode_or_ack ack;
|
|
struct dev_send_buf send_buf;
|
|
struct fota_data_att data_att;
|
|
} __attribute__((packed));
|
|
struct fotas_write_env
|
|
{
|
|
int old_length;
|
|
int length;
|
|
uint32_t write_addr;
|
|
uint32_t erase_addr;
|
|
uint16_t all_fnum;
|
|
uint16_t cur_num;
|
|
uint8_t para_buf[256];
|
|
};
|
|
typedef struct
|
|
{
|
|
uint32_t Status; /**/
|
|
uint32_t SoftVer; /**/
|
|
uint32_t ACheck; /*crc32*/
|
|
uint32_t AAddr; /*sector align*/
|
|
uint32_t ALen; /*page align*/
|
|
uint32_t BCheck; /**/
|
|
uint32_t BAddr; /**/
|
|
uint32_t BLen; /**/
|
|
uint32_t ALoadAddr;
|
|
uint32_t BLoadAddr;
|
|
uint32_t RESERVED0[2];
|
|
} PARAM_UNIT;
|
|
struct fotas_flag
|
|
{
|
|
uint8_t fir_ack_idx;
|
|
uint8_t fir_rsp_stat;
|
|
uint8_t first_pack_write_flag;
|
|
uint8_t ota_succ_flag;
|
|
uint8_t rsp_stat;
|
|
uint8_t ack_idx;
|
|
uint8_t sector_erase_flag;
|
|
uint8_t wtitten_page_num; //
|
|
uint8_t wtite_flag; // Only when the valid data of the bin file is received,
|
|
// it will be set to 1
|
|
};
|
|
typedef enum
|
|
{
|
|
InvalidStat = 0,
|
|
ActiveAStat = 1,
|
|
EraseAStat = 2,
|
|
OverwriteAStat = 3,
|
|
ActiveBStat = 4,
|
|
EraseBStat = 5,
|
|
OverwriteBStat = 6,
|
|
} STATUS_T;
|
|
|
|
typedef struct
|
|
{
|
|
PARAM_UNIT ParamUnit[PARAM_CNT];
|
|
uint32_t ParamCrc32;
|
|
uint32_t RESERVED1[3];
|
|
} sBOOT_MESSAGE;
|
|
extern uint32_t GenerateCrc32(uint32_t PartialCrc, char *Buffer,
|
|
uint32_t Length);
|
|
|
|
void fota_disconnect_reset(void);
|
|
|
|
void xc_fota_dev_ack_cmd(uint16_t sn, uint8_t cmd, uint8_t data_or_atype,
|
|
uint16_t fnum_or_para);
|
|
|
|
void xc_fota_server_write_ind(uint8_t *buffer, uint16_t buff_size);
|
|
|
|
void ota_write_flash(void);
|
|
|
|
void xc_ota_schedule(void);
|
|
|
|
void wdt_reset_system(void);
|
|
|
|
#endif // __OTA_PROTOCOL_H_
|