666 lines
		
	
	
		
			21 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			666 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.
 | |
| 
 | |
| ****************************************************************************/
 | |
| #include "os_mem_api.h"
 | |
| #include "os_utils_api.h"
 | |
| #include "iot_utils_api.h"
 | |
| 
 | |
| #include "iot_io_api.h"
 | |
| #include "iot_crc_api.h"
 | |
| #include "iot_pkt_api.h"
 | |
| #include "iot_module_api.h"
 | |
| #include "iot_cli_upgrade_api.h"
 | |
| #include "os_timer_api.h"
 | |
| #include "iot_upgrade_api.h"
 | |
| #include "iot_cli_upgrade_status_type_def.h"
 | |
| 
 | |
| #include "app_upg.h"
 | |
| #include "app_common.h"
 | |
| 
 | |
| #if PLC_SUPPORT_CCO_ROLE
 | |
| 
 | |
| proto_upgrd_contxt_t *upgrd_contxt;
 | |
| 
 | |
| void app_proto_upgrd_timer_callback(void)
 | |
| {
 | |
|     APP_PRINTF("[upd]Upgrade procedure Done !!\n");
 | |
|     upgrd_contxt->stage = UPGRADE_TRANS_FILE_DONE;
 | |
|     return;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_upgrd_timer_init(void)
 | |
| {
 | |
|     upgrd_contxt->mon_timer = os_create_timer(IOT_APP_DEMO_MID, false,
 | |
|         (os_timer_func_t)app_proto_upgrd_timer_callback, NULL);
 | |
|     if (0 == upgrd_contxt->mon_timer) {
 | |
|         APP_PRINTF("[ERR]Create monitor Timer Failed !!");
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| void app_proto_upgrd_update_progress(void)
 | |
| {
 | |
|     uint16_t start_indx = 0;
 | |
|     uint16_t q_cnt = 0;
 | |
|     uint16_t i;
 | |
|     iot_plc_upgrade_dst_status_query status;
 | |
|     iot_plc_upgrade_dst_status_list *status_list;
 | |
|     sta_upgrd_info_t *sta_info;
 | |
| 
 | |
|     iot_cli_upgrade_qr_dst_status(&status);
 | |
|     upgrd_contxt->total_sta_cnt = status.dst_total_num;
 | |
|     upgrd_contxt->cco_upd_state = status.upgrade_state;
 | |
|     upgrd_contxt->rpt_num = status.dst_num;
 | |
|     upgrd_contxt->fail_sta_cnt = 0;
 | |
| 
 | |
|     APP_PRINTF("Upgrade progress, total sta[%d], cco state[%d], fail sta[%d]",
 | |
|         upgrd_contxt->total_sta_cnt, upgrd_contxt->cco_upd_state, upgrd_contxt->fail_sta_cnt);
 | |
| 
 | |
|     if (upgrd_contxt->file_attr == UPGRADE_FILE_ATTR_LOCAL) {
 | |
|         APP_PRINTF("Upgrade local device, skip querry progress info");
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     if (upgrd_contxt->cco_upd_state == IOT_PLC_UPGRADE_INIT) {
 | |
|         upgrd_contxt->stage = UPGRADE_TRANS_FILE_DONE;
 | |
|         APP_PRINTF("not start upgrade, skip querry progress info");
 | |
|         return;
 | |
|     } else if (upgrd_contxt->cco_upd_state < IOT_PLC_UPGRADE_TRANSFERRED) {
 | |
|         upgrd_contxt->stage = UPGRADE_TRANS_FILE_INPROGRESS;
 | |
|         APP_PRINTF("Sending remote data, skip querry progress info");
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     status_list = (iot_plc_upgrade_dst_status_list *)upgrd_contxt->temp_data;
 | |
| 
 | |
|     do {
 | |
|         q_cnt = upgrd_contxt->rpt_num;
 | |
|         if (start_indx + q_cnt > upgrd_contxt->total_sta_cnt) {
 | |
|             q_cnt = upgrd_contxt->total_sta_cnt - start_indx;
 | |
|         }
 | |
|         os_mem_set((uint8_t *)status_list, 0, sizeof(iot_plc_upgrade_dst_status_list));
 | |
| 
 | |
|         APP_PRINTF("iot_cli_upgrade_qr_dst_status_list %d %d", start_indx, q_cnt);
 | |
|         iot_cli_upgrade_qr_dst_status_list(start_indx, q_cnt, status_list);
 | |
| 
 | |
|         APP_PRINTF("Upgrade status list,indx[%d],end[%d],dst_num[%d]",
 | |
|             status_list->dst_status_list_idx, status_list->end, status_list->dst_num);
 | |
| 
 | |
|         for(i = 0; i < status_list->dst_num; i++) {
 | |
|             if (iot_mac_addr_valid(status_list->dst_status_list[i].dst)) {
 | |
|                 sta_info = &upgrd_contxt->sta_info[status_list->dst_status_list_idx + i];
 | |
|                 iot_mac_addr_cpy(sta_info->mac, status_list->dst_status_list[i].dst);
 | |
|                 sta_info->status = status_list->dst_status_list[i].status;
 | |
|                 sta_info->progress = status_list->dst_status_list[i].percentage;
 | |
|                 if (status_list->dst_status_list[i].error_code != UPGRADE_DATA_SUCCESS) {
 | |
|                     upgrd_contxt->fail_sta_cnt++;
 | |
|                 }
 | |
|                 APP_PRINTF("Upgrade sta[%02x:%02x:%02x:%02x:%02x:%02x], status[%d], progress[%d], "
 | |
|                     "errno[%d]", sta_info->mac[0], sta_info->mac[1], sta_info->mac[2],
 | |
|                     sta_info->mac[3], sta_info->mac[4], sta_info->mac[5], sta_info->status,
 | |
|                 sta_info->progress, status_list->dst_status_list[i].error_code);
 | |
|             }
 | |
|         }
 | |
|         if (status_list->end == 1) {
 | |
|             if (q_cnt >= upgrd_contxt->total_sta_cnt) {
 | |
|                 start_indx = upgrd_contxt->total_sta_cnt;
 | |
|             }
 | |
|         }
 | |
|         start_indx += status_list->dst_num;
 | |
|     } while (start_indx < upgrd_contxt->total_sta_cnt);
 | |
| 
 | |
|     if(upgrd_contxt->fail_sta_cnt == 0) {
 | |
|         if (upgrd_contxt->stage != UPGRADE_TRANS_FILE_DONE) {
 | |
|             upgrd_contxt->stage = UPGRADE_TRANS_FILE_DONE;
 | |
|         }
 | |
|     } else {
 | |
|         upgrd_contxt->stage = UPGRADE_TRANS_FILE_UNCOMPLETE;
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* start_index should start form 0 */
 | |
| void app_proto_upgrd_update_query_progress(query_progress_ind *outbuf,
 | |
|     uint16_t start_index, uint16_t count, uint16_t *buflen)
 | |
| {
 | |
|     uint16_t q_cnt = 0;
 | |
|     uint16_t i, buf_index = 0;
 | |
|     iot_plc_upgrade_dst_status_query status;
 | |
|     iot_plc_upgrade_dst_status_list *status_list;
 | |
|     sta_upgrd_info_t *sta_info;
 | |
|     /* 这个变量保存数据读取的最高位置的编号 */
 | |
|     uint16_t max_check_pos = 0;
 | |
|     uint16_t count_max;
 | |
| 
 | |
|     iot_cli_upgrade_qr_dst_status(&status);
 | |
|     upgrd_contxt->total_sta_cnt = status.dst_total_num;
 | |
|     upgrd_contxt->cco_upd_state = status.upgrade_state;
 | |
|     upgrd_contxt->rpt_num = status.dst_num;
 | |
|     upgrd_contxt->fail_sta_cnt = 0;
 | |
| 
 | |
|     APP_PRINTF("start_index = %d, count = %d\n", start_index, count);
 | |
| 
 | |
|     APP_PRINTF("Upgrade progress query, total sta[%d], cco state[%d], rpt_num[%d]\n",
 | |
|         upgrd_contxt->total_sta_cnt, upgrd_contxt->cco_upd_state, upgrd_contxt->rpt_num);
 | |
| 
 | |
|     outbuf->start_index = start_index;
 | |
|     if (start_index > status.dst_total_num) {
 | |
|         APP_PRINTF("start_index is too large");
 | |
|         outbuf->upgrade_total_count = 0;
 | |
|         outbuf->resp_count = 0;
 | |
|         *buflen = sizeof(query_progress_ind);
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     if (start_index + count > status.dst_total_num) {
 | |
|         count = status.dst_total_num - start_index ;
 | |
|     }
 | |
| 
 | |
|     count_max = (MAX_RSP_DATA_LEN - sizeof(query_progress_ind)) / sizeof (sta_upgrd_info_t);
 | |
|     if(count > count_max) {
 | |
|         count = count_max;
 | |
|         iot_cus_printf("count is too large , only %d progress can return\n", count);
 | |
|     }
 | |
| 
 | |
|     outbuf->upgrade_total_count = status.dst_total_num;
 | |
|     max_check_pos = start_index + count;
 | |
| 
 | |
|     outbuf->resp_count = count;
 | |
|     *buflen = sizeof(query_progress_ind) + sizeof(sta_upgrd_info_t) * outbuf->resp_count;
 | |
|     status_list = (iot_plc_upgrade_dst_status_list *)upgrd_contxt->temp_data;
 | |
|     do {
 | |
|         q_cnt = upgrd_contxt->rpt_num;
 | |
|         if (start_index + q_cnt > max_check_pos) {
 | |
|             q_cnt = max_check_pos - start_index;
 | |
|         }
 | |
|         os_mem_set((uint8_t *)status_list, 0, sizeof(iot_plc_upgrade_dst_status_list));
 | |
| 
 | |
|         APP_PRINTF("iot_cli_upgrade_qr_dst_status_list %d %d", start_index, q_cnt);
 | |
|         iot_cli_upgrade_qr_dst_status_list(start_index, q_cnt, status_list);
 | |
| 
 | |
|         APP_PRINTF("Upgrade status list, indx[%d], end[%d],dst_num[%d]",
 | |
|             status_list->dst_status_list_idx, status_list->end, q_cnt);
 | |
| 
 | |
|         for (i = 0; i < q_cnt ; i++) {
 | |
|             if (iot_mac_addr_valid(status_list->dst_status_list[i].dst)) {
 | |
|                 sta_info =  &outbuf->info[i + buf_index];
 | |
|                 iot_mac_addr_cpy(sta_info->mac, status_list->dst_status_list[i].dst);
 | |
|                 sta_info->status = status_list->dst_status_list[i].status;
 | |
|                 sta_info->progress = status_list->dst_status_list[i].percentage;
 | |
| 
 | |
|                 // APP_PRINTF("Upgrade sta["MAC_FMT"], status[%d], progress[%d]",
 | |
|                 //     MAC_ARG(outbuf->info[i + buf_index].mac), outbuf->info[i + buf_index].status,
 | |
|                 //     outbuf->info[i + buf_index].progress);
 | |
|                 APP_PRINTF("Upgrade sta["MAC_FMT"], status[%d], progress[%d]",
 | |
|                     MAC_ARG(status_list->dst_status_list[i].dst), status_list->dst_status_list[i].status,
 | |
|                     status_list->dst_status_list[i].percentage);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         start_index += q_cnt;
 | |
|         buf_index += q_cnt;
 | |
|     } while (start_index < max_check_pos);
 | |
| }
 | |
| 
 | |
| uint16_t app_proto_upgrd_contxt_init(void)
 | |
| {
 | |
|     uint16_t ret = ERR_FAIL;
 | |
|     uint8_t reason = 0;
 | |
|     iot_pkt_t *tmp_pkt;
 | |
|     iot_pkt_t *tmp_data_pkt;
 | |
| 
 | |
|     tmp_pkt = iot_pkt_alloc(sizeof(proto_upgrd_contxt_t), IOT_APP_DEMO_MID);
 | |
|     if (tmp_pkt == NULL) {
 | |
|         reason = 1;
 | |
|         goto out;
 | |
|     }
 | |
|     upgrd_contxt = (proto_upgrd_contxt_t *)iot_pkt_data(tmp_pkt);
 | |
|     os_mem_set(upgrd_contxt, 0, sizeof(proto_upgrd_contxt_t));
 | |
| 
 | |
|     tmp_data_pkt = iot_pkt_alloc(sizeof(iot_plc_upgrade_dst_status_list),
 | |
|         IOT_APP_DEMO_MID);
 | |
|     if (tmp_data_pkt == NULL) {
 | |
|         reason = 2;
 | |
|         goto errno_2;
 | |
|     }
 | |
|     upgrd_contxt->temp_data = iot_pkt_data(tmp_data_pkt);
 | |
| 
 | |
|     if (ERR_OK != app_proto_upgrd_timer_init()) {
 | |
|         reason = 3;
 | |
|         goto errno_3;
 | |
|     }
 | |
|     upgrd_contxt->stage = UPGRADE_TRANS_FILE_INVALID;
 | |
|     /* get a new upgrade id if it's 0. */
 | |
|     if (upgrd_contxt->upgrade_id == 0) {
 | |
|         upgrd_contxt->upgrade_id = os_rand();
 | |
|     }
 | |
| 
 | |
|     upgrd_contxt->upgrade_id++;
 | |
|     if (upgrd_contxt->upgrade_id == 0) {
 | |
|         upgrd_contxt->upgrade_id++;
 | |
|     }
 | |
|     ret = ERR_OK;
 | |
|     APP_PRINTF("[INFO]initialize upgrade context success");
 | |
|     goto out;
 | |
| 
 | |
| errno_3:
 | |
|     iot_pkt_free(tmp_data_pkt);
 | |
| errno_2:
 | |
|     iot_pkt_free(tmp_pkt);
 | |
| out:
 | |
|     if (reason != 0) {
 | |
|         APP_PRINTF("[ERR]initialize upgrade context faild for reason[%d]", reason);
 | |
|     }
 | |
|     return ret;
 | |
| }
 | |
| 
 | |
| void app_proto_upgrd_compelet_handle(void)
 | |
| {
 | |
|     upgrd_contxt->stage = UPGRADE_TRANS_FILE_DONE;
 | |
|     APP_PRINTF("[upd]Upgrading commpleted!!!!\n");
 | |
|     return;
 | |
| }
 | |
| 
 | |
| static void app_proto_upgrd_clear_old_file(void)
 | |
| {
 | |
|     if ((upgrd_contxt->file_len != 0) && (upgrd_contxt->file_data != NULL)) {
 | |
|         os_mem_set(upgrd_contxt->file_data, 0, upgrd_contxt->file_len);
 | |
|     }
 | |
|     upgrd_contxt->cur_seg_indx = 0;
 | |
|     upgrd_contxt->stage = UPGRADE_TRANS_FILE_INVALID;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_upgrd_update_data(proto_trans_file_data_req *req)
 | |
| {
 | |
|     uint32_t ret = ERR_FAIL;
 | |
|     uint16_t crc = 0;
 | |
|     uint8_t reason = 0;
 | |
| 
 | |
|     if (req == NULL) {
 | |
|         reason = 1;
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     APP_PRINTF("[upd]data RX,type[%d],progress[%d]/[%d],size[%d] cur_seg_indx:%d",
 | |
|         upgrd_contxt->file_attr, req->seg_num + 1, upgrd_contxt->seg_total,
 | |
|         req->seg_size, upgrd_contxt->cur_seg_indx);
 | |
| 
 | |
|     if ((req->seg_num >= upgrd_contxt->seg_total) ||
 | |
|         (req->seg_num != upgrd_contxt->cur_seg_indx) ||
 | |
|         (req->seg_size == 0)) {
 | |
|         reason = 2;
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     if (req->seg_num == 0) {
 | |
|         if (upgrd_contxt->file_attr == IOT_PLC_UPGRADE_LOCAL) {
 | |
|             /* start rset */
 | |
|             iot_start_rst_t rst;
 | |
|             ret = iot_pkg_upgrade_start(&rst, upgrd_contxt->upgrade_id,
 | |
|                 req->seg_size, 0, 0, 0, 0, req->seg_data);
 | |
|             if (ret != ERR_OK) {
 | |
|                 APP_PRINTF("upgrade: start failed. result=%d", ret);
 | |
|                 reason = 3;
 | |
|                 goto out;
 | |
|             }
 | |
|         } else {
 | |
|             iot_cli_upgrade_prepare(app_proto_upgrd_compelet_handle,
 | |
|                 upgrd_contxt->file_len, &upgrd_contxt->file_data, req->seg_data,
 | |
|                 req->seg_size, upgrd_contxt->file_attr);
 | |
|         }
 | |
|         /* mark the segment size, 100, 200, 300 or 400bytes */
 | |
|         upgrd_contxt->seg_size = req->seg_size;
 | |
|     }
 | |
| 
 | |
|     if (req->seg_num + 1 != upgrd_contxt->seg_total) {
 | |
|         if (req->seg_size != upgrd_contxt->seg_size) {
 | |
|             reason = 4;
 | |
|             goto out;
 | |
|         }
 | |
|     } else {
 | |
|         if (req->seg_size != (upgrd_contxt->file_len -
 | |
|             upgrd_contxt->seg_size * upgrd_contxt->cur_seg_indx)) {
 | |
|             reason = 5;
 | |
|             goto out;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     crc = iot_getcrc16(req->seg_data, req->seg_size, IOT_CRC16_TYPE_XMODEM);
 | |
|     crc = APP_SWAP16(crc);
 | |
|     if (crc != req->seg_crc) {
 | |
|         APP_PRINTF("[upd]crc=%x %x", crc, req->seg_crc);
 | |
|         reason = 6;
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     upgrd_contxt->calc_crc32 = iot_getcrc32_update(upgrd_contxt->calc_crc32,
 | |
|         req->seg_data, req->seg_size);
 | |
|     if ((upgrd_contxt->file_attr == IOT_PLC_UPGRADE_ALL) ||
 | |
|         (upgrd_contxt->file_attr == IOT_PLC_UPGRADE_STA_LIST)) {
 | |
|         ret = iot_cli_upgrade_write(
 | |
|             upgrd_contxt->cur_seg_indx * upgrd_contxt->seg_size,
 | |
|             req->seg_data, req->seg_size);
 | |
|         if (ret != ERR_OK) {
 | |
|             reason = 7;
 | |
|             goto out;
 | |
|         }
 | |
|     } else {
 | |
|         iot_trans_cmd_t trans;
 | |
|         iot_pkt_t *trans_pkt = NULL;
 | |
|         trans.id = upgrd_contxt->upgrade_id;
 | |
|         trans.crc_flag = 0;
 | |
|         trans_pkt = (iot_pkt_t *)iot_pkt_alloc(upgrd_contxt->seg_size,
 | |
|             IOT_APP_DEMO_MID);
 | |
|         if (trans_pkt == NULL) {
 | |
|             reason = 8;
 | |
|             goto out;
 | |
|         }
 | |
|         iot_pkt_put(trans_pkt, req->seg_size);
 | |
|         os_mem_cpy(iot_pkt_data(trans_pkt), req->seg_data, req->seg_size);
 | |
|         trans.data = trans_pkt;
 | |
|         trans.block_num = req->seg_num;
 | |
|         trans.data_type = UPGRADE_DATA_TYPE_PKG;
 | |
|         trans.id = upgrd_contxt->upgrade_id;
 | |
|         trans.crc_flag = 0;
 | |
|         if (ERR_OK != (ret = iot_pkg_upgrade_trans(&trans))) {
 | |
|             reason = 9;
 | |
|             APP_PRINTF("[upd]update data failed. result=%d", ret);
 | |
|             goto out;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     upgrd_contxt->cur_seg_indx++;
 | |
|     APP_PRINTF("[upd]write data index=%d", upgrd_contxt->cur_seg_indx);
 | |
| 
 | |
|     if (upgrd_contxt->cur_seg_indx == upgrd_contxt->seg_total) {
 | |
|         upgrd_contxt->calc_crc32 ^= 0xFFFFFFFF;
 | |
|         if (upgrd_contxt->calc_crc32 != upgrd_contxt->file_crc) {
 | |
|             APP_PRINTF("[upd]file crc verif failed, calc crc:%08X file crc:%08X",
 | |
|                 upgrd_contxt->calc_crc32, upgrd_contxt->file_crc);
 | |
|             reason = 10;
 | |
|             goto out;
 | |
|         }
 | |
|         if ((upgrd_contxt->file_attr == IOT_PLC_UPGRADE_ALL) ||
 | |
|             (upgrd_contxt->file_attr == IOT_PLC_UPGRADE_STA_LIST)) {
 | |
|             APP_PRINTF("[upd]all upd data received, type[%d]",
 | |
|                 upgrd_contxt->file_attr);
 | |
|             iot_cli_upgrade_start(upgrd_contxt->file_attr, 0);
 | |
|         } else {
 | |
|             APP_PRINTF("[upd]start commiting... type[%d]",
 | |
|                 upgrd_contxt->file_attr);
 | |
|             iot_pkg_upgrade_commit(upgrd_contxt->upgrade_id);
 | |
|             iot_pkg_upgrade_reset();
 | |
|         }
 | |
|     }
 | |
|     ret = ERR_OK;
 | |
| out:
 | |
|     if (ret != ERR_OK) {
 | |
|         APP_PRINTF("[upd]update data failed for reason[%d]", reason);
 | |
|     }
 | |
|     return ret;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_upgrd_trans_file_start(const proto_start_trans_file_req *req)
 | |
| {
 | |
|     if (upgrd_contxt == NULL || req == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
|     upgrd_contxt->trans_timeout = req->trans_timeout;
 | |
|     switch(req->file_attr) {
 | |
|     case UPGRADE_FILE_ATTR_LOCAL:
 | |
|     {
 | |
|         if (upgrd_contxt->trans_timeout == 0) {
 | |
|             upgrd_contxt->trans_timeout = 10;
 | |
|         }
 | |
|         if ((upgrd_contxt->stage == UPGRADE_TRANS_FILE_INPROGRESS) ||
 | |
|             (upgrd_contxt->stage == UPGRADE_TRANS_FILE_INVALID)) {
 | |
|             iot_pkg_upgrade_cancel_commit(upgrd_contxt->upgrade_id, true);
 | |
|             upgrd_contxt->upgrade_id++;
 | |
|         }
 | |
|         break;
 | |
|     }
 | |
|     case UPGRADE_FILE_ATTR_REMOTE:
 | |
|     {
 | |
|         if (upgrd_contxt->trans_timeout == 0) {
 | |
|             upgrd_contxt->trans_timeout = 20;
 | |
|         }
 | |
|         break;
 | |
|     }
 | |
|     case UPGRADE_FILE_ATTR_LIST:
 | |
|     {
 | |
|         if (upgrd_contxt->trans_timeout == 0) {
 | |
|             upgrd_contxt->trans_timeout = 30;
 | |
|         }
 | |
|         break;
 | |
|     }
 | |
|     default:
 | |
|         APP_PRINTF("[ERR]Unsupported file attr[%d]!!", req->file_attr);
 | |
|         return ERR_INVAL;
 | |
|     }
 | |
| 
 | |
|     upgrd_contxt->file_attr = req->file_attr;
 | |
|     upgrd_contxt->file_len = req->file_len;
 | |
|     upgrd_contxt->file_crc = req->file_crc;
 | |
|     upgrd_contxt->seg_total = req->seg_total;
 | |
|     upgrd_contxt->calc_crc32 = 0xFFFFFFFF;
 | |
| 
 | |
|     if (os_is_timer_active(upgrd_contxt->mon_timer)) {
 | |
|         os_stop_timer(upgrd_contxt->mon_timer);
 | |
|     }
 | |
|     os_start_timer(upgrd_contxt->mon_timer,
 | |
|         upgrd_contxt->trans_timeout * ONE_MINUTES);
 | |
|     upgrd_contxt->stage = UPGRADE_TRANS_FILE_INPROGRESS;
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| uint32_t app_proto_handle_trans_file_attr(const proto_start_trans_file_req *req)
 | |
| {
 | |
|     uint32_t ret = ERR_OK;
 | |
| 
 | |
|     if (req == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     APP_PRINTF("[upd]start trans file, file attr[%d], total seg[%d]"
 | |
|         " file len[%d], timeout[%d]", req->file_attr, req->seg_total,
 | |
|         req->file_len, req->trans_timeout);
 | |
| 
 | |
|     switch (req->file_attr) {
 | |
|     case UPGRADE_FILE_ATTR_CLEAR:
 | |
|     {
 | |
|         app_proto_upgrd_clear_old_file();
 | |
|         break;
 | |
|     }
 | |
|     case UPGRADE_FILE_ATTR_LOCAL:
 | |
|     case UPGRADE_FILE_ATTR_REMOTE:
 | |
|     case UPGRADE_FILE_ATTR_LIST:
 | |
|     {
 | |
|         ret = app_proto_upgrd_trans_file_start(req);
 | |
|         break;
 | |
|     }
 | |
|     default:
 | |
|         ret = ERR_FAIL;
 | |
|         break;
 | |
|     }
 | |
| 
 | |
|     return ret;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_start_trans_file(const proto_start_trans_file_req *req, uint8_t *out_buffer, uint16_t *length)
 | |
| {
 | |
|     uint32_t ret = ERR_FAIL;
 | |
|     upgrd_resp_t *resp = NULL;
 | |
|     uint16_t payload_length = sizeof(upgrd_resp_t);
 | |
| 
 | |
|     if (req == NULL || out_buffer == NULL || length == NULL) {
 | |
|         APP_PRINTF("cmd arg with null");
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     resp = (upgrd_resp_t *)(out_buffer + UPGRADE_DATA_HEAD_LEN);
 | |
|     resp->state = PROTO_RET_CODE_FAILED;
 | |
|     resp->reason = PROTO_REASON_FORMAT;
 | |
| 
 | |
|     ret = app_proto_handle_trans_file_attr(req);
 | |
|     if (ret == ERR_OK) {
 | |
|         resp->state = PROTO_RET_CODE_SUCCESS;
 | |
|         resp->reason = PROTO_REASON_NORMAL;
 | |
|     }
 | |
| 
 | |
|     *length = payload_length;
 | |
| 
 | |
|     APP_PRINTF("[upd]trans_file file_attr=%d.seg_total=%d",
 | |
|         upgrd_contxt->file_attr, upgrd_contxt->seg_total);
 | |
| 
 | |
|     return ret;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_trans_file_data(proto_trans_file_data_req *req,
 | |
|     uint8_t *out_buffer, uint16_t *length)
 | |
| {
 | |
|     uint32_t ret;
 | |
|     upgrd_resp_t *ind = NULL;
 | |
|     uint16_t payload_length = sizeof(upgrd_resp_t);
 | |
| 
 | |
|     if (out_buffer == NULL || length == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     APP_PRINTF("[upd]start handle data....");
 | |
|     APP_PRINTF("[upd]file_attr=%d.seg_total=%d",
 | |
|         upgrd_contxt->file_attr, upgrd_contxt->seg_total);
 | |
| 
 | |
|     ind = (upgrd_resp_t *)(out_buffer + UPGRADE_DATA_HEAD_LEN);
 | |
|     ind->state = PROTO_RET_CODE_FAILED;
 | |
|     ind->reason = PROTO_REASON_OTHER;
 | |
| 
 | |
|     ret = app_proto_upgrd_update_data(req);
 | |
|     APP_PRINTF("[upd]app_proto_upgrd_update_data ret[%d]", ret);
 | |
|     if (ret == ERR_OK) {
 | |
|         ind->state = PROTO_RET_CODE_SUCCESS;
 | |
|         ind->reason = PROTO_REASON_NORMAL;
 | |
|     } else {
 | |
|         ind->reason = PROTO_REASON_FORMAT;
 | |
|     }
 | |
| 
 | |
|     *length = payload_length;
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_trans_file_query(uint8_t *out_buffer, uint16_t *length)
 | |
| {
 | |
|     upgrade_state_resp_t *ind = NULL;
 | |
|     uint16_t payload_length = sizeof(upgrade_state_resp_t);
 | |
| 
 | |
|     if (out_buffer == NULL || length == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     ind = (upgrade_state_resp_t *)(out_buffer + UPGRADE_DATA_HEAD_LEN);
 | |
| 
 | |
|     app_proto_upgrd_update_progress();
 | |
| 
 | |
|     ind->state = upgrd_contxt->stage;
 | |
|     ind->fail_sta = upgrd_contxt->fail_sta_cnt;
 | |
| 
 | |
|     *length = payload_length;
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_set_upgrd_list(set_upgrd_list_t *req, uint8_t *out_buffer,
 | |
|     uint16_t *length, uint8_t renew)
 | |
| {
 | |
|     uint32_t ret = ERR_FAIL;
 | |
|     upgrd_resp_t *ind = NULL;
 | |
|     iot_plc_upgrade_dst_list_t *list;
 | |
|     uint8_t i = 0;
 | |
|     uint16_t payload_length = sizeof(upgrd_resp_t);
 | |
| 
 | |
|     if (out_buffer == NULL || length == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     list = (iot_plc_upgrade_dst_list_t *)upgrd_contxt->temp_data;
 | |
| 
 | |
|     ind = (upgrd_resp_t *)(out_buffer + UPGRADE_DATA_HEAD_LEN);
 | |
|     ind->state = PROTO_RET_CODE_FAILED;
 | |
|     ind->reason = PROTO_REASON_FORMAT;
 | |
| 
 | |
|     APP_PRINTF("[upd]set upgrade list count[%d]", req->count);
 | |
| 
 | |
|     os_mem_set(list, 0, sizeof(*list));
 | |
|     list->dst_num = req->count;
 | |
|     if (renew) {
 | |
|         upgrd_contxt->upgrd_list_indx = 0;
 | |
|     }
 | |
|     list->dst_list_idx = upgrd_contxt->upgrd_list_indx;
 | |
| 
 | |
|     if (list->dst_list_idx + 1 > UPGRADE_REMOTE_ADDR_NUM) {
 | |
|         ret = ERR_FAIL;
 | |
|         goto out;
 | |
|     }
 | |
| 
 | |
|     for (i = 0; i < req->count; i++) {
 | |
|         iot_mac_addr_cpy(&list->dst_list[i * IOT_MAC_ADDR_LEN],
 | |
|             &req->mac[i * IOT_MAC_ADDR_LEN]);
 | |
|         APP_PRINTF("[upd]add mac["MAC_FMT"] to list",
 | |
|             MAC_ARG(req->mac + i * IOT_MAC_ADDR_LEN));
 | |
|         upgrd_contxt->upgrd_list_indx++;
 | |
|     }
 | |
| 
 | |
|     ret = iot_cli_upgrade_set_dst_list(list, renew);
 | |
| 
 | |
|     if (ret == ERR_OK) {
 | |
|         ind->state = PROTO_RET_CODE_SUCCESS;
 | |
|         ind->reason = PROTO_REASON_NORMAL;
 | |
|     } else {
 | |
|         ind->reason = PROTO_REASON_OTHER;
 | |
|     }
 | |
| 
 | |
| out:
 | |
| 
 | |
|     *length = payload_length;
 | |
| 
 | |
|     return ret;
 | |
| }
 | |
| 
 | |
| uint32_t app_proto_trans_file_query_progress(query_progress_req *req, uint8_t *out_buffer, uint16_t *length)
 | |
| {
 | |
|     query_progress_ind *ind = NULL;
 | |
|     uint16_t req_lenth = 0;
 | |
| 
 | |
|     if (out_buffer == NULL || length == NULL) {
 | |
|         return ERR_FAIL;
 | |
|     }
 | |
| 
 | |
|     ind = (query_progress_ind *)(out_buffer + UPGRADE_DATA_HEAD_LEN);
 | |
|     app_proto_upgrd_update_query_progress(ind, req->start_index -1, req->count , &req_lenth);
 | |
| 
 | |
|     *length = req_lenth;
 | |
|     APP_PRINT_BUF("query progress ", out_buffer, MAX_RSP_DATA_LEN);
 | |
| 
 | |
|     return ERR_OK;
 | |
| }
 | |
| 
 | |
| #elif PLC_SUPPORT_STA_ROLE
 | |
| 
 | |
| uint16_t app_proto_upgrd_contxt_init(void)
 | |
| {
 | |
|     return true;
 | |
| }
 | |
| 
 | |
| #endif
 |