Files
kunlun/app/brm/inc/iot_brm_adp.h
2024-09-28 14:24:04 +08:00

212 lines
8.3 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 _INCLUDE_IOT_BRM_ADP_H_
#define _INCLUDE_IOT_BRM_ADP_H_
#define IOT_EM_REPORT_EVENT_RECORD_MAX (5) /* Report 5 event records at a time. */
#define IOT_EM_REPORT_LOAD_RECORD_MAX (5) /* Report 5 load records at a time. */
#define IOT_BRM_RECORD_PHASE (3) /* record three phase data. */
#define IOT_BRM_RECORD_PHASE_MAX (4) /* record three phase and total data. */
#define IOT_BRM_FSM_REQ_CFM_CNT (3)
/* Event of plug-in device and our brm. */
typedef enum _iot_dm_event_e {
/* Customer event start. Customer care about those events. */
DM_EVENT_DEV_POWER_ON = 0x0,/* Device power on. I > I(standby). */
DM_EVENT_DEV_POWER_OFF = 0x1,/* Device power off. I <= I(standby). */
DM_EVENT_DEV_WORK_START = 0x2,/* Device start working. I > I(work). */
DM_EVENT_DEV_WORK_STOP = 0x3,/* Device stop working. I <= I(work). */
DM_EVENT_DEV_TRAP_START = 0x4,/* Device work in trap. P > P(trap). */
DM_EVENT_DEV_TRAP_STOP = 0x5,/* Device restore from trap. P <= P(trap). */
/* Customer event end. */
DM_EVENT_INVALID
} iot_dm_event_e;
/* State of plug-in device. */
typedef enum _iot_dm_dev_state_e {
/* No device plug into our brm. */
DM_STATE_DEV_OFFLINE = 0x00,
/* Device plug into our brm, staying in standby. */
DM_STATE_DEV_STANDBY = 0x01,
/* Device plug into our brm, working right. */
DM_STATE_DEV_WORKING = 0x02,
/* Device plug into our brm, running in trouble. */
DM_STATE_DEV_TRAPING = 0x03,
DM_STATE_DEV_MAX = 0x0F
} iot_dm_dev_state_e;
/* Seconds have passed since 1970, Reference Unix time.*/
typedef uint32_t iot_brm_time_t;
/* Item of each reported load record. */
typedef struct _iot_brm_load_record_item_t {
uint16_t event; /* Event type. see iot_dm_event_e. */
uint16_t v[IOT_BRM_RECORD_PHASE]; /* Voltage, uint is 0.1V. */
int32_t i[IOT_BRM_RECORD_PHASE]; /* Electrical current, uint is 0.001A. */
int32_t p[IOT_BRM_RECORD_PHASE_MAX]; /* Active power, uint is 0.0001 KW. */
uint16_t f[IOT_BRM_RECORD_PHASE_MAX]; /* Power factor, uint is 0.001. */
uint32_t w; /* Work(meter bottom value), uint is 0.0001 KWH. */
} iot_brm_load_record_t;
/* Item of each reported event record. */
typedef struct _iot_brm_event_record_item_t {
uint32_t state; /* State type. see iot_dm_dev_state_e*/
iot_brm_time_t time; /* Load record timestamp.*/
iot_brm_load_record_t rcd_data; /* load record. */
} iot_brm_event_record_t;
/* The device state of energy meter adapter. Reference to iot_dm_dev_state_e. */
typedef uint8_t brm_adp_dev_state_e;
/* The device finite state machine handle. */
typedef brm_adp_dev_state_e(*brm_adp_dev_fsm_handle_t)(uint32_t event,
iot_brm_event_record_t *p_event_record);
/* Threshold data. */
typedef struct _iot_brm_threshold_t {
int32_t i_standby; /* Current threshold for standby.*/
int32_t i_work; /* Current threshold for working.*/
int32_t p_trap; /* Power threshold for runing in trap.*/
} iot_brm_threshold_t;
/* The table of device finite state machine handle. */
typedef struct _brm_adp_dev_fsm_table_t {
brm_adp_dev_fsm_handle_t *handle;
} brm_adp_dev_fsm_table_t;
/* Energy meter adapter device finite state machine. */
typedef struct _brm_adp_dev_fsm_t {
/* Previous state */
brm_adp_dev_state_e prev_state;
/* Current state */
brm_adp_dev_state_e cur_state;
/* The table of device finite state machine handle */
brm_adp_dev_fsm_table_t table;
/* counter of request to change state */
uint16_t chg_req_cnt;
/* bitmap of request to change state */
uint16_t chg_req_bitmp;
} brm_adp_dev_fsm_t;
/* The energy meter adapter context. */
typedef struct _brm_adp_context_t {
/* Device finite state machine. */
brm_adp_dev_fsm_t dev_fsm;
/* Threshold saved in local. */
iot_brm_threshold_t threshold;
/* Meter bottom value saved in local, WH. */
uint32_t meter_bottom_val;
/* PLC network status: 0--not joined, 1--joined*/
uint16_t link_ready;
/* flag to indicate whether already send event report once joining nw*/
uint16_t evt_on_join;
} brm_adp_context_t;
/* Event record command, used by response operation.
* Response query event record of the day on timestamp.
*/
typedef struct _iot_brm_adp_cmd_event_record_resp_t {
uint32_t start_idx; /* Record start index. */
uint32_t record_num; /* Number of records reported. */
iot_brm_event_record_t record_buf[0]; /* Buffer that stores records. */
} iot_brm_adp_cmd_event_record_resp_t;
/* Process corresponding event for device finite state machine.
* Compare ADC data with threshold, trigger event,
* call process state function.
*/
void iot_brm_adp_fsm_event_process(void);
/* load the energy meter adapter threshold value. */
uint8_t iot_brm_adp_threshold_load(void);
/**
* @brief iot_brm_adp_get_threshold() - get threshold.
* @retval : return the threshold.
*/
iot_brm_threshold_t *iot_brm_adp_get_threshold(void);
/**
* @brief iot_brm_adp_save_threshold() - save the energy meter adapter
* threshold value..
* @param threshold : the threshold to save.
*/
void iot_brm_adp_threshold_save(iot_brm_threshold_t *threshold);
/* load the energy meter adapter bottom value. */
uint8_t iot_brm_adp_bottom_val_load(void);
/* @brief iot_brm_adp_bottom_val_save() - save the energy meter adapter
* bottom value.
* @param bottom_val: the bottom value to save.
*/
void iot_brm_adp_bottom_val_save(uint32_t bottom_val);
/**
* @brief iot_brm_get_adp_evt_entry_size() - get event record entry size.
* @retval : return event record entry size.
*/
uint8_t iot_brm_get_adp_evt_entry_size(void);
/**
* @brief iot_brm_adp_evt_save() - record adp eventing data.
* @param p_evt_rec: adp eventing record data.
*/
void iot_brm_adp_evt_save(iot_brm_event_record_t *p_evt_rec);
/**
* @brief iot_brm_read_adp_evt_daily_cnt() - get event record count by time.
* @param ts : find record event time
* @retval : return event record count.
*/
uint8_t iot_brm_read_adp_evt_daily_cnt(uint32_t ts);
/**
* @brief iot_brm_adp_evt_load() - load record adp eventing data.
* @param ts : find record event time
* @param start_inx : find record event time start index
* @param cnt : find record event time count from start index
* @param pkt : adp event record data.
* @retval : response event data count.
*/
uint8_t iot_brm_adp_evt_load(uint32_t ts, uint8_t start_inx, uint8_t cnt,
iot_pkt_t **pkt);
/**
* @brief iot_brm_adp_evt_load() - load the latest event record from flash.
* @param pkt : adp event record data.
* @retval : 0--no err, 1--err.
*/
uint8_t iot_brm_adp_latest_evt_load(iot_pkt_t **pkt);
/**
* @brief iot_brm_adp_load_data() - load record adp load data.
* @param ts : find record load start time
* @param cnt : find record load count from ts
* @param pkt : adp load record data.
* @retval : response load data count.
*/
uint8_t iot_brm_adp_load_data(uint32_t ts, uint8_t cnt, iot_pkt_t **pkt);
/* Initialize the energy meter adapter module. */
uint32_t iot_brm_adp_init(void);
#endif /* _INCLUDE_IOT_BRM_ADP_H_ */