316 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			316 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
Copyright(c) 2024 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_HX_DLMS_H
 | 
						|
#define PROTO_HX_DLMS_H
 | 
						|
 | 
						|
#include "os_types_api.h"
 | 
						|
#include "iot_utils_api.h"
 | 
						|
#include "iot_pkt_api.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 */
 | 
						|
 | 
						|
/* default baud rate for Q/HX-J011-04/02-2015 */
 | 
						|
#define PROTO_HX_DLMS_DEFAULT_BAUD         9600
 | 
						|
#define PROTO_HX_DLMS_START_BYTE           0x7e
 | 
						|
#define PROTO_HX_DLMS_END_BYTE             0x7e
 | 
						|
#define PROTO_HX_DLMS_START_END_LEN        (2)
 | 
						|
#define PROTO_HX_DLMS_FCS_LEN              (2)
 | 
						|
#define PROTO_HX_DLMS_FRAME_MODULE_REQ     (5)
 | 
						|
#define PROTO_HX_DLMS_FRAME_TRANS_REQ      (12)
 | 
						|
#define PROTO_HX_DLMS_LOGICAL_MODULE_ADDR  0x7c
 | 
						|
#define PROTO_HX_DLMS_LOGICAL_METER_ADDR   0x01
 | 
						|
#define PROTO_HX_DLMS_SEQ_MAX              7
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_GET_REQ       0xC0
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_GET_RSP       0xC4
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_SET_REQ       0xC1
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_SET_RSP       0xC5
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_ACTION_REQ    0xC3
 | 
						|
#define PROTO_HX_DLMS_CMD_ID_ACTION_RSP    0xC7
 | 
						|
#define PROTO_HX_DLMS_CMD_DESC_NORMAL      0x01
 | 
						|
#define PROTO_HX_DLMS_RESULT_DATA          0x00
 | 
						|
#define PROTO_HX_DLMS_RESULT_ACCESS        0x01
 | 
						|
 | 
						|
/* data type definition, see Q/HX-J011-04/02-2015 */
 | 
						|
#define PROTO_HX_DLMS_DATA_TYPE_STRUCTURE  0x02
 | 
						|
#define PROTO_HX_DLMS_DATA_TYPE_BIT_STR    0x04
 | 
						|
#define PROTO_HX_DLMS_DATA_TYPE_OCTET_STR  0x09
 | 
						|
#define PROTO_HX_DLMS_DATA_TYPE_UINT8      0x11
 | 
						|
#define PROTO_HX_DLMS_DATA_TYPE_ENUM       0x16
 | 
						|
 | 
						|
/* definition network state */
 | 
						|
#define PROTO_HX_DLMS_NET_STATE_OFFLINE    0x01
 | 
						|
#define PROTO_HX_DLMS_NET_STATE_ONLINE     0x02
 | 
						|
 | 
						|
/* definition signal strength max */
 | 
						|
#define PROTO_HX_DLMS_SIGNAL_STRENGTH_MAX  0x07
 | 
						|
 | 
						|
/* definition HX DLMS state sync len */
 | 
						|
#define PROTO_HX_DLMS_STATE_SYNC_LEN       0x22
 | 
						|
/* definition HX DLMS module version len */
 | 
						|
#define PROTO_HX_DLMS_MODULE_VER_LEN       0x04
 | 
						|
/* definition HX DLMS cco mac len */
 | 
						|
#define PROTO_HX_DLMS_CCO_MAC_LEN          0x0C
 | 
						|
 | 
						|
/* define HX DLMS event status word */
 | 
						|
#define PROTO_HX_DLMS_STATUS_BITMAP_LENGTH (8)
 | 
						|
#define PROTO_HX_DLMS_STATUS_EVENT_FLAG    (0x80)
 | 
						|
#define PROTO_HX_DLMS_STATUS_PTA_FLAG      (0x40)
 | 
						|
#define PROTO_HX_DLMS_STATUS_IR_FLAG       (0x20)
 | 
						|
 | 
						|
/* define HX DLMS event status words comm media type */
 | 
						|
#define PROTO_HX_DLMS_COMM_MEDIA_PRIME     (0x00)
 | 
						|
#define PROTO_HX_DLMS_COMM_MEDIA_G3PLC     (0x01)
 | 
						|
 | 
						|
/* define HX DLMS event push client values */
 | 
						|
#define PROTO_HX_DLMS_EVENT_PUSH_CLIENT_MANAGE      (0x01)
 | 
						|
#define PROTO_HX_DLMS_EVENT_PUSH_CLIENT_READING     (0x02)
 | 
						|
#define PROTO_HX_DLMS_EVENT_PUSH_CLIENT_PUBLIC      (0x10)
 | 
						|
#define PROTO_HX_DLMS_EVENT_PUSH_CLIENT_PRECONNECT  (0x66)
 | 
						|
 | 
						|
#define PROTO_HX_DLMS_EVENT_STR_LEN_NUM_MAX         (2)
 | 
						|
#define PROTO_HX_DLMS_EVENT_DATA_STRUCT_NUM         (5)
 | 
						|
 | 
						|
union proto_hx_dlms_seq
 | 
						|
{
 | 
						|
    struct {
 | 
						|
        /* frame sequence */
 | 
						|
        uint8_t frame_seq : 3,
 | 
						|
        /* serial id */
 | 
						|
                serial_id : 3,
 | 
						|
        /* reserved for future */
 | 
						|
                rsvd      : 2;
 | 
						|
    } bit;
 | 
						|
    uint8_t value;
 | 
						|
};
 | 
						|
 | 
						|
/* frame head info of HX DLMS protocol link layer frame */
 | 
						|
typedef struct _proto_hx_dlms_head {
 | 
						|
    /* start char, see PROTO_HX_DLMS_START_BYTE */
 | 
						|
    uint8_t start_char;
 | 
						|
    /* frame length except start and end byte */
 | 
						|
    uint8_t len_h     :  4,
 | 
						|
    /* type of the frame, see PROTO_HX_DLMS_FRAME_XXX */
 | 
						|
            frame_type:  4;
 | 
						|
    /* frame length except start and end byte */
 | 
						|
    uint8_t len_l:       8;
 | 
						|
    /* sequence format */
 | 
						|
    union proto_hx_dlms_seq seq;
 | 
						|
    /* destination logical address */
 | 
						|
    uint8_t dst_addr;
 | 
						|
    /* source logical address */
 | 
						|
    uint8_t src_addr;
 | 
						|
    /* data */
 | 
						|
    uint8_t data[0];
 | 
						|
} proto_hx_dlms_head_t;
 | 
						|
 | 
						|
/* frame tail info of HX DLMS protocol link layer frame */
 | 
						|
typedef struct _proto_hx_dlms_tail {
 | 
						|
    /* check sum */
 | 
						|
    uint8_t fcs[PROTO_HX_DLMS_FCS_LEN];
 | 
						|
    /* end char. see PROTO_DLMS_HDLC_END_BYTE */
 | 
						|
    uint8_t end_char;
 | 
						|
} proto_hx_dlms_tail_t;
 | 
						|
 | 
						|
/* frame head info of HX HPLC app protocol link layer frame */
 | 
						|
typedef struct _proto_hx_dlms_app_hdr {
 | 
						|
    /* version, default is 0x0001, big endian data*/
 | 
						|
    uint16_t ver;
 | 
						|
    /* source logical address, big endian data */
 | 
						|
    uint16_t src_addr;
 | 
						|
    /* destination logical address, big endian data */
 | 
						|
    uint16_t dst_addr;
 | 
						|
    /* length of data, big endian data */
 | 
						|
    uint16_t len;
 | 
						|
    /* data */
 | 
						|
    uint8_t data[0];
 | 
						|
} proto_hx_dlms_app_hdr_t;
 | 
						|
 | 
						|
/* structure of DLMS HDLC link layer frame
 | 
						|
 -------------
 | 
						|
 |----7EH-------------|  start of frame, 0x7E.
 | 
						|
 |----frame_format----|  2 byte of frame format. Include length(12 bit)
 | 
						|
 |                    |  and frame type(4 bit).
 | 
						|
 |----seq-------------|  sequence.
 | 
						|
 |----dst address-----|  1 byte of destination logical address.
 | 
						|
 |----src address-----|  1 byte of source logical address
 | 
						|
 |----Data------------|  APDU. length is variable
 | 
						|
 |----FCS-------------|  2 byte of frame
 | 
						|
 |----7EH-------------|  end of frame, 0x7E.
 | 
						|
 -------------
 | 
						|
 */
 | 
						|
 | 
						|
/* frame head info of HX DLMS protocol apdu */
 | 
						|
typedef struct _proto_hx_dlms_apdu_hdr {
 | 
						|
    /* command id, see PROTO_HX_DLMS_CMD_ID_XXX */
 | 
						|
    uint8_t cmd_id;
 | 
						|
    /* command descriptor, see PROTO_HX_DLMS_DESC_XXX */
 | 
						|
    uint8_t cmd_desc;
 | 
						|
    /*  invoke id and priority  */
 | 
						|
    uint8_t inv_id : 4,
 | 
						|
            rsvd   : 2,
 | 
						|
    /* Service confirmation */
 | 
						|
            ser_class:1,
 | 
						|
            pri    : 1;
 | 
						|
    /* command data */
 | 
						|
    uint8_t data[0];
 | 
						|
} proto_hx_dlms_apdu_hdr_t;
 | 
						|
 | 
						|
/* HX DLMS data di */
 | 
						|
typedef struct _proto_hx_dlms_data_di {
 | 
						|
    /* class id, big endian data */
 | 
						|
    uint8_t class_id[2];
 | 
						|
    /* obis, big endian data */
 | 
						|
    uint8_t obis[6];
 | 
						|
    /* attribute */
 | 
						|
    uint8_t attr;
 | 
						|
} proto_hx_dlms_data_di_t;
 | 
						|
 | 
						|
/* HX DLMS data di */
 | 
						|
typedef struct _proto_hx_dlms_rsp_result {
 | 
						|
    /* response result type, 0 mean data, 1 mean access result */
 | 
						|
    uint8_t type;
 | 
						|
    /* result data */
 | 
						|
    uint8_t data[0];
 | 
						|
} proto_hx_dlms_rsp_result_t;
 | 
						|
 | 
						|
/* HX DLMS data */
 | 
						|
typedef struct _proto_hx_dlms_data {
 | 
						|
    /* data type */
 | 
						|
    uint8_t type;
 | 
						|
    /* data length or data include cnt */
 | 
						|
    uint8_t num;
 | 
						|
    /* data content */
 | 
						|
    uint8_t con[0];
 | 
						|
} proto_hx_dlms_data_t;
 | 
						|
 | 
						|
/* HX DLMS data with fix length */
 | 
						|
typedef struct _proto_hx_dlms_data_fix {
 | 
						|
    /* data type */
 | 
						|
    uint8_t type;
 | 
						|
    /* data content */
 | 
						|
    uint8_t data[0];
 | 
						|
} proto_hx_dlms_data_fix_t;
 | 
						|
 | 
						|
/* HX DLMS state sync */
 | 
						|
typedef struct _proto_hx_dlms_state_sync {
 | 
						|
    /* type is PROTO_HX_DLMS_DATA_TYPE_ENUM */
 | 
						|
    uint8_t type1;
 | 
						|
    /* wan type */
 | 
						|
    uint8_t wan_type;
 | 
						|
    /* type is PROTO_HX_DLMS_DATA_TYPE_OCTET_STR */
 | 
						|
    uint8_t type2;
 | 
						|
    /* data len */
 | 
						|
    uint8_t data_len;
 | 
						|
    /* net state, see PROTO_HX_DLMS_NET_STATE_XXX */
 | 
						|
    uint8_t net_state;
 | 
						|
    /* signal strength, see PROTO_HX_DLMS_SIGNAL_STRENGT_XXX */
 | 
						|
    uint8_t signal_strength;
 | 
						|
    /* ver len */
 | 
						|
    uint8_t ver_len;
 | 
						|
    /* ver info */
 | 
						|
    uint8_t ver[PROTO_HX_DLMS_MODULE_VER_LEN];
 | 
						|
    /* cco mac len */
 | 
						|
    uint8_t cco_mac_len;
 | 
						|
    /* cco mac, big endian data */
 | 
						|
    uint8_t cco_mac[PROTO_HX_DLMS_CCO_MAC_LEN];
 | 
						|
} proto_hx_dlms_state_sync_t;
 | 
						|
 | 
						|
#pragma pack(pop)   /* restore the pack status */
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_check - a dlms packet check.
 | 
						|
 * @param data:       packet data
 | 
						|
 * @param len:        length of the packet data
 | 
						|
 * @retval:     NULL      -- parse protocol fail.
 | 
						|
 * @retval:     otherwise -- pointer to the hx dlms protocol header
 | 
						|
 */
 | 
						|
proto_hx_dlms_head_t *proto_hx_dlms_check(uint8_t* data,
 | 
						|
    uint16_t len);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_msg_build - creat a hx dlms message.
 | 
						|
 * @param data:       apdu data
 | 
						|
 * @param len:        length of the apdu data
 | 
						|
 * @param frame_type: type of the frame, see PROTO_HX_DLMS_FRAME_XXX
 | 
						|
 * @param dst_addr:   destination logical address
 | 
						|
 * @param src_addr:   source logical address
 | 
						|
 * @param serial_id:  serial id
 | 
						|
 * @param seq:        sequence of message
 | 
						|
 * @retval:     NULL      -- parse protocol fail.
 | 
						|
 * @retval:     otherwise -- pointer to the hx dlms protocol message.
 | 
						|
 */
 | 
						|
iot_pkt_t *proto_hx_dlms_msg_build(const uint8_t *data, uint16_t len,
 | 
						|
    uint8_t frame_type, uint8_t dst_addr, uint8_t src_addr, uint8_t serial_id,
 | 
						|
    uint8_t seq);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_change_app_to_meter - change message from HX HPLC app
 | 
						|
 *            protocol to HX DLMS protocol.
 | 
						|
 * @param head:       head info of HX HPLC app protocol message
 | 
						|
 * @param len:        length of message
 | 
						|
 * @param serial_id:  serial id
 | 
						|
 * @param seq:        sequence of HX DLMS message
 | 
						|
 * @retval:     NULL      -- parse protocol fail.
 | 
						|
 * @retval:     otherwise -- pointer to the HX DLMS protocol message.
 | 
						|
 */
 | 
						|
iot_pkt_t *proto_hx_dlms_change_app_to_meter(
 | 
						|
    proto_hx_dlms_app_hdr_t *head, uint16_t len, uint8_t serial_id,
 | 
						|
    uint8_t seq);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_change_meter_to_app - change message from HX DLMS
 | 
						|
 *            protocol to HX HPLC app protocol.
 | 
						|
 * @param head: head info of HX DLMS protocol message
 | 
						|
 * @retval:     NULL      -- parse protocol fail.
 | 
						|
 * @retval:     otherwise -- pointer to the HX HPLC app protocol message.
 | 
						|
 */
 | 
						|
iot_pkt_t *proto_hx_dlms_change_meter_to_app(
 | 
						|
    proto_hx_dlms_head_t *head);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_check_frame_handler - check whether the frame
 | 
						|
 *                                          is legitimate.
 | 
						|
 * @param buffer:     the buffer containing data.
 | 
						|
 * @param buffer_len: len of the buffer.
 | 
						|
 * @param is_frame:   false: the frame isn't legitimate.
 | 
						|
 *                    true:  the frame is legitimate.
 | 
						|
 */
 | 
						|
void proto_hx_dlms_check_frame_handler(uint8_t* buffer,
 | 
						|
    uint32_t buffer_len, bool_t* is_frame);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief proto_hx_dlms_change_evt_to_app - change event message from HX DLMS
 | 
						|
 *            protocol to HX HPLC app protocol
 | 
						|
 * @param data:     event data.
 | 
						|
 * @param len:      len of event length.
 | 
						|
 * @retval:     NULL      -- parse protocol fail.
 | 
						|
 * @retval:     otherwise -- pointer to the HX HPLC app protocol message.
 | 
						|
 */
 | 
						|
iot_pkt_t *proto_hx_dlms_change_evt_to_app(uint8_t *data,
 | 
						|
    uint16_t len);
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
#endif /* PROTO_HX_DLMS_H */
 |