Files
kunlun/export/inc/sg_lib/proto_3761.h
2024-09-28 14:24:04 +08:00

316 lines
11 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_3761_H
#define PROTO_3761_H
#include "os_types_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 */
/* frame start char definition */
#define PROTO_3761_START_CHAR (0x68)
/* frame end char definition */
#define PROTO_3761_END_CHAR (0x16)
/* define administrative region code length */
#define PROTO_3761_AREA_CODE_LEN (2)
/* broadcast address of device(terminal) */
#define PROTO_3761_BCAST_DEV_ADRR 0xFFFF
/* define proto 3761 pass word length */
#define PROTO_3761_PW_LEN 16
/* define seq number max value */
#define PROTO_3761_SN_MAX 0xF
/* definition of function code of starting station */
#define PROTO_3761_FN_P_RESET_CMD 1
#define PROTO_3761_FN_P_USER_DATA 4
#define PROTO_3761_FN_P_REQ_LINK_TEST 9
#define PROTO_3761_FN_P_REQ_CLASS1_DATA 10
#define PROTO_3761_FN_P_REQ_CLASS2_DATA 11
/* definition of function code of slave station */
#define PROTO_3761_FN_S_CONFIRM 0
#define PROTO_3761_FN_S_RSP_USER_DATA 8
#define PROTO_3761_FN_S_RSP_NON_DATA 9
#define PROTO_3761_FN_S_RSP_LINK_TEST 11
/* define message direction */
#define PROTO_3761_DIR_SLAVE 1
#define PROTO_3761_DIR_MASTER 0
/* define info point and info type length */
#define PROTO_3761_PN_LEN 2
#define PROTO_3761_FN_LEN 2
/* some definitions of information points */
#define PROTO_3761_PN_0 (0)
#define PROTO_3761_PN_MAX (2040)
#define PROTO_3761_PN_ALL (0xFF00)
/* app function number definition */
#define PROTO_3761_AFN_10 (0x10)
/* define communication port number */
#define PROTO_3761_PORT_RS485_1 1
#define PROTO_3761_PORT_RS485_2 2
#define PROTO_3761_PORT_LOCAL_COMM 31
/* define the number of UART data bits */
#define PROTO_3761_UART_DATA_5_BIT 0
#define PROTO_3761_UART_DATA_6_BIT 1
#define PROTO_3761_UART_DATA_7_BIT 2
#define PROTO_3761_UART_DATA_8_BIT 3
/* define the number of UART stop bits */
#define PROTO_3761_UART_STOP_1_BIT 0
#define PROTO_3761_UART_STOP_2_BIT 1
/* define UART baud id */
#define PROTO_3761_UART_BAUD_ID_300 0
#define PROTO_3761_UART_BAUD_ID_600 1
#define PROTO_3761_UART_BAUD_ID_1200 2
#define PROTO_3761_UART_BAUD_ID_2400 3
#define PROTO_3761_UART_BAUD_ID_4800 4
#define PROTO_3761_UART_BAUD_ID_7200 5
#define PROTO_3761_UART_BAUD_ID_9600 6
#define PROTO_3761_UART_BAUD_ID_19200 7
#define PROTO_3761_UART_BAUD_ID_MAX 8
/* protocol 3761 header layout */
typedef struct _proto_3761_hdr {
/* start character 1 */
uint8_t start_char1;
/* protocol coding 1, it must be 2 */
uint16_t proto_code1 : 2,
/* data length 1 */
len1 : 14;
/* protocol coding 2, it must be 2 */
uint16_t proto_code2 : 2,
/* data length 2 */
len2 : 14;
/* start character 2 */
uint8_t start_char2;
/* data payload */
uint8_t data[0];
} proto_3761_hdr_t;
/* control field defined */
typedef struct _proto_3761_ctrl {
/* function code, see PROTO_3761_FN_XXXX */
uint8_t fn : 4,
/* up load: rsvd.
* down link: frame connter valid bit.
*/
fcv : 1,
/* up load:
* request access bit: 1 - there are important events requesting access,
* 0 - not.
* down link:
* frame connter bit.
*/
fcb_acd : 1,
/* start flag bit: 1 - from starting station, 0 - from slave station */
prm : 1,
/* message direction, see PROTO_3761_DIR_XXX.
* 0 - downlink from main station, 1 - up to main station.
*/
dir : 1;
} proto_3761_ctrl_t;
/* address field defined */
typedef struct _proto_3761_addr {
/* administrative region code - BCD */
uint8_t area_code[PROTO_3761_AREA_CODE_LEN];
/* terminal device address */
uint16_t dev_addr;
/* group address identification */
uint8_t is_group :1,
/* master addr */
msa :7;
} proto_3761_addr_t;
/* user data layout */
typedef struct _proto_3761_user_data {
/* control field */
proto_3761_ctrl_t ctrl;
/* address field */
proto_3761_addr_t addr_filed;
/* data payload */
uint8_t data[0];
} proto_3761_user_data_t;
/* application header layout */
typedef struct proto_3761_app_hdr {
/* application function code */
uint8_t afn;
/* seq number */
uint8_t seq :4,
/* flag to mark if need to confirm */
con :1,
/* flag to mark if finish frame */
fin :1,
/* flag to mark if first frame */
fir :1,
/* Time label valid flag */
tpv :1;
/* data payload */
uint8_t data[0];
} proto_3761_app_hdr_t;
/* data unit layout */
typedef struct proto_3761_data_uint {
/* data unit identification - information point */
uint8_t pn[PROTO_3761_PN_LEN];
/* data unit identification - information type */
uint8_t fn[PROTO_3761_FN_LEN];
/* data unit */
uint8_t data[0];
} proto_3761_data_uint_t;
/* protocol 3761 tail layout */
typedef struct _proto_3761_tail {
/* check sum */
uint8_t cs;
/* end character */
uint8_t end_char;
} proto_3761_tail_t;
/* AFN = 10H, FN = 1: Transparent forwarding downlink message layout */
typedef struct proto_3761_afn10f1_dl_t {
/* communication port number, see PROTO_3761_PORT_XXX */
uint8_t port;
/* uart frame format -- data bit num, see PROTO_3761_UART_DATA_X_BIT */
uint8_t data_bit_num : 2,
/* uart frame format -- parity flag: 1 - odd, 0 - even */
parity : 1,
/* parity bit valid flag */
parity_valid : 1,
/* uart frame format -- stop bit num, see PROTO_3761_UART_STOP_X_BIT */
stop_bit_num : 1,
/* baud id, see PROTO_3761_UART_BAUD_ID_XXX */
buad_id : 3;
/* timeout waiting for message */
uint8_t timeout1 : 7,
/* if set, the timeout1 of a field is 1s */
uint_is_sec : 1;
/* timeout per byte interval, uint is 10 ms */
uint8_t timeout2;
/* data length to be forwarded */
uint16_t len;
/* data payload to be forwarded */
uint8_t data[0];
} proto_3761_afn10f1_dl_t;
/* AFN = 10H, FN = 1: transparent forwarding uplink message layout */
typedef struct proto_3761_afn10f1_ul {
/* communication port number */
uint8_t port;
/* data length to be forwarded */
uint16_t len;
/* data payload to be forwarded */
uint8_t data[0];
} proto_3761_afn10f1_ul_t;
#pragma pack(pop) /* save the pack status */
/* app message descriptor */
typedef struct proto_3761_app_desc {
/* address field */
proto_3761_addr_t addr_filed;
/* start flag bit: 1 - from starting station, 0 - from slave station */
uint8_t prm : 1,
/* message direction, see PROTO_3761_DIR_XXX.
* 0 - downlink from main station, 1 - up to main station.
*/
dir : 1,
/* reserved for further use */
rsvd : 6;
/* application function code */
uint8_t afn;
/* seq number */
uint8_t seq;
/* pointer to app data*/
uint8_t *data;
/* data length */
uint16_t len;
} proto_3761_app_data_desc_t;
/**
* @brief proto_3761_parse() - check and parse 376.1 data frame
* @param data: pointer to the start of the original data frame.
* @param len: len len the original data frame.
* @param app_desc: if the app info description is returned successfully.
* @retval: 0 -- for success case, otherwise -- error code
*/
uint32_t proto_3761_parse(uint8_t *data, uint16_t len,
proto_3761_app_data_desc_t *app_desc);
/**
* @brief proto_3761_du_parse() - parse data unit
* @param pn: info point.
* @param fn: info type.
* @param data: pointer to pointer to the start of the original data.
* @param len: pointer to the len of the original data.
* @retval: 0 -- for success case, otherwise -- error code
*/
uint32_t proto_3761_du_parse(uint16_t *pn, uint16_t *fn, uint8_t **data,
uint16_t *len);
/**
* @brief proto_3761_build_afn10f01_dl_msg() - build create an afn19f01(
* transparent forwarding) downlink message.
* @param ds: pointer to data to be transparently forwarded.
* @param len: data len to be transparently forwarded.
* @param buud: baud for transparent forwarding.
* @param port: port number of transparent forwarding, see PROTO_PORT_RS485_X.
* @param parity: uart frame format - parity bit type, see IOT_UART_PARITY_XX.
* @param timeout: timeout for transparent forwarding to wait for response, uint
* is 1s.
* @param mid: module id of Creator.
* @param dev_addr: administrative region code.
* @param area_code: terminal address region.
* @param sn: maessage seq number.
* @retval: NULL -- for failure.
* @retval: pkt -- the iot pkt buffer for message.
*/
iot_pkt_t *proto_3761_build_afn10f01_dl_msg(uint8_t *ds,
uint16_t len, uint32_t baud, uint8_t port,
uint8_t parity, uint8_t timeout, module_id_t mid,
uint16_t *dev_addr, uint16_t *area_code, uint8_t sn);
/**
* @brief proto_3761_get_sn() - get and update message seq number
* @retval: message seq number.
*/
uint8_t proto_3761_get_sn(void);
#ifdef __cplusplus
}
#endif
#endif /* PROTO_3761_H */