Files
kunlun/inc/host_interface/plc/iot_cli_host_upgrade.h
2024-09-28 14:24:04 +08:00

574 lines
21 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#ifndef IOT_CLI_HOST_UPGRADE_H
#define IOT_CLI_HOST_UPGRADE_H
/* os shim includes */
#include "os_types.h"
/* common includes */
#include "iot_addr_hash_table_api.h"
#include "iot_plc_sta_api.h"
#include "upgrade.h"
#include "iot_cli_plc_module.h"
#include "iot_cli_upgrade.h"
#include "iot_cli_upgrade_api.h"
#ifdef __cplusplus
extern "C" {
#endif
/* tei map byte len */
#define PLC_TEI_MAP_BYTE_LEN (256)
/* type of notify ack */
#define IOT_PLC_NOTIF_ACK 1
#define IOT_PLC_NOTIF_ACK_UPGRADED 2
/* max remote uprade retry times */
#define IOT_PLC_UPGRADE_MAX_START_RETRY_TIMES 7
#define IOT_PLC_UPGRADE_LATEST_BLOCK_CNT 3
#define IOT_PLC_UPGRADE_PROGRESS_REPORT_INTERVAL 5
#define IOT_PLC_UPGRADE_PROGRESS_BLOCKS_INTERVAL 30
#define IOT_PLC_UPGRADE_DEFAULT_TIMER 3000
#define IOT_PLC_UPGRADE_LONG_TIMER_CNT 10
#define IOT_PLC_UPGRADE_COMPLETE_CHECK_TIMER_CNT 250
#define IOT_PLC_UPGRADE_TIME_WINDOW_ADJ_VALUE1 (0.7)
#define IOT_PLC_UPGRADE_TIME_WINDOW_ADJ_VALUE2 2
#define IOT_PLC_UPGRADE_TIME_WINDOW_ADJ_VALUE3 (1.3)
#define IOT_PLC_UPGRADE_STOP_RETRY_TIMES 5
#define IOT_PLC_UPGRADE_RESET_RETRY_TIMES 3
#define IOT_PLC_UPGRADE_START_RETRY_TIMES 3
#define IOT_PLC_UPGRADE_DATA_RETRY_TIMES 1
#define IOT_PLC_UPGRADE_PHASE_THREE_RETRY_TIMES 3
/* cco boardcast parameter define */
/* cco simulator boardcast interval time, unit: ms */
#define IOT_PLC_UPGRADE_CCO_BCAST_TIME 800
/* cco boardcast min interval time, unit: ms */
#define IOT_PLC_UPGRADE_CCO_MIN_BCAST_TIME 100
/* cco boardcast normal interval time, unit: ms */
#define IOT_PLC_UPGRADE_CCO_SHORT_BCAST_TIME 200
/* cco boardcast min sta count use min boardcast interval tm */
#define IOT_PLC_UPGRADE_CCO_MIN_BCAST_NODE_CNT 5
/* cco min boardcast plc retry times */
#define IOT_PLC_UPGRADE_CCO_BCAST_MIN_RETRY_CNT 2
/* cco boardcast upgrade file times about upgrade sta cnt threshold1 */
#define IOT_PLC_UPGRADE_BCAST_STA_THRESHOLD 200
/* cco boardcast upgrade file times about upgrade sta cnt threshold2 */
#define IOT_PLC_UPGRADE_BCAST_STA_THRESHOLD2 900
/* cco boardcast upgrade file times level define */
#define IOT_PLC_UPGRADE_BCAST_TIMES_LEV1 1
#define IOT_PLC_UPGRADE_BCAST_TIMES_LEV2 2
#define IOT_PLC_UPGRADE_BCAST_TIMES_LEV3 3
#define IOT_PLC_UPGRADE_BCAST_TIMES_LEV4 4
/* cco boardcast start upgrade sta max count per time window */
#define IOT_PLC_UPGRADE_BROADCAST_STA_MAX_CNT 50
/* max broadcast start upgrade count */
#define IOT_PLC_UPGRADE_BROADCAST_TIMES_CNT 10
#define IOT_PLC_UPGRADE_PCO_REJECT_REQ_TIME 400
#define IOT_PLC_UPGRADE_CCO_REJECT_REQ_TIME 500
#define IOT_PLC_UPGRADE_STA_SKIP_REQ_TIME 800
#define IOT_PLC_UPGRADE_STA_CNT_ADJ_VALUE (0.70)
#define IOT_PLC_UPGRADE_STA_CNT_ADJ_SIMU_VALUE 2
#define IOT_PLC_UPGRADE_PCO_ADJ_TIME 15
#define IOT_PLC_UPGRADE_PCO_ADJ_COUNT 50
#define IOT_PLC_UPGRADE_LATEST_STATUS_MAX_CNT 240
#define IOT_PLC_UPGRADE_DEFAULT_REMOTE_BLOCK_CNT 1
#define IOT_PLC_UPGRADE_PCO_DEFAULT_ADJ_COUNT 1
#define IOT_PLC_UPGRADE_SMALL_NODES_COUNT 32
#define IOT_PLC_UPGRADE_MID_NODES_COUNT 100
#define IOT_PLC_UPGRADE_STATUS_TABLE_SIZE 5
#define IOT_PLC_UPGRADE_STATUS_ITEM_MAX_CNT PLC_NETWORK_SCALE
#define IOT_PLC_UPGRADE_PHASE_THREE_THRESHOLD (0.50)
#define IOT_PLC_UPGRADE_PHASE_THREE_MAX_SAT_CNT PLC_NETWORK_SCALE
#define IOT_PLC_UPGRADE_PHASE_THREE_START_CNT 2
#define IOT_PLC_UPGRADE_RETRY_COUNT (1)
/* in ms */
#define IOT_PLC_UPGRADE_WRITE_FLASH_INTERVAL 20
/* max supported upgrading FW size is 300*8*400 bytes. */
#define IOT_PLC_UPGRADE_STA_BM_SIZE_MAX (300)
/* cco query node info timeout, unit ms */
#define IOT_PLC_UPGRADE_QUERY_NODE_INFO_TIMEOUT (4000)
/* query node info retry count max*/
#define IOT_PLC_UPGRADE_QUERY_RETRY_MAX (6)
/* query node all bitmap */
#define IOT_PLC_UPGRADE_QUERY_BM_ALL (0xFFFF)
/* invalid block index */
#define IOT_PLC_UPGRADE_INVALID_BLOCK_INDEX (0xFFFF)
/* send 5 data block at once */
#define IOT_PLC_UPGRADE_SEND_BLOCK_CNT (3)
/* the link load greater than threshold, broadcast wait flag is used */
#define IOT_PLC_UPGRADE_LINK_LOAD_LIMIT (75)
#if PLC_SUPPORT_CCO_ROLE
typedef enum {
CLI_QUERY_NODE = 0,
CLI_SEND_DATA,
} cli_resend_data_state;
typedef enum {
CLI_UPGRADE_TYPE_UNKNOWN = 0,
CLI_UPGRADE_TYPE_V0,
CLI_UPGRADE_TYPE_V1,
} cli_upgrade_module_type;
typedef enum {
CLI_FIND_START_NODE,
CLI_FIND_QUERY_NODE,
} cli_upgrade_find_mode;
typedef struct _iot_plc_upgrade_dest_desc_t {
iot_addr_hash_entry_t entry;
uint8_t status; // upgrade result for dest
uint8_t error_code; // upgrade error reason
uint8_t percentage; // upgrade result for dest
uint8_t reported_percentage;
uint8_t start_retry_times; // remote upgrade retry times
uint8_t updated :1,
upgrade_type :2, // node upgrade type
reserved :5;
} iot_plc_upgrade_dest_desc_t;
/* dests management, for remote upgrade cco role only */
typedef struct _iot_plc_upgrade_dest_info_t {
/* hash table for dest info entry. */
iot_addr_hash_table_h table;
/* pointer array point to hash table entry for iteration. */
iot_plc_upgrade_dest_desc_t **entry_ptr;
/* current upgrading dst list for remote upgrade */
iot_plc_upgrade_dest_desc_t
*latest_status_list[IOT_PLC_UPGRADE_LATEST_STATUS_MAX_CNT];
/* upgrading sta list */
iot_plc_upgrade_dest_desc_t
*upgrading_list[IOT_PLC_UPGRADE_PHASE_THREE_MAX_SAT_CNT];
uint16_t upgrading_list_idx; // upgrading list index
uint16_t dst_total_num; // total dst num
uint16_t dst_transferred_num; // total transferred num
uint16_t dest_idx; // current dest index
uint16_t query_node_timeout; // wait node response timeout, unit ms
uint16_t query_node_timeout_cnt; // node response timeout count
}iot_plc_upgrade_dest_info_t;
#endif
/* upgrade block info */
typedef struct _iot_plc_upgrade_block_info_t {
uint16_t block_idx;
uint8_t file_type;
uint16_t request_times;
}iot_plc_upgrade_block_info_t;
/* upgrade latest block info in cco/pco */
typedef struct _iot_plc_upgrade_latest_block_info_t {
iot_plc_upgrade_block_info_t
latest_block_request[IOT_PLC_UPGRADE_LATEST_BLOCK_CNT];
}iot_plc_upgrade_latest_block_info_t;
#if IOT_CLI_UPGRADE_ENABLE
typedef struct _iot_plc_upgrade_info {
#if PLC_SUPPORT_CCO_ROLE
uint8_t sta_tei_bm[PLC_TEI_MAP_BYTE_LEN];
uint8_t sta_ack_bm[PLC_TEI_MAP_BYTE_LEN];
#endif
uint16_t block_size; // block size
uint32_t fw_size; // fw size
uint16_t fw_blocks; // total blocks
uint32_t pib_size; // pib size
uint16_t pib_blocks; // total pib blocks
uint32_t cus_size; // cus size
uint16_t cus_blocks; // total cus blocks
uint16_t block_idx; // block idx waited for
uint8_t current_file_type; // current transferring file
uint8_t fw_recv_done; // 1 means done 0 means not
uint8_t pib_recv_done; // 1 means done 0 means not
uint8_t cus_recv_done; // 1 means done 0 means not
uint8_t upgrade_type;
uint32_t fw_version; // firmware version
uint32_t pib_version; // PIB version
uint32_t cus_version; // customer version
timer_id_t upgrade_timer; // timer for retry sending
uint16_t time_window; // time window for sta data request
uint16_t phase_one_broadcast_times;// phase one broadcast times
uint16_t remote_block_cnt; // blocks in one data packet
uint16_t remote_block_size; // remote block size
uint8_t level_one_pco_count; // dest num once for remote upgrade
uint32_t upgrade_state;
uint32_t upgrade_id;
uint32_t fw_checksum; // fw checksum from plc mgr
uint32_t pib_checksum; // pib checksum from plc mgr
uint32_t cus_checksum; // cus checksum from plc mgr
iot_start_src_t src; // for falsh operation
iot_start_rst_t result; // for falsh operation
uint8_t reported_percentage; // for sta latest reported percentage
uint32_t last_timer_time; // for sta last request time
uint8_t skip_timer; // need to skip next request timer
uint8_t action_times;
iot_plc_upgrade_latest_block_info_t
latest_block_info;
uint16_t request_times;
uint8_t stop_upgrade_rpt; // stop report upgrade message
/* recv req flag 1 - valid, 0 -invalid */
uint8_t recv_req_flag :1,
control_upgrade_flag :1, // cco controllable upgrade sta flag
/* reserved for further use */
rsvd :6;
#if PLC_SUPPORT_CCO_ROLE
uint8_t *file_data; // cco recv local buffer
uint32_t file_size; // local buffer size
uint8_t *fw_data; // cco fw buffer, for remote upgrade
uint8_t *pib_data; // cco pib buffer, for remote upgrade
uint8_t *cus_data; // cco cus buffer, for remote upgrade
/* bitmap for sta upgrade, the pib and fw continuous save according to bit*/
uint8_t *full_bitmap;
uint16_t dst_list_idx; // dst list index, for remote upgrade
uint32_t fw_start; // fw start position
uint32_t pib_start; // pib start position
uint32_t cus_start; // cus start position
uint8_t sta_upgrade_active; // sta in upgrade status
uint8_t broadcast_retry_times; // broadcast retry times
uint8_t broadcast_times; // broadcast retry times
iot_plc_upgrade_dest_info_t dest_info; // dest info for remote upgrade
iot_cli_upgrade_completed_handler
completed_handler; // upgrade completed handler
iot_cli_upgrade_app_handler
sg_upgrade_handler; // sg upgrade handler
uint8_t upgrade_src_mac[IOT_MAC_ADDR_LEN]; // src mac of upgrade start
uint8_t sta_mac[IOT_MAC_ADDR_LEN];
uint8_t query_node_retry_cnt; // a node resend data count
uint8_t resend_data_state :1, // query node info flag
node_response_flag :1, // node response flag
calc_time_window_flag :1, //calculate send data time window
bcast_wait_flag :1, // wait for bcast sended flag
reserve :4;
#endif
}iot_plc_upgrade_info_t;
#else /* IOT_CLI_UPGRADE_ENABLE */
typedef struct _iot_plc_upgrade_info {
#if PLC_SUPPORT_CCO_ROLE
uint8_t *file_data; // cco recv local buffer
uint16_t block_size; // block size
uint32_t file_size; // local buffer size
#endif
uint32_t upgrade_id;
}iot_plc_upgrade_info_t;
#endif /* IOT_CLI_UPGRADE_ENABLE */
typedef struct _iot_plc_upgrade_notify {
uint32_t fw_version;
}iot_plc_upgrade_notify_t;
typedef struct _iot_plc_upgrade_notify_ack {
uint16_t sta_tei;
uint8_t ack_result;
}iot_plc_upgrade_notify_ack_t;
/**
* @brief cli_upgrade_start - handle upgrade start command
* @param buffer: buffer with argument for start command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_start(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_dst_status_query - handle upgrade dst status query command
* @param buffer: buffer with argument for start command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_dst_status_query(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_dst_status_list - handle upgrade dst status list command
* @param buffer: buffer with argument for start command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_dst_status_list(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_dst_list - handle upgrade dst list command
* @param buffer: buffer with argument for start command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_dst_list(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_send_upgrade_progress - handle upgrade progress
* @param progress_value:progress value
* @param mac: sta mac
*/
void cli_remote_upgrade_send_upgrade_progress(uint8_t progress_value,
uint8_t *mac);
/**
* @brief cli_upgrade_data - handle upgrade data command
* @param buffer: buffer with argument for data command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_data(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_stop - handle upgrade stop command
* @param buffer: buffer with argument for stop command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_stop(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_stop_ack - handle upgrade stop ack command
* @param buffer: buffer with argument for stop ack command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_stop_ack(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_reset - handle upgrade reset command
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_reset(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_upgrade_reset_ack - handle upgrade reset ack command
* @param buffer: buffer with argument for reset ack command
* @param bufferlen: length of the buffer
*/
void cli_upgrade_reset_ack(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_start - sta handle upgrade start command from cco
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_start(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_data - sta receive remote upgrade data from cco
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_data(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_data_request - coo/pco handle upgrade request command
* from sta
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_data_request(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_set_stop_upgrade_rpt - sta handle upgrade stop report command
* from cco
* @param buffer: buffer with argument for stop report command
* @param bufferlen: length of the buffer
*/
void cli_set_stop_upgrade_rpt(uint8_t *buffer, uint32_t bufferlen,
uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_node_info_query - sta handle query bitmap command
* @param buffer: buffer with argument for stop report command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_node_info_query(uint8_t *buffer, uint32_t bufferlen,
uint8_t *src_mac);
#if PLC_SUPPORT_CCO_ROLE
/**
* @brief cli_remote_upgrade_find_init_dest_from_idx - find node dest in upgrade
* init status
* @param start_idx: start index.
* @retval:
* NULL - not node dest in upgrade init status.
* otherwise - dest entry ptr.
*/
iot_plc_upgrade_dest_desc_t *
cli_remote_upgrade_find_init_dest_from_idx(uint16_t start_idx);
/**
* @brief cli_remote_upgrade_multi_node_start_notify - send multi node start cmd
* @param send_type: see - IOT_PLC_MSG_TYPE_XXX.
* @param phase_flag: see - UPGRADE_START_UPGRADE_XXX.
* @retval: ERR_OK - all list upgrade node send end.
* otherwise - need send again.
*/
uint32_t cli_remote_upgrade_multi_node_start_notify(uint8_t send_type,
uint8_t phase_flag);
void cli_remote_upgrade_add_report_status(
iot_plc_upgrade_dest_desc_t *status);
/**
* @brief cli_remote_upgrade_start_ack - sta handle upgrade start command from
* cco
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_start_ack(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_result - cco handle remote upgrade result
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_result(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_node_info_rsp - cco handle ack node info from sta
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_node_info_rsp(uint8_t *buffer, uint32_t bufferlen,
uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_progress - cco handle remote upgrade progress
* @param buffer: buffer with argument for reset command
* @param bufferlen: length of the buffer
*/
void cli_remote_upgrade_progress(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_remote_upgrade_get_resend_time_window - get resend data time window
* @param node_cnt: upgrading node count
* @retval: time window
*/
uint16_t cli_remote_upgrade_get_resend_time_window(uint16_t node_cnt);
/**
* @brief cli_remote_upgrade_resend_phase - into resend phase
* @retval: resend phase node count.
*/
uint8_t cli_remote_upgrade_resend_phase();
/**
* @brief cli_remote_upgrade_start_phase_three - cco start phase three
* @retval: start upgrade node count.
*/
uint8_t cli_remote_upgrade_start_phase_three();
/**
* @brief cli_remote_upgrade_complete - cli upgrade complete
*/
void cli_remote_upgrade_complete();
/**
* @brief cli_remote_upgrade_initialize_upgrading_list - cco initialize upgrading list
*/
void cli_remote_upgrade_initialize_upgrading_list();
/**
* @brief cli_sg_upgrade_data - handle sg upgrade data
* @param buffer: upgrade data ptr
* @param bufferlen: buffer length
*/
void cli_sg_upgrade_data(uint8_t *buffer, uint32_t bufferlen, uint8_t *src_mac);
/**
* @brief cli_sg_upgrade_stop - stop sg upgrade.
*/
void cli_sg_upgrade_stop();
#endif
/**
* @brief iot_cli_upgrade_init - init the upgrade module of CLI host
*/
void iot_cli_upgrade_init();
/**
* @brief iot_cli_upgrade_deinit - deinit the upgrade module of CLI host
*/
void iot_cli_upgrade_deinit();
#if PLC_SUPPORT_CCO_ROLE
/**
* @brief iot_cli_sta_join - handle STA joining when upgrading
* @param tei: STA joining the network
*/
void iot_cli_cco_handle_sta_join(uint8_t *mac);
/**
* @brief iot_cli_coo_handle_sta_leave - handle STA leaving when upgrading
* @param sta: STA leaving the network
*/
void iot_cli_cco_handle_sta_leave(iot_plc_sta_info_t *sta);
#endif
/**
* @brief cli_remote_upgrade_start_phase2 - CCO/PCO start remote upgrade phase 2
* @param sub_node_cnt: Count of sub nodes
*/
void cli_remote_upgrade_start_phase2(uint16_t sub_node_cnt);
/**
* @brief cli_remote_upgrade_start_phase3
*/
void cli_remote_upgrade_start_phase3();
/**
* @brief iot_cli_sta_state_change - STA handle state change event
* @param prev_nw_sn: previous role before change
* @param nw_sn: previous role before change
*/
void iot_cli_sta_state_change(uint8_t prev_nw_sn, uint8_t nw_sn);
/**
* @brief iot_cli_sta_leave - STA handle leaving when upgrading
*/
void iot_cli_sta_leave();
/**
* @brief iot_cli_role_change - handle STA role change when upgrading
* @param prev_role: previous role before change
* @param cur_role: current role when change
*/
void iot_cli_role_change(uint8_t prev_role, uint8_t cur_role);
/*
* @brief iot_cli_upgrade_handle_timer_msg - handle upgrade timer msg
*/
void iot_cli_upgrade_handle_timer_msg();
/*
* @brief cli_remote_upgrade_bcast_tx_done - handle upgrade tx done msg
*/
void cli_remote_upgrade_bcast_tx_done();
#ifdef __cplusplus
}
#endif
#endif /* IOT_CLI_HOST_UPGRADE_H */