Files
kunlun/app/smart_grid/inc/internal/iot_sg_sta_tsfm.h
2024-09-28 14:24:04 +08:00

378 lines
12 KiB
C
Executable File

/****************************************************************************
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_SG_STA_TSFM_H
#define IOT_SG_STA_TSFM_H
#include "iot_pkt_api.h"
#include "iot_utils_api.h"
#include "proto_645.h"
#include "iot_plc_hw_tsfm_api.h"
/* smart grid internal header files */
#include "iot_sg_fr.h"
#ifdef __cplusplus
extern "C" {
#endif
/* pack for the structures in the whole file */
#pragma pack(push) // save the pack status
#pragma pack(1) // 1 byte align
#define IOT_SG_STA_TSFM_CHIP_CCO_ADDR_LEN 4
#define IOT_SG_STA_TSFM_TOPO_SN_LEN 2
/* transformer detection feature type */
#define IOT_SG_STA_TSFM_FEATURE_INVALID (0)
#define IOT_SG_STA_TSFM_FEATURE_VOLTAGE (1)
#define IOT_SG_STA_TSFM_FEATURE_FREQUENCY (2)
#define IOT_SG_STA_TSFM_FEATURE_PERIOD (3)
#define IOT_SG_STA_TSFM_FEATURE_POWER_FACTOR (4)
#define IOT_SG_STA_TSFM_FEATURE_CURRENT (5)
#define IOT_SG_STA_TSFM_FEATURE_SNR (6)
/* tsfm sign send status */
#define IOT_SG_STA_TSFM_SEND_SIGN_STATE_IDLE 0
#define IOT_SG_STA_TSFM_SEND_SIGN_STATE_SEND 1
#define IOT_SG_STA_TSFM_SEND_SIGN_STATE_WAIT 2
/* tsfm info report status */
#define IOT_SG_STA_TSFM_INFO_RPT_STATE_IDLE 0
#define IOT_SG_STA_TSFM_INFO_RPT_STATE_RPT 1
/* tsfm info report interval, uint is 1s */
#define IOT_SG_STA_TSFM_INFO_RPT_INTER 60
/* tsfm info report max cnt */
#define IOT_SG_STA_TSFM_INFO_RPT_MAX_CNT 30
/* br2 launch duration initial value, uint is 1s */
#define IOT_SG_STA_TSFM_TOPO_LAUNCH_DUR_INIT 10
/* define br2 modulate pulse signal high duration default time, unit: 1us */
#define IOT_SG_STA_TSFM_BR2_PULSE_HIGH_DUR_DEF (400)
/* define br2 modulate pulse signal low duration default time, unit: 1us */
#define IOT_SG_STA_TSFM_BR2_PULSE_LOW_DUR_DEF (800)
/* define br2 characteristics data per bit duration time, unit: 1ms */
#define IOT_SG_STA_TSFM_BR2_CHARA_BIT_DUR_DEF (600)
/* define br2 characteristics data per bit duration time max, unit: 1ms */
#define IOT_SG_STA_TSFM_BR2_CHARA_BIT_DUR_MAX (1200)
typedef struct _iot_sg_sta_tsfm_info {
/* reserve for future */
uint8_t reserve;
/* cco address recognized, little-endian */
uint8_t cco_addr[IOT_MAC_ADDR_LEN];
/* phase recognized, see IOT_PLC_PHASE_XXX */
uint8_t phase;
} iot_sg_sta_tsfm_info_t;
/* tsfm info definition from chip */
typedef struct _iot_sg_sta_tsfm_chip_info {
/* reserve for future */
uint8_t reserve;
/* cco address recognized, little-endian */
uint8_t cco_addr[IOT_SG_STA_TSFM_CHIP_CCO_ADDR_LEN];
/* phase recognized */
uint8_t phase;
} iot_sg_sta_tsfm_chip_info_t;
#pragma pack(pop)
/* sta tsfm collect voltage data info */
typedef struct _iot_sg_sta_tsfm_collect_info {
/* feature type of collection data */
uint8_t feature_type;
/* NTB at the start of the whole network collection */
uint32_t start_ntb;
/* collection interval, unit is 1s. when transformer
* feature type is GW_APP_TSFM_FEATURE_PERIOD, interval
* is invalid.
*/
uint8_t interval;
/* collection cnt */
uint8_t cnt;
/* collected cnt */
uint8_t collected_cnt;
/* collection sequence, the sequence is generated by CCO
* the value range of sequence is 1-255.
*/
uint8_t seq;
/* collecting type. see IOT_PLC_ZC_CT_XXX_EDGE */
uint8_t clct_type;
/* NTB at the start of collect the first feature data */
uint32_t start_ntb_first;
/* the timestamp of the current collection start, uint is 1s.
* when transformer feature type is GW_APP_TSFM_FEATURE_PERIOD,
* curr_collect_start_ts is invalid.
*/
uint32_t curr_collect_start_ts;
/* number of item in each phase */
uint8_t phase_cnt[3];
/* collect data, the data structure is related to
* feature type of collection data.
* if feature_type is GW_APP_TSFM_FEATURE_PERIOD,
* data is consistent with the data structure of
* iot_plc_zc_collect_rpt_t.
* if feature_type is IOT_SG_STA_TSFM_FEATURE_VOLTAGE,
* unit data is consistent with the data structure of
* proto_645_v_t.
* if feature_type is GW_APP_TSFM_FEATURE_FREQUENCY,
* unit data is consistent with the data structure of
* proto_645_freq_t.
*/
iot_pkt_t *data;
} iot_sg_sta_tsfm_collect_info_t;
/* head device tsfm command information */
typedef struct iot_sg_sta_tsfm_send_ctrl {
/* duration of bit data output, uint is 1us
* less than or equal to IOT_PLC_HW_TSFM_OUTPUT_MAX_TIME,
* greater than or equal to IOT_PLC_HW_TSFM_OUTPUT_MIN_TIME.
*/
uint32_t dur_output;
/* ahead interval time of sending data relative to zero-crossing point,
* uint is 1us. less than or equal to IOT_PLC_HW_TSFM_MAX_AHEAD_TIME,
* greater than or equal to IOT_PLC_HW_TSFM_MIN_AHEAD_TIME.
*/
uint32_t ahead;
/* retry times */
uint8_t retry_cnt;
/* send times */
uint8_t send_cnt;
/* sending interval, uint is 1s */
uint16_t interval;
/* sending interval cnt */
uint16_t time_cnt;
/* transformer sequence number. */
uint8_t sn;
/* transformer addr, little endian */
uint8_t tsfm_addr[IOT_MAC_ADDR_LEN];
/* send tsfm sign state, see IOT_SG_STA_TSFM_SEND_SIGN_STATE_XXX */
uint8_t send_state : 3,
/* plc rising edge zc send tsfm, and it's phase negative send tsfm flag */
rising_send : 1,
/* reserve */
reserve : 4;
/* launch phase bitmap */
uint8_t launch_phase_bm;
/* current launch phase phase bitmap */
uint8_t launch_phase_bm_cur;
} iot_sg_sta_tsfm_send_ctrl_t;
/* sta tsfm report ctrl information */
typedef struct _iot_sg_sta_tsfm_rpt_ctrl {
/* report state, see IOT_SG_STA_TSFM_INFO_RPT_STATE_XXX */
uint8_t rpt_state : 3,
/* reserve */
reserve : 5;
/* report cnt */
uint8_t rpt_cnt;
/* report interval cnt, uint is 1s */
uint16_t rpt_time_cnt;
} iot_sg_sta_tsfm_rpt_ctrl_t;
#if IOT_SMART_GRID_HW_TSFM_ENABLE
/**
* @brief iot_sg_sta_hw_tsfm_init - hw tsfm detect init.
*/
uint32_t iot_sg_sta_hw_tsfm_init(void);
/**
* @brief iot_sg_sta_hw_tsfm_deinit - hw tsfm detect deinit.
*/
void iot_sg_sta_hw_tsfm_deinit(void);
/**
* @brief iot_sg_sta_hw_tsfm_start - start hw tsfm detect.
*/
void iot_sg_sta_hw_tsfm_start(void);
/**
* @brief iot_sg_sta_hw_tsfm_is_on - check whether the hw tsfm detect function
* is open.
*/
uint8_t iot_sg_sta_hw_tsfm_is_on(void);
/**
* @brief iot_sg_sta_hw_tsfm_is_3p() - check whether 3-phase tsfm signal
* transmission is supported
* @retuval: 1 - supported, 0 - not.
*/
uint8_t iot_sg_sta_hw_tsfm_is_3p(void);
/**
* @brief iot_sg_sta_tsfm_msg_handle - handle message from tsfm chip.
* @param pkt: iot packet to data from tsfm chip.
*/
void iot_sg_sta_tsfm_msg_handle(iot_pkt_t *pkt);
/**
* @brief iot_sg_sta_tsfm_msg_handle_from_cco - handle message from cco.
* @param data_type: type of iot packet, see GW_APP_DATA_TYPE_XXX.
* @param data: pointer to protocol data buffer.
* @param len: data length.
* @retval: 0 - for success case.
* @retval: otherwise - error code.
*/
uint32_t iot_sg_sta_tsfm_msg_handle_from_cco(uint8_t data_type, uint8_t *data,
uint32_t len);
/**
* @brief iot_sg_sta_save_tsfm_info - save hardware transformer detection info
* @param tsfm_info: - hardware transformer detection info for save.
*/
void iot_sg_sta_save_tsfm_info(iot_sg_sta_tsfm_info_t *tsfm_info);
/**
* @brief iot_sg_sta_tsfm_send_restart - tsfm send restart.
* @param retry_cnt: hardware transformer topo info for save.
* @param interval: tsfm sign send interval time.
* @param tsfm_addr: need to send tsfm addr.
* @param cfg: send configuration structure.
* @param sn: transformer sequence number.
* @retval: ERR_OK - for success case.
* @retval: otherwise - error code.
*/
uint32_t iot_sg_sta_tsfm_send_restart(uint8_t retry_cnt,
uint16_t interval, uint8_t *tsfm_addr, hw_tsfm_send_cfg_t *cfg, uint8_t sn);
/**
* @brief topo sn repeat send.
*/
void iot_sg_sta_tsfm_repeat(void);
/**
* @brief hw tsfm rpt info start.
*/
void iot_sg_sta_tsfm_info_rpt_start(void);
/**
* @brief save the tsfm topo send time.
*/
void iot_sg_sta_tsfm_topo_time_save(iot_time_tm_t *start_tm,
iot_time_tm_t *end_tm);
/**
* @brief iot_sg_sta_tsfm_topo_br2_launch_start - br2 launch start.
* @param launch_tm: pointer to the br2 launch time.
* @param launch_code: pointer to the br2 launch code.
* @param launch_code_len: length of br2 launch coe.
* @param pulse_high_dur: br2 modulate pulse signal high duration default time,
* unit: 1us.
* @param pulse_low_dur: br2 modulate pulse signal low duration default time,
* unit: 1us.
* @param chara_bit_dur: br2 characteristics data per bit duration time,
* unit: 1ms.
* @param seq: pointer to the br2 launch sequence.
* @param encoding_mode: encoding mode, see IOT_PLC_HW_TSFM_LOAD_ENCODE_XXX
* @retval: ERR_OK - for success case.
* @retval: otherwise - for fail case.
*/
uint8_t iot_sg_sta_tsfm_topo_br2_launch_start(
proto_645_07_time_ymdhms_t *launch_tm, uint8_t *launch_code,
uint8_t launch_code_len, uint16_t pulse_high_dur,
uint16_t pulse_low_dur, uint16_t chara_bit_dur, uint16_t *seq,
uint8_t encoding_mode);
/*
* @brief init the tsfm topo send time.
* @param dev_type: dev type.
*/
void iot_sg_sta_hw_tsfm_topo_record_init();
/*
* @brief tsfm support mode check.
* @param mode: tsfm mode, see IOT_PLC_HW_TSFM_SEND_MODE_XXX.
* @retval: ERR_OK - for success case.
* @retval: otherwise - for fail case.
*/
uint32_t iot_sg_sta_tsfm_supp_mode_check(uint8_t mode);
#else /* IOT_SMART_GRID_HW_TSFM_ENABLE */
#define iot_sg_sta_hw_tsfm_init()
#define iot_sg_sta_hw_tsfm_deinit()
#define iot_sg_sta_hw_tsfm_start()
#define iot_sg_sta_hw_tsfm_is_on() 0
#define iot_sg_sta_hw_tsfm_is_3p() 0
#define iot_sg_sta_tsfm_msg_handle(pkt) (iot_pkt_free(pkt))
#define iot_sg_sta_tsfm_msg_handle_from_cco(data_type, data, len) (ERR_NOSUPP)
#define iot_sg_sta_save_tsfm_info(tsfm_info)
#define iot_sg_sta_tsfm_send_restart(retry_cnt, interval, tsfm_addr, cfg, sn) \
(ERR_NOSUPP)
#define iot_sg_sta_tsfm_repeat()
#define iot_sg_sta_tsfm_info_rpt_start()
#define iot_sg_sta_tsfm_topo_time_save(start_tm, end_tm)
#define iot_sg_sta_tsfm_topo_br2_launch_start(launch_tm, launch_code, \
launch_code_len, pulse_high_dur, pulse_low_dur, chara_bit_dur, seq, \
encoding_mode) (1)
#define iot_sg_sta_hw_tsfm_topo_record_init()
#define iot_sg_sta_tsfm_supp_mode_check(mode) ERR_NOSUPP
#endif /* IOT_SMART_GRID_HW_TSFM_ENABLE */
/**
* @brief iot_sg_sta_tsfm_collect_init - tsfm collect feature init.
*/
void iot_sg_sta_tsfm_collect_init(void);
/**
* @brief iot_sg_sta_tsfm_collect_deinit - tsfm collect feature deinit.
*/
void iot_sg_sta_tsfm_collect_deinit(void);
/**
* @brief iot_sg_sta_tsfm_collect_start - tsfm start collect feature data.
*/
void iot_sg_sta_tsfm_collect_start(void);
/**
* @brief iot_sg_sta_tsfm_collect_stop - tsfm stop collect feature data.
*/
void iot_sg_sta_tsfm_collect_stop(void);
/**
* @brief handle feature data from meter.
* @param seq: sequence of collect request.
* @param data_type: data type.
* @param resp_data: feature data of response from meter.
* @param resp_len: response data length.
* @param req_data: request data of feature collection.
* @param req_len: request data length.
*/
void iot_sg_sta_tsfm_collect_feature_handle(uint16_t seq,
uint8_t data_type, uint8_t *resp_data, uint16_t resp_len,
uint8_t *req_data, uint16_t req_len);
#ifdef __cplusplus
}
#endif
#endif /* IOT_SG_STA_TSFM_H */