716 lines
		
	
	
		
			23 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			716 lines
		
	
	
		
			23 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.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
 | 
						|
/* smart grid internal header files */
 | 
						|
#include "proto_645_fj.h"
 | 
						|
#include "iot_oem_api.h"
 | 
						|
#include "iot_errno_api.h"
 | 
						|
#include "iot_version_api.h"
 | 
						|
#include "iot_sg_drv_api.h"
 | 
						|
 | 
						|
/* iot common header files */
 | 
						|
#include "iot_io_api.h"
 | 
						|
 | 
						|
#if (IOT_SMART_GRID_ENABLE)
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_mr_msg(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t len, uint32_t di, uint8_t fn)
 | 
						|
{
 | 
						|
    return proto_645_build_msg(addr, data, len, di, PROTO_645_DIR_MASTER,
 | 
						|
        PROTO_645_ACK_NORMAL, fn, PROTO_645_2007_ID, PROTO_645_FOLLOW_INVALID);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_msg(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t len, uint32_t di, uint8_t fn)
 | 
						|
{
 | 
						|
    return proto_645_build_msg(addr, data, len, di, PROTO_645_DIR_SLAVE,
 | 
						|
        PROTO_645_ACK_NORMAL, fn, PROTO_645_2007_ID, PROTO_645_FOLLOW_INVALID);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_muilt_mr_msg(uint8_t *addr, uint32_t *di_list,
 | 
						|
    uint8_t di_cnt)
 | 
						|
{
 | 
						|
    iot_pkt_t *buff = NULL, *pkt = NULL;
 | 
						|
    uint8_t buff_len, separatorm_len, i, *buff_data;
 | 
						|
    uint32_t di;
 | 
						|
 | 
						|
    if (!di_cnt || di_cnt > PROTO_645_FJ_METER_MULTI_DI_CNT_MAX) {
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    separatorm_len = di_cnt - 1;
 | 
						|
    buff_len = di_cnt * PROTO_645_2007_DI_LEN + separatorm_len;
 | 
						|
    buff = iot_pkt_alloc(buff_len, IOT_SMART_GRID_MID);
 | 
						|
    if (!buff) {
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    buff_data = iot_pkt_put(buff, buff_len);
 | 
						|
    for (i = 0; i < di_cnt; i++) {
 | 
						|
        os_mem_cpy(buff_data, di_list + i, PROTO_645_2007_DI_LEN);
 | 
						|
        buff_data += PROTO_645_2007_DI_LEN;
 | 
						|
        if (i < di_cnt - 1) {
 | 
						|
            /* fill separator */
 | 
						|
            *buff_data = 0xbb;
 | 
						|
            buff_data += 1;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    di = PROTO_645_FJ_METER_MULTI_DI_READ + di_cnt;
 | 
						|
    pkt =  proto_645_build_msg(addr, iot_pkt_data(buff), buff_len, di,
 | 
						|
        PROTO_645_DIR_MASTER, PROTO_645_ACK_NORMAL, PROTO_645_2007_FN_READ_DATA,
 | 
						|
        PROTO_645_2007_ID, PROTO_645_FOLLOW_INVALID);
 | 
						|
out:
 | 
						|
    if (buff) {
 | 
						|
        iot_pkt_free(buff);
 | 
						|
    }
 | 
						|
    return pkt;
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_chip_id(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_CHIP_ID, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_mod_id(uint8_t *addr,
 | 
						|
    uint8_t *data, uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_MOD_ID, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_mod_ver(uint8_t *addr,
 | 
						|
    uint8_t *data, uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_MOD_VER, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_opposite_phase_info(uint8_t *addr, uint8_t fn,
 | 
						|
    uint8_t opposite)
 | 
						|
{
 | 
						|
    proto_645_fj_opposite_phase_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.opposite_phase = opposite;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_OPPOSITE_PHASE, fn);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_phase_info(uint8_t *addr, uint8_t phase)
 | 
						|
{
 | 
						|
    proto_645_fj_phase_info_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.phase = phase;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_QR_PHASE_INFO, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_tsfm_enable_resp(uint8_t *addr,
 | 
						|
    uint8_t tsfm_detect_way, uint16_t timeout)
 | 
						|
{
 | 
						|
    proto_645_fj_tsfm_enable_t tsfm_enable = { 0 };
 | 
						|
 | 
						|
    tsfm_enable.tsfm_detect_way = tsfm_detect_way;
 | 
						|
    tsfm_enable.timeout = timeout;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&tsfm_enable,
 | 
						|
        sizeof(tsfm_enable), PROTO_645_FJ_DI_TSFM_DETECT_START,
 | 
						|
        PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_node_reg_enable_resp(uint8_t *addr,
 | 
						|
    uint16_t timeout)
 | 
						|
{
 | 
						|
    proto_645_fj_node_reg_enable_t reg_enable = { 0 };
 | 
						|
 | 
						|
    reg_enable.timeout = timeout;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)®_enable,
 | 
						|
        sizeof(reg_enable), PROTO_645_FJ_DI_NODE_REG_START,
 | 
						|
        PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_topo_resp(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len, PROTO_645_FJ_DI_QR_TOPO,
 | 
						|
        PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_diff_tsfm_node(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_DIFF_TSFM_NODE, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_not_reg_node(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_NOT_REG_NODE, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_nb_nw_info_resp(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_NB_NW_INFO, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_freq_band_resp(uint8_t *addr,
 | 
						|
    uint8_t freq_band)
 | 
						|
{
 | 
						|
    proto_645_fj_query_freq_band_t rsp;
 | 
						|
 | 
						|
    rsp.freq_band = freq_band;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rsp, sizeof(rsp),
 | 
						|
        PROTO_645_FJ_DI_QR_FREQ_BAND, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_chip_id_f112_resp(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_CHIP_ID_F112, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_wl_resp(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_OP_WL, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_upgrade_send_file_ack(uint8_t *addr,
 | 
						|
    uint32_t segment_index)
 | 
						|
{
 | 
						|
    proto_645_fj_send_file_ul_t rsp;
 | 
						|
 | 
						|
    rsp.segment_index = segment_index;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rsp, sizeof(rsp),
 | 
						|
        PROTO_645_FJ_DI_UPGRADE_SEND_FILE, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_upgrade_query_file_ack(uint8_t *addr,
 | 
						|
    uint16_t array_index, uint8_t *bitmap)
 | 
						|
{
 | 
						|
    proto_645_fj_query_file_ul_t rsp;
 | 
						|
 | 
						|
    rsp.array_index = array_index;
 | 
						|
    os_mem_cpy(rsp.array_bitmap, bitmap, PROTO_645_FJ_FILE_SEGMENT_ARRAY_LEN);
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rsp, sizeof(rsp),
 | 
						|
        PROTO_645_FJ_DI_UPGRADE_REQ_FILE, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_major_node_mac(uint8_t *addr)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, addr, IOT_MAC_ADDR_LEN,
 | 
						|
        PROTO_645_FJ_DI_QR_MAJOR_NODE_MAC, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_list_abnormal_node_rpt(uint8_t *addr,
 | 
						|
    uint8_t err_type, uint8_t *tsfm_addr)
 | 
						|
{
 | 
						|
    proto_645_fj_list_abnormal_node_rpt_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.err_type = err_type;
 | 
						|
    rpt_info.success_rate = 100;
 | 
						|
    iot_mac_addr_cpy(rpt_info.tsfm_addr, tsfm_addr);
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_TSFM_ERR_NODE_RPT, PROTO_645_FJ_FN_ACTIVE_RPT);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_tsfm_detect_result(uint8_t *addr, uint8_t fn,
 | 
						|
    uint16_t used_time, uint16_t same_tsfm_cnt, uint16_t diff_tsfm_cnt)
 | 
						|
{
 | 
						|
    proto_645_fj_tsfm_result_rpt_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.used_time = used_time;
 | 
						|
    rpt_info.same_tsfm_cnt = same_tsfm_cnt;
 | 
						|
    rpt_info.diff_tsfm_cnt = diff_tsfm_cnt;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_TSFM_RESULT_RPT, fn);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_power_evt_rpt(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_POWER_EVT_RPT, PROTO_645_FJ_FN_ACTIVE_RPT);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_module_reset_cnt(uint8_t *addr,
 | 
						|
    uint16_t reset_cnt)
 | 
						|
{
 | 
						|
    proto_645_fj_module_reset_cnt_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.reset_cnt = reset_cnt;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_QR_MODULE_RESET_CNT, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_module_pd_cnt(uint8_t *addr, uint16_t pd_cnt)
 | 
						|
{
 | 
						|
    proto_645_fj_pd_cnt_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.pd_cnt = pd_cnt;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_QR_PD_CNT, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_pd_valid_time(uint8_t *addr, uint8_t valid_time)
 | 
						|
{
 | 
						|
    proto_645_fj_pd_valid_time_t rpt_info = { 0 };
 | 
						|
 | 
						|
    rpt_info.pd_valid_time = valid_time;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt_info, sizeof(rpt_info),
 | 
						|
        PROTO_645_FJ_DI_PD_EVT_VALID_TIME, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_work_power_rpt(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_MOD_WORK_V, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_multi_di_rpt(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_MULTI_DI_READ, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_ctrl_evt_rpt(uint8_t *addr,
 | 
						|
    uint8_t connect_state)
 | 
						|
{
 | 
						|
    proto_645_fj_ctrl_connect_evt_rpt_t ctrl_rpt;
 | 
						|
 | 
						|
    if (connect_state) {
 | 
						|
        ctrl_rpt.state = PROTO_645_FJ_CTRL_STATE_CONNECTED;
 | 
						|
    } else {
 | 
						|
        ctrl_rpt.state = PROTO_645_FJ_CTRL_STATE_DISCONNECTED;
 | 
						|
    }
 | 
						|
    iot_mac_addr_cpy(ctrl_rpt.ctrl_mac, addr);
 | 
						|
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&ctrl_rpt, sizeof(ctrl_rpt),
 | 
						|
        PROTO_645_FJ_DI_CTRL_CONNECT_RPT, PROTO_645_FJ_FN_ACTIVE_RPT);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_tm_soc(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_MOD_TM_SOC, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_ct_rec(uint8_t *addr,
 | 
						|
    uint8_t *data, uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_MOD_LAST_CT_REC, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_ct_dur(uint8_t *addr,
 | 
						|
    uint8_t *data, uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_MOD_CT_DUR, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_query_mod_tm(uint8_t *addr,
 | 
						|
    uint8_t *data, uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_MOD_TM, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_evt_rpt_cfg(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_EVT_RPT_CFG, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_event_pin_report(uint8_t *addr, uint8_t state)
 | 
						|
{
 | 
						|
    proto_645_fj_evt_rpt_t rpt = { 0 };
 | 
						|
    rpt.state = state;
 | 
						|
    return proto_645_fj_build_msg(addr, (uint8_t *)&rpt, sizeof(rpt),
 | 
						|
        PROTO_645_FJ_DI_EVT_RPT, PROTO_645_FJ_FN_ACTIVE_RPT);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_rst_cnt(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_MODULE_RESET_CNT, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
iot_pkt_t *proto_645_fj_build_qr_pd_cnt(uint8_t *addr, uint8_t *data,
 | 
						|
    uint8_t data_len)
 | 
						|
{
 | 
						|
    return proto_645_fj_build_msg(addr, data, data_len,
 | 
						|
        PROTO_645_FJ_DI_QR_PD_CNT, PROTO_645_2007_FN_READ_DATA);
 | 
						|
}
 | 
						|
 | 
						|
uint32_t proto_645_fj_task_cycle_to_sec(proto_645_fj_task_cycle_t *cycle)
 | 
						|
{
 | 
						|
    uint32_t cycle_time = 0;
 | 
						|
 | 
						|
    switch (cycle->unit) {
 | 
						|
    case PROTO_645_FJ_TASK_CYCLE_UNIT_TYPE_SEC:
 | 
						|
    {
 | 
						|
        if (cycle->value == 2 || cycle->value == 3 || cycle->value == 4 ||
 | 
						|
            cycle->value == 5 || cycle->value == 10 || cycle->value == 15 ||
 | 
						|
            cycle->value == 20 || cycle->value == 30 || cycle->value == 60) {
 | 
						|
            cycle_time = cycle->value;
 | 
						|
        }
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_CYCLE_UNIT_TYPE_MIN:
 | 
						|
    {
 | 
						|
        if (cycle->value == 1 || cycle->value == 2 || cycle->value == 3
 | 
						|
            || cycle->value == 4 || cycle->value == 5 || cycle->value == 10
 | 
						|
            || cycle->value == 15 || cycle->value == 20 || cycle->value == 30
 | 
						|
            || cycle->value == 60) {
 | 
						|
            cycle_time = cycle->value * 60;
 | 
						|
        }
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_CYCLE_UNIT_TYPE_HOUR:
 | 
						|
    {
 | 
						|
        if (cycle->value == 1 || cycle->value == 2 || cycle->value == 3
 | 
						|
            || cycle->value == 4 || cycle->value == 6 || cycle->value == 8
 | 
						|
            || cycle->value == 12 || cycle->value == 24) {
 | 
						|
            cycle_time = cycle->value * 60 * 60;
 | 
						|
        }
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_CYCLE_UNIT_TYPE_DAY:
 | 
						|
    {
 | 
						|
        if (cycle->value == 1) {
 | 
						|
            cycle_time = cycle->value * 60 * 60 * 24;
 | 
						|
        }
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    default:
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    return cycle_time;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t proto_645_fj_task_time_domain_to_sec(
 | 
						|
    proto_645_fj_task_time_domain_t* domain, uint32_t* sec)
 | 
						|
{
 | 
						|
    uint32_t ret = ERR_FAIL;
 | 
						|
    static const uint8_t unit_list[2][11] = {
 | 
						|
        {0, 1, 2, 3, 4, 5, 10, 15, 20, 30, 60},
 | 
						|
        {0, 1, 2, 3, 4, 6, 8, 12, 24} };
 | 
						|
 | 
						|
    if (!domain) {
 | 
						|
        return ret;
 | 
						|
    }
 | 
						|
    for (int i = 0; i < sizeof(unit_list[domain->unit]); i++) {
 | 
						|
        if (unit_list[domain->unit][i] == domain->value) {
 | 
						|
            ret = ERR_OK;
 | 
						|
            if (sec) {
 | 
						|
                *sec = (uint32_t)domain->value * (domain->unit ? 60 * 60 : 60);
 | 
						|
            }
 | 
						|
            break;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t proto_645_fj_task_info_desc_check(uint8_t task_type, uint8_t *desc,
 | 
						|
    uint8_t desc_len, uint8_t group)
 | 
						|
{
 | 
						|
    uint8_t reason = 0, *data, len, i;
 | 
						|
    uint32_t ret = ERR_FAIL;
 | 
						|
    proto_645_fj_task_set_freeze_hdr_t *freeze_hdr;
 | 
						|
    proto_645_fj_freeze_data_desc_hdr_t *freeze_des_hdr;
 | 
						|
    proto_645_fj_task_set_incon_rpt_hdr_t *incon_hdr;
 | 
						|
    proto_645_fj_task_set_flw_rpt_hdr_t *flw_tail;
 | 
						|
    proto_645_fj_task_set_cross_rpt_t *cross_hdr;
 | 
						|
    proto_645_fj_task_set_change_rpt_hdr_t *change_hdr;
 | 
						|
    proto_645_fj_task_time_domain_t* time_domain;
 | 
						|
 | 
						|
    switch (task_type) {
 | 
						|
    case PROTO_645_FJ_TASK_TYPE_FREEZE:
 | 
						|
    {
 | 
						|
        if (desc_len < sizeof(*freeze_hdr)) {
 | 
						|
            reason = 1;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        freeze_hdr = (proto_645_fj_task_set_freeze_hdr_t*)desc;
 | 
						|
        if ((freeze_hdr->data_cnt > PROTO_645_FJ_TASK_DESC_FREEZE_MAX_CNT)
 | 
						|
            || !freeze_hdr->data_cnt) {
 | 
						|
            reason = 2;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        data = freeze_hdr->data_desc;
 | 
						|
        len = desc_len - sizeof(*freeze_hdr);
 | 
						|
        for (i = 0; i < freeze_hdr->data_cnt; i++) {
 | 
						|
            if (len < sizeof(*freeze_des_hdr)) {
 | 
						|
                reason = 3;
 | 
						|
                goto out;
 | 
						|
            }
 | 
						|
            freeze_des_hdr = (proto_645_fj_freeze_data_desc_hdr_t*)data;
 | 
						|
            switch (freeze_des_hdr->format_type) {
 | 
						|
            case PROTO_645_FJ_FREEZE_DATA_FORMAT_NO_TS:
 | 
						|
            {
 | 
						|
                if (group == PROTO_645_FJ_TASK_GROUP_698 &&
 | 
						|
                    freeze_des_hdr->data_len != PROTO_645_2007_DI_LEN) {
 | 
						|
                    reason = 4;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                if ((freeze_des_hdr->data_len < PROTO_645_2007_DI_LEN)
 | 
						|
                    || (freeze_des_hdr->data_len >
 | 
						|
                    PROTO_645_FJ_TASK_DESC_FREEZE_MAX_LEN)) {
 | 
						|
                    reason = 5;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                break;
 | 
						|
            }
 | 
						|
            case PROTO_645_FJ_FREEZE_DATA_FORMAT_WITH_TS:
 | 
						|
            {
 | 
						|
                if (group == PROTO_645_FJ_TASK_GROUP_698) {
 | 
						|
                    reason = 6;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                if ((freeze_des_hdr->data_len < (PROTO_645_2007_DI_LEN +
 | 
						|
                    sizeof(proto_645_fj_freeze_data_desc_tail_t)))
 | 
						|
                    || (freeze_des_hdr->data_len >
 | 
						|
                    PROTO_645_FJ_TASK_DESC_FREEZE_MAX_LEN)) {
 | 
						|
                    reason = 7;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                break;
 | 
						|
            }
 | 
						|
            case PROTO_645_FJ_FREEZE_DATA_FORMAT_WITH_TSR:
 | 
						|
            {
 | 
						|
                if (group == PROTO_645_FJ_TASK_GROUP_698) {
 | 
						|
                    reason = 8;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                if ((freeze_des_hdr->data_len < (PROTO_645_2007_DI_LEN +
 | 
						|
                    sizeof(proto_645_fj_freeze_data_desc_tail_t) +
 | 
						|
                    sizeof(proto_645_fj_task_time_domain_t)))
 | 
						|
                   || (freeze_des_hdr->data_len >
 | 
						|
                       PROTO_645_FJ_TASK_DESC_FREEZE_MAX_LEN)) {
 | 
						|
                    reason = 9;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                time_domain = (proto_645_fj_task_time_domain_t*)
 | 
						|
                    (freeze_des_hdr->data +
 | 
						|
                    (freeze_des_hdr->data_len - sizeof(*time_domain)));
 | 
						|
                if (proto_645_fj_task_time_domain_to_sec(time_domain, NULL)) {
 | 
						|
                    reason = 10;
 | 
						|
                    goto out;
 | 
						|
                }
 | 
						|
                break;
 | 
						|
            }
 | 
						|
            default:
 | 
						|
                reason = 11;
 | 
						|
                goto out;
 | 
						|
            }
 | 
						|
            if (len < (sizeof(*freeze_des_hdr) + freeze_des_hdr->data_len)) {
 | 
						|
                reason = 12;
 | 
						|
                goto out;
 | 
						|
            }
 | 
						|
            len = len - sizeof(*freeze_des_hdr) - freeze_des_hdr->data_len;
 | 
						|
            data = data + sizeof(*freeze_des_hdr) + freeze_des_hdr->data_len;
 | 
						|
        }
 | 
						|
        ret = ERR_OK;
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_TYPE_INCON_RPT:
 | 
						|
    {
 | 
						|
        if (desc_len < sizeof(*incon_hdr)) {
 | 
						|
            reason = 13;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        incon_hdr = (proto_645_fj_task_set_incon_rpt_hdr_t*)desc;
 | 
						|
        if (!incon_hdr->triger_cnt) {
 | 
						|
            reason = 14;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        if (!incon_hdr->di_cnt) {
 | 
						|
            reason = 15;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        data = (uint8_t*)incon_hdr->di;
 | 
						|
        len = desc_len - sizeof(*incon_hdr);
 | 
						|
        if (len < (incon_hdr->di_cnt * PROTO_645_2007_DI_LEN)) {
 | 
						|
            reason = 16;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - incon_hdr->di_cnt * PROTO_645_2007_DI_LEN;
 | 
						|
        data = data + incon_hdr->di_cnt * PROTO_645_2007_DI_LEN;
 | 
						|
        if (len < sizeof(*flw_tail)) {
 | 
						|
            reason = 17;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - sizeof(*flw_tail);
 | 
						|
        flw_tail = (proto_645_fj_task_set_flw_rpt_hdr_t*)data;
 | 
						|
        if ((flw_tail->follow_di_cnt * PROTO_645_2007_DI_LEN) > len) {
 | 
						|
            reason = 18;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        ret = ERR_OK;
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_TYPE_CROSS_RPT:
 | 
						|
    {
 | 
						|
        if (desc_len < sizeof(*cross_hdr)) {
 | 
						|
            reason = 19;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        cross_hdr = (proto_645_fj_task_set_cross_rpt_t*)desc;
 | 
						|
        if (!cross_hdr->triger_cnt) {
 | 
						|
            reason = 20;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        if (cross_hdr->value_len > PROTO_645_FJ_TASK_DESC_CROSS_VALUE_MAX_LEN
 | 
						|
            || !cross_hdr->value_len) {
 | 
						|
            reason = 21;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        if (cross_hdr->cnt < PROTO_645_FJ_TASK_DESC_CROSS_VALUE_MIN_CNT
 | 
						|
            || cross_hdr->cnt > PROTO_645_FJ_TASK_DESC_CROSS_VALUE_MAX_CNT) {
 | 
						|
            reason = 22;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        data = cross_hdr->value_list;
 | 
						|
        len = desc_len - sizeof(*cross_hdr);
 | 
						|
        if (len < cross_hdr->value_len * cross_hdr->cnt) {
 | 
						|
            reason = 23;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - (cross_hdr->value_len * cross_hdr->cnt);
 | 
						|
        data = data + (cross_hdr->value_len * cross_hdr->cnt);
 | 
						|
        if (len < sizeof(*flw_tail)) {
 | 
						|
            reason = 24;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - sizeof(*flw_tail);
 | 
						|
        flw_tail = (proto_645_fj_task_set_flw_rpt_hdr_t*)data;
 | 
						|
        if ((flw_tail->follow_di_cnt * PROTO_645_2007_DI_LEN) > len) {
 | 
						|
            reason = 25;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        ret = ERR_OK;
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    case PROTO_645_FJ_TASK_TYPE_CHG_RPT:
 | 
						|
    {
 | 
						|
        if (desc_len < sizeof(*change_hdr)) {
 | 
						|
            reason = 26;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        change_hdr = (proto_645_fj_task_set_change_rpt_hdr_t*)desc;
 | 
						|
        if (!change_hdr->triger_cnt) {
 | 
						|
            reason = 27;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        if (change_hdr->value_len > PROTO_645_FJ_TASK_DESC_CHANGE_VALUE_MAX_LEN
 | 
						|
            || !change_hdr->value_len) {
 | 
						|
            reason = 28;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        data = change_hdr->value_list;
 | 
						|
        len = desc_len - sizeof(*change_hdr);
 | 
						|
        if (len < change_hdr->value_len) {
 | 
						|
            reason = 29;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - change_hdr->value_len;
 | 
						|
        data = data + change_hdr->value_len;
 | 
						|
        if (len < sizeof(*flw_tail)) {
 | 
						|
            reason = 30;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        len = len - sizeof(*flw_tail);
 | 
						|
        flw_tail = (proto_645_fj_task_set_flw_rpt_hdr_t*)data;
 | 
						|
        if ((flw_tail->follow_di_cnt * PROTO_645_2007_DI_LEN) > len) {
 | 
						|
            reason = 31;
 | 
						|
            goto out;
 | 
						|
        }
 | 
						|
        ret = ERR_OK;
 | 
						|
        break;
 | 
						|
    }
 | 
						|
    default:
 | 
						|
        break;
 | 
						|
    }
 | 
						|
out:
 | 
						|
    if (ret) {
 | 
						|
        iot_printf("%s fail, reason %lu\n", __FUNCTION__, reason);
 | 
						|
    }
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
uint32_t proto_645_fj_task_set_msg_check(proto_645_fj_task_set_hdr_t *set,
 | 
						|
    uint8_t len, uint8_t group)
 | 
						|
{
 | 
						|
    uint8_t reason = 0, desc_len;
 | 
						|
    uint32_t ret = ERR_FAIL, cycle_sec;
 | 
						|
 | 
						|
    if (!set || !len) {
 | 
						|
        reason = 1;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    if (set->type != PROTO_645_FJ_TASK_TYPE_FREEZE
 | 
						|
        && set->type != PROTO_645_FJ_TASK_TYPE_CROSS_RPT
 | 
						|
        && set->type != PROTO_645_FJ_TASK_TYPE_INCON_RPT
 | 
						|
        && set->type != PROTO_645_FJ_TASK_TYPE_CHG_RPT) {
 | 
						|
        reason = 2;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    cycle_sec = proto_645_fj_task_cycle_to_sec(&set->cycle);
 | 
						|
    if (cycle_sec == 0) {
 | 
						|
        reason = 3;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    if (set->delay >= cycle_sec) {
 | 
						|
        reason = 4;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    if (!set->save_cnt) {
 | 
						|
        reason = 5;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    if (!set->exe_cnt) {
 | 
						|
        reason = 6;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    desc_len = len - sizeof(*set);
 | 
						|
    if (desc_len > PROTO_645_FJ_FREEZE_TASK_CFG_MAX_LEN) {
 | 
						|
        reason = 7;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    if (proto_645_fj_task_info_desc_check(set->type, set->task_info_desc,
 | 
						|
        desc_len, group)) {
 | 
						|
        reason = 8;
 | 
						|
        goto out;
 | 
						|
    }
 | 
						|
    ret = ERR_OK;
 | 
						|
out:
 | 
						|
    if (ret) {
 | 
						|
        iot_printf("%s fail, reason %lu\n", __FUNCTION__, reason);
 | 
						|
    }
 | 
						|
    return ret;
 | 
						|
}
 | 
						|
 | 
						|
#endif  /* IOT_SMART_GRID_ENABLE */
 |