482 lines
18 KiB
C
482 lines
18 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_SPG_H
|
|
#define PROTO_SPG_H
|
|
|
|
#include "os_types_api.h"
|
|
#include "iot_utils_api.h"
|
|
#include "iot_sg_fr.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 */
|
|
|
|
/* length of spg packet with data len as 0 */
|
|
#define PROTO_SPG_PKT_OVERHEAD (12)
|
|
|
|
#define PROTO_SPG_FIX_FIELD_LEN (6)
|
|
|
|
#define PROTO_SPG_LENGTH_SIZE (2)
|
|
#define PROTO_SPG_CTRL_OFFSET (PROTO_SPG_LENGTH_SIZE + 1)
|
|
#define PROTO_SPG_DATA_OFFSET (PROTO_SPG_CTRL_OFFSET + 1)
|
|
#define PROTO_SPG_CHECKSUM_LEN (1)
|
|
#define PROTO_SPG_BACKCODE_LEN (1)
|
|
#define PROTO_SPG_PRECODE_LEN (1)
|
|
|
|
#define PROTO_SPG_SOF_BYTE (0x68)
|
|
#define PROTO_SPG_EOF_BYTE (0x16)
|
|
#define PROTO_SPG_DEFAULT_BAUD (9600)
|
|
|
|
#define PROTO_SPG_APP_CCO_DI3 (0xE8)
|
|
#define PROTO_SPG_APP_STA_DI3 (0xEA)
|
|
#define PROTO_SPG_APP_CTRL_DI3 (0xE5)
|
|
|
|
#define PROTO_SPG_APP_DI2_00 (0x00)
|
|
#define PROTO_SPG_APP_DI2_01 (0x01)
|
|
#define PROTO_SPG_APP_DI2_02 (0x02)
|
|
#define PROTO_SPG_APP_DI2_03 (0x03)
|
|
#define PROTO_SPG_APP_DI2_04 (0x04)
|
|
#define PROTO_SPG_APP_DI2_05 (0x05)
|
|
#define PROTO_SPG_APP_DI2_06 (0x06)
|
|
#define PROTO_SPG_APP_DI2_13 (0x13)
|
|
|
|
#define PROTO_SPG_DIR_DOWN_LINK (0)
|
|
#define PROTO_SPG_DIR_UP_LINK (1)
|
|
|
|
#define PROTO_SPG_PRM_MASTER (1)
|
|
#define PROTO_SPG_PRM_SLAVE (0)
|
|
|
|
#define PROTO_SPG_NO_ADDRESS (0)
|
|
#define PROTO_SPG_WITH_ADDRESS (1)
|
|
|
|
#define PROTO_SPG_DEFAULT_VERSION (0)
|
|
|
|
#define PROTO_SPG_MAC_ADDR_LEN (6)
|
|
|
|
/* app function number definition */
|
|
/* CCO and STA support */
|
|
#define PROTO_SPG_AFN_00 (0x00)
|
|
|
|
/* CCO support */
|
|
#define PROTO_SPG_AFN_01 (0x01)
|
|
#define PROTO_SPG_AFN_02 (0x02)
|
|
#define PROTO_SPG_AFN_03 (0x03)
|
|
#define PROTO_SPG_AFN_04 (0x04)
|
|
#define PROTO_SPG_AFN_05 (0x05)
|
|
#define PROTO_SPG_AFN_06 (0x06)
|
|
#define PROTO_SPG_AFN_07 (0x07)
|
|
#define PROTO_SPG_AFN_08 (0x08)
|
|
#define PROTO_SPG_AFN_22 (0x22)
|
|
#define PROTO_SPG_AFN_41 (0x41)
|
|
#define PROTO_SPG_AFN_F0 (0xF0)
|
|
|
|
/* STA support */
|
|
#define PROTO_SPG_AFN_21 (0x21)
|
|
#define PROTO_SPG_AFN_22 (0x22)
|
|
#define PROTO_SPG_AFN_23 (0x23)
|
|
#define PROTO_SPG_AFN_24 (0x24)
|
|
#define PROTO_SPG_AFN_25 (0x25)
|
|
#define PROTO_SPG_AFN_26 (0x26)
|
|
#define PROTO_SPG_AFN_31 (0x31)
|
|
|
|
/* function code for each command */
|
|
#define PROTO_SPG_F1 (0x01)
|
|
#define PROTO_SPG_F2 (0x02)
|
|
#define PROTO_SPG_F3 (0x03)
|
|
#define PROTO_SPG_F4 (0x04)
|
|
#define PROTO_SPG_F5 (0x05)
|
|
#define PROTO_SPG_F6 (0x06)
|
|
#define PROTO_SPG_F7 (0x07)
|
|
#define PROTO_SPG_F8 (0x08)
|
|
#define PROTO_SPG_F9 (0x09)
|
|
#define PROTO_SPG_FA (0x0A)
|
|
#define PROTO_SPG_FB (0x0B)
|
|
#define PROTO_SPG_FC (0x0C)
|
|
#define PROTO_SPG_FD (0x0D)
|
|
#define PROTO_SPG_FE (0x0E)
|
|
#define PROTO_SPG_FF (0x0F)
|
|
#define PROTO_SPG_F10 (0x10)
|
|
#define PROTO_SPG_F11 (0x11)
|
|
#define PROTO_SPG_F12 (0x12)
|
|
#define PROTO_SPG_F13 (0x13)
|
|
#define PROTO_SPG_F14 (0x14)
|
|
#define PROTO_SPG_F15 (0x15)
|
|
#define PROTO_SPG_F16 (0x16)
|
|
#define PROTO_SPG_F20 (0x20)
|
|
#define PROTO_SPG_F21 (0x21)
|
|
#define PROTO_SPG_F22 (0x22)
|
|
#define PROTO_SPG_F23 (0x23)
|
|
#define PROTO_SPG_F24 (0x24)
|
|
#define PROTO_SPG_F65 (0x65)
|
|
#define PROTO_SPG_F66 (0x66)
|
|
#define PROTO_SPG_F67 (0x67)
|
|
#define PROTO_SPG_F68 (0x68)
|
|
#define PROTO_SPG_F69 (0x69)
|
|
#define PROTO_SPG_F70 (0x70)
|
|
#define PROTO_SPG_F80 (0x80)
|
|
#define PROTO_SPG_F81 (0x81)
|
|
#define PROTO_SPG_F85 (0x85)
|
|
#define PROTO_SPG_F90 (0x90)
|
|
#define PROTO_SPG_F91 (0x91)
|
|
#define PROTO_SPG_F93 (0x93)
|
|
#define PROTO_SPG_FB0 (0xB0)
|
|
#define PROTO_SPG_FB1 (0xB1)
|
|
#define PROTO_SPG_FB2 (0xB2)
|
|
#define PROTO_SPG_FC1 (0xC1)
|
|
#define PROTO_SPG_FDE (0xDE)
|
|
#define PROTO_SPG_FE0 (0xE0)
|
|
#define PROTO_SPG_FE1 (0xE1)
|
|
#define PROTO_SPG_FE2 (0xE2)
|
|
#define PROTO_SPG_FF4 (0xF4)
|
|
#define PROTO_SPG_FF5 (0xF5)
|
|
#define PROTO_SPG_FF6 (0xF6)
|
|
|
|
/* di data len */
|
|
#define PROTO_SPG_DI_LEN (4)
|
|
|
|
/* fn count of afn */
|
|
/* CCO and STA support */
|
|
#define PROTO_SPG_AFN00_FN_COUNT (3)
|
|
|
|
/* CCO support */
|
|
#define PROTO_SPG_AFN01_FN_COUNT (4)
|
|
#define PROTO_SPG_AFN02_FN_COUNT (10)
|
|
#define PROTO_SPG_AFN03_FN_COUNT (9)
|
|
#define PROTO_SPG_AFN04_FN_COUNT (7)
|
|
#define PROTO_SPG_AFN05_FN_COUNT (6)
|
|
#define PROTO_SPG_AFN06_FN_COUNT (2)
|
|
#define PROTO_SPG_AFN07_FN_COUNT (6)
|
|
#define PROTO_SPG_AFNF0_FN_COUNT (1)
|
|
|
|
/* STA support */
|
|
#define PROTO_SPG_AFN21_FN_COUNT (6)
|
|
#define PROTO_SPG_AFN22_FN_COUNT (2)
|
|
#define PROTO_SPG_AFN23_FN_COUNT (2)
|
|
#define PROTO_SPG_AFN24_FN_COUNT (5)
|
|
|
|
/* wait 0 seconds by default */
|
|
#define PROTO_SPG_DEFAULT_WAIT_TIME (0)
|
|
/* wait 1 seconds */
|
|
#define PROTO_SPG_1_WAIT_TIME (1)
|
|
/* wait 10 seconds */
|
|
#define PROTO_SPG_10_WAIT_TIME (10)
|
|
/* wait 5 * 60 seconds for setting band */
|
|
#define PROTO_SPG_SET_BAND_WAIT_TIME (5 * 60)
|
|
|
|
/* error no for reject packet */
|
|
#define PROTO_SPG_ERR_COMM_TIMEOUT (0)
|
|
#define PROTO_SPG_ERR_INVALID_DATA (1)
|
|
#define PROTO_SPG_ERR_INVALID_DATA_LEN (2)
|
|
#define PROTO_SPG_ERR_VALIDATE_FAILED (3)
|
|
#define PROTO_SPG_ERR_INFO_TYPE_NOT_EXIST (4)
|
|
#define PROTO_SPG_ERR_INVALID_FORMAT (5)
|
|
#define PROTO_SPG_ERR_METER_DUPLICATED (6)
|
|
#define PROTO_SPG_ERR_METER_NOT_EXIST (7)
|
|
#define PROTO_SPG_ERR_METER_APP_NO_RSP (8)
|
|
#define PROTO_SPG_ERR_MASTER_IS_BUSY (9)
|
|
#define PROTO_SPG_ERR_MASTER_UNSUPPORTED_CMD (10)
|
|
#define PROTO_SPG_ERR_SLAVE_NO_RSP (11)
|
|
#define PROTO_SPG_ERR_SLAVE_NOT_IN_SPG (12)
|
|
#define PROTO_SPG_ERR_TASK_ID_SPACE_INSUFFICIENT (13)
|
|
#define PROTO_SPG_ERR_TASK_ID_NOT_EXIST (14)
|
|
#define PROTO_SPG_ERR_TASK_ID_DUPLICATED (15)
|
|
#define PROTO_SPG_ERR_DEV_NO_SUCH_TASK_ID (16)
|
|
#define PROTO_SPG_ERR_NO_SUCH_TASK_ID (17)
|
|
#define PROTO_SPG_ERR_PARAM_INVALID (18)
|
|
#define PROTO_SPG_ERR_OTHER (0xFF)
|
|
|
|
/* 1 byte checksum , and 1 byte backcode */
|
|
#define PROTO_SPG_CS_BCODE_LEN (2)
|
|
|
|
/* meter data max len */
|
|
#define PROTO_SPG_MAX_MR_DATA_LEN (0xFF)
|
|
|
|
/* protocol type */
|
|
#define PROTO_SPG_PROTO_TYPE_UNKNOWN (0)
|
|
#define PROTO_SPG_PROTO_TYPE_645_1997 (1)
|
|
#define PROTO_SPG_PROTO_TYPE_645_2007 (2)
|
|
#define PROTO_SPG_PROTO_TYPE_CJ_T188 (3)
|
|
#define PROTO_SPG_PROTO_TYPE_698_45 (4)
|
|
|
|
/* spg supported segment size for transferring file */
|
|
#define PROTO_SPG_UPGRADE_FILE_SEG_SIZE1 (64)
|
|
#define PROTO_SPG_UPGRADE_FILE_SEG_SIZE2 (128)
|
|
#define PROTO_SPG_UPGRADE_FILE_SEG_SIZE3 (256)
|
|
#define PROTO_SPG_UPGRADE_FILE_SEG_SIZE4 (512)
|
|
#define PROTO_SPG_UPGRADE_FILE_SEG_SIZE5 (1024)
|
|
|
|
/* sec node module type */
|
|
#define PROTO_SPG_MODULE_COLLECTOR (0)
|
|
#define PROTO_SPG_MODULE_POWER_METER (1)
|
|
#define PROTO_SPG_MODULE_POWER_METER_3P (2)
|
|
#define PROTO_SPG_MODULE_COLLECTOR_METER (3)
|
|
|
|
/* preamble code length */
|
|
#define PROTO_SPG_PREAMBLE_LEN 4
|
|
|
|
#define PROTO_SPG_COMM_DELAY (1) // comm delay in seconds
|
|
|
|
/* nid length */
|
|
#define PROTO_SPG_NID_LEN (3)
|
|
|
|
/* address field used flag */
|
|
#define SPG_USED_ADDR (1)
|
|
#define SPG_NO_USED_ADDR (0)
|
|
|
|
/* max report node count */
|
|
#define PROTO_SPG_MAX_RPT_NODE_CNT (64)
|
|
/* max query topo node count */
|
|
#define PROTO_SPG_TOPO_QR_MAX_CNT (64)
|
|
/* max report reject black list count */
|
|
#define PROTO_SPG_REJECT_NODE_MAX_CNT (20)
|
|
|
|
/* define single phase meter zc state */
|
|
#define PROTO_SPG_SINGLE_PHASE_ZC_NOSUPP (0)
|
|
#define PROTO_SPG_SINGLE_PHASE_ZC_SUPP (1)
|
|
|
|
/* define node three phase zc state */
|
|
#define PROTO_SPG_THREE_PHASE_ZC_UNKNOWN (0)
|
|
#define PROTO_SPG_THREE_PHASE_ZC_ABC (1)
|
|
#define PROTO_SPG_THREE_PHASE_ZC_ERR (2)
|
|
#define PROTO_SPG_THREE_PHASE_ZC_DISC (3)
|
|
#define PROTO_SPG_THREE_PHASE_ZC_SAME (4)
|
|
/* define node reset reason */
|
|
#define PROTO_SPG_RUN_RESET_REASON_POWER (0)
|
|
#define PROTO_SPG_RUN_RESET_REASON_GPIO (1)
|
|
#define PROTO_SPG_RUN_RESET_REASON_UPGRADE (2)
|
|
#define PROTO_SPG_RUN_RESET_REASON_CCO (3)
|
|
|
|
typedef struct _proto_spg_ctrl {
|
|
uint8_t rsvd : 3; /* bit 0 - bit 2 */
|
|
uint8_t ver : 2; /* bit 3 - bit 4 */
|
|
uint8_t addr : 1; /* bit 5 */
|
|
uint8_t prm : 1; /* bit 6 */
|
|
uint8_t dir : 1; /* bit 7 */
|
|
} proto_spg_ctrl_t;
|
|
|
|
typedef struct _spg_afn00_fn01_conf {
|
|
/* wait timeout limit in second */
|
|
uint16_t wait_duration;
|
|
} spg_afn00_fn01_conf_t;
|
|
|
|
typedef struct _spg_afn00_fn02_reject {
|
|
uint8_t err_no;
|
|
} spg_afn00_fn02_reject_t;
|
|
|
|
typedef struct _module_spg_hw_info {
|
|
/* manufacture code */
|
|
uint16_t vendor_id;
|
|
/* chip code */
|
|
uint16_t chip_code;
|
|
/* day in BCD */
|
|
uint8_t day;
|
|
/* month in BCD */
|
|
uint8_t month;
|
|
/* year in BCD */
|
|
uint8_t year;
|
|
/* version in BCD */
|
|
uint8_t version[2];
|
|
}mod_spg_hw_info_t;
|
|
|
|
/* structure of SPG frame
|
|
* -------------
|
|
* |----68H----| start of frame, 0x68
|
|
* |----Len----| 2 byte of Len
|
|
* |----Ctrl---| 1 byte of control
|
|
* |----Data---| data. length is variable
|
|
* |----CS-----| checksum
|
|
* |----16H----| end of frame, 0x16
|
|
* -------------
|
|
*/
|
|
typedef struct _proto_spg_hdr {
|
|
uint8_t sof_byte;
|
|
uint16_t len;
|
|
proto_spg_ctrl_t ctrl_byte;
|
|
} proto_spg_hdr_t;
|
|
|
|
typedef struct _proto_spg_addr_field {
|
|
uint8_t src_mac[PROTO_SPG_MAC_ADDR_LEN];
|
|
uint8_t dest_mac[PROTO_SPG_MAC_ADDR_LEN];
|
|
} proto_spg_addr_field_t;
|
|
|
|
typedef struct _proto_spg_app_data {
|
|
uint8_t afn;
|
|
uint8_t seq;
|
|
uint8_t di[PROTO_SPG_DI_LEN];
|
|
uint8_t data[0];
|
|
} proto_spg_app_data_t;
|
|
|
|
typedef struct _proto_spg_app_tail {
|
|
uint8_t cs;
|
|
uint8_t sof_byte;
|
|
} proto_spg_tail_t;
|
|
|
|
typedef struct _proto_spg_collector_evt {
|
|
uint16_t data_len;
|
|
uint8_t data[0];
|
|
} proto_spg_collector_evt_t;
|
|
|
|
/* spg data length before app data */
|
|
#define PROTO_SPG_RSVD_LEN \
|
|
(sizeof(proto_spg_hdr_t) + sizeof(proto_spg_app_data_t))
|
|
|
|
/* @brief: get length of a spg proto packet.
|
|
* @param mac_used: mac used flag
|
|
* @param data_len: app data len in spg packet
|
|
* @return: length of the spg proto packet
|
|
*/
|
|
uint32_t proto_spg_get_proto_pkt_len(uint8_t mac_used, uint32_t data_len);
|
|
|
|
/* @brief: get length to reserve before app data in spg pkt.
|
|
* it's the sum of header length, app data length, tail
|
|
* length and extra headroom.
|
|
* @param mac_used: mac used flag.
|
|
* @param mac_used: app data len in spg proto packet
|
|
* @return: length of the spg UART packet
|
|
*/
|
|
uint32_t proto_spg_get_pkt_len(uint8_t mac_used, uint32_t data_len);
|
|
|
|
/* @brief: get length of the spg proto header
|
|
* @param mac_used: mac used flag 1-used, 0-no used
|
|
* @return: count of byte to be reserved.
|
|
*/
|
|
uint32_t proto_spg_get_header_len(uint8_t mac_used);
|
|
|
|
/* @brief: get length to reserve before app data in spg UART pkt
|
|
* it's consist of the extra uart packet and the spg
|
|
* proto header
|
|
* @param mac_used: mac used flag 1-used, 0-no used
|
|
* @return: count of byte to be reserved.
|
|
*/
|
|
uint32_t proto_spg_get_rsvd_len(uint8_t mac_used);
|
|
|
|
/**
|
|
* @brief: check if a mac is spg broadcast mac
|
|
* @param dst: the mac address to be checked
|
|
* @return 1 is spg broadcast mac
|
|
* @return 0 NOT a spg broadcast mac
|
|
*/
|
|
uint8_t proto_spg_is_bcast(uint8_t* dst);
|
|
|
|
/**
|
|
* @brief: calculate the check sum according, to spg protocol
|
|
* @param data: data to calculate checksum.
|
|
* @param len: length of data.
|
|
* @return: the checksum.
|
|
*/
|
|
uint8_t proto_spg_get_checksum(uint8_t *data, uint32_t len);
|
|
|
|
/**
|
|
* @brief proto_spg_nid_to_buf() - convert uint32_t's nid to 3 bytes
|
|
* @param nid: the value of uint32_t's network id
|
|
* @param p_nid: output the pointer of 3 bytes buf
|
|
*/
|
|
void proto_spg_nid_to_buf(uint32_t nid, uint8_t *p_nid);
|
|
|
|
/**
|
|
* @brief: fill data in spg protocol frame.
|
|
* @param frames_filled: pointer to the spg protocol frame that needs
|
|
* @param to be filled.
|
|
* @param role: message direction.
|
|
* @param afn: fnction code type.
|
|
* @param fn: fnction code subtype.
|
|
* @param di2: uplink and downlink types.
|
|
* @param di3: communication type.
|
|
* @param seq: sequence number.
|
|
* @param app_data: pointer to the spg protocol frame data field.
|
|
* @param data_len: length of frame data field.
|
|
* @param mac_buf: application address domain.
|
|
* @param mac_cnt: address count of application address domain.
|
|
* @param dir: transmission direction bit.
|
|
* @return: spg protocol frame length.
|
|
*/
|
|
uint16_t proto_spg_fill_frame(uint8_t *frames_filled, uint8_t role, uint8_t afn,
|
|
uint8_t fn, uint8_t di2, uint8_t di3, uint8_t seq, uint8_t *app_data,
|
|
uint16_t data_len, uint8_t *mac_buf, uint8_t mac_cnt, uint8_t dir);
|
|
|
|
/**
|
|
* @brief: alloc spg protocol frame.
|
|
* @param role: message direction.
|
|
* @param afn: fnction code type.
|
|
* @param fn: fnction code subtype.
|
|
* @param di2: uplink and downlink types.
|
|
* @param di3: communication type.
|
|
* @param seq: sequence number.
|
|
* @param app_data: pointer to the spg protocol frame data field.
|
|
* @param len: length of frame data field.
|
|
* @param mac_buf: application address domain.
|
|
* @param mac_cnt: address count of application address domain.
|
|
* @param dir: transmission direction bit.
|
|
* @return: NULL -- for failure case.
|
|
* @return: pkt -- the iot pkt buffer for spg protocol.
|
|
*/
|
|
iot_pkt_t *proto_spg_alloc_frame(uint8_t role, uint8_t afn, uint8_t fn,
|
|
uint8_t di2, uint8_t di3, uint8_t seq, uint8_t *app_data, uint16_t len,
|
|
uint8_t *mac_buf, uint8_t mac_cnt, uint8_t dir);
|
|
|
|
/**
|
|
* @brief: check if a spg packet is a valid.
|
|
* @param data: packet data.
|
|
* @param len: length of the packet data.
|
|
* @param reason: reason of the no passed
|
|
* @param di3: communication type.
|
|
* return
|
|
* NULL -- spg format data not found.
|
|
* otherwise -- pointer to the spg protocol header
|
|
*/
|
|
uint8_t *proto_spg_sanity_check(uint8_t *data, uint16_t len, uint8_t *reason,
|
|
uint8_t di3);
|
|
|
|
/**
|
|
* @brief check whether the frame is legitimate
|
|
* @param buffer: the buffer containing spg data.
|
|
* @param buffer_len: len of the buffer.
|
|
* @param is_frame: false: the frame isn't legitimate; true: the frame is
|
|
* legitimate.
|
|
*/
|
|
void proto_spg_check_frame_handler(uint8_t* buffer, uint32_t buffer_len,
|
|
bool_t* is_frame);
|
|
|
|
/**
|
|
* @brief: set the dest mac as bcast addr
|
|
* @param dst: the mac address to be set
|
|
*/
|
|
void proto_spg_set_bcast(uint8_t *dst);
|
|
|
|
/**
|
|
* @brief get upgrade file block size
|
|
* @param file_size: upgrade file total size
|
|
* @param block_cnt: upgrade file total block count
|
|
* @retval: upgrade file block size
|
|
*/
|
|
uint32_t proto_spg_get_block_size(uint32_t file_size, uint16_t block_cnt);
|
|
|
|
extern uint8_t proto_spg_preamble[];
|
|
|
|
#pragma pack(pop) /* restore the pack status */
|
|
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* PROTO_SPG_H */
|