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

544 lines
21 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 PROTO_645_EXT_H
#define PROTO_645_EXT_H
#include "os_types_api.h"
#include "iot_utils_api.h"
#include "iot_pkt_api.h"
#include "iot_smart_meter.h"
#ifdef __cplusplus
extern "C" {
#endif
#define PROTO_645_EXT_DEBUG 0
/* pack for the structures in the whole file */
#pragma pack(push) /* save the pack status */
#pragma pack(1) /* 1 byte align */
/* Invalid di */
#define PROTO_645_EXT_INVALID_DI (0xffffffffu)
/* extended data item of extended 645-2007 frame */
/* read value of current and paramter of power threshold */
#define PROTO_645_EXT_CURRENT_AND_POWER_THR_DI (0x04400050u)
/* read counter of event in someday */
#define PROTO_645_EXT_EVT_COUNTER_DI (0x04400055u)
/* read record of event in someday */
#define PROTO_645_EXT_EVT_RECORD_DI (0x04400056u)
/* read diagram of load */
#define PROTO_645_EXT_LOAD_DIAGRAM_DI (0x04400057u)
/* read real time data */
#define PROTO_645_EXT_REAL_TIME_DATA_DI (0x04400058u)
/* write value of meter bottom */
#define PROTO_645_EXT_METER_BOTTOM_VALUE_DI (0x0440002Eu)
/* data identification length for DL/T 645-2007 */
#define PROTO_645_2007_EXT_DI_LEN (4)
/* extend control function code definition for DL/T 645-2007 */
#define PROTO_645_2007_EXT_FN_READ_ADDR (0x13)
#define PROTO_645_2007_EXT_FN_READ_DATA (0x11)
#define PROTO_645_2007_EXT_FN_WRITE_DATA (0x14)
/* frame start char definition */
#define PROTO_645_EXT_START_CHAR (0x68)
/* frame end char definition */
#define PROTO_645_EXT_END_CHAR (0x16)
/* preamble char definition */
#define PROTO_645_EXT_PREAMBLE_CHAR (0xFE)
/* preamble code length */
#define PROTO_645_EXT_PREAMBLE_LEN (4)
/* Message direction: slave->master */
#define PROTO_645_EXT_DIR_TO_MASTER (1)
/* Message direction: master->slave */
#define PROTO_645_EXT_DIR_TO_SLAVE (0)
/* Ack flag: means abnormal ack */
#define PROTO_645_EXT_ACK_ABNORMAL (1)
/* Ack flag: means normal ack */
#define PROTO_645_EXT_ACK_NORMAL (0)
/* Follow data flag: means following data available */
#define PROTO_645_EXT_FOLLOW_AVAILABLE (1)
/* Follow data flag: means no following data */
#define PROTO_645_EXT_FOLLOW_INVALID (0)
#define proto_645_2007_ext_di_to_byte(di,ptr) \
do { \
((uint8_t *)(ptr))[3] = (uint8_t)((di) >> 24); \
((uint8_t *)(ptr))[2] = (uint8_t)((di) >> 16); \
((uint8_t *)(ptr))[1] = (uint8_t)((di) >> 8); \
((uint8_t *)(ptr))[0] = (uint8_t)((di) & 0xff); \
} while(0)
/* structure define area */
typedef struct _proto_645_ext_ctrl_t {
uint8_t fn :5, /* function code. see
* PROTO645_2007(1997)_FN_XXX
*/
follow :1, /* follow flag. 0 means no following data.
* 1 means following data available.
* see PROTO_645_FOLLOW_XXX
*/
ack :1, /* ack flag. 0 means normal ack.
* 1 means abnormal ack. see PROTO_645_ACK_XXX
*/
dir :1; /* direction flag. 1 means from primary
* to secondary. 0 means from secondary
* to primary,see PROTO_645_DIR_XXX
*/
} proto_645_ext_ctrl_t;
typedef struct _proto_645_ext_header_t {
uint8_t start_char_1; /* start char,
* see 645_START_CHAR
*/
uint8_t addr[IOT_MAC_ADDR_LEN]; /* target mac address */
uint8_t start_char_2; /* start char,
* see 645_START_CHAR
*/
proto_645_ext_ctrl_t control; /* control code */
uint8_t len; /* data length */
uint8_t data[0]; /* data */
} proto_645_ext_header_t;
typedef struct _proto_645_ext_tailer_t {
/* check sum */
uint8_t cs;
/* end char. see 645_END_CHAR */
uint8_t end_char;
} proto_645_ext_tailer_t;
static inline uint32_t proto_645_2007_ext_byte_to_di(uint8_t *ptr)
{
return ((((uint8_t *)(ptr))[3]<<24)
|(((uint8_t *)(ptr))[2]<<16)
|(((uint8_t *)(ptr))[1]<<8)
|((uint8_t *)(ptr))[0]);
}
typedef enum _proto_645_ext_event_e {
/* Event type, used to report the event. */
PROTO_645_EXT_EVENT_DEV_POWER_ON = 0x0,/* Device power on. */
PROTO_645_EXT_EVENT_DEV_WORK_START = 0x1,/* Device start working. */
PROTO_645_EXT_EVENT_DEV_WORK_STOP = 0x2,/* Device stop working. */
PROTO_645_EXT_EVENT_DEV_POWER_OFF = 0x3,/* Device power off. */
PROTO_645_EXT_EVENT_DEV_TRAP_START = 0x4,/* Device work in trap. */
PROTO_645_EXT_EVENT_DEV_TRAP_STOP = 0x5,/* Device restore from trap. */
PROTO_645_EXT_EVENT_INVALID
}proto_645_ext_event_e;
typedef struct _proto_645_ext_corr_time_t {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint8_t year;
} proto_645_ext_corr_time_t;
typedef struct _proto_645_07_ext_time_t {
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t week;
uint8_t day;
uint8_t month;
uint8_t year;
} proto_645_07_ext_time_t;
typedef struct _proto_645_07_ext_v {
/* voltage.use 2 bytes of BCD code to represent the voltage.
* for example, 220.0V uses 2 bytes of BCD code to represent
* the following 0x22, 0x00. */
uint8_t tenth:4; /* one-tenth unit. */
uint8_t one:4; /* ont unit. */
uint8_t ten:4; /* ten unit. */
uint8_t hundred:4; /* hundred unit. */
} proto_645_07_ext_v_t;
typedef struct _proto_645_07_ext_thrphase_v_t {
/* Concentrators support three-phase voltages and recently
* use only a single term. */
proto_645_07_ext_v_t A;
proto_645_07_ext_v_t B;
proto_645_07_ext_v_t C;
} proto_645_07_ext_thrphase_v_t;
typedef struct _proto_645_07_ext_a {
/* current.use 3 bytes of BCD code to represent the current.
* for example, 123.456A uses 3 bytes of BCD code to represent
* the following 0x12, 0x34, 0x56. */
uint8_t thousandth:4; /* one-thousandth unit. */
uint8_t hundredth:4; /* one-hundredth unit. */
uint8_t tenth:4; /* ten unit. */
uint8_t one:4; /* one unit. */
uint8_t ten:4; /* ten unit. */
uint8_t hundred:3; /* hundred unit. */
uint8_t s:1; /* sign bit, 1 for negative. */
} proto_645_07_ext_a_t;
typedef struct _proto_645_07_ext_thrphase_a_t {
/* Concentrators support three-phase currents and recently
* use only a single term. */
proto_645_07_ext_a_t A;
proto_645_07_ext_a_t B;
proto_645_07_ext_a_t C;
} proto_645_07_ext_thrphase_a_t;
typedef struct _proto_645_07_ext_p {
/* total power. use 3 bytes of BCD code to represent the active
* power. for example, 12.3456kW uses 3 bytes of BCD code to represent
* the following 0x12, 0x34, 0x56. */
uint8_t ten_thousandth:4; /* ten_thousandth unit. */
uint8_t thousandth:4; /* one-hundredth unit. */
uint8_t hundredth:4; /* one-hundredth unit. */
uint8_t tenth:4; /* one-tenth unit. */
uint8_t one:4; /* one unit. */
uint8_t ten:2; /* ten unit. */
uint8_t d:1; /* carry bit, 1 for need multiply by 10. */
uint8_t s:1; /* sign bit, 1 for negative. */
} proto_645_07_ext_p_t;
typedef struct _proto_645_07_ext_thrphase_p_t {
/* Concentrators support three-phase power and recently
* use only a single term. */
proto_645_07_ext_p_t total;
proto_645_07_ext_p_t A;
proto_645_07_ext_p_t B;
proto_645_07_ext_p_t C;
} proto_645_07_ext_thrphase_p_t;
typedef struct _proto_645_07_ext_pf {
/* total power factor.use 2 bytes of BCD code to represent the power
* factor. for example, 0.999 uses 2 bytes of BCD code to represent
* the following 0x09, 0x99. */
uint8_t tenth:4; /* one-tenth unit. */
uint8_t one:4; /* one unit. */
uint8_t ten:4; /* ten unit. */
uint8_t hundred:3; /* hundred unit. */
uint8_t s:1; /* sign bit, 1 for negative. */
} proto_645_07_ext_pf_t;
typedef struct _proto_645_07_ext_thrphase_pf_t {
/* Concentrators support three-phase power factor and recently
* use only a single term. */
proto_645_07_ext_pf_t total;
proto_645_07_ext_pf_t A;
proto_645_07_ext_pf_t B;
proto_645_07_ext_pf_t C;
} proto_645_07_ext_thrphase_pf_t;
typedef struct _proto_645_07_ext_w {
/* use 4 bytes of BCD code to represent bottom energy value. for example,
* 123456.78KWh uses 4 bytes of BCD code to represent the following
* 0x12, 0x34, 0x56, 0x78.
*/
uint8_t hundredth:4; /* one-hundredth unit. */
uint8_t tenth:4; /* one-tenth unit. */
uint8_t one:4; /* one unit. */
uint8_t ten:4; /* ten unit. */
uint8_t hundred:4; /* hundred unit. */
uint8_t thousand:4; /* thousand unit. */
uint8_t ten_thousand:4; /* ten_thousand unit. */
uint8_t hundred_thousand:4; /* hundred_thousand unit. */
} proto_645_07_ext_w_t;
/* Item of each power threshold item. */
typedef struct _proto_645_ext_power_record_item_t {
proto_645_07_ext_thrphase_v_t v; /* Voltage, V. */
proto_645_07_ext_thrphase_a_t i; /* Electrical current, A. */
proto_645_07_ext_thrphase_p_t p; /* Power, KW. */
proto_645_07_ext_thrphase_pf_t pf; /* Power factor, %. */
proto_645_07_ext_w_t w; /* Work(meter bottom value), KWH.
*/
}proto_645_ext_power_record_item_t;
/* Describes password field of 645_ext write data field. */
typedef struct _proto_645_ext_write_data_passwd_t {
uint8_t A; /* Password permission. */
uint8_t B;
uint8_t C;
} proto_645_ext_write_data_passwd_t;
/* Describes 645_ext write data field information. */
typedef struct _proto_645_ext_write_data_fields_t {
uint8_t pass_perm; /* Password permission. */
proto_645_ext_write_data_passwd_t passwd; /* Password. */
uint32_t operator_code; /* Operator code. */
} proto_645_ext_write_data_fields_t;
/* Response read current and power limit parameters. */
typedef struct _proto_645_ext_resp_threashold_t {
proto_645_07_ext_a_t i_standby; /* Standby current threshold, A. */
proto_645_07_ext_a_t i_work; /* Working current threshold, A. */
proto_645_07_ext_p_t p_trap; /* Traped power threshold, KW. */
} proto_645_ext_resp_threashold_t;
/* Response read event counter. */
typedef struct _proto_645_ext_resp_event_counter_t {
proto_645_ext_corr_time_t start_time; /* The time of start querrying. */
uint16_t event_counter; /* Number of events queried. */
} proto_645_ext_resp_event_counter_t;
/* Item of each reported event record. */
typedef struct _proto_645_ext_event_record_item_t {
uint8_t type; /* Event record type. */
proto_645_ext_corr_time_t etime; /* Event record timestamp. */
proto_645_ext_power_record_item_t event_record_item; /* event record item. */
}proto_645_ext_event_record_item_t;
/* Response read event record. */
typedef struct _proto_645_ext_resp_event_record_t {
uint16_t record_start_index; /* Start number of records.*/
uint16_t record_num; /* The number of records reported this time. */
proto_645_ext_event_record_item_t record_data[0]; /* event record data. */
}proto_645_ext_resp_event_record_t;
/* Item of each reported load diagram. */
typedef struct _proto_645_ext_load_diagram_item_t {
proto_645_ext_corr_time_t etime; /* Event record timestamp. */
proto_645_ext_power_record_item_t load_diagram_item; /* Load diagram. */
}proto_645_ext_load_diagram_item_t;
/* Response load diagram. */
typedef struct _proto_645_ext_resp_load_diagram_t {
uint16_t record_num; /* The number of records
* at 15-minute intervals
* starting from the
* starting time.
*/
proto_645_ext_load_diagram_item_t load_diagram[0]; /* data of records. */
}proto_645_ext_resp_load_diagram_t;
/* Response realtime load diagram. */
typedef struct _proto_645_ext_realtime_load_data_t {
proto_645_ext_corr_time_t etime; /* the current time. */
proto_645_ext_power_record_item_t realtime_load_data; /* realtime load
*data.
*/
}proto_645_ext_realtime_load_data_t;
/* Meter bottom value. */
typedef struct _proto_645_ext_bottom_val_t
{
proto_645_07_ext_w_t bottom_val; /* Meter bottom value. */
}proto_645_ext_bottom_val_t;
#pragma pack(pop) /* restore the pack status */
/**
* @brief proto_ext_645_fn_is_extented() - check if function code is extended
* item.
* @param fn: function code.
*
* @return true -- extended data item.
* @return false -- not extended data item.
*/
bool_t proto_ext_645_fn_is_extented(uint32_t fn);
/**
* @brief proto_645_ext_calc_cs() - calculate checksum for 645 protocol.
* @param app_id: pointer to the 645 protocol header.
*
* return
* calculated checksum value.
*/
uint8_t proto_645_ext_calc_cs(proto_645_ext_header_t *hdr);
/**
* @brief proto_645_ext_format_check() - 645 protocol format check.
* @param data: pointer to protocol data buffer.
* @param len: data length.
* @param dir: message direction--see PROTO_645_DIR_XXX
* return
* NULL -- Incorrect protocol format.
* otherwise --pointer to the 645 protocol header.
*/
proto_645_ext_header_t *proto_645_ext_format_check(uint8_t *data,
uint32_t len, uint32_t dir);
/**
* @brief proto_645_ext_sub33_and_get_di() - subtracting 0x33 in the
* 645 protocol data field and get di.
* This function can only be used when
* the data field has di.
* @param data: pointer to data field.
* @param len: length of data field.
* @param di: pointer to di cache.
* @retval: ERR_OK -- get di successfully.
* @retval: ERR_FAIL -- get di fail.
*/
uint32_t proto_645_ext_sub33_and_get_di(uint8_t *data, uint8_t len,
uint32_t *di);
/**
* @brief proto_645_ext_add33_handle() - add 0x33 of the every data in the
* 645 protocol data field.
* @param uint8_t: pointer to data field.
* @param size: length of data field.
*/
void proto_645_ext_add33_handle(uint8_t *ds, uint32_t size);
/**
* @brief proto_645_ext_sub33_handle() - sub 0x33 of the every data in the
* 645 protocol data field.
* @param uint8_t: pointer to data field.
* @param size: length of data field.
*/
void proto_645_ext_sub33_handle(uint8_t *ds, uint32_t size);
/**
* @brief proto_handle_645_ext_req_read_event_counter() - request counter of
* event in someday.
* @param data: point to the request to adp .
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_read_threshold(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_resp_read_threshold() - response read current
* and power limit parameters.
* @param data: point to the response from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_resp_read_threshold(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_read_event_counter() - request counter of
* event in someday.
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_read_event_counter(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_resp_read_event_counter() - response counter
* of event in someday.
* @param data: point to the response from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_resp_read_event_counter(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_read_event_record() - request record of event
* in someday.
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_read_event_record(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_resp_read_event_record() - response record of
* event in someday.
* @param data: point to the response from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_resp_read_event_record(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_read_load_diagram() - request diagram of load.
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_read_load_diagram(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_resp_read_load_diagram() - response diagram
* of load.
* @param data: point to the response from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_resp_read_load_diagram(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_read_realtime_load_data() - request
* real time data.
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_read_realtime_load_data(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_resp_read_realtime_load_data() - response real
* time datay.
* @param data: point to the response from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_resp_read_realtime_load_data(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_write_threshold() - request write current
* and power limit parameters
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_write_threshold(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_confirm_write_threshold() - process confirmation
* from adp.
* @param data: point to the confirm status from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_confirm_write_threshold(
uint8_t* data, uint32_t data_len);
/**
* @brief proto_handle_645_ext_req_write_bottom_value() - request write
* bottom value.
* @param data: point to the request to adp.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_req_write_bottom_value(
uint8_t* data, uint8_t data_len);
/**
* @brief proto_handle_645_ext_confirm_write_threshold() - process confirmation
* from adp.
* @param data: point to the confirm status from adp to app.
* @param data_len: the length of data.
*/
void proto_handle_645_ext_confirm_write_bottom_value(
uint8_t* data, uint32_t data_len);
#ifdef __cplusplus
}
#endif
#endif /* PROTO_645_EXT_H */