Files
kunlun/app/brm/inc/iot_brm.h

468 lines
17 KiB
C
Raw Normal View History

2024-09-28 14:24:04 +08:00
/****************************************************************************
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_BRM_H
#define IOT_BRM_H
/* os shim includes */
#include "os_types_api.h"
#include "os_timer_api.h"
/* common includes */
#include "iot_plc_api.h"
#include "iot_mem_pool_api.h"
#include "iot_queue_api.h"
#include "iot_utils_api.h"
#include "iot_task_api.h"
#include "iot_uart_api.h"
#include "iot_plc_pm_api.h"
#include "iot_addr_hash_table_api.h"
#include "iot_rtc_api.h"
/* protocol header file */
#include "proto_645.h"
#include "proto_69845.h"
#include "proto_gw_app.h"
#include "iot_brm_common.h"
#include "iot_brm_nv.h"
#include "iot_brm_cache.h"
#include "iot_brm_rec.h"
#if IOT_BRM_DEVADP_ENABLE
#include "iot_brm_adp.h"
#include "iot_brm_proto_devadp_645_ext.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if (IOT_BRM_ENABLE)
/* define node listed node hash table size */
#define IOT_BRM_NODE_HASH_TABLE_SIZE 4
/* define the maximum number of reading task supported */
#define IOT_BRM_SUPPORT_MR_TASK_NUM 4
/* maximum number of data items supported per read task */
#define IOT_BRM_MR_TASK_SUPPORT_DI_MAX 10
/* Define the default maximum number of reading times for reading task */
#define IOT_BRM_MR_TASK_SEND_MAX_DEFAULT 2
/* update htm info interval time, unit is 1s */
#define IOT_BRM_HTM_TIME_INTERVAL (60)
/* debug fastly get htm info delay, unit is 1s */
#define IOT_BRM_HTM_TIME_INTERVAL_FAST (1)
/* define the status of the read task */
#define IOT_BRM_MR_TASK_STATE_IDLE 0 /* idle state, can be started */
#define IOT_BRM_MR_TASK_STATE_READY 1 /* ready state, to be executed */
#define IOT_BRM_MR_TASK_STATE_EXE 2 /* execution state, exit at done */
/* define the command reason for the read task */
#define IOT_BRM_MR_TASK_CMD_REASON_STARTUP 0
#define IOT_BRM_MR_TASK_CMD_REASON_UPGRADE 1
/* define 485 management status */
#define IOT_BRM_RS485_STATE_IDLE 0
#define IOT_BRM_RS485_STATE_READY 1
#define IOT_BRM_RS485_STATE_FWD 2
#define IOT_BRM_RS485_STATE_MR 3
#define IOT_BRM_RS485_STATE_CHECK 4
#define IOT_BRM_RS485_STATE_FWD_MODBUS 5
#define IOT_BRM_RS485_STATE_CORR_TIME 6
#define IOT_BRM_RS485_STATE_SLAVE_MODE 7
/* defines the load record type of the node */
#define IOT_BRM_NODE_LR_REC_TYPE_UNKNOWN 0
#define IOT_BRM_NODE_LR_REC_TYPE_V0 1
#define IOT_BRM_NODE_LR_REC_TYPE_V1 2
#define IOT_BRM_NODE_LR_REC_TYPE_NOSUPP 3
/* define types of curve reading results */
#define IOT_BRM_NODE_LR_REC_RES_UNKNOWN 0
#define IOT_BRM_NODE_LR_REC_RES_NORMAL 1
#define IOT_BRM_NODE_LR_REC_RES_ABRNORMAL 2
/* meter reading task node descriptor */
typedef struct _iot_brm_mr_task_node {
/* pointer to node descriptor */
void *node_desc;
/* read task node queue link */
iot_list_head_t link;
/* remaining send nud per data item */
uint8_t di_resend[IOT_BRM_MR_TASK_SUPPORT_DI_MAX];
/* data item bitmap currently being read */
uint16_t di_bm_cur;
/* current data item bitmap to be being read */
uint16_t di_bm;
} iot_brm_mr_task_node_t;
/* branch management node descriptor */
typedef struct _iot_brm_node_desc {
/* hash table entry */
iot_addr_hash_entry_t entry;
/* proto type, see IOT_BRM_PROTO_TYPE_XXXX */
uint8_t proto_type;
/* node index */
uint8_t idx;
/* node port, see IOT_BRM_PORT_XXX */
uint8_t port;
/* voltage transformer ratio of node, uint is x1 */
uint8_t v_ratio;
/* current transformer ratio of node, uint is x1 */
uint16_t i_ratio;
/* baud rate, this field is valid only if the node is 485 */
uint32_t baud;
/* creator of node, see IOT_BRM_OPERATOR_XXX */
uint8_t creator : 2,
/* flag to mark if node time abnormal */
time_abnormal : 1,
/* flag to mark if lr rec reading abnormal */
lr_rec_mr_result : 2,
/* PPM ping node state, see IOT_BRM_PPM_PING_NODE_STATE_XXX */
ppm_node_sate : 3;
uint8_t lr_rec_type;
/* load record type bitmap to be detected */
uint8_t lr_rec_detect_bm;
/* detecting counter before determined lr type:
* detect_cnt[0] is IOT_BRM_DI_DESC_TYPE_LR_REC_645_V0 deny count;
* detect_cnt[1] is IOT_BRM_DI_DESC_TYPE_LR_REC_645_V1 deny count;
* detect_cnt[2] is IOT_BRM_DI_DESC_TYPE_NOR accept count;
*/
uint8_t detect_cnt[3];
/* load record reading error countrer */
uint8_t lr_rec_abnormal_cnt;
/* assembly number */
uint8_t ass_pos : 6,
/* reserved for further use */
rsvd1 : 2;
/* current transmission PPM fix value */
int8_t ppm_fix;
/* ppm fix value valid time stamp, uint is 1s */
uint32_t ppm_valid_ts;
/* read task node queue links */
iot_brm_mr_task_node_t mr_task_node[IOT_BRM_SUPPORT_MR_TASK_NUM];
/* node sampling data set */
iot_brm_node_samp_t samp;
/* samp curve buffer */
iot_brm_cache_cr_t cr;
} iot_brm_node_desc_t;
/* node list descriptor */
typedef struct _iot_brm_node_list {
/* meter info hash table */
iot_addr_hash_table_h node_table;
/* pointer to the node info in the hash meter*/
iot_brm_node_desc_t *entry[IOT_BRM_SUPPORT_NODE_MAX];
/* node cnt */
uint8_t node_cnt;
} iot_brm_node_list_t;
/* meter reading statistical info descriptor */
typedef struct {
/* total reading counter */
uint8_t total_cnt;
/* successful reading counter */
uint8_t sus_cnt;
} iot_brm_mr_stat_t;
/* define the callback function of reading task completion analysis */
typedef void (*iot_brm_mr_task_done_analysis_func_t)(void *arg);
/* this callback function allows each node to configure different data items */
typedef void (*iot_brm_mr_task_node_cfg_func_t)(void *task,
iot_brm_node_desc_t *node);
/* define data item type */
#define IOT_BRM_DI_DESC_TYPE_NOR 0
#define IOT_BRM_DI_DESC_TYPE_LR_REC_645_V0 1
#define IOT_BRM_DI_DESC_TYPE_LR_REC_645_V1 2
#define IOT_BRM_DI_DESC_TYPE_LR_REC_OOP 3
/* data item description structure */
typedef struct {
/* data item magic */
uint32_t di_magic;
/* data item type, see IOT_BRM_DI_DESC_TYPE_XXX */
uint8_t type;
/* pointer to record column selection data item table */
uint32_t *csd_magic_tab;
} di_desc_t;
/* meter read task configuration structure definition */
typedef struct {
/* the data item descriptor table for dl/t 645 */
di_desc_t di_645[IOT_BRM_MR_TASK_SUPPORT_DI_MAX];
/* the data item descriptor table for oop */
di_desc_t di_oop[IOT_BRM_MR_TASK_SUPPORT_DI_MAX];
/* if set, the task runs automatically according to the start
* time and interval.
*/
uint32_t auto_start :1,
/* reserved for further use */
rsvd :31;
/* collection interval, uint is 1s. when all nodes are on port 485,
* this read interval is used.
*/
uint32_t interval1;
/* collection interval, uint is 1s */
uint32_t interval2;
/* task run timeout, uins is 1s */
uint32_t timeout;
/* task start reference time */
iot_time_tm_t start_time;
/* maximum number of retries per data item for 485 port */
uint8_t send_max_rs485;
/* maximum number of retries per data item for PLC port */
uint8_t send_max_plc;
/* random additional maximum value of start-up reference time,
* uint is min.
*/
uint8_t start_dly_max;
} iot_brm_mr_task_cfg_t;
/* meter reading task descriptor */
typedef struct _iot_brm_mr_task_t {
/* task status, see IOT_BRM_MR_TASK_STATE_XXX */
uint8_t state;
/* the returned customer ID is registered with the meter reading buffer */
uint8_t cus_id;
/* flag mark to if the task is already active */
uint8_t active :1,
/* flag mark to if it is necessary to recalculate the reading time */
reload_req :1,
/* reserved for further use */
rsvd :6;
/* bitmap of the reason for the request to disable the task */
uint8_t dis_req_reason_bm;
/* node index bitmap to be collected */
uint32_t node_bm;
/* queue of nodes to be read */
iot_list_head_t node_queue;
/* queue of nodes to be read */
iot_list_head_t node_queue_485;
/* start timestamp - boot time, uint is 1ms */
uint32_t start_ts;
/* time when the task was last started -- RTC */
uint32_t last_start_tm;
/* time when the task was next started -- RTC */
uint32_t next_start_tm;
/* time cost of task, uint is 1ms */
uint32_t cons;
/* the time-out for the task to run in one round, uint is 1s */
uint32_t timeout;
/* start-up reference time */
uint32_t start_tm;
/* collection interval, uint is 1s */
uint32_t interval;
/* the cumulative duration of the task run, uint is 1s */
uint32_t run_dur;
/* number of 485 nodes to be read */
uint8_t rs485_node_cnt;
/* number of plc nodes to be read */
uint8_t plc_node_cnt;
/* meter reading statistics for task */
iot_brm_mr_stat_t stat;
/* callback function for node collection information configuration */
iot_brm_mr_task_node_cfg_func_t node_cfg;
/* analysis callback function of reading task done */
iot_brm_mr_task_done_analysis_func_t done_analysis_func;
/* pointer to meter reading task config structure */
const iot_brm_mr_task_cfg_t *cfg;
} iot_brm_mr_task_t;
/* config info descriptor */
typedef struct _iot_brm_config_info {
/* address, little endian */
uint8_t addr[IOT_MAC_ADDR_LEN];
/* tsfm address, little-endian */
uint8_t tsfm_addr[IOT_MAC_ADDR_LEN];
/* longitude */
iot_brm_location_unit_t longitude;
/* latitude */
iot_brm_location_unit_t latitude;
/* start time of curve recording */
uint32_t cr_start;
/* curve recording interval, uins is 1s */
uint16_t cr_interval;
/* TODO: adding more configuration info */
} iot_brm_config_info_t;
/* plc link state info */
typedef struct _iot_brm_plc_state {
/* plc app register status. 1 means registered */
uint8_t app_reg : 1,
/* plc link status */
link_ready : 1,
/* conn less status */
conn_less_ready : 1,
/* reserved for further use */
rsvd : 5;
/* device ID */
uint8_t dev_id[IOT_MAC_ADDR_LEN];
/* reset counter */
uint16_t reset_cnt;
/* plc mac address */
uint8_t addr[IOT_MAC_ADDR_LEN];
/* cco mac address */
uint8_t cco_addr[IOT_MAC_ADDR_LEN];
/* tei of the device */
uint16_t dev_tei;
} iot_brm_plc_state_t;
/* 485 configuration descriptor*/
typedef struct _iot_brm_rs485_config {
/** baud rate */
uint32_t baud;
/* threshold for receive timeout. */
uint32_t thd_rx_tm;
/** parity. see IOT_UART_PARITY_XXX */
uint8_t parity;
/** length of data. see IOT_UART_DLEN_X_BITS */
uint8_t data;
/** stop bit. see IOT_UART_STOP_XX_BITS */
uint8_t stop;
/** stands for if 'fmt' valid. 1 : valid, 0 : invalid. */
uint8_t fmt_valid;
/** the format structure of the frame */
iot_frame_fmt fmt;
} iot_brm_rs485_config_t;
/* 485 control block descriptor */
typedef struct _iot_brm_rs485_cb {
/* state, see IOT_BRM_RS485_STATE_XXX */
uint8_t state;
/* pending iot_pkt */
iot_pkt_t *pend_pkt;
/* who pend iot_pkt, see iot_brm_data_owner_t */
iot_brm_data_owner_t pend_from;
/* 485 uart handle*/
iot_uart_h handle;
/* 485 management timer */
timer_id_t man_timer;
/* config info form sg app */
iot_brm_rs485_config_t ext_config;
/* cache last send data */
uint8_t last_send_data[GW_APP_CON_MR_MAX_MSG_LEN];
/* cache data length */
uint16_t last_send_len;
/* 485 bus fwd data packets and takes up bus time */
uint32_t fwd_taken_time;
/* where does the message to be forwarded come from */
iot_brm_data_owner_t fwd_from;
} iot_brm_rs485_cb_t;
/* the present correcting time node inf */
typedef struct _iot_brm_correct_time_inf {
/* the present correcting time node addr */
uint8_t addr[IOT_MAC_ADDR_LEN];
/* correct time retry max retry number */
uint8_t max_num;
} iot_brm_correct_time_inf_t;
/* the connectionless correcting time information */
typedef struct _iot_brm_conn_less_cor_tm_inf {
/* count after last correct, increase by IOT_BRM_PERIOND_TIMER_INTERVAL */
uint16_t count;
/* send point, when count equal this, send message. */
uint16_t send_point;
/* correct time retry max retry number */
uint8_t retry_num;
} iot_brm_conn_less_cor_tm_inf_t;
/* branch management global descriptor */
typedef struct _iot_brm_global {
/* branch management task configuration */
iot_task_config_t task_cfg;
/* branch management task handle */
iot_task_h task_h;
/* periodic timer */
timer_id_t periodic_timer;
/* timer for node list update to nv storage */
timer_id_t node_list_timer;
/* 485 control block descriptor */
iot_brm_rs485_cb_t rs485_cb;
/* plc app regsiter configuration */
iot_plc_app_t plc_app_cfg;
/* handle of registered plc app */
iot_plc_app_h plc_app_h;
/* buffer is used to cache protocol data */
uint8_t proto_buff[PROTO_645_MAX_PKT_LEN];
/* node list structure */
iot_brm_node_list_t node_list;
/* handle of registered IPC */
iot_ipc_h ipc_h;
/* plc link state info */
iot_brm_plc_state_t plc_state;
/* branch management app global config info */
iot_brm_config_info_t cfg;
/* hanale for basic meter read */
uint32_t basic_mr_h;
/* hanale for curve pre reading task */
uint32_t lr_mr_h;
/* humidity and temperature mesurement counter, unit is 1s */
uint16_t time_cnt;
/* data forwarding id in mr buf */
uint8_t conless_cus_id;
/* flag of init external htm device fail */
uint8_t htm_invalid :1,
/* flag of get htm done */
get_htm_done :1,
/* flag to mark if power down */
pd :1,
/* flag to mark if device start up, device address is ok */
start_up :1,
/* req renew bt address flag */
set_bt_req :1,
/* is breaker */
is_brk :1,
/* reserved for future */
rsvd :2;
/* external temperature, uint is 0.1 centigrade */
int16_t ext_temperature;
/* external humidity, unit is 0.1 %RH */
int16_t ext_humidity;
/* calibrate status see - IOT_BRM_ONESHOT_CALIBRATE_XXX */
uint8_t cal_sts :2,
/* calibrate fail reason code - IOT_EM_EXT_ONESHOT_CALI_FAIL_XXX */
cal_fail_code :2,
/* reserved for future */
rsvd2 :4;
/* brm whether move dev */
uint8_t is_move_dev;
/* only proto 698.45 use sn */
uint8_t sn_proto_698;
/* seq number of 3761 message last send */
uint8_t sn_3761;
/* the present correcting time node information */
iot_brm_correct_time_inf_t cor_tm_inf;
/* onnectionless correcting time information */
iot_brm_conn_less_cor_tm_inf_t conn_less_cor_tm_inf;
/* clean mr reason timeout tm - uint: s */
uint32_t clean_mr_reason_cnt;
/* br result record buffer */
iot_brm_recv_rec_entry_t br_rec_buf;
/* br result record countdown */
uint8_t br_rec_cd;
} iot_brm_global_t;
#endif /* IOT_BRM_ENABLE && PLC_SUPPORT_STA_ROLE */
#ifdef __cplusplus
}
#endif
#endif /* IOT_BRM_H */