251 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			251 lines
		
	
	
		
			10 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.
 | 
						|
 *
 | 
						|
 * ****************************************************************************/
 | 
						|
 | 
						|
#ifndef _INCLUDE_IOT_EM_ADP_H_
 | 
						|
#define _INCLUDE_IOT_EM_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_EM_ADP_CMD_ARG_DEF_PRIO      (0) /* Default priority of the cmd arg. */
 | 
						|
#define IOT_EM_ADP_CMD_ARG_DEF_NEED_ACK  (true) /* Default whether if need ack. */
 | 
						|
 | 
						|
/* Seconds have passed since 1970, Reference Unix time.*/
 | 
						|
typedef uint32_t iot_em_time_t;
 | 
						|
 | 
						|
/* Threshold data. */
 | 
						|
typedef struct _iot_em_threshold_t {
 | 
						|
    uint32_t    i_standby;   /* Current threshold for standby.*/
 | 
						|
    uint32_t    i_work;      /* Current threshold for working.*/
 | 
						|
    uint32_t    p_trap;      /* Power threshold for runing in trap.*/
 | 
						|
}iot_em_threshold_t;
 | 
						|
 | 
						|
/* Energe meter threshold type. It is bitmap, indicates which component values
 | 
						|
 * in threshold data struct is effectively.
 | 
						|
 * For example, when type equals (EM_THR_STANDBY_CURRENT | EM_THR_WORK_CURRENT),
 | 
						|
 * it means standby current, work current values in threshold struct is effectively.
 | 
						|
 */
 | 
						|
typedef enum _iot_em_threshold_type_e {
 | 
						|
    EM_THR_STANDBY_CURRENT = 1 << 0,
 | 
						|
    EM_THR_WORK_CURRENT    = 1 << 1,
 | 
						|
    EM_THR_POWER_IN_TRAP   = 1 << 2,
 | 
						|
}iot_em_threshold_type_e;
 | 
						|
 | 
						|
/* Item of each reported event record. */
 | 
						|
typedef struct _iot_em_event_record_item_t {
 | 
						|
    uint32_t        etype;  /* Event type. */
 | 
						|
    uint32_t        state;  /* State type */
 | 
						|
    iot_em_time_t   etime;  /* Event record timestamp. */
 | 
						|
    uint32_t        v;      /* Voltage, mV. */
 | 
						|
    uint32_t        f;      /* Power factor, 0.1%. */
 | 
						|
    uint32_t        i;      /* Electrical current, mA. */
 | 
						|
    uint32_t        p;      /* Active power, 0.1W(1A * 0.1V). */
 | 
						|
    uint32_t        w;      /* Work(meter bottom value), WH(1A * 1V * 1h). */
 | 
						|
}iot_em_event_record_t;
 | 
						|
 | 
						|
/* Item of each reported load record. */
 | 
						|
typedef struct _iot_em_load_record_item_t {
 | 
						|
    iot_em_time_t   time;   /* Load record timestamp.*/
 | 
						|
    uint32_t        v;      /* Voltage, mV. */
 | 
						|
    uint32_t        f;      /* Power factor, 0.1%. */
 | 
						|
    uint32_t        i;      /* Electrical current, mA. */
 | 
						|
    uint32_t        p;      /* Active power, 0.1W(1A * 0.1V). */
 | 
						|
    uint32_t        w;      /* Work(meter bottom value), WH(1A * 1V * 1h). */
 | 
						|
}iot_em_load_record_t;
 | 
						|
 | 
						|
/* Realtime load data. */
 | 
						|
typedef struct _iot_em_realtime_load_data_t {
 | 
						|
    iot_em_time_t   time;   /* Monitor unit current time. */
 | 
						|
    uint32_t        v;      /* Voltage, mV. */
 | 
						|
    uint32_t        f;      /* Power factor, 0.1%. */
 | 
						|
    uint32_t        i;      /* Electrical current, mA. */
 | 
						|
    uint32_t        p;      /* Active power, 0.1W(1A * 0.1V). */
 | 
						|
    uint32_t        w;      /* Work(meter bottom value), WH(1A * 1V * 1h). */
 | 
						|
}iot_em_realtime_load_data_t;
 | 
						|
 | 
						|
/*****************************************************************************/
 | 
						|
/**************************** Defination used by Task ************************/
 | 
						|
/*****************************************************************************/
 | 
						|
 | 
						|
/* List of the command id. */
 | 
						|
enum em_adp_command_id_e {
 | 
						|
    /* Current, power threshold. */
 | 
						|
    EM_ADP_CID_THRESHOLD,
 | 
						|
    /* Counter of event on given day. */
 | 
						|
    EM_ADP_CID_EVT_COUNTER,
 | 
						|
    /* Record of event on given day. */
 | 
						|
    EM_ADP_CID_EVT_RECORD,
 | 
						|
    /* Counter of load record on given day. */
 | 
						|
    EM_ADP_CID_LOAD_COUNTER,
 | 
						|
    /* Diagram of load on given day. */
 | 
						|
    EM_ADP_CID_LOAD_RECORD,
 | 
						|
    /* Realtime load data. */
 | 
						|
    EM_ADP_CID_REALTIME_LOAD_DATA,
 | 
						|
    /* Meter bottom value. */
 | 
						|
    EM_ADP_CID_METER_BOTTOM_VAL,
 | 
						|
 | 
						|
    /* ... */
 | 
						|
 | 
						|
    EM_ADP_CID_MAX
 | 
						|
};
 | 
						|
 | 
						|
/* Operation code. */
 | 
						|
enum em_adp_command_opcode_e {
 | 
						|
    /* Command for change the configuration of device. */
 | 
						|
    EM_ADP_OP_CONFIG,
 | 
						|
    /* Response config result error or ok to smart meter layer. */
 | 
						|
    EM_ADP_OP_CFM,
 | 
						|
    /* Command for query the status /configuration /statistics of device. */
 | 
						|
    EM_ADP_OP_QUERY,
 | 
						|
    /* Response query message to smart meter layer. */
 | 
						|
    EM_ADP_OP_RESPONSE,
 | 
						|
    /* Indication message to smart meter layer. */
 | 
						|
    EM_ADP_OP_INDICATION,
 | 
						|
};
 | 
						|
 | 
						|
/* Return status of the command. */
 | 
						|
enum em_adp_command_response_e {
 | 
						|
    EM_ADP_RESP_OK,
 | 
						|
    EM_ADP_RESP_ERROR,
 | 
						|
    EM_ADP_RESP_BUSY,
 | 
						|
    EM_ADP_RESP_UNKNOWN_CMD
 | 
						|
};
 | 
						|
 | 
						|
typedef struct _iot_em_adp_cid_t {
 | 
						|
    uint8_t cid;    /* Reference to enum em_adp_command_id_e */
 | 
						|
    uint8_t opcode; /* Reference to enum em_adp_command_opcode_e */
 | 
						|
}iot_em_adp_cid_t;
 | 
						|
 | 
						|
/* The argument for energy meter adapter command handle. */
 | 
						|
typedef struct _iot_em_adp_cmd_arg_t {
 | 
						|
    iot_em_adp_cid_t    cid;        /* The em_adp command id. */
 | 
						|
    uint16_t            prio;       /* The priority of this command. */
 | 
						|
    uint16_t            dlen;
 | 
						|
    uint16_t            need_ack;   /* If need ack */
 | 
						|
    uint8_t             arg[0];     /* The argument for this command. */
 | 
						|
}iot_em_adp_cmd_arg_t;
 | 
						|
 | 
						|
/* Response data from energy meter adapter layer to smart meter layer. */
 | 
						|
typedef struct _iot_em_adp_command_response_t {
 | 
						|
    iot_em_adp_cid_t    cid;        /* Response command ID. */
 | 
						|
    uint16_t            resp;       /* Return from executer. */
 | 
						|
    uint16_t            dlen;
 | 
						|
    uint16_t            resv;       /* 4 bytes aligned for data[0]. */
 | 
						|
    uint8_t             data[0];    /* Response data from energy meter adapter layer. */
 | 
						|
}iot_em_adp_cmd_resp_t;
 | 
						|
 | 
						|
/* Threshold command, used by config and response operation. */
 | 
						|
typedef struct _iot_em_adp_cmd_threshold_t {
 | 
						|
    iot_em_threshold_type_e thr_type; /* Threshold type is bitmap, indicates which
 | 
						|
                                       * values in threshold struct is effectively. */
 | 
						|
    iot_em_threshold_t      thr_data; /* Threshold data value. */
 | 
						|
}iot_em_adp_cmd_threshold_t;
 | 
						|
 | 
						|
/* Event counter command, used by query operation.
 | 
						|
 * Query event count number of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_event_cnt_query_t {
 | 
						|
    iot_em_time_t timestamp; /* Timestamp for querying event number of a day. */
 | 
						|
}iot_em_adp_cmd_event_cnt_query_t;
 | 
						|
 | 
						|
/* Event counter command, used by response operation.
 | 
						|
 * Response query event count number of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_event_cnt_resp_t {
 | 
						|
    iot_em_time_t timestamp; /* Timestamp for querying event number of a day. */
 | 
						|
    uint32_t      event_num; /* The number of event record for a given day. */
 | 
						|
}iot_em_adp_cmd_event_cnt_resp_t;
 | 
						|
 | 
						|
/* Event record command, used by query operation.
 | 
						|
 * Query event record of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_event_record_query_t {
 | 
						|
    iot_em_time_t timestamp;  /* Timestamp for querying event record of a day. */
 | 
						|
    uint32_t      start_idx;  /* Record start index. */
 | 
						|
    uint32_t      record_num; /* The number of record to get(effective value: 1~5). */
 | 
						|
}iot_em_adp_cmd_event_record_query_t;
 | 
						|
 | 
						|
/* Event record command, used by response operation.
 | 
						|
 * Response query event record of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_event_record_resp_t {
 | 
						|
    uint32_t              start_idx;     /* Record start index. */
 | 
						|
    uint32_t              record_num;    /* Number of records reported this time.*/
 | 
						|
    iot_em_event_record_t record_buf[0]; /* Buffer that stores records. */
 | 
						|
}iot_em_adp_cmd_event_record_resp_t;
 | 
						|
 | 
						|
/* Load counter command, used by query operation.
 | 
						|
 * Query load count number of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_load_cnt_query_t {
 | 
						|
    iot_em_time_t timestamp;  /* Timestamp for querying load record number of a day. */
 | 
						|
}iot_em_adp_cmd_load_cnt_query_t;
 | 
						|
 | 
						|
/* Load counter command, used by response operation.
 | 
						|
 * Response query load count number of the day on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_load_cnt_resp_t {
 | 
						|
    iot_em_time_t timestamp;  /* Timestamp for querying load record number of a day. */
 | 
						|
    uint32_t      load_num;   /* The number of load record for a given day. */
 | 
						|
}iot_em_adp_cmd_load_cnt_resp_t;
 | 
						|
 | 
						|
/* Load record command, used by query operation.
 | 
						|
 * Query load record on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_load_record_query_t {
 | 
						|
    iot_em_time_t timestamp;  /* Start timestamp for querying load record. */
 | 
						|
    uint32_t      start_idx;  /* Record start index. */
 | 
						|
    uint32_t      record_num; /* The number of record to get(effective value: 1~5). */
 | 
						|
}iot_em_adp_cmd_load_record_query_t;
 | 
						|
 | 
						|
/* Load record command, used by response operation.
 | 
						|
 * Response query load record on timestamp.
 | 
						|
 */
 | 
						|
typedef struct _iot_em_adp_cmd_load_record_resp_t {
 | 
						|
    iot_em_time_t        start_time;    /* Start timestamp for querying load record. */
 | 
						|
    uint32_t             req_record_num;/* Number of records that app required. */
 | 
						|
    uint32_t             record_num;    /* Number of records reported this time.*/
 | 
						|
    iot_em_load_record_t record_buf[0]; /* Buffer that stores records. */
 | 
						|
}iot_em_adp_cmd_load_record_resp_t;
 | 
						|
 | 
						|
/* Meter bottom value command, used by config and response operation. */
 | 
						|
typedef struct _iot_em_adp_cmd_meter_bottom_val_t {
 | 
						|
    uint32_t meter_bottom_val; /* Meter bottom value. */
 | 
						|
}iot_em_adp_cmd_meter_bottom_val_t;
 | 
						|
 | 
						|
/* Realtime load data command, only used by response operation. */
 | 
						|
typedef struct _iot_em_adp_cmd_realtime_load_data_t {
 | 
						|
    iot_em_realtime_load_data_t data; /* Realtime load data. */
 | 
						|
}iot_em_adp_cmd_realtime_load_data_t;
 | 
						|
 | 
						|
typedef uint32_t(*smart_meter_resp_cb)(iot_pkt_t * pkt);
 | 
						|
 | 
						|
/* Initialize the energy meter adapter module. */
 | 
						|
uint32_t iot_em_adp_task_init(void);
 | 
						|
 | 
						|
/* API for de-initializing module. */
 | 
						|
void iot_em_adp_task_deinit(void);
 | 
						|
 | 
						|
/* API function for smart meter layer to send cmd to energy meter adapter task. */
 | 
						|
uint32_t iot_em_adp_cmd_send_mssage(iot_pkt_t *arg);
 | 
						|
 | 
						|
/* Smart meter layer registed callback. Energy meter adapter layer use to
 | 
						|
 * response command previous received. Cmd-Resp works as ping-pong method.
 | 
						|
 * Hook interface with smart_meter layer.
 | 
						|
 */
 | 
						|
uint32_t iot_em_adp_smart_meter_register(smart_meter_resp_cb smart_meter_cb);
 | 
						|
 | 
						|
#endif
 |